Changeset - r18300:87ebd90f3b7f
[Not reviewed]
master
0 7 0
frosch - 13 years ago 2011-11-08 17:26:13
frosch@openttd.org
(svn r23146) -Change: [NewGRF v8] Make callback 22 return a probability to use instead of property 18.
7 files changed with 33 insertions and 23 deletions:
0 comments (0 inline, 0 general)
src/ai/api/ai_industrytype.cpp
Show inline comments
 
@@ -88,25 +88,25 @@
 
}
 

	
 
/* static */ bool AIIndustryType::CanBuildIndustry(IndustryType industry_type)
 
{
 
	if (!IsValidIndustryType(industry_type)) return false;
 

	
 
	if (!::CheckIfCallBackAllowsAvailability(industry_type, IACT_USERCREATION)) return false;
 
	if (::GetIndustryProbabilityCallback(industry_type, IACT_USERCREATION, 1) == 0) return false;
 
	if (!::GetIndustrySpec(industry_type)->IsRawIndustry()) return true;
 

	
 
	/* raw_industry_construction == 1 means "Build as other industries" */
 
	return _settings_game.construction.raw_industry_construction == 1;
 
}
 

	
 
/* static */ bool AIIndustryType::CanProspectIndustry(IndustryType industry_type)
 
{
 
	if (!IsValidIndustryType(industry_type)) return false;
 

	
 
	if (!::GetIndustrySpec(industry_type)->IsRawIndustry()) return false;
 
	if (!::CheckIfCallBackAllowsAvailability(industry_type, IACT_USERCREATION)) return false;
 
	if (::GetIndustryProbabilityCallback(industry_type, IACT_USERCREATION, 1) == 0) return false;
 

	
 
	/* raw_industry_construction == 2 means "prospect" */
 
	return _settings_game.construction.raw_industry_construction == 2;
 
}
 

	
 
/* static */ bool AIIndustryType::BuildIndustry(IndustryType industry_type, TileIndex tile)
src/industry_cmd.cpp
Show inline comments
 
@@ -1774,13 +1774,13 @@ CommandCost CmdBuildIndustry(TileIndex t
 
	/* If the setting for raw-material industries is not on, you cannot build raw-material industries.
 
	 * Raw material industries are industries that do not accept cargo (at least for now) */
 
	if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
 
		return CMD_ERROR;
 
	}
 

	
 
	if (_game_mode != GM_EDITOR && !CheckIfCallBackAllowsAvailability(it, IACT_USERCREATION)) {
 
	if (_game_mode != GM_EDITOR && GetIndustryProbabilityCallback(it, IACT_USERCREATION, 1) == 0) {
 
		return CMD_ERROR;
 
	}
 

	
 
	Randomizer randomizer;
 
	randomizer.SetSeed(p2);
 
	uint16 random_initial_bits = GB(p2, 0, 16);
 
@@ -1873,15 +1873,15 @@ static Industry *CreateNewIndustry(TileI
 
 * @return Relative probability for the industry to appear.
 
 */
 
static uint32 GetScaledIndustryGenerationProbability(IndustryType it, bool *force_at_least_one)
 
{
 
	const IndustrySpec *ind_spc = GetIndustrySpec(it);
 
	uint32 chance = ind_spc->appear_creation[_settings_game.game_creation.landscape] * 16; // * 16 to increase precision
 
	if (!ind_spc->enabled || chance == 0 || ind_spc->num_table == 0 ||
 
			!CheckIfCallBackAllowsAvailability(it, IACT_MAPGENERATION) ||
 
			(_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY)) {
 
	if (!ind_spc->enabled || ind_spc->num_table == 0 ||
 
			(_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY) ||
 
			(chance = GetIndustryProbabilityCallback(it, IACT_MAPGENERATION, chance)) == 0) {
 
		*force_at_least_one = false;
 
		return 0;
 
	} else {
 
		/* We want industries appearing at coast to appear less often on bigger maps, as length of coast increases slower than map area.
 
		 * For simplicity we scale in both cases, though scaling the probabilities of all industries has no effect. */
 
		chance = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(chance) : ScaleByMapSize(chance);
 
@@ -1903,16 +1903,16 @@ static uint16 GetIndustryGamePlayProbabi
 
		*min_number = 0;
 
		return 0;
 
	}
 

	
 
	const IndustrySpec *ind_spc = GetIndustrySpec(it);
 
	byte chance = ind_spc->appear_ingame[_settings_game.game_creation.landscape];
 
	if (!ind_spc->enabled || chance == 0 || ind_spc->num_table == 0 ||
 
	if (!ind_spc->enabled || ind_spc->num_table == 0 ||
 
			((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) ||
 
			((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) ||
 
			!CheckIfCallBackAllowsAvailability(it, IACT_RANDOMCREATION)) {
 
			(chance = GetIndustryProbabilityCallback(it, IACT_RANDOMCREATION, chance)) == 0) {
 
		*min_number = 0;
 
		return 0;
 
	}
 
	*min_number = (ind_spc->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) ? 1 : 0;
 
	return chance;
 
}
src/industry_gui.cpp
Show inline comments
 
@@ -206,13 +206,13 @@ class BuildIndustryWindow : public Windo
 
	int selected_index;                         ///< index of the element in the matrix
 
	IndustryType selected_type;                 ///< industry corresponding to the above index
 
	uint16 callback_timer;                      ///< timer counter for callback eventual verification
 
	bool timer_enabled;                         ///< timer can be used
 
	uint16 count;                               ///< How many industries are loaded
 
	IndustryType index[NUM_INDUSTRYTYPES + 1];  ///< Type of industry, in the order it was loaded
 
	bool enabled[NUM_INDUSTRYTYPES + 1];        ///< availability state, coming from CBID_INDUSTRY_AVAILABLE (if ever)
 
	bool enabled[NUM_INDUSTRYTYPES + 1];        ///< availability state, coming from CBID_INDUSTRY_PROBABILITY (if ever)
 
	Scrollbar *vscroll;
 

	
 
	/** The offset for the text in the matrix. */
 
	static const int MATRIX_TEXT_OFFSET = 17;
 

	
 
	void SetupArrays()
 
@@ -244,13 +244,13 @@ class BuildIndustryWindow : public Windo
 
				if (_game_mode != GM_EDITOR && indsp->IsRawIndustry() && _settings_game.construction.raw_industry_construction == 0) {
 
					/* Unselect if the industry is no longer in the list */
 
					if (this->selected_type == ind) this->selected_index = -1;
 
					continue;
 
				}
 
				this->index[this->count] = ind;
 
				this->enabled[this->count] = (_game_mode == GM_EDITOR) || CheckIfCallBackAllowsAvailability(ind, IACT_USERCREATION);
 
				this->enabled[this->count] = (_game_mode == GM_EDITOR) || GetIndustryProbabilityCallback(ind, IACT_USERCREATION, 1) > 0;
 
				/* Keep the selection to the correct line */
 
				if (this->selected_type == ind) this->selected_index = this->count;
 
				this->count++;
 
			}
 
		}
 

	
 
@@ -579,13 +579,13 @@ public:
 
			 * See if we need to update availability of currently selected industry */
 
			this->callback_timer = DAY_TICKS; // restart counter
 

	
 
			const IndustrySpec *indsp = GetIndustrySpec(this->selected_type);
 

	
 
			if (indsp->enabled) {
 
				bool call_back_result = CheckIfCallBackAllowsAvailability(this->selected_type, IACT_USERCREATION);
 
				bool call_back_result = GetIndustryProbabilityCallback(this->selected_type, IACT_USERCREATION, 1) > 0;
 

	
 
				/* Only if result does match the previous state would it require a redraw. */
 
				if (call_back_result != this->enabled[this->selected_index]) {
 
					this->enabled[this->selected_index] = call_back_result;
 
					this->SetButtons();
 
					this->SetDirty();
src/newgrf_callbacks.h
Show inline comments
 
@@ -82,14 +82,14 @@ enum CallbackID {
 
	/** Called to indicate how long the current animation frame should last. */
 
	CBID_HOUSE_ANIMATION_SPEED           = 0x20, // 8 bit callback
 

	
 
	/** Called periodically to determine if a house should be destroyed. */
 
	CBID_HOUSE_DESTRUCTION               = 0x21, // 8 bit callback
 

	
 
	/** Called to determine if the given industry type is available */
 
	CBID_INDUSTRY_AVAILABLE              = 0x22, // 15 bit callback
 
	/** Called to determine if the given industry type is available. For grf version >= 8 also a probability can be returned. */
 
	CBID_INDUSTRY_PROBABILITY            = 0x22, // 15 bit callback
 

	
 
	/**
 
	 * 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,
 
@@ -338,13 +338,13 @@ enum CargoCallbackMask {
 
};
 

	
 
/**
 
 * Callback masks for Industries
 
 */
 
enum IndustryCallbackMask {
 
	CBM_IND_AVAILABLE                 =  0, ///< industry availability callback
 
	CBM_IND_PROBABILITY               =  0, ///< industry availability/probability callback
 
	CBM_IND_PRODUCTION_CARGO_ARRIVAL  =  1, ///< call production callback when cargo arrives at the industry
 
	CBM_IND_PRODUCTION_256_TICKS      =  2, ///< call production callback every 256 ticks
 
	CBM_IND_LOCATION                  =  3, ///< check industry construction on given area
 
	CBM_IND_PRODUCTION_CHANGE         =  4, ///< controls random production change
 
	CBM_IND_MONTHLYPROD_CHANGE        =  5, ///< controls monthly random production change
 
	CBM_IND_CARGO_SUFFIX              =  6, ///< cargo sub-type display
src/newgrf_industries.cpp
Show inline comments
 
@@ -547,28 +547,38 @@ CommandCost CheckIfCallBackAllowsCreatio
 
	if (result == CALLBACK_FAILED) return CommandCost();
 

	
 
	return GetErrorMessageFromLocationCallbackResult(result, indspec->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
 
}
 

	
 
/**
 
 * Check with callback #CBM_IND_AVAILABLE whether the industry can be built.
 
 * Check with callback #CBID_INDUSTRY_PROBABILITY whether the industry can be built.
 
 * @param type Industry type to check.
 
 * @param creation_type Reason to construct a new industry.
 
 * @return If the industry has no callback or allows building, \c true is returned. Otherwise, \c false is returned.
 
 */
 
bool CheckIfCallBackAllowsAvailability(IndustryType type, IndustryAvailabilityCallType creation_type)
 
uint32 GetIndustryProbabilityCallback(IndustryType type, IndustryAvailabilityCallType creation_type, uint32 default_prob)
 
{
 
	const IndustrySpec *indspec = GetIndustrySpec(type);
 

	
 
	if (HasBit(indspec->callback_mask, CBM_IND_AVAILABLE)) {
 
		uint16 res = GetIndustryCallback(CBID_INDUSTRY_AVAILABLE, 0, creation_type, NULL, type, INVALID_TILE);
 
	if (HasBit(indspec->callback_mask, CBM_IND_PROBABILITY)) {
 
		uint16 res = GetIndustryCallback(CBID_INDUSTRY_PROBABILITY, 0, creation_type, NULL, type, INVALID_TILE);
 
		if (res != CALLBACK_FAILED) {
 
			return (res == 0);
 
			if (indspec->grf_prop.grffile->grf_version < 8) {
 
				/* Disallow if result != 0 */
 
				if (res != 0) default_prob = 0;
 
			} else {
 
				/* Use returned probability. 0x100 to use default */
 
				if (res < 0x100) {
 
					default_prob = res;
 
				} else if (res > 0x100) {
 
					ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_PROBABILITY, res);
 
				}
 
			}
 
		}
 
	}
 
	return true;
 
	return default_prob;
 
}
 

	
 
static int32 DerefIndProd(int field, bool use_register)
 
{
 
	return use_register ? (int32)GetRegister(field) : field;
 
}
src/newgrf_industries.h
Show inline comments
 
@@ -23,13 +23,13 @@ enum IndustryTrigger {
 
	/** Triggered (whole industry) each 256 ticks */
 
	INDUSTRY_TRIGGER_256_TICKS        = 2,
 
	/** Triggered on cargo delivery */
 
	INDUSTRY_TRIGGER_CARGO_DELIVERY   = 4,
 
};
 

	
 
/** From where is callback CBID_INDUSTRY_AVAILABLE been called */
 
/** From where is callback CBID_INDUSTRY_PROBABILITY been called */
 
enum IndustryAvailabilityCallType {
 
	IACT_MAPGENERATION,    ///< during random map generation
 
	IACT_RANDOMCREATION,   ///< during creation of random ingame industry
 
	IACT_USERCREATION,     ///< from the Fund/build window
 
	IACT_PROSPECTCREATION, ///< from the Fund/build using prospecting
 
};
 
@@ -37,13 +37,13 @@ enum IndustryAvailabilityCallType {
 
/* in newgrf_industry.cpp */
 
uint32 IndustryGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available);
 
uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile);
 
uint32 GetIndustryIDAtOffset(TileIndex new_tile, const Industry *i, uint32 cur_grfid);
 
void IndustryProductionCallback(Industry *ind, int reason);
 
CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type);
 
bool CheckIfCallBackAllowsAvailability(IndustryType type, IndustryAvailabilityCallType creation_type);
 
uint32 GetIndustryProbabilityCallback(IndustryType type, IndustryAvailabilityCallType creation_type, uint32 default_prob);
 
bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type);
 

	
 
IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id);
 

	
 
/* in newgrf_industrytiles.cpp*/
 
uint32 GetNearbyIndustryTileInformation(byte parameter, TileIndex tile, IndustryID index, bool signed_offsets = true);
src/table/newgrf_debug_data.h
Show inline comments
 
@@ -250,13 +250,13 @@ static const NIProperty _nip_industries[
 
	NIP(0x11, Industry, accepts_cargo[2],  NIT_CARGO, "accepted cargo 2"),
 
	NIP_END()
 
};
 

	
 
#define NICI(cb_id, bit) NIC(cb_id, IndustrySpec, callback_mask, bit)
 
static const NICallback _nic_industries[] = {
 
	NICI(CBID_INDUSTRY_AVAILABLE,            CBM_IND_AVAILABLE),
 
	NICI(CBID_INDUSTRY_PROBABILITY,          CBM_IND_PROBABILITY),
 
	NICI(CBID_INDUSTRY_LOCATION,             CBM_IND_LOCATION),
 
	NICI(CBID_INDUSTRY_PRODUCTION_CHANGE,    CBM_IND_PRODUCTION_CHANGE),
 
	NICI(CBID_INDUSTRY_MONTHLYPROD_CHANGE,   CBM_IND_MONTHLYPROD_CHANGE),
 
	NICI(CBID_INDUSTRY_CARGO_SUFFIX,         CBM_IND_CARGO_SUFFIX),
 
	NICI(CBID_INDUSTRY_FUND_MORE_TEXT,       CBM_IND_FUND_MORE_TEXT),
 
	NICI(CBID_INDUSTRY_WINDOW_MORE_TEXT,     CBM_IND_WINDOW_MORE_TEXT),
0 comments (0 inline, 0 general)