# newgrf_industries.cpp

Go to the documentation of this file.

    00001 /* $Id$ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "debug.h"
    00014 #include "industry.h"
    00015 #include "newgrf_industries.h"
    00016 #include "newgrf_town.h"
    00017 #include "newgrf_cargo.h"
    00018 #include "window_func.h"
    00019 #include "town.h"
    00020 #include "company_base.h"
    00021 #include "error.h"
    00022 #include "strings_func.h"
    00023 #include "core/random_func.hpp"
    00024
    00025 #include "table/strings.h"
    00026
    00027 static uint32 _industry_creation_random_bits;
    00028
    00029 /* Since the industry IDs defined by the GRF file don't necessarily correlate
    00030  * to those used by the game, the IDs used for overriding old industries must be
    00031  * translated when the idustry spec is set. */
    00032 IndustryOverrideManager _industry_mngr(NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, INVALID_INDUSTRYTYPE);
    00033 IndustryTileOverrideManager _industile_mngr(NEW_INDUSTRYTILEOFFSET, NUM_INDUSTRYTILES, INVALID_INDUSTRYTILE);
    00034
    00041 IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id)
    00042 {
    00043   if (grf_type == IT_INVALID) return IT_INVALID;
    00044   if (!HasBit(grf_type, 7)) return GB(grf_type, 0, 6);
    00045
    00046   return _industry_mngr.GetID(GB(grf_type, 0, 6), grf_id);
    00047 }
    00048
    00057 uint32 GetIndustryIDAtOffset(TileIndex tile, const Industry *i, uint32 cur_grfid)
    00058 {
    00059   if (!IsTileType(tile, MP_INDUSTRY) || GetIndustryIndex(tile) != i->index) {
    00060     /* No industry and/or the tile does not have the same industry as the one we match it with */
    00061     return 0xFFFF;
    00062   }
    00063
    00064   IndustryGfx gfx = GetCleanIndustryGfx(tile);
    00065   const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
    00066
    00067   if (gfx < NEW_INDUSTRYTILEOFFSET) { // Does it belongs to an old type?
    00068     /* It is an old tile.  We have to see if it's been overriden */
    00069     if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) { // has it been overridden?
    00070       return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
    00071     }
    00072     /* Overriden */
    00073     const IndustryTileSpec *tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);
    00074
    00075     if (tile_ovr->grf_prop.grffile->grfid == cur_grfid) {
    00076       return tile_ovr->grf_prop.local_id; // same grf file
    00077     } else {
    00078       return 0xFFFE; // not the same grf file
    00079     }
    00080   }
    00081   /* Not an 'old type' tile */
    00082   if (indtsp->grf_prop.spritegroup[0] != NULL) { // tile has a spritegroup ?
    00083     if (indtsp->grf_prop.grffile->grfid == cur_grfid) { // same industry, same grf ?
    00084       return indtsp->grf_prop.local_id;
    00085     } else {
    00086       return 0xFFFE; // Defined in another grf file
    00087     }
    00088   }
    00089   /* The tile has no spritegroup */
    00090   return 0xFF << 8 | indtsp->grf_prop.subst_id; // so just give him the substitute
    00091 }
    00092
    00093 static uint32 GetClosestIndustry(TileIndex tile, IndustryType type, const Industry *current)
    00094 {
    00095   uint32 best_dist = UINT32_MAX;
    00096   const Industry *i;
    00097   FOR_ALL_INDUSTRIES(i) {
    00098     if (i->type != type || i == current) continue;
    00099
    00100     best_dist = min(best_dist, DistanceManhattan(tile, i->location.tile));
    00101   }
    00102
    00103   return best_dist;
    00104 }
    00105
    00116 static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout_filter, bool town_filter, const Industry *current)
    00117 {
    00118   uint32 GrfID = GetRegister(0x100);
    00119   IndustryType ind_index;
    00120   uint32 closest_dist = UINT32_MAX;
    00121   byte count = 0;
    00122
    00123   /* Determine what will be the industry type to look for */
    00124   switch (GrfID) {
    00125     case 0:  // this is a default industry type
    00126       ind_index = param_setID;
    00127       break;
    00128
    00129     case 0xFFFFFFFF: // current grf
    00130       GrfID = GetIndustrySpec(current->type)->grf_prop.grffile->grfid;
    00131       /* FALL THROUGH */
    00132
    00133     default: // use the grfid specified in register 100h
    00134       SetBit(param_setID, 7); // bit 7 means it is not an old type
    00135       ind_index = MapNewGRFIndustryType(param_setID, GrfID);
    00136       break;
    00137   }
    00138
    00139   /* If the industry type is invalid, there is none and the closest is far away. */
    00140   if (ind_index >= NUM_INDUSTRYTYPES) return 0 | 0xFFFF;
    00141
    00142   if (layout_filter == 0 && !town_filter) {
    00143     /* If the filter is 0, it could be because none was specified as well as being really a 0.
    00144      * In either case, just do the regular var67 */
    00145     closest_dist = GetClosestIndustry(current->location.tile, ind_index, current);
    00146     count = min(Industry::GetIndustryTypeCount(ind_index), UINT8_MAX); // clamp to 8 bit
    00147   } else {
    00148     /* Count only those who match the same industry type and layout filter
    00149      * Unfortunately, we have to do it manually */
    00150     const Industry *i;
    00151     FOR_ALL_INDUSTRIES(i) {
    00152       if (i->type == ind_index && i != current && (i->selected_layout == layout_filter || layout_filter == 0) && (!town_filter || i->town == current->town)) {
    00153         closest_dist = min(closest_dist, DistanceManhattan(current->location.tile, i->location.tile));
    00154         count++;
    00155       }
    00156     }
    00157   }
    00158
    00159   return count << 16 | GB(closest_dist, 0, 16);
    00160 }
    00161
    00170 uint32 IndustryGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
    00171 {
    00172   const Industry *industry = object->u.industry.ind;
    00173   TileIndex tile = object->u.industry.tile;
    00174   IndustryType type = object->u.industry.type;
    00175   const IndustrySpec *indspec = GetIndustrySpec(type);
    00176
    00177   /* Shall the variable get resolved in parent scope and are we not yet in parent scope? */
    00178   if (object->u.industry.gfx == INVALID_INDUSTRYTILE && object->scope == VSG_SCOPE_PARENT) {
    00179     /* Pass the request on to the town of the industry */
    00180     Town *t;
    00181
    00182     if (industry != NULL) {
    00183       t = industry->town;
    00184     } else if (tile != INVALID_TILE) {
    00185       t = ClosestTownFromTile(tile, UINT_MAX);
    00186     } else {
    00187       *available = false;
    00188       return UINT_MAX;
    00189     }
    00190
    00191     return TownGetVariable(variable, parameter, available, t, object->grffile);
    00192   }
    00193
    00194   if (industry == NULL) {
    00195     DEBUG(grf, 1, "Unhandled variable 0x%X (no available industry) in callback 0x%x", variable, object->callback);
    00196
    00197     *available = false;
    00198     return UINT_MAX;
    00199   }
    00200
    00201   switch (variable) {
    00202     case 0x40:
    00203     case 0x41:
    00204     case 0x42: { // waiting cargo, but only if those two callback flags are set
    00205       uint16 callback = indspec->callback_mask;
    00206       if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
    00207         if ((indspec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) {
    00208           if (industry->prod_level == 0) return 0;
    00209           return min(industry->incoming_cargo_waiting[variable - 0x40] / industry->prod_level, (uint16)0xFFFF);
    00210         } else {
    00211           return min(industry->incoming_cargo_waiting[variable - 0x40], (uint16)0xFFFF);
    00212         }
    00213       } else {
    00214         return 0;
    00215       }
    00216     }
    00217
    00218     /* Manhattan distance of closes dry/water tile */
    00219     case 0x43: return GetClosestWaterDistance(tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
    00220
    00221     /* Layout number */
    00222     case 0x44: return industry->selected_layout;
    00223
    00224     /* Company info */
    00225     case 0x45: {
    00226       byte colours = 0;
    00227       bool is_ai = false;
    00228
    00229       const Company *c = Company::GetIfValid(industry->founder);
    00230       if (c != NULL) {
    00231         const Livery *l = &c->livery[LS_DEFAULT];
    00232
    00233         is_ai = c->is_ai;
    00234         colours = l->colour1 + l->colour2 * 16;
    00235       }
    00236
    00237       return industry->founder | (is_ai ? 0x10000 : 0) | (colours << 24);
    00238     }
    00239
    00240     case 0x46: return industry->construction_date; // Date when built - long format - (in days)
    00241
    00242     /* Get industry ID at offset param */
    00243     case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, industry->location.tile, false), industry, object->grffile->grfid);
    00244
    00245     /* Get random tile bits at offset param */
    00246     case 0x61:
    00247       tile = GetNearbyTile(parameter, tile, false);
    00248       return industry->TileBelongsToIndustry(tile) ? GetIndustryRandomBits(tile) : 0;
    00249
    00250     /* Land info of nearby tiles */
    00251     case 0x62: return GetNearbyIndustryTileInformation(parameter, tile, INVALID_INDUSTRY, false, object->grffile->grf_version >= 8);
    00252
    00253     /* Animation stage of nearby tiles */
    00254     case 0x63:
    00255       tile = GetNearbyTile(parameter, tile, false);
    00256       if (industry->TileBelongsToIndustry(tile)) {
    00257         return GetAnimationFrame(tile);
    00258       }
    00259       return 0xFFFFFFFF;
    00260
    00261     /* Distance of nearest industry of given type */
    00262     case 0x64: return GetClosestIndustry(tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), industry);
    00263     /* Get town zone and Manhattan distance of closest town */
    00264     case 0x65: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceManhattan(tile, industry->town->xy), 0xFFFF);
    00265     /* Get square of Euclidian distance of closes town */
    00266     case 0x66: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceSquare(tile, industry->town->xy), 0xFFFF);
    00267
    00268     /* Count of industry, distance of closest instance
    00269      * 68 is the same as 67, but with a filtering on selected layout */
    00270     case 0x67:
    00271     case 0x68: {
    00272       byte layout_filter = 0;
    00273       bool town_filter = false;
    00274       if (variable == 0x68) {
    00275         uint32 reg = GetRegister(0x101);
    00276         layout_filter = GB(reg, 0, 8);
    00277         town_filter = HasBit(reg, 8);
    00278       }
    00279       return GetCountAndDistanceOfClosestInstance(parameter, layout_filter, town_filter, industry);
    00280     }
    00281
    00282     /* Get a variable from the persistent storage */
    00283     case 0x7C: return (industry->psa != NULL) ? industry->psa->GetValue(parameter) : 0;
    00284
    00285     /* Industry structure access*/
    00286     case 0x80: return industry->location.tile;
    00287     case 0x81: return GB(industry->location.tile, 8, 8);
    00288     /* Pointer to the town the industry is associated with */
    00289     case 0x82: return industry->town->index;
    00290     case 0x83:
    00291     case 0x84:
    00292     case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
    00293     case 0x86: return industry->location.w;
    00294     case 0x87: return industry->location.h;// xy dimensions
    00295
    00296     case 0x88:
    00297     case 0x89: return industry->produced_cargo[variable - 0x88];
    00298     case 0x8A: return industry->produced_cargo_waiting[0];
    00299     case 0x8B: return GB(industry->produced_cargo_waiting[0], 8, 8);
    00300     case 0x8C: return industry->produced_cargo_waiting[1];
    00301     case 0x8D: return GB(industry->produced_cargo_waiting[1], 8, 8);
    00302     case 0x8E:
    00303     case 0x8F: return industry->production_rate[variable - 0x8E];
    00304     case 0x90:
    00305     case 0x91:
    00306     case 0x92: return industry->accepts_cargo[variable - 0x90];
    00307     case 0x93: return industry->prod_level;
    00308     /* amount of cargo produced so far THIS month. */
    00309     case 0x94: return industry->this_month_production[0];
    00310     case 0x95: return GB(industry->this_month_production[0], 8, 8);
    00311     case 0x96: return industry->this_month_production[1];
    00312     case 0x97: return GB(industry->this_month_production[1], 8, 8);
    00313     /* amount of cargo transported so far THIS month. */
    00314     case 0x98: return industry->this_month_transported[0];
    00315     case 0x99: return GB(industry->this_month_transported[0], 8, 8);
    00316     case 0x9A: return industry->this_month_transported[1];
    00317     case 0x9B: return GB(industry->this_month_transported[1], 8, 8);
    00318     /* fraction of cargo transported LAST month. */
    00319     case 0x9C:
    00320     case 0x9D: return industry->last_month_pct_transported[variable - 0x9C];
    00321     /* amount of cargo produced LAST month. */
    00322     case 0x9E: return industry->last_month_production[0];
    00323     case 0x9F: return GB(industry->last_month_production[0], 8, 8);
    00324     case 0xA0: return industry->last_month_production[1];
    00325     case 0xA1: return GB(industry->last_month_production[1], 8, 8);
    00326     /* amount of cargo transported last month. */
    00327     case 0xA2: return industry->last_month_transported[0];
    00328     case 0xA3: return GB(industry->last_month_transported[0], 8, 8);
    00329     case 0xA4: return industry->last_month_transported[1];
    00330     case 0xA5: return GB(industry->last_month_transported[1], 8, 8);
    00331
    00332     case 0xA6: return industry->type;
    00333     case 0xA7: return industry->founder;
    00334     case 0xA8: return industry->random_colour;
    00335     case 0xA9: return Clamp(industry->last_prod_year - ORIGINAL_BASE_YEAR, 0, 255);
    00336     case 0xAA: return industry->counter;
    00337     case 0xAB: return GB(industry->counter, 8, 8);
    00338     case 0xAC: return industry->was_cargo_delivered;
    00339
    00340     case 0xB0: return Clamp(industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date when built since 1920 (in days)
    00341     case 0xB3: return industry->construction_type; // Construction type
    00342     case 0xB4: return Clamp(industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date last cargo accepted since 1920 (in days)
    00343   }
    00344
    00345   DEBUG(grf, 1, "Unhandled industry variable 0x%X", variable);
    00346
    00347   *available = false;
    00348   return UINT_MAX;
    00349 }
    00350
    00351 static const SpriteGroup *IndustryResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
    00352 {
    00353   /* IndustryTile do not have 'real' groups */
    00354   return NULL;
    00355 }
    00356
    00357 static uint32 IndustryGetRandomBits(const ResolverObject *object)
    00358 {
    00359   const Industry *ind = object->u.industry.ind;
    00360   return ind != NULL ? ind->random: 0;
    00361 }
    00362
    00363 static uint32 IndustryGetTriggers(const ResolverObject *object)
    00364 {
    00365   const Industry *ind = object->u.industry.ind;
    00366   return ind != NULL ? ind->random_triggers : 0;
    00367 }
    00368
    00369 static void IndustrySetTriggers(const ResolverObject *object, int triggers)
    00370 {
    00371   Industry *ind = object->u.industry.ind;
    00372   assert(ind != NULL && ind->index != INVALID_INDUSTRY);
    00373   ind->random_triggers = triggers;
    00374 }
    00375
    00382 void IndustryStorePSA(ResolverObject *object, uint pos, int32 value)
    00383 {
    00384   Industry *ind = object->u.industry.ind;
    00385   if (ind->index == INVALID_INDUSTRY) return;
    00386
    00387   if (object->scope != VSG_SCOPE_SELF) {
    00388     /* Pass the request on to the town of the industry. */
    00389     TownStorePSA(ind->town, object->grffile, pos, value);
    00390     return;
    00391   }
    00392
    00393   if (ind->psa == NULL) {
    00394     /* There is no need to create a storage if the value is zero. */
    00395     if (value == 0) return;
    00396
    00397     /* Create storage on first modification. */
    00398     const IndustrySpec *indsp = GetIndustrySpec(ind->type);
    00399     uint32 grfid = (indsp->grf_prop.grffile != NULL) ? indsp->grf_prop.grffile->grfid : 0;
    00400     assert(PersistentStorage::CanAllocateItem());
    00401     ind->psa = new PersistentStorage(grfid);
    00402   }
    00403
    00404   ind->psa->StoreValue(pos, value);
    00405 }
    00406
    00407 static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus, IndustryType type)
    00408 {
    00409   res->GetRandomBits = IndustryGetRandomBits;
    00410   res->GetTriggers   = IndustryGetTriggers;
    00411   res->SetTriggers   = IndustrySetTriggers;
    00412   res->GetVariable   = IndustryGetVariable;
    00413   res->ResolveReal   = IndustryResolveReal;
    00414   res->StorePSA      = IndustryStorePSA;
    00415
    00416   res->u.industry.tile = tile;
    00417   res->u.industry.ind  = indus;
    00418   res->u.industry.gfx  = INVALID_INDUSTRYTILE;
    00419   res->u.industry.type = type;
    00420
    00421   res->callback        = CBID_NO_CALLBACK;
    00422   res->callback_param1 = 0;
    00423   res->callback_param2 = 0;
    00424   res->ResetState();
    00425
    00426   const IndustrySpec *indspec = GetIndustrySpec(type);
    00427   res->grffile         = (indspec != NULL ? indspec->grf_prop.grffile : NULL);
    00428 }
    00429
    00440 uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
    00441 {
    00442   ResolverObject object;
    00443   const SpriteGroup *group;
    00444
    00445   NewIndustryResolver(&object, tile, industry, type);
    00446   object.callback = callback;
    00447   object.callback_param1 = param1;
    00448   object.callback_param2 = param2;
    00449
    00450   group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
    00451   if (group == NULL) return CALLBACK_FAILED;
    00452
    00453   return group->GetCallbackResult();
    00454 }
    00455
    00456 uint32 IndustryLocationGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
    00457 {
    00458   const Industry *industry = object->u.industry.ind;
    00459   TileIndex tile = object->u.industry.tile;
    00460
    00461   if (object->scope == VSG_SCOPE_PARENT) {
    00462     return TownGetVariable(variable, parameter, available, industry->town, object->grffile);
    00463   }
    00464
    00465   switch (variable) {
    00466     case 0x80: return tile;
    00467     case 0x81: return GB(tile, 8, 8);
    00468
    00469     /* Pointer to the town the industry is associated with */
    00470     case 0x82: return industry->town->index;
    00471     case 0x83:
    00472     case 0x84:
    00473     case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
    00474
    00475     /* Number of the layout */
    00476     case 0x86: return industry->selected_layout;
    00477
    00478     /* Ground type */
    00479     case 0x87: return GetTerrainType(tile);
    00480
    00481     /* Town zone */
    00482     case 0x88: return GetTownRadiusGroup(industry->town, tile);
    00483
    00484     /* Manhattan distance of the closest town */
    00485     case 0x89: return min(DistanceManhattan(industry->town->xy, tile), 255);
    00486
    00487     /* Lowest height of the tile */
    00488     case 0x8A: return Clamp(GetTileZ(tile) * (object->grffile->grf_version >= 8 ? 1 : TILE_HEIGHT), 0, 0xFF);
    00489
    00490     /* Distance to the nearest water/land tile */
    00491     case 0x8B: return GetClosestWaterDistance(tile, (GetIndustrySpec(industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
    00492
    00493     /* Square of Euclidian distance from town */
    00494     case 0x8D: return min(DistanceSquare(industry->town->xy, tile), 65535);
    00495
    00496     /* 32 random bits */
    00497     case 0x8F: return _industry_creation_random_bits;
    00498   }
    00499
    00500   /* None of the special ones, so try the general ones */
    00501   return IndustryGetVariable(object, variable, parameter, available);
    00502 }
    00503
    00515 CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
    00516 {
    00517   const IndustrySpec *indspec = GetIndustrySpec(type);
    00518
    00519   ResolverObject object;
    00520   const SpriteGroup *group;
    00521
    00522   Industry ind;
    00523   ind.index = INVALID_INDUSTRY;
    00524   ind.location.tile = tile;
    00525   ind.location.w = 0; // important to mark the industry invalid
    00526   ind.type = type;
    00527   ind.selected_layout = layout;
    00528   ind.town = ClosestTownFromTile(tile, UINT_MAX);
    00529   ind.random = initial_random_bits;
    00530   ind.founder = founder;
    00531
    00532   NewIndustryResolver(&object, tile, &ind, type);
    00533   object.GetVariable = IndustryLocationGetVariable;
    00534   object.callback = CBID_INDUSTRY_LOCATION;
    00535   object.callback_param2 = creation_type;
    00536   _industry_creation_random_bits = seed;
    00537
    00538   group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
    00539
    00540   /* Unlike the "normal" cases, not having a valid result means we allow
    00541    * the building of the industry, as that's how it's done in TTDP. */
    00542   if (group == NULL) return CommandCost();
    00543   uint16 result = group->GetCallbackResult();
    00544   if (result == CALLBACK_FAILED) return CommandCost();
    00545
    00546   return GetErrorMessageFromLocationCallbackResult(result, indspec->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
    00547 }
    00548
    00555 uint32 GetIndustryProbabilityCallback(IndustryType type, IndustryAvailabilityCallType creation_type, uint32 default_prob)
    00556 {
    00557   const IndustrySpec *indspec = GetIndustrySpec(type);
    00558
    00559   if (HasBit(indspec->callback_mask, CBM_IND_PROBABILITY)) {
    00560     uint16 res = GetIndustryCallback(CBID_INDUSTRY_PROBABILITY, 0, creation_type, NULL, type, INVALID_TILE);
    00561     if (res != CALLBACK_FAILED) {
    00562       if (indspec->grf_prop.grffile->grf_version < 8) {
    00563         /* Disallow if result != 0 */
    00564         if (res != 0) default_prob = 0;
    00565       } else {
    00566         /* Use returned probability. 0x100 to use default */
    00567         if (res < 0x100) {
    00568           default_prob = res;
    00569         } else if (res > 0x100) {
    00570           ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_PROBABILITY, res);
    00571         }
    00572       }
    00573     }
    00574   }
    00575   return default_prob;
    00576 }
    00577
    00578 static int32 DerefIndProd(int field, bool use_register)
    00579 {
    00580   return use_register ? (int32)GetRegister(field) : field;
    00581 }
    00582
    00588 void IndustryProductionCallback(Industry *ind, int reason)
    00589 {
    00590   const IndustrySpec *spec = GetIndustrySpec(ind->type);
    00591   ResolverObject object;
    00592   NewIndustryResolver(&object, ind->location.tile, ind, ind->type);
    00593   if ((spec->behaviour & INDUSTRYBEH_PRODCALLBACK_RANDOM) != 0) object.callback_param1 = Random();
    00594   int multiplier = 1;
    00595   if ((spec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) multiplier = ind->prod_level;
    00596   object.callback_param2 = reason;
    00597
    00598   for (uint loop = 0;; loop++) {
    00599     /* limit the number of calls to break infinite loops.
    00600      * 'loop' is provided as 16 bits to the newgrf, so abort when those are exceeded. */
    00601     if (loop >= 0x10000) {
    00602       /* display error message */
    00603       SetDParamStr(0, spec->grf_prop.grffile->filename);
    00604       SetDParam(1, spec->name);
    00605       ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK, WL_WARNING);
    00606
    00607       /* abort the function early, this error isn't critical and will allow the game to continue to run */
    00608       break;
    00609     }
    00610
    00611     SB(object.callback_param2, 8, 16, loop);
    00612     const SpriteGroup *tgroup = SpriteGroup::Resolve(spec->grf_prop.spritegroup[0], &object);
    00613     if (tgroup == NULL || tgroup->type != SGT_INDUSTRY_PRODUCTION) break;
    00614     const IndustryProductionSpriteGroup *group = (const IndustryProductionSpriteGroup *)tgroup;
    00615
    00616     bool deref = (group->version == 1);
    00617
    00618     for (uint i = 0; i < 3; i++) {
    00619       ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->subtract_input[i], deref) * multiplier, 0, 0xFFFF);
    00620     }
    00621     for (uint i = 0; i < 2; i++) {
    00622       ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + max(DerefIndProd(group->add_output[i], deref), 0) * multiplier, 0, 0xFFFF);
    00623     }
    00624
    00625     int32 again = DerefIndProd(group->again, deref);
    00626     if (again == 0) break;
    00627
    00628     SB(object.callback_param2, 24, 8, again);
    00629   }
    00630
    00631   SetWindowDirty(WC_INDUSTRY_VIEW, ind->index);
    00632 }
    00633
    00639 void GetIndustryResolver(ResolverObject *ro, uint index)
    00640 {
    00641   Industry *i = Industry::Get(index);
    00642   NewIndustryResolver(ro, i->location.tile, i, i->type);
    00643 }
    00644
    00652 bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type)
    00653 {
    00654   assert(cargo_type == ind->accepts_cargo[0] || cargo_type == ind->accepts_cargo[1] || cargo_type == ind->accepts_cargo[2]);
    00655
    00656   const IndustrySpec *indspec = GetIndustrySpec(ind->type);
    00657   if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) {
    00658     uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO,
    00659         0, GetReverseCargoTranslation(cargo_type, indspec->grf_prop.grffile),
    00660         ind, ind->type, ind->location.tile);
    00661     if (res != CALLBACK_FAILED) return !ConvertBooleanCallback(indspec->grf_prop.grffile, CBID_INDUSTRY_REFUSE_CARGO, res);
    00662   }
    00663   return false;
    00664 }