src/newgrf_industries.cpp
branchNewGRF_ports
changeset 6720 35756db7e577
child 6725 23339968083f
equal deleted inserted replaced
6719:4cc327ad39d5 6720:35756db7e577
       
     1 /* $Id$ */
       
     2 
       
     3 /** @file newgrf_industries.cpp */
       
     4 
       
     5 #include "stdafx.h"
       
     6 #include "openttd.h"
       
     7 #include "debug.h"
       
     8 #include "functions.h"
       
     9 #include "macros.h"
       
    10 #include "variables.h"
       
    11 #include "landscape.h"
       
    12 #include "table/strings.h"
       
    13 #include "industry.h"
       
    14 #include "industry_map.h"
       
    15 #include "newgrf.h"
       
    16 #include "newgrf_callbacks.h"
       
    17 #include "newgrf_spritegroup.h"
       
    18 #include "newgrf_industries.h"
       
    19 #include "newgrf_commons.h"
       
    20 #include "newgrf_text.h"
       
    21 #include "newgrf_town.h"
       
    22 #include "date.h"
       
    23 
       
    24 /* Since the industry IDs defined by the GRF file don't necessarily correlate
       
    25  * to those used by the game, the IDs used for overriding old industries must be
       
    26  * translated when the idustry spec is set. */
       
    27 IndustryOverrideManager _industry_mngr(NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, INVALID_INDUSTRYTYPE);
       
    28 IndustryTileOverrideManager _industile_mngr(NEW_INDUSTRYTILEOFFSET, NUM_INDUSTRYTILES, INVALID_INDUSTRYTILE);
       
    29 
       
    30 IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id)
       
    31 {
       
    32 	if (grf_type == IT_INVALID) return IT_INVALID;
       
    33 	if (!HASBIT(grf_type, 7)) return GB(grf_type, 0, 6);
       
    34 
       
    35 	return _industry_mngr.GetID(GB(grf_type, 0, 6), grf_id);
       
    36 }
       
    37 
       
    38 /**
       
    39  * Finds the distance for the closest tile with water/land given a tile
       
    40  * @param tile  the tile to find the distance too
       
    41  * @param water whether to find water or land
       
    42  * @note FAILS when an industry should be seen as water
       
    43  */
       
    44 static uint GetClosestWaterDistance(TileIndex tile, bool water)
       
    45 {
       
    46 	TileIndex t;
       
    47 	int best_dist;
       
    48 	for (t = 0; t < MapSize(); t++) {
       
    49 		if (IsTileType(t, MP_WATER) == water) break;
       
    50 	}
       
    51 	best_dist = DistanceManhattan(tile, t);
       
    52 
       
    53 	for (; t < MapSize(); t++) {
       
    54 		int dist = DistanceManhattan(tile, t);
       
    55 		if (dist < best_dist) {
       
    56 			if (IsTileType(t, MP_WATER) == water) best_dist = dist;
       
    57 		} else {
       
    58 			/* When the Y distance between the current row and the 'source' tile
       
    59 			 * is larger than the best distance, we've found the best distance */
       
    60 			if ((int)TileY(t) - (int)TileY(tile) > best_dist) break;
       
    61 			if (TileX(tile) > TileX(t)) {
       
    62 				/* We can safely skip this many tiles; from here all tiles have a
       
    63 				 * higher or equal distance than the best distance */
       
    64 				t |= MapMaxX();
       
    65 				continue;
       
    66 			} else {
       
    67 				/* We can safely skip this many tiles; up to here all tiles have a
       
    68 				 * higher or equal distance than the best distance */
       
    69 				t += max(best_dist - dist, 0);
       
    70 				continue;
       
    71 			}
       
    72 		}
       
    73 	}
       
    74 
       
    75 	return best_dist;
       
    76 }
       
    77 
       
    78 /** Make an analysis of a tile and check for its belonging to the same
       
    79  * industry, and/or the same grf file
       
    80  * @param new_tile TileIndex of the tile to query
       
    81  * @param old_tile TileINdex of teh reference tile
       
    82  * @param i Industry to which old_tile belongs to
       
    83  * @return value encoded as per NFO specs */
       
    84 uint32 GetIndustryIDAtOffset(TileIndex new_tile, TileIndex old_tile, const Industry *i)
       
    85 {
       
    86 	if (IsTileType(new_tile, MP_INDUSTRY)) {  // Is this an industry tile?
       
    87 
       
    88 		if (GetIndustryIndex(new_tile) == i->index) {  // Does it belong to the same industry?
       
    89 			IndustryGfx gfx = GetIndustryGfx(new_tile);
       
    90 			const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
       
    91 			const IndustryTileSpec *indold = GetIndustryTileSpec(GetIndustryGfx(old_tile));
       
    92 
       
    93 			if (gfx < NEW_INDUSTRYOFFSET) {  // Does it belongs to an old type?
       
    94 				/* It is an old tile.  We have to see if it's been overriden */
       
    95 				if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) {  // has it been overridden?
       
    96 					return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
       
    97 				} else { // yes.  FInd out if it is from the same grf file or not
       
    98 					const IndustryTileSpec *old_tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);
       
    99 
       
   100 					if (old_tile_ovr->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) {
       
   101 						return old_tile_ovr->grf_prop.local_id; // same grf file
       
   102 					} else {
       
   103 						return 0xFFFE; // not the same grf file
       
   104 					}
       
   105 				}
       
   106 			} else {
       
   107 				if (indtsp->grf_prop.spritegroup != NULL) {  // tile has a spritegroup ?
       
   108 					if (indtsp->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) {  // same industry, same grf ?
       
   109 						return indtsp->grf_prop.local_id;
       
   110 					} else {
       
   111 						return 0xFFFE;  // Defined in another grf file
       
   112 					}
       
   113 				} else {  // tile has no spritegroup
       
   114 					return 0xFF << 8 | indtsp->grf_prop.subst_id;  // so just give him the substitute
       
   115 				}
       
   116 			}
       
   117 		}
       
   118 	}
       
   119 
       
   120 	return 0xFFFF; // tile is not an industry one or  does not belong to the current industry
       
   121 }
       
   122 
       
   123 static uint32 GetClosestIndustry(TileIndex tile, IndustryType type, const Industry *current)
       
   124 {
       
   125 	uint32 best_dist = MAX_UVALUE(uint32);
       
   126 	const Industry *i;
       
   127 	FOR_ALL_INDUSTRIES(i) {
       
   128 		if (i->type != type || i == current) continue;
       
   129 
       
   130 		best_dist = min(best_dist, DistanceManhattan(tile, i->xy));
       
   131 	}
       
   132 
       
   133 	return best_dist;
       
   134 }
       
   135 
       
   136 /** This function implements the industries variables that newGRF defines.
       
   137  * @param variable that is queried
       
   138  * @param parameter unused
       
   139  * @param available will return false if ever the variable asked for does not exist
       
   140  * @param ind is of course the industry we are inquiring
       
   141  * @return the value stored in the corresponding variable*/
       
   142 uint32 IndustryGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
       
   143 {
       
   144 	const Industry *industry = object->u.industry.ind;
       
   145 	TileIndex tile = object->u.industry.tile;
       
   146 	const IndustrySpec *indspec = GetIndustrySpec(industry->type);
       
   147 
       
   148 	switch (variable) {
       
   149 		case 0x40:
       
   150 		case 0x41:
       
   151 		case 0x42: { // waiting cargo, but only if those two callback flags are set
       
   152 			uint16 callback = indspec->callback_flags;
       
   153 			if (HASBIT(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HASBIT(callback, CBM_IND_PRODUCTION_256_TICKS)) {
       
   154 				return min(industry->incoming_cargo_waiting[variable - 0x40], (uint16)0xFFFF);
       
   155 			} else {
       
   156 				return 0;
       
   157 			}
       
   158 		}
       
   159 
       
   160 		/* Manhattan distance of closes dry/water tile */
       
   161 		case 0x43: return GetClosestWaterDistance(tile, (object->u.industry_location.spec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
       
   162 
       
   163 		/* Get industry ID at offset param */
       
   164 		case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, industry->xy), tile, industry);
       
   165 
       
   166 		case 0x61: return 0; // Get random tile bits at offset param
       
   167 
       
   168 		/* Land info of nearby tiles */
       
   169 		case 0x62: return GetNearbyIndustryTileInformation(parameter, tile, INVALID_INDUSTRY);
       
   170 
       
   171 		/* Animation stage of nearby tiles */
       
   172 		case 0x63 : {
       
   173 			tile = GetNearbyTile(parameter, tile);
       
   174 			if (IsTileType(tile, MP_INDUSTRY) && GetIndustryByTile(tile) == industry) {
       
   175 				return GetIndustryAnimationState(tile);
       
   176 			}
       
   177 			return 0xFFFFFFFF;
       
   178 		}
       
   179 
       
   180 		/* Distance of nearest industry of given type */
       
   181 		case 0x64: return GetClosestIndustry(tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), industry); // Distance of nearest industry of given type
       
   182 		/* Get town zone and Manhattan distance of closest town */
       
   183  		case 0x65: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceManhattan(tile, industry->town->xy), 0xFFFF);
       
   184 		/* Get square of Euclidian distance of closes town */
       
   185 		case 0x66: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceSquare(tile, industry->town->xy), 0xFFFF);
       
   186 
       
   187 		/* Count of industry, distance of closest instance
       
   188 		 * format is rr(reserved) cc(count)  dddd(manhattan distance of closest sister)
       
   189 		 * A lot more should be done, since it has to check for local id, grf id etc...
       
   190 		 * let's just say it is a beginning ;) */
       
   191 		case 0x67: return GetIndustryTypeCount(industry->type) << 16 | 0;
       
   192 
       
   193 		/* Industry structure access*/
       
   194 		case 0x80: return industry->xy;
       
   195 		case 0x81: return GB(industry->xy, 8, 8);
       
   196 		/* Pointer to the town the industry is associated with */
       
   197 		case 0x82: return industry->town->index;
       
   198 		case 0x83:
       
   199 		case 0x84:
       
   200 		case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
       
   201 		case 0x86: return industry->width;
       
   202 		case 0x87: return industry->height;// xy dimensions
       
   203 		/*  */
       
   204 		case 0x88:
       
   205 		case 0x89: return indspec->produced_cargo[variable - 0x88];
       
   206 		case 0x8A: return industry->produced_cargo_waiting[0];
       
   207 		case 0x8B: return GB(industry->produced_cargo_waiting[0], 8, 8);
       
   208 		case 0x8C: return industry->produced_cargo_waiting[1];
       
   209 		case 0x8D: return GB(industry->produced_cargo_waiting[1], 8, 8);
       
   210 		case 0x8E:
       
   211 		case 0x8F: return industry->production_rate[variable - 0x8E];
       
   212 		case 0x90:
       
   213 		case 0x91:
       
   214 		case 0x92: return indspec->accepts_cargo[variable - 0x90];
       
   215 		case 0x93: return industry->prod_level;
       
   216 		/* amount of cargo produced so far THIS month. */
       
   217 		case 0x94: return industry->this_month_production[0];
       
   218 		case 0x95: return GB(industry->this_month_production[0], 8, 8);
       
   219 		case 0x96: return industry->this_month_production[1];
       
   220 		case 0x97: return GB(industry->this_month_production[1], 8, 8);
       
   221 		/* amount of cargo transported so far THIS month. */
       
   222 		case 0x98: return industry->this_month_transported[0];
       
   223 		case 0x99: return GB(industry->this_month_transported[0], 8, 8);
       
   224 		case 0x9A: return industry->this_month_transported[1];
       
   225 		case 0x9B: return GB(industry->this_month_transported[0], 8, 8);
       
   226 		/* fraction of cargo transported LAST month. */
       
   227 		case 0x9C:
       
   228 		case 0x9D: return industry->last_month_pct_transported[variable - 0x9C];
       
   229 		/* amount of cargo produced LAST month. */
       
   230 		case 0x9E: return industry->last_month_production[0];
       
   231 		case 0x9F: return GB(industry->last_month_production[0], 8, 8);
       
   232 		case 0xA0: return industry->last_month_production[1];
       
   233 		case 0xA1: return GB(industry->last_month_production[1], 8, 8);
       
   234 		/* amount of cargo transported last month. */
       
   235 		case 0xA2: return industry->last_month_transported[0];
       
   236 		case 0xA3: return GB(industry->last_month_transported[0], 8, 8);
       
   237 		case 0xA4: return industry->last_month_transported[1];
       
   238 		case 0xA5: return GB(industry->last_month_transported[0], 8, 8);
       
   239 
       
   240 		case 0xA6: return industry->type;
       
   241 		case 0xA7: return industry->founder;
       
   242 		case 0xA8: return industry->random_color;
       
   243 		case 0xA9: return clamp(0, industry->last_prod_year - 1920, 255);
       
   244 		case 0xAA: return industry->counter;
       
   245 		case 0xAB: return GB(industry->counter, 8, 8);
       
   246 		case 0xAC: return industry->was_cargo_delivered;
       
   247 
       
   248 		case 0xB0: return clamp(0, industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 65535); // Date when built since 1920 (in days)
       
   249 		case 0xB3: return industry->construction_type; // Construction type
       
   250 		case 0xB4: return clamp(0, industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 65535); // Date last cargo accepted since 1920 (in days)
       
   251 	}
       
   252 
       
   253 	DEBUG(grf, 1, "Unhandled industry property 0x%X", variable);
       
   254 
       
   255 	*available = false;
       
   256 	return (uint32)-1;
       
   257 }
       
   258 
       
   259 static const SpriteGroup *IndustryResolveReal(const ResolverObject *object, const SpriteGroup *group)
       
   260 {
       
   261 	/* IndustryTile do not have 'real' groups */
       
   262 	return NULL;
       
   263 }
       
   264 
       
   265 static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus)
       
   266 {
       
   267 	res->GetRandomBits = IndustryTileGetRandomBits;
       
   268 	res->GetTriggers   = IndustryTileGetTriggers;
       
   269 	res->SetTriggers   = IndustryTileSetTriggers;
       
   270 	res->GetVariable   = IndustryGetVariable;
       
   271 	res->ResolveReal   = IndustryResolveReal;
       
   272 
       
   273 	res->u.industry.tile = tile;
       
   274 	res->u.industry.ind  = indus;
       
   275 	res->u.industry.gfx  = INVALID_INDUSTRYTILE;
       
   276 
       
   277 	res->callback        = 0;
       
   278 	res->callback_param1 = 0;
       
   279 	res->callback_param2 = 0;
       
   280 	res->last_value      = 0;
       
   281 	res->trigger         = 0;
       
   282 	res->reseed          = 0;
       
   283 }
       
   284 
       
   285 uint16 GetIndustryCallback(uint16 callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
       
   286 {
       
   287 	ResolverObject object;
       
   288 	const SpriteGroup *group;
       
   289 
       
   290 	NewIndustryResolver(&object, tile, industry);
       
   291 	object.callback = callback;
       
   292 	object.callback_param1 = param1;
       
   293 	object.callback_param2 = param2;
       
   294 
       
   295 	group = Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
       
   296 	if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
       
   297 
       
   298 	return group->g.callback.result;
       
   299 }
       
   300 
       
   301 uint32 IndustryLocationGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
       
   302 {
       
   303 	TileIndex tile = object->u.industry_location.tile;
       
   304 
       
   305 	if (object->scope == VSG_SCOPE_PARENT) {
       
   306 		return TownGetVariable(variable, parameter, available, ClosestTownFromTile(tile, (uint)-1));
       
   307 	}
       
   308 
       
   309 	switch (variable) {
       
   310 		/* Land info of nearby tiles */
       
   311 		case 0x62: return GetNearbyIndustryTileInformation(parameter, tile, INVALID_INDUSTRY);
       
   312 
       
   313 		/* Distance of nearest industry of given type */
       
   314 		case 0x64: return GetClosestIndustry(tile, MapNewGRFIndustryType(parameter, object->u.industry_location.spec->grf_prop.grffile->grfid), NULL);
       
   315 
       
   316 		/* Location where to build the industry */
       
   317 		case 0x80: return tile;
       
   318 		case 0x81: return GB(tile, 8, 8);
       
   319 
       
   320 		/* Pointer to the town the industry is associated with */
       
   321 		case 0x82: return ClosestTownFromTile(tile, (uint)-1)->index;
       
   322 		case 0x83:
       
   323 		case 0x84:
       
   324 		case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
       
   325 
       
   326 		/* Number of the layout */
       
   327 		case 0x86: return object->u.industry_location.itspec_index;
       
   328 
       
   329 		/* Ground type */
       
   330 		case 0x87: return GetTerrainType(tile);
       
   331 
       
   332 		/* Town zone */
       
   333 		case 0x88: return GetTownRadiusGroup(ClosestTownFromTile(tile, (uint)-1), tile);
       
   334 
       
   335 		/* Manhattan distance of the closest town */
       
   336 		case 0x89: return min(DistanceManhattan(ClosestTownFromTile(tile, (uint)-1)->xy, tile), 255);
       
   337 
       
   338 		/* Lowest height of the tile */
       
   339 		case 0x8A: return GetTileZ(tile);
       
   340 
       
   341 		/* Distance to the nearest water/land tile */
       
   342 		case 0x8B: return GetClosestWaterDistance(tile, (object->u.industry_location.spec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
       
   343 
       
   344 		/* Square of Euclidian distance from town */
       
   345 		case 0x8D: return min(DistanceSquare(ClosestTownFromTile(tile, (uint)-1)->xy, tile), 65535);
       
   346 	}
       
   347 
       
   348 	DEBUG(grf, 1, "Unhandled location industry property 0x%X", variable);
       
   349 
       
   350 	*available = false;
       
   351 	return (uint32)-1;
       
   352 }
       
   353 
       
   354 bool CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint itspec_index)
       
   355 {
       
   356 	const IndustrySpec *indspec = GetIndustrySpec(type);
       
   357 
       
   358 	ResolverObject object;
       
   359 	const SpriteGroup *group;
       
   360 
       
   361 	NewIndustryResolver(&object, tile, NULL);
       
   362 	object.GetVariable = IndustryLocationGetVariable;
       
   363 	object.callback = CBID_INDUSTRY_LOCATION;
       
   364 	object.u.industry_location.tile = tile;
       
   365 	object.u.industry_location.spec = indspec;
       
   366 	object.u.industry_location.itspec_index = itspec_index;
       
   367 
       
   368 	group = Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
       
   369 
       
   370 	if (group == NULL || group->type != SGT_CALLBACK) return false;
       
   371 
       
   372 	switch (group->g.callback.result) {
       
   373 		case 0x400: return true;
       
   374 		case 0x401: _error_message = STR_0239_SITE_UNSUITABLE; break;
       
   375 		case 0x402: _error_message = STR_0317_CAN_ONLY_BE_BUILT_IN_RAINFOREST; break;
       
   376 		case 0x403: _error_message = STR_0318_CAN_ONLY_BE_BUILT_IN_DESERT; break;
       
   377 		default: _error_message = GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + group->g.callback.result); break;
       
   378 	}
       
   379 
       
   380 	return false;
       
   381 }
       
   382 
       
   383 static int32 DerefIndProd(uint field, bool use_register)
       
   384 {
       
   385 	return use_register ? (int32)GetRegister(field) : field;
       
   386 }
       
   387 
       
   388 /**
       
   389  * Get the industry production callback and apply it to the industry.
       
   390  * @param ind    the industry this callback has to be called for
       
   391  * @param reason the reason it is called (0 = incoming cargo, 1 = periodic tick callback)
       
   392  */
       
   393 void IndustryProductionCallback(Industry *ind, int reason)
       
   394 {
       
   395 	ResolverObject object;
       
   396 	NewIndustryResolver(&object, ind->xy, ind);
       
   397 	object.callback_param2 = reason;
       
   398 
       
   399 	for (uint loop = 0;; loop++) {
       
   400 		SB(object.callback_param2, 8, 16, loop);
       
   401 		const SpriteGroup *group = Resolve(GetIndustrySpec(ind->type)->grf_prop.spritegroup, &object);
       
   402 		if (group == NULL || group->type != SGT_INDUSTRY_PRODUCTION) break;
       
   403 
       
   404 		bool deref = (group->g.indprod.version == 1);
       
   405 
       
   406 		for (uint i = 0; i < 3; i++) {
       
   407 			ind->incoming_cargo_waiting[i] = clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->g.indprod.substract_input[i], deref), 0, 0xFFFF);
       
   408 		}
       
   409 		for (uint i = 0; i < 2; i++) {
       
   410 			ind->produced_cargo_waiting[i] = clamp(ind->produced_cargo_waiting[i] + DerefIndProd(group->g.indprod.add_output[i], deref), 0, 0xFFFF);
       
   411 		}
       
   412 
       
   413 		int32 again = DerefIndProd(group->g.indprod.again, deref);
       
   414 		if (again == 0) break;
       
   415 
       
   416 		SB(object.callback_param2, 24, 8, again);
       
   417 	}
       
   418 
       
   419 	InvalidateWindow(WC_INDUSTRY_VIEW, ind->index);
       
   420 }