changeset 17860:89ea8ed98e38 draft

(svn r22656) -Codechange: Deduplicate the custom error message of the industry shape and location callbacks.
author michi_cc <michi_cc@openttd.org>
date Mon, 11 Jul 2011 16:32:19 +0000
parents d6f85eb5ec53
children 7f2e98164c7e
files src/newgrf_commons.cpp src/newgrf_commons.h src/newgrf_industries.cpp src/newgrf_industrytiles.cpp
diffstat 4 files changed, 32 insertions(+), 26 deletions(-) [+]
line wrap: on
line diff
--- a/src/newgrf_commons.cpp
+++ b/src/newgrf_commons.cpp
@@ -24,6 +24,9 @@
 #include "newgrf_object.h"
 #include "genworld.h"
 #include "newgrf_spritegroup.h"
+#include "newgrf_text.h"
+
+#include "table/strings.h"
 
 /**
  * Constructor of generic class
@@ -446,6 +449,30 @@
 	return tile_type << 24 | z << 16 | terrain_type << 8 | tileh;
 }
 
+/**
+ * Get the error message from a shape/location/slope check callback result.
+ * @param cb_res Callback result to translate. If bit 10 is set this is a standard error message, otherwise a NewGRF provided string.
+ * @param grfid grfID to use to resolve a custom error message.
+ * @param default_error Error message to use for the generic error.
+ * @return CommandCost indicating success or the error message.
+ */
+CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, uint32 grfid, StringID default_error)
+{
+	CommandCost res;
+	switch (cb_res) {
+		case 0x400: return res; // No error.
+		case 0x401: res = CommandCost(default_error); break;
+		case 0x402: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST); break;
+		case 0x403: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT); break;
+		default:    res = CommandCost(GetGRFStringID(grfid, 0xD000 + cb_res)); break;
+	}
+
+	/* Copy some parameters from the registers to the error message text ref. stack */
+	res.UseTextRefStack(4);
+
+	return res;
+}
+
 /* static */ SmallVector<DrawTileSeqStruct, 8> NewGRFSpriteLayout::result_seq;
 
 /**
--- a/src/newgrf_commons.h
+++ b/src/newgrf_commons.h
@@ -19,6 +19,7 @@
 #include "sprite.h"
 #include "core/alloc_type.hpp"
 #include "core/smallvec_type.hpp"
+#include "command_type.h"
 
 /** Context for tile accesses */
 enum TileContext {
@@ -267,6 +268,7 @@
 uint32 GetTerrainType(TileIndex tile, TileContext context = TCX_NORMAL);
 TileIndex GetNearbyTile(byte parameter, TileIndex tile, bool signed_offsets = true);
 uint32 GetNearbyTileInformation(TileIndex tile);
+CommandCost GetErrorMessageFromLocationCallbackResult(uint16 cb_res, uint32 grfid, StringID default_error);
 
 /**
  * Data related to the handling of grf files.
--- a/src/newgrf_industries.cpp
+++ b/src/newgrf_industries.cpp
@@ -544,20 +544,9 @@
 	 * the building of the industry, as that's how it's done in TTDP. */
 	if (group == NULL) return CommandCost();
 	uint16 result = group->GetCallbackResult();
-	if (result == 0x400 || result == CALLBACK_FAILED) return CommandCost();
+	if (result == CALLBACK_FAILED) return CommandCost();
 
-	CommandCost res;
-	switch (result) {
-		case 0x401: res = CommandCost(STR_ERROR_SITE_UNSUITABLE); break;
-		case 0x402: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST); break;
-		case 0x403: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT); break;
-		default:    res = CommandCost(GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + result)); break;
-	}
-
-	/* Copy some parameters from the registers to the error message text ref. stack */
-	res.UseTextRefStack(4);
-
-	return res;
+	return GetErrorMessageFromLocationCallbackResult(result, indspec->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
 }
 
 /**
--- a/src/newgrf_industrytiles.cpp
+++ b/src/newgrf_industrytiles.cpp
@@ -303,20 +303,8 @@
 		if (callback_res != 0) return CommandCost();
 		return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
 	}
-	if (callback_res == 0x400) return CommandCost();
 
-	CommandCost res;
-	switch (callback_res) {
-		case 0x401: res = CommandCost(STR_ERROR_SITE_UNSUITABLE); break;
-		case 0x402: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST); break;
-		case 0x403: res = CommandCost(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT); break;
-		default:    res = CommandCost(GetGRFStringID(its->grf_prop.grffile->grfid, 0xD000 + callback_res)); break;
-	}
-
-	/* Copy some parameters from the registers to the error message text ref. stack */
-	res.UseTextRefStack(4);
-
-	return res;
+	return GetErrorMessageFromLocationCallbackResult(callback_res, its->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
 }
 
 /* Simple wrapper for GetHouseCallback to keep the animation unified. */