changeset 14608:94969f662ceb draft

(svn r19185) -Codechange: Return succeeded or failed CommandCost from CheckIfCallBackAllowsCreation().
author alberth <alberth@openttd.org>
date Sun, 21 Feb 2010 17:01:23 +0000
parents 473d68383c30
children 69c586c53c78
files src/industry_cmd.cpp src/newgrf_industries.cpp src/newgrf_industries.h
diffstat 3 files changed, 21 insertions(+), 14 deletions(-) [+]
line wrap: on
line diff
--- a/src/industry_cmd.cpp
+++ b/src/industry_cmd.cpp
@@ -1708,12 +1708,12 @@
 	if (ret.Failed()) return NULL;
 
 	if (HasBit(GetIndustrySpec(type)->callback_mask, CBM_IND_LOCATION)) {
-		if (!CheckIfCallBackAllowsCreation(tile, type, itspec_index, seed)) return NULL;
+		ret = CheckIfCallBackAllowsCreation(tile, type, itspec_index, seed);
 	} else {
 		ret = _check_new_industry_procs[indspec->check_proc](tile);
-		ret.SetGlobalErrorMessage();
-		if (ret.Failed()) return NULL;
 	}
+	ret.SetGlobalErrorMessage();
+	if (ret.Failed()) return NULL;
 
 	if (!custom_shape_check && _settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !_ignore_restrictions && !CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER, it, type)) return NULL;
 	ret = CheckIfFarEnoughFromIndustry(tile, type);
--- a/src/newgrf_industries.cpp
+++ b/src/newgrf_industries.cpp
@@ -448,7 +448,14 @@
 	return IndustryGetVariable(object, variable, parameter, available);
 }
 
-bool CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint itspec_index, uint32 seed)
+/** Check that the industry callback allows creation of the industry.
+ * @param tile %Tile to build the industry.
+ * @param type Type of industry to build.
+ * @param layout Layout number.
+ * @param seed Seed for the random generator.
+ * @return Succeeded or failed command.
+ */
+CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed)
 {
 	const IndustrySpec *indspec = GetIndustrySpec(type);
 
@@ -460,7 +467,7 @@
 	ind.location.tile = tile;
 	ind.location.w = 0;
 	ind.type = type;
-	ind.selected_layout = itspec_index;
+	ind.selected_layout = layout;
 	ind.town = ClosestTownFromTile(tile, UINT_MAX);
 
 	NewIndustryResolver(&object, tile, &ind, type);
@@ -472,9 +479,9 @@
 
 	/* Unlike the "normal" cases, not having a valid result means we allow
 	 * the building of the industry, as that's how it's done in TTDP. */
-	if (group == NULL) return true;
+	if (group == NULL) return CommandCost();
 	uint16 result = group->GetCallbackResult();
-	if (result == 0x400 || result == CALLBACK_FAILED) return true;
+	if (result == 0x400 || result == CALLBACK_FAILED) return CommandCost();
 
 	/* Copy some parameters from the registers to the error message text ref. stack */
 	SwitchToErrorRefStack();
@@ -482,13 +489,12 @@
 	SwitchToNormalRefStack();
 
 	switch (result) {
-		case 0x401: _error_message = STR_ERROR_SITE_UNSUITABLE; break;
-		case 0x402: _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST; break;
-		case 0x403: _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT; break;
-		default: _error_message = GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + result); break;
+		case 0x401: return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
+		case 0x402: return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST);
+		case 0x403: return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT);
+		default:    return_cmd_error(GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + result));
 	}
-
-	return false;
+	NOT_REACHED();
 }
 
 bool CheckIfCallBackAllowsAvailability(IndustryType type, IndustryAvailabilityCallType creation_type)
--- a/src/newgrf_industries.h
+++ b/src/newgrf_industries.h
@@ -12,6 +12,7 @@
 #ifndef NEWGRF_INDUSTRIES_H
 #define NEWGRF_INDUSTRIES_H
 
+#include "command_type.h"
 #include "newgrf_spritegroup.h"
 
 /** When should the industry(tile) be triggered for random bits? */
@@ -36,7 +37,7 @@
 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);
-bool CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint itspec_index, uint32 seed);
+CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed);
 bool CheckIfCallBackAllowsAvailability(IndustryType type, IndustryAvailabilityCallType creation_type);
 
 IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id);