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