# newgrf_industries.cpp

Go to the documentation of this file.

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