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 (!IsTileType(tile, MP_INDUSTRY) || GetIndustryIndex(tile) != i->index) {
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: return GetClosestWaterDistance(this->tile, (indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) == 0);
00229
00230
00231 case 0x44: return this->industry->selected_layout;
00232
00233
00234 case 0x45: {
00235 byte colours = 0;
00236 bool is_ai = false;
00237
00238 const Company *c = Company::GetIfValid(this->industry->founder);
00239 if (c != NULL) {
00240 const Livery *l = &c->livery[LS_DEFAULT];
00241
00242 is_ai = c->is_ai;
00243 colours = l->colour1 + l->colour2 * 16;
00244 }
00245
00246 return this->industry->founder | (is_ai ? 0x10000 : 0) | (colours << 24);
00247 }
00248
00249 case 0x46: return this->industry->construction_date;
00250
00251
00252 case 0x60: return GetIndustryIDAtOffset(GetNearbyTile(parameter, this->industry->location.tile, false), this->industry, this->ro->grffile->grfid);
00253
00254
00255 case 0x61: {
00256 TileIndex tile = GetNearbyTile(parameter, this->tile, false);
00257 return this->industry->TileBelongsToIndustry(tile) ? GetIndustryRandomBits(tile) : 0;
00258 }
00259
00260
00261 case 0x62: return GetNearbyIndustryTileInformation(parameter, this->tile, INVALID_INDUSTRY, false, this->ro->grffile->grf_version >= 8);
00262
00263
00264 case 0x63: {
00265 TileIndex tile = GetNearbyTile(parameter, this->tile, false);
00266 if (this->industry->TileBelongsToIndustry(tile)) {
00267 return GetAnimationFrame(tile);
00268 }
00269 return 0xFFFFFFFF;
00270 }
00271
00272
00273 case 0x64: return GetClosestIndustry(this->tile, MapNewGRFIndustryType(parameter, indspec->grf_prop.grffile->grfid), this->industry);
00274
00275 case 0x65: return GetTownRadiusGroup(this->industry->town, this->tile) << 16 | min(DistanceManhattan(this->tile, this->industry->town->xy), 0xFFFF);
00276
00277 case 0x66: return GetTownRadiusGroup(this->industry->town, this->tile) << 16 | min(DistanceSquare(this->tile, this->industry->town->xy), 0xFFFF);
00278
00279
00280
00281 case 0x67:
00282 case 0x68: {
00283 byte layout_filter = 0;
00284 bool town_filter = false;
00285 if (variable == 0x68) {
00286 uint32 reg = GetRegister(0x101);
00287 layout_filter = GB(reg, 0, 8);
00288 town_filter = HasBit(reg, 8);
00289 }
00290 return GetCountAndDistanceOfClosestInstance(parameter, layout_filter, town_filter, this->industry);
00291 }
00292
00293
00294 case 0x7C: return (this->industry->psa != NULL) ? this->industry->psa->GetValue(parameter) : 0;
00295
00296
00297 case 0x80: return this->industry->location.tile;
00298 case 0x81: return GB(this->industry->location.tile, 8, 8);
00299
00300 case 0x82: return this->industry->town->index;
00301 case 0x83:
00302 case 0x84:
00303 case 0x85: DEBUG(grf, 0, "NewGRFs shouldn't be doing pointer magic"); break;
00304 case 0x86: return this->industry->location.w;
00305 case 0x87: return this->industry->location.h;
00306
00307 case 0x88:
00308 case 0x89: return this->industry->produced_cargo[variable - 0x88];
00309 case 0x8A: return this->industry->produced_cargo_waiting[0];
00310 case 0x8B: return GB(this->industry->produced_cargo_waiting[0], 8, 8);
00311 case 0x8C: return this->industry->produced_cargo_waiting[1];
00312 case 0x8D: return GB(this->industry->produced_cargo_waiting[1], 8, 8);
00313 case 0x8E:
00314 case 0x8F: return this->industry->production_rate[variable - 0x8E];
00315 case 0x90:
00316 case 0x91:
00317 case 0x92: return this->industry->accepts_cargo[variable - 0x90];
00318 case 0x93: return this->industry->prod_level;
00319
00320 case 0x94: return this->industry->this_month_production[0];
00321 case 0x95: return GB(this->industry->this_month_production[0], 8, 8);
00322 case 0x96: return this->industry->this_month_production[1];
00323 case 0x97: return GB(this->industry->this_month_production[1], 8, 8);
00324
00325 case 0x98: return this->industry->this_month_transported[0];
00326 case 0x99: return GB(this->industry->this_month_transported[0], 8, 8);
00327 case 0x9A: return this->industry->this_month_transported[1];
00328 case 0x9B: return GB(this->industry->this_month_transported[1], 8, 8);
00329
00330 case 0x9C:
00331 case 0x9D: return this->industry->last_month_pct_transported[variable - 0x9C];
00332
00333 case 0x9E: return this->industry->last_month_production[0];
00334 case 0x9F: return GB(this->industry->last_month_production[0], 8, 8);
00335 case 0xA0: return this->industry->last_month_production[1];
00336 case 0xA1: return GB(this->industry->last_month_production[1], 8, 8);
00337
00338 case 0xA2: return this->industry->last_month_transported[0];
00339 case 0xA3: return GB(this->industry->last_month_transported[0], 8, 8);
00340 case 0xA4: return this->industry->last_month_transported[1];
00341 case 0xA5: return GB(this->industry->last_month_transported[1], 8, 8);
00342
00343 case 0xA6: return this->industry->type;
00344 case 0xA7: return this->industry->founder;
00345 case 0xA8: return this->industry->random_colour;
00346 case 0xA9: return Clamp(this->industry->last_prod_year - ORIGINAL_BASE_YEAR, 0, 255);
00347 case 0xAA: return this->industry->counter;
00348 case 0xAB: return GB(this->industry->counter, 8, 8);
00349 case 0xAC: return this->industry->was_cargo_delivered;
00350
00351 case 0xB0: return Clamp(this->industry->construction_date - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00352 case 0xB3: return this->industry->construction_type;
00353 case 0xB4: return Clamp(this->industry->last_cargo_accepted_at - DAYS_TILL_ORIGINAL_BASE_YEAR, 0, 65535);
00354 }
00355
00356 DEBUG(grf, 1, "Unhandled industry variable 0x%X", variable);
00357
00358 *available = false;
00359 return UINT_MAX;
00360 }
00361
00362 uint32 IndustriesScopeResolver::GetRandomBits() const
00363 {
00364 return this->industry != NULL ? this->industry->random : 0;
00365 }
00366
00367 uint32 IndustriesScopeResolver::GetTriggers() const
00368 {
00369 return this->industry != NULL ? this->industry->random_triggers : 0;
00370 }
00371
00372 void IndustriesScopeResolver::SetTriggers(int triggers) const
00373 {
00374 assert(this->industry != NULL && this->industry->index != INVALID_INDUSTRY);
00375 this->industry->random_triggers = triggers;
00376 }
00377
00378 void IndustriesScopeResolver::StorePSA(uint pos, int32 value)
00379 {
00380 if (this->industry->index == INVALID_INDUSTRY) return;
00381
00382 if (this->industry->psa == NULL) {
00383
00384 if (value == 0) return;
00385
00386
00387 const IndustrySpec *indsp = GetIndustrySpec(this->industry->type);
00388 uint32 grfid = (indsp->grf_prop.grffile != NULL) ? indsp->grf_prop.grffile->grfid : 0;
00389 assert(PersistentStorage::CanAllocateItem());
00390 this->industry->psa = new PersistentStorage(grfid);
00391 }
00392
00393 this->industry->psa->StoreValue(pos, value);
00394 }
00395
00401 static const GRFFile *GetGrffile(IndustryType type)
00402 {
00403 const IndustrySpec *indspec = GetIndustrySpec(type);
00404 return (indspec != NULL) ? indspec->grf_prop.grffile : NULL;
00405 }
00406
00417 IndustriesResolverObject::IndustriesResolverObject(TileIndex tile, Industry *indus, IndustryType type, uint32 random_bits,
00418 CallbackID callback, uint32 callback_param1, uint32 callback_param2)
00419 : ResolverObject(GetGrffile(type), callback, callback_param1, callback_param2),
00420 industries_scope(this, tile, indus, type, random_bits),
00421 town_scope(NULL)
00422 {
00423 }
00424
00425 IndustriesResolverObject::~IndustriesResolverObject()
00426 {
00427 delete this->town_scope;
00428 }
00429
00434 TownScopeResolver *IndustriesResolverObject::GetTown()
00435 {
00436 if (this->town_scope == NULL) {
00437 Town *t = NULL;
00438 if (this->industries_scope.industry != NULL) {
00439 t = this->industries_scope.industry->town;
00440 } else if (this->industries_scope.tile != INVALID_TILE) {
00441 t = ClosestTownFromTile(this->industries_scope.tile, UINT_MAX);
00442 }
00443 if (t == NULL) return NULL;
00444 this->town_scope = new TownScopeResolver(this, t, this->industries_scope.industry->index == INVALID_INDUSTRY);
00445 }
00446 return this->town_scope;
00447 }
00448
00457 IndustriesScopeResolver::IndustriesScopeResolver(ResolverObject *ro, TileIndex tile, Industry *industry, IndustryType type, uint32 random_bits)
00458 : ScopeResolver(ro)
00459 {
00460 this->tile = tile;
00461 this->industry = industry;
00462 this->type = type;
00463 this->random_bits = random_bits;
00464 }
00465
00476 uint16 GetIndustryCallback(CallbackID callback, uint32 param1, uint32 param2, Industry *industry, IndustryType type, TileIndex tile)
00477 {
00478 IndustriesResolverObject object(tile, industry, type, 0, callback, param1, param2);
00479 const SpriteGroup *group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
00480 if (group == NULL) return CALLBACK_FAILED;
00481
00482 return group->GetCallbackResult();
00483 }
00484
00496 CommandCost CheckIfCallBackAllowsCreation(TileIndex tile, IndustryType type, uint layout, uint32 seed, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type)
00497 {
00498 const IndustrySpec *indspec = GetIndustrySpec(type);
00499
00500 Industry ind;
00501 ind.index = INVALID_INDUSTRY;
00502 ind.location.tile = tile;
00503 ind.location.w = 0;
00504 ind.type = type;
00505 ind.selected_layout = layout;
00506 ind.town = ClosestTownFromTile(tile, UINT_MAX);
00507 ind.random = initial_random_bits;
00508 ind.founder = founder;
00509 ind.psa = NULL;
00510
00511 IndustriesResolverObject object(tile, &ind, type, seed, CBID_INDUSTRY_LOCATION, 0, creation_type);
00512 const SpriteGroup *group = SpriteGroup::Resolve(GetIndustrySpec(type)->grf_prop.spritegroup[0], &object);
00513
00514
00515
00516 if (group == NULL) return CommandCost();
00517 uint16 result = group->GetCallbackResult();
00518 if (result == CALLBACK_FAILED) return CommandCost();
00519
00520 return GetErrorMessageFromLocationCallbackResult(result, indspec->grf_prop.grffile->grfid, STR_ERROR_SITE_UNSUITABLE);
00521 }
00522
00529 uint32 GetIndustryProbabilityCallback(IndustryType type, IndustryAvailabilityCallType creation_type, uint32 default_prob)
00530 {
00531 const IndustrySpec *indspec = GetIndustrySpec(type);
00532
00533 if (HasBit(indspec->callback_mask, CBM_IND_PROBABILITY)) {
00534 uint16 res = GetIndustryCallback(CBID_INDUSTRY_PROBABILITY, 0, creation_type, NULL, type, INVALID_TILE);
00535 if (res != CALLBACK_FAILED) {
00536 if (indspec->grf_prop.grffile->grf_version < 8) {
00537
00538 if (res != 0) default_prob = 0;
00539 } else {
00540
00541 if (res < 0x100) {
00542 default_prob = res;
00543 } else if (res > 0x100) {
00544 ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_PROBABILITY, res);
00545 }
00546 }
00547 }
00548 }
00549 return default_prob;
00550 }
00551
00552 static int32 DerefIndProd(int field, bool use_register)
00553 {
00554 return use_register ? (int32)GetRegister(field) : field;
00555 }
00556
00562 void IndustryProductionCallback(Industry *ind, int reason)
00563 {
00564 const IndustrySpec *spec = GetIndustrySpec(ind->type);
00565 IndustriesResolverObject object(ind->location.tile, ind, ind->type);
00566 if ((spec->behaviour & INDUSTRYBEH_PRODCALLBACK_RANDOM) != 0) object.callback_param1 = Random();
00567 int multiplier = 1;
00568 if ((spec->behaviour & INDUSTRYBEH_PROD_MULTI_HNDLING) != 0) multiplier = ind->prod_level;
00569 object.callback_param2 = reason;
00570
00571 for (uint loop = 0;; loop++) {
00572
00573
00574 if (loop >= 0x10000) {
00575
00576 SetDParamStr(0, spec->grf_prop.grffile->filename);
00577 SetDParam(1, spec->name);
00578 ShowErrorMessage(STR_NEWGRF_BUGGY, STR_NEWGRF_BUGGY_ENDLESS_PRODUCTION_CALLBACK, WL_WARNING);
00579
00580
00581 break;
00582 }
00583
00584 SB(object.callback_param2, 8, 16, loop);
00585 const SpriteGroup *tgroup = SpriteGroup::Resolve(spec->grf_prop.spritegroup[0], &object);
00586 if (tgroup == NULL || tgroup->type != SGT_INDUSTRY_PRODUCTION) break;
00587 const IndustryProductionSpriteGroup *group = (const IndustryProductionSpriteGroup *)tgroup;
00588
00589 bool deref = (group->version == 1);
00590
00591 for (uint i = 0; i < 3; i++) {
00592 ind->incoming_cargo_waiting[i] = Clamp(ind->incoming_cargo_waiting[i] - DerefIndProd(group->subtract_input[i], deref) * multiplier, 0, 0xFFFF);
00593 }
00594 for (uint i = 0; i < 2; i++) {
00595 ind->produced_cargo_waiting[i] = Clamp(ind->produced_cargo_waiting[i] + max(DerefIndProd(group->add_output[i], deref), 0) * multiplier, 0, 0xFFFF);
00596 }
00597
00598 int32 again = DerefIndProd(group->again, deref);
00599 if (again == 0) break;
00600
00601 SB(object.callback_param2, 24, 8, again);
00602 }
00603
00604 SetWindowDirty(WC_INDUSTRY_VIEW, ind->index);
00605 }
00606
00614 bool IndustryTemporarilyRefusesCargo(Industry *ind, CargoID cargo_type)
00615 {
00616 assert(cargo_type == ind->accepts_cargo[0] || cargo_type == ind->accepts_cargo[1] || cargo_type == ind->accepts_cargo[2]);
00617
00618 const IndustrySpec *indspec = GetIndustrySpec(ind->type);
00619 if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) {
00620 uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO,
00621 0, indspec->grf_prop.grffile->cargo_map[cargo_type],
00622 ind, ind->type, ind->location.tile);
00623 if (res != CALLBACK_FAILED) return !ConvertBooleanCallback(indspec->grf_prop.grffile, CBID_INDUSTRY_REFUSE_CARGO, res);
00624 }
00625 return false;
00626 }