changeset 7283:3a2f5f6aa1c1 draft

(svn r10622) -Feature: Introduction of the CBID_INDUSTRY_AVAILABLE's handling, in fund industry window. In order to stay as much consistent with the specs, the fund window will perform (if required) the callback every game-day. TTDPatch performs the same call every second, but after discussions with Csaboka, we agreed that it was not required to be that intensive. So a game-day is plenty enough.
author belugas <belugas@openttd.org>
date Thu, 19 Jul 2007 01:47:32 +0000
parents 0be74e4c8378
children 82bf20b4e8b6
files src/industry_gui.cpp src/newgrf_callbacks.h
diffstat 2 files changed, 37 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/src/industry_gui.cpp
+++ b/src/industry_gui.cpp
@@ -24,6 +24,7 @@
 #include "newgrf_callbacks.h"
 #include "newgrf_industries.h"
 #include "newgrf_text.h"
+#include "date.h"
 
 extern Industry *CreateNewIndustry(TileIndex tile, IndustryType type);
 
@@ -62,6 +63,8 @@
 struct fnd_d {
 	int index;             ///< index of the element in the matrix
 	IndustryType select;   ///< industry corresponding to the above index
+	uint16 callback_timer; ///< timer counter for callback eventual verification
+	bool timer_enabled;    ///< timer can be used
 };
 assert_compile(WINDOW_CUSTOM_SIZE >= sizeof(fnd_d));
 
@@ -70,6 +73,7 @@
 	uint16 count;                               ///< How many industries are loaded
 	IndustryType index[NUM_INDUSTRYTYPES + 1];  ///< Type of industry, in the order it was loaded
 	StringID text[NUM_INDUSTRYTYPES + 1];       ///< Text coming from CBM_IND_FUND_MORE_TEXT (if ever)
+	bool enabled[NUM_INDUSTRYTYPES + 1];        ///< availability state, coming from CBID_INDUSTRY_AVAILABLE (if ever)
 } _fund_gui;
 
 static void BuildDynamicIndustryWndProc(Window *w, WindowEvent *e)
@@ -91,9 +95,12 @@
 				w->resize.height = w->height -= 44;
 			}
 
+			WP(w, fnd_d).timer_enabled = _loaded_newgrf_features.has_newindustries;
+
 			/* Initilialize structures */
 			memset(&_fund_gui.index, 0xFF, NUM_INDUSTRYTYPES);
 			memset(&_fund_gui.text, STR_NULL, NUM_INDUSTRYTYPES);
+			memset(&_fund_gui.enabled, false, NUM_INDUSTRYTYPES);
 			_fund_gui.count = 0;
 
 			w->vscroll.cap = 8; // rows in grid, same in scroller
@@ -102,6 +109,7 @@
 			if (_game_mode == GM_EDITOR) { // give room for the Many Random "button"
 				_fund_gui.index[_fund_gui.count] = INVALID_INDUSTRYTYPE;
 				_fund_gui.count++;
+				WP(w, fnd_d).timer_enabled = false;
 			}
 
 			/* We'll perform two distinct loops, one for secondary industries, and the other one for
@@ -110,6 +118,7 @@
 				indsp = GetIndustrySpec(ind);
 				if (indsp->enabled && (!indsp->IsRawIndustry() || _game_mode == GM_EDITOR)) {
 					_fund_gui.index[_fund_gui.count] = ind;
+					_fund_gui.enabled[_fund_gui.count] = CheckIfCallBackAllowsAvailability(ind, IACT_USERCREATION);
 					_fund_gui.count++;
 				}
 			}
@@ -119,14 +128,17 @@
 					indsp = GetIndustrySpec(ind);
 					if (indsp->enabled && indsp->IsRawIndustry()) {
 						_fund_gui.index[_fund_gui.count] = ind;
+						_fund_gui.enabled[_fund_gui.count] = CheckIfCallBackAllowsAvailability(ind, IACT_USERCREATION);
 						_fund_gui.count++;
 					}
 				}
 			}
+
 			/* first indutry type is selected.
 			 * I'll be damned if there are none available ;) */
 			WP(w, fnd_d).index = 0;
 			WP(w, fnd_d).select = _fund_gui.index[0];
+			WP(w, fnd_d).callback_timer = DAY_TICKS;
 		} break;
 
 		case WE_PAINT: {
@@ -140,10 +152,15 @@
 			/* Raw industries might be prospected. Show this fact by changing the string
 			 * In Editor, you just build, while ingame, or you fund or you prospect */
 			if (_game_mode == GM_EDITOR) {
+				/* We've chosen many random industries but no industries have been specified */
+				if (indsp == NULL && _patches.raw_industry_construction == 0) {
+					_fund_gui.enabled[WP(w, fnd_d).index] = false;
+				}
 				w->widget[DYNA_INDU_FUND_WIDGET].data = STR_BUILD_NEW_INDUSTRY;
 			} else {
 				w->widget[DYNA_INDU_FUND_WIDGET].data = (_patches.raw_industry_construction == 2 && indsp->IsRawIndustry()) ? STR_PROSPECT_NEW_INDUSTRY : STR_FUND_NEW_INDUSTRY;
 			}
+			SetWindowWidgetDisabledState(w, DYNA_INDU_FUND_WIDGET, !_fund_gui.enabled[WP(w, fnd_d).index]);
 
 			SetVScrollCount(w, _fund_gui.count);
 
@@ -308,6 +325,25 @@
 			ResetObjectToPlace();
 		} break;
 
+		case WE_TICK:
+			if (!WP(w, fnd_d).timer_enabled) break;
+			if (--WP(w, fnd_d).callback_timer == 0) {
+				/* We have just passed another day.
+				 * See if we need to update availability of currently selected industry */
+				WP(w, fnd_d).callback_timer = DAY_TICKS;  //restart counter
+
+				const IndustrySpec *indsp = GetIndustrySpec(WP(w, fnd_d).select);
+
+				if (indsp->enabled) {
+					bool call_back_result = CheckIfCallBackAllowsAvailability(WP(w, fnd_d).select, IACT_USERCREATION);
+
+					/* Only if result does match the previous state would it require a redraw. */
+					if (call_back_result != _fund_gui.enabled[WP(w, fnd_d).index]) {
+						_fund_gui.enabled[WP(w, fnd_d).index] = call_back_result;
+						SetWindowDirty(w);
+					}
+				}
+			}
 			break;
 
 		case WE_TIMEOUT:
--- a/src/newgrf_callbacks.h
+++ b/src/newgrf_callbacks.h
@@ -75,7 +75,7 @@
 	CBID_HOUSE_DESTRUCTION               = 0x21,
 
 	/** Called to determine if the given industry type is available */
-	CBID_INDUSTRY_AVAILABLE              = 0x22, // not implemented
+	CBID_INDUSTRY_AVAILABLE              = 0x22,
 
 	/** This callback is called from vehicle purchase lists. It returns a value to be
 	 * used as a custom string ID in the 0xD000 range. */