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_commons.h"
00018 #include "newgrf_text.h"
00019 #include "newgrf_town.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 != 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
00111 static uint32 GetCountAndDistanceOfClosestInstance(byte param_setID, byte layout_filter, const Industry *current)
00112 {
00113 uint32 GrfID = GetRegister(0x100);
00114 IndustryType ind_index;
00115 uint32 closest_dist = UINT32_MAX;
00116 byte count = 0;
00117
00118
00119 switch (GrfID) {
00120 case 0:
00121 ind_index = param_setID;
00122 break;
00123
00124 case 0xFFFFFFFF:
00125 GrfID = GetIndustrySpec(current->type)->grf_prop.grffile->grfid;
00126
00127
00128 default:
00129 SetBit(param_setID, 7);
00130 ind_index = MapNewGRFIndustryType(param_setID, GrfID);
00131 break;
00132 }
00133
00134
00135 if (ind_index >= NUM_INDUSTRYTYPES) return 0 | 0xFFFF;
00136
00137 if (layout_filter == 0) {
00138
00139
00140 closest_dist = GetClosestIndustry(current->location.tile, ind_index, current);
00141 count = GetIndustryTypeCount(ind_index);
00142 } else {
00143
00144
00145 const Industry *i;
00146 FOR_ALL_INDUSTRIES(i) {
00147 if (i->type == ind_index && i != current && i->selected_layout == layout_filter) {
00148 closest_dist = min(closest_dist, DistanceManhattan(current->location.tile, i->location.tile));
00149 count++;
00150 }
00151 }
00152 }
00153
00154 return count << 16 | GB(closest_dist, 0, 16);
00155 }
00156
00163 uint32 IndustryGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00164 {
00165 const Industry *industry = object->u.industry.ind;
00166 TileIndex tile = object->u.industry.tile;
00167 IndustryType type = object->u.industry.type;
00168 const IndustrySpec *indspec = GetIndustrySpec(type);
00169
00170
00171 if (object->u.industry.gfx == INVALID_INDUSTRYTILE && object->scope == VSG_SCOPE_PARENT) {
00172
00173 const Town *t;
00174
00175 if (industry != NULL) {
00176 t = industry->town;
00177 } else if (tile != INVALID_TILE) {
00178 t = ClosestTownFromTile(tile, UINT_MAX);
00179 } else {
00180 *available = false;
00181 return UINT_MAX;
00182 }
00183
00184 return TownGetVariable(variable, parameter, available, t);
00185 }
00186
00187 if (industry == NULL) {
00188 DEBUG(grf, 1, "Unhandled property 0x%X (no available industry) in callback 0x%x", variable, object->callback);
00189
00190 *available = false;
00191 return UINT_MAX;
00192 }
00193
00194 switch (variable) {
00195 case 0x40:
00196 case 0x41:
00197 case 0x42: {
00198 uint16 callback = indspec->callback_mask;
00199 if (HasBit(callback, CBM_IND_PRODUCTION_CARGO_ARRIVAL) || HasBit(callback, CBM_IND_PRODUCTION_256_TICKS)) {
00200 if ((indspec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) {
00201 if (industry->prod_level == 0) return 0;
00202 return min(industry->incoming_cargo_waiting[variable - 0x40] / industry->prod_level, (uint16)0xFFFF);
00203 } else {
00204 return min(industry->incoming_cargo_waiting[variable - 0x40], (uint16)0xFFFF);
00205 }
00206 } else {
00207 return 0;
00208 }
00209 }
00210
00211
00212 case 0x43: return GetClosestWaterDistance(tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
00213
00214
00215 case 0x44: return industry->selected_layout;
00216
00217
00218 case 0x45: {
00219 byte colours = 0;
00220 bool is_ai = false;
00221
00222 const Company *c = Company::GetIfValid(industry->founder);
00223 if (c != NULL) {
00224 const Livery *l = &c->livery[LS_DEFAULT];
00225
00226 is_ai = c->is_ai;
00227 colours = l->colour1 + l->colour2 * 16;
00228 }
00229
00230 return industry->founder | (is_ai ? 0x10000 : 0) | (colours << 24);
00231 }
00232
00233 case 0x46: return industry->construction_date;
00234
00235
00236 case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, industry->location.tile, false), industry, object->grffile->grfid);
00237
00238
00239 case 0x61:
00240 tile = GetNearbyTile(parameter, tile, false);
00241 return (IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile) == industry) ? GetIndustryRandomBits(tile) : 0;
00242
00243
00244 case 0x62: return GetNearbyIndustryTileInformation(parameter, tile, INVALID_INDUSTRY, false);
00245
00246
00247 case 0x63:
00248 tile = GetNearbyTile(parameter, tile, false);
00249 if (IsTileType(tile, MP_INDUSTRY) && Industry::GetByTile(tile) == industry) {
00250 return GetIndustryAnimationState(tile);
00251 }
00252 return 0xFFFFFFFF;
00253
00254
00255 case 0x64: return GetClosestIndustry(tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), industry);
00256
00257 case 0x65: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceManhattan(tile, industry->town->xy), 0xFFFF);
00258
00259 case 0x66: return GetTownRadiusGroup(industry->town, tile) << 16 | min(DistanceSquare(tile, industry->town->xy), 0xFFFF);
00260
00261
00262
00263 case 0x67:
00264 case 0x68: return GetCountAndDistanceOfClosestInstance(parameter, variable == 0x68 ? GB(GetRegister(0x101), 0, 8) : 0, industry);
00265
00266
00267 case 0x7C: return industry->psa.Get(parameter);
00268
00269
00270 case 0x80: return industry->location.tile;
00271 case 0x81: return GB(industry->location.tile, 8, 8);
00272
00273 case 0x82: return industry->town->index;
00274 case 0x83:
00275 case 0x84:
00276 case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break;
00277 case 0x86: return industry->location.w;
00278 case 0x87: return industry->location.h;
00279
00280 case 0x88:
00281 case 0x89: return industry->produced_cargo[variable - 0x88];
00282 case 0x8A: return industry->produced_cargo_waiting[0];
00283 case 0x8B: return GB(industry->produced_cargo_waiting[0], 8, 8);
00284 case 0x8C: return industry->produced_cargo_waiting[1];
00285 case 0x8D: return GB(industry->produced_cargo_waiting[1], 8, 8);
00286 case 0x8E:
00287 case 0x8F: return industry->production_rate[variable - 0x8E];
00288 case 0x90:
00289 case 0x91:
00290 case 0x92: return industry->accepts_cargo[variable - 0x90];
00291 case 0x93: return industry->prod_level;
00292
00293 case 0x94: return industry->this_month_production[0];
00294 case 0x95: return GB(industry->this_month_production[0], 8, 8);
00295 case 0x96: return industry->this_month_production[1];
00296 case 0x97: return GB(industry->this_month_production[1], 8, 8);
00297
00298 case 0x98: return industry->this_month_transported[0];
00299 case 0x99: return GB(industry->this_month_transported[0], 8, 8);
00300 case 0x9A: return industry->this_month_transported[1];
00301 case 0x9B: return GB(industry->this_month_transported[1], 8, 8);
00302
00303 case 0x9C:
00304 case 0x9D: return industry->last_month_pct_transported[variable - 0x9C];
00305
00306 case 0x9E: return industry->last_month_production[0];
00307 case 0x9F: return GB(industry->last_month_production[0], 8, 8);
00308 case 0xA0: return industry->last_month_production[1];
00309 case 0xA1: return GB(industry->last_month_production[1], 8, 8);
00310
00311 case 0xA2: return industry->last_month_transported[0];
00312 case 0xA3: return GB(industry->last_month_transported[0], 8, 8);
00313 case 0xA4: return industry->last_month_transported[1];
00314 case 0xA5: return GB(industry->last_month_transported[1], 8, 8);
00315
00316 case 0xA6: return industry->type;
00317 case 0xA7: return industry->founder;
00318 case 0xA8: return industry->random_colour;
00319 case 0xA9: return Clamp(industry->last_prod_year - ORIGINAL_BASE_YEAR, 0, 255);
00320 case 0xAA: return industry->counter;
00321 case 0xAB: return GB(industry->counter, 8, 8);
00322 case 0xAC: return industry->was_cargo_delivered;
00323
00324 case 0xB0: return Clamp(industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00325 case 0xB3: return industry->construction_type;
00326 case 0xB4: return Clamp(industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00327 }
00328
00329 DEBUG(grf, 1, "Unhandled industry property 0x%X", variable);
00330
00331 *available = false;
00332 return UINT_MAX;
00333 }
00334
00335 static const SpriteGroup *IndustryResolveReal(const ResolverObject *object, const RealSpriteGroup *group)
00336 {
00337
00338 return NULL;
00339 }
00340
00341 static uint32 IndustryGetRandomBits(const ResolverObject *object)
00342 {
00343 const Industry *ind = object->u.industry.ind;
00344 return ind != NULL ? ind->random: 0;
00345 }
00346
00347 static uint32 IndustryGetTriggers(const ResolverObject *object)
00348 {
00349 const Industry *ind = object->u.industry.ind;
00350 return ind != NULL ? ind->random_triggers : 0;
00351 }
00352
00353 static void IndustrySetTriggers(const ResolverObject *object, int triggers)
00354 {
00355 Industry *ind = object->u.industry.ind;
00356 assert(ind != NULL && ind->index != INVALID_INDUSTRY);
00357 ind->random_triggers = triggers;
00358 }
00359
00360 static void NewIndustryResolver(ResolverObject *res, TileIndex tile, Industry *indus, IndustryType type)
00361 {
00362 res->GetRandomBits = IndustryGetRandomBits;
00363 res->GetTriggers = IndustryGetTriggers;
00364 res->SetTriggers = IndustrySetTriggers;
00365 res->GetVariable = IndustryGetVariable;
00366 res->ResolveReal = IndustryResolveReal;
00367
00368 res->psa = &indus->psa;
00369 res->u.industry.tile = tile;
00370 res->u.industry.ind = indus;
00371 res->u.industry.gfx = INVALID_INDUSTRYTILE;
00372 res->u.industry.type = type;
00373
00374 res->callback = CBID_NO_CALLBACK;
00375 res->callback_param1 = 0;
00376 res->callback_param2 = 0;
00377 res->last_value = 0;
00378 res->trigger = 0;
00379 res->reseed = 0;
00380 res->count = 0;
00381
00382 const IndustrySpec *indspec = GetIndustrySpec(type);
00383 res->grffile = (indspec != NULL ? indspec->grf_prop.grffile : NULL);
00384 }
00385
00386 uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
00387 {
00388 ResolverObject object;
00389 const SpriteGroup *group;
00390
00391 NewIndustryResolver(&object, tile, industry, type);
00392 object.callback = callback;
00393 object.callback_param1 = param1;
00394 object.callback_param2 = param2;
00395
00396 group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
00397 if (group == NULL) return CALLBACK_FAILED;
00398
00399 return group->GetCallbackResult();
00400 }
00401
00402 uint32 IndustryLocationGetVariable(const ResolverObject *object, byte variable, byte parameter, bool *available)
00403 {
00404 const Industry *industry = object->u.industry.ind;
00405 TileIndex tile = object->u.industry.tile;
00406
00407 if (object->scope == VSG_SCOPE_PARENT) {
00408 return TownGetVariable(variable, parameter, available, industry->town);
00409 }
00410
00411 switch (variable) {
00412 case 0x80: return tile;
00413 case 0x81: return GB(tile, 8, 8);
00414
00415
00416 case 0x82: return industry->town->index;
00417 case 0x83:
00418 case 0x84:
00419 case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break;
00420
00421
00422 case 0x86: return industry->selected_layout;
00423
00424
00425 case 0x87: return GetTerrainType(tile);
00426
00427
00428 case 0x88: return GetTownRadiusGroup(industry->town, tile);
00429
00430
00431 case 0x89: return min(DistanceManhattan(industry->town->xy, tile), 255);
00432
00433
00434 case 0x8A: return GetTileZ(tile);
00435
00436
00437 case 0x8B: return GetClosestWaterDistance(tile, (GetIndustrySpec(industry->type)->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
00438
00439
00440 case 0x8D: return min(DistanceSquare(industry->town->xy, tile), 65535);
00441
00442
00443 case 0x8F: return _industry_creation_random_bits;
00444 }
00445
00446
00447 return IndustryGetVariable(object, variable, parameter, available);
00448 }
00449
00450 bool CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint itspec_index, uint32 seed)
00451 {
00452 const IndustrySpec *indspec = GetIndustrySpec(type);
00453
00454 ResolverObject object;
00455 const SpriteGroup *group;
00456
00457 Industry ind;
00458 ind.index = INVALID_INDUSTRY;
00459 ind.location.tile = tile;
00460 ind.location.w = 0;
00461 ind.type = type;
00462 ind.selected_layout = itspec_index;
00463 ind.town = ClosestTownFromTile(tile, UINT_MAX);
00464
00465 NewIndustryResolver(&object, tile, &ind, type);
00466 object.GetVariable = IndustryLocationGetVariable;
00467 object.callback = CBID_INDUSTRY_LOCATION;
00468 _industry_creation_random_bits = seed;
00469
00470 group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup, &object);
00471
00472
00473
00474 if (group == NULL) return true;
00475 uint16 result = group->GetCallbackResult();
00476 if (result == 0x400 || result == CALLBACK_FAILED) return true;
00477
00478
00479 SwitchToErrorRefStack();
00480 PrepareTextRefStackUsage(4);
00481 SwitchToNormalRefStack();
00482
00483 switch (result) {
00484 case 0x401: _error_message = STR_ERROR_SITE_UNSUITABLE; break;
00485 case 0x402: _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST; break;
00486 case 0x403: _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT; break;
00487 default: _error_message = GetGRFStringID(indspec->grf_prop.grffile->grfid, 0xD000 + result); break;
00488 }
00489
00490 return false;
00491 }
00492
00493 bool CheckIfCallBackAllowsAvailability(IndustryType type, IndustryAvailabilityCallType creation_type)
00494 {
00495 const IndustrySpec *indspec = GetIndustrySpec(type);
00496
00497 if (HasBit(indspec->callback_mask, CBM_IND_AVAILABLE)) {
00498 uint16 res = GetIndustryCallback(CBID_INDUSTRY_AVAILABLE, 0, creation_type, NULL, type, INVALID_TILE);
00499 if (res != CALLBACK_FAILED) {
00500 return (res == 0);
00501 }
00502 }
00503 return true;
00504 }
00505
00506 static int32 DerefIndProd(int field, bool use_register)
00507 {
00508 return use_register ? (int32)GetRegister(field) : field;
00509 }
00510
00516 void IndustryProductionCallback(Industry *ind, int reason)
00517 {
00518 const IndustrySpec *spec = GetIndustrySpec(ind->type);
00519 ResolverObject object;
00520 NewIndustryResolver(&object, ind->location.tile, ind, ind->type);
00521 if ((spec->behaviour & INDUSTRYBEH_PRODCALLBACK_RANDOM) != 0) object.callback_param1 = Random();
00522 int multiplier = 1;
00523 if ((spec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) multiplier = ind->prod_level;
00524 object.callback_param2 = reason;
00525
00526 for (uint loop = 0;; loop++) {
00527
00528
00529 if (loop >= 0x10000) {
00530
00531 SetDParamStr(0, spec->grf_prop.grffile->filename);
00532 SetDParam(1, spec->name);
00533 ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK, 0, 0);
00534
00535
00536 break;
00537 }
00538
00539 SB(object.callback_param2, 8, 16, loop);
00540 const SpriteGroup *tgroup = SpriteGroup::Resolve(spec->grf_prop.spritegroup, &object);
00541 if (tgroup == NULL || tgroup->type != SGT_INDUSTRY_PRODUCTION) break;
00542 const IndustryProductionSpriteGroup *group = (const IndustryProductionSpriteGroup *)tgroup;
00543
00544 bool deref = (group->version == 1);
00545
00546 for (uint i = 0; i < 3; i++) {
00547 ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->subtract_input[i], deref) * multiplier, 0, 0xFFFF);
00548 }
00549 for (uint i = 0; i < 2; i++) {
00550 ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + max(DerefIndProd(group->add_output[i], deref), 0) * multiplier, 0, 0xFFFF);
00551 }
00552
00553 int32 again = DerefIndProd(group->again, deref);
00554 if (again == 0) break;
00555
00556 SB(object.callback_param2, 24, 8, again);
00557 }
00558
00559 SetWindowDirty(WC_INDUSTRY_VIEW, ind->index);
00560 }