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