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