src/newgrf_industries.cpp
branchgamebalance
changeset 9913 e79cd19772dd
parent 9912 1ac8aac92385
--- a/src/newgrf_industries.cpp	Wed Jun 13 12:05:56 2007 +0000
+++ b/src/newgrf_industries.cpp	Tue Jun 19 07:21:01 2007 +0000
@@ -8,6 +8,7 @@
 #include "functions.h"
 #include "macros.h"
 #include "industry.h"
+#include "industry_map.h"
 #include "newgrf.h"
 #include "newgrf_callbacks.h"
 #include "newgrf_spritegroup.h"
@@ -59,6 +60,51 @@
 	return best_dist;
 }
 
+/** Make an analysis of a tile and check for its belonging to the same
+ * industry, and/or the same grf file
+ * @param new_tile TileIndex of the tile to query
+ * @param old_tile TileINdex of teh reference tile
+ * @param i Industry to which old_tile belongs to
+ * @return value encoded as per NFO specs */
+uint32 GetIndustryIDAtOffset(TileIndex new_tile, TileIndex old_tile, const Industry *i)
+{
+	if (IsTileType(new_tile, MP_INDUSTRY)) {  // Is this an industry tile?
+
+		if (GetIndustryIndex(new_tile) == i->index) {  // Does it belong to the same industry?
+			IndustryGfx gfx = GetIndustryGfx(new_tile);
+			const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
+			const IndustryTileSpec *indold = GetIndustryTileSpec(GetIndustryGfx(old_tile));
+
+			if (gfx < NEW_INDUSTRYOFFSET) {  // Does it belongs to an old type?
+				/* It is an old tile.  We have to see if it's been overriden */
+				if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) {  // has it been overridden?
+					return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
+				} else { // yes.  FInd out if it is from the same grf file or not
+					const IndustryTileSpec *old_tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);
+
+					if (old_tile_ovr->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) {
+						return old_tile_ovr->grf_prop.local_id; // same grf file
+					} else {
+						return 0xFFFE; // not the same grf file
+					}
+				}
+			} else {
+				if (indtsp->grf_prop.spritegroup != NULL) {  // tile has a spritegroup ?
+					if (indtsp->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) {  // same industry, same grf ?
+						return indtsp->grf_prop.local_id;
+					} else {
+						return 0xFFFE;  // Defined in another grf file
+					}
+				} else {  // tile has no spritegroup
+					return 0xFF << 8 | indtsp->grf_prop.subst_id;  // so just give him the substitute
+				}
+			}
+		}
+	}
+
+	return 0xFFFF; // tile is not an industry one or  does not belong to the current industry
+}
+
 /** This function implements the industries variables that newGRF defines.
  * @param variable that is queried
  * @param parameter unused
@@ -85,16 +131,8 @@
 		/* TODO: somehow determine whether we're in water or not */
 		case 0x43: return GetClosestWaterDistance(tile, true); // Manhattan distance of closes dry/water tile
 
-		case 0x60: { /* Get industry ID at offset param */
-			/*The parameter of this variable is an offset from the northernmost tile of the industry:
-			 * the high nibble contains the Y offset, the low one the X offset; both are unsigned.
-			 * The high word of the return value is currently reserved, and the low word can be:
-			 * 00xxh if the tile is an industry tile and was defined in the current GRF with ID xx.
-			 * FFxxh if the tile is an industry tile of an old type, and has the ID xx.
-			 * FFFEh if the tile is an industry tile that was defined in another GRF file
-			 * FFFFh if the tile isn't an industry tile, or doesn't belong to the current industry */
-			return GetIndustry(GetNearbyTile(parameter, tile))->type;
-		}
+		/* Get industry ID at offset param */
+		case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, industry->xy), tile, industry);
 
 		case 0x61: return 0; // Get random tile bits at offset param
 
@@ -189,16 +227,17 @@
 	return NULL;
 }
 
-static void NewIndustryResolver(ResolverObject *res, IndustryType ind_id, TileIndex tile, Industry *indus)
+static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus)
 {
-	res->GetRandomBits = NULL;//IndustryTileGetRandomBits;
-	res->GetTriggers   = NULL;//IndustryTileGetTriggers;
-	res->SetTriggers   = NULL;//IndustryTileSetTriggers;
+	res->GetRandomBits = IndustryTileGetRandomBits;
+	res->GetTriggers   = IndustryTileGetTriggers;
+	res->SetTriggers   = IndustryTileSetTriggers;
 	res->GetVariable   = IndustryGetVariable;
 	res->ResolveReal   = IndustryResolveReal;
 
 	res->u.industry.tile = tile;
 	res->u.industry.ind  = indus;
+	res->u.industry.gfx  = INVALID_INDUSTRYTILE;
 
 	res->callback        = 0;
 	res->callback_param1 = 0;
@@ -213,7 +252,7 @@
 	ResolverObject object;
 	const SpriteGroup *group;
 
-	NewIndustryResolver(&object, industry->type, tile, industry);
+	NewIndustryResolver(&object, tile, industry);
 	object.callback = callback;
 	object.callback_param1 = param1;
 	object.callback_param2 = param2;