src/newgrf_industries.cpp
branchgamebalance
changeset 9913 e79cd19772dd
parent 9912 1ac8aac92385
equal deleted inserted replaced
9912:1ac8aac92385 9913:e79cd19772dd
     6 #include "openttd.h"
     6 #include "openttd.h"
     7 #include "debug.h"
     7 #include "debug.h"
     8 #include "functions.h"
     8 #include "functions.h"
     9 #include "macros.h"
     9 #include "macros.h"
    10 #include "industry.h"
    10 #include "industry.h"
       
    11 #include "industry_map.h"
    11 #include "newgrf.h"
    12 #include "newgrf.h"
    12 #include "newgrf_callbacks.h"
    13 #include "newgrf_callbacks.h"
    13 #include "newgrf_spritegroup.h"
    14 #include "newgrf_spritegroup.h"
    14 #include "newgrf_industries.h"
    15 #include "newgrf_industries.h"
    15 #include "newgrf_commons.h"
    16 #include "newgrf_commons.h"
    57 	}
    58 	}
    58 
    59 
    59 	return best_dist;
    60 	return best_dist;
    60 }
    61 }
    61 
    62 
       
    63 /** Make an analysis of a tile and check for its belonging to the same
       
    64  * industry, and/or the same grf file
       
    65  * @param new_tile TileIndex of the tile to query
       
    66  * @param old_tile TileINdex of teh reference tile
       
    67  * @param i Industry to which old_tile belongs to
       
    68  * @return value encoded as per NFO specs */
       
    69 uint32 GetIndustryIDAtOffset(TileIndex new_tile, TileIndex old_tile, const Industry *i)
       
    70 {
       
    71 	if (IsTileType(new_tile, MP_INDUSTRY)) {  // Is this an industry tile?
       
    72 
       
    73 		if (GetIndustryIndex(new_tile) == i->index) {  // Does it belong to the same industry?
       
    74 			IndustryGfx gfx = GetIndustryGfx(new_tile);
       
    75 			const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
       
    76 			const IndustryTileSpec *indold = GetIndustryTileSpec(GetIndustryGfx(old_tile));
       
    77 
       
    78 			if (gfx < NEW_INDUSTRYOFFSET) {  // Does it belongs to an old type?
       
    79 				/* It is an old tile.  We have to see if it's been overriden */
       
    80 				if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) {  // has it been overridden?
       
    81 					return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
       
    82 				} else { // yes.  FInd out if it is from the same grf file or not
       
    83 					const IndustryTileSpec *old_tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);
       
    84 
       
    85 					if (old_tile_ovr->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) {
       
    86 						return old_tile_ovr->grf_prop.local_id; // same grf file
       
    87 					} else {
       
    88 						return 0xFFFE; // not the same grf file
       
    89 					}
       
    90 				}
       
    91 			} else {
       
    92 				if (indtsp->grf_prop.spritegroup != NULL) {  // tile has a spritegroup ?
       
    93 					if (indtsp->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) {  // same industry, same grf ?
       
    94 						return indtsp->grf_prop.local_id;
       
    95 					} else {
       
    96 						return 0xFFFE;  // Defined in another grf file
       
    97 					}
       
    98 				} else {  // tile has no spritegroup
       
    99 					return 0xFF << 8 | indtsp->grf_prop.subst_id;  // so just give him the substitute
       
   100 				}
       
   101 			}
       
   102 		}
       
   103 	}
       
   104 
       
   105 	return 0xFFFF; // tile is not an industry one or  does not belong to the current industry
       
   106 }
       
   107 
    62 /** This function implements the industries variables that newGRF defines.
   108 /** This function implements the industries variables that newGRF defines.
    63  * @param variable that is queried
   109  * @param variable that is queried
    64  * @param parameter unused
   110  * @param parameter unused
    65  * @param available will return false if ever the variable asked for does not exist
   111  * @param available will return false if ever the variable asked for does not exist
    66  * @param ind is of course the industry we are inquiring
   112  * @param ind is of course the industry we are inquiring
    83 			}
   129 			}
    84 		}
   130 		}
    85 		/* TODO: somehow determine whether we're in water or not */
   131 		/* TODO: somehow determine whether we're in water or not */
    86 		case 0x43: return GetClosestWaterDistance(tile, true); // Manhattan distance of closes dry/water tile
   132 		case 0x43: return GetClosestWaterDistance(tile, true); // Manhattan distance of closes dry/water tile
    87 
   133 
    88 		case 0x60: { /* Get industry ID at offset param */
   134 		/* Get industry ID at offset param */
    89 			/*The parameter of this variable is an offset from the northernmost tile of the industry:
   135 		case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, industry->xy), tile, industry);
    90 			 * the high nibble contains the Y offset, the low one the X offset; both are unsigned.
       
    91 			 * The high word of the return value is currently reserved, and the low word can be:
       
    92 			 * 00xxh if the tile is an industry tile and was defined in the current GRF with ID xx.
       
    93 			 * FFxxh if the tile is an industry tile of an old type, and has the ID xx.
       
    94 			 * FFFEh if the tile is an industry tile that was defined in another GRF file
       
    95 			 * FFFFh if the tile isn't an industry tile, or doesn't belong to the current industry */
       
    96 			return GetIndustry(GetNearbyTile(parameter, tile))->type;
       
    97 		}
       
    98 
   136 
    99 		case 0x61: return 0; // Get random tile bits at offset param
   137 		case 0x61: return 0; // Get random tile bits at offset param
   100 
   138 
   101 		case 0x62: // Land info of nearby tiles
   139 		case 0x62: // Land info of nearby tiles
   102 		case 0x63: // Animation stage of nerby tiles
   140 		case 0x63: // Animation stage of nerby tiles
   187 {
   225 {
   188 	/* IndustryTile do not have 'real' groups */
   226 	/* IndustryTile do not have 'real' groups */
   189 	return NULL;
   227 	return NULL;
   190 }
   228 }
   191 
   229 
   192 static void NewIndustryResolver(ResolverObject *res, IndustryType ind_id, TileIndex tile, Industry *indus)
   230 static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus)
   193 {
   231 {
   194 	res->GetRandomBits = NULL;//IndustryTileGetRandomBits;
   232 	res->GetRandomBits = IndustryTileGetRandomBits;
   195 	res->GetTriggers   = NULL;//IndustryTileGetTriggers;
   233 	res->GetTriggers   = IndustryTileGetTriggers;
   196 	res->SetTriggers   = NULL;//IndustryTileSetTriggers;
   234 	res->SetTriggers   = IndustryTileSetTriggers;
   197 	res->GetVariable   = IndustryGetVariable;
   235 	res->GetVariable   = IndustryGetVariable;
   198 	res->ResolveReal   = IndustryResolveReal;
   236 	res->ResolveReal   = IndustryResolveReal;
   199 
   237 
   200 	res->u.industry.tile = tile;
   238 	res->u.industry.tile = tile;
   201 	res->u.industry.ind  = indus;
   239 	res->u.industry.ind  = indus;
       
   240 	res->u.industry.gfx  = INVALID_INDUSTRYTILE;
   202 
   241 
   203 	res->callback        = 0;
   242 	res->callback        = 0;
   204 	res->callback_param1 = 0;
   243 	res->callback_param1 = 0;
   205 	res->callback_param2 = 0;
   244 	res->callback_param2 = 0;
   206 	res->last_value      = 0;
   245 	res->last_value      = 0;
   211 uint16 GetIndustryCallback(uint16 callback, uint32 param1, uint32 param2, Industry *industry, TileIndex tile)
   250 uint16 GetIndustryCallback(uint16 callback, uint32 param1, uint32 param2, Industry *industry, TileIndex tile)
   212 {
   251 {
   213 	ResolverObject object;
   252 	ResolverObject object;
   214 	const SpriteGroup *group;
   253 	const SpriteGroup *group;
   215 
   254 
   216 	NewIndustryResolver(&object, industry->type, tile, industry);
   255 	NewIndustryResolver(&object, tile, industry);
   217 	object.callback = callback;
   256 	object.callback = callback;
   218 	object.callback_param1 = param1;
   257 	object.callback_param1 = param1;
   219 	object.callback_param2 = param2;
   258 	object.callback_param2 = param2;
   220 
   259 
   221 	group = Resolve(GetIndustrySpec(industry->type)->grf_prop.spritegroup, &object);
   260 	group = Resolve(GetIndustrySpec(industry->type)->grf_prop.spritegroup, &object);