00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "stdafx.h"
00013 #include "openttd.h"
00014 #include "clear_map.h"
00015 #include "industry.h"
00016 #include "station_base.h"
00017 #include "train.h"
00018 #include "landscape.h"
00019 #include "viewport_func.h"
00020 #include "command_func.h"
00021 #include "town.h"
00022 #include "news_func.h"
00023 #include "variables.h"
00024 #include "cheat_type.h"
00025 #include "genworld.h"
00026 #include "tree_map.h"
00027 #include "newgrf.h"
00028 #include "newgrf_cargo.h"
00029 #include "newgrf_commons.h"
00030 #include "newgrf_industries.h"
00031 #include "newgrf_industrytiles.h"
00032 #include "autoslope.h"
00033 #include "transparency.h"
00034 #include "water.h"
00035 #include "strings_func.h"
00036 #include "functions.h"
00037 #include "window_func.h"
00038 #include "date_func.h"
00039 #include "vehicle_func.h"
00040 #include "sound_func.h"
00041 #include "animated_tile_func.h"
00042 #include "effectvehicle_func.h"
00043 #include "ai/ai.hpp"
00044 #include "core/pool_func.hpp"
00045 #include "subsidy_func.h"
00046
00047 #include "table/strings.h"
00048 #include "table/industry_land.h"
00049 #include "table/build_industry.h"
00050
00051 IndustryPool _industry_pool("Industry");
00052 INSTANTIATE_POOL_METHODS(Industry)
00053
00054 void ShowIndustryViewWindow(int industry);
00055 void BuildOilRig(TileIndex tile);
00056
00057 static byte _industry_sound_ctr;
00058 static TileIndex _industry_sound_tile;
00059
00060 uint16 _industry_counts[NUM_INDUSTRYTYPES];
00061
00062 IndustrySpec _industry_specs[NUM_INDUSTRYTYPES];
00063 IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES];
00064
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
00088 void ResetIndustryCreationProbility(IndustryType type)
00089 {
00090 assert(type < INVALID_INDUSTRYTYPE);
00091 _industry_specs[type].appear_creation[_settings_game.game_creation.landscape] = 0;
00092 }
00093
00102 IndustryType GetIndustryType(TileIndex tile)
00103 {
00104 assert(IsTileType(tile, MP_INDUSTRY));
00105
00106 const Industry *ind = Industry::GetByTile(tile);
00107 assert(ind != NULL);
00108 return ind->type;
00109 }
00110
00119 const IndustrySpec *GetIndustrySpec(IndustryType thistype)
00120 {
00121 assert(thistype < NUM_INDUSTRYTYPES);
00122 return &_industry_specs[thistype];
00123 }
00124
00133 const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
00134 {
00135 assert(gfx < INVALID_INDUSTRYTILE);
00136 return &_industry_tile_specs[gfx];
00137 }
00138
00139 Industry::~Industry()
00140 {
00141 if (CleaningPool()) return;
00142
00143
00144
00145 if (this->location.w == 0) return;
00146
00147 TILE_AREA_LOOP(tile_cur, this->location) {
00148 if (IsTileType(tile_cur, MP_INDUSTRY)) {
00149 if (GetIndustryIndex(tile_cur) == this->index) {
00150
00151 MakeWaterKeepingClass(tile_cur, OWNER_NONE);
00152
00153
00154
00155 DeleteAnimatedTile(tile_cur);
00156
00157 MarkTileDirtyByTile(tile_cur);
00158 }
00159 } else if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
00160 DeleteOilRig(tile_cur);
00161 }
00162 }
00163
00164 if (GetIndustrySpec(this->type)->behaviour & INDUSTRYBEH_PLANT_FIELDS) {
00165
00166 TILE_LOOP(tile_cur, 42, 42, this->location.tile - TileDiffXY(21, 21)) {
00167 tile_cur = TILE_MASK(tile_cur);
00168 if (IsTileType(tile_cur, MP_CLEAR) && IsClearGround(tile_cur, CLEAR_FIELDS) &&
00169 GetIndustryIndexOfField(tile_cur) == this->index) {
00170 SetIndustryIndexOfField(tile_cur, INVALID_INDUSTRY);
00171 }
00172 }
00173 }
00174
00175
00176 ReleaseDisastersTargetingIndustry(this->index);
00177
00178 DecIndustryTypeCount(this->type);
00179
00180 DeleteIndustryNews(this->index);
00181 DeleteWindowById(WC_INDUSTRY_VIEW, this->index);
00182
00183 DeleteSubsidyWith(ST_INDUSTRY, this->index);
00184 CargoPacket::InvalidateAllFrom(ST_INDUSTRY, this->index);
00185 }
00186
00191 void Industry::PostDestructor(size_t index)
00192 {
00193 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
00194 Station::RecomputeIndustriesNearForAll();
00195 }
00196
00197
00202 Industry *Industry::GetRandom()
00203 {
00204 if (Industry::GetNumItems() == 0) return NULL;
00205 int num = RandomRange((uint16)Industry::GetNumItems());
00206 size_t index = MAX_UVALUE(size_t);
00207
00208 while (num >= 0) {
00209 num--;
00210 index++;
00211
00212
00213 while (!Industry::IsValidID(index)) {
00214 index++;
00215 assert(index < Industry::GetPoolSize());
00216 }
00217 }
00218
00219 return Industry::Get(index);
00220 }
00221
00222
00223 static void IndustryDrawSugarMine(const TileInfo *ti)
00224 {
00225 const DrawIndustryAnimationStruct *d;
00226
00227 if (!IsIndustryCompleted(ti->tile)) return;
00228
00229 d = &_draw_industry_spec1[GetIndustryAnimationState(ti->tile)];
00230
00231 AddChildSpriteScreen(SPR_IT_SUGAR_MINE_SIEVE + d->image_1, PAL_NONE, d->x, 0);
00232
00233 if (d->image_2 != 0) {
00234 AddChildSpriteScreen(SPR_IT_SUGAR_MINE_CLOUDS + d->image_2 - 1, PAL_NONE, 8, 41);
00235 }
00236
00237 if (d->image_3 != 0) {
00238 AddChildSpriteScreen(SPR_IT_SUGAR_MINE_PILE + d->image_3 - 1, PAL_NONE,
00239 _drawtile_proc1[d->image_3 - 1].x, _drawtile_proc1[d->image_3 - 1].y);
00240 }
00241 }
00242
00243 static void IndustryDrawToffeeQuarry(const TileInfo *ti)
00244 {
00245 uint8 x = 0;
00246
00247 if (IsIndustryCompleted(ti->tile)) {
00248 x = _industry_anim_offs_toffee[GetIndustryAnimationState(ti->tile)];
00249 if (x == 0xFF)
00250 x = 0;
00251 }
00252
00253 AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_SHOVEL, PAL_NONE, 22 - x, 24 + x);
00254 AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_TOFFEE, PAL_NONE, 6, 14);
00255 }
00256
00257 static void IndustryDrawBubbleGenerator( const TileInfo *ti)
00258 {
00259 if (IsIndustryCompleted(ti->tile)) {
00260 AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_BUBBLE, PAL_NONE, 5, _industry_anim_offs_bubbles[GetIndustryAnimationState(ti->tile)]);
00261 } else {
00262 AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_SPRING, PAL_NONE, 3, 67);
00263 }
00264 }
00265
00266 static void IndustryDrawToyFactory(const TileInfo *ti)
00267 {
00268 const DrawIndustryAnimationStruct *d;
00269
00270 d = &_industry_anim_offs_toys[GetIndustryAnimationState(ti->tile)];
00271
00272 if (d->image_1 != 0xFF) {
00273 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_CLAY, PAL_NONE, d->x, 96 + d->image_1);
00274 }
00275
00276 if (d->image_2 != 0xFF) {
00277 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_ROBOT, PAL_NONE, 16 - d->image_2 * 2, 100 + d->image_2);
00278 }
00279
00280 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP, PAL_NONE, 7, d->image_3);
00281 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP_HOLDER, PAL_NONE, 0, 42);
00282 }
00283
00284 static void IndustryDrawCoalPlantSparks(const TileInfo *ti)
00285 {
00286 if (IsIndustryCompleted(ti->tile)) {
00287 uint8 image = GetIndustryAnimationState(ti->tile);
00288
00289 if (image != 0 && image < 7) {
00290 AddChildSpriteScreen(image + SPR_IT_POWER_PLANT_TRANSFORMERS,
00291 PAL_NONE,
00292 _coal_plant_sparks[image - 1].x,
00293 _coal_plant_sparks[image - 1].y
00294 );
00295 }
00296 }
00297 }
00298
00299 typedef void IndustryDrawTileProc(const TileInfo *ti);
00300 static IndustryDrawTileProc * const _industry_draw_tile_procs[5] = {
00301 IndustryDrawSugarMine,
00302 IndustryDrawToffeeQuarry,
00303 IndustryDrawBubbleGenerator,
00304 IndustryDrawToyFactory,
00305 IndustryDrawCoalPlantSparks,
00306 };
00307
00308 static void DrawTile_Industry(TileInfo *ti)
00309 {
00310 IndustryGfx gfx = GetIndustryGfx(ti->tile);
00311 Industry *ind = Industry::GetByTile(ti->tile);
00312 const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
00313 const DrawBuildingsTileStruct *dits;
00314 SpriteID image;
00315 SpriteID pal;
00316
00317
00318 if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00319
00320
00321
00322
00323 if (indts->grf_prop.spritegroup != NULL && DrawNewIndustryTile(ti, ind, gfx, indts)) {
00324 return;
00325 } else {
00326
00327
00328 if (indts->grf_prop.subst_id != INVALID_INDUSTRYTILE) {
00329 gfx = indts->grf_prop.subst_id;
00330
00331 indts = GetIndustryTileSpec(gfx);
00332 }
00333 }
00334 }
00335
00336 dits = &_industry_draw_tile_data[gfx << 2 | (indts->anim_state ?
00337 GetIndustryAnimationState(ti->tile) & INDUSTRY_COMPLETED :
00338 GetIndustryConstructionStage(ti->tile))];
00339
00340 image = dits->ground.sprite;
00341 if (HasBit(image, PALETTE_MODIFIER_COLOUR) && dits->ground.pal == PAL_NONE) {
00342 pal = GENERAL_SPRITE_COLOUR(ind->random_colour);
00343 } else {
00344 pal = dits->ground.pal;
00345 }
00346
00347
00348 if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
00349
00350
00351
00352 if (image == SPR_FLAT_WATER_TILE && IsIndustryTileOnWater(ti->tile)) {
00353 DrawWaterClassGround(ti);
00354 } else {
00355 DrawGroundSprite(image, pal);
00356 }
00357
00358
00359 if (IsInvisibilitySet(TO_INDUSTRIES)) return;
00360
00361
00362 image = dits->building.sprite;
00363 if (image != 0) {
00364 AddSortableSpriteToDraw(image,
00365 (HasBit(image, PALETTE_MODIFIER_COLOUR) && dits->building.pal == PAL_NONE) ? GENERAL_SPRITE_COLOUR(ind->random_colour) : dits->building.pal,
00366 ti->x + dits->subtile_x,
00367 ti->y + dits->subtile_y,
00368 dits->width,
00369 dits->height,
00370 dits->dz,
00371 ti->z,
00372 IsTransparencySet(TO_INDUSTRIES));
00373
00374 if (IsTransparencySet(TO_INDUSTRIES)) return;
00375 }
00376
00377 {
00378 int proc = dits->draw_proc - 1;
00379 if (proc >= 0) _industry_draw_tile_procs[proc](ti);
00380 }
00381 }
00382
00383 static uint GetSlopeZ_Industry(TileIndex tile, uint x, uint y)
00384 {
00385 return GetTileMaxZ(tile);
00386 }
00387
00388 static Foundation GetFoundation_Industry(TileIndex tile, Slope tileh)
00389 {
00390 IndustryGfx gfx = GetIndustryGfx(tile);
00391
00392
00393
00394
00395
00396 if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00397 const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
00398 if (indts->grf_prop.spritegroup != NULL && HasBit(indts->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
00399 uint32 callback_res = GetIndustryTileCallback(CBID_INDUSTRY_DRAW_FOUNDATIONS, 0, 0, gfx, Industry::GetByTile(tile), tile);
00400 if (callback_res == 0) return FOUNDATION_NONE;
00401 }
00402 }
00403 return FlatteningFoundation(tileh);
00404 }
00405
00406 static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray &acceptance, uint32 *always_accepted)
00407 {
00408 IndustryGfx gfx = GetIndustryGfx(tile);
00409 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00410
00411
00412 CargoID raw_accepts_cargo[lengthof(itspec->accepts_cargo)];
00413 uint8 raw_cargo_acceptance[lengthof(itspec->acceptance)];
00414
00415
00416 const CargoID *accepts_cargo = itspec->accepts_cargo;
00417 const uint8 *cargo_acceptance = itspec->acceptance;
00418
00419 if (HasBit(itspec->callback_mask, CBM_INDT_ACCEPT_CARGO)) {
00420 uint16 res = GetIndustryTileCallback(CBID_INDTILE_ACCEPT_CARGO, 0, 0, gfx, Industry::GetByTile(tile), tile);
00421 if (res != CALLBACK_FAILED) {
00422 accepts_cargo = raw_accepts_cargo;
00423 for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_accepts_cargo[i] = GetCargoTranslation(GB(res, i * 5, 5), itspec->grf_prop.grffile);
00424 }
00425 }
00426
00427 if (HasBit(itspec->callback_mask, CBM_INDT_CARGO_ACCEPTANCE)) {
00428 uint16 res = GetIndustryTileCallback(CBID_INDTILE_CARGO_ACCEPTANCE, 0, 0, gfx, Industry::GetByTile(tile), tile);
00429 if (res != CALLBACK_FAILED) {
00430 cargo_acceptance = raw_cargo_acceptance;
00431 for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_cargo_acceptance[i] = GB(res, i * 4, 4);
00432 }
00433 }
00434
00435 const Industry *ind = Industry::GetByTile(tile);
00436 for (byte i = 0; i < lengthof(itspec->accepts_cargo); i++) {
00437 CargoID a = accepts_cargo[i];
00438 if (a == CT_INVALID || cargo_acceptance[i] == 0) continue;
00439
00440
00441 acceptance[a] += cargo_acceptance[i];
00442
00443
00444 if (HasBit(*always_accepted, a)) continue;
00445
00446 bool accepts = false;
00447 for (uint cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
00448
00449 if (ind->accepts_cargo[cargo_index] == a) {
00450 accepts = true;
00451 break;
00452 }
00453 }
00454
00455 if (accepts) continue;
00456
00457
00458 SetBit(*always_accepted, a);
00459 }
00460 }
00461
00462 static void GetTileDesc_Industry(TileIndex tile, TileDesc *td)
00463 {
00464 const Industry *i = Industry::GetByTile(tile);
00465 const IndustrySpec *is = GetIndustrySpec(i->type);
00466
00467 td->owner[0] = i->owner;
00468 td->str = is->name;
00469 if (!IsIndustryCompleted(tile)) {
00470 SetDParamX(td->dparam, 0, td->str);
00471 td->str = STR_LAI_TOWN_INDUSTRY_DESCRIPTION_UNDER_CONSTRUCTION;
00472 }
00473
00474 if (is->grf_prop.grffile != NULL) {
00475 td->grf = GetGRFConfig(is->grf_prop.grffile->grfid)->name;
00476 }
00477 }
00478
00479 static CommandCost ClearTile_Industry(TileIndex tile, DoCommandFlag flags)
00480 {
00481 Industry *i = Industry::GetByTile(tile);
00482 const IndustrySpec *indspec = GetIndustrySpec(i->type);
00483
00484
00485
00486
00487
00488
00489 if ((_current_company != OWNER_WATER && _game_mode != GM_EDITOR &&
00490 !_cheats.magic_bulldozer.value) ||
00491 ((flags & DC_AUTO) != 0) ||
00492 (_current_company == OWNER_WATER &&
00493 ((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) ||
00494 HasBit(GetIndustryTileSpec(GetIndustryGfx(tile))->slopes_refused, 5)))) {
00495 SetDParam(0, indspec->name);
00496 return_cmd_error(flags & DC_AUTO ? STR_ERROR_UNMOVABLE_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
00497 }
00498
00499 if (flags & DC_EXEC) {
00500 AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
00501 delete i;
00502 }
00503 return CommandCost(EXPENSES_CONSTRUCTION, indspec->GetRemovalCost());
00504 }
00505
00506 static void TransportIndustryGoods(TileIndex tile)
00507 {
00508 Industry *i = Industry::GetByTile(tile);
00509 const IndustrySpec *indspec = GetIndustrySpec(i->type);
00510 bool moved_cargo = false;
00511
00512 StationFinder stations(i->location);
00513
00514 for (uint j = 0; j < lengthof(i->produced_cargo_waiting); j++) {
00515 uint cw = min(i->produced_cargo_waiting[j], 255);
00516 if (cw > indspec->minimal_cargo && i->produced_cargo[j] != CT_INVALID) {
00517 i->produced_cargo_waiting[j] -= cw;
00518
00519
00520 if (_economy.fluct <= 0) cw = (cw + 1) / 2;
00521
00522 i->this_month_production[j] += cw;
00523
00524 uint am = MoveGoodsToStation(i->produced_cargo[j], cw, ST_INDUSTRY, i->index, stations.GetStations());
00525 i->this_month_transported[j] += am;
00526
00527 moved_cargo |= (am != 0);
00528 }
00529 }
00530
00531 if (moved_cargo && !StartStopIndustryTileAnimation(i, IAT_INDUSTRY_DISTRIBUTES_CARGO)) {
00532 uint newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_production;
00533
00534 if (newgfx != INDUSTRYTILE_NOANIM) {
00535 ResetIndustryConstructionStage(tile);
00536 SetIndustryCompleted(tile, true);
00537 SetIndustryGfx(tile, newgfx);
00538 MarkTileDirtyByTile(tile);
00539 }
00540 }
00541 }
00542
00543
00544 static void AnimateTile_Industry(TileIndex tile)
00545 {
00546 byte m;
00547 IndustryGfx gfx = GetIndustryGfx(tile);
00548
00549 if (GetIndustryTileSpec(gfx)->animation_info != 0xFFFF) {
00550 AnimateNewIndustryTile(tile);
00551 return;
00552 }
00553
00554 switch (gfx) {
00555 case GFX_SUGAR_MINE_SIEVE:
00556 if ((_tick_counter & 1) == 0) {
00557 m = GetIndustryAnimationState(tile) + 1;
00558
00559 switch (m & 7) {
00560 case 2: SndPlayTileFx(SND_2D_RIP_2, tile); break;
00561 case 6: SndPlayTileFx(SND_29_RIP, tile); break;
00562 }
00563
00564 if (m >= 96) {
00565 m = 0;
00566 DeleteAnimatedTile(tile);
00567 }
00568 SetIndustryAnimationState(tile, m);
00569
00570 MarkTileDirtyByTile(tile);
00571 }
00572 break;
00573
00574 case GFX_TOFFEE_QUARY:
00575 if ((_tick_counter & 3) == 0) {
00576 m = GetIndustryAnimationState(tile);
00577
00578 if (_industry_anim_offs_toffee[m] == 0xFF) {
00579 SndPlayTileFx(SND_30_CARTOON_SOUND, tile);
00580 }
00581
00582 if (++m >= 70) {
00583 m = 0;
00584 DeleteAnimatedTile(tile);
00585 }
00586 SetIndustryAnimationState(tile, m);
00587
00588 MarkTileDirtyByTile(tile);
00589 }
00590 break;
00591
00592 case GFX_BUBBLE_CATCHER:
00593 if ((_tick_counter & 1) == 0) {
00594 m = GetIndustryAnimationState(tile);
00595
00596 if (++m >= 40) {
00597 m = 0;
00598 DeleteAnimatedTile(tile);
00599 }
00600 SetIndustryAnimationState(tile, m);
00601
00602 MarkTileDirtyByTile(tile);
00603 }
00604 break;
00605
00606
00607 case GFX_POWERPLANT_SPARKS:
00608 if ((_tick_counter & 3) == 0) {
00609 m = GetIndustryAnimationState(tile);
00610 if (m == 6) {
00611 SetIndustryAnimationState(tile, 0);
00612 DeleteAnimatedTile(tile);
00613 } else {
00614 SetIndustryAnimationState(tile, m + 1);
00615 MarkTileDirtyByTile(tile);
00616 }
00617 }
00618 break;
00619
00620 case GFX_TOY_FACTORY:
00621 if ((_tick_counter & 1) == 0) {
00622 m = GetIndustryAnimationState(tile) + 1;
00623
00624 switch (m) {
00625 case 1: SndPlayTileFx(SND_2C_MACHINERY, tile); break;
00626 case 23: SndPlayTileFx(SND_2B_COMEDY_HIT, tile); break;
00627 case 28: SndPlayTileFx(SND_2A_EXTRACT_AND_POP, tile); break;
00628 default:
00629 if (m >= 50) {
00630 int n = GetIndustryAnimationLoop(tile) + 1;
00631 m = 0;
00632 if (n >= 8) {
00633 n = 0;
00634 DeleteAnimatedTile(tile);
00635 }
00636 SetIndustryAnimationLoop(tile, n);
00637 }
00638 }
00639
00640 SetIndustryAnimationState(tile, m);
00641 MarkTileDirtyByTile(tile);
00642 }
00643 break;
00644
00645 case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
00646 case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
00647 case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
00648 case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
00649 if ((_tick_counter & 3) == 0) {
00650 IndustryGfx gfx = GetIndustryGfx(tile);
00651
00652 gfx = (gfx < 155) ? gfx + 1 : 148;
00653 SetIndustryGfx(tile, gfx);
00654 MarkTileDirtyByTile(tile);
00655 }
00656 break;
00657
00658 case GFX_OILWELL_ANIMATED_1:
00659 case GFX_OILWELL_ANIMATED_2:
00660 case GFX_OILWELL_ANIMATED_3:
00661 if ((_tick_counter & 7) == 0) {
00662 bool b = Chance16(1, 7);
00663 IndustryGfx gfx = GetIndustryGfx(tile);
00664
00665 m = GetIndustryAnimationState(tile) + 1;
00666 if (m == 4 && (m = 0, ++gfx) == GFX_OILWELL_ANIMATED_3 + 1 && (gfx = GFX_OILWELL_ANIMATED_1, b)) {
00667 SetIndustryGfx(tile, GFX_OILWELL_NOT_ANIMATED);
00668 SetIndustryConstructionStage(tile, 3);
00669 DeleteAnimatedTile(tile);
00670 } else {
00671 SetIndustryAnimationState(tile, m);
00672 SetIndustryGfx(tile, gfx);
00673 MarkTileDirtyByTile(tile);
00674 }
00675 }
00676 break;
00677
00678 case GFX_COAL_MINE_TOWER_ANIMATED:
00679 case GFX_COPPER_MINE_TOWER_ANIMATED:
00680 case GFX_GOLD_MINE_TOWER_ANIMATED: {
00681 int state = _tick_counter & 0x7FF;
00682
00683 if ((state -= 0x400) < 0)
00684 return;
00685
00686 if (state < 0x1A0) {
00687 if (state < 0x20 || state >= 0x180) {
00688 m = GetIndustryAnimationState(tile);
00689 if (!(m & 0x40)) {
00690 SetIndustryAnimationState(tile, m | 0x40);
00691 SndPlayTileFx(SND_0B_MINING_MACHINERY, tile);
00692 }
00693 if (state & 7)
00694 return;
00695 } else {
00696 if (state & 3)
00697 return;
00698 }
00699 m = (GetIndustryAnimationState(tile) + 1) | 0x40;
00700 if (m > 0xC2) m = 0xC0;
00701 SetIndustryAnimationState(tile, m);
00702 MarkTileDirtyByTile(tile);
00703 } else if (state >= 0x200 && state < 0x3A0) {
00704 int i;
00705 i = (state < 0x220 || state >= 0x380) ? 7 : 3;
00706 if (state & i)
00707 return;
00708
00709 m = (GetIndustryAnimationState(tile) & 0xBF) - 1;
00710 if (m < 0x80) m = 0x82;
00711 SetIndustryAnimationState(tile, m);
00712 MarkTileDirtyByTile(tile);
00713 }
00714 } break;
00715 }
00716 }
00717
00718 static void CreateChimneySmoke(TileIndex tile)
00719 {
00720 uint x = TileX(tile) * TILE_SIZE;
00721 uint y = TileY(tile) * TILE_SIZE;
00722 uint z = GetTileMaxZ(tile);
00723
00724 CreateEffectVehicle(x + 15, y + 14, z + 59, EV_CHIMNEY_SMOKE);
00725 }
00726
00727 static void MakeIndustryTileBigger(TileIndex tile)
00728 {
00729 byte cnt = GetIndustryConstructionCounter(tile) + 1;
00730 byte stage;
00731
00732 if (cnt != 4) {
00733 SetIndustryConstructionCounter(tile, cnt);
00734 return;
00735 }
00736
00737 stage = GetIndustryConstructionStage(tile) + 1;
00738 SetIndustryConstructionCounter(tile, 0);
00739 SetIndustryConstructionStage(tile, stage);
00740 StartStopIndustryTileAnimation(tile, IAT_CONSTRUCTION_STATE_CHANGE);
00741 if (stage == INDUSTRY_COMPLETED) SetIndustryCompleted(tile, true);
00742
00743 MarkTileDirtyByTile(tile);
00744
00745 if (!IsIndustryCompleted(tile)) return;
00746
00747 IndustryGfx gfx = GetIndustryGfx(tile);
00748 if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00749
00750 return;
00751 }
00752
00753 switch (gfx) {
00754 case GFX_POWERPLANT_CHIMNEY:
00755 CreateChimneySmoke(tile);
00756 break;
00757
00758 case GFX_OILRIG_1: {
00759
00760
00761
00762
00763 TileIndex other = tile + TileDiffXY(0, 1);
00764
00765 if (IsTileType(other, MP_INDUSTRY) &&
00766 GetIndustryGfx(other) == GFX_OILRIG_1 &&
00767 GetIndustryIndex(tile) == GetIndustryIndex(other)) {
00768 BuildOilRig(tile);
00769 }
00770 } break;
00771
00772 case GFX_TOY_FACTORY:
00773 case GFX_BUBBLE_CATCHER:
00774 case GFX_TOFFEE_QUARY:
00775 SetIndustryAnimationState(tile, 0);
00776 SetIndustryAnimationLoop(tile, 0);
00777 break;
00778
00779 case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
00780 case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
00781 case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
00782 case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
00783 AddAnimatedTile(tile);
00784 break;
00785 }
00786 }
00787
00788 static void TileLoopIndustry_BubbleGenerator(TileIndex tile)
00789 {
00790 static const int8 _bubble_spawn_location[3][4] = {
00791 { 11, 0, -4, -14 },
00792 { -4, -10, -4, 1 },
00793 { 49, 59, 60, 65 },
00794 };
00795
00796 SndPlayTileFx(SND_2E_EXTRACT_AND_POP, tile);
00797
00798 int dir = Random() & 3;
00799
00800 EffectVehicle *v = CreateEffectVehicleAbove(
00801 TileX(tile) * TILE_SIZE + _bubble_spawn_location[0][dir],
00802 TileY(tile) * TILE_SIZE + _bubble_spawn_location[1][dir],
00803 _bubble_spawn_location[2][dir],
00804 EV_BUBBLE
00805 );
00806
00807 if (v != NULL) v->animation_substate = dir;
00808 }
00809
00810 static void TileLoop_Industry(TileIndex tile)
00811 {
00812 IndustryGfx newgfx;
00813 IndustryGfx gfx;
00814
00815 if (IsIndustryTileOnWater(tile)) TileLoop_Water(tile);
00816
00817 TriggerIndustryTile(tile, INDTILE_TRIGGER_TILE_LOOP);
00818
00819 if (!IsIndustryCompleted(tile)) {
00820 MakeIndustryTileBigger(tile);
00821 return;
00822 }
00823
00824 if (_game_mode == GM_EDITOR) return;
00825
00826 TransportIndustryGoods(tile);
00827
00828 if (StartStopIndustryTileAnimation(tile, IAT_TILELOOP)) return;
00829
00830 newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_next;
00831 if (newgfx != INDUSTRYTILE_NOANIM) {
00832 ResetIndustryConstructionStage(tile);
00833 SetIndustryGfx(tile, newgfx);
00834 MarkTileDirtyByTile(tile);
00835 return;
00836 }
00837
00838 gfx = GetIndustryGfx(tile);
00839
00840 switch (gfx) {
00841 case GFX_COAL_MINE_TOWER_NOT_ANIMATED:
00842 case GFX_COPPER_MINE_TOWER_NOT_ANIMATED:
00843 case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:
00844 if (!(_tick_counter & 0x400) && Chance16(1, 2)) {
00845 switch (gfx) {
00846 case GFX_COAL_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COAL_MINE_TOWER_ANIMATED; break;
00847 case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_ANIMATED; break;
00848 case GFX_GOLD_MINE_TOWER_NOT_ANIMATED: gfx = GFX_GOLD_MINE_TOWER_ANIMATED; break;
00849 }
00850 SetIndustryGfx(tile, gfx);
00851 SetIndustryAnimationState(tile, 0x80);
00852 AddAnimatedTile(tile);
00853 }
00854 break;
00855
00856 case GFX_OILWELL_NOT_ANIMATED:
00857 if (Chance16(1, 6)) {
00858 SetIndustryGfx(tile, GFX_OILWELL_ANIMATED_1);
00859 SetIndustryAnimationState(tile, 0);
00860 AddAnimatedTile(tile);
00861 }
00862 break;
00863
00864 case GFX_COAL_MINE_TOWER_ANIMATED:
00865 case GFX_COPPER_MINE_TOWER_ANIMATED:
00866 case GFX_GOLD_MINE_TOWER_ANIMATED:
00867 if (!(_tick_counter & 0x400)) {
00868 switch (gfx) {
00869 case GFX_COAL_MINE_TOWER_ANIMATED: gfx = GFX_COAL_MINE_TOWER_NOT_ANIMATED; break;
00870 case GFX_COPPER_MINE_TOWER_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_NOT_ANIMATED; break;
00871 case GFX_GOLD_MINE_TOWER_ANIMATED: gfx = GFX_GOLD_MINE_TOWER_NOT_ANIMATED; break;
00872 }
00873 SetIndustryGfx(tile, gfx);
00874 SetIndustryCompleted(tile, true);
00875 SetIndustryConstructionStage(tile, 3);
00876 DeleteAnimatedTile(tile);
00877 }
00878 break;
00879
00880 case GFX_POWERPLANT_SPARKS:
00881 if (Chance16(1, 3)) {
00882 SndPlayTileFx(SND_0C_ELECTRIC_SPARK, tile);
00883 AddAnimatedTile(tile);
00884 }
00885 break;
00886
00887 case GFX_COPPER_MINE_CHIMNEY:
00888 CreateEffectVehicleAbove(TileX(tile) * TILE_SIZE + 6, TileY(tile) * TILE_SIZE + 6, 43, EV_SMOKE);
00889 break;
00890
00891
00892 case GFX_TOY_FACTORY: {
00893 Industry *i = Industry::GetByTile(tile);
00894 if (i->was_cargo_delivered) {
00895 i->was_cargo_delivered = false;
00896 SetIndustryAnimationLoop(tile, 0);
00897 AddAnimatedTile(tile);
00898 }
00899 }
00900 break;
00901
00902 case GFX_BUBBLE_GENERATOR:
00903 TileLoopIndustry_BubbleGenerator(tile);
00904 break;
00905
00906 case GFX_TOFFEE_QUARY:
00907 AddAnimatedTile(tile);
00908 break;
00909
00910 case GFX_SUGAR_MINE_SIEVE:
00911 if (Chance16(1, 3)) AddAnimatedTile(tile);
00912 break;
00913 }
00914 }
00915
00916 static bool ClickTile_Industry(TileIndex tile)
00917 {
00918 ShowIndustryViewWindow(GetIndustryIndex(tile));
00919 return true;
00920 }
00921
00922 static TrackStatus GetTileTrackStatus_Industry(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
00923 {
00924 return 0;
00925 }
00926
00927 static void ChangeTileOwner_Industry(TileIndex tile, Owner old_owner, Owner new_owner)
00928 {
00929
00930 Industry *i = Industry::GetByTile(tile);
00931 if (i->founder == old_owner) i->founder = (new_owner == INVALID_OWNER) ? OWNER_NONE : new_owner;
00932 }
00933
00934 static const byte _plantfarmfield_type[] = {1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6};
00935
00936 static bool IsBadFarmFieldTile(TileIndex tile)
00937 {
00938 switch (GetTileType(tile)) {
00939 case MP_CLEAR: return IsClearGround(tile, CLEAR_FIELDS) || IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
00940 case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
00941 default: return true;
00942 }
00943 }
00944
00945 static bool IsBadFarmFieldTile2(TileIndex tile)
00946 {
00947 switch (GetTileType(tile)) {
00948 case MP_CLEAR: return IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
00949 case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
00950 default: return true;
00951 }
00952 }
00953
00954 static void SetupFarmFieldFence(TileIndex tile, int size, byte type, Axis direction)
00955 {
00956 do {
00957 tile = TILE_MASK(tile);
00958
00959 if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) {
00960 byte or_ = type;
00961
00962 if (or_ == 1 && Chance16(1, 7)) or_ = 2;
00963
00964 if (direction == AXIS_X) {
00965 SetFenceSE(tile, or_);
00966 } else {
00967 SetFenceSW(tile, or_);
00968 }
00969 }
00970
00971 tile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00972 } while (--size);
00973 }
00974
00975 static void PlantFarmField(TileIndex tile, IndustryID industry)
00976 {
00977 uint size_x, size_y;
00978 uint32 r;
00979 uint count;
00980 uint counter;
00981 uint field_type;
00982 int type;
00983
00984 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
00985 if (GetTileZ(tile) + TILE_HEIGHT * 2 >= GetSnowLine())
00986 return;
00987 }
00988
00989
00990 r = (Random() & 0x303) + 0x404;
00991 if (_settings_game.game_creation.landscape == LT_ARCTIC) r += 0x404;
00992 size_x = GB(r, 0, 8);
00993 size_y = GB(r, 8, 8);
00994
00995
00996 tile -= TileDiffXY(size_x / 2, size_y / 2);
00997
00998 if (TileX(tile) + size_x >= MapSizeX() || TileY(tile) + size_y >= MapSizeY()) return;
00999
01000
01001 count = 0;
01002 TILE_LOOP(cur_tile, size_x, size_y, tile) {
01003 assert(cur_tile < MapSize());
01004 count += IsBadFarmFieldTile(cur_tile);
01005 }
01006 if (count * 2 >= size_x * size_y) return;
01007
01008
01009 r = Random();
01010 counter = GB(r, 5, 3);
01011 field_type = GB(r, 8, 8) * 9 >> 8;
01012
01013
01014 TILE_LOOP(cur_tile, size_x, size_y, tile) {
01015 assert(cur_tile < MapSize());
01016 if (!IsBadFarmFieldTile2(cur_tile)) {
01017 MakeField(cur_tile, field_type, industry);
01018 SetClearCounter(cur_tile, counter);
01019 MarkTileDirtyByTile(cur_tile);
01020 }
01021 }
01022
01023 type = 3;
01024 if (_settings_game.game_creation.landscape != LT_ARCTIC && _settings_game.game_creation.landscape != LT_TROPIC) {
01025 type = _plantfarmfield_type[Random() & 0xF];
01026 }
01027
01028 SetupFarmFieldFence(tile - TileDiffXY(1, 0), size_y, type, AXIS_Y);
01029 SetupFarmFieldFence(tile - TileDiffXY(0, 1), size_x, type, AXIS_X);
01030 SetupFarmFieldFence(tile + TileDiffXY(size_x - 1, 0), size_y, type, AXIS_Y);
01031 SetupFarmFieldFence(tile + TileDiffXY(0, size_y - 1), size_x, type, AXIS_X);
01032 }
01033
01034 void PlantRandomFarmField(const Industry *i)
01035 {
01036 int x = i->location.w / 2 + Random() % 31 - 16;
01037 int y = i->location.h / 2 + Random() % 31 - 16;
01038
01039 TileIndex tile = TileAddWrap(i->location.tile, x, y);
01040
01041 if (tile != INVALID_TILE) PlantFarmField(tile, i->index);
01042 }
01043
01050 static bool SearchLumberMillTrees(TileIndex tile, void *user_data)
01051 {
01052 if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) {
01053 CompanyID old_company = _current_company;
01054
01055
01056 _current_company = OWNER_NONE;
01057 _industry_sound_ctr = 1;
01058 _industry_sound_tile = tile;
01059 SndPlayTileFx(SND_38_CHAINSAW, tile);
01060
01061 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
01062
01063 _current_company = old_company;
01064 return true;
01065 }
01066 return false;
01067 }
01068
01073 static void ChopLumberMillTrees(Industry *i)
01074 {
01075 TileIndex tile = i->location.tile;
01076
01077 if (!IsIndustryCompleted(tile)) return;
01078
01079 if (CircularTileSearch(&tile, 40, SearchLumberMillTrees, NULL))
01080 i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + 45);
01081 }
01082
01083 static void ProduceIndustryGoods(Industry *i)
01084 {
01085 uint32 r;
01086 uint num;
01087 const IndustrySpec *indsp = GetIndustrySpec(i->type);
01088
01089
01090 if ((i->counter & 0x3F) == 0) {
01091 if (Chance16R(1, 14, r) && (num = indsp->number_of_sounds) != 0) {
01092 SndPlayTileFx(
01093 (SoundFx)(indsp->random_sounds[((r >> 16) * num) >> 16]),
01094 i->location.tile);
01095 }
01096 }
01097
01098 i->counter--;
01099
01100
01101 if ((i->counter & 0xFF) == 0) {
01102 if (HasBit(indsp->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) IndustryProductionCallback(i, 1);
01103
01104 IndustryBehaviour indbehav = indsp->behaviour;
01105 i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + i->production_rate[0]);
01106 i->produced_cargo_waiting[1] = min(0xffff, i->produced_cargo_waiting[1] + i->production_rate[1]);
01107
01108 if ((indbehav & INDUSTRYBEH_PLANT_FIELDS) != 0) {
01109 bool plant;
01110 if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
01111 plant = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, Random(), 0, i, i->type, i->location.tile) != 0);
01112 } else {
01113 plant = Chance16(1, 8);
01114 }
01115
01116 if (plant) PlantRandomFarmField(i);
01117 }
01118 if ((indbehav & INDUSTRYBEH_CUT_TREES) != 0) {
01119 bool cut = ((i->counter & 0x1FF) == 0);
01120 if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
01121 cut = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, 0, 1, i, i->type, i->location.tile) != 0);
01122 }
01123
01124 if (cut) ChopLumberMillTrees(i);
01125 }
01126
01127 TriggerIndustry(i, INDUSTRY_TRIGGER_INDUSTRY_TICK);
01128 StartStopIndustryTileAnimation(i, IAT_INDUSTRY_TICK);
01129 }
01130 }
01131
01132 void OnTick_Industry()
01133 {
01134 Industry *i;
01135
01136 if (_industry_sound_ctr != 0) {
01137 _industry_sound_ctr++;
01138
01139 if (_industry_sound_ctr == 75) {
01140 SndPlayTileFx(SND_37_BALLOON_SQUEAK, _industry_sound_tile);
01141 } else if (_industry_sound_ctr == 160) {
01142 _industry_sound_ctr = 0;
01143 SndPlayTileFx(SND_36_CARTOON_CRASH, _industry_sound_tile);
01144 }
01145 }
01146
01147 if (_game_mode == GM_EDITOR) return;
01148
01149 FOR_ALL_INDUSTRIES(i) {
01150 ProduceIndustryGoods(i);
01151 }
01152 }
01153
01154 static bool CheckNewIndustry_NULL(TileIndex tile)
01155 {
01156 return true;
01157 }
01158
01159 static bool CheckNewIndustry_Forest(TileIndex tile)
01160 {
01161 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
01162 if (GetTileZ(tile) < HighestSnowLine() + TILE_HEIGHT * 2U) {
01163 _error_message = STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED;
01164 return false;
01165 }
01166 }
01167 return true;
01168 }
01169
01170 static bool CheckNewIndustry_OilRefinery(TileIndex tile)
01171 {
01172 if (_game_mode == GM_EDITOR) return true;
01173 if (DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return true;
01174
01175 _error_message = STR_ERROR_CAN_ONLY_BE_POSITIONED;
01176 return false;
01177 }
01178
01179 extern bool _ignore_restrictions;
01180
01181 static bool CheckNewIndustry_OilRig(TileIndex tile)
01182 {
01183 if (_game_mode == GM_EDITOR && _ignore_restrictions) return true;
01184 if (TileHeight(tile) == 0 &&
01185 DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return true;
01186
01187 _error_message = STR_ERROR_CAN_ONLY_BE_POSITIONED;
01188 return false;
01189 }
01190
01191 static bool CheckNewIndustry_Farm(TileIndex tile)
01192 {
01193 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
01194 if (GetTileZ(tile) + TILE_HEIGHT * 2 >= HighestSnowLine()) {
01195 _error_message = STR_ERROR_SITE_UNSUITABLE;
01196 return false;
01197 }
01198 }
01199 return true;
01200 }
01201
01202 static bool CheckNewIndustry_Plantation(TileIndex tile)
01203 {
01204 if (GetTropicZone(tile) == TROPICZONE_DESERT) {
01205 _error_message = STR_ERROR_SITE_UNSUITABLE;
01206 return false;
01207 }
01208
01209 return true;
01210 }
01211
01212 static bool CheckNewIndustry_Water(TileIndex tile)
01213 {
01214 if (GetTropicZone(tile) != TROPICZONE_DESERT) {
01215 _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT;
01216 return false;
01217 }
01218
01219 return true;
01220 }
01221
01222 static bool CheckNewIndustry_Lumbermill(TileIndex tile)
01223 {
01224 if (GetTropicZone(tile) != TROPICZONE_RAINFOREST) {
01225 _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST;
01226 return false;
01227 }
01228 return true;
01229 }
01230
01231 static bool CheckNewIndustry_BubbleGen(TileIndex tile)
01232 {
01233 return GetTileZ(tile) <= TILE_HEIGHT * 4;
01234 }
01235
01236 typedef bool CheckNewIndustryProc(TileIndex tile);
01237 static CheckNewIndustryProc * const _check_new_industry_procs[CHECK_END] = {
01238 CheckNewIndustry_NULL,
01239 CheckNewIndustry_Forest,
01240 CheckNewIndustry_OilRefinery,
01241 CheckNewIndustry_Farm,
01242 CheckNewIndustry_Plantation,
01243 CheckNewIndustry_Water,
01244 CheckNewIndustry_Lumbermill,
01245 CheckNewIndustry_BubbleGen,
01246 CheckNewIndustry_OilRig
01247 };
01248
01249 static const Town *CheckMultipleIndustryInTown(TileIndex tile, int type)
01250 {
01251 const Town *t;
01252 const Industry *i;
01253
01254 t = ClosestTownFromTile(tile, UINT_MAX);
01255
01256 if (_settings_game.economy.multiple_industry_per_town) return t;
01257
01258 FOR_ALL_INDUSTRIES(i) {
01259 if (i->type == (byte)type &&
01260 i->town == t) {
01261 _error_message = STR_ERROR_ONLY_ONE_ALLOWED_PER_TOWN;
01262 return NULL;
01263 }
01264 }
01265
01266 return t;
01267 }
01268
01269 bool IsSlopeRefused(Slope current, Slope refused)
01270 {
01271 if (IsSteepSlope(current)) return true;
01272 if (current != SLOPE_FLAT) {
01273 if (IsSteepSlope(refused)) return true;
01274
01275 Slope t = ComplementSlope(current);
01276
01277 if ((refused & SLOPE_W) && (t & SLOPE_NW)) return true;
01278 if ((refused & SLOPE_S) && (t & SLOPE_NE)) return true;
01279 if ((refused & SLOPE_E) && (t & SLOPE_SW)) return true;
01280 if ((refused & SLOPE_N) && (t & SLOPE_SE)) return true;
01281 }
01282
01283 return false;
01284 }
01285
01286 static bool CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTileTable *it, uint itspec_index, int type, bool *custom_shape_check = NULL)
01287 {
01288 _error_message = STR_ERROR_SITE_UNSUITABLE;
01289 bool refused_slope = false;
01290 bool custom_shape = false;
01291
01292 do {
01293 IndustryGfx gfx = GetTranslatedIndustryTileID(it->gfx);
01294 if (TileX(tile) + it->ti.x >= MapSizeX()) return false;
01295 if (TileY(tile) + it->ti.y >= MapSizeY()) return false;
01296 TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
01297
01298 if (!IsValidTile(cur_tile)) {
01299 if (gfx == GFX_WATERTILE_SPECIALCHECK) continue;
01300 return false;
01301 }
01302
01303 if (gfx == GFX_WATERTILE_SPECIALCHECK) {
01304 if (!IsTileType(cur_tile, MP_WATER) ||
01305 GetTileSlope(cur_tile, NULL) != SLOPE_FLAT) {
01306 return false;
01307 }
01308 } else {
01309 if (!EnsureNoVehicleOnGround(cur_tile)) return false;
01310 if (MayHaveBridgeAbove(cur_tile) && IsBridgeAbove(cur_tile)) return false;
01311
01312 const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
01313
01314 IndustryBehaviour ind_behav = GetIndustrySpec(type)->behaviour;
01315
01316
01317 if (!HasBit(its->slopes_refused, 5) && (IsWaterTile(cur_tile) == !(ind_behav & INDUSTRYBEH_BUILT_ONWATER))) return false;
01318
01319 if (HasBit(its->callback_mask, CBM_INDT_SHAPE_CHECK)) {
01320 custom_shape = true;
01321 if (!PerformIndustryTileSlopeCheck(tile, cur_tile, its, type, gfx, itspec_index)) return false;
01322 } else {
01323 Slope tileh = GetTileSlope(cur_tile, NULL);
01324 refused_slope |= IsSlopeRefused(tileh, its->slopes_refused);
01325 }
01326
01327 if ((ind_behav & (INDUSTRYBEH_ONLY_INTOWN | INDUSTRYBEH_TOWN1200_MORE)) ||
01328 ((ind_behav & INDUSTRYBEH_ONLY_NEARTOWN) && IsTileType(cur_tile, MP_HOUSE))) {
01329 if (!IsTileType(cur_tile, MP_HOUSE)) {
01330 _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS;
01331 return false;
01332 }
01333
01334
01335 CompanyID old_company = _current_company;
01336 _current_company = OWNER_TOWN;
01337 bool not_clearable = CmdFailed(DoCommand(cur_tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR));
01338 _current_company = old_company;
01339
01340 if (not_clearable) return false;
01341 } else {
01342
01343 bool not_clearable = CmdFailed(DoCommand(cur_tile, 0, 0, DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR));
01344
01345 if (not_clearable) return false;
01346 }
01347 }
01348 } while ((++it)->ti.x != -0x80);
01349
01350 if (custom_shape_check != NULL) *custom_shape_check = custom_shape;
01351
01352
01353
01354
01355 return !refused_slope || (_settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !custom_shape && !_ignore_restrictions);
01356 }
01357
01358 static bool CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t)
01359 {
01360 if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_TOWN1200_MORE) && t->population < 1200) {
01361 _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS_WITH_POPULATION_OF_1200;
01362 return false;
01363 }
01364
01365 if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_ONLY_NEARTOWN) && DistanceMax(t->xy, tile) > 9) {
01366 _error_message = STR_ERROR_SITE_UNSUITABLE;
01367 return false;
01368 }
01369
01370 return true;
01371 }
01372
01373 static bool CheckCanTerraformSurroundingTiles(TileIndex tile, uint height, int internal)
01374 {
01375 int size_x, size_y;
01376 uint curh;
01377
01378 size_x = 2;
01379 size_y = 2;
01380
01381
01382 if (TileX(tile) == 0 || TileY(tile) == 0 || GetTileType(tile) == MP_VOID) return false;
01383
01384 tile += TileDiffXY(-1, -1);
01385 TILE_LOOP(tile_walk, size_x, size_y, tile) {
01386 curh = TileHeight(tile_walk);
01387
01388 if ((GetTileType(tile_walk) != MP_CLEAR) && (GetTileType(tile_walk) != MP_TREES))
01389 return false;
01390
01391
01392 if (internal != 0 && Delta(curh, height) > 1) return false;
01393
01394
01395
01396
01397 if (internal == 0 && curh != height) {
01398 if (TileX(tile_walk) == 0 || TileY(tile_walk) == 0 || !CheckCanTerraformSurroundingTiles(tile_walk + TileDiffXY(-1, -1), height, internal + 1))
01399 return false;
01400 }
01401 }
01402
01403 return true;
01404 }
01405
01410 static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlag flags, const IndustryTileTable *it, int type)
01411 {
01412 const int MKEND = -0x80;
01413 int max_x = 0;
01414 int max_y = 0;
01415 TileIndex cur_tile;
01416 uint size_x, size_y;
01417 uint h, curh;
01418
01419
01420 do {
01421 if (it->gfx == 0xFF) continue;
01422 if (it->ti.x > max_x) max_x = it->ti.x;
01423 if (it->ti.y > max_y) max_y = it->ti.y;
01424 } while ((++it)->ti.x != MKEND);
01425
01426
01427 h = TileHeight(tile);
01428
01429 if (TileX(tile) <= 1 || TileY(tile) <= 1) return false;
01430
01431
01432 cur_tile = tile + TileDiffXY(-1, -1);
01433 size_x = max_x + 4;
01434 size_y = max_y + 4;
01435
01436
01437 if (TileX(cur_tile) + size_x >= MapMaxX() || TileY(cur_tile) + size_y >= MapMaxY()) return false;
01438
01439
01440
01441 CompanyID old_company = _current_company;
01442 _current_company = OWNER_TOWN;
01443
01444 TILE_LOOP(tile_walk, size_x, size_y, cur_tile) {
01445 curh = TileHeight(tile_walk);
01446 if (curh != h) {
01447
01448
01449 if (!CheckCanTerraformSurroundingTiles(tile_walk, h, 0)) {
01450 _current_company = old_company;
01451 return false;
01452 }
01453
01454
01455 if (CmdFailed(DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND))) {
01456 _current_company = old_company;
01457 return false;
01458 }
01459 }
01460 }
01461
01462 if (flags & DC_EXEC) {
01463
01464 TILE_LOOP(tile_walk, size_x, size_y, cur_tile) {
01465 curh = TileHeight(tile_walk);
01466 while (curh != h) {
01467
01468
01469
01470 DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
01471 curh += (curh > h) ? -1 : 1;
01472 }
01473 }
01474 }
01475
01476 _current_company = old_company;
01477 return true;
01478 }
01479
01480
01481 static bool CheckIfFarEnoughFromIndustry(TileIndex tile, int type)
01482 {
01483 const IndustrySpec *indspec = GetIndustrySpec(type);
01484 const Industry *i;
01485
01486 if (_settings_game.economy.same_industry_close && indspec->IsRawIndustry())
01487
01488 return true;
01489
01490 FOR_ALL_INDUSTRIES(i) {
01491
01492 bool in_low_distance = DistanceMax(tile, i->location.tile) <= 14;
01493
01494
01495 if (in_low_distance &&
01496 !indspec->IsRawIndustry() &&
01497 indspec->accepts_cargo[0] == i->accepts_cargo[0] && (
01498
01499 _game_mode != GM_EDITOR ||
01500 !_settings_game.economy.same_industry_close ||
01501 !_settings_game.economy.multiple_industry_per_town)) {
01502 _error_message = STR_ERROR_INDUSTRY_TOO_CLOSE;
01503 return false;
01504 }
01505
01506
01507 if ((i->type == indspec->conflicting[0] ||
01508 i->type == indspec->conflicting[1] ||
01509 i->type == indspec->conflicting[2]) &&
01510 in_low_distance) {
01511 _error_message = STR_ERROR_INDUSTRY_TOO_CLOSE;
01512 return false;
01513 }
01514 }
01515 return true;
01516 }
01517
01521 enum ProductionLevels {
01522 PRODLEVEL_CLOSURE = 0x00,
01523 PRODLEVEL_MINIMUM = 0x04,
01524 PRODLEVEL_DEFAULT = 0x10,
01525 PRODLEVEL_MAXIMUM = 0x80,
01526 };
01527
01528 static void DoCreateNewIndustry(Industry *i, TileIndex tile, int type, const IndustryTileTable *it, byte layout, const Town *t, Owner owner, Owner founder)
01529 {
01530 const IndustrySpec *indspec = GetIndustrySpec(type);
01531 uint32 r;
01532 uint j;
01533
01534 i->location = TileArea(tile, 1, 1);
01535 i->type = type;
01536 IncIndustryTypeCount(type);
01537
01538 i->produced_cargo[0] = indspec->produced_cargo[0];
01539 i->produced_cargo[1] = indspec->produced_cargo[1];
01540 i->accepts_cargo[0] = indspec->accepts_cargo[0];
01541 i->accepts_cargo[1] = indspec->accepts_cargo[1];
01542 i->accepts_cargo[2] = indspec->accepts_cargo[2];
01543 i->production_rate[0] = indspec->production_rate[0];
01544 i->production_rate[1] = indspec->production_rate[1];
01545
01546
01547 if (_settings_game.economy.smooth_economy &&
01548 !(HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_256_TICKS) || HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) &&
01549 !(HasBit(indspec->callback_mask, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_CHANGE))
01550 ) {
01551 i->production_rate[0] = min((RandomRange(256) + 128) * i->production_rate[0] >> 8, 255);
01552 i->production_rate[1] = min((RandomRange(256) + 128) * i->production_rate[1] >> 8, 255);
01553 }
01554
01555 i->town = t;
01556 i->owner = owner;
01557
01558 r = Random();
01559 i->random_colour = GB(r, 0, 4);
01560 i->counter = GB(r, 4, 12);
01561 i->random = GB(r, 16, 16);
01562 i->produced_cargo_waiting[0] = 0;
01563 i->produced_cargo_waiting[1] = 0;
01564 i->incoming_cargo_waiting[0] = 0;
01565 i->incoming_cargo_waiting[1] = 0;
01566 i->incoming_cargo_waiting[2] = 0;
01567 i->this_month_production[0] = 0;
01568 i->this_month_production[1] = 0;
01569 i->this_month_transported[0] = 0;
01570 i->this_month_transported[1] = 0;
01571 i->last_month_pct_transported[0] = 0;
01572 i->last_month_pct_transported[1] = 0;
01573 i->last_month_transported[0] = 0;
01574 i->last_month_transported[1] = 0;
01575 i->was_cargo_delivered = false;
01576 i->last_prod_year = _cur_year;
01577 i->last_month_production[0] = i->production_rate[0] * 8;
01578 i->last_month_production[1] = i->production_rate[1] * 8;
01579 i->founder = founder;
01580
01581 if (HasBit(indspec->callback_mask, CBM_IND_DECIDE_COLOUR)) {
01582 uint16 res = GetIndustryCallback(CBID_INDUSTRY_DECIDE_COLOUR, 0, 0, i, type, INVALID_TILE);
01583 if (res != CALLBACK_FAILED) i->random_colour = GB(res, 0, 4);
01584 }
01585
01586 if (HasBit(indspec->callback_mask, CBM_IND_INPUT_CARGO_TYPES)) {
01587 for (j = 0; j < lengthof(i->accepts_cargo); j++) i->accepts_cargo[j] = CT_INVALID;
01588 for (j = 0; j < lengthof(i->accepts_cargo); j++) {
01589 uint16 res = GetIndustryCallback(CBID_INDUSTRY_INPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
01590 if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
01591 i->accepts_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
01592 }
01593 }
01594
01595 if (HasBit(indspec->callback_mask, CBM_IND_OUTPUT_CARGO_TYPES)) {
01596 for (j = 0; j < lengthof(i->produced_cargo); j++) i->produced_cargo[j] = CT_INVALID;
01597 for (j = 0; j < lengthof(i->produced_cargo); j++) {
01598 uint16 res = GetIndustryCallback(CBID_INDUSTRY_OUTPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
01599 if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
01600 i->produced_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
01601 }
01602 }
01603
01604 i->construction_date = _date;
01605 i->construction_type = (_game_mode == GM_EDITOR) ? ICT_SCENARIO_EDITOR :
01606 (_generating_world ? ICT_MAP_GENERATION : ICT_NORMAL_GAMEPLAY);
01607
01608
01609
01610
01611 i->selected_layout = layout + 1;
01612
01613 if (!_generating_world) i->last_month_production[0] = i->last_month_production[1] = 0;
01614
01615 i->prod_level = PRODLEVEL_DEFAULT;
01616
01617 do {
01618 TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
01619
01620 if (it->gfx != GFX_WATERTILE_SPECIALCHECK) {
01621 i->location.Add(cur_tile);
01622
01623 WaterClass wc = (IsWaterTile(cur_tile) ? GetWaterClass(cur_tile) : WATER_CLASS_INVALID);
01624
01625 DoCommand(cur_tile, 0, 0, DC_EXEC | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
01626
01627 MakeIndustry(cur_tile, i->index, it->gfx, Random(), wc);
01628
01629 if (_generating_world) {
01630 SetIndustryConstructionCounter(cur_tile, 3);
01631 SetIndustryConstructionStage(cur_tile, 2);
01632 }
01633
01634
01635 IndustryGfx cur_gfx = GetTranslatedIndustryTileID(it->gfx);
01636 const IndustryTileSpec *its = GetIndustryTileSpec(cur_gfx);
01637 if (its->animation_info != 0xFFFF) AddAnimatedTile(cur_tile);
01638 }
01639 } while ((++it)->ti.x != -0x80);
01640
01641 if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
01642 for (j = 0; j != 50; j++) PlantRandomFarmField(i);
01643 }
01644 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
01645
01646 Station::RecomputeIndustriesNearForAll();
01647 }
01648
01659 static Industry *CreateNewIndustryHelper(TileIndex tile, IndustryType type, DoCommandFlag flags, const IndustrySpec *indspec, uint itspec_index, uint32 seed, Owner founder)
01660 {
01661 assert(itspec_index < indspec->num_table);
01662 const IndustryTileTable *it = indspec->table[itspec_index];
01663 bool custom_shape_check = false;
01664
01665 if (!CheckIfIndustryTilesAreFree(tile, it, itspec_index, type, &custom_shape_check)) return NULL;
01666
01667 if (HasBit(GetIndustrySpec(type)->callback_mask, CBM_IND_LOCATION)) {
01668 if (!CheckIfCallBackAllowsCreation(tile, type, itspec_index, seed)) return NULL;
01669 } else {
01670 if (!_check_new_industry_procs[indspec->check_proc](tile)) return NULL;
01671 }
01672
01673 if (!custom_shape_check && _settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !_ignore_restrictions && !CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER, it, type)) return NULL;
01674 if (!CheckIfFarEnoughFromIndustry(tile, type)) return NULL;
01675
01676 const Town *t = CheckMultipleIndustryInTown(tile, type);
01677 if (t == NULL) return NULL;
01678
01679 if (!CheckIfIndustryIsAllowed(tile, type, t)) return NULL;
01680
01681 if (!Industry::CanAllocateItem()) return NULL;
01682
01683 if (flags & DC_EXEC) {
01684 Industry *i = new Industry(tile);
01685 if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER | DC_EXEC, it, type);
01686 DoCreateNewIndustry(i, tile, type, it, itspec_index, t, OWNER_NONE, founder);
01687
01688 return i;
01689 }
01690
01691
01692
01693 return (Industry *)-1;
01694 }
01695
01706 CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01707 {
01708 IndustryType it = GB(p1, 0, 8);
01709 if (it >= NUM_INDUSTRYTYPES) return CMD_ERROR;
01710
01711 const IndustrySpec *indspec = GetIndustrySpec(it);
01712
01713
01714 if (!indspec->enabled || indspec->num_table == 0) return CMD_ERROR;
01715
01716
01717
01718 if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
01719 return CMD_ERROR;
01720 }
01721
01722 const Industry *ind = NULL;
01723 if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indspec->IsRawIndustry()) {
01724 if (flags & DC_EXEC) {
01725
01726 CompanyID founder = _current_company;
01727 _current_company = OWNER_TOWN;
01728
01729
01730
01731 if (Random() <= indspec->prospecting_chance) {
01732 for (int i = 0; i < 5000; i++) {
01733
01734
01735
01736 tile = RandomTile();
01737 ind = CreateNewIndustryHelper(tile, it, flags, indspec, RandomRange(indspec->num_table), p2, founder);
01738 if (ind != NULL) {
01739 break;
01740 }
01741 }
01742 }
01743 _current_company = founder;
01744 }
01745 } else {
01746 int count = indspec->num_table;
01747 const IndustryTileTable * const *itt = indspec->table;
01748 int num = GB(p1, 8, 8);
01749 if (num >= count) return CMD_ERROR;
01750
01751 _error_message = STR_ERROR_SITE_UNSUITABLE;
01752 do {
01753 if (--count < 0) return CMD_ERROR;
01754 if (--num < 0) num = indspec->num_table - 1;
01755 } while (!CheckIfIndustryTilesAreFree(tile, itt[num], num, it));
01756
01757 ind = CreateNewIndustryHelper(tile, it, flags, indspec, num, p2, _current_company);
01758 if (ind == NULL) return CMD_ERROR;
01759 }
01760
01761 if ((flags & DC_EXEC) && _game_mode != GM_EDITOR && ind != NULL) {
01762 SetDParam(0, indspec->name);
01763 if (indspec->new_industry_text > STR_LAST_STRINGID) {
01764 SetDParam(1, STR_TOWN_NAME);
01765 SetDParam(2, ind->town->index);
01766 } else {
01767 SetDParam(1, ind->town->index);
01768 }
01769 AddIndustryNewsItem(indspec->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
01770 AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
01771 }
01772
01773 return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost());
01774 }
01775
01776
01777 static Industry *CreateNewIndustry(TileIndex tile, IndustryType type)
01778 {
01779 const IndustrySpec *indspec = GetIndustrySpec(type);
01780
01781 uint32 seed = Random();
01782 return CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, RandomRange(indspec->num_table), seed, OWNER_NONE);
01783 }
01784
01785 enum {
01786 NB_NUMOFINDUSTRY = 11,
01787 NB_DIFFICULTY_LEVEL = 5,
01788 };
01789
01790 static const byte _numof_industry_table[NB_DIFFICULTY_LEVEL][NB_NUMOFINDUSTRY] = {
01791
01792 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
01793 {0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1},
01794 {0, 1, 1, 1, 2, 2, 3, 3, 4, 4, 5},
01795 {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10},
01796 {0, 2, 3, 4, 6, 7, 8, 9, 10, 10, 10},
01797 };
01798
01803 static void PlaceInitialIndustry(IndustryType type, int amount)
01804 {
01805
01806
01807 int num = (amount > NB_NUMOFINDUSTRY) ? amount : _numof_industry_table[_settings_game.difficulty.number_industries][amount];
01808 const IndustrySpec *ind_spc = GetIndustrySpec(type);
01809
01810
01811 num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
01812
01813 if (_settings_game.difficulty.number_industries != 0) {
01814 CompanyID old_company = _current_company;
01815 _current_company = OWNER_NONE;
01816 assert(num > 0);
01817
01818 do {
01819 uint i;
01820
01821 IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
01822
01823 for (i = 0; i < 2000; i++) {
01824 if (CreateNewIndustry(RandomTile(), type) != NULL) break;
01825 }
01826 } while (--num);
01827
01828 _current_company = old_company;
01829 }
01830 }
01831
01834 void GenerateIndustries()
01835 {
01836 uint i = 0;
01837 uint8 chance;
01838 IndustryType it;
01839 const IndustrySpec *ind_spc;
01840
01841
01842 if (_settings_game.difficulty.number_industries > 0) {
01843 for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
01844
01845 ind_spc = GetIndustrySpec(it);
01846
01847 if (!CheckIfCallBackAllowsAvailability(it, IACT_MAPGENERATION)) {
01848 ResetIndustryCreationProbility(it);
01849 }
01850
01851 chance = ind_spc->appear_creation[_settings_game.game_creation.landscape];
01852 if (ind_spc->enabled && chance > 0 && ind_spc->num_table > 0) {
01853
01854
01855
01856 int num = (chance > NB_NUMOFINDUSTRY) ? chance : _numof_industry_table[_settings_game.difficulty.number_industries][chance];
01857
01858
01859 num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
01860 i += num;
01861 }
01862 }
01863 }
01864
01865 SetGeneratingWorldProgress(GWP_INDUSTRY, i);
01866
01867 if (_settings_game.difficulty.number_industries > 0) {
01868 for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
01869
01870
01871
01872
01873
01874 ind_spc = GetIndustrySpec(it);
01875 if (ind_spc->enabled && ind_spc->num_table > 0) {
01876 chance = ind_spc->appear_creation[_settings_game.game_creation.landscape];
01877 if (chance > 0) PlaceInitialIndustry(it, chance);
01878 }
01879 }
01880 }
01881 }
01882
01883 static void UpdateIndustryStatistics(Industry *i)
01884 {
01885 byte pct;
01886 bool refresh = false;
01887
01888 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
01889 if (i->produced_cargo[j] != CT_INVALID) {
01890 pct = 0;
01891 if (i->this_month_production[j] != 0) {
01892 i->last_prod_year = _cur_year;
01893 pct = min(i->this_month_transported[j] * 256 / i->this_month_production[j], 255);
01894 }
01895 i->last_month_pct_transported[j] = pct;
01896
01897 i->last_month_production[j] = i->this_month_production[j];
01898 i->this_month_production[j] = 0;
01899
01900 i->last_month_transported[j] = i->this_month_transported[j];
01901 i->this_month_transported[j] = 0;
01902 refresh = true;
01903 }
01904 }
01905
01906 if (refresh) SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
01907 }
01908
01910 struct ProbabilityHelper {
01911 uint16 prob;
01912 IndustryType ind;
01913 };
01914
01918 static void MaybeNewIndustry()
01919 {
01920 Industry *ind;
01921 IndustryType rndtype, j;
01922 const IndustrySpec *ind_spc;
01923 uint num = 0;
01924 ProbabilityHelper cumulative_probs[NUM_INDUSTRYTYPES];
01925 uint16 probability_max = 0;
01926
01927
01928 for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
01929 ind_spc = GetIndustrySpec(j);
01930 byte chance = ind_spc->appear_ingame[_settings_game.game_creation.landscape];
01931
01932 if (!ind_spc->enabled || chance == 0 || ind_spc->num_table == 0) continue;
01933
01934
01935
01936 if (CheckIfCallBackAllowsAvailability(j, IACT_RANDOMCREATION)) {
01937 probability_max += chance;
01938
01939 cumulative_probs[num].ind = j;
01940 cumulative_probs[num++].prob = probability_max;
01941 }
01942 }
01943
01944
01945 if (probability_max == 0) return;
01946
01947
01948 rndtype = RandomRange(probability_max);
01949 for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
01950
01951 if (cumulative_probs[j].prob >= rndtype) break;
01952 }
01953
01954 ind_spc = GetIndustrySpec(cumulative_probs[j].ind);
01955
01956 if ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) return;
01957 if ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) return;
01958
01959
01960 num = 2000;
01961 for (;;) {
01962 ind = CreateNewIndustry(RandomTile(), cumulative_probs[j].ind);
01963 if (ind != NULL) break;
01964 if (--num == 0) return;
01965 }
01966
01967 SetDParam(0, ind_spc->name);
01968 if (ind_spc->new_industry_text > STR_LAST_STRINGID) {
01969 SetDParam(1, STR_TOWN_NAME);
01970 SetDParam(2, ind->town->index);
01971 } else {
01972 SetDParam(1, ind->town->index);
01973 }
01974 AddIndustryNewsItem(ind_spc->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
01975 AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
01976 }
01977
01986 static bool CheckIndustryCloseDownProtection(IndustryType type)
01987 {
01988 const IndustrySpec *indspec = GetIndustrySpec(type);
01989
01990
01991 if ((indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE) return false;
01992 return (indspec->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) == 0 && GetIndustryTypeCount(type) <= 1;
01993 }
01994
02004 static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces)
02005 {
02006 const IndustrySpec *indspec = GetIndustrySpec(ind->type);
02007
02008
02009 for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) {
02010 if (ind->accepts_cargo[j] == CT_INVALID) continue;
02011 if (cargo == ind->accepts_cargo[j]) {
02012 if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) {
02013 uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO,
02014 0, GetReverseCargoTranslation(cargo, indspec->grf_prop.grffile),
02015 ind, ind->type, ind->location.tile);
02016 if (res == 0) continue;
02017 }
02018 *c_accepts = true;
02019 break;
02020 }
02021 }
02022
02023
02024 for (byte j = 0; j < lengthof(ind->produced_cargo); j++) {
02025 if (ind->produced_cargo[j] == CT_INVALID) continue;
02026 if (cargo == ind->produced_cargo[j]) {
02027 *c_produces = true;
02028 break;
02029 }
02030 }
02031 }
02032
02046 static int WhoCanServiceIndustry(Industry *ind)
02047 {
02048
02049 StationList stations;
02050 FindStationsAroundTiles(ind->location, &stations);
02051
02052 if (stations.Length() == 0) return 0;
02053
02054 const Vehicle *v;
02055 int result = 0;
02056 FOR_ALL_VEHICLES(v) {
02057
02058 if (v->owner != _local_company && result != 0) continue;
02059
02060
02061 bool c_accepts = false;
02062 bool c_produces = false;
02063 if (v->type == VEH_TRAIN && Train::From(v)->IsFrontEngine()) {
02064 for (const Vehicle *u = v; u != NULL; u = u->Next()) {
02065 CanCargoServiceIndustry(u->cargo_type, ind, &c_accepts, &c_produces);
02066 }
02067 } else if (v->type == VEH_ROAD || v->type == VEH_SHIP || v->type == VEH_AIRCRAFT) {
02068 CanCargoServiceIndustry(v->cargo_type, ind, &c_accepts, &c_produces);
02069 } else {
02070 continue;
02071 }
02072 if (!c_accepts && !c_produces) continue;
02073
02074
02075
02076
02077
02078 const Order *o;
02079 FOR_VEHICLE_ORDERS(v, o) {
02080 if (o->IsType(OT_GOTO_STATION) && !(o->GetUnloadType() & OUFB_TRANSFER)) {
02081
02082 Station *st = Station::Get(o->GetDestination());
02083 assert(st != NULL);
02084
02085
02086 if ((o->GetUnloadType() & OUFB_UNLOAD) && !c_accepts) break;
02087
02088 if (stations.Contains(st)) {
02089 if (v->owner == _local_company) return 2;
02090 result = 1;
02091 }
02092 }
02093 }
02094 }
02095 return result;
02096 }
02097
02105 static void ReportNewsProductionChangeIndustry(Industry *ind, CargoID type, int percent)
02106 {
02107 NewsSubtype ns;
02108
02109 switch (WhoCanServiceIndustry(ind)) {
02110 case 0: ns = NS_INDUSTRY_NOBODY; break;
02111 case 1: ns = NS_INDUSTRY_OTHER; break;
02112 case 2: ns = NS_INDUSTRY_COMPANY; break;
02113 default: NOT_REACHED();
02114 }
02115 SetDParam(2, abs(percent));
02116 SetDParam(0, CargoSpec::Get(type)->name);
02117 SetDParam(1, ind->index);
02118 AddIndustryNewsItem(
02119 percent >= 0 ? STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_SMOOTH : STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_SMOOTH,
02120 ns,
02121 ind->index
02122 );
02123 }
02124
02125 enum {
02126 PERCENT_TRANSPORTED_60 = 153,
02127 PERCENT_TRANSPORTED_80 = 204,
02128 };
02129
02134 static void ChangeIndustryProduction(Industry *i, bool monthly)
02135 {
02136 StringID str = STR_NULL;
02137 bool closeit = false;
02138 const IndustrySpec *indspec = GetIndustrySpec(i->type);
02139 bool standard = false;
02140 bool suppress_message = false;
02141 bool recalculate_multipliers = false;
02142
02143 bool smooth_economy = _settings_game.economy.smooth_economy &&
02144 !(HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_256_TICKS) || HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) &&
02145 !(HasBit(indspec->callback_mask, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_CHANGE));
02146 byte div = 0;
02147 byte mul = 0;
02148 int8 increment = 0;
02149
02150 bool callback_enabled = HasBit(indspec->callback_mask, monthly ? CBM_IND_MONTHLYPROD_CHANGE : CBM_IND_PRODUCTION_CHANGE);
02151 if (callback_enabled) {
02152 uint16 res = GetIndustryCallback(monthly ? CBID_INDUSTRY_MONTHLYPROD_CHANGE : CBID_INDUSTRY_PRODUCTION_CHANGE, 0, Random(), i, i->type, i->location.tile);
02153 if (res != CALLBACK_FAILED) {
02154 suppress_message = HasBit(res, 7);
02155
02156 if (HasBit(res, 8)) str = MapGRFStringID(indspec->grf_prop.grffile->grfid, GB(GetRegister(0x100), 0, 16));
02157 res = GB(res, 0, 4);
02158 switch (res) {
02159 default: NOT_REACHED();
02160 case 0x0: break;
02161 case 0x1: div = 1; break;
02162 case 0x2: mul = 1; break;
02163 case 0x3: closeit = true; break;
02164 case 0x4: standard = true; break;
02165 case 0x5: case 0x6: case 0x7:
02166 case 0x8: div = res - 0x3; break;
02167 case 0x9: case 0xA: case 0xB:
02168 case 0xC: mul = res - 0x7; break;
02169 case 0xD:
02170 case 0xE:
02171 increment = res == 0x0D ? -1 : 1;
02172 break;
02173 case 0xF:
02174 i->prod_level = Clamp(GB(GetRegister(0x100), 16, 8), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
02175 recalculate_multipliers = true;
02176 break;
02177 }
02178 }
02179 } else {
02180 if (monthly != smooth_economy) return;
02181 if (indspec->life_type == INDUSTRYLIFE_BLACK_HOLE) return;
02182 }
02183
02184 if (standard || (!callback_enabled && (indspec->life_type & (INDUSTRYLIFE_ORGANIC | INDUSTRYLIFE_EXTRACTIVE)) != 0)) {
02185
02186 bool only_decrease = (indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE;
02187
02188 if (smooth_economy) {
02189 closeit = true;
02190 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
02191 if (i->produced_cargo[j] == CT_INVALID) continue;
02192 uint32 r = Random();
02193 int old_prod, new_prod, percent;
02194
02195 int mult = (i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_60) ? 1 : -1;
02196
02197 new_prod = old_prod = i->production_rate[j];
02198
02199
02200
02201 if (only_decrease) {
02202 mult = -1;
02203
02204
02205 } else if (Chance16I(1, ((i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_80) ? 6 : 3), r)) {
02206 mult *= -1;
02207 }
02208
02209
02210
02211 if (Chance16I(1, 22, r >> 16)) {
02212 new_prod += mult * (max(((RandomRange(50) + 10) * old_prod) >> 8, 1U));
02213 }
02214
02215
02216 new_prod = Clamp(new_prod, 1, 255);
02217
02218 if (((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0) && j == 1)
02219 new_prod = Clamp(new_prod, 0, 16);
02220
02221
02222 if (new_prod == old_prod && old_prod > 1) {
02223 closeit = false;
02224 continue;
02225 }
02226
02227 percent = (old_prod == 0) ? 100 : (new_prod * 100 / old_prod - 100);
02228 i->production_rate[j] = new_prod;
02229
02230
02231 if (new_prod > 1) closeit = false;
02232
02233 if (abs(percent) >= 10) {
02234 ReportNewsProductionChangeIndustry(i, i->produced_cargo[j], percent);
02235 }
02236 }
02237 } else {
02238 if (only_decrease || Chance16(1, 3)) {
02239
02240 if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != Chance16(1, 3)) {
02241 mul = 1;
02242 } else {
02243 div = 1;
02244 }
02245 }
02246 }
02247 }
02248
02249 if (!callback_enabled && (indspec->life_type & INDUSTRYLIFE_PROCESSING)) {
02250 if ( (byte)(_cur_year - i->last_prod_year) >= 5 && Chance16(1, smooth_economy ? 180 : 2)) {
02251 closeit = true;
02252 }
02253 }
02254
02255
02256 while (mul-- != 0 && i->prod_level < PRODLEVEL_MAXIMUM) {
02257 i->prod_level = min(i->prod_level * 2, PRODLEVEL_MAXIMUM);
02258 recalculate_multipliers = true;
02259 if (str == STR_NULL) str = indspec->production_up_text;
02260 }
02261
02262
02263 while (div-- != 0 && !closeit) {
02264 if (i->prod_level == PRODLEVEL_MINIMUM) {
02265 closeit = true;
02266 } else {
02267 i->prod_level = max(i->prod_level / 2, (int)PRODLEVEL_MINIMUM);
02268 recalculate_multipliers = true;
02269 if (str == STR_NULL) str = indspec->production_down_text;
02270 }
02271 }
02272
02273
02274 if (increment != 0) {
02275 if (increment < 0 && i->prod_level == PRODLEVEL_MINIMUM) {
02276 closeit = true;
02277 } else {
02278 i->prod_level = ClampU(i->prod_level + increment, PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
02279 recalculate_multipliers = true;
02280 }
02281 }
02282
02283
02284
02285 if (recalculate_multipliers) {
02286
02287 i->production_rate[0] = min((indspec->production_rate[0] * i->prod_level + PRODLEVEL_DEFAULT - 1) / PRODLEVEL_DEFAULT, 0xFF);
02288 i->production_rate[1] = min((indspec->production_rate[1] * i->prod_level + PRODLEVEL_DEFAULT - 1) / PRODLEVEL_DEFAULT, 0xFF);
02289 }
02290
02291
02292 if (closeit && !CheckIndustryCloseDownProtection(i->type)) {
02293 i->prod_level = PRODLEVEL_CLOSURE;
02294 str = indspec->closure_text;
02295 }
02296
02297 if (!suppress_message && str != STR_NULL) {
02298 NewsSubtype ns;
02299
02300 if (closeit) {
02301 ns = NS_INDUSTRY_CLOSE;
02302 AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
02303 } else {
02304 switch (WhoCanServiceIndustry(i)) {
02305 case 0: ns = NS_INDUSTRY_NOBODY; break;
02306 case 1: ns = NS_INDUSTRY_OTHER; break;
02307 case 2: ns = NS_INDUSTRY_COMPANY; break;
02308 default: NOT_REACHED();
02309 }
02310 }
02311
02312 if (str > STR_LAST_STRINGID) {
02313 SetDParam(0, STR_TOWN_NAME);
02314 SetDParam(1, i->town->index);
02315 SetDParam(2, indspec->name);
02316 } else if (closeit) {
02317 SetDParam(0, STR_FORMAT_INDUSTRY_NAME);
02318 SetDParam(1, i->town->index);
02319 SetDParam(2, indspec->name);
02320 } else {
02321 SetDParam(0, i->index);
02322 }
02323
02324 AddNewsItem(str,
02325 ns,
02326 closeit ? NR_TILE : NR_INDUSTRY,
02327 closeit ? i->location.tile + TileDiffXY(1, 1) : i->index);
02328 }
02329 }
02330
02336 void IndustryDailyLoop()
02337 {
02338 _economy.industry_daily_change_counter += _economy.industry_daily_increment;
02339
02340
02341
02342
02343 uint16 change_loop = _economy.industry_daily_change_counter >> 16;
02344
02345
02346 _economy.industry_daily_change_counter &= 0xFFFF;
02347
02348 if (change_loop == 0) {
02349 return;
02350 }
02351
02352 CompanyID old_company = _current_company;
02353 _current_company = OWNER_NONE;
02354
02355
02356 for (uint16 j = 0; j < change_loop; j++) {
02357
02358 if (Chance16(3, 100)) {
02359 MaybeNewIndustry();
02360 } else {
02361 Industry *i = Industry::GetRandom();
02362 if (i != NULL) ChangeIndustryProduction(i, false);
02363 }
02364 }
02365
02366 _current_company = old_company;
02367
02368
02369 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
02370 }
02371
02372 void IndustryMonthlyLoop()
02373 {
02374 Industry *i;
02375 CompanyID old_company = _current_company;
02376 _current_company = OWNER_NONE;
02377
02378 FOR_ALL_INDUSTRIES(i) {
02379 UpdateIndustryStatistics(i);
02380 if (i->prod_level == PRODLEVEL_CLOSURE) {
02381 delete i;
02382 } else {
02383 ChangeIndustryProduction(i, true);
02384 }
02385 }
02386
02387 _current_company = old_company;
02388
02389
02390 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
02391 }
02392
02393
02394 void InitializeIndustries()
02395 {
02396 _industry_pool.CleanPool();
02397
02398 ResetIndustryCounts();
02399 _industry_sound_tile = 0;
02400 }
02401
02402 bool IndustrySpec::IsRawIndustry() const
02403 {
02404
02405 return (this->life_type & (INDUSTRYLIFE_EXTRACTIVE | INDUSTRYLIFE_ORGANIC)) != 0 &&
02406 (this->behaviour & INDUSTRYBEH_CUT_TREES) == 0;
02407 }
02408
02409 Money IndustrySpec::GetConstructionCost() const
02410 {
02411
02412 return (_price[(_settings_game.construction.raw_industry_construction == 1 && this->IsRawIndustry()) ?
02413 PR_BUILD_INDUSTRY_RAW : PR_BUILD_INDUSTRY] * this->cost_multiplier) >> 8;
02414 }
02415
02416 Money IndustrySpec::GetRemovalCost() const
02417 {
02418 return (_price[PR_CLEAR_INDUSTRY] * this->removal_cost_multiplier) >> 8;
02419 }
02420
02421 static CommandCost TerraformTile_Industry(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
02422 {
02423 if (AutoslopeEnabled()) {
02424
02425
02426
02427
02428
02429
02430 Slope tileh_old = GetTileSlope(tile, NULL);
02431
02432 if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
02433 const IndustryGfx gfx = GetIndustryGfx(tile);
02434 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
02435
02436
02437 if (HasBit(itspec->callback_mask, CBM_INDT_AUTOSLOPE)) {
02438
02439 uint16 res = GetIndustryTileCallback(CBID_INDUSTRY_AUTOSLOPE, 0, 0, gfx, Industry::GetByTile(tile), tile);
02440 if ((res == 0) || (res == CALLBACK_FAILED)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
02441 } else {
02442
02443 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
02444 }
02445 }
02446 }
02447 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
02448 }
02449
02450 extern const TileTypeProcs _tile_type_industry_procs = {
02451 DrawTile_Industry,
02452 GetSlopeZ_Industry,
02453 ClearTile_Industry,
02454 AddAcceptedCargo_Industry,
02455 GetTileDesc_Industry,
02456 GetTileTrackStatus_Industry,
02457 ClickTile_Industry,
02458 AnimateTile_Industry,
02459 TileLoop_Industry,
02460 ChangeTileOwner_Industry,
02461 NULL,
02462 NULL,
02463 GetFoundation_Industry,
02464 TerraformTile_Industry,
02465 };