00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "clear_map.h"
00014 #include "industry.h"
00015 #include "station_base.h"
00016 #include "train.h"
00017 #include "viewport_func.h"
00018 #include "command_func.h"
00019 #include "town.h"
00020 #include "news_func.h"
00021 #include "cheat_type.h"
00022 #include "genworld.h"
00023 #include "tree_map.h"
00024 #include "newgrf_cargo.h"
00025 #include "newgrf_debug.h"
00026 #include "newgrf_industrytiles.h"
00027 #include "autoslope.h"
00028 #include "water.h"
00029 #include "strings_func.h"
00030 #include "window_func.h"
00031 #include "date_func.h"
00032 #include "vehicle_func.h"
00033 #include "sound_func.h"
00034 #include "animated_tile_func.h"
00035 #include "effectvehicle_func.h"
00036 #include "effectvehicle_base.h"
00037 #include "ai/ai.hpp"
00038 #include "core/pool_func.hpp"
00039 #include "subsidy_func.h"
00040 #include "core/backup_type.hpp"
00041 #include "object_base.h"
00042
00043 #include "table/strings.h"
00044 #include "table/industry_land.h"
00045 #include "table/build_industry.h"
00046
00047 IndustryPool _industry_pool("Industry");
00048 INSTANTIATE_POOL_METHODS(Industry)
00049
00050 void ShowIndustryViewWindow(int industry);
00051 void BuildOilRig(TileIndex tile);
00052
00053 static byte _industry_sound_ctr;
00054 static TileIndex _industry_sound_tile;
00055
00056 uint16 Industry::counts[NUM_INDUSTRYTYPES];
00057
00058 IndustrySpec _industry_specs[NUM_INDUSTRYTYPES];
00059 IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES];
00060 IndustryBuildData _industry_builder;
00061
00068 void ResetIndustries()
00069 {
00070 memset(&_industry_specs, 0, sizeof(_industry_specs));
00071 memcpy(&_industry_specs, &_origin_industry_specs, sizeof(_origin_industry_specs));
00072
00073
00074 for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
00075 _industry_specs[i].enabled = i < NEW_INDUSTRYOFFSET &&
00076 HasBit(_origin_industry_specs[i].climate_availability, _settings_game.game_creation.landscape);
00077 }
00078
00079 memset(&_industry_tile_specs, 0, sizeof(_industry_tile_specs));
00080 memcpy(&_industry_tile_specs, &_origin_industry_tile_specs, sizeof(_origin_industry_tile_specs));
00081
00082
00083 _industile_mngr.ResetOverride();
00084 _industry_mngr.ResetOverride();
00085 }
00086
00095 IndustryType GetIndustryType(TileIndex tile)
00096 {
00097 assert(IsTileType(tile, MP_INDUSTRY));
00098
00099 const Industry *ind = Industry::GetByTile(tile);
00100 assert(ind != NULL);
00101 return ind->type;
00102 }
00103
00112 const IndustrySpec *GetIndustrySpec(IndustryType thistype)
00113 {
00114 assert(thistype < NUM_INDUSTRYTYPES);
00115 return &_industry_specs[thistype];
00116 }
00117
00126 const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
00127 {
00128 assert(gfx < INVALID_INDUSTRYTILE);
00129 return &_industry_tile_specs[gfx];
00130 }
00131
00132 Industry::~Industry()
00133 {
00134 if (CleaningPool()) return;
00135
00136
00137
00138
00139 if (this->location.w == 0) return;
00140
00141 TILE_AREA_LOOP(tile_cur, this->location) {
00142 if (IsTileType(tile_cur, MP_INDUSTRY)) {
00143 if (GetIndustryIndex(tile_cur) == this->index) {
00144 DeleteNewGRFInspectWindow(GSF_INDUSTRYTILES, tile_cur);
00145
00146
00147 MakeWaterKeepingClass(tile_cur, OWNER_NONE);
00148 }
00149 } else if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
00150 DeleteOilRig(tile_cur);
00151 }
00152 }
00153
00154 if (GetIndustrySpec(this->type)->behaviour & INDUSTRYBEH_PLANT_FIELDS) {
00155 TileArea ta(this->location.tile - TileDiffXY(min(TileX(this->location.tile), 21), min(TileY(this->location.tile), 21)), 42, 42);
00156 ta.ClampToMap();
00157
00158
00159 TILE_AREA_LOOP(tile_cur, ta) {
00160 if (IsTileType(tile_cur, MP_CLEAR) && IsClearGround(tile_cur, CLEAR_FIELDS) &&
00161 GetIndustryIndexOfField(tile_cur) == this->index) {
00162 SetIndustryIndexOfField(tile_cur, INVALID_INDUSTRY);
00163 }
00164 }
00165 }
00166
00167
00168 ReleaseDisastersTargetingIndustry(this->index);
00169
00170 DecIndustryTypeCount(this->type);
00171
00172 DeleteIndustryNews(this->index);
00173 DeleteWindowById(WC_INDUSTRY_VIEW, this->index);
00174 DeleteNewGRFInspectWindow(GSF_INDUSTRIES, this->index);
00175
00176 DeleteSubsidyWith(ST_INDUSTRY, this->index);
00177 CargoPacket::InvalidateAllFrom(ST_INDUSTRY, this->index);
00178 }
00179
00184 void Industry::PostDestructor(size_t index)
00185 {
00186 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
00187 Station::RecomputeIndustriesNearForAll();
00188 }
00189
00190
00200 Industry *Industry::GetRandom(EnumIndustryProc enum_proc, IndustryID skip, void *data)
00201 {
00202 assert(skip == INVALID_INDUSTRY || Industry::IsValidID(skip));
00203
00204 uint16 max_num = 0;
00205 if (enum_proc != NULL) {
00206
00207 Industry *ind;
00208 FOR_ALL_INDUSTRIES(ind) {
00209 if (ind->index != skip && enum_proc(ind, data)) max_num++;
00210 }
00211 } else {
00212 max_num = (uint16)Industry::GetNumItems();
00213
00214
00215 if (skip != INVALID_INDUSTRY) max_num--;
00216 }
00217 if (max_num == 0) return NULL;
00218
00219 uint num = RandomRange(max_num) + 1;
00220 size_t index = MAX_UVALUE(size_t);
00221 do {
00222 index++;
00223
00224
00225 while (!Industry::IsValidID(index)) {
00226 index++;
00227 assert(index < Industry::GetPoolSize());
00228 }
00229
00230 if (index != skip && (enum_proc == NULL || enum_proc(Industry::Get(index), data))) num--;
00231 } while (num > 0);
00232
00233 return Industry::Get(index);
00234 }
00235
00236
00237 static void IndustryDrawSugarMine(const TileInfo *ti)
00238 {
00239 if (!IsIndustryCompleted(ti->tile)) return;
00240
00241 const DrawIndustryAnimationStruct *d = &_draw_industry_spec1[GetAnimationFrame(ti->tile)];
00242
00243 AddChildSpriteScreen(SPR_IT_SUGAR_MINE_SIEVE + d->image_1, PAL_NONE, d->x, 0);
00244
00245 if (d->image_2 != 0) {
00246 AddChildSpriteScreen(SPR_IT_SUGAR_MINE_CLOUDS + d->image_2 - 1, PAL_NONE, 8, 41);
00247 }
00248
00249 if (d->image_3 != 0) {
00250 AddChildSpriteScreen(SPR_IT_SUGAR_MINE_PILE + d->image_3 - 1, PAL_NONE,
00251 _drawtile_proc1[d->image_3 - 1].x, _drawtile_proc1[d->image_3 - 1].y);
00252 }
00253 }
00254
00255 static void IndustryDrawToffeeQuarry(const TileInfo *ti)
00256 {
00257 uint8 x = 0;
00258
00259 if (IsIndustryCompleted(ti->tile)) {
00260 x = _industry_anim_offs_toffee[GetAnimationFrame(ti->tile)];
00261 if (x == 0xFF) {
00262 x = 0;
00263 }
00264 }
00265
00266 AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_SHOVEL, PAL_NONE, 22 - x, 24 + x);
00267 AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_TOFFEE, PAL_NONE, 6, 14);
00268 }
00269
00270 static void IndustryDrawBubbleGenerator( const TileInfo *ti)
00271 {
00272 if (IsIndustryCompleted(ti->tile)) {
00273 AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_BUBBLE, PAL_NONE, 5, _industry_anim_offs_bubbles[GetAnimationFrame(ti->tile)]);
00274 } else {
00275 AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_SPRING, PAL_NONE, 3, 67);
00276 }
00277 }
00278
00279 static void IndustryDrawToyFactory(const TileInfo *ti)
00280 {
00281 const DrawIndustryAnimationStruct *d = &_industry_anim_offs_toys[GetAnimationFrame(ti->tile)];
00282
00283 if (d->image_1 != 0xFF) {
00284 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_CLAY, PAL_NONE, d->x, 96 + d->image_1);
00285 }
00286
00287 if (d->image_2 != 0xFF) {
00288 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_ROBOT, PAL_NONE, 16 - d->image_2 * 2, 100 + d->image_2);
00289 }
00290
00291 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP, PAL_NONE, 7, d->image_3);
00292 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP_HOLDER, PAL_NONE, 0, 42);
00293 }
00294
00295 static void IndustryDrawCoalPlantSparks(const TileInfo *ti)
00296 {
00297 if (IsIndustryCompleted(ti->tile)) {
00298 uint8 image = GetAnimationFrame(ti->tile);
00299
00300 if (image != 0 && image < 7) {
00301 AddChildSpriteScreen(image + SPR_IT_POWER_PLANT_TRANSFORMERS,
00302 PAL_NONE,
00303 _coal_plant_sparks[image - 1].x,
00304 _coal_plant_sparks[image - 1].y
00305 );
00306 }
00307 }
00308 }
00309
00310 typedef void IndustryDrawTileProc(const TileInfo *ti);
00311 static IndustryDrawTileProc * const _industry_draw_tile_procs[5] = {
00312 IndustryDrawSugarMine,
00313 IndustryDrawToffeeQuarry,
00314 IndustryDrawBubbleGenerator,
00315 IndustryDrawToyFactory,
00316 IndustryDrawCoalPlantSparks,
00317 };
00318
00319 static void DrawTile_Industry(TileInfo *ti)
00320 {
00321 IndustryGfx gfx = GetIndustryGfx(ti->tile);
00322 Industry *ind = Industry::GetByTile(ti->tile);
00323 const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
00324
00325
00326 if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00327
00328
00329
00330
00331 if (indts->grf_prop.spritegroup[0] != NULL && DrawNewIndustryTile(ti, ind, gfx, indts)) {
00332 return;
00333 } else {
00334
00335
00336 if (indts->grf_prop.subst_id != INVALID_INDUSTRYTILE) {
00337 gfx = indts->grf_prop.subst_id;
00338
00339 indts = GetIndustryTileSpec(gfx);
00340 }
00341 }
00342 }
00343
00344 const DrawBuildingsTileStruct *dits = &_industry_draw_tile_data[gfx << 2 | (indts->anim_state ?
00345 GetAnimationFrame(ti->tile) & INDUSTRY_COMPLETED :
00346 GetIndustryConstructionStage(ti->tile))];
00347
00348 SpriteID image = dits->ground.sprite;
00349
00350
00351 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
00352
00353
00354
00355 if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
00356 DrawWaterClassGround(ti);
00357 } else {
00358 DrawGroundSprite(image, GroundSpritePaletteTransform(image, dits->ground.pal, GENERAL_SPRITE_COLOUR(ind->random_colour)));
00359 }
00360
00361
00362 if (IsInvisibilitySet(TO_INDUSTRIES)) return;
00363
00364
00365 image = dits->building.sprite;
00366 if (image != 0) {
00367 AddSortableSpriteToDraw(image, SpriteLayoutPaletteTransform(image, dits->building.pal, GENERAL_SPRITE_COLOUR(ind->random_colour)),
00368 ti->x + dits->subtile_x,
00369 ti->y + dits->subtile_y,
00370 dits->width,
00371 dits->height,
00372 dits->dz,
00373 ti->z,
00374 IsTransparencySet(TO_INDUSTRIES));
00375
00376 if (IsTransparencySet(TO_INDUSTRIES)) return;
00377 }
00378
00379 {
00380 int proc = dits->draw_proc - 1;
00381 if (proc >= 0) _industry_draw_tile_procs[proc](ti);
00382 }
00383 }
00384
00385 static uint GetSlopeZ_Industry(TileIndex tile, uint x, uint y)
00386 {
00387 return GetTileMaxZ(tile);
00388 }
00389
00390 static Foundation GetFoundation_Industry(TileIndex tile, Slope tileh)
00391 {
00392 IndustryGfx gfx = GetIndustryGfx(tile);
00393
00394
00395
00396
00397
00398 if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00399 const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
00400 if (indts->grf_prop.spritegroup[0] != NULL && HasBit(indts->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
00401 uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, Industry::GetByTile(tile), tile);
00402 if (callback_res == 0) return FOUNDATION_NONE;
00403 }
00404 }
00405 return FlatteningFoundation(tileh);
00406 }
00407
00408 static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray &acceptance, uint32 *always_accepted)
00409 {
00410 IndustryGfx gfx = GetIndustryGfx(tile);
00411 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00412
00413
00414 CargoID raw_accepts_cargo[lengthof(itspec->accepts_cargo)];
00415 uint8 raw_cargo_acceptance[lengthof(itspec->acceptance)];
00416
00417
00418 const CargoID *accepts_cargo = itspec->accepts_cargo;
00419 const uint8 *cargo_acceptance = itspec->acceptance;
00420
00421 if (HasBit(itspec->callback_mask, CBM_INDT_ACCEPT_CARGO)) {
00422 uint16 res = GetIndustryTileCallback(CBID_INDTILE_ACCEPT_CARGO, 0, 0, gfx, Industry::GetByTile(tile), tile);
00423 if (res != CALLBACK_FAILED) {
00424 accepts_cargo = raw_accepts_cargo;
00425 for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_accepts_cargo[i] = GetCargoTranslation(GB(res, i * 5, 5), itspec->grf_prop.grffile);
00426 }
00427 }
00428
00429 if (HasBit(itspec->callback_mask, CBM_INDT_CARGO_ACCEPTANCE)) {
00430 uint16 res = GetIndustryTileCallback(CBID_INDTILE_CARGO_ACCEPTANCE, 0, 0, gfx, Industry::GetByTile(tile), tile);
00431 if (res != CALLBACK_FAILED) {
00432 cargo_acceptance = raw_cargo_acceptance;
00433 for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_cargo_acceptance[i] = GB(res, i * 4, 4);
00434 }
00435 }
00436
00437 const Industry *ind = Industry::GetByTile(tile);
00438 for (byte i = 0; i < lengthof(itspec->accepts_cargo); i++) {
00439 CargoID a = accepts_cargo[i];
00440 if (a == CT_INVALID || cargo_acceptance[i] == 0) continue;
00441
00442
00443 acceptance[a] += cargo_acceptance[i];
00444
00445
00446 if (HasBit(*always_accepted, a)) continue;
00447
00448 bool accepts = false;
00449 for (uint cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
00450
00451 if (ind->accepts_cargo[cargo_index] == a) {
00452 accepts = true;
00453 break;
00454 }
00455 }
00456
00457 if (accepts) continue;
00458
00459
00460 SetBit(*always_accepted, a);
00461 }
00462 }
00463
00464 static void GetTileDesc_Industry(TileIndex tile, TileDesc *td)
00465 {
00466 const Industry *i = Industry::GetByTile(tile);
00467 const IndustrySpec *is = GetIndustrySpec(i->type);
00468
00469 td->owner[0] = i->owner;
00470 td->str = is->name;
00471 if (!IsIndustryCompleted(tile)) {
00472 SetDParamX(td->dparam, 0, td->str);
00473 td->str = STR_LAI_TOWN_INDUSTRY_DESCRIPTION_UNDER_CONSTRUCTION;
00474 }
00475
00476 if (is->grf_prop.grffile != NULL) {
00477 td->grf = GetGRFConfig(is->grf_prop.grffile->grfid)->GetName();
00478 }
00479 }
00480
00481 static CommandCost ClearTile_Industry(TileIndex tile, DoCommandFlag flags)
00482 {
00483 Industry *i = Industry::GetByTile(tile);
00484 const IndustrySpec *indspec = GetIndustrySpec(i->type);
00485
00486
00487
00488
00489
00490
00491 if ((_current_company != OWNER_WATER && _game_mode != GM_EDITOR &&
00492 !_cheats.magic_bulldozer.value) ||
00493 ((flags & DC_AUTO) != 0) ||
00494 (_current_company == OWNER_WATER &&
00495 ((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) ||
00496 HasBit(GetIndustryTileSpec(GetIndustryGfx(tile))->slopes_refused, 5)))) {
00497 SetDParam(1, indspec->name);
00498 return_cmd_error(flags & DC_AUTO ? STR_ERROR_GENERIC_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
00499 }
00500
00501 if (flags & DC_EXEC) {
00502 AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
00503 delete i;
00504 }
00505 return CommandCost(EXPENSES_CONSTRUCTION, indspec->GetRemovalCost());
00506 }
00507
00508 static void TransportIndustryGoods(TileIndex tile)
00509 {
00510 Industry *i = Industry::GetByTile(tile);
00511 const IndustrySpec *indspec = GetIndustrySpec(i->type);
00512 bool moved_cargo = false;
00513
00514 StationFinder stations(i->location);
00515
00516 for (uint j = 0; j < lengthof(i->produced_cargo_waiting); j++) {
00517 uint cw = min(i->produced_cargo_waiting[j], 255);
00518 if (cw > indspec->minimal_cargo && i->produced_cargo[j] != CT_INVALID) {
00519 i->produced_cargo_waiting[j] -= cw;
00520
00521
00522 if (EconomyIsInRecession()) cw = (cw + 1) / 2;
00523
00524 i->this_month_production[j] += cw;
00525
00526 uint am = MoveGoodsToStation(i->produced_cargo[j], cw, ST_INDUSTRY, i->index, stations.GetStations(), tile);
00527 i->this_month_transported[j] += am;
00528
00529 moved_cargo |= (am != 0);
00530 }
00531 }
00532
00533 if (moved_cargo && !StartStopIndustryTileAnimation(i, IAT_INDUSTRY_DISTRIBUTES_CARGO)) {
00534 uint newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_production;
00535
00536 if (newgfx != INDUSTRYTILE_NOANIM) {
00537 ResetIndustryConstructionStage(tile);
00538 SetIndustryCompleted(tile, true);
00539 SetIndustryGfx(tile, newgfx);
00540 MarkTileDirtyByTile(tile);
00541 }
00542 }
00543 }
00544
00545
00546 static void AnimateTile_Industry(TileIndex tile)
00547 {
00548 IndustryGfx gfx = GetIndustryGfx(tile);
00549
00550 if (GetIndustryTileSpec(gfx)->animation.status != ANIM_STATUS_NO_ANIMATION) {
00551 AnimateNewIndustryTile(tile);
00552 return;
00553 }
00554
00555 switch (gfx) {
00556 case GFX_SUGAR_MINE_SIEVE:
00557 if ((_tick_counter & 1) == 0) {
00558 byte m = GetAnimationFrame(tile) + 1;
00559
00560 switch (m & 7) {
00561 case 2: SndPlayTileFx(SND_2D_RIP_2, tile); break;
00562 case 6: SndPlayTileFx(SND_29_RIP, tile); break;
00563 }
00564
00565 if (m >= 96) {
00566 m = 0;
00567 DeleteAnimatedTile(tile);
00568 }
00569 SetAnimationFrame(tile, m);
00570
00571 MarkTileDirtyByTile(tile);
00572 }
00573 break;
00574
00575 case GFX_TOFFEE_QUARY:
00576 if ((_tick_counter & 3) == 0) {
00577 byte m = GetAnimationFrame(tile);
00578
00579 if (_industry_anim_offs_toffee[m] == 0xFF) {
00580 SndPlayTileFx(SND_30_CARTOON_SOUND, tile);
00581 }
00582
00583 if (++m >= 70) {
00584 m = 0;
00585 DeleteAnimatedTile(tile);
00586 }
00587 SetAnimationFrame(tile, m);
00588
00589 MarkTileDirtyByTile(tile);
00590 }
00591 break;
00592
00593 case GFX_BUBBLE_CATCHER:
00594 if ((_tick_counter & 1) == 0) {
00595 byte m = GetAnimationFrame(tile);
00596
00597 if (++m >= 40) {
00598 m = 0;
00599 DeleteAnimatedTile(tile);
00600 }
00601 SetAnimationFrame(tile, m);
00602
00603 MarkTileDirtyByTile(tile);
00604 }
00605 break;
00606
00607
00608 case GFX_POWERPLANT_SPARKS:
00609 if ((_tick_counter & 3) == 0) {
00610 byte m = GetAnimationFrame(tile);
00611 if (m == 6) {
00612 SetAnimationFrame(tile, 0);
00613 DeleteAnimatedTile(tile);
00614 } else {
00615 SetAnimationFrame(tile, m + 1);
00616 MarkTileDirtyByTile(tile);
00617 }
00618 }
00619 break;
00620
00621 case GFX_TOY_FACTORY:
00622 if ((_tick_counter & 1) == 0) {
00623 byte m = GetAnimationFrame(tile) + 1;
00624
00625 switch (m) {
00626 case 1: SndPlayTileFx(SND_2C_MACHINERY, tile); break;
00627 case 23: SndPlayTileFx(SND_2B_COMEDY_HIT, tile); break;
00628 case 28: SndPlayTileFx(SND_2A_EXTRACT_AND_POP, tile); break;
00629 default:
00630 if (m >= 50) {
00631 int n = GetIndustryAnimationLoop(tile) + 1;
00632 m = 0;
00633 if (n >= 8) {
00634 n = 0;
00635 DeleteAnimatedTile(tile);
00636 }
00637 SetIndustryAnimationLoop(tile, n);
00638 }
00639 }
00640
00641 SetAnimationFrame(tile, m);
00642 MarkTileDirtyByTile(tile);
00643 }
00644 break;
00645
00646 case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
00647 case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
00648 case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
00649 case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
00650 if ((_tick_counter & 3) == 0) {
00651 IndustryGfx gfx = GetIndustryGfx(tile);
00652
00653 gfx = (gfx < 155) ? gfx + 1 : 148;
00654 SetIndustryGfx(tile, gfx);
00655 MarkTileDirtyByTile(tile);
00656 }
00657 break;
00658
00659 case GFX_OILWELL_ANIMATED_1:
00660 case GFX_OILWELL_ANIMATED_2:
00661 case GFX_OILWELL_ANIMATED_3:
00662 if ((_tick_counter & 7) == 0) {
00663 bool b = Chance16(1, 7);
00664 IndustryGfx gfx = GetIndustryGfx(tile);
00665
00666 byte m = GetAnimationFrame(tile) + 1;
00667 if (m == 4 && (m = 0, ++gfx) == GFX_OILWELL_ANIMATED_3 + 1 && (gfx = GFX_OILWELL_ANIMATED_1, b)) {
00668 SetIndustryGfx(tile, GFX_OILWELL_NOT_ANIMATED);
00669 SetIndustryConstructionStage(tile, 3);
00670 DeleteAnimatedTile(tile);
00671 } else {
00672 SetAnimationFrame(tile, m);
00673 SetIndustryGfx(tile, gfx);
00674 MarkTileDirtyByTile(tile);
00675 }
00676 }
00677 break;
00678
00679 case GFX_COAL_MINE_TOWER_ANIMATED:
00680 case GFX_COPPER_MINE_TOWER_ANIMATED:
00681 case GFX_GOLD_MINE_TOWER_ANIMATED: {
00682 int state = _tick_counter & 0x7FF;
00683
00684 if ((state -= 0x400) < 0) return;
00685
00686 if (state < 0x1A0) {
00687 if (state < 0x20 || state >= 0x180) {
00688 byte m = GetAnimationFrame(tile);
00689 if (!(m & 0x40)) {
00690 SetAnimationFrame(tile, m | 0x40);
00691 SndPlayTileFx(SND_0B_MINING_MACHINERY, tile);
00692 }
00693 if (state & 7) return;
00694 } else {
00695 if (state & 3) return;
00696 }
00697 byte m = (GetAnimationFrame(tile) + 1) | 0x40;
00698 if (m > 0xC2) m = 0xC0;
00699 SetAnimationFrame(tile, m);
00700 MarkTileDirtyByTile(tile);
00701 } else if (state >= 0x200 && state < 0x3A0) {
00702 int i = (state < 0x220 || state >= 0x380) ? 7 : 3;
00703 if (state & i) return;
00704
00705 byte m = (GetAnimationFrame(tile) & 0xBF) - 1;
00706 if (m < 0x80) m = 0x82;
00707 SetAnimationFrame(tile, m);
00708 MarkTileDirtyByTile(tile);
00709 }
00710 break;
00711 }
00712 }
00713 }
00714
00715 static void CreateChimneySmoke(TileIndex tile)
00716 {
00717 uint x = TileX(tile) * TILE_SIZE;
00718 uint y = TileY(tile) * TILE_SIZE;
00719 uint z = GetTileMaxZ(tile);
00720
00721 CreateEffectVehicle(x + 15, y + 14, z + 59, EV_CHIMNEY_SMOKE);
00722 }
00723
00724 static void MakeIndustryTileBigger(TileIndex tile)
00725 {
00726 byte cnt = GetIndustryConstructionCounter(tile) + 1;
00727 if (cnt != 4) {
00728 SetIndustryConstructionCounter(tile, cnt);
00729 return;
00730 }
00731
00732 byte stage = GetIndustryConstructionStage(tile) + 1;
00733 SetIndustryConstructionCounter(tile, 0);
00734 SetIndustryConstructionStage(tile, stage);
00735 StartStopIndustryTileAnimation(tile, IAT_CONSTRUCTION_STATE_CHANGE);
00736 if (stage == INDUSTRY_COMPLETED) SetIndustryCompleted(tile, true);
00737
00738 MarkTileDirtyByTile(tile);
00739
00740 if (!IsIndustryCompleted(tile)) return;
00741
00742 IndustryGfx gfx = GetIndustryGfx(tile);
00743 if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00744
00745 return;
00746 }
00747
00748 switch (gfx) {
00749 case GFX_POWERPLANT_CHIMNEY:
00750 CreateChimneySmoke(tile);
00751 break;
00752
00753 case GFX_OILRIG_1: {
00754
00755
00756
00757
00758 TileIndex other = tile + TileDiffXY(0, 1);
00759
00760 if (IsTileType(other, MP_INDUSTRY) &&
00761 GetIndustryGfx(other) == GFX_OILRIG_1 &&
00762 GetIndustryIndex(tile) == GetIndustryIndex(other)) {
00763 BuildOilRig(tile);
00764 }
00765 break;
00766 }
00767
00768 case GFX_TOY_FACTORY:
00769 case GFX_BUBBLE_CATCHER:
00770 case GFX_TOFFEE_QUARY:
00771 SetAnimationFrame(tile, 0);
00772 SetIndustryAnimationLoop(tile, 0);
00773 break;
00774
00775 case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
00776 case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
00777 case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
00778 case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
00779 AddAnimatedTile(tile);
00780 break;
00781 }
00782 }
00783
00784 static void TileLoopIndustry_BubbleGenerator(TileIndex tile)
00785 {
00786 static const int8 _bubble_spawn_location[3][4] = {
00787 { 11, 0, -4, -14 },
00788 { -4, -10, -4, 1 },
00789 { 49, 59, 60, 65 },
00790 };
00791
00792 SndPlayTileFx(SND_2E_EXTRACT_AND_POP, tile);
00793
00794 int dir = Random() & 3;
00795
00796 EffectVehicle *v = CreateEffectVehicleAbove(
00797 TileX(tile) * TILE_SIZE + _bubble_spawn_location[0][dir],
00798 TileY(tile) * TILE_SIZE + _bubble_spawn_location[1][dir],
00799 _bubble_spawn_location[2][dir],
00800 EV_BUBBLE
00801 );
00802
00803 if (v != NULL) v->animation_substate = dir;
00804 }
00805
00806 static void TileLoop_Industry(TileIndex tile)
00807 {
00808 if (IsTileOnWater(tile)) TileLoop_Water(tile);
00809
00810
00811
00812
00813
00814 if (!IsTileType(tile, MP_INDUSTRY)) return;
00815
00816 TriggerIndustryTile(tile, INDTILE_TRIGGER_TILE_LOOP);
00817
00818 if (!IsIndustryCompleted(tile)) {
00819 MakeIndustryTileBigger(tile);
00820 return;
00821 }
00822
00823 if (_game_mode == GM_EDITOR) return;
00824
00825 TransportIndustryGoods(tile);
00826
00827 if (StartStopIndustryTileAnimation(tile, IAT_TILELOOP)) return;
00828
00829 IndustryGfx newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_next;
00830 if (newgfx != INDUSTRYTILE_NOANIM) {
00831 ResetIndustryConstructionStage(tile);
00832 SetIndustryGfx(tile, newgfx);
00833 MarkTileDirtyByTile(tile);
00834 return;
00835 }
00836
00837 IndustryGfx gfx = GetIndustryGfx(tile);
00838 switch (gfx) {
00839 case GFX_COAL_MINE_TOWER_NOT_ANIMATED:
00840 case GFX_COPPER_MINE_TOWER_NOT_ANIMATED:
00841 case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:
00842 if (!(_tick_counter & 0x400) && Chance16(1, 2)) {
00843 switch (gfx) {
00844 case GFX_COAL_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COAL_MINE_TOWER_ANIMATED; break;
00845 case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_ANIMATED; break;
00846 case GFX_GOLD_MINE_TOWER_NOT_ANIMATED: gfx = GFX_GOLD_MINE_TOWER_ANIMATED; break;
00847 }
00848 SetIndustryGfx(tile, gfx);
00849 SetAnimationFrame(tile, 0x80);
00850 AddAnimatedTile(tile);
00851 }
00852 break;
00853
00854 case GFX_OILWELL_NOT_ANIMATED:
00855 if (Chance16(1, 6)) {
00856 SetIndustryGfx(tile, GFX_OILWELL_ANIMATED_1);
00857 SetAnimationFrame(tile, 0);
00858 AddAnimatedTile(tile);
00859 }
00860 break;
00861
00862 case GFX_COAL_MINE_TOWER_ANIMATED:
00863 case GFX_COPPER_MINE_TOWER_ANIMATED:
00864 case GFX_GOLD_MINE_TOWER_ANIMATED:
00865 if (!(_tick_counter & 0x400)) {
00866 switch (gfx) {
00867 case GFX_COAL_MINE_TOWER_ANIMATED: gfx = GFX_COAL_MINE_TOWER_NOT_ANIMATED; break;
00868 case GFX_COPPER_MINE_TOWER_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_NOT_ANIMATED; break;
00869 case GFX_GOLD_MINE_TOWER_ANIMATED: gfx = GFX_GOLD_MINE_TOWER_NOT_ANIMATED; break;
00870 }
00871 SetIndustryGfx(tile, gfx);
00872 SetIndustryCompleted(tile, true);
00873 SetIndustryConstructionStage(tile, 3);
00874 DeleteAnimatedTile(tile);
00875 }
00876 break;
00877
00878 case GFX_POWERPLANT_SPARKS:
00879 if (Chance16(1, 3)) {
00880 SndPlayTileFx(SND_0C_ELECTRIC_SPARK, tile);
00881 AddAnimatedTile(tile);
00882 }
00883 break;
00884
00885 case GFX_COPPER_MINE_CHIMNEY:
00886 CreateEffectVehicleAbove(TileX(tile) * TILE_SIZE + 6, TileY(tile) * TILE_SIZE + 6, 43, EV_COPPER_MINE_SMOKE);
00887 break;
00888
00889
00890 case GFX_TOY_FACTORY: {
00891 Industry *i = Industry::GetByTile(tile);
00892 if (i->was_cargo_delivered) {
00893 i->was_cargo_delivered = false;
00894 SetIndustryAnimationLoop(tile, 0);
00895 AddAnimatedTile(tile);
00896 }
00897 }
00898 break;
00899
00900 case GFX_BUBBLE_GENERATOR:
00901 TileLoopIndustry_BubbleGenerator(tile);
00902 break;
00903
00904 case GFX_TOFFEE_QUARY:
00905 AddAnimatedTile(tile);
00906 break;
00907
00908 case GFX_SUGAR_MINE_SIEVE:
00909 if (Chance16(1, 3)) AddAnimatedTile(tile);
00910 break;
00911 }
00912 }
00913
00914 static bool ClickTile_Industry(TileIndex tile)
00915 {
00916 ShowIndustryViewWindow(GetIndustryIndex(tile));
00917 return true;
00918 }
00919
00920 static TrackStatus GetTileTrackStatus_Industry(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
00921 {
00922 return 0;
00923 }
00924
00925 static void ChangeTileOwner_Industry(TileIndex tile, Owner old_owner, Owner new_owner)
00926 {
00927
00928 Industry *i = Industry::GetByTile(tile);
00929 if (i->founder == old_owner) i->founder = (new_owner == INVALID_OWNER) ? OWNER_NONE : new_owner;
00930 }
00931
00932 static const byte _plantfarmfield_type[] = {1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6};
00933
00934 static bool IsBadFarmFieldTile(TileIndex tile)
00935 {
00936 switch (GetTileType(tile)) {
00937 case MP_CLEAR: return IsClearGround(tile, CLEAR_FIELDS) || IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
00938 case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
00939 default: return true;
00940 }
00941 }
00942
00943 static bool IsBadFarmFieldTile2(TileIndex tile)
00944 {
00945 switch (GetTileType(tile)) {
00946 case MP_CLEAR: return IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
00947 case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
00948 default: return true;
00949 }
00950 }
00951
00952 static void SetupFarmFieldFence(TileIndex tile, int size, byte type, Axis direction)
00953 {
00954 do {
00955 tile = TILE_MASK(tile);
00956
00957 if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) {
00958 byte or_ = type;
00959
00960 if (or_ == 1 && Chance16(1, 7)) or_ = 2;
00961
00962 if (direction == AXIS_X) {
00963 SetFenceSE(tile, or_);
00964 } else {
00965 SetFenceSW(tile, or_);
00966 }
00967 }
00968
00969 tile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00970 } while (--size);
00971 }
00972
00973 static void PlantFarmField(TileIndex tile, IndustryID industry)
00974 {
00975 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
00976 if (GetTileZ(tile) + TILE_HEIGHT * 2 >= GetSnowLine()) return;
00977 }
00978
00979
00980 uint32 r = (Random() & 0x303) + 0x404;
00981 if (_settings_game.game_creation.landscape == LT_ARCTIC) r += 0x404;
00982 uint size_x = GB(r, 0, 8);
00983 uint size_y = GB(r, 8, 8);
00984
00985 TileArea ta(tile - TileDiffXY(min(TileX(tile), size_x / 2), min(TileY(tile), size_y / 2)), size_x, size_y);
00986 ta.ClampToMap();
00987
00988 if (ta.w == 0 || ta.h == 0) return;
00989
00990
00991 int count = 0;
00992 TILE_AREA_LOOP(cur_tile, ta) {
00993 assert(cur_tile < MapSize());
00994 count += IsBadFarmFieldTile(cur_tile);
00995 }
00996 if (count * 2 >= ta.w * ta.h) return;
00997
00998
00999 r = Random();
01000 uint counter = GB(r, 5, 3);
01001 uint field_type = GB(r, 8, 8) * 9 >> 8;
01002
01003
01004 TILE_AREA_LOOP(cur_tile, ta) {
01005 assert(cur_tile < MapSize());
01006 if (!IsBadFarmFieldTile2(cur_tile)) {
01007 MakeField(cur_tile, field_type, industry);
01008 SetClearCounter(cur_tile, counter);
01009 MarkTileDirtyByTile(cur_tile);
01010 }
01011 }
01012
01013 int type = 3;
01014 if (_settings_game.game_creation.landscape != LT_ARCTIC && _settings_game.game_creation.landscape != LT_TROPIC) {
01015 type = _plantfarmfield_type[Random() & 0xF];
01016 }
01017
01018 SetupFarmFieldFence(ta.tile - TileDiffXY(1, 0), ta.h, type, AXIS_Y);
01019 SetupFarmFieldFence(ta.tile - TileDiffXY(0, 1), ta.w, type, AXIS_X);
01020 SetupFarmFieldFence(ta.tile + TileDiffXY(ta.w - 1, 0), ta.h, type, AXIS_Y);
01021 SetupFarmFieldFence(ta.tile + TileDiffXY(0, ta.h - 1), ta.w, type, AXIS_X);
01022 }
01023
01024 void PlantRandomFarmField(const Industry *i)
01025 {
01026 int x = i->location.w / 2 + Random() % 31 - 16;
01027 int y = i->location.h / 2 + Random() % 31 - 16;
01028
01029 TileIndex tile = TileAddWrap(i->location.tile, x, y);
01030
01031 if (tile != INVALID_TILE) PlantFarmField(tile, i->index);
01032 }
01033
01040 static bool SearchLumberMillTrees(TileIndex tile, void *user_data)
01041 {
01042 if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) {
01043
01044
01045 Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
01046
01047 _industry_sound_ctr = 1;
01048 _industry_sound_tile = tile;
01049 SndPlayTileFx(SND_38_CHAINSAW, tile);
01050
01051 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
01052
01053 cur_company.Restore();
01054 return true;
01055 }
01056 return false;
01057 }
01058
01063 static void ChopLumberMillTrees(Industry *i)
01064 {
01065 TileIndex tile = i->location.tile;
01066
01067 if (!IsIndustryCompleted(tile)) return;
01068
01069 if (CircularTileSearch(&tile, 40, SearchLumberMillTrees, NULL)) {
01070 i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + 45);
01071 }
01072 }
01073
01074 static void ProduceIndustryGoods(Industry *i)
01075 {
01076 const IndustrySpec *indsp = GetIndustrySpec(i->type);
01077
01078
01079 if ((i->counter & 0x3F) == 0) {
01080 uint32 r;
01081 uint num;
01082 if (Chance16R(1, 14, r) && (num = indsp->number_of_sounds) != 0) {
01083 SndPlayTileFx(
01084 (SoundFx)(indsp->random_sounds[((r >> 16) * num) >> 16]),
01085 i->location.tile);
01086 }
01087 }
01088
01089 i->counter--;
01090
01091
01092 if ((i->counter % INDUSTRY_PRODUCE_TICKS) == 0) {
01093 if (HasBit(indsp->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) IndustryProductionCallback(i, 1);
01094
01095 IndustryBehaviour indbehav = indsp->behaviour;
01096 i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + i->production_rate[0]);
01097 i->produced_cargo_waiting[1] = min(0xffff, i->produced_cargo_waiting[1] + i->production_rate[1]);
01098
01099 if ((indbehav & INDUSTRYBEH_PLANT_FIELDS) != 0) {
01100 bool plant;
01101 if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
01102 plant = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, Random(), 0, i, i->type, i->location.tile) != 0);
01103 } else {
01104 plant = Chance16(1, 8);
01105 }
01106
01107 if (plant) PlantRandomFarmField(i);
01108 }
01109 if ((indbehav & INDUSTRYBEH_CUT_TREES) != 0) {
01110 bool cut = ((i->counter & 0x1FF) == 0);
01111 if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
01112 cut = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, 0, 1, i, i->type, i->location.tile) != 0);
01113 }
01114
01115 if (cut) ChopLumberMillTrees(i);
01116 }
01117
01118 TriggerIndustry(i, INDUSTRY_TRIGGER_INDUSTRY_TICK);
01119 StartStopIndustryTileAnimation(i, IAT_INDUSTRY_TICK);
01120 }
01121 }
01122
01123 void OnTick_Industry()
01124 {
01125 if (_industry_sound_ctr != 0) {
01126 _industry_sound_ctr++;
01127
01128 if (_industry_sound_ctr == 75) {
01129 SndPlayTileFx(SND_37_BALLOON_SQUEAK, _industry_sound_tile);
01130 } else if (_industry_sound_ctr == 160) {
01131 _industry_sound_ctr = 0;
01132 SndPlayTileFx(SND_36_CARTOON_CRASH, _industry_sound_tile);
01133 }
01134 }
01135
01136 if (_game_mode == GM_EDITOR) return;
01137
01138 Industry *i;
01139 FOR_ALL_INDUSTRIES(i) {
01140 ProduceIndustryGoods(i);
01141 }
01142 }
01143
01149 static CommandCost CheckNewIndustry_NULL(TileIndex tile)
01150 {
01151 return CommandCost();
01152 }
01153
01159 static CommandCost CheckNewIndustry_Forest(TileIndex tile)
01160 {
01161 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
01162 if (GetTileZ(tile) < HighestSnowLine() + TILE_HEIGHT * 2U) {
01163 return_cmd_error(STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED);
01164 }
01165 }
01166 return CommandCost();
01167 }
01168
01174 static CommandCost CheckNewIndustry_OilRefinery(TileIndex tile)
01175 {
01176 if (_game_mode == GM_EDITOR) return CommandCost();
01177 if (DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return CommandCost();
01178
01179 return_cmd_error(STR_ERROR_CAN_ONLY_BE_POSITIONED);
01180 }
01181
01182 extern bool _ignore_restrictions;
01183
01189 static CommandCost CheckNewIndustry_OilRig(TileIndex tile)
01190 {
01191 if (_game_mode == GM_EDITOR && _ignore_restrictions) return CommandCost();
01192 if (TileHeight(tile) == 0 &&
01193 DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return CommandCost();
01194
01195 return_cmd_error(STR_ERROR_CAN_ONLY_BE_POSITIONED);
01196 }
01197
01203 static CommandCost CheckNewIndustry_Farm(TileIndex tile)
01204 {
01205 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
01206 if (GetTileZ(tile) + TILE_HEIGHT * 2 >= HighestSnowLine()) {
01207 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01208 }
01209 }
01210 return CommandCost();
01211 }
01212
01218 static CommandCost CheckNewIndustry_Plantation(TileIndex tile)
01219 {
01220 if (GetTropicZone(tile) == TROPICZONE_DESERT) {
01221 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01222 }
01223 return CommandCost();
01224 }
01225
01231 static CommandCost CheckNewIndustry_Water(TileIndex tile)
01232 {
01233 if (GetTropicZone(tile) != TROPICZONE_DESERT) {
01234 return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT);
01235 }
01236 return CommandCost();
01237 }
01238
01244 static CommandCost CheckNewIndustry_Lumbermill(TileIndex tile)
01245 {
01246 if (GetTropicZone(tile) != TROPICZONE_RAINFOREST) {
01247 return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST);
01248 }
01249 return CommandCost();
01250 }
01251
01257 static CommandCost CheckNewIndustry_BubbleGen(TileIndex tile)
01258 {
01259 if (GetTileZ(tile) > TILE_HEIGHT * 4) {
01260 return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_LOW_AREAS);
01261 }
01262 return CommandCost();
01263 }
01264
01270 typedef CommandCost CheckNewIndustryProc(TileIndex tile);
01271
01273 static CheckNewIndustryProc * const _check_new_industry_procs[CHECK_END] = {
01274 CheckNewIndustry_NULL,
01275 CheckNewIndustry_Forest,
01276 CheckNewIndustry_OilRefinery,
01277 CheckNewIndustry_Farm,
01278 CheckNewIndustry_Plantation,
01279 CheckNewIndustry_Water,
01280 CheckNewIndustry_Lumbermill,
01281 CheckNewIndustry_BubbleGen,
01282 CheckNewIndustry_OilRig,
01283 };
01284
01295 static CommandCost FindTownForIndustry(TileIndex tile, int type, const Town **t)
01296 {
01297 *t = ClosestTownFromTile(tile, UINT_MAX);
01298
01299 if (_settings_game.economy.multiple_industry_per_town) return CommandCost();
01300
01301 const Industry *i;
01302 FOR_ALL_INDUSTRIES(i) {
01303 if (i->type == (byte)type && i->town == *t) {
01304 *t = NULL;
01305 return_cmd_error(STR_ERROR_ONLY_ONE_ALLOWED_PER_TOWN);
01306 }
01307 }
01308
01309 return CommandCost();
01310 }
01311
01312 bool IsSlopeRefused(Slope current, Slope refused)
01313 {
01314 if (IsSteepSlope(current)) return true;
01315 if (current != SLOPE_FLAT) {
01316 if (IsSteepSlope(refused)) return true;
01317
01318 Slope t = ComplementSlope(current);
01319
01320 if ((refused & SLOPE_W) && (t & SLOPE_NW)) return true;
01321 if ((refused & SLOPE_S) && (t & SLOPE_NE)) return true;
01322 if ((refused & SLOPE_E) && (t & SLOPE_SW)) return true;
01323 if ((refused & SLOPE_N) && (t & SLOPE_SE)) return true;
01324 }
01325
01326 return false;
01327 }
01328
01341 static CommandCost CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTileTable *it, uint itspec_index, int type, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type, bool *custom_shape_check = NULL)
01342 {
01343 bool refused_slope = false;
01344 bool custom_shape = false;
01345
01346 do {
01347 IndustryGfx gfx = GetTranslatedIndustryTileID(it->gfx);
01348 TileIndex cur_tile = TileAddWrap(tile, it->ti.x, it->ti.y);
01349
01350 if (!IsValidTile(cur_tile)) {
01351 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01352 }
01353
01354 if (gfx == GFX_WATERTILE_SPECIALCHECK) {
01355 if (!IsTileType(cur_tile, MP_WATER) ||
01356 GetTileSlope(cur_tile, NULL) != SLOPE_FLAT) {
01357 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01358 }
01359 } else {
01360 CommandCost ret = EnsureNoVehicleOnGround(cur_tile);
01361 if (ret.Failed()) return ret;
01362 if (MayHaveBridgeAbove(cur_tile) && IsBridgeAbove(cur_tile)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01363
01364 const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
01365
01366 IndustryBehaviour ind_behav = GetIndustrySpec(type)->behaviour;
01367
01368
01369 if (!HasBit(its->slopes_refused, 5) && ((HasTileWaterClass(cur_tile) && IsTileOnWater(cur_tile)) == !(ind_behav & INDUSTRYBEH_BUILT_ONWATER))) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01370
01371 if (HasBit(its->callback_mask, CBM_INDT_SHAPE_CHECK)) {
01372 custom_shape = true;
01373 CommandCost ret = PerformIndustryTileSlopeCheck(tile, cur_tile, its, type, gfx, itspec_index, initial_random_bits, founder, creation_type);
01374 if (ret.Failed()) return ret;
01375 } else {
01376 Slope tileh = GetTileSlope(cur_tile, NULL);
01377 refused_slope |= IsSlopeRefused(tileh, its->slopes_refused);
01378 }
01379
01380 if ((ind_behav & (INDUSTRYBEH_ONLY_INTOWN | INDUSTRYBEH_TOWN1200_MORE)) ||
01381 ((ind_behav & INDUSTRYBEH_ONLY_NEARTOWN) && IsTileType(cur_tile, MP_HOUSE))) {
01382 if (!IsTileType(cur_tile, MP_HOUSE)) {
01383 return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS);
01384 }
01385
01386
01387 Backup<CompanyByte> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
01388 CommandCost ret = DoCommand(cur_tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
01389 cur_company.Restore();
01390
01391 if (ret.Failed()) return ret;
01392 } else {
01393
01394 CommandCost ret = DoCommand(cur_tile, 0, 0, DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
01395
01396 if (ret.Failed()) return ret;
01397 }
01398 }
01399 } while ((++it)->ti.x != -0x80);
01400
01401 if (custom_shape_check != NULL) *custom_shape_check = custom_shape;
01402
01403
01404
01405
01406 if (!refused_slope || (_settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !custom_shape && !_ignore_restrictions)) {
01407 return CommandCost();
01408 }
01409 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01410 }
01411
01419 static CommandCost CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t)
01420 {
01421 if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_TOWN1200_MORE) && t->population < 1200) {
01422 return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS_WITH_POPULATION_OF_1200);
01423 }
01424
01425 if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_ONLY_NEARTOWN) && DistanceMax(t->xy, tile) > 9) {
01426 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01427 }
01428
01429 return CommandCost();
01430 }
01431
01432 static bool CheckCanTerraformSurroundingTiles(TileIndex tile, uint height, int internal)
01433 {
01434
01435 if (TileX(tile) == 0 || TileY(tile) == 0 || GetTileType(tile) == MP_VOID) return false;
01436
01437 TileArea ta(tile - TileDiffXY(1, 1), 2, 2);
01438 TILE_AREA_LOOP(tile_walk, ta) {
01439 uint curh = TileHeight(tile_walk);
01440
01441 if ((GetTileType(tile_walk) != MP_CLEAR) && (GetTileType(tile_walk) != MP_TREES)) return false;
01442
01443
01444 if (internal != 0 && Delta(curh, height) > 1) return false;
01445
01446
01447
01448
01449 if (internal == 0 && curh != height) {
01450 if (TileX(tile_walk) == 0 || TileY(tile_walk) == 0 || !CheckCanTerraformSurroundingTiles(tile_walk + TileDiffXY(-1, -1), height, internal + 1)) {
01451 return false;
01452 }
01453 }
01454 }
01455
01456 return true;
01457 }
01458
01463 static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlag flags, const IndustryTileTable *it, int type)
01464 {
01465 const int MKEND = -0x80;
01466 int max_x = 0;
01467 int max_y = 0;
01468
01469
01470 do {
01471 if (it->gfx == 0xFF) continue;
01472 if (it->ti.x > max_x) max_x = it->ti.x;
01473 if (it->ti.y > max_y) max_y = it->ti.y;
01474 } while ((++it)->ti.x != MKEND);
01475
01476
01477 uint h = TileHeight(tile);
01478
01479 if (TileX(tile) <= _settings_game.construction.industry_platform + 1U || TileY(tile) <= _settings_game.construction.industry_platform + 1U) return false;
01480
01481
01482
01483 TileArea ta(tile + TileDiffXY(-_settings_game.construction.industry_platform, -_settings_game.construction.industry_platform),
01484 max_x + 2 + 2 * _settings_game.construction.industry_platform, max_y + 2 + 2 * _settings_game.construction.industry_platform);
01485
01486 if (TileX(ta.tile) + ta.w >= MapMaxX() || TileY(ta.tile) + ta.h >= MapMaxY()) return false;
01487
01488
01489
01490 Backup<CompanyByte> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
01491
01492 TILE_AREA_LOOP(tile_walk, ta) {
01493 uint curh = TileHeight(tile_walk);
01494 if (curh != h) {
01495
01496
01497 if (!CheckCanTerraformSurroundingTiles(tile_walk, h, 0)) {
01498 cur_company.Restore();
01499 return false;
01500 }
01501
01502
01503 if (DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND).Failed()) {
01504 cur_company.Restore();
01505 return false;
01506 }
01507 }
01508 }
01509
01510 if (flags & DC_EXEC) {
01511
01512 TILE_AREA_LOOP(tile_walk, ta) {
01513 uint curh = TileHeight(tile_walk);
01514 while (curh != h) {
01515
01516
01517
01518 DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
01519 curh += (curh > h) ? -1 : 1;
01520 }
01521 }
01522 }
01523
01524 cur_company.Restore();
01525 return true;
01526 }
01527
01528
01535 static CommandCost CheckIfFarEnoughFromConflictingIndustry(TileIndex tile, int type)
01536 {
01537 const IndustrySpec *indspec = GetIndustrySpec(type);
01538 const Industry *i;
01539 FOR_ALL_INDUSTRIES(i) {
01540
01541 if (DistanceMax(tile, i->location.tile) > 14) continue;
01542
01543
01544 if (i->type == indspec->conflicting[0] ||
01545 i->type == indspec->conflicting[1] ||
01546 i->type == indspec->conflicting[2]) {
01547 return_cmd_error(STR_ERROR_INDUSTRY_TOO_CLOSE);
01548 }
01549 }
01550 return CommandCost();
01551 }
01552
01554 void UpdateIndustryAcceptance(Industry *ind)
01555 {
01556 CargoArray accepted;
01557 uint32 always_accepted = 0;
01558
01559
01560 TILE_AREA_LOOP(tile, ind->location) {
01561 if (IsTileType(tile, MP_INDUSTRY) && GetIndustryIndex(tile) == ind->index) {
01562 AddAcceptedCargo_Industry(tile, accepted, &always_accepted);
01563 }
01564 }
01565
01566
01567 uint32 produced = 0;
01568 for (uint i = 0; i < lengthof(ind->produced_cargo); i++) {
01569 if (ind->produced_cargo[i] != CT_INVALID) SetBit(produced, ind->produced_cargo[i]);
01570 }
01571
01572 ind->produced_accepted_mask = always_accepted & produced;
01573 }
01574
01586 static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type, const IndustryTileTable *it, byte layout, const Town *t, Owner founder, uint16 initial_random_bits)
01587 {
01588 const IndustrySpec *indspec = GetIndustrySpec(type);
01589
01590 i->location = TileArea(tile, 1, 1);
01591 i->type = type;
01592 Industry::IncIndustryTypeCount(type);
01593
01594 i->produced_cargo[0] = indspec->produced_cargo[0];
01595 i->produced_cargo[1] = indspec->produced_cargo[1];
01596 i->accepts_cargo[0] = indspec->accepts_cargo[0];
01597 i->accepts_cargo[1] = indspec->accepts_cargo[1];
01598 i->accepts_cargo[2] = indspec->accepts_cargo[2];
01599 i->production_rate[0] = indspec->production_rate[0];
01600 i->production_rate[1] = indspec->production_rate[1];
01601
01602
01603 if (indspec->UsesSmoothEconomy()) {
01604 i->production_rate[0] = min((RandomRange(256) + 128) * i->production_rate[0] >> 8, 255);
01605 i->production_rate[1] = min((RandomRange(256) + 128) * i->production_rate[1] >> 8, 255);
01606 }
01607
01608 i->town = t;
01609 i->owner = OWNER_NONE;
01610
01611 uint16 r = Random();
01612 i->random_colour = GB(r, 0, 4);
01613 i->counter = GB(r, 4, 12);
01614 i->random = initial_random_bits;
01615 i->produced_cargo_waiting[0] = 0;
01616 i->produced_cargo_waiting[1] = 0;
01617 i->incoming_cargo_waiting[0] = 0;
01618 i->incoming_cargo_waiting[1] = 0;
01619 i->incoming_cargo_waiting[2] = 0;
01620 i->this_month_production[0] = 0;
01621 i->this_month_production[1] = 0;
01622 i->this_month_transported[0] = 0;
01623 i->this_month_transported[1] = 0;
01624 i->last_month_pct_transported[0] = 0;
01625 i->last_month_pct_transported[1] = 0;
01626 i->last_month_transported[0] = 0;
01627 i->last_month_transported[1] = 0;
01628 i->was_cargo_delivered = false;
01629 i->last_prod_year = _cur_year;
01630 i->last_month_production[0] = i->production_rate[0] * 8;
01631 i->last_month_production[1] = i->production_rate[1] * 8;
01632 i->founder = founder;
01633
01634 i->construction_date = _date;
01635 i->construction_type = (_game_mode == GM_EDITOR) ? ICT_SCENARIO_EDITOR :
01636 (_generating_world ? ICT_MAP_GENERATION : ICT_NORMAL_GAMEPLAY);
01637
01638
01639
01640
01641 i->selected_layout = layout + 1;
01642
01643 if (!_generating_world) i->last_month_production[0] = i->last_month_production[1] = 0;
01644
01645 i->prod_level = PRODLEVEL_DEFAULT;
01646
01647
01648
01649 if (HasBit(indspec->callback_mask, CBM_IND_DECIDE_COLOUR)) {
01650 uint16 res = GetIndustryCallback(CBID_INDUSTRY_DECIDE_COLOUR, 0, 0, i, type, INVALID_TILE);
01651 if (res != CALLBACK_FAILED) i->random_colour = GB(res, 0, 4);
01652 }
01653
01654 if (HasBit(indspec->callback_mask, CBM_IND_INPUT_CARGO_TYPES)) {
01655 for (uint j = 0; j < lengthof(i->accepts_cargo); j++) i->accepts_cargo[j] = CT_INVALID;
01656 for (uint j = 0; j < lengthof(i->accepts_cargo); j++) {
01657 uint16 res = GetIndustryCallback(CBID_INDUSTRY_INPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
01658 if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
01659 i->accepts_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
01660 }
01661 }
01662
01663 if (HasBit(indspec->callback_mask, CBM_IND_OUTPUT_CARGO_TYPES)) {
01664 for (uint j = 0; j < lengthof(i->produced_cargo); j++) i->produced_cargo[j] = CT_INVALID;
01665 for (uint j = 0; j < lengthof(i->produced_cargo); j++) {
01666 uint16 res = GetIndustryCallback(CBID_INDUSTRY_OUTPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
01667 if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
01668 i->produced_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
01669 }
01670 }
01671
01672
01673
01674 do {
01675 TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
01676
01677 if (it->gfx != GFX_WATERTILE_SPECIALCHECK) {
01678 i->location.Add(cur_tile);
01679
01680 WaterClass wc = (IsWaterTile(cur_tile) ? GetWaterClass(cur_tile) : WATER_CLASS_INVALID);
01681
01682 DoCommand(cur_tile, 0, 0, DC_EXEC | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
01683
01684 MakeIndustry(cur_tile, i->index, it->gfx, Random(), wc);
01685
01686 if (_generating_world) {
01687 SetIndustryConstructionCounter(cur_tile, 3);
01688 SetIndustryConstructionStage(cur_tile, 2);
01689 }
01690
01691
01692 IndustryGfx cur_gfx = GetTranslatedIndustryTileID(it->gfx);
01693 const IndustryTileSpec *its = GetIndustryTileSpec(cur_gfx);
01694 if (its->animation.status != ANIM_STATUS_NO_ANIMATION) AddAnimatedTile(cur_tile);
01695 }
01696 } while ((++it)->ti.x != -0x80);
01697
01698 if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
01699 for (uint j = 0; j != 50; j++) PlantRandomFarmField(i);
01700 }
01701 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
01702
01703 UpdateIndustryAcceptance(i);
01704 Station::RecomputeIndustriesNearForAll();
01705 }
01706
01723 static CommandCost CreateNewIndustryHelper(TileIndex tile, IndustryType type, DoCommandFlag flags, const IndustrySpec *indspec, uint itspec_index, uint32 random_var8f, uint16 random_initial_bits, Owner founder, IndustryAvailabilityCallType creation_type, Industry **ip)
01724 {
01725 assert(itspec_index < indspec->num_table);
01726 const IndustryTileTable *it = indspec->table[itspec_index];
01727 bool custom_shape_check = false;
01728
01729 *ip = NULL;
01730
01731 SmallVector<ClearedObjectArea, 1> object_areas(_cleared_object_areas);
01732 CommandCost ret = CheckIfIndustryTilesAreFree(tile, it, itspec_index, type, random_initial_bits, founder, creation_type, &custom_shape_check);
01733 _cleared_object_areas = object_areas;
01734 if (ret.Failed()) return ret;
01735
01736 if (HasBit(GetIndustrySpec(type)->callback_mask, CBM_IND_LOCATION)) {
01737 ret = CheckIfCallBackAllowsCreation(tile, type, itspec_index, random_var8f, random_initial_bits, founder, creation_type);
01738 } else {
01739 ret = _check_new_industry_procs[indspec->check_proc](tile);
01740 }
01741 if (ret.Failed()) return ret;
01742
01743 if (!custom_shape_check && _settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world &&
01744 !_ignore_restrictions && !CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER, it, type)) {
01745 return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01746 }
01747
01748 ret = CheckIfFarEnoughFromConflictingIndustry(tile, type);
01749 if (ret.Failed()) return ret;
01750
01751 const Town *t = NULL;
01752 ret = FindTownForIndustry(tile, type, &t);
01753 if (ret.Failed()) return ret;
01754 assert(t != NULL);
01755
01756 ret = CheckIfIndustryIsAllowed(tile, type, t);
01757 if (ret.Failed()) return ret;
01758
01759 if (!Industry::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_INDUSTRIES);
01760
01761 if (flags & DC_EXEC) {
01762 *ip = new Industry(tile);
01763 if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER | DC_EXEC, it, type);
01764 DoCreateNewIndustry(*ip, tile, type, it, itspec_index, t, founder, random_initial_bits);
01765 }
01766
01767 return CommandCost();
01768 }
01769
01781 CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01782 {
01783 IndustryType it = GB(p1, 0, 8);
01784 if (it >= NUM_INDUSTRYTYPES) return CMD_ERROR;
01785
01786 const IndustrySpec *indspec = GetIndustrySpec(it);
01787
01788
01789 if (!indspec->enabled || indspec->num_table == 0) return CMD_ERROR;
01790
01791
01792
01793 if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
01794 return CMD_ERROR;
01795 }
01796
01797 if (_game_mode != GM_EDITOR && !CheckIfCallBackAllowsAvailability(it, IACT_USERCREATION)) {
01798 return CMD_ERROR;
01799 }
01800
01801 Randomizer randomizer;
01802 randomizer.SetSeed(p2);
01803 uint16 random_initial_bits = GB(p2, 0, 16);
01804 uint32 random_var8f = randomizer.Next();
01805 int num_layouts = indspec->num_table;
01806 CommandCost ret = CommandCost(STR_ERROR_SITE_UNSUITABLE);
01807
01808 Industry *ind = NULL;
01809 if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indspec->IsRawIndustry()) {
01810 if (flags & DC_EXEC) {
01811
01812 Backup<CompanyByte> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
01813
01814
01815
01816 if (Random() <= indspec->prospecting_chance) {
01817 for (int i = 0; i < 5000; i++) {
01818
01819
01820
01821 tile = RandomTile();
01822
01823 int layout = RandomRange(num_layouts);
01824
01825 for (int j = 0; j < num_layouts; j++) {
01826 layout = (layout + 1) % num_layouts;
01827 ret = CreateNewIndustryHelper(tile, it, flags, indspec, layout, random_var8f, random_initial_bits, cur_company.GetOriginalValue(), IACT_PROSPECTCREATION, &ind);
01828 if (ret.Succeeded()) break;
01829 }
01830 if (ret.Succeeded()) break;
01831 }
01832 }
01833 cur_company.Restore();
01834 }
01835 } else {
01836 int layout = GB(p1, 8, 8);
01837 if (layout >= num_layouts) return CMD_ERROR;
01838
01839
01840 for (int i = 0; i < num_layouts; i++) {
01841 layout = (layout + 1) % num_layouts;
01842 ret = CreateNewIndustryHelper(tile, it, flags, indspec, layout, random_var8f, random_initial_bits, _current_company, IACT_USERCREATION, &ind);
01843 if (ret.Succeeded()) break;
01844 }
01845
01846
01847 if (ret.Failed()) return ret;
01848 }
01849
01850 if ((flags & DC_EXEC) && ind != NULL && _game_mode != GM_EDITOR) {
01851
01852 SetDParam(0, indspec->name);
01853 if (indspec->new_industry_text > STR_LAST_STRINGID) {
01854 SetDParam(1, STR_TOWN_NAME);
01855 SetDParam(2, ind->town->index);
01856 } else {
01857 SetDParam(1, ind->town->index);
01858 }
01859 AddIndustryNewsItem(indspec->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
01860 AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
01861 }
01862
01863 return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost());
01864 }
01865
01866
01874 static Industry *CreateNewIndustry(TileIndex tile, IndustryType type, IndustryAvailabilityCallType creation_type)
01875 {
01876 const IndustrySpec *indspec = GetIndustrySpec(type);
01877
01878 uint32 seed = Random();
01879 uint32 seed2 = Random();
01880 Industry *i = NULL;
01881 CommandCost ret = CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, RandomRange(indspec->num_table), seed, GB(seed2, 0, 16), OWNER_NONE, creation_type, &i);
01882 assert(i != NULL || ret.Failed());
01883 return i;
01884 }
01885
01892 static uint32 GetScaledIndustryGenerationProbability(IndustryType it, bool *force_at_least_one)
01893 {
01894 const IndustrySpec *ind_spc = GetIndustrySpec(it);
01895 uint32 chance = ind_spc->appear_creation[_settings_game.game_creation.landscape] * 16;
01896 if (!ind_spc->enabled || chance == 0 || ind_spc->num_table == 0 ||
01897 !CheckIfCallBackAllowsAvailability(it, IACT_MAPGENERATION) ||
01898 (_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY)) {
01899 *force_at_least_one = false;
01900 return 0;
01901 } else {
01902
01903
01904 chance = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(chance) : ScaleByMapSize(chance);
01905
01906 *force_at_least_one = (chance > 0) && !(ind_spc->behaviour & INDUSTRYBEH_NOBUILT_MAPCREATION) && (_game_mode != GM_EDITOR);
01907 return chance;
01908 }
01909 }
01910
01917 static uint16 GetIndustryGamePlayProbability(IndustryType it, byte *min_number)
01918 {
01919 if (_settings_game.difficulty.industry_density == ID_FUND_ONLY) {
01920 *min_number = 0;
01921 return 0;
01922 }
01923
01924 const IndustrySpec *ind_spc = GetIndustrySpec(it);
01925 byte chance = ind_spc->appear_ingame[_settings_game.game_creation.landscape];
01926 if (!ind_spc->enabled || chance == 0 || ind_spc->num_table == 0 ||
01927 ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) ||
01928 ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) ||
01929 !CheckIfCallBackAllowsAvailability(it, IACT_RANDOMCREATION)) {
01930 *min_number = 0;
01931 return 0;
01932 }
01933 *min_number = (ind_spc->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) ? 1 : 0;
01934 return chance;
01935 }
01936
01941 static uint GetNumberOfIndustries()
01942 {
01943
01944 static const uint16 numof_industry_table[] = {
01945 0,
01946 0,
01947 10,
01948 25,
01949 55,
01950 80,
01951 };
01952
01953 assert(lengthof(numof_industry_table) == ID_END);
01954 uint difficulty = (_game_mode != GM_EDITOR) ? _settings_game.difficulty.industry_density : (uint)ID_VERY_LOW;
01955 return ScaleByMapSize(numof_industry_table[difficulty]);
01956 }
01957
01962 static void AdvertiseIndustryOpening(const Industry *ind)
01963 {
01964 const IndustrySpec *ind_spc = GetIndustrySpec(ind->type);
01965 SetDParam(0, ind_spc->name);
01966 if (ind_spc->new_industry_text > STR_LAST_STRINGID) {
01967 SetDParam(1, STR_TOWN_NAME);
01968 SetDParam(2, ind->town->index);
01969 } else {
01970 SetDParam(1, ind->town->index);
01971 }
01972 AddIndustryNewsItem(ind_spc->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
01973 AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
01974 }
01975
01984 static Industry *PlaceIndustry(IndustryType type, IndustryAvailabilityCallType creation_type, bool try_hard)
01985 {
01986 uint tries = try_hard ? 10000u : 2000u;
01987 for (; tries > 0; tries--) {
01988 Industry *ind = CreateNewIndustry(RandomTile(), type, creation_type);
01989 if (ind != NULL) return ind;
01990 }
01991 return NULL;
01992 }
01993
01999 static void PlaceInitialIndustry(IndustryType type, bool try_hard)
02000 {
02001 Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
02002
02003 IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
02004 PlaceIndustry(type, IACT_MAPGENERATION, try_hard);
02005
02006 cur_company.Restore();
02007 }
02008
02013 static uint GetCurrentTotalNumberOfIndustries()
02014 {
02015 int total = 0;
02016 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) total += Industry::GetIndustryTypeCount(it);
02017 return total;
02018 }
02019
02020
02022 void IndustryTypeBuildData::Reset()
02023 {
02024 this->probability = 0;
02025 this->min_number = 0;
02026 this->target_count = 0;
02027 this->max_wait = 1;
02028 this->wait_count = 0;
02029 }
02030
02032 void IndustryBuildData::Reset()
02033 {
02034 this->wanted_inds = GetCurrentTotalNumberOfIndustries() << 16;
02035
02036 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02037 this->builddata[it].Reset();
02038 }
02039 }
02040
02042 void IndustryBuildData::MonthlyLoop()
02043 {
02044 static const int NEWINDS_PER_MONTH = 0x38000 / (10 * 12);
02045 if (_settings_game.difficulty.industry_density == ID_FUND_ONLY) return;
02046
02047
02048
02049 uint max_behind = 1 + min(99u, ScaleByMapSize(3));
02050 if (GetCurrentTotalNumberOfIndustries() + max_behind >= (this->wanted_inds >> 16)) {
02051 this->wanted_inds += ScaleByMapSize(NEWINDS_PER_MONTH);
02052 }
02053 }
02054
02059 void GenerateIndustries()
02060 {
02061 if (_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY) return;
02062
02063 uint32 industry_probs[NUM_INDUSTRYTYPES];
02064 bool force_at_least_one[NUM_INDUSTRYTYPES];
02065 uint32 total_prob = 0;
02066 uint num_forced = 0;
02067
02068 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02069 industry_probs[it] = GetScaledIndustryGenerationProbability(it, force_at_least_one + it);
02070 total_prob += industry_probs[it];
02071 if (force_at_least_one[it]) num_forced++;
02072 }
02073
02074 uint total_amount = GetNumberOfIndustries();
02075 if (total_prob == 0 || total_amount < num_forced) {
02076
02077 total_amount = num_forced;
02078 }
02079
02080 SetGeneratingWorldProgress(GWP_INDUSTRY, total_amount);
02081
02082
02083 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02084 if (force_at_least_one[it]) {
02085 assert(total_amount > 0);
02086 total_amount--;
02087 PlaceInitialIndustry(it, true);
02088 }
02089 }
02090
02091
02092 for (uint i = 0; i < total_amount; i++) {
02093 uint32 r = RandomRange(total_prob);
02094 IndustryType it = 0;
02095 while (r >= industry_probs[it]) {
02096 r -= industry_probs[it];
02097 it++;
02098 assert(it < NUM_INDUSTRYTYPES);
02099 }
02100 assert(industry_probs[it] > 0);
02101 PlaceInitialIndustry(it, false);
02102 }
02103 _industry_builder.Reset();
02104 }
02105
02110 static void UpdateIndustryStatistics(Industry *i)
02111 {
02112 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
02113 if (i->produced_cargo[j] != CT_INVALID) {
02114 byte pct = 0;
02115 if (i->this_month_production[j] != 0) {
02116 i->last_prod_year = _cur_year;
02117 pct = min(i->this_month_transported[j] * 256 / i->this_month_production[j], 255);
02118 }
02119 i->last_month_pct_transported[j] = pct;
02120
02121 i->last_month_production[j] = i->this_month_production[j];
02122 i->this_month_production[j] = 0;
02123
02124 i->last_month_transported[j] = i->this_month_transported[j];
02125 i->this_month_transported[j] = 0;
02126
02127
02128 i->average_production[j] = (i->average_production[j] * 7 + i->last_month_production[j]) / 8;
02129 }
02130 }
02131 }
02132
02137 void Industry::RecomputeProductionMultipliers()
02138 {
02139 const IndustrySpec *indspec = GetIndustrySpec(this->type);
02140 assert(!indspec->UsesSmoothEconomy());
02141
02142
02143 this->production_rate[0] = min(CeilDiv(indspec->production_rate[0] * this->prod_level, PRODLEVEL_DEFAULT), 0xFF);
02144 this->production_rate[1] = min(CeilDiv(indspec->production_rate[1] * this->prod_level, PRODLEVEL_DEFAULT), 0xFF);
02145 }
02146
02147
02153 bool IndustryTypeBuildData::GetIndustryTypeData(IndustryType it)
02154 {
02155 byte min_number;
02156 uint32 probability = GetIndustryGamePlayProbability(it, &min_number);
02157 bool changed = min_number != this->min_number || probability != this->probability;
02158 this->min_number = min_number;
02159 this->probability = probability;
02160 return changed;
02161 }
02162
02164 void IndustryBuildData::SetupTargetCount()
02165 {
02166 bool changed = false;
02167 uint num_planned = 0;
02168 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02169 changed |= this->builddata[it].GetIndustryTypeData(it);
02170 num_planned += this->builddata[it].target_count;
02171 }
02172 uint total_amount = this->wanted_inds >> 16;
02173 changed |= num_planned != total_amount;
02174 if (!changed) return;
02175
02176
02177 uint force_build = 0;
02178 uint32 total_prob = 0;
02179 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02180 IndustryTypeBuildData *ibd = this->builddata + it;
02181 force_build += ibd->min_number;
02182 ibd->target_count = ibd->min_number;
02183 total_prob += ibd->probability;
02184 }
02185
02186 if (total_prob == 0) return;
02187
02188
02189 total_amount = (total_amount <= force_build) ? 0 : total_amount - force_build;
02190
02191
02192 while (total_amount > 0) {
02193 uint32 r = RandomRange(total_prob);
02194 IndustryType it = 0;
02195 while (r >= this->builddata[it].probability) {
02196 r -= this->builddata[it].probability;
02197 it++;
02198 assert(it < NUM_INDUSTRYTYPES);
02199 }
02200 assert(this->builddata[it].probability > 0);
02201 this->builddata[it].target_count++;
02202 total_amount--;
02203 }
02204 }
02205
02209 void IndustryBuildData::TryBuildNewIndustry()
02210 {
02211 this->SetupTargetCount();
02212
02213 int missing = 0;
02214 uint count = 0;
02215 uint32 total_prob = 0;
02216 IndustryType forced_build = NUM_INDUSTRYTYPES;
02217 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02218 int difference = this->builddata[it].target_count - Industry::GetIndustryTypeCount(it);
02219 missing += difference;
02220 if (this->builddata[it].wait_count > 0) continue;
02221 if (difference > 0) {
02222 if (Industry::GetIndustryTypeCount(it) == 0 && this->builddata[it].min_number > 0) {
02223
02224 if (forced_build == NUM_INDUSTRYTYPES ||
02225 difference > this->builddata[forced_build].target_count - Industry::GetIndustryTypeCount(forced_build)) {
02226 forced_build = it;
02227 }
02228 }
02229 total_prob += difference;
02230 count++;
02231 }
02232 }
02233
02234 if (EconomyIsInRecession() || (forced_build == NUM_INDUSTRYTYPES && (missing <= 0 || total_prob == 0))) count = 0;
02235
02236 if (count >= 1) {
02237
02238
02239 IndustryType it;
02240 if (forced_build != NUM_INDUSTRYTYPES) {
02241 it = forced_build;
02242 } else {
02243
02244 uint32 r = 0;
02245 if (count > 1) r = RandomRange(total_prob);
02246 for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
02247 if (this->builddata[it].wait_count > 0) continue;
02248 int difference = this->builddata[it].target_count - Industry::GetIndustryTypeCount(it);
02249 if (difference <= 0) continue;
02250 if (count == 1) break;
02251 if (r < (uint)difference) break;
02252 r -= difference;
02253 }
02254 assert(it < NUM_INDUSTRYTYPES && this->builddata[it].target_count > Industry::GetIndustryTypeCount(it));
02255 }
02256
02257
02258 const Industry *ind = PlaceIndustry(it, IACT_RANDOMCREATION, false);
02259 if (ind == NULL) {
02260 this->builddata[it].wait_count = this->builddata[it].max_wait + 1;
02261 this->builddata[it].max_wait = min(1000, this->builddata[it].max_wait + 2);
02262 } else {
02263 AdvertiseIndustryOpening(ind);
02264 this->builddata[it].max_wait = max(this->builddata[it].max_wait / 2, 1);
02265 }
02266 }
02267
02268
02269 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02270 if (this->builddata[it].wait_count > 0) this->builddata[it].wait_count--;
02271 }
02272 }
02273
02282 static bool CheckIndustryCloseDownProtection(IndustryType type)
02283 {
02284 const IndustrySpec *indspec = GetIndustrySpec(type);
02285
02286
02287 if ((indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE) return false;
02288 return (indspec->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) == 0 && Industry::GetIndustryTypeCount(type) <= 1;
02289 }
02290
02300 static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces)
02301 {
02302 if (cargo == CT_INVALID) return;
02303
02304
02305 for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) {
02306 if (cargo == ind->accepts_cargo[j] && !IndustryTemporarilyRefusesCargo(ind, cargo)) {
02307 *c_accepts = true;
02308 break;
02309 }
02310 }
02311
02312
02313 for (byte j = 0; j < lengthof(ind->produced_cargo); j++) {
02314 if (cargo == ind->produced_cargo[j]) {
02315 *c_produces = true;
02316 break;
02317 }
02318 }
02319 }
02320
02334 static int WhoCanServiceIndustry(Industry *ind)
02335 {
02336
02337 StationList stations;
02338 FindStationsAroundTiles(ind->location, &stations);
02339
02340 if (stations.Length() == 0) return 0;
02341
02342 const Vehicle *v;
02343 int result = 0;
02344 FOR_ALL_VEHICLES(v) {
02345
02346 if (v->owner != _local_company && result != 0) continue;
02347
02348
02349 bool c_accepts = false;
02350 bool c_produces = false;
02351 if (v->type == VEH_TRAIN && v->IsFrontEngine()) {
02352 for (const Vehicle *u = v; u != NULL; u = u->Next()) {
02353 CanCargoServiceIndustry(u->cargo_type, ind, &c_accepts, &c_produces);
02354 }
02355 } else if (v->type == VEH_ROAD || v->type == VEH_SHIP || v->type == VEH_AIRCRAFT) {
02356 CanCargoServiceIndustry(v->cargo_type, ind, &c_accepts, &c_produces);
02357 } else {
02358 continue;
02359 }
02360 if (!c_accepts && !c_produces) continue;
02361
02362
02363
02364
02365
02366 const Order *o;
02367 FOR_VEHICLE_ORDERS(v, o) {
02368 if (o->IsType(OT_GOTO_STATION) && !(o->GetUnloadType() & OUFB_TRANSFER)) {
02369
02370 Station *st = Station::Get(o->GetDestination());
02371 assert(st != NULL);
02372
02373
02374 if ((o->GetUnloadType() & OUFB_UNLOAD) && !c_accepts) break;
02375
02376 if (stations.Contains(st)) {
02377 if (v->owner == _local_company) return 2;
02378 result = 1;
02379 }
02380 }
02381 }
02382 }
02383 return result;
02384 }
02385
02393 static void ReportNewsProductionChangeIndustry(Industry *ind, CargoID type, int percent)
02394 {
02395 NewsSubtype ns;
02396
02397 switch (WhoCanServiceIndustry(ind)) {
02398 case 0: ns = NS_INDUSTRY_NOBODY; break;
02399 case 1: ns = NS_INDUSTRY_OTHER; break;
02400 case 2: ns = NS_INDUSTRY_COMPANY; break;
02401 default: NOT_REACHED();
02402 }
02403 SetDParam(2, abs(percent));
02404 SetDParam(0, CargoSpec::Get(type)->name);
02405 SetDParam(1, ind->index);
02406 AddIndustryNewsItem(
02407 percent >= 0 ? STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_SMOOTH : STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_SMOOTH,
02408 ns,
02409 ind->index
02410 );
02411 }
02412
02413 static const uint PERCENT_TRANSPORTED_60 = 153;
02414 static const uint PERCENT_TRANSPORTED_80 = 204;
02415
02421 static void ChangeIndustryProduction(Industry *i, bool monthly)
02422 {
02423 StringID str = STR_NULL;
02424 bool closeit = false;
02425 const IndustrySpec *indspec = GetIndustrySpec(i->type);
02426 bool standard = false;
02427 bool suppress_message = false;
02428 bool recalculate_multipliers = false;
02429
02430 bool smooth_economy = indspec->UsesSmoothEconomy();
02431 byte div = 0;
02432 byte mul = 0;
02433 int8 increment = 0;
02434
02435 bool callback_enabled = HasBit(indspec->callback_mask, monthly ? CBM_IND_MONTHLYPROD_CHANGE : CBM_IND_PRODUCTION_CHANGE);
02436 if (callback_enabled) {
02437 uint16 res = GetIndustryCallback(monthly ? CBID_INDUSTRY_MONTHLYPROD_CHANGE : CBID_INDUSTRY_PRODUCTION_CHANGE, 0, Random(), i, i->type, i->location.tile);
02438 if (res != CALLBACK_FAILED) {
02439 suppress_message = HasBit(res, 7);
02440
02441 if (HasBit(res, 8)) str = MapGRFStringID(indspec->grf_prop.grffile->grfid, GB(GetRegister(0x100), 0, 16));
02442 res = GB(res, 0, 4);
02443 switch (res) {
02444 default: NOT_REACHED();
02445 case 0x0: break;
02446 case 0x1: div = 1; break;
02447 case 0x2: mul = 1; break;
02448 case 0x3: closeit = true; break;
02449 case 0x4: standard = true; break;
02450 case 0x5: case 0x6: case 0x7:
02451 case 0x8: div = res - 0x3; break;
02452 case 0x9: case 0xA: case 0xB:
02453 case 0xC: mul = res - 0x7; break;
02454 case 0xD:
02455 case 0xE:
02456 increment = res == 0x0D ? -1 : 1;
02457 break;
02458 case 0xF:
02459 i->prod_level = Clamp(GB(GetRegister(0x100), 16, 8), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
02460 recalculate_multipliers = true;
02461 break;
02462 }
02463 }
02464 } else {
02465 if (monthly != smooth_economy) return;
02466 if (indspec->life_type == INDUSTRYLIFE_BLACK_HOLE) return;
02467 }
02468
02469 if (standard || (!callback_enabled && (indspec->life_type & (INDUSTRYLIFE_ORGANIC | INDUSTRYLIFE_EXTRACTIVE)) != 0)) {
02470
02471 bool only_decrease = (indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE;
02472
02473 if (smooth_economy) {
02474 closeit = true;
02475 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
02476 if (i->produced_cargo[j] == CT_INVALID) continue;
02477 uint32 r = Random();
02478 int old_prod, new_prod, percent;
02479
02480 int mult = (i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_60) ? 1 : -1;
02481
02482 new_prod = old_prod = i->production_rate[j];
02483
02484
02485
02486 if (only_decrease) {
02487 mult = -1;
02488
02489
02490 } else if (Chance16I(1, ((i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_80) ? 6 : 3), r)) {
02491 mult *= -1;
02492 }
02493
02494
02495
02496 if (Chance16I(1, 22, r >> 16)) {
02497 new_prod += mult * (max(((RandomRange(50) + 10) * old_prod) >> 8, 1U));
02498 }
02499
02500
02501 new_prod = Clamp(new_prod, 1, 255);
02502
02503 if (((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0) && j == 1) {
02504 new_prod = Clamp(new_prod, 0, 16);
02505 }
02506
02507
02508 if (new_prod == old_prod && old_prod > 1) {
02509 closeit = false;
02510 continue;
02511 }
02512
02513 percent = (old_prod == 0) ? 100 : (new_prod * 100 / old_prod - 100);
02514 i->production_rate[j] = new_prod;
02515
02516
02517 if (new_prod > 1) closeit = false;
02518
02519 if (abs(percent) >= 10) {
02520 ReportNewsProductionChangeIndustry(i, i->produced_cargo[j], percent);
02521 }
02522 }
02523 } else {
02524 if (only_decrease || Chance16(1, 3)) {
02525
02526 if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != Chance16(1, 3)) {
02527 mul = 1;
02528 } else {
02529 div = 1;
02530 }
02531 }
02532 }
02533 }
02534
02535 if (!callback_enabled && (indspec->life_type & INDUSTRYLIFE_PROCESSING)) {
02536 if ( (byte)(_cur_year - i->last_prod_year) >= 5 && Chance16(1, smooth_economy ? 180 : 2)) {
02537 closeit = true;
02538 }
02539 }
02540
02541
02542 while (mul-- != 0 && i->prod_level < PRODLEVEL_MAXIMUM) {
02543 i->prod_level = min(i->prod_level * 2, PRODLEVEL_MAXIMUM);
02544 recalculate_multipliers = true;
02545 if (str == STR_NULL) str = indspec->production_up_text;
02546 }
02547
02548
02549 while (div-- != 0 && !closeit) {
02550 if (i->prod_level == PRODLEVEL_MINIMUM) {
02551 closeit = true;
02552 } else {
02553 i->prod_level = max(i->prod_level / 2, (int)PRODLEVEL_MINIMUM);
02554 recalculate_multipliers = true;
02555 if (str == STR_NULL) str = indspec->production_down_text;
02556 }
02557 }
02558
02559
02560 if (increment != 0) {
02561 if (increment < 0 && i->prod_level == PRODLEVEL_MINIMUM) {
02562 closeit = true;
02563 } else {
02564 i->prod_level = ClampU(i->prod_level + increment, PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
02565 recalculate_multipliers = true;
02566 }
02567 }
02568
02569
02570
02571 if (recalculate_multipliers) i->RecomputeProductionMultipliers();
02572
02573
02574 if (closeit && !CheckIndustryCloseDownProtection(i->type)) {
02575 i->prod_level = PRODLEVEL_CLOSURE;
02576 str = indspec->closure_text;
02577 }
02578
02579 if (!suppress_message && str != STR_NULL) {
02580 NewsSubtype ns;
02581
02582 if (closeit) {
02583 ns = NS_INDUSTRY_CLOSE;
02584 AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
02585 } else {
02586 switch (WhoCanServiceIndustry(i)) {
02587 case 0: ns = NS_INDUSTRY_NOBODY; break;
02588 case 1: ns = NS_INDUSTRY_OTHER; break;
02589 case 2: ns = NS_INDUSTRY_COMPANY; break;
02590 default: NOT_REACHED();
02591 }
02592 }
02593
02594 if (str > STR_LAST_STRINGID) {
02595 SetDParam(0, STR_TOWN_NAME);
02596 SetDParam(1, i->town->index);
02597 SetDParam(2, indspec->name);
02598 } else if (closeit) {
02599 SetDParam(0, STR_FORMAT_INDUSTRY_NAME);
02600 SetDParam(1, i->town->index);
02601 SetDParam(2, indspec->name);
02602 } else {
02603 SetDParam(0, i->index);
02604 }
02605
02606 AddNewsItem(str,
02607 ns,
02608 closeit ? NR_TILE : NR_INDUSTRY,
02609 closeit ? i->location.tile + TileDiffXY(1, 1) : i->index);
02610 }
02611 }
02612
02620 void IndustryDailyLoop()
02621 {
02622 _economy.industry_daily_change_counter += _economy.industry_daily_increment;
02623
02624
02625
02626
02627 uint16 change_loop = _economy.industry_daily_change_counter >> 16;
02628
02629
02630 _economy.industry_daily_change_counter &= 0xFFFF;
02631
02632 if (change_loop == 0) {
02633 return;
02634 }
02635
02636 Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
02637
02638
02639
02640 uint perc = 3;
02641 if ((_industry_builder.wanted_inds >> 16) > GetCurrentTotalNumberOfIndustries()) {
02642 perc = min(9u, perc + (_industry_builder.wanted_inds >> 16) - GetCurrentTotalNumberOfIndustries());
02643 }
02644 for (uint16 j = 0; j < change_loop; j++) {
02645 if (Chance16(perc, 100)) {
02646 _industry_builder.TryBuildNewIndustry();
02647 } else {
02648 Industry *i = Industry::GetRandom();
02649 if (i != NULL) {
02650 ChangeIndustryProduction(i, false);
02651 SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
02652 }
02653 }
02654 }
02655
02656 cur_company.Restore();
02657
02658
02659 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
02660 }
02661
02662 void IndustryMonthlyLoop()
02663 {
02664 Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
02665
02666 _industry_builder.MonthlyLoop();
02667
02668 Industry *i;
02669 FOR_ALL_INDUSTRIES(i) {
02670 UpdateIndustryStatistics(i);
02671 UpdateIndustryAcceptance(i);
02672 if (i->prod_level == PRODLEVEL_CLOSURE) {
02673 delete i;
02674 } else {
02675 ChangeIndustryProduction(i, true);
02676 SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
02677 }
02678 }
02679
02680 cur_company.Restore();
02681
02682
02683 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
02684 }
02685
02686
02687 void InitializeIndustries()
02688 {
02689 Industry::ResetIndustryCounts();
02690 _industry_sound_tile = 0;
02691
02692 _industry_builder.Reset();
02693 }
02694
02699 bool IndustrySpec::IsRawIndustry() const
02700 {
02701
02702 return (this->life_type & (INDUSTRYLIFE_EXTRACTIVE | INDUSTRYLIFE_ORGANIC)) != 0 &&
02703 (this->behaviour & INDUSTRYBEH_CUT_TREES) == 0;
02704 }
02705
02710 Money IndustrySpec::GetConstructionCost() const
02711 {
02712
02713 return (_price[(_settings_game.construction.raw_industry_construction == 1 && this->IsRawIndustry()) ?
02714 PR_BUILD_INDUSTRY_RAW : PR_BUILD_INDUSTRY] * this->cost_multiplier) >> 8;
02715 }
02716
02723 Money IndustrySpec::GetRemovalCost() const
02724 {
02725 return (_price[PR_CLEAR_INDUSTRY] * this->removal_cost_multiplier) >> 8;
02726 }
02727
02732 bool IndustrySpec::UsesSmoothEconomy() const
02733 {
02734 return _settings_game.economy.smooth_economy &&
02735 !(HasBit(this->callback_mask, CBM_IND_PRODUCTION_256_TICKS) || HasBit(this->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) &&
02736 !(HasBit(this->callback_mask, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(this->callback_mask, CBM_IND_PRODUCTION_CHANGE));
02737 }
02738
02739 static CommandCost TerraformTile_Industry(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
02740 {
02741 if (AutoslopeEnabled()) {
02742
02743
02744
02745
02746
02747
02748 Slope tileh_old = GetTileSlope(tile, NULL);
02749
02750 if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
02751 const IndustryGfx gfx = GetIndustryGfx(tile);
02752 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
02753
02754
02755 if (HasBit(itspec->callback_mask, CBM_INDT_AUTOSLOPE)) {
02756
02757 uint16 res = GetIndustryTileCallback(CBID_INDTILE_AUTOSLOPE, 0, 0, gfx, Industry::GetByTile(tile), tile);
02758 if ((res == 0) || (res == CALLBACK_FAILED)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
02759 } else {
02760
02761 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
02762 }
02763 }
02764 }
02765 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
02766 }
02767
02768 extern const TileTypeProcs _tile_type_industry_procs = {
02769 DrawTile_Industry,
02770 GetSlopeZ_Industry,
02771 ClearTile_Industry,
02772 AddAcceptedCargo_Industry,
02773 GetTileDesc_Industry,
02774 GetTileTrackStatus_Industry,
02775 ClickTile_Industry,
02776 AnimateTile_Industry,
02777 TileLoop_Industry,
02778 ChangeTileOwner_Industry,
02779 NULL,
02780 NULL,
02781 GetFoundation_Industry,
02782 TerraformTile_Industry,
02783 };