# newgrf_industries.cpp

Go to the documentation of this file.

    00001 /* $Id: newgrf_industries.cpp 14426 2008-10-01 11:48:57Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "debug.h"
    00008 #include "variables.h"
    00009 #include "landscape.h"
    00010 #include "industry.h"
    00011 #include "industry_map.h"
    00012 #include "newgrf.h"
    00013 #include "newgrf_callbacks.h"
    00014 #include "newgrf_spritegroup.h"
    00015 #include "newgrf_industries.h"
    00016 #include "newgrf_industrytiles.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 "player_func.h"
    00023 #include "player_base.h"
    00024 #include "command_func.h"
    00025
    00026 #include "table/strings.h"
    00027
    00028 static uint32 _industry_creation_random_bits;
    00029
    00030 /* Since the industry IDs defined by the GRF file don't necessarily correlate
    00031  * to those used by the game, the IDs used for overriding old industries must be
    00032  * translated when the idustry spec is set. */
    00033 IndustryOverrideManager _industry_mngr(NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, INVALID_INDUSTRYTYPE);
    00034 IndustryTileOverrideManager _industile_mngr(NEW_INDUSTRYTILEOFFSET, NUM_INDUSTRYTILES, INVALID_INDUSTRYTILE);
    00035
    00036 IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id)
    00037 {
    00038   if (grf_type == IT_INVALID) return IT_INVALID;
    00039   if (!HasBit(grf_type, 7)) return GB(grf_type, 0, 6);
    00040
    00041   return _industry_mngr.GetID(GB(grf_type, 0, 6), grf_id);
    00042 }
    00043
    00044 static uint32 GetGRFParameter(IndustryType ind_id, byte parameter)
    00045 {
    00046   const IndustrySpec *indspec = GetIndustrySpec(ind_id);
    00047   const GRFFile *file = indspec->grf_prop.grffile;
    00048
    00049   if (parameter >= file->param_end) return 0;
    00050   return file->param[parameter];
    00051 }
    00052
    00060 static uint GetClosestWaterDistance(TileIndex tile, bool water)
    00061 {
    00062   if (IsTileType(tile, MP_WATER) == water) return 0;
    00063
    00064   uint max_dist = water ? 0x7F : 0x200;
    00065
    00066   int x = TileX(tile);
    00067   int y = TileY(tile);
    00068
    00069   uint max_x = MapMaxX();
    00070   uint max_y = MapMaxY();
    00071
    00072   /* go in a 'spiral' with increasing manhattan distance in each iteration */
    00073   for (uint dist = 1; dist < max_dist; dist++) {
    00074     /* next 'diameter' */
    00075     y--;
    00076
    00077     /* going counter-clockwise around this square */
    00078     for (DiagDirection dir = DIAGDIR_BEGIN; dir < DIAGDIR_END; dir++) {
    00079       static const int8 ddx[DIAGDIR_END] = { -1,  1,  1, -1};
    00080       static const int8 ddy[DIAGDIR_END] = {  1,  1, -1, -1};
    00081
    00082       int dx = ddx[dir];
    00083       int dy = ddy[dir];
    00084
    00085       /* each side of this square has length 'dist' */
    00086       for (uint a = 0; a < dist; a++) {
    00087         /* MP_VOID tiles are not checked (interval is [0; max) for IsInsideMM())*/
    00088         if (IsInsideMM(x, 0, max_x) && IsInsideMM(y, 0, max_y)) {
    00089           TileIndex t = TileXY(x, y);
    00090           if (IsTileType(t, MP_WATER) == water) return dist;
    00091         }
    00092         x += dx;
    00093         y += dy;
    00094       }
    00095     }
    00096   }
    00097
    00098   if (!water) {
    00099     /* no land found - is this a water-only map? */
    00100     for (TileIndex t = 0; t < MapSize(); t++) {
    00101       if (!IsTileType(t, MP_VOID) && !IsTileType(t, MP_WATER)) return 0x1FF;
    00102     }
    00103   }
    00104
    00105   return max_dist;
    00106 }
    00107
    00113 uint32 GetIndustryIDAtOffset(TileIndex tile, const Industry *i)
    00114 {
    00115   if (!IsTileType(tile, MP_INDUSTRY) || GetIndustryIndex(tile) != i->index) {
    00116     /* No industry and/or the tile does not have the same industry as the one we match it with */
    00117     return 0xFFFF;
    00118   }
    00119
    00120   IndustryGfx gfx = GetCleanIndustryGfx(tile);
    00121   const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
    00122   const IndustrySpec *indold = GetIndustrySpec(i->type);
    00123
    00124   if (gfx < NEW_INDUSTRYOFFSET) { // Does it belongs to an old type?
    00125     /* It is an old tile.  We have to see if it's been overriden */
    00126     if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) { // has it been overridden?
    00127       return 0xFF << 8 | gfx; // no. Tag FF + the gfx id of that tile
    00128     }
    00129     /* Not overriden */
    00130     const IndustryTileSpec *tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);
    00131
    00132     if (tile_ovr->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) {
    00133       return tile_ovr->grf_prop.local_id; // same grf file
    00134     } else {
    00135       return 0xFFFE; // not the same grf file
    00136     }
    00137   }
    00138   /* Not an 'old type' tile */
    00139   if (indtsp->grf_prop.spritegroup != NULL) { // tile has a spritegroup ?
    00140     if (indtsp->grf_prop.grffile->grfid == indold->grf_prop.grffile->grfid) { // same industry, same grf ?
    00141       return indtsp->grf_prop.local_id;
    00142     } else {
    00143       return 0xFFFE; // Defined in another grf file
    00144     }
    00145   }
    00146   /* The tile has no spritegroup */
    00147   return 0xFF << 8 | indtsp->grf_prop.subst_id; // so just give him the substitute
    00148 }
    00149
    00150 static uint32 GetClosestIndustry(TileIndex tile, IndustryType type, const Industry *current)
    00151 {
    00152   uint32 best_dist = MAX_UVALUE(uint32);
    00153   const Industry *i;
    00154   FOR_ALL_INDUSTRIES(i) {
    00155     if (i->type != type || i == current) continue;
    00156
    00157     best_dist = min(best_dist, DistanceManhattan(tile, i->xy));
    00158   }
    00159
    00160   return best_dist;
    00161 }
    00162
    00171 static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout_filter, const Industry *current)
    00172 {
    00173   uint32 GrfID = GetRegister(0x100);
    00174   IndustryType ind_index;
    00175   uint32 closest_dist = MAX_UVALUE(uint32);
    00176   byte count = 0;
    00177
    00178   /* Determine what will be the industry type to look for */
    00179   switch (GrfID) {
    00180     case 0:  // this is a default industry type
    00181       ind_index = param_setID;
    00182       break;
    00183
    00184     case 0xFFFFFFFF: // current grf
    00185       GrfID = GetIndustrySpec(current->type)->grf_prop.grffile->grfid;
    00186       /* Fall through */
    00187
    00188     default: //use the grfid specified in register 100h
    00189       SetBit(param_setID, 7); // bit 7 means it is not an old type
    00190       ind_index = MapNewGRFIndustryType(param_setID, GrfID);
    00191       break;
    00192   }
    00193
    00194   if (layout_filter == 0) {
    00195     /* If the filter is 0, it could be because none was specified as well as being really a 0.
    00196      * In either case, just do the regular var67 */
    00197     closest_dist = GetClosestIndustry(current->xy, ind_index, current);
    00198     count = GetIndustryTypeCount(ind_index);
    00199   } else {
    00200     /* Count only those who match the same industry type and layout filter
    00201      * Unfortunately, we have to do it manually */
    00202     const Industry *i;
    00203     FOR_ALL_INDUSTRIES(i) {
    00204       if (i->type == ind_index && i != current && i->selected_layout == layout_filter) {
    00205         closest_dist = min(closest_dist, DistanceManhattan(current->xy, i->xy));
    00206         count++;
    00207       }
    00208     }
    00209   }
    00210
    00211   return count << 16 | GB(closest_dist, 0, 16);
    00212 }
    00213
    00220 uint32 IndustryGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
    00221 {
    00222   const Industry *industry = object->u.industry.ind;
    00223   TileIndex tile = object->u.industry.tile;
    00224   IndustryType type = object->u.industry.type;
    00225   const IndustrySpec *indspec = GetIndustrySpec(type);
    00226
    00227   /* Shall the variable get resolved in parent scope and are we not yet in parent scope? */
    00228   if (object->u.industry.gfx == INVALID_INDUSTRYTILE && object->scope == VSG_SCOPE_PARENT) {
    00229     /* Pass the request on to the town of the industry */
    00230     const Town *t;
    00231
    00232     if (industry != NULL) {
    00233       t = industry->town;
    00234     } else if (tile != INVALID_TILE) {
    00235       t = ClosestTownFromTile(tile, UINT_MAX);
    00236     } else {
    00237       *available = false;
    00238       return UINT_MAX;
    00239     }
    00240
    00241     return TownGetVariable(variable, parameter, available, t);
    00242   }
    00243
    00244   if (industry == NULL) {
    00245     /* industry does not exist, only use those variables that are "safe" */
    00246     switch (variable) {
    00247       /* Read GRF parameter */
    00248       case 0x7F: return GetGRFParameter(type, parameter);
    00249       /* Manhattan distance of closes dry/water tile */
    00250       case 0x43: return GetClosestWaterDistance(tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
    00251     }
    00252
    00253     DEBUG(grf, 1, "Unhandled property 0x%X (no available industry) in callback 0x%x", variable, object->callback);
    00254
    00255     *available = false;
    00256     return UINT_MAX;
    00257   }
    00258
    00259   switch (variable) {
    00260     case 0x40:
    00261     case 0x41:
    00262     case 0x42: { // waiting cargo, but only if those two callback flags are set
    00263       uint16 callback = indspec->callback_flags;
    00264       if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
    00265         if ((indspec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) {
    00266           return min(industry->incoming_cargo_waiting[variable - 0x40] / industry->prod_level, (uint16)0xFFFF);
    00267         } else {
    00268           return min(industry->incoming_cargo_waiting[variable - 0x40], (uint16)0xFFFF);
    00269         }
    00270       } else {
    00271         return 0;
    00272       }
    00273     }
    00274
    00275     /* Manhattan distance of closes dry/water tile */
    00276     case 0x43: return GetClosestWaterDistance(tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
    00277
    00278     /* Layout number */
    00279     case 0x44: return industry->selected_layout;
    00280
    00281     /* player info */
    00282     case 0x45: {
    00283       byte colours;
    00284       bool is_ai = false;
    00285
    00286       if (IsValidPlayer(industry->founder)) {
    00287         const Player *p = GetPlayer(industry->founder);
    00288         const Livery *l = &p->livery[LS_DEFAULT];
    00289
    00290         is_ai = p->is_ai;
    00291         colours = l->colour1 + l->colour2 * 16;
    00292       } else {
    00293         colours = GB(Random(), 0, 8);
    00294       }
    00295
    00296       return industry->founder | (is_ai ? 0x10000 : 0) | (colours << 24);
    00297     }
    00298
    00299     /* Get industry ID at offset param */
    00300     case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, industry->xy), industry);
    00301
    00302     /* Get random tile bits at offset param */
    00303     case 0x61:
    00304       tile = GetNearbyTile(parameter, tile);
    00305       return (IsTileType(tile, MP_INDUSTRY) && GetIndustryByTile(tile) == industry) ? GetIndustryRandomBits(tile) : 0;
    00306
    00307     /* Land info of nearby tiles */
    00308     case 0x62: return GetNearbyIndustryTileInformation(parameter, tile, INVALID_INDUSTRY);
    00309
    00310     /* Animation stage of nearby tiles */
    00311     case 0x63:
    00312       tile = GetNearbyTile(parameter, tile);
    00313       if (IsTileType(tile, MP_INDUSTRY) && GetIndustryByTile(tile) == industry) {
    00314         return GetIndustryAnimationState(tile);
    00315       }
    00316       return 0xFFFFFFFF;
    00317
    00318     /* Distance of nearest industry of given type */
    00319     case 0x64: return GetClosestIndustry(tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), industry);
    00320     /* Get town zone and Manhattan distance of closest town */
    00321     case 0x65: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceManhattan(tile, industry->town->xy), 0xFFFF);
    00322     /* Get square of Euclidian distance of closes town */
    00323     case 0x66: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceSquare(tile, industry->town->xy), 0xFFFF);
    00324
    00325     /* Count of industry, distance of closest instance
    00326      * 68 is the same as 67, but with a filtering on selected layout */
    00327     case 0x67:
    00328     case 0x68: return GetCountAndDistanceOfClosestInstance(parameter, variable == 0x68 ? GB(GetRegister(0x101), 0, 8) : 0, industry);
    00329
    00330     /* Get a variable from the persistent storage */
    00331     case 0x7C: return industry->psa.Get(parameter);
    00332
    00333     /* Read GRF parameter */
    00334     case 0x7F: return GetGRFParameter(type, parameter);
    00335
    00336     /* Industry structure access*/
    00337     case 0x80: return industry->xy;
    00338     case 0x81: return GB(industry->xy, 8, 8);
    00339     /* Pointer to the town the industry is associated with */
    00340     case 0x82: return industry->town->index;
    00341     case 0x83:
    00342     case 0x84:
    00343     case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
    00344     case 0x86: return industry->width;
    00345     case 0x87: return industry->height;// xy dimensions
    00346     /*  */
    00347     case 0x88:
    00348     case 0x89: return industry->produced_cargo[variable - 0x88];
    00349     case 0x8A: return industry->produced_cargo_waiting[0];
    00350     case 0x8B: return GB(industry->produced_cargo_waiting[0], 8, 8);
    00351     case 0x8C: return industry->produced_cargo_waiting[1];
    00352     case 0x8D: return GB(industry->produced_cargo_waiting[1], 8, 8);
    00353     case 0x8E:
    00354     case 0x8F: return industry->production_rate[variable - 0x8E];
    00355     case 0x90:
    00356     case 0x91:
    00357     case 0x92: return industry->accepts_cargo[variable - 0x90];
    00358     case 0x93: return industry->prod_level;
    00359     /* amount of cargo produced so far THIS month. */
    00360     case 0x94: return industry->this_month_production[0];
    00361     case 0x95: return GB(industry->this_month_production[0], 8, 8);
    00362     case 0x96: return industry->this_month_production[1];
    00363     case 0x97: return GB(industry->this_month_production[1], 8, 8);
    00364     /* amount of cargo transported so far THIS month. */
    00365     case 0x98: return industry->this_month_transported[0];
    00366     case 0x99: return GB(industry->this_month_transported[0], 8, 8);
    00367     case 0x9A: return industry->this_month_transported[1];
    00368     case 0x9B: return GB(industry->this_month_transported[0], 8, 8);
    00369     /* fraction of cargo transported LAST month. */
    00370     case 0x9C:
    00371     case 0x9D: return industry->last_month_pct_transported[variable - 0x9C];
    00372     /* amount of cargo produced LAST month. */
    00373     case 0x9E: return industry->last_month_production[0];
    00374     case 0x9F: return GB(industry->last_month_production[0], 8, 8);
    00375     case 0xA0: return industry->last_month_production[1];
    00376     case 0xA1: return GB(industry->last_month_production[1], 8, 8);
    00377     /* amount of cargo transported last month. */
    00378     case 0xA2: return industry->last_month_transported[0];
    00379     case 0xA3: return GB(industry->last_month_transported[0], 8, 8);
    00380     case 0xA4: return industry->last_month_transported[1];
    00381     case 0xA5: return GB(industry->last_month_transported[0], 8, 8);
    00382
    00383     case 0xA6: return industry->type;
    00384     case 0xA7: return industry->founder;
    00385     case 0xA8: return industry->random_color;
    00386     case 0xA9: return Clamp(industry->last_prod_year - ORIGINAL_BASE_YEAR, 0, 255);
    00387     case 0xAA: return industry->counter;
    00388     case 0xAB: return GB(industry->counter, 8, 8);
    00389     case 0xAC: return industry->was_cargo_delivered;
    00390
    00391     case 0xB0: return Clamp(industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date when built since 1920 (in days)
    00392     case 0xB3: return industry->construction_type; // Construction type
    00393     case 0xB4: return Clamp(industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535); // Date last cargo accepted since 1920 (in days)
    00394   }
    00395
    00396   DEBUG(grf, 1, "Unhandled industry property 0x%X", variable);
    00397
    00398   *available = false;
    00399   return (uint32)-1;
    00400 }
    00401
    00402 static const SpriteGroup *IndustryResolveReal(const ResolverObject *object, const SpriteGroup *group)
    00403 {
    00404   /* IndustryTile do not have 'real' groups */
    00405   return NULL;
    00406 }
    00407
    00408 static uint32 IndustryGetRandomBits(const ResolverObject *object)
    00409 {
    00410   return object->u.industry.ind == NULL ? 0 : object->u.industry.ind->random;
    00411 }
    00412
    00413 static uint32 IndustryGetTriggers(const ResolverObject *object)
    00414 {
    00415   return object->u.industry.ind == NULL ? 0 : object->u.industry.ind->random_triggers;
    00416 }
    00417
    00418 static void IndustrySetTriggers(const ResolverObject *object, int triggers)
    00419 {
    00420   if (object->u.industry.ind == NULL) return;
    00421   object->u.industry.ind->random_triggers = triggers;
    00422 }
    00423
    00424 static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus, IndustryType type)
    00425 {
    00426   res->GetRandomBits = IndustryGetRandomBits;
    00427   res->GetTriggers   = IndustryGetTriggers;
    00428   res->SetTriggers   = IndustrySetTriggers;
    00429   res->GetVariable   = IndustryGetVariable;
    00430   res->ResolveReal   = IndustryResolveReal;
    00431
    00432   res->psa             = &indus->psa;
    00433   res->u.industry.tile = tile;
    00434   res->u.industry.ind  = indus;
    00435   res->u.industry.gfx  = INVALID_INDUSTRYTILE;
    00436   res->u.industry.type = type;
    00437
    00438   res->callback        = CBID_NO_CALLBACK;
    00439   res->callback_param1 = 0;
    00440   res->callback_param2 = 0;
    00441   res->last_value      = 0;
    00442   res->trigger         = 0;
    00443   res->reseed          = 0;
    00444 }
    00445
    00446 uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
    00447 {
    00448   ResolverObject object;
    00449   const SpriteGroup *group;
    00450
    00451   NewIndustryResolver(&object, tile, industry, type);
    00452   object.callback = callback;
    00453   object.callback_param1 = param1;
    00454   object.callback_param2 = param2;
    00455
    00456   group = Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
    00457   if (group == NULL || group->type != SGT_CALLBACK) return CALLBACK_FAILED;
    00458
    00459   return group->g.callback.result;
    00460 }
    00461
    00462 uint32 IndustryLocationGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
    00463 {
    00464   const Industry *industry = object->u.industry.ind;
    00465   TileIndex tile = object->u.industry.tile;
    00466
    00467   if (object->scope == VSG_SCOPE_PARENT) {
    00468     return TownGetVariable(variable, parameter, available, industry->town);
    00469   }
    00470
    00471   switch (variable) {
    00472     case 0x80: return tile;
    00473     case 0x81: return GB(tile, 8, 8);
    00474
    00475     /* Pointer to the town the industry is associated with */
    00476     case 0x82: return industry->town->index;
    00477     case 0x83:
    00478     case 0x84:
    00479     case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break; // not supported
    00480
    00481     /* Number of the layout */
    00482     case 0x86: return industry->selected_layout;
    00483
    00484     /* Ground type */
    00485     case 0x87: return GetTerrainType(tile);
    00486
    00487     /* Town zone */
    00488     case 0x88: return GetTownRadiusGroup(industry->town, tile);
    00489
    00490     /* Manhattan distance of the closest town */
    00491     case 0x89: return min(DistanceManhattan(industry->town->xy, tile), 255);
    00492
    00493     /* Lowest height of the tile */
    00494     case 0x8A: return GetTileZ(tile);
    00495
    00496     /* Distance to the nearest water/land tile */
    00497     case 0x8B: return GetClosestWaterDistance(tile, (GetIndustrySpec(industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
    00498
    00499     /* Square of Euclidian distance from town */
    00500     case 0x8D: return min(DistanceSquare(industry->town->xy, tile), 65535);
    00501
    00502     /* 32 random bits */
    00503     case 0x8F: return _industry_creation_random_bits;
    00504   }
    00505
    00506   /* None of the special ones, so try the general ones */
    00507   return IndustryGetVariable(object, variable, parameter, available);
    00508 }
    00509
    00510 bool CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint itspec_index, uint32 seed)
    00511 {
    00512   const IndustrySpec *indspec = GetIndustrySpec(type);
    00513
    00514   ResolverObject object;
    00515   const SpriteGroup *group;
    00516
    00517   Industry ind;
    00518   ind.index = INVALID_INDUSTRY;
    00519   ind.xy = tile;
    00520   ind.width = 0;
    00521   ind.type = type;
    00522   ind.selected_layout = itspec_index;
    00523   ind.town = ClosestTownFromTile(tile, (uint)-1);
    00524
    00525   NewIndustryResolver(&object, tile, &ind, type);
    00526   object.GetVariable = IndustryLocationGetVariable;
    00527   object.callback = CBID_INDUSTRY_LOCATION;
    00528   _industry_creation_random_bits = seed;
    00529
    00530   group = Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
    00531
    00532   /* Unlike the "normal" cases, not having a valid result means we allow
    00533    * the building of the industry, as that's how it's done in TTDP. */
    00534   if (group == NULL || group->type != SGT_CALLBACK) return true;
    00535
    00536   /* Copy some parameters from the registers to the error message text ref. stack */
    00537   SwitchToErrorRefStack();
    00538   PrepareTextRefStackUsage(4);
    00539   SwitchToNormalRefStack();
    00540
    00541   switch (group->g.callback.result) {
    00542     case 0x400: return true;
    00543     case 0x401: _error_message = STR_0239_SITE_UNSUITABLE; break;
    00544     case 0x402: _error_message = STR_0317_CAN_ONLY_BE_BUILT_IN_RAINFOREST; break;
    00545     case 0x403: _error_message = STR_0318_CAN_ONLY_BE_BUILT_IN_DESERT; break;
    00546     default: _error_message = GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + group->g.callback.result); break;
    00547   }
    00548
    00549   return false;
    00550 }
    00551
    00552 bool CheckIfCallBackAllowsAvailability(IndustryType type, IndustryAvailabilityCallType creation_type)
    00553 {
    00554   const IndustrySpec *indspec = GetIndustrySpec(type);
    00555
    00556   if (HasBit(indspec->callback_flags, CBM_IND_AVAILABLE)) {
    00557     uint16 res = GetIndustryCallback(CBID_INDUSTRY_AVAILABLE, 0, creation_type, NULL, type, INVALID_TILE);
    00558     if (res != CALLBACK_FAILED) {
    00559       return (res == 0);
    00560     }
    00561   }
    00562   return true;
    00563 }
    00564
    00565 static int32 DerefIndProd(uint field, bool use_register)
    00566 {
    00567   return use_register ? (int32)GetRegister(field) : field;
    00568 }
    00569
    00575 void IndustryProductionCallback(Industry *ind, int reason)
    00576 {
    00577   const IndustrySpec *spec = GetIndustrySpec(ind->type);
    00578   ResolverObject object;
    00579   NewIndustryResolver(&object, ind->xy, ind, ind->type);
    00580   if ((spec->behaviour & INDUSTRYBEH_PRODCALLBACK_RANDOM) != 0) object.callback_param1 = Random();
    00581   int multiplier = 1;
    00582   if ((spec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) multiplier = ind->prod_level;
    00583   object.callback_param2 = reason;
    00584
    00585   for (uint loop = 0;; loop++) {
    00586     SB(object.callback_param2, 8, 16, loop);
    00587     const SpriteGroup *group = Resolve(spec->grf_prop.spritegroup, &object);
    00588     if (group == NULL || group->type != SGT_INDUSTRY_PRODUCTION) break;
    00589
    00590     bool deref = (group->g.indprod.version == 1);
    00591
    00592     for (uint i = 0; i < 3; i++) {
    00593       ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->g.indprod.substract_input[i], deref) * multiplier, 0, 0xFFFF);
    00594     }
    00595     for (uint i = 0; i < 2; i++) {
    00596       ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + max(DerefIndProd(group->g.indprod.add_output[i], deref), 0) * multiplier, 0, 0xFFFF);
    00597     }
    00598
    00599     int32 again = DerefIndProd(group->g.indprod.again, deref);
    00600     if (again == 0) break;
    00601
    00602     SB(object.callback_param2, 24, 8, again);
    00603   }
    00604
    00605   InvalidateWindow(WC_INDUSTRY_VIEW, ind->index);
    00606 }