# newgrf_industries.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_industries.cpp 24973 2013-02-05 21:38:38Z peter1138 $ */
    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 /* Since the industry IDs defined by the GRF file don't necessarily correlate
    00028  * to those used by the game, the IDs used for overriding old industries must be
    00029  * translated when the idustry spec is set. */
    00030 IndustryOverrideManager _industry_mngr(NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, INVALID_INDUSTRYTYPE);
    00031 IndustryTileOverrideManager _industile_mngr(NEW_INDUSTRYTILEOFFSET, NUM_INDUSTRYTILES, INVALID_INDUSTRYTILE);
    00032
    00039 IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id)
    00040 {
    00041   if (grf_type == IT_INVALID) return IT_INVALID;
    00042   if (!HasBit(grf_type, 7)) return GB(grf_type, 0, 6);
    00043
    00044   return _industry_mngr.GetID(GB(grf_type, 0, 6), grf_id);
    00045 }
    00046
    00055 uint32 GetIndustryIDAtOffset(TileIndex tile, const Industry *i, uint32 cur_grfid)
    00056 {
    00057   if (!IsTileType(tile, MP_INDUSTRY) || GetIndustryIndex(tile) != i->index) {
    00058     /* No industry and/or the tile does not have the same industry as the one we match it with */
    00059     return 0xFFFF;
    00060   }
    00061
    00062   IndustryGfx gfx = GetCleanIndustryGfx(tile);
    00063   const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
    00064
    00065   if (gfx < NEW_INDUSTRYTILEOFFSET) { // Does it belongs to an old type?
    00066     /* It is an old tile.  We have to see if it's been overridden */
    00067     if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) { // has it been overridden?
    00068       return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
    00069     }
    00070     /* Overridden */
    00071     const IndustryTileSpec *tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);
    00072
    00073     if (tile_ovr->grf_prop.grffile->grfid == cur_grfid) {
    00074       return tile_ovr->grf_prop.local_id; // same grf file
    00075     } else {
    00076       return 0xFFFE; // not the same grf file
    00077     }
    00078   }
    00079   /* Not an 'old type' tile */
    00080   if (indtsp->grf_prop.spritegroup[0] != NULL) { // tile has a spritegroup ?
    00081     if (indtsp->grf_prop.grffile->grfid == cur_grfid) { // same industry, same grf ?
    00082       return indtsp->grf_prop.local_id;
    00083     } else {
    00084       return 0xFFFE; // Defined in another grf file
    00085     }
    00086   }
    00087   /* The tile has no spritegroup */
    00088   return 0xFF << 8 | indtsp->grf_prop.subst_id; // so just give him the substitute
    00089 }
    00090
    00091 static uint32 GetClosestIndustry(TileIndex tile, IndustryType type, const Industry *current)
    00092 {
    00093   uint32 best_dist = UINT32_MAX;
    00094   const Industry *i;
    00095   FOR_ALL_INDUSTRIES(i) {
    00096     if (i->type != type || i == current) continue;
    00097
    00098     best_dist = min(best_dist, DistanceManhattan(tile, i->location.tile));
    00099   }
    00100
    00101   return best_dist;
    00102 }
    00103
    00114 static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout_filter, bool town_filter, const Industry *current)
    00115 {
    00116   uint32 GrfID = GetRegister(0x100);
    00117   IndustryType ind_index;
    00118   uint32 closest_dist = UINT32_MAX;
    00119   byte count = 0;
    00120
    00121   /* Determine what will be the industry type to look for */
    00122   switch (GrfID) {
    00123     case 0:  // this is a default industry type
    00124       ind_index = param_setID;
    00125       break;
    00126
    00127     case 0xFFFFFFFF: // current grf
    00128       GrfID = GetIndustrySpec(current->type)->grf_prop.grffile->grfid;
    00129       /* FALL THROUGH */
    00130
    00131     default: // use the grfid specified in register 100h
    00132       SetBit(param_setID, 7); // bit 7 means it is not an old type
    00133       ind_index = MapNewGRFIndustryType(param_setID, GrfID);
    00134       break;
    00135   }
    00136
    00137   /* If the industry type is invalid, there is none and the closest is far away. */
    00138   if (ind_index >= NUM_INDUSTRYTYPES) return 0 | 0xFFFF;
    00139
    00140   if (layout_filter == 0 && !town_filter) {
    00141     /* If the filter is 0, it could be because none was specified as well as being really a 0.
    00142      * In either case, just do the regular var67 */
    00143     closest_dist = GetClosestIndustry(current->location.tile, ind_index, current);
    00144     count = min(Industry::GetIndustryTypeCount(ind_index), UINT8_MAX); // clamp to 8 bit
    00145   } else {
    00146     /* Count only those who match the same industry type and layout filter
    00147      * Unfortunately, we have to do it manually */
    00148     const Industry *i;
    00149     FOR_ALL_INDUSTRIES(i) {
    00150       if (i->type == ind_index && i != current && (i->selected_layout == layout_filter || layout_filter == 0) && (!town_filter || i->town == current->town)) {
    00151         closest_dist = min(closest_dist, DistanceManhattan(current->location.tile, i->location.tile));
    00152         count++;
    00153       }
    00154     }
    00155   }
    00156
    00157   return count << 16 | GB(closest_dist, 0, 16);
    00158 }
    00159
    00160 /* virtual */ uint32 IndustriesScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
    00161 {
    00162   if (this->ro->callback == CBID_INDUSTRY_LOCATION) {
    00163     /* Variables available during construction check. */
    00164
    00165     switch (variable) {
    00166       case 0x80: return this->tile;
    00167       case 0x81: return GB(this->tile, 8, 8);
    00168
    00169       /* Pointer to the town the industry is associated with */
    00170       case 0x82: return this->industry->town->index;
    00171       case 0x83:
    00172       case 0x84:
    00173       case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
    00174
    00175       /* Number of the layout */
    00176       case 0x86: return this->industry->selected_layout;
    00177
    00178       /* Ground type */
    00179       case 0x87: return GetTerrainType(this->tile);
    00180
    00181       /* Town zone */
    00182       case 0x88: return GetTownRadiusGroup(this->industry->town, this->tile);
    00183
    00184       /* Manhattan distance of the closest town */
    00185       case 0x89: return min(DistanceManhattan(this->industry->town->xy, this->tile), 255);
    00186
    00187       /* Lowest height of the tile */
    00188       case 0x8A: return Clamp(GetTileZ(this->tile) * (this->ro->grffile->grf_version >= 8 ? 1 : TILE_HEIGHT), 0, 0xFF);
    00189
    00190       /* Distance to the nearest water/land tile */
    00191       case 0x8B: return GetClosestWaterDistance(this->tile, (GetIndustrySpec(this->industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
    00192
    00193       /* Square of Euclidian distance from town */
    00194       case 0x8D: return min(DistanceSquare(this->industry->town->xy, this->tile), 65535);
    00195
    00196       /* 32 random bits */
    00197       case 0x8F: return this->random_bits;
    00198     }
    00199   }
    00200
    00201   const IndustrySpec *indspec = GetIndustrySpec(this->type);
    00202
    00203   if (this->industry == NULL) {
    00204     DEBUG(grf, 1, "Unhandled variable 0x%X (no available industry) in callback 0x%x", variable, this->ro->callback);
    00205
    00206     *available = false;
    00207     return UINT_MAX;
    00208   }
    00209
    00210   switch (variable) {
    00211     case 0x40:
    00212     case 0x41:
    00213     case 0x42: { // waiting cargo, but only if those two callback flags are set
    00214       uint16 callback = indspec->callback_mask;
    00215       if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
    00216         if ((indspec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) {
    00217           if (this->industry->prod_level == 0) return 0;
    00218           return min(this->industry->incoming_cargo_waiting[variable - 0x40] / this->industry->prod_level, (uint16)0xFFFF);
    00219         } else {
    00220           return min(this->industry->incoming_cargo_waiting[variable - 0x40], (uint16)0xFFFF);
    00221         }
    00222       } else {
    00223         return 0;
    00224       }
    00225     }
    00226
    00227     /* Manhattan distance of closes dry/water tile */
    00228     case 0x43:
    00229       if (this->tile == INVALID_TILE) break;
    00230       return GetClosestWaterDistance(this->tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
    00231
    00232     /* Layout number */
    00233     case 0x44: return this->industry->selected_layout;
    00234
    00235     /* Company info */
    00236     case 0x45: {
    00237       byte colours = 0;
    00238       bool is_ai = false;
    00239
    00240       const Company *c = Company::GetIfValid(this->industry->founder);
    00241       if (c != NULL) {
    00242         const Livery *l = &c->livery[LS_DEFAULT];
    00243
    00244         is_ai = c->is_ai;
    00245         colours = l->colour1 + l->colour2 * 16;
    00246       }
    00247
    00248       return this->industry->founder | (is_ai ? 0x10000 : 0) | (colours << 24);
    00249     }
    00250
    00251     case 0x46: return this->industry->construction_date; // Date when built - long format - (in days)
    00252
    00253     /* Get industry ID at offset param */
    00254     case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, this->industry->location.tile, false), this->industry, this->ro->grffile->grfid);
    00255
    00256     /* Get random tile bits at offset param */
    00257     case 0x61: {
    00258       if (this->tile == INVALID_TILE) break;
    00259       TileIndex tile = GetNearbyTile(parameter, this->tile, false);
    00260       return this->industry->TileBelongsToIndustry(tile) ? GetIndustryRandomBits(tile) : 0;
    00261     }
    00262
    00263     /* Land info of nearby tiles */
    00264     case 0x62:
    00265       if (this->tile == INVALID_TILE) break;
    00266       return GetNearbyIndustryTileInformation(parameter, this->tile, INVALID_INDUSTRY, false, this->ro->grffile->grf_version >= 8);
    00267
    00268     /* Animation stage of nearby tiles */
    00269     case 0x63: {
    00270       if (this->tile == INVALID_TILE) break;
    00271       TileIndex tile = GetNearbyTile(parameter, this->tile, false);
    00272       if (this->industry->TileBelongsToIndustry(tile)) {
    00273         return GetAnimationFrame(tile);
    00274       }
    00275       return 0xFFFFFFFF;
    00276     }
    00277
    00278     /* Distance of nearest industry of given type */
    00279     case 0x64:
    00280       if (this->tile == INVALID_TILE) break;
    00281       return GetClosestIndustry(this->tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), this->industry);
    00282     /* Get town zone and Manhattan distance of closest town */
    00283     case 0x65:
    00284       if (this->tile == INVALID_TILE) break;
    00285       return GetTownRadiusGroup(this->industry->town, this->tile) << 16 | min(DistanceManhattan(this->tile, this->industry->town->xy), 0xFFFF);
    00286     /* Get square of Euclidian distance of closes town */
    00287     case 0x66:
    00288       if (this->tile == INVALID_TILE) break;
    00289       return GetTownRadiusGroup(this->industry->town, this->tile) << 16 | min(DistanceSquare(this->tile, this->industry->town->xy), 0xFFFF);
    00290
    00291     /* Count of industry, distance of closest instance
    00292      * 68 is the same as 67, but with a filtering on selected layout */
    00293     case 0x67:
    00294     case 0x68: {
    00295       byte layout_filter = 0;
    00296       bool town_filter = false;
    00297       if (variable == 0x68) {
    00298         uint32 reg = GetRegister(0x101);
    00299         layout_filter = GB(reg, 0, 8);
    00300         town_filter = HasBit(reg, 8);
    00301       }
    00302       return GetCountAndDistanceOfClosestInstance(parameter, layout_filter, town_filter, this->industry);
    00303     }
    00304
    00305     /* Get a variable from the persistent storage */
    00306     case 0x7C: return (this->industry->psa != NULL) ? this->industry->psa->GetValue(parameter) : 0;
    00307
    00308     /* Industry structure access*/
    00309     case 0x80: return this->industry->location.tile;
    00310     case 0x81: return GB(this->industry->location.tile, 8, 8);
    00311     /* Pointer to the town the industry is associated with */
    00312     case 0x82: return this->industry->town->index;
    00313     case 0x83:
    00314     case 0x84:
    00315     case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
    00316     case 0x86: return this->industry->location.w;
    00317     case 0x87: return this->industry->location.h;// xy dimensions
    00318
    00319     case 0x88:
    00320     case 0x89: return this->industry->produced_cargo[variable - 0x88];
    00321     case 0x8A: return this->industry->produced_cargo_waiting[0];
    00322     case 0x8B: return GB(this->industry->produced_cargo_waiting[0], 8, 8);
    00323     case 0x8C: return this->industry->produced_cargo_waiting[1];
    00324     case 0x8D: return GB(this->industry->produced_cargo_waiting[1], 8, 8);
    00325     case 0x8E:
    00326     case 0x8F: return this->industry->production_rate[variable - 0x8E];
    00327     case 0x90:
    00328     case 0x91:
    00329     case 0x92: return this->industry->accepts_cargo[variable - 0x90];
    00330     case 0x93: return this->industry->prod_level;
    00331     /* amount of cargo produced so far THIS month. */
    00332     case 0x94: return this->industry->this_month_production[0];
    00333     case 0x95: return GB(this->industry->this_month_production[0], 8, 8);
    00334     case 0x96: return this->industry->this_month_production[1];
    00335     case 0x97: return GB(this->industry->this_month_production[1], 8, 8);
    00336     /* amount of cargo transported so far THIS month. */
    00337     case 0x98: return this->industry->this_month_transported[0];
    00338     case 0x99: return GB(this->industry->this_month_transported[0], 8, 8);
    00339     case 0x9A: return this->industry->this_month_transported[1];
    00340     case 0x9B: return GB(this->industry->this_month_transported[1], 8, 8);
    00341     /* fraction of cargo transported LAST month. */
    00342     case 0x9C:
    00343     case 0x9D: return this->industry->last_month_pct_transported[variable - 0x9C];
    00344     /* amount of cargo produced LAST month. */
    00345     case 0x9E: return this->industry->last_month_production[0];
    00346     case 0x9F: return GB(this->industry->last_month_production[0], 8, 8);
    00347     case 0xA0: return this->industry->last_month_production[1];
    00348     case 0xA1: return GB(this->industry->last_month_production[1], 8, 8);
    00349     /* amount of cargo transported last month. */
    00350     case 0xA2: return this->industry->last_month_transported[0];
    00351     case 0xA3: return GB(this->industry->last_month_transported[0], 8, 8);
    00352     case 0xA4: return this->industry->last_month_transported[1];
    00353     case 0xA5: return GB(this->industry->last_month_transported[1], 8, 8);
    00354
    00355     case 0xA6: return this->industry->type;
    00356     case 0xA7: return this->industry->founder;
    00357     case 0xA8: return this->industry->random_colour;
    00358     case 0xA9: return Clamp(this->industry->last_prod_year - ORIGINAL_BASE_YEAR, 0, 255);
    00359     case 0xAA: return this->industry->counter;
    00360     case 0xAB: return GB(this->industry->counter, 8, 8);
    00361     case 0xAC: return this->industry->was_cargo_delivered;
    00362
    00363     case 0xB0: return Clamp(this->industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date when built since 1920 (in days)
    00364     case 0xB3: return this->industry->construction_type; // Construction type
    00365     case 0xB4: return Clamp(this->industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date last cargo accepted since 1920 (in days)
    00366   }
    00367
    00368   DEBUG(grf, 1, "Unhandled industry variable 0x%X", variable);
    00369
    00370   *available = false;
    00371   return UINT_MAX;
    00372 }
    00373
    00374 /* virtual */ uint32 IndustriesScopeResolver::GetRandomBits() const
    00375 {
    00376   return this->industry != NULL ? this->industry->random : 0;
    00377 }
    00378
    00379 /* virtual */ uint32 IndustriesScopeResolver::GetTriggers() const
    00380 {
    00381   return this->industry != NULL ? this->industry->random_triggers : 0;
    00382 }
    00383
    00384 /* virtual */ void IndustriesScopeResolver::SetTriggers(int triggers) const
    00385 {
    00386   assert(this->industry != NULL && this->industry->index != INVALID_INDUSTRY);
    00387   this->industry->random_triggers = triggers;
    00388 }
    00389
    00390 /* virtual */ void IndustriesScopeResolver::StorePSA(uint pos, int32 value)
    00391 {
    00392   if (this->industry->index == INVALID_INDUSTRY) return;
    00393
    00394   if (this->industry->psa == NULL) {
    00395     /* There is no need to create a storage if the value is zero. */
    00396     if (value == 0) return;
    00397
    00398     /* Create storage on first modification. */
    00399     const IndustrySpec *indsp = GetIndustrySpec(this->industry->type);
    00400     uint32 grfid = (indsp->grf_prop.grffile != NULL) ? indsp->grf_prop.grffile->grfid : 0;
    00401     assert(PersistentStorage::CanAllocateItem());
    00402     this->industry->psa = new PersistentStorage(grfid);
    00403   }
    00404
    00405   this->industry->psa->StoreValue(pos, value);
    00406 }
    00407
    00413 static const GRFFile *GetGrffile(IndustryType type)
    00414 {
    00415   const IndustrySpec *indspec = GetIndustrySpec(type);
    00416   return (indspec != NULL) ? indspec->grf_prop.grffile : NULL;
    00417 }
    00418
    00429 IndustriesResolverObject::IndustriesResolverObject(TileIndex tile, Industry *indus, IndustryType type, uint32 random_bits,
    00430     CallbackID callback, uint32 callback_param1, uint32 callback_param2)
    00431   : ResolverObject(GetGrffile(type), callback, callback_param1, callback_param2),
    00432   industries_scope(this, tile, indus, type, random_bits),
    00433   town_scope(NULL)
    00434 {
    00435 }
    00436
    00437 IndustriesResolverObject::~IndustriesResolverObject()
    00438 {
    00439   delete this->town_scope;
    00440 }
    00441
    00446 TownScopeResolver *IndustriesResolverObject::GetTown()
    00447 {
    00448   if (this->town_scope == NULL) {
    00449     Town *t = NULL;
    00450     if (this->industries_scope.industry != NULL) {
    00451       t = this->industries_scope.industry->town;
    00452     } else if (this->industries_scope.tile != INVALID_TILE) {
    00453       t = ClosestTownFromTile(this->industries_scope.tile, UINT_MAX);
    00454     }
    00455     if (t == NULL) return NULL;
    00456     this->town_scope = new TownScopeResolver(this, t, this->industries_scope.industry->index == INVALID_INDUSTRY);
    00457   }
    00458   return this->town_scope;
    00459 }
    00460
    00469 IndustriesScopeResolver::IndustriesScopeResolver(ResolverObject *ro, TileIndex tile, Industry *industry, IndustryType type, uint32 random_bits)
    00470   : ScopeResolver(ro)
    00471 {
    00472   this->tile = tile;
    00473   this->industry = industry;
    00474   this->type = type;
    00475   this->random_bits = random_bits;
    00476 }
    00477
    00488 uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
    00489 {
    00490   IndustriesResolverObject object(tile, industry, type, 0, callback, param1, param2);
    00491   const SpriteGroup *group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
    00492   if (group == NULL) return CALLBACK_FAILED;
    00493
    00494   return group->GetCallbackResult();
    00495 }
    00496
    00508 CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
    00509 {
    00510   const IndustrySpec *indspec = GetIndustrySpec(type);
    00511
    00512   Industry ind;
    00513   ind.index = INVALID_INDUSTRY;
    00514   ind.location.tile = tile;
    00515   ind.location.w = 0; // important to mark the industry invalid
    00516   ind.type = type;
    00517   ind.selected_layout = layout;
    00518   ind.town = ClosestTownFromTile(tile, UINT_MAX);
    00519   ind.random = initial_random_bits;
    00520   ind.founder = founder;
    00521   ind.psa = NULL;
    00522
    00523   IndustriesResolverObject object(tile, &ind, type, seed, CBID_INDUSTRY_LOCATION, 0, creation_type);
    00524   const SpriteGroup *group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
    00525
    00526   /* Unlike the "normal" cases, not having a valid result means we allow
    00527    * the building of the industry, as that's how it's done in TTDP. */
    00528   if (group == NULL) return CommandCost();
    00529   uint16 result = group->GetCallbackResult();
    00530   if (result == CALLBACK_FAILED) return CommandCost();
    00531
    00532   return GetErrorMessageFromLocationCallbackResult(result, indspec->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
    00533 }
    00534
    00541 uint32 GetIndustryProbabilityCallback(IndustryType type, IndustryAvailabilityCallType creation_type, uint32 default_prob)
    00542 {
    00543   const IndustrySpec *indspec = GetIndustrySpec(type);
    00544
    00545   if (HasBit(indspec->callback_mask, CBM_IND_PROBABILITY)) {
    00546     uint16 res = GetIndustryCallback(CBID_INDUSTRY_PROBABILITY, 0, creation_type, NULL, type, INVALID_TILE);
    00547     if (res != CALLBACK_FAILED) {
    00548       if (indspec->grf_prop.grffile->grf_version < 8) {
    00549         /* Disallow if result != 0 */
    00550         if (res != 0) default_prob = 0;
    00551       } else {
    00552         /* Use returned probability. 0x100 to use default */
    00553         if (res < 0x100) {
    00554           default_prob = res;
    00555         } else if (res > 0x100) {
    00556           ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_PROBABILITY, res);
    00557         }
    00558       }
    00559     }
    00560   }
    00561   return default_prob;
    00562 }
    00563
    00564 static int32 DerefIndProd(int field, bool use_register)
    00565 {
    00566   return use_register ? (int32)GetRegister(field) : field;
    00567 }
    00568
    00574 void IndustryProductionCallback(Industry *ind, int reason)
    00575 {
    00576   const IndustrySpec *spec = GetIndustrySpec(ind->type);
    00577   IndustriesResolverObject object(ind->location.tile, ind, ind->type);
    00578   if ((spec->behaviour & INDUSTRYBEH_PRODCALLBACK_RANDOM) != 0) object.callback_param1 = Random();
    00579   int multiplier = 1;
    00580   if ((spec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) multiplier = ind->prod_level;
    00581   object.callback_param2 = reason;
    00582
    00583   for (uint loop = 0;; loop++) {
    00584     /* limit the number of calls to break infinite loops.
    00585      * 'loop' is provided as 16 bits to the newgrf, so abort when those are exceeded. */
    00586     if (loop >= 0x10000) {
    00587       /* display error message */
    00588       SetDParamStr(0, spec->grf_prop.grffile->filename);
    00589       SetDParam(1, spec->name);
    00590       ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK, WL_WARNING);
    00591
    00592       /* abort the function early, this error isn't critical and will allow the game to continue to run */
    00593       break;
    00594     }
    00595
    00596     SB(object.callback_param2, 8, 16, loop);
    00597     const SpriteGroup *tgroup = SpriteGroup::Resolve(spec->grf_prop.spritegroup[0], &object);
    00598     if (tgroup == NULL || tgroup->type != SGT_INDUSTRY_PRODUCTION) break;
    00599     const IndustryProductionSpriteGroup *group = (const IndustryProductionSpriteGroup *)tgroup;
    00600
    00601     bool deref = (group->version == 1);
    00602
    00603     for (uint i = 0; i < 3; i++) {
    00604       ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->subtract_input[i], deref) * multiplier, 0, 0xFFFF);
    00605     }
    00606     for (uint i = 0; i < 2; i++) {
    00607       ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + max(DerefIndProd(group->add_output[i], deref), 0) * multiplier, 0, 0xFFFF);
    00608     }
    00609
    00610     int32 again = DerefIndProd(group->again, deref);
    00611     if (again == 0) break;
    00612
    00613     SB(object.callback_param2, 24, 8, again);
    00614   }
    00615
    00616   SetWindowDirty(WC_INDUSTRY_VIEW, ind->index);
    00617 }
    00618
    00626 bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type)
    00627 {
    00628   assert(cargo_type == ind->accepts_cargo[0] || cargo_type == ind->accepts_cargo[1] || cargo_type == ind->accepts_cargo[2]);
    00629
    00630   const IndustrySpec *indspec = GetIndustrySpec(ind->type);
    00631   if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) {
    00632     uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO,
    00633         0, indspec->grf_prop.grffile->cargo_map[cargo_type],
    00634         ind, ind->type, ind->location.tile);
    00635     if (res != CALLBACK_FAILED) return !ConvertBooleanCallback(indspec->grf_prop.grffile, CBID_INDUSTRY_REFUSE_CARGO, res);
    00636   }
    00637   return false;
    00638 }