(svn r11193) -Fix: be more compliant with the specifications of callback #2F (and undocumented side effects in TTDP in corner cases).
authorrubidium
Tue, 02 Oct 2007 16:56:45 +0000
changeset 7662 b9da9d79a9a9
parent 7661 3c1f788966ec
child 7663 798fd9251f8e
(svn r11193) -Fix: be more compliant with the specifications of callback #2F (and undocumented side effects in TTDP in corner cases).
src/industry_cmd.cpp
src/newgrf_industrytiles.cpp
src/newgrf_industrytiles.h
--- a/src/industry_cmd.cpp	Tue Oct 02 01:12:06 2007 +0000
+++ b/src/industry_cmd.cpp	Tue Oct 02 16:56:45 2007 +0000
@@ -1179,7 +1179,23 @@
 	return t;
 }
 
-static bool CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTileTable *it, int type, bool *custom_shape_check = NULL)
+bool IsSlopeRefused(Slope current, Slope refused)
+{
+	if (current != SLOPE_FLAT) {
+		if (refused & SLOPE_STEEP) return true;
+
+		Slope t = ComplementSlope(current);
+
+		if (refused & 1 && (t & SLOPE_NW)) return false;
+		if (refused & 2 && (t & SLOPE_NE)) return false;
+		if (refused & 4 && (t & SLOPE_SW)) return false;
+		if (refused & 8 && (t & SLOPE_SE)) return false;
+	}
+
+	return false;
+}
+
+static bool CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTileTable *it, uint itspec_index, int type, bool *custom_shape_check = NULL)
 {
 	_error_message = STR_0239_SITE_UNSUITABLE;
 
@@ -1207,7 +1223,7 @@
 
 			if (HASBIT(its->callback_flags, CBM_INDT_SHAPE_CHECK)) {
 				if (custom_shape_check != NULL) *custom_shape_check = true;
-				if (!PerformIndustryTileSlopeCheck(cur_tile, its, type, gfx)) return false;
+				if (!PerformIndustryTileSlopeCheck(tile, cur_tile, its, type, gfx, itspec_index)) return false;
 			} else {
 				if (ind_behav & INDUSTRYBEH_BUILT_ONWATER) {
 					/* As soon as the tile is not water, bail out.
@@ -1226,19 +1242,7 @@
 						/* It is almost impossible to have a fully flat land in TG, so what we
 						*  do is that we check if we can make the land flat later on. See
 						*  CheckIfCanLevelIndustryPlatform(). */
-						if (tileh != SLOPE_FLAT) {
-							Slope t;
-							byte bits = its->slopes_refused;
-
-							if (bits & 0x10) return false;
-
-							t = ComplementSlope(tileh);
-
-							if (bits & 1 && (t & SLOPE_NW)) return false;
-							if (bits & 2 && (t & SLOPE_NE)) return false;
-							if (bits & 4 && (t & SLOPE_SW)) return false;
-							if (bits & 8 && (t & SLOPE_SE)) return false;
-						}
+						if (IsSlopeRefused(tileh, its->slopes_refused)) return false;
 					}
 				}
 			}
@@ -1541,7 +1545,7 @@
 	const IndustryTileTable *it = indspec->table[itspec_index];
 	bool custom_shape_check = false;
 
-	if (!CheckIfIndustryTilesAreFree(tile, it, type, &custom_shape_check)) return NULL;
+	if (!CheckIfIndustryTilesAreFree(tile, it, itspec_index, type, &custom_shape_check)) return NULL;
 
 	if (HASBIT(GetIndustrySpec(type)->callback_flags, CBM_IND_LOCATION)) {
 		if (!CheckIfCallBackAllowsCreation(tile, type, itspec_index)) return NULL;
@@ -1582,7 +1586,6 @@
 {
 	int num;
 	const IndustryTileTable * const *itt;
-	const IndustryTileTable *it;
 	const IndustrySpec *indspec;
 
 	SET_EXPENSES_TYPE(EXPENSES_OTHER);
@@ -1626,7 +1629,7 @@
 
 		do {
 			if (--num < 0) return_cmd_error(STR_0239_SITE_UNSUITABLE);
-		} while (!CheckIfIndustryTilesAreFree(tile, it = itt[num], p1));
+		} while (!CheckIfIndustryTilesAreFree(tile, itt[num], num, p1));
 
 		if (CreateNewIndustryHelper(tile, p1, flags, indspec, num) == NULL) return CMD_ERROR;
 	}
--- a/src/newgrf_industrytiles.cpp	Tue Oct 02 01:12:06 2007 +0000
+++ b/src/newgrf_industrytiles.cpp	Tue Oct 02 16:56:45 2007 +0000
@@ -247,18 +247,23 @@
 	}
 }
 
-bool PerformIndustryTileSlopeCheck(TileIndex tile, const IndustryTileSpec *its, IndustryType type, IndustryGfx gfx)
+extern bool IsSlopeRefused(Slope current, Slope refused);
+
+bool PerformIndustryTileSlopeCheck(TileIndex ind_base_tile, TileIndex ind_tile, const IndustryTileSpec *its, IndustryType type, IndustryGfx gfx, uint itspec_index)
 {
 	Industry ind;
-	ind.xy = 0;
+	ind.index = INVALID_INDUSTRY;
+	ind.xy = ind_base_tile;
 	ind.width = 0;
 	ind.type = type;
 
-	uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, 0, gfx, &ind, tile);
+	uint16 callback_res = GetIndustryTileCallback(CBID_INDTILE_SHAPE_CHECK, 0, itspec_index, gfx, &ind, ind_tile);
+	if (callback_res == CALLBACK_FAILED) {
+		return !IsSlopeRefused(GetTileSlope(ind_tile, NULL), its->slopes_refused);
+	}
 	if (its->grf_prop.grffile->grf_version < 7) {
 		return callback_res != 0;
 	}
-	if (callback_res == CALLBACK_FAILED) return false;
 
 	switch (callback_res) {
 		case 0x400: return true;
--- a/src/newgrf_industrytiles.h	Tue Oct 02 01:12:06 2007 +0000
+++ b/src/newgrf_industrytiles.h	Tue Oct 02 16:56:45 2007 +0000
@@ -15,7 +15,7 @@
 
 bool DrawNewIndustryTile(TileInfo *ti, Industry *i, IndustryGfx gfx, const IndustryTileSpec *inds);
 uint16 GetIndustryTileCallback(CallbackID callback, uint32 param1, uint32 param2, IndustryGfx gfx_id, Industry *industry, TileIndex tile);
-bool PerformIndustryTileSlopeCheck(TileIndex tile, const IndustryTileSpec *its, IndustryType type, IndustryGfx gfx);
+bool PerformIndustryTileSlopeCheck(TileIndex ind_base_tile, TileIndex ind_tile, const IndustryTileSpec *its, IndustryType type, IndustryGfx gfx, uint itspec_index);
 
 void AnimateNewIndustryTile(TileIndex tile);
 bool StartStopIndustryTileAnimation(TileIndex tile, IndustryAnimationTrigger iat, uint32 random = Random());