Changeset - r7283:49a2e04751af
[Not reviewed]
master
0 2 0
belugas - 17 years ago 2007-07-19 01:47:32
belugas@openttd.org
(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.
2 files changed with 37 insertions and 1 deletions:
0 comments (0 inline, 0 general)
src/industry_gui.cpp
Show inline comments
 
@@ -21,12 +21,13 @@
 
#include "helpers.hpp"
 
#include "cargotype.h"
 
#include "newgrf.h"
 
#include "newgrf_callbacks.h"
 
#include "newgrf_industries.h"
 
#include "newgrf_text.h"
 
#include "date.h"
 

	
 
extern Industry *CreateNewIndustry(TileIndex tile, IndustryType type);
 

	
 
/**
 
 * Search callback function for TryBuildIndustry
 
 * @param tile to test
 
@@ -59,20 +60,23 @@ enum {
 
};
 

	
 
/** Attached struct to the window extended data */
 
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));
 

	
 
/** Helper struct holding the available industries for current situation */
 
static struct IndustryData {
 
	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)
 
{
 
	switch (e->event) {
 
		case WE_CREATE: {
 
@@ -88,48 +92,56 @@ static void BuildDynamicIndustryWndProc(
 
				w->widget[DYNA_INDU_FUND_WIDGET].top -= 44;
 
				w->widget[DYNA_INDU_RESIZE_WIDGET].bottom -= 44;
 
				w->widget[DYNA_INDU_RESIZE_WIDGET].top -= 44;
 
				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
 
			w->resize.step_height = 13;
 

	
 
			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
 
			 * primary ones. Each loop will fill the _fund_gui structure. */
 
			for (ind = IT_COAL_MINE; ind < NUM_INDUSTRYTYPES; ind++) {
 
				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++;
 
				}
 
			}
 

	
 
			if (_patches.raw_industry_construction != 0 && _game_mode != GM_EDITOR) {
 
				for (ind = IT_COAL_MINE; ind < NUM_INDUSTRYTYPES; ind++) {
 
					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: {
 
			const IndustrySpec *indsp = (WP(w, fnd_d).select == INVALID_INDUSTRYTYPE) ? NULL : GetIndustrySpec(WP(w, fnd_d).select);
 
			StringID str = STR_4827_REQUIRES;
 
			int x_str = w->widget[DYNA_INDU_INFOPANEL].left + 3;
 
@@ -137,16 +149,21 @@ static void BuildDynamicIndustryWndProc(
 
			const Widget *wi = &w->widget[DYNA_INDU_INFOPANEL];
 
			int max_width = wi->right - wi->left - 4;
 

	
 
			/* 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);
 

	
 
			DrawWindowWidgets(w);
 

	
 
			/* and now with the matrix painting */
 
@@ -305,12 +322,31 @@ static void BuildDynamicIndustryWndProc(
 
			} else DoCommandP(e->we.place.tile, WP(w, fnd_d).select, 0, NULL, CMD_BUILD_INDUSTRY | CMD_MSG(STR_4830_CAN_T_CONSTRUCT_THIS_INDUSTRY));
 

	
 
			/* Whatever the outcome of the actions, just reset the cursor and the system */
 
			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:
 
		case WE_ABORT_PLACE_OBJ:
 
			RaiseWindowButtons(w);
 
			break;
src/newgrf_callbacks.h
Show inline comments
 
@@ -72,13 +72,13 @@ enum CallbackID {
 
	CBID_HOUSE_ANIMATION_SPEED           = 0x20,
 

	
 
	/** Called periodically to determine if a house should be destroyed. */
 
	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. */
 
	CBID_VEHICLE_ADDITIONAL_TEXT         = 0x23,
 

	
 
	/** Called when building a station to customize the tile layout */
0 comments (0 inline, 0 general)