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 static uint32 _industry_creation_random_bits;
00028
00029
00030
00031
00032 IndustryOverrideManager _industry_mngr(NEW_INDUSTRYOFFSET, NUM_INDUSTRYTYPES, INVALID_INDUSTRYTYPE);
00033 IndustryTileOverrideManager _industile_mngr(NEW_INDUSTRYTILEOFFSET, NUM_INDUSTRYTILES, INVALID_INDUSTRYTILE);
00034
00041 IndustryType MapNewGRFIndustryType(IndustryType grf_type, uint32 grf_id)
00042 {
00043 if (grf_type == IT_INVALID) return IT_INVALID;
00044 if (!HasBit(grf_type, 7)) return GB(grf_type, 0, 6);
00045
00046 return _industry_mngr.GetID(GB(grf_type, 0, 6), grf_id);
00047 }
00048
00057 uint32 GetIndustryIDAtOffset(TileIndex tile, const Industry *i, uint32 cur_grfid)
00058 {
00059 if (!IsTileType(tile, MP_INDUSTRY) || GetIndustryIndex(tile) != i->index) {
00060
00061 return 0xFFFF;
00062 }
00063
00064 IndustryGfx gfx = GetCleanIndustryGfx(tile);
00065 const IndustryTileSpec *indtsp = GetIndustryTileSpec(gfx);
00066
00067 if (gfx < NEW_INDUSTRYTILEOFFSET) {
00068
00069 if (indtsp->grf_prop.override == INVALID_INDUSTRYTILE) {
00070 return 0xFF << 8 | gfx;
00071 }
00072
00073 const IndustryTileSpec *tile_ovr = GetIndustryTileSpec(indtsp->grf_prop.override);
00074
00075 if (tile_ovr->grf_prop.grffile->grfid == cur_grfid) {
00076 return tile_ovr->grf_prop.local_id;
00077 } else {
00078 return 0xFFFE;
00079 }
00080 }
00081
00082 if (indtsp->grf_prop.spritegroup[0] != NULL) {
00083 if (indtsp->grf_prop.grffile->grfid == cur_grfid) {
00084 return indtsp->grf_prop.local_id;
00085 } else {
00086 return 0xFFFE;
00087 }
00088 }
00089
00090 return 0xFF << 8 | indtsp->grf_prop.subst_id;
00091 }
00092
00093 static uint32 GetClosestIndustry(TileIndex tile, IndustryType type, const Industry *current)
00094 {
00095 uint32 best_dist = UINT32_MAX;
00096 const Industry *i;
00097 FOR_ALL_INDUSTRIES(i) {
00098 if (i->type != type || i == current) continue;
00099
00100 best_dist = min(best_dist, DistanceManhattan(tile, i->location.tile));
00101 }
00102
00103 return best_dist;
00104 }
00105
00116 static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout_filter, bool town_filter, const Industry *current)
00117 {
00118 uint32 GrfID = GetRegister(0x100);
00119 IndustryType ind_index;
00120 uint32 closest_dist = UINT32_MAX;
00121 byte count = 0;
00122
00123
00124 switch (GrfID) {
00125 case 0:
00126 ind_index = param_setID;
00127 break;
00128
00129 case 0xFFFFFFFF:
00130 GrfID = GetIndustrySpec(current->type)->grf_prop.grffile->grfid;
00131
00132
00133 default:
00134 SetBit(param_setID, 7);
00135 ind_index = MapNewGRFIndustryType(param_setID, GrfID);
00136 break;
00137 }
00138
00139
00140 if (ind_index >= NUM_INDUSTRYTYPES) return 0 | 0xFFFF;
00141
00142 if (layout_filter == 0 && !town_filter) {
00143
00144
00145 closest_dist = GetClosestIndustry(current->location.tile, ind_index, current);
00146 count = min(Industry::GetIndustryTypeCount(ind_index), UINT8_MAX);
00147 } else {
00148
00149
00150 const Industry *i;
00151 FOR_ALL_INDUSTRIES(i) {
00152 if (i->type == ind_index && i != current && (i->selected_layout == layout_filter || layout_filter == 0) && (!town_filter || i->town == current->town)) {
00153 closest_dist = min(closest_dist, DistanceManhattan(current->location.tile, i->location.tile));
00154 count++;
00155 }
00156 }
00157 }
00158
00159 return count << 16 | GB(closest_dist, 0, 16);
00160 }
00161
00170 uint32 IndustryGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00171 {
00172 const Industry *industry = object->u.industry.ind;
00173 TileIndex tile = object->u.industry.tile;
00174 IndustryType type = object->u.industry.type;
00175 const IndustrySpec *indspec = GetIndustrySpec(type);
00176
00177
00178 if (object->u.industry.gfx == INVALID_INDUSTRYTILE && object->scope == VSG_SCOPE_PARENT) {
00179
00180 Town *t;
00181
00182 if (industry != NULL) {
00183 t = industry->town;
00184 } else if (tile != INVALID_TILE) {
00185 t = ClosestTownFromTile(tile, UINT_MAX);
00186 } else {
00187 *available = false;
00188 return UINT_MAX;
00189 }
00190
00191 return TownGetVariable(variable, parameter, available, t, object->grffile);
00192 }
00193
00194 if (industry == NULL) {
00195 DEBUG(grf, 1, "Unhandled variable 0x%X (no available industry) in callback 0x%x", variable, object->callback);
00196
00197 *available = false;
00198 return UINT_MAX;
00199 }
00200
00201 switch (variable) {
00202 case 0x40:
00203 case 0x41:
00204 case 0x42: {
00205 uint16 callback = indspec->callback_mask;
00206 if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
00207 if ((indspec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) {
00208 if (industry->prod_level == 0) return 0;
00209 return min(industry->incoming_cargo_waiting[variable - 0x40] / industry->prod_level, (uint16)0xFFFF);
00210 } else {
00211 return min(industry->incoming_cargo_waiting[variable - 0x40], (uint16)0xFFFF);
00212 }
00213 } else {
00214 return 0;
00215 }
00216 }
00217
00218
00219 case 0x43: return GetClosestWaterDistance(tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
00220
00221
00222 case 0x44: return industry->selected_layout;
00223
00224
00225 case 0x45: {
00226 byte colours = 0;
00227 bool is_ai = false;
00228
00229 const Company *c = Company::GetIfValid(industry->founder);
00230 if (c != NULL) {
00231 const Livery *l = &c->livery[LS_DEFAULT];
00232
00233 is_ai = c->is_ai;
00234 colours = l->colour1 + l->colour2 * 16;
00235 }
00236
00237 return industry->founder | (is_ai ? 0x10000 : 0) | (colours << 24);
00238 }
00239
00240 case 0x46: return industry->construction_date;
00241
00242
00243 case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, industry->location.tile, false), industry, object->grffile->grfid);
00244
00245
00246 case 0x61:
00247 tile = GetNearbyTile(parameter, tile, false);
00248 return industry->TileBelongsToIndustry(tile) ? GetIndustryRandomBits(tile) : 0;
00249
00250
00251 case 0x62: return GetNearbyIndustryTileInformation(parameter, tile, INVALID_INDUSTRY, false, object->grffile->grf_version >= 8);
00252
00253
00254 case 0x63:
00255 tile = GetNearbyTile(parameter, tile, false);
00256 if (industry->TileBelongsToIndustry(tile)) {
00257 return GetAnimationFrame(tile);
00258 }
00259 return 0xFFFFFFFF;
00260
00261
00262 case 0x64: return GetClosestIndustry(tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), industry);
00263
00264 case 0x65: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceManhattan(tile, industry->town->xy), 0xFFFF);
00265
00266 case 0x66: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceSquare(tile, industry->town->xy), 0xFFFF);
00267
00268
00269
00270 case 0x67:
00271 case 0x68: {
00272 byte layout_filter = 0;
00273 bool town_filter = false;
00274 if (variable == 0x68) {
00275 uint32 reg = GetRegister(0x101);
00276 layout_filter = GB(reg, 0, 8);
00277 town_filter = HasBit(reg, 8);
00278 }
00279 return GetCountAndDistanceOfClosestInstance(parameter, layout_filter, town_filter, industry);
00280 }
00281
00282
00283 case 0x7C: return (industry->psa != NULL) ? industry->psa->GetValue(parameter) : 0;
00284
00285
00286 case 0x80: return industry->location.tile;
00287 case 0x81: return GB(industry->location.tile, 8, 8);
00288
00289 case 0x82: return industry->town->index;
00290 case 0x83:
00291 case 0x84:
00292 case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break;
00293 case 0x86: return industry->location.w;
00294 case 0x87: return industry->location.h;
00295
00296 case 0x88:
00297 case 0x89: return industry->produced_cargo[variable - 0x88];
00298 case 0x8A: return industry->produced_cargo_waiting[0];
00299 case 0x8B: return GB(industry->produced_cargo_waiting[0], 8, 8);
00300 case 0x8C: return industry->produced_cargo_waiting[1];
00301 case 0x8D: return GB(industry->produced_cargo_waiting[1], 8, 8);
00302 case 0x8E:
00303 case 0x8F: return industry->production_rate[variable - 0x8E];
00304 case 0x90:
00305 case 0x91:
00306 case 0x92: return industry->accepts_cargo[variable - 0x90];
00307 case 0x93: return industry->prod_level;
00308
00309 case 0x94: return industry->this_month_production[0];
00310 case 0x95: return GB(industry->this_month_production[0], 8, 8);
00311 case 0x96: return industry->this_month_production[1];
00312 case 0x97: return GB(industry->this_month_production[1], 8, 8);
00313
00314 case 0x98: return industry->this_month_transported[0];
00315 case 0x99: return GB(industry->this_month_transported[0], 8, 8);
00316 case 0x9A: return industry->this_month_transported[1];
00317 case 0x9B: return GB(industry->this_month_transported[1], 8, 8);
00318
00319 case 0x9C:
00320 case 0x9D: return industry->last_month_pct_transported[variable - 0x9C];
00321
00322 case 0x9E: return industry->last_month_production[0];
00323 case 0x9F: return GB(industry->last_month_production[0], 8, 8);
00324 case 0xA0: return industry->last_month_production[1];
00325 case 0xA1: return GB(industry->last_month_production[1], 8, 8);
00326
00327 case 0xA2: return industry->last_month_transported[0];
00328 case 0xA3: return GB(industry->last_month_transported[0], 8, 8);
00329 case 0xA4: return industry->last_month_transported[1];
00330 case 0xA5: return GB(industry->last_month_transported[1], 8, 8);
00331
00332 case 0xA6: return industry->type;
00333 case 0xA7: return industry->founder;
00334 case 0xA8: return industry->random_colour;
00335 case 0xA9: return Clamp(industry->last_prod_year - ORIGINAL_BASE_YEAR, 0, 255);
00336 case 0xAA: return industry->counter;
00337 case 0xAB: return GB(industry->counter, 8, 8);
00338 case 0xAC: return industry->was_cargo_delivered;
00339
00340 case 0xB0: return Clamp(industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00341 case 0xB3: return industry->construction_type;
00342 case 0xB4: return Clamp(industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00343 }
00344
00345 DEBUG(grf, 1, "Unhandled industry variable 0x%X", variable);
00346
00347 *available = false;
00348 return UINT_MAX;
00349 }
00350
00351 static const SpriteGroup *IndustryResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00352 {
00353
00354 return NULL;
00355 }
00356
00357 static uint32 IndustryGetRandomBits(const ResolverObject *object)
00358 {
00359 const Industry *ind = object->u.industry.ind;
00360 return ind != NULL ? ind->random: 0;
00361 }
00362
00363 static uint32 IndustryGetTriggers(const ResolverObject *object)
00364 {
00365 const Industry *ind = object->u.industry.ind;
00366 return ind != NULL ? ind->random_triggers : 0;
00367 }
00368
00369 static void IndustrySetTriggers(const ResolverObject *object, int triggers)
00370 {
00371 Industry *ind = object->u.industry.ind;
00372 assert(ind != NULL && ind->index != INVALID_INDUSTRY);
00373 ind->random_triggers = triggers;
00374 }
00375
00382 void IndustryStorePSA(ResolverObject *object, uint pos, int32 value)
00383 {
00384 Industry *ind = object->u.industry.ind;
00385 if (ind->index == INVALID_INDUSTRY) return;
00386
00387 if (object->scope != VSG_SCOPE_SELF) {
00388
00389 TownStorePSA(ind->town, object->grffile, pos, value);
00390 return;
00391 }
00392
00393 if (ind->psa == NULL) {
00394
00395 if (value == 0) return;
00396
00397
00398 const IndustrySpec *indsp = GetIndustrySpec(ind->type);
00399 uint32 grfid = (indsp->grf_prop.grffile != NULL) ? indsp->grf_prop.grffile->grfid : 0;
00400 assert(PersistentStorage::CanAllocateItem());
00401 ind->psa = new PersistentStorage(grfid);
00402 }
00403
00404 ind->psa->StoreValue(pos, value);
00405 }
00406
00407 static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus, IndustryType type)
00408 {
00409 res->GetRandomBits = IndustryGetRandomBits;
00410 res->GetTriggers = IndustryGetTriggers;
00411 res->SetTriggers = IndustrySetTriggers;
00412 res->GetVariable = IndustryGetVariable;
00413 res->ResolveReal = IndustryResolveReal;
00414 res->StorePSA = IndustryStorePSA;
00415
00416 res->u.industry.tile = tile;
00417 res->u.industry.ind = indus;
00418 res->u.industry.gfx = INVALID_INDUSTRYTILE;
00419 res->u.industry.type = type;
00420
00421 res->callback = CBID_NO_CALLBACK;
00422 res->callback_param1 = 0;
00423 res->callback_param2 = 0;
00424 res->ResetState();
00425
00426 const IndustrySpec *indspec = GetIndustrySpec(type);
00427 res->grffile = (indspec != NULL ? indspec->grf_prop.grffile : NULL);
00428 }
00429
00440 uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
00441 {
00442 ResolverObject object;
00443 const SpriteGroup *group;
00444
00445 NewIndustryResolver(&object, tile, industry, type);
00446 object.callback = callback;
00447 object.callback_param1 = param1;
00448 object.callback_param2 = param2;
00449
00450 group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
00451 if (group == NULL) return CALLBACK_FAILED;
00452
00453 return group->GetCallbackResult();
00454 }
00455
00456 uint32 IndustryLocationGetVariable(const ResolverObject *object, byte variable, uint32 parameter, bool *available)
00457 {
00458 const Industry *industry = object->u.industry.ind;
00459 TileIndex tile = object->u.industry.tile;
00460
00461 if (object->scope == VSG_SCOPE_PARENT) {
00462 return TownGetVariable(variable, parameter, available, industry->town, object->grffile);
00463 }
00464
00465 switch (variable) {
00466 case 0x80: return tile;
00467 case 0x81: return GB(tile, 8, 8);
00468
00469
00470 case 0x82: return industry->town->index;
00471 case 0x83:
00472 case 0x84:
00473 case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break;
00474
00475
00476 case 0x86: return industry->selected_layout;
00477
00478
00479 case 0x87: return GetTerrainType(tile);
00480
00481
00482 case 0x88: return GetTownRadiusGroup(industry->town, tile);
00483
00484
00485 case 0x89: return min(DistanceManhattan(industry->town->xy, tile), 255);
00486
00487
00488 case 0x8A: return Clamp(GetTileZ(tile) * (object->grffile->grf_version >= 8 ? 1 : TILE_HEIGHT), 0, 0xFF);
00489
00490
00491 case 0x8B: return GetClosestWaterDistance(tile, (GetIndustrySpec(industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
00492
00493
00494 case 0x8D: return min(DistanceSquare(industry->town->xy, tile), 65535);
00495
00496
00497 case 0x8F: return _industry_creation_random_bits;
00498 }
00499
00500
00501 return IndustryGetVariable(object, variable, parameter, available);
00502 }
00503
00515 CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
00516 {
00517 const IndustrySpec *indspec = GetIndustrySpec(type);
00518
00519 ResolverObject object;
00520 const SpriteGroup *group;
00521
00522 Industry ind;
00523 ind.index = INVALID_INDUSTRY;
00524 ind.location.tile = tile;
00525 ind.location.w = 0;
00526 ind.type = type;
00527 ind.selected_layout = layout;
00528 ind.town = ClosestTownFromTile(tile, UINT_MAX);
00529 ind.random = initial_random_bits;
00530 ind.founder = founder;
00531 ind.psa = NULL;
00532
00533 NewIndustryResolver(&object, tile, &ind, type);
00534 object.GetVariable = IndustryLocationGetVariable;
00535 object.callback = CBID_INDUSTRY_LOCATION;
00536 object.callback_param2 = creation_type;
00537 _industry_creation_random_bits = seed;
00538
00539 group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
00540
00541
00542
00543 if (group == NULL) return CommandCost();
00544 uint16 result = group->GetCallbackResult();
00545 if (result == CALLBACK_FAILED) return CommandCost();
00546
00547 return GetErrorMessageFromLocationCallbackResult(result, indspec->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
00548 }
00549
00556 uint32 GetIndustryProbabilityCallback(IndustryType type, IndustryAvailabilityCallType creation_type, uint32 default_prob)
00557 {
00558 const IndustrySpec *indspec = GetIndustrySpec(type);
00559
00560 if (HasBit(indspec->callback_mask, CBM_IND_PROBABILITY)) {
00561 uint16 res = GetIndustryCallback(CBID_INDUSTRY_PROBABILITY, 0, creation_type, NULL, type, INVALID_TILE);
00562 if (res != CALLBACK_FAILED) {
00563 if (indspec->grf_prop.grffile->grf_version < 8) {
00564
00565 if (res != 0) default_prob = 0;
00566 } else {
00567
00568 if (res < 0x100) {
00569 default_prob = res;
00570 } else if (res > 0x100) {
00571 ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_PROBABILITY, res);
00572 }
00573 }
00574 }
00575 }
00576 return default_prob;
00577 }
00578
00579 static int32 DerefIndProd(int field, bool use_register)
00580 {
00581 return use_register ? (int32)GetRegister(field) : field;
00582 }
00583
00589 void IndustryProductionCallback(Industry *ind, int reason)
00590 {
00591 const IndustrySpec *spec = GetIndustrySpec(ind->type);
00592 ResolverObject object;
00593 NewIndustryResolver(&object, ind->location.tile, ind, ind->type);
00594 if ((spec->behaviour & INDUSTRYBEH_PRODCALLBACK_RANDOM) != 0) object.callback_param1 = Random();
00595 int multiplier = 1;
00596 if ((spec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) multiplier = ind->prod_level;
00597 object.callback_param2 = reason;
00598
00599 for (uint loop = 0;; loop++) {
00600
00601
00602 if (loop >= 0x10000) {
00603
00604 SetDParamStr(0, spec->grf_prop.grffile->filename);
00605 SetDParam(1, spec->name);
00606 ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK, WL_WARNING);
00607
00608
00609 break;
00610 }
00611
00612 SB(object.callback_param2, 8, 16, loop);
00613 const SpriteGroup *tgroup = SpriteGroup::Resolve(spec->grf_prop.spritegroup[0], &object);
00614 if (tgroup == NULL || tgroup->type != SGT_INDUSTRY_PRODUCTION) break;
00615 const IndustryProductionSpriteGroup *group = (const IndustryProductionSpriteGroup *)tgroup;
00616
00617 bool deref = (group->version == 1);
00618
00619 for (uint i = 0; i < 3; i++) {
00620 ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->subtract_input[i], deref) * multiplier, 0, 0xFFFF);
00621 }
00622 for (uint i = 0; i < 2; i++) {
00623 ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + max(DerefIndProd(group->add_output[i], deref), 0) * multiplier, 0, 0xFFFF);
00624 }
00625
00626 int32 again = DerefIndProd(group->again, deref);
00627 if (again == 0) break;
00628
00629 SB(object.callback_param2, 24, 8, again);
00630 }
00631
00632 SetWindowDirty(WC_INDUSTRY_VIEW, ind->index);
00633 }
00634
00640 void GetIndustryResolver(ResolverObject *ro, uint index)
00641 {
00642 Industry *i = Industry::Get(index);
00643 NewIndustryResolver(ro, i->location.tile, i, i->type);
00644 }
00645
00653 bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type)
00654 {
00655 assert(cargo_type == ind->accepts_cargo[0] || cargo_type == ind->accepts_cargo[1] || cargo_type == ind->accepts_cargo[2]);
00656
00657 const IndustrySpec *indspec = GetIndustrySpec(ind->type);
00658 if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) {
00659 uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO,
00660 0, GetReverseCargoTranslation(cargo_type, indspec->grf_prop.grffile),
00661 ind, ind->type, ind->location.tile);
00662 if (res != CALLBACK_FAILED) return !ConvertBooleanCallback(indspec->grf_prop.grffile, CBID_INDUSTRY_REFUSE_CARGO, res);
00663 }
00664 return false;
00665 }