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