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