00001
00002
00003
00004
00005
00006
00007
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
00028
00029
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 (!i->TileBelongsToIndustry(tile)) {
00058
00059 return 0xFFFF;
00060 }
00061
00062 IndustryGfx gfx = GetCleanIndustryGfx(tile);
00063 const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
00064
00065 if (gfx < NEW_INDUSTRYTILEOFFSET) {
00066
00067 if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) {
00068 return 0xFF << 8 | gfx;
00069 }
00070
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;
00075 } else {
00076 return 0xFFFE;
00077 }
00078 }
00079
00080 if (indtsp->grf_prop.spritegroup[0] != NULL) {
00081 if (indtsp->grf_prop.grffile->grfid == cur_grfid) {
00082 return indtsp->grf_prop.local_id;
00083 } else {
00084 return 0xFFFE;
00085 }
00086 }
00087
00088 return 0xFF << 8 | indtsp->grf_prop.subst_id;
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
00122 switch (GrfID) {
00123 case 0:
00124 ind_index = param_setID;
00125 break;
00126
00127 case 0xFFFFFFFF:
00128 GrfID = GetIndustrySpec(current->type)->grf_prop.grffile->grfid;
00129
00130
00131 default:
00132 SetBit(param_setID, 7);
00133 ind_index = MapNewGRFIndustryType(param_setID, GrfID);
00134 break;
00135 }
00136
00137
00138 if (ind_index >= NUM_INDUSTRYTYPES) return 0 | 0xFFFF;
00139
00140 if (layout_filter == 0 && !town_filter) {
00141
00142
00143 closest_dist = GetClosestIndustry(current->location.tile, ind_index, current);
00144 count = min(Industry::GetIndustryTypeCount(ind_index), UINT8_MAX);
00145 } else {
00146
00147
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 uint32 IndustriesScopeResolver::GetVariable(byte variable, uint32 parameter, bool *available) const
00161 {
00162 if (this->ro.callback == CBID_INDUSTRY_LOCATION) {
00163
00164
00165 switch (variable) {
00166 case 0x80: return this->tile;
00167 case 0x81: return GB(this->tile, 8, 8);
00168
00169
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;
00174
00175
00176 case 0x86: return this->industry->selected_layout;
00177
00178
00179 case 0x87: return GetTerrainType(this->tile);
00180
00181
00182 case 0x88: return GetTownRadiusGroup(this->industry->town, this->tile);
00183
00184
00185 case 0x89: return min(DistanceManhattan(this->industry->town->xy, this->tile), 255);
00186
00187
00188 case 0x8A: return Clamp(GetTileZ(this->tile) * (this->ro.grffile->grf_version >= 8 ? 1 : TILE_HEIGHT), 0, 0xFF);
00189
00190
00191 case 0x8B: return GetClosestWaterDistance(this->tile, (GetIndustrySpec(this->industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
00192
00193
00194 case 0x8D: return min(DistanceSquare(this->industry->town->xy, this->tile), 65535);
00195
00196
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: {
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
00228 case 0x43:
00229 if (this->tile == INVALID_TILE) break;
00230 return GetClosestWaterDistance(this->tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
00231
00232
00233 case 0x44: return this->industry->selected_layout;
00234
00235
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;
00252
00253
00254 case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, this->industry->location.tile, false), this->industry, this->ro.grffile->grfid);
00255
00256
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
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
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
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
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
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
00292
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
00306 case 0x7C: return (this->industry->psa != NULL) ? this->industry->psa->GetValue(parameter) : 0;
00307
00308
00309 case 0x80: return this->industry->location.tile;
00310 case 0x81: return GB(this->industry->location.tile, 8, 8);
00311
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;
00316 case 0x86: return this->industry->location.w;
00317 case 0x87: return this->industry->location.h;
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
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
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
00342 case 0x9C:
00343 case 0x9D: return this->industry->last_month_pct_transported[variable - 0x9C];
00344
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
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);
00364 case 0xB3: return this->industry->construction_type;
00365 case 0xB4: return Clamp(this->industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00366 }
00367
00368 DEBUG(grf, 1, "Unhandled industry variable 0x%X", variable);
00369
00370 *available = false;
00371 return UINT_MAX;
00372 }
00373
00374 uint32 IndustriesScopeResolver::GetRandomBits() const
00375 {
00376 return this->industry != NULL ? this->industry->random : 0;
00377 }
00378
00379 uint32 IndustriesScopeResolver::GetTriggers() const
00380 {
00381 return this->industry != NULL ? this->industry->random_triggers : 0;
00382 }
00383
00384 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 void IndustriesScopeResolver::StorePSA(uint pos, int32 value)
00391 {
00392 if (this->industry->index == INVALID_INDUSTRY) return;
00393
00394 if (this->industry->psa == NULL) {
00395
00396 if (value == 0) return;
00397
00398
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, GSF_INDUSTRIES, this->industry->location.tile);
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 bool readonly = true;
00451 if (this->industries_scope.industry != NULL) {
00452 t = this->industries_scope.industry->town;
00453 readonly = this->industries_scope.industry->index == INVALID_INDUSTRY;
00454 } else if (this->industries_scope.tile != INVALID_TILE) {
00455 t = ClosestTownFromTile(this->industries_scope.tile, UINT_MAX);
00456 }
00457 if (t == NULL) return NULL;
00458 this->town_scope = new TownScopeResolver(*this, t, readonly);
00459 }
00460 return this->town_scope;
00461 }
00462
00471 IndustriesScopeResolver::IndustriesScopeResolver(ResolverObject &ro, TileIndex tile, Industry *industry, IndustryType type, uint32 random_bits)
00472 : ScopeResolver(ro)
00473 {
00474 this->tile = tile;
00475 this->industry = industry;
00476 this->type = type;
00477 this->random_bits = random_bits;
00478 }
00479
00490 uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
00491 {
00492 IndustriesResolverObject object(tile, industry, type, 0, callback, param1, param2);
00493 const SpriteGroup *group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], object);
00494 if (group == NULL) return CALLBACK_FAILED;
00495
00496 return group->GetCallbackResult();
00497 }
00498
00510 CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
00511 {
00512 const IndustrySpec *indspec = GetIndustrySpec(type);
00513
00514 Industry ind;
00515 ind.index = INVALID_INDUSTRY;
00516 ind.location.tile = tile;
00517 ind.location.w = 0;
00518 ind.type = type;
00519 ind.selected_layout = layout;
00520 ind.town = ClosestTownFromTile(tile, UINT_MAX);
00521 ind.random = initial_random_bits;
00522 ind.founder = founder;
00523 ind.psa = NULL;
00524
00525 IndustriesResolverObject object(tile, &ind, type, seed, CBID_INDUSTRY_LOCATION, 0, creation_type);
00526 const SpriteGroup *group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], object);
00527
00528
00529
00530 if (group == NULL) return CommandCost();
00531 uint16 result = group->GetCallbackResult();
00532 if (result == CALLBACK_FAILED) return CommandCost();
00533
00534 return GetErrorMessageFromLocationCallbackResult(result, indspec->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
00535 }
00536
00543 uint32 GetIndustryProbabilityCallback(IndustryType type, IndustryAvailabilityCallType creation_type, uint32 default_prob)
00544 {
00545 const IndustrySpec *indspec = GetIndustrySpec(type);
00546
00547 if (HasBit(indspec->callback_mask, CBM_IND_PROBABILITY)) {
00548 uint16 res = GetIndustryCallback(CBID_INDUSTRY_PROBABILITY, 0, creation_type, NULL, type, INVALID_TILE);
00549 if (res != CALLBACK_FAILED) {
00550 if (indspec->grf_prop.grffile->grf_version < 8) {
00551
00552 if (res != 0) default_prob = 0;
00553 } else {
00554
00555 if (res < 0x100) {
00556 default_prob = res;
00557 } else if (res > 0x100) {
00558 ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_PROBABILITY, res);
00559 }
00560 }
00561 }
00562 }
00563 return default_prob;
00564 }
00565
00566 static int32 DerefIndProd(int field, bool use_register)
00567 {
00568 return use_register ? (int32)GetRegister(field) : field;
00569 }
00570
00576 void IndustryProductionCallback(Industry *ind, int reason)
00577 {
00578 const IndustrySpec *spec = GetIndustrySpec(ind->type);
00579 IndustriesResolverObject object(ind->location.tile, 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
00587
00588 if (loop >= 0x10000) {
00589
00590 SetDParamStr(0, spec->grf_prop.grffile->filename);
00591 SetDParam(1, spec->name);
00592 ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK, WL_WARNING);
00593
00594
00595 break;
00596 }
00597
00598 SB(object.callback_param2, 8, 16, loop);
00599 const SpriteGroup *tgroup = SpriteGroup::Resolve(spec->grf_prop.spritegroup[0], object);
00600 if (tgroup == NULL || tgroup->type != SGT_INDUSTRY_PRODUCTION) break;
00601 const IndustryProductionSpriteGroup *group = (const IndustryProductionSpriteGroup *)tgroup;
00602
00603 bool deref = (group->version == 1);
00604
00605 for (uint i = 0; i < 3; i++) {
00606 ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->subtract_input[i], deref) * multiplier, 0, 0xFFFF);
00607 }
00608 for (uint i = 0; i < 2; i++) {
00609 ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + max(DerefIndProd(group->add_output[i], deref), 0) * multiplier, 0, 0xFFFF);
00610 }
00611
00612 int32 again = DerefIndProd(group->again, deref);
00613 if (again == 0) break;
00614
00615 SB(object.callback_param2, 24, 8, again);
00616 }
00617
00618 SetWindowDirty(WC_INDUSTRY_VIEW, ind->index);
00619 }
00620
00628 bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type)
00629 {
00630 assert(cargo_type == ind->accepts_cargo[0] || cargo_type == ind->accepts_cargo[1] || cargo_type == ind->accepts_cargo[2]);
00631
00632 const IndustrySpec *indspec = GetIndustrySpec(ind->type);
00633 if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) {
00634 uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO,
00635 0, indspec->grf_prop.grffile->cargo_map[cargo_type],
00636 ind, ind->type, ind->location.tile);
00637 if (res != CALLBACK_FAILED) return !ConvertBooleanCallback(indspec->grf_prop.grffile, CBID_INDUSTRY_REFUSE_CARGO, res);
00638 }
00639 return false;
00640 }