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 "landscape.h"
00018 #include "viewport_func.h"
00019 #include "command_func.h"
00020 #include "town.h"
00021 #include "news_func.h"
00022 #include "variables.h"
00023 #include "cheat_type.h"
00024 #include "genworld.h"
00025 #include "tree_map.h"
00026 #include "newgrf.h"
00027 #include "newgrf_cargo.h"
00028 #include "newgrf_commons.h"
00029 #include "newgrf_industries.h"
00030 #include "newgrf_industrytiles.h"
00031 #include "autoslope.h"
00032 #include "water.h"
00033 #include "strings_func.h"
00034 #include "functions.h"
00035 #include "window_func.h"
00036 #include "date_func.h"
00037 #include "vehicle_func.h"
00038 #include "sound_func.h"
00039 #include "animated_tile_func.h"
00040 #include "effectvehicle_func.h"
00041 #include "effectvehicle_base.h"
00042 #include "ai/ai.hpp"
00043 #include "core/pool_func.hpp"
00044 #include "subsidy_func.h"
00045
00046 #include "table/strings.h"
00047 #include "table/industry_land.h"
00048 #include "table/build_industry.h"
00049
00050 IndustryPool _industry_pool("Industry");
00051 INSTANTIATE_POOL_METHODS(Industry)
00052
00053 void ShowIndustryViewWindow(int industry);
00054 void BuildOilRig(TileIndex tile);
00055
00056 static byte _industry_sound_ctr;
00057 static TileIndex _industry_sound_tile;
00058
00059 uint16 _industry_counts[NUM_INDUSTRYTYPES];
00060
00061 IndustrySpec _industry_specs[NUM_INDUSTRYTYPES];
00062 IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES];
00063
00068 void ResetIndustries()
00069 {
00070 memset(&_industry_specs, 0, sizeof(_industry_specs));
00071 memcpy(&_industry_specs, &_origin_industry_specs, sizeof(_origin_industry_specs));
00072
00073
00074 for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
00075 _industry_specs[i].enabled = i < NEW_INDUSTRYOFFSET &&
00076 HasBit(_origin_industry_specs[i].climate_availability, _settings_game.game_creation.landscape);
00077 }
00078
00079 memset(&_industry_tile_specs, 0, sizeof(_industry_tile_specs));
00080 memcpy(&_industry_tile_specs, &_origin_industry_tile_specs, sizeof(_origin_industry_tile_specs));
00081
00082
00083 _industile_mngr.ResetOverride();
00084 _industry_mngr.ResetOverride();
00085 }
00086
00095 IndustryType GetIndustryType(TileIndex tile)
00096 {
00097 assert(IsTileType(tile, MP_INDUSTRY));
00098
00099 const Industry *ind = Industry::GetByTile(tile);
00100 assert(ind != NULL);
00101 return ind->type;
00102 }
00103
00112 const IndustrySpec *GetIndustrySpec(IndustryType thistype)
00113 {
00114 assert(thistype < NUM_INDUSTRYTYPES);
00115 return &_industry_specs[thistype];
00116 }
00117
00126 const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
00127 {
00128 assert(gfx < INVALID_INDUSTRYTILE);
00129 return &_industry_tile_specs[gfx];
00130 }
00131
00132 Industry::~Industry()
00133 {
00134 if (CleaningPool()) return;
00135
00136
00137
00138 if (this->location.w == 0) return;
00139
00140 TILE_AREA_LOOP(tile_cur, this->location) {
00141 if (IsTileType(tile_cur, MP_INDUSTRY)) {
00142 if (GetIndustryIndex(tile_cur) == this->index) {
00143
00144 MakeWaterKeepingClass(tile_cur, OWNER_NONE);
00145
00146
00147
00148 DeleteAnimatedTile(tile_cur);
00149
00150 MarkTileDirtyByTile(tile_cur);
00151 }
00152 } else if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
00153 DeleteOilRig(tile_cur);
00154 }
00155 }
00156
00157 if (GetIndustrySpec(this->type)->behaviour & INDUSTRYBEH_PLANT_FIELDS) {
00158
00159 TILE_LOOP(tile_cur, 42, 42, this->location.tile - TileDiffXY(21, 21)) {
00160 tile_cur = TILE_MASK(tile_cur);
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 DecIndustryTypeCount(this->type);
00172
00173 DeleteIndustryNews(this->index);
00174 DeleteWindowById(WC_INDUSTRY_VIEW, this->index);
00175
00176 DeleteSubsidyWith(ST_INDUSTRY, this->index);
00177 CargoPacket::InvalidateAllFrom(ST_INDUSTRY, this->index);
00178 }
00179
00184 void Industry::PostDestructor(size_t index)
00185 {
00186 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
00187 Station::RecomputeIndustriesNearForAll();
00188 }
00189
00190
00195 Industry *Industry::GetRandom()
00196 {
00197 if (Industry::GetNumItems() == 0) return NULL;
00198 int num = RandomRange((uint16)Industry::GetNumItems());
00199 size_t index = MAX_UVALUE(size_t);
00200
00201 while (num >= 0) {
00202 num--;
00203 index++;
00204
00205
00206 while (!Industry::IsValidID(index)) {
00207 index++;
00208 assert(index < Industry::GetPoolSize());
00209 }
00210 }
00211
00212 return Industry::Get(index);
00213 }
00214
00215
00216 static void IndustryDrawSugarMine(const TileInfo *ti)
00217 {
00218 const DrawIndustryAnimationStruct *d;
00219
00220 if (!IsIndustryCompleted(ti->tile)) return;
00221
00222 d = &_draw_industry_spec1[GetIndustryAnimationState(ti->tile)];
00223
00224 AddChildSpriteScreen(SPR_IT_SUGAR_MINE_SIEVE + d->image_1, PAL_NONE, d->x, 0);
00225
00226 if (d->image_2 != 0) {
00227 AddChildSpriteScreen(SPR_IT_SUGAR_MINE_CLOUDS + d->image_2 - 1, PAL_NONE, 8, 41);
00228 }
00229
00230 if (d->image_3 != 0) {
00231 AddChildSpriteScreen(SPR_IT_SUGAR_MINE_PILE + d->image_3 - 1, PAL_NONE,
00232 _drawtile_proc1[d->image_3 - 1].x, _drawtile_proc1[d->image_3 - 1].y);
00233 }
00234 }
00235
00236 static void IndustryDrawToffeeQuarry(const TileInfo *ti)
00237 {
00238 uint8 x = 0;
00239
00240 if (IsIndustryCompleted(ti->tile)) {
00241 x = _industry_anim_offs_toffee[GetIndustryAnimationState(ti->tile)];
00242 if (x == 0xFF)
00243 x = 0;
00244 }
00245
00246 AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_SHOVEL, PAL_NONE, 22 - x, 24 + x);
00247 AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_TOFFEE, PAL_NONE, 6, 14);
00248 }
00249
00250 static void IndustryDrawBubbleGenerator( const TileInfo *ti)
00251 {
00252 if (IsIndustryCompleted(ti->tile)) {
00253 AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_BUBBLE, PAL_NONE, 5, _industry_anim_offs_bubbles[GetIndustryAnimationState(ti->tile)]);
00254 } else {
00255 AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_SPRING, PAL_NONE, 3, 67);
00256 }
00257 }
00258
00259 static void IndustryDrawToyFactory(const TileInfo *ti)
00260 {
00261 const DrawIndustryAnimationStruct *d;
00262
00263 d = &_industry_anim_offs_toys[GetIndustryAnimationState(ti->tile)];
00264
00265 if (d->image_1 != 0xFF) {
00266 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_CLAY, PAL_NONE, d->x, 96 + d->image_1);
00267 }
00268
00269 if (d->image_2 != 0xFF) {
00270 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_ROBOT, PAL_NONE, 16 - d->image_2 * 2, 100 + d->image_2);
00271 }
00272
00273 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP, PAL_NONE, 7, d->image_3);
00274 AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP_HOLDER, PAL_NONE, 0, 42);
00275 }
00276
00277 static void IndustryDrawCoalPlantSparks(const TileInfo *ti)
00278 {
00279 if (IsIndustryCompleted(ti->tile)) {
00280 uint8 image = GetIndustryAnimationState(ti->tile);
00281
00282 if (image != 0 && image < 7) {
00283 AddChildSpriteScreen(image + SPR_IT_POWER_PLANT_TRANSFORMERS,
00284 PAL_NONE,
00285 _coal_plant_sparks[image - 1].x,
00286 _coal_plant_sparks[image - 1].y
00287 );
00288 }
00289 }
00290 }
00291
00292 typedef void IndustryDrawTileProc(const TileInfo *ti);
00293 static IndustryDrawTileProc * const _industry_draw_tile_procs[5] = {
00294 IndustryDrawSugarMine,
00295 IndustryDrawToffeeQuarry,
00296 IndustryDrawBubbleGenerator,
00297 IndustryDrawToyFactory,
00298 IndustryDrawCoalPlantSparks,
00299 };
00300
00301 static void DrawTile_Industry(TileInfo *ti)
00302 {
00303 IndustryGfx gfx = GetIndustryGfx(ti->tile);
00304 Industry *ind = Industry::GetByTile(ti->tile);
00305 const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
00306 const DrawBuildingsTileStruct *dits;
00307
00308
00309 if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00310
00311
00312
00313
00314 if (indts->grf_prop.spritegroup != 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 dits = &_industry_draw_tile_data[gfx << 2 | (indts->anim_state ?
00328 GetIndustryAnimationState(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 && IsIndustryTileOnWater(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 uint GetSlopeZ_Industry(TileIndex tile, uint x, uint y)
00369 {
00370 return GetTileMaxZ(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 != NULL && HasBit(indts->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
00384 uint32 callback_res = GetIndustryTileCallback(CBID_INDUSTRY_DRAW_FOUNDATIONS, 0, 0, gfx, Industry::GetByTile(tile), tile);
00385 if (callback_res == 0) 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)->name;
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_UNMOVABLE_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
00482 }
00483
00484 if (flags & DC_EXEC) {
00485 AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
00486 delete i;
00487 }
00488 return CommandCost(EXPENSES_CONSTRUCTION, indspec->GetRemovalCost());
00489 }
00490
00491 static void TransportIndustryGoods(TileIndex tile)
00492 {
00493 Industry *i = Industry::GetByTile(tile);
00494 const IndustrySpec *indspec = GetIndustrySpec(i->type);
00495 bool moved_cargo = false;
00496
00497 StationFinder stations(i->location);
00498
00499 for (uint j = 0; j < lengthof(i->produced_cargo_waiting); j++) {
00500 uint cw = min(i->produced_cargo_waiting[j], 255);
00501 if (cw > indspec->minimal_cargo && i->produced_cargo[j] != CT_INVALID) {
00502 i->produced_cargo_waiting[j] -= cw;
00503
00504
00505 if (_economy.fluct <= 0) cw = (cw + 1) / 2;
00506
00507 i->this_month_production[j] += cw;
00508
00509 uint am = MoveGoodsToStation(i->produced_cargo[j], cw, ST_INDUSTRY, i->index, stations.GetStations());
00510 i->this_month_transported[j] += am;
00511
00512 moved_cargo |= (am != 0);
00513 }
00514 }
00515
00516 if (moved_cargo && !StartStopIndustryTileAnimation(i, IAT_INDUSTRY_DISTRIBUTES_CARGO)) {
00517 uint newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_production;
00518
00519 if (newgfx != INDUSTRYTILE_NOANIM) {
00520 ResetIndustryConstructionStage(tile);
00521 SetIndustryCompleted(tile, true);
00522 SetIndustryGfx(tile, newgfx);
00523 MarkTileDirtyByTile(tile);
00524 }
00525 }
00526 }
00527
00528
00529 static void AnimateTile_Industry(TileIndex tile)
00530 {
00531 byte m;
00532 IndustryGfx gfx = GetIndustryGfx(tile);
00533
00534 if (GetIndustryTileSpec(gfx)->animation_info != 0xFFFF) {
00535 AnimateNewIndustryTile(tile);
00536 return;
00537 }
00538
00539 switch (gfx) {
00540 case GFX_SUGAR_MINE_SIEVE:
00541 if ((_tick_counter & 1) == 0) {
00542 m = GetIndustryAnimationState(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 SetIndustryAnimationState(tile, m);
00554
00555 MarkTileDirtyByTile(tile);
00556 }
00557 break;
00558
00559 case GFX_TOFFEE_QUARY:
00560 if ((_tick_counter & 3) == 0) {
00561 m = GetIndustryAnimationState(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 SetIndustryAnimationState(tile, m);
00572
00573 MarkTileDirtyByTile(tile);
00574 }
00575 break;
00576
00577 case GFX_BUBBLE_CATCHER:
00578 if ((_tick_counter & 1) == 0) {
00579 m = GetIndustryAnimationState(tile);
00580
00581 if (++m >= 40) {
00582 m = 0;
00583 DeleteAnimatedTile(tile);
00584 }
00585 SetIndustryAnimationState(tile, m);
00586
00587 MarkTileDirtyByTile(tile);
00588 }
00589 break;
00590
00591
00592 case GFX_POWERPLANT_SPARKS:
00593 if ((_tick_counter & 3) == 0) {
00594 m = GetIndustryAnimationState(tile);
00595 if (m == 6) {
00596 SetIndustryAnimationState(tile, 0);
00597 DeleteAnimatedTile(tile);
00598 } else {
00599 SetIndustryAnimationState(tile, m + 1);
00600 MarkTileDirtyByTile(tile);
00601 }
00602 }
00603 break;
00604
00605 case GFX_TOY_FACTORY:
00606 if ((_tick_counter & 1) == 0) {
00607 m = GetIndustryAnimationState(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 SetIndustryAnimationState(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 m = GetIndustryAnimationState(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 SetIndustryAnimationState(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)
00669 return;
00670
00671 if (state < 0x1A0) {
00672 if (state < 0x20 || state >= 0x180) {
00673 m = GetIndustryAnimationState(tile);
00674 if (!(m & 0x40)) {
00675 SetIndustryAnimationState(tile, m | 0x40);
00676 SndPlayTileFx(SND_0B_MINING_MACHINERY, tile);
00677 }
00678 if (state & 7)
00679 return;
00680 } else {
00681 if (state & 3)
00682 return;
00683 }
00684 m = (GetIndustryAnimationState(tile) + 1) | 0x40;
00685 if (m > 0xC2) m = 0xC0;
00686 SetIndustryAnimationState(tile, m);
00687 MarkTileDirtyByTile(tile);
00688 } else if (state >= 0x200 && state < 0x3A0) {
00689 int i;
00690 i = (state < 0x220 || state >= 0x380) ? 7 : 3;
00691 if (state & i)
00692 return;
00693
00694 m = (GetIndustryAnimationState(tile) & 0xBF) - 1;
00695 if (m < 0x80) m = 0x82;
00696 SetIndustryAnimationState(tile, m);
00697 MarkTileDirtyByTile(tile);
00698 }
00699 } break;
00700 }
00701 }
00702
00703 static void CreateChimneySmoke(TileIndex tile)
00704 {
00705 uint x = TileX(tile) * TILE_SIZE;
00706 uint y = TileY(tile) * TILE_SIZE;
00707 uint z = GetTileMaxZ(tile);
00708
00709 CreateEffectVehicle(x + 15, y + 14, z + 59, EV_CHIMNEY_SMOKE);
00710 }
00711
00712 static void MakeIndustryTileBigger(TileIndex tile)
00713 {
00714 byte cnt = GetIndustryConstructionCounter(tile) + 1;
00715 byte stage;
00716
00717 if (cnt != 4) {
00718 SetIndustryConstructionCounter(tile, cnt);
00719 return;
00720 }
00721
00722 stage = GetIndustryConstructionStage(tile) + 1;
00723 SetIndustryConstructionCounter(tile, 0);
00724 SetIndustryConstructionStage(tile, stage);
00725 StartStopIndustryTileAnimation(tile, IAT_CONSTRUCTION_STATE_CHANGE);
00726 if (stage == INDUSTRY_COMPLETED) SetIndustryCompleted(tile, true);
00727
00728 MarkTileDirtyByTile(tile);
00729
00730 if (!IsIndustryCompleted(tile)) return;
00731
00732 IndustryGfx gfx = GetIndustryGfx(tile);
00733 if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00734
00735 return;
00736 }
00737
00738 switch (gfx) {
00739 case GFX_POWERPLANT_CHIMNEY:
00740 CreateChimneySmoke(tile);
00741 break;
00742
00743 case GFX_OILRIG_1: {
00744
00745
00746
00747
00748 TileIndex other = tile + TileDiffXY(0, 1);
00749
00750 if (IsTileType(other, MP_INDUSTRY) &&
00751 GetIndustryGfx(other) == GFX_OILRIG_1 &&
00752 GetIndustryIndex(tile) == GetIndustryIndex(other)) {
00753 BuildOilRig(tile);
00754 }
00755 } break;
00756
00757 case GFX_TOY_FACTORY:
00758 case GFX_BUBBLE_CATCHER:
00759 case GFX_TOFFEE_QUARY:
00760 SetIndustryAnimationState(tile, 0);
00761 SetIndustryAnimationLoop(tile, 0);
00762 break;
00763
00764 case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
00765 case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
00766 case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
00767 case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
00768 AddAnimatedTile(tile);
00769 break;
00770 }
00771 }
00772
00773 static void TileLoopIndustry_BubbleGenerator(TileIndex tile)
00774 {
00775 static const int8 _bubble_spawn_location[3][4] = {
00776 { 11, 0, -4, -14 },
00777 { -4, -10, -4, 1 },
00778 { 49, 59, 60, 65 },
00779 };
00780
00781 SndPlayTileFx(SND_2E_EXTRACT_AND_POP, tile);
00782
00783 int dir = Random() & 3;
00784
00785 EffectVehicle *v = CreateEffectVehicleAbove(
00786 TileX(tile) * TILE_SIZE + _bubble_spawn_location[0][dir],
00787 TileY(tile) * TILE_SIZE + _bubble_spawn_location[1][dir],
00788 _bubble_spawn_location[2][dir],
00789 EV_BUBBLE
00790 );
00791
00792 if (v != NULL) v->animation_substate = dir;
00793 }
00794
00795 static void TileLoop_Industry(TileIndex tile)
00796 {
00797 IndustryGfx newgfx;
00798 IndustryGfx gfx;
00799
00800 if (IsIndustryTileOnWater(tile)) TileLoop_Water(tile);
00801
00802
00803
00804
00805
00806 if (!IsTileType(tile, MP_INDUSTRY)) return;
00807
00808 TriggerIndustryTile(tile, INDTILE_TRIGGER_TILE_LOOP);
00809
00810 if (!IsIndustryCompleted(tile)) {
00811 MakeIndustryTileBigger(tile);
00812 return;
00813 }
00814
00815 if (_game_mode == GM_EDITOR) return;
00816
00817 TransportIndustryGoods(tile);
00818
00819 if (StartStopIndustryTileAnimation(tile, IAT_TILELOOP)) return;
00820
00821 newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_next;
00822 if (newgfx != INDUSTRYTILE_NOANIM) {
00823 ResetIndustryConstructionStage(tile);
00824 SetIndustryGfx(tile, newgfx);
00825 MarkTileDirtyByTile(tile);
00826 return;
00827 }
00828
00829 gfx = GetIndustryGfx(tile);
00830
00831 switch (gfx) {
00832 case GFX_COAL_MINE_TOWER_NOT_ANIMATED:
00833 case GFX_COPPER_MINE_TOWER_NOT_ANIMATED:
00834 case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:
00835 if (!(_tick_counter & 0x400) && Chance16(1, 2)) {
00836 switch (gfx) {
00837 case GFX_COAL_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COAL_MINE_TOWER_ANIMATED; break;
00838 case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_ANIMATED; break;
00839 case GFX_GOLD_MINE_TOWER_NOT_ANIMATED: gfx = GFX_GOLD_MINE_TOWER_ANIMATED; break;
00840 }
00841 SetIndustryGfx(tile, gfx);
00842 SetIndustryAnimationState(tile, 0x80);
00843 AddAnimatedTile(tile);
00844 }
00845 break;
00846
00847 case GFX_OILWELL_NOT_ANIMATED:
00848 if (Chance16(1, 6)) {
00849 SetIndustryGfx(tile, GFX_OILWELL_ANIMATED_1);
00850 SetIndustryAnimationState(tile, 0);
00851 AddAnimatedTile(tile);
00852 }
00853 break;
00854
00855 case GFX_COAL_MINE_TOWER_ANIMATED:
00856 case GFX_COPPER_MINE_TOWER_ANIMATED:
00857 case GFX_GOLD_MINE_TOWER_ANIMATED:
00858 if (!(_tick_counter & 0x400)) {
00859 switch (gfx) {
00860 case GFX_COAL_MINE_TOWER_ANIMATED: gfx = GFX_COAL_MINE_TOWER_NOT_ANIMATED; break;
00861 case GFX_COPPER_MINE_TOWER_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_NOT_ANIMATED; break;
00862 case GFX_GOLD_MINE_TOWER_ANIMATED: gfx = GFX_GOLD_MINE_TOWER_NOT_ANIMATED; break;
00863 }
00864 SetIndustryGfx(tile, gfx);
00865 SetIndustryCompleted(tile, true);
00866 SetIndustryConstructionStage(tile, 3);
00867 DeleteAnimatedTile(tile);
00868 }
00869 break;
00870
00871 case GFX_POWERPLANT_SPARKS:
00872 if (Chance16(1, 3)) {
00873 SndPlayTileFx(SND_0C_ELECTRIC_SPARK, tile);
00874 AddAnimatedTile(tile);
00875 }
00876 break;
00877
00878 case GFX_COPPER_MINE_CHIMNEY:
00879 CreateEffectVehicleAbove(TileX(tile) * TILE_SIZE + 6, TileY(tile) * TILE_SIZE + 6, 43, EV_SMOKE);
00880 break;
00881
00882
00883 case GFX_TOY_FACTORY: {
00884 Industry *i = Industry::GetByTile(tile);
00885 if (i->was_cargo_delivered) {
00886 i->was_cargo_delivered = false;
00887 SetIndustryAnimationLoop(tile, 0);
00888 AddAnimatedTile(tile);
00889 }
00890 }
00891 break;
00892
00893 case GFX_BUBBLE_GENERATOR:
00894 TileLoopIndustry_BubbleGenerator(tile);
00895 break;
00896
00897 case GFX_TOFFEE_QUARY:
00898 AddAnimatedTile(tile);
00899 break;
00900
00901 case GFX_SUGAR_MINE_SIEVE:
00902 if (Chance16(1, 3)) AddAnimatedTile(tile);
00903 break;
00904 }
00905 }
00906
00907 static bool ClickTile_Industry(TileIndex tile)
00908 {
00909 ShowIndustryViewWindow(GetIndustryIndex(tile));
00910 return true;
00911 }
00912
00913 static TrackStatus GetTileTrackStatus_Industry(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
00914 {
00915 return 0;
00916 }
00917
00918 static void ChangeTileOwner_Industry(TileIndex tile, Owner old_owner, Owner new_owner)
00919 {
00920
00921 Industry *i = Industry::GetByTile(tile);
00922 if (i->founder == old_owner) i->founder = (new_owner == INVALID_OWNER) ? OWNER_NONE : new_owner;
00923 }
00924
00925 static const byte _plantfarmfield_type[] = {1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6};
00926
00927 static bool IsBadFarmFieldTile(TileIndex tile)
00928 {
00929 switch (GetTileType(tile)) {
00930 case MP_CLEAR: return IsClearGround(tile, CLEAR_FIELDS) || IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
00931 case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
00932 default: return true;
00933 }
00934 }
00935
00936 static bool IsBadFarmFieldTile2(TileIndex tile)
00937 {
00938 switch (GetTileType(tile)) {
00939 case MP_CLEAR: return 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 void SetupFarmFieldFence(TileIndex tile, int size, byte type, Axis direction)
00946 {
00947 do {
00948 tile = TILE_MASK(tile);
00949
00950 if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) {
00951 byte or_ = type;
00952
00953 if (or_ == 1 && Chance16(1, 7)) or_ = 2;
00954
00955 if (direction == AXIS_X) {
00956 SetFenceSE(tile, or_);
00957 } else {
00958 SetFenceSW(tile, or_);
00959 }
00960 }
00961
00962 tile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00963 } while (--size);
00964 }
00965
00966 static void PlantFarmField(TileIndex tile, IndustryID industry)
00967 {
00968 uint size_x, size_y;
00969 uint32 r;
00970 uint count;
00971 uint counter;
00972 uint field_type;
00973 int type;
00974
00975 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
00976 if (GetTileZ(tile) + TILE_HEIGHT * 2 >= GetSnowLine())
00977 return;
00978 }
00979
00980
00981 r = (Random() & 0x303) + 0x404;
00982 if (_settings_game.game_creation.landscape == LT_ARCTIC) r += 0x404;
00983 size_x = GB(r, 0, 8);
00984 size_y = GB(r, 8, 8);
00985
00986
00987 tile -= TileDiffXY(size_x / 2, size_y / 2);
00988
00989 if (TileX(tile) + size_x >= MapSizeX() || TileY(tile) + size_y >= MapSizeY()) return;
00990
00991
00992 count = 0;
00993 TILE_LOOP(cur_tile, size_x, size_y, tile) {
00994 assert(cur_tile < MapSize());
00995 count += IsBadFarmFieldTile(cur_tile);
00996 }
00997 if (count * 2 >= size_x * size_y) return;
00998
00999
01000 r = Random();
01001 counter = GB(r, 5, 3);
01002 field_type = GB(r, 8, 8) * 9 >> 8;
01003
01004
01005 TILE_LOOP(cur_tile, size_x, size_y, tile) {
01006 assert(cur_tile < MapSize());
01007 if (!IsBadFarmFieldTile2(cur_tile)) {
01008 MakeField(cur_tile, field_type, industry);
01009 SetClearCounter(cur_tile, counter);
01010 MarkTileDirtyByTile(cur_tile);
01011 }
01012 }
01013
01014 type = 3;
01015 if (_settings_game.game_creation.landscape != LT_ARCTIC && _settings_game.game_creation.landscape != LT_TROPIC) {
01016 type = _plantfarmfield_type[Random() & 0xF];
01017 }
01018
01019 SetupFarmFieldFence(tile - TileDiffXY(1, 0), size_y, type, AXIS_Y);
01020 SetupFarmFieldFence(tile - TileDiffXY(0, 1), size_x, type, AXIS_X);
01021 SetupFarmFieldFence(tile + TileDiffXY(size_x - 1, 0), size_y, type, AXIS_Y);
01022 SetupFarmFieldFence(tile + TileDiffXY(0, size_y - 1), size_x, type, AXIS_X);
01023 }
01024
01025 void PlantRandomFarmField(const Industry *i)
01026 {
01027 int x = i->location.w / 2 + Random() % 31 - 16;
01028 int y = i->location.h / 2 + Random() % 31 - 16;
01029
01030 TileIndex tile = TileAddWrap(i->location.tile, x, y);
01031
01032 if (tile != INVALID_TILE) PlantFarmField(tile, i->index);
01033 }
01034
01041 static bool SearchLumberMillTrees(TileIndex tile, void *user_data)
01042 {
01043 if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) {
01044 CompanyID old_company = _current_company;
01045
01046
01047 _current_company = OWNER_NONE;
01048 _industry_sound_ctr = 1;
01049 _industry_sound_tile = tile;
01050 SndPlayTileFx(SND_38_CHAINSAW, tile);
01051
01052 DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
01053
01054 _current_company = old_company;
01055 return true;
01056 }
01057 return false;
01058 }
01059
01064 static void ChopLumberMillTrees(Industry *i)
01065 {
01066 TileIndex tile = i->location.tile;
01067
01068 if (!IsIndustryCompleted(tile)) return;
01069
01070 if (CircularTileSearch(&tile, 40, SearchLumberMillTrees, NULL))
01071 i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + 45);
01072 }
01073
01074 static void ProduceIndustryGoods(Industry *i)
01075 {
01076 uint32 r;
01077 uint num;
01078 const IndustrySpec *indsp = GetIndustrySpec(i->type);
01079
01080
01081 if ((i->counter & 0x3F) == 0) {
01082 if (Chance16R(1, 14, r) && (num = indsp->number_of_sounds) != 0) {
01083 SndPlayTileFx(
01084 (SoundFx)(indsp->random_sounds[((r >> 16) * num) >> 16]),
01085 i->location.tile);
01086 }
01087 }
01088
01089 i->counter--;
01090
01091
01092 if ((i->counter & 0xFF) == 0) {
01093 if (HasBit(indsp->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) IndustryProductionCallback(i, 1);
01094
01095 IndustryBehaviour indbehav = indsp->behaviour;
01096 i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + i->production_rate[0]);
01097 i->produced_cargo_waiting[1] = min(0xffff, i->produced_cargo_waiting[1] + i->production_rate[1]);
01098
01099 if ((indbehav & INDUSTRYBEH_PLANT_FIELDS) != 0) {
01100 bool plant;
01101 if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
01102 plant = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, Random(), 0, i, i->type, i->location.tile) != 0);
01103 } else {
01104 plant = Chance16(1, 8);
01105 }
01106
01107 if (plant) PlantRandomFarmField(i);
01108 }
01109 if ((indbehav & INDUSTRYBEH_CUT_TREES) != 0) {
01110 bool cut = ((i->counter & 0x1FF) == 0);
01111 if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
01112 cut = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, 0, 1, i, i->type, i->location.tile) != 0);
01113 }
01114
01115 if (cut) ChopLumberMillTrees(i);
01116 }
01117
01118 TriggerIndustry(i, INDUSTRY_TRIGGER_INDUSTRY_TICK);
01119 StartStopIndustryTileAnimation(i, IAT_INDUSTRY_TICK);
01120 }
01121 }
01122
01123 void OnTick_Industry()
01124 {
01125 Industry *i;
01126
01127 if (_industry_sound_ctr != 0) {
01128 _industry_sound_ctr++;
01129
01130 if (_industry_sound_ctr == 75) {
01131 SndPlayTileFx(SND_37_BALLOON_SQUEAK, _industry_sound_tile);
01132 } else if (_industry_sound_ctr == 160) {
01133 _industry_sound_ctr = 0;
01134 SndPlayTileFx(SND_36_CARTOON_CRASH, _industry_sound_tile);
01135 }
01136 }
01137
01138 if (_game_mode == GM_EDITOR) return;
01139
01140 FOR_ALL_INDUSTRIES(i) {
01141 ProduceIndustryGoods(i);
01142 }
01143 }
01144
01149 static bool CheckNewIndustry_NULL(TileIndex tile)
01150 {
01151 return true;
01152 }
01153
01158 static bool CheckNewIndustry_Forest(TileIndex tile)
01159 {
01160 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
01161 if (GetTileZ(tile) < HighestSnowLine() + TILE_HEIGHT * 2U) {
01162 _error_message = STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED;
01163 return false;
01164 }
01165 }
01166 return true;
01167 }
01168
01173 static bool CheckNewIndustry_OilRefinery(TileIndex tile)
01174 {
01175 if (_game_mode == GM_EDITOR) return true;
01176 if (DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return true;
01177
01178 _error_message = STR_ERROR_CAN_ONLY_BE_POSITIONED;
01179 return false;
01180 }
01181
01182 extern bool _ignore_restrictions;
01183
01188 static bool CheckNewIndustry_OilRig(TileIndex tile)
01189 {
01190 if (_game_mode == GM_EDITOR && _ignore_restrictions) return true;
01191 if (TileHeight(tile) == 0 &&
01192 DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return true;
01193
01194 _error_message = STR_ERROR_CAN_ONLY_BE_POSITIONED;
01195 return false;
01196 }
01197
01202 static bool CheckNewIndustry_Farm(TileIndex tile)
01203 {
01204 if (_settings_game.game_creation.landscape == LT_ARCTIC) {
01205 if (GetTileZ(tile) + TILE_HEIGHT * 2 >= HighestSnowLine()) {
01206 _error_message = STR_ERROR_SITE_UNSUITABLE;
01207 return false;
01208 }
01209 }
01210 return true;
01211 }
01212
01217 static bool CheckNewIndustry_Plantation(TileIndex tile)
01218 {
01219 if (GetTropicZone(tile) == TROPICZONE_DESERT) {
01220 _error_message = STR_ERROR_SITE_UNSUITABLE;
01221 return false;
01222 }
01223
01224 return true;
01225 }
01226
01231 static bool CheckNewIndustry_Water(TileIndex tile)
01232 {
01233 if (GetTropicZone(tile) != TROPICZONE_DESERT) {
01234 _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT;
01235 return false;
01236 }
01237
01238 return true;
01239 }
01240
01245 static bool CheckNewIndustry_Lumbermill(TileIndex tile)
01246 {
01247 if (GetTropicZone(tile) != TROPICZONE_RAINFOREST) {
01248 _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST;
01249 return false;
01250 }
01251 return true;
01252 }
01253
01258 static bool CheckNewIndustry_BubbleGen(TileIndex tile)
01259 {
01260 if (GetTileZ(tile) > TILE_HEIGHT * 4) {
01261 _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_LOW_AREAS;
01262 return false;
01263 }
01264 return true;
01265 }
01266
01271 typedef bool CheckNewIndustryProc(TileIndex tile);
01272
01274 static CheckNewIndustryProc * const _check_new_industry_procs[CHECK_END] = {
01275 CheckNewIndustry_NULL,
01276 CheckNewIndustry_Forest,
01277 CheckNewIndustry_OilRefinery,
01278 CheckNewIndustry_Farm,
01279 CheckNewIndustry_Plantation,
01280 CheckNewIndustry_Water,
01281 CheckNewIndustry_Lumbermill,
01282 CheckNewIndustry_BubbleGen,
01283 CheckNewIndustry_OilRig,
01284 };
01285
01292 static const Town *FindTownForIndustry(TileIndex tile, int type)
01293 {
01294 const Town *t;
01295 const Industry *i;
01296
01297 t = ClosestTownFromTile(tile, UINT_MAX);
01298
01299 if (_settings_game.economy.multiple_industry_per_town) return t;
01300
01301 FOR_ALL_INDUSTRIES(i) {
01302 if (i->type == (byte)type &&
01303 i->town == t) {
01304 _error_message = STR_ERROR_ONLY_ONE_ALLOWED_PER_TOWN;
01305 return NULL;
01306 }
01307 }
01308
01309 return t;
01310 }
01311
01312 bool IsSlopeRefused(Slope current, Slope refused)
01313 {
01314 if (IsSteepSlope(current)) return true;
01315 if (current != SLOPE_FLAT) {
01316 if (IsSteepSlope(refused)) return true;
01317
01318 Slope t = ComplementSlope(current);
01319
01320 if ((refused & SLOPE_W) && (t & SLOPE_NW)) return true;
01321 if ((refused & SLOPE_S) && (t & SLOPE_NE)) return true;
01322 if ((refused & SLOPE_E) && (t & SLOPE_SW)) return true;
01323 if ((refused & SLOPE_N) && (t & SLOPE_SE)) return true;
01324 }
01325
01326 return false;
01327 }
01328
01329 static bool CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTileTable *it, uint itspec_index, int type, bool *custom_shape_check = NULL)
01330 {
01331 _error_message = STR_ERROR_SITE_UNSUITABLE;
01332 bool refused_slope = false;
01333 bool custom_shape = false;
01334
01335 do {
01336 IndustryGfx gfx = GetTranslatedIndustryTileID(it->gfx);
01337 TileIndex cur_tile = TileAddWrap(tile, it->ti.x, it->ti.y);
01338
01339 if (!IsValidTile(cur_tile)) {
01340 return false;
01341 }
01342
01343 if (gfx == GFX_WATERTILE_SPECIALCHECK) {
01344 if (!IsTileType(cur_tile, MP_WATER) ||
01345 GetTileSlope(cur_tile, NULL) != SLOPE_FLAT) {
01346 return false;
01347 }
01348 } else {
01349 if (!EnsureNoVehicleOnGround(cur_tile)) return false;
01350 if (MayHaveBridgeAbove(cur_tile) && IsBridgeAbove(cur_tile)) return false;
01351
01352 const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
01353
01354 IndustryBehaviour ind_behav = GetIndustrySpec(type)->behaviour;
01355
01356
01357 if (!HasBit(its->slopes_refused, 5) && (IsWaterTile(cur_tile) == !(ind_behav & INDUSTRYBEH_BUILT_ONWATER))) return false;
01358
01359 if (HasBit(its->callback_mask, CBM_INDT_SHAPE_CHECK)) {
01360 custom_shape = true;
01361 if (!PerformIndustryTileSlopeCheck(tile, cur_tile, its, type, gfx, itspec_index)) return false;
01362 } else {
01363 Slope tileh = GetTileSlope(cur_tile, NULL);
01364 refused_slope |= IsSlopeRefused(tileh, its->slopes_refused);
01365 }
01366
01367 if ((ind_behav & (INDUSTRYBEH_ONLY_INTOWN | INDUSTRYBEH_TOWN1200_MORE)) ||
01368 ((ind_behav & INDUSTRYBEH_ONLY_NEARTOWN) && IsTileType(cur_tile, MP_HOUSE))) {
01369 if (!IsTileType(cur_tile, MP_HOUSE)) {
01370 _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS;
01371 return false;
01372 }
01373
01374
01375 CompanyID old_company = _current_company;
01376 _current_company = OWNER_TOWN;
01377 bool not_clearable = DoCommand(cur_tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR).Failed();
01378 _current_company = old_company;
01379
01380 if (not_clearable) return false;
01381 } else {
01382
01383 bool not_clearable = DoCommand(cur_tile, 0, 0, DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR).Failed();
01384
01385 if (not_clearable) return false;
01386 }
01387 }
01388 } while ((++it)->ti.x != -0x80);
01389
01390 if (custom_shape_check != NULL) *custom_shape_check = custom_shape;
01391
01392
01393
01394
01395 return !refused_slope || (_settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !custom_shape && !_ignore_restrictions);
01396 }
01397
01398 static bool CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t)
01399 {
01400 if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_TOWN1200_MORE) && t->population < 1200) {
01401 _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS_WITH_POPULATION_OF_1200;
01402 return false;
01403 }
01404
01405 if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_ONLY_NEARTOWN) && DistanceMax(t->xy, tile) > 9) {
01406 _error_message = STR_ERROR_SITE_UNSUITABLE;
01407 return false;
01408 }
01409
01410 return true;
01411 }
01412
01413 static bool CheckCanTerraformSurroundingTiles(TileIndex tile, uint height, int internal)
01414 {
01415 int size_x, size_y;
01416 uint curh;
01417
01418 size_x = 2;
01419 size_y = 2;
01420
01421
01422 if (TileX(tile) == 0 || TileY(tile) == 0 || GetTileType(tile) == MP_VOID) return false;
01423
01424 tile += TileDiffXY(-1, -1);
01425 TILE_LOOP(tile_walk, size_x, size_y, tile) {
01426 curh = TileHeight(tile_walk);
01427
01428 if ((GetTileType(tile_walk) != MP_CLEAR) && (GetTileType(tile_walk) != MP_TREES))
01429 return false;
01430
01431
01432 if (internal != 0 && Delta(curh, height) > 1) return false;
01433
01434
01435
01436
01437 if (internal == 0 && curh != height) {
01438 if (TileX(tile_walk) == 0 || TileY(tile_walk) == 0 || !CheckCanTerraformSurroundingTiles(tile_walk + TileDiffXY(-1, -1), height, internal + 1))
01439 return false;
01440 }
01441 }
01442
01443 return true;
01444 }
01445
01450 static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlag flags, const IndustryTileTable *it, int type)
01451 {
01452 const int MKEND = -0x80;
01453 int max_x = 0;
01454 int max_y = 0;
01455 TileIndex cur_tile;
01456 uint size_x, size_y;
01457 uint h, curh;
01458
01459
01460 do {
01461 if (it->gfx == 0xFF) continue;
01462 if (it->ti.x > max_x) max_x = it->ti.x;
01463 if (it->ti.y > max_y) max_y = it->ti.y;
01464 } while ((++it)->ti.x != MKEND);
01465
01466
01467 h = TileHeight(tile);
01468
01469 if (TileX(tile) <= 1 || TileY(tile) <= 1) return false;
01470
01471
01472 cur_tile = tile + TileDiffXY(-1, -1);
01473 size_x = max_x + 4;
01474 size_y = max_y + 4;
01475
01476
01477 if (TileX(cur_tile) + size_x >= MapMaxX() || TileY(cur_tile) + size_y >= MapMaxY()) return false;
01478
01479
01480
01481 CompanyID old_company = _current_company;
01482 _current_company = OWNER_TOWN;
01483
01484 TILE_LOOP(tile_walk, size_x, size_y, cur_tile) {
01485 curh = TileHeight(tile_walk);
01486 if (curh != h) {
01487
01488
01489 if (!CheckCanTerraformSurroundingTiles(tile_walk, h, 0)) {
01490 _current_company = old_company;
01491 return false;
01492 }
01493
01494
01495 if (DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND).Failed()) {
01496 _current_company = old_company;
01497 return false;
01498 }
01499 }
01500 }
01501
01502 if (flags & DC_EXEC) {
01503
01504 TILE_LOOP(tile_walk, size_x, size_y, cur_tile) {
01505 curh = TileHeight(tile_walk);
01506 while (curh != h) {
01507
01508
01509
01510 DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
01511 curh += (curh > h) ? -1 : 1;
01512 }
01513 }
01514 }
01515
01516 _current_company = old_company;
01517 return true;
01518 }
01519
01520
01521 static bool CheckIfFarEnoughFromConflictingIndustry(TileIndex tile, int type)
01522 {
01523 const IndustrySpec *indspec = GetIndustrySpec(type);
01524 const Industry *i;
01525 FOR_ALL_INDUSTRIES(i) {
01526
01527 bool in_low_distance = DistanceMax(tile, i->location.tile) <= 14;
01528
01529
01530 if ((i->type == indspec->conflicting[0] ||
01531 i->type == indspec->conflicting[1] ||
01532 i->type == indspec->conflicting[2]) &&
01533 in_low_distance) {
01534 _error_message = STR_ERROR_INDUSTRY_TOO_CLOSE;
01535 return false;
01536 }
01537 }
01538 return true;
01539 }
01540
01544 enum ProductionLevels {
01545 PRODLEVEL_CLOSURE = 0x00,
01546 PRODLEVEL_MINIMUM = 0x04,
01547 PRODLEVEL_DEFAULT = 0x10,
01548 PRODLEVEL_MAXIMUM = 0x80,
01549 };
01550
01551 static void DoCreateNewIndustry(Industry *i, TileIndex tile, int type, const IndustryTileTable *it, byte layout, const Town *t, Owner owner, Owner founder)
01552 {
01553 const IndustrySpec *indspec = GetIndustrySpec(type);
01554 uint32 r;
01555 uint j;
01556
01557 i->location = TileArea(tile, 1, 1);
01558 i->type = type;
01559 IncIndustryTypeCount(type);
01560
01561 i->produced_cargo[0] = indspec->produced_cargo[0];
01562 i->produced_cargo[1] = indspec->produced_cargo[1];
01563 i->accepts_cargo[0] = indspec->accepts_cargo[0];
01564 i->accepts_cargo[1] = indspec->accepts_cargo[1];
01565 i->accepts_cargo[2] = indspec->accepts_cargo[2];
01566 i->production_rate[0] = indspec->production_rate[0];
01567 i->production_rate[1] = indspec->production_rate[1];
01568
01569
01570 if (_settings_game.economy.smooth_economy &&
01571 !(HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_256_TICKS) || HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) &&
01572 !(HasBit(indspec->callback_mask, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_CHANGE))
01573 ) {
01574 i->production_rate[0] = min((RandomRange(256) + 128) * i->production_rate[0] >> 8, 255);
01575 i->production_rate[1] = min((RandomRange(256) + 128) * i->production_rate[1] >> 8, 255);
01576 }
01577
01578 i->town = t;
01579 i->owner = owner;
01580
01581 r = Random();
01582 i->random_colour = GB(r, 0, 4);
01583 i->counter = GB(r, 4, 12);
01584 i->random = GB(r, 16, 16);
01585 i->produced_cargo_waiting[0] = 0;
01586 i->produced_cargo_waiting[1] = 0;
01587 i->incoming_cargo_waiting[0] = 0;
01588 i->incoming_cargo_waiting[1] = 0;
01589 i->incoming_cargo_waiting[2] = 0;
01590 i->this_month_production[0] = 0;
01591 i->this_month_production[1] = 0;
01592 i->this_month_transported[0] = 0;
01593 i->this_month_transported[1] = 0;
01594 i->last_month_pct_transported[0] = 0;
01595 i->last_month_pct_transported[1] = 0;
01596 i->last_month_transported[0] = 0;
01597 i->last_month_transported[1] = 0;
01598 i->was_cargo_delivered = false;
01599 i->last_prod_year = _cur_year;
01600 i->last_month_production[0] = i->production_rate[0] * 8;
01601 i->last_month_production[1] = i->production_rate[1] * 8;
01602 i->founder = founder;
01603
01604 if (HasBit(indspec->callback_mask, CBM_IND_DECIDE_COLOUR)) {
01605 uint16 res = GetIndustryCallback(CBID_INDUSTRY_DECIDE_COLOUR, 0, 0, i, type, INVALID_TILE);
01606 if (res != CALLBACK_FAILED) i->random_colour = GB(res, 0, 4);
01607 }
01608
01609 if (HasBit(indspec->callback_mask, CBM_IND_INPUT_CARGO_TYPES)) {
01610 for (j = 0; j < lengthof(i->accepts_cargo); j++) i->accepts_cargo[j] = CT_INVALID;
01611 for (j = 0; j < lengthof(i->accepts_cargo); j++) {
01612 uint16 res = GetIndustryCallback(CBID_INDUSTRY_INPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
01613 if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
01614 i->accepts_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
01615 }
01616 }
01617
01618 if (HasBit(indspec->callback_mask, CBM_IND_OUTPUT_CARGO_TYPES)) {
01619 for (j = 0; j < lengthof(i->produced_cargo); j++) i->produced_cargo[j] = CT_INVALID;
01620 for (j = 0; j < lengthof(i->produced_cargo); j++) {
01621 uint16 res = GetIndustryCallback(CBID_INDUSTRY_OUTPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
01622 if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
01623 i->produced_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
01624 }
01625 }
01626
01627 i->construction_date = _date;
01628 i->construction_type = (_game_mode == GM_EDITOR) ? ICT_SCENARIO_EDITOR :
01629 (_generating_world ? ICT_MAP_GENERATION : ICT_NORMAL_GAMEPLAY);
01630
01631
01632
01633
01634 i->selected_layout = layout + 1;
01635
01636 if (!_generating_world) i->last_month_production[0] = i->last_month_production[1] = 0;
01637
01638 i->prod_level = PRODLEVEL_DEFAULT;
01639
01640 do {
01641 TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
01642
01643 if (it->gfx != GFX_WATERTILE_SPECIALCHECK) {
01644 i->location.Add(cur_tile);
01645
01646 WaterClass wc = (IsWaterTile(cur_tile) ? GetWaterClass(cur_tile) : WATER_CLASS_INVALID);
01647
01648 DoCommand(cur_tile, 0, 0, DC_EXEC | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
01649
01650 MakeIndustry(cur_tile, i->index, it->gfx, Random(), wc);
01651
01652 if (_generating_world) {
01653 SetIndustryConstructionCounter(cur_tile, 3);
01654 SetIndustryConstructionStage(cur_tile, 2);
01655 }
01656
01657
01658 IndustryGfx cur_gfx = GetTranslatedIndustryTileID(it->gfx);
01659 const IndustryTileSpec *its = GetIndustryTileSpec(cur_gfx);
01660 if (its->animation_info != 0xFFFF) AddAnimatedTile(cur_tile);
01661 }
01662 } while ((++it)->ti.x != -0x80);
01663
01664 if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
01665 for (j = 0; j != 50; j++) PlantRandomFarmField(i);
01666 }
01667 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
01668
01669 Station::RecomputeIndustriesNearForAll();
01670 }
01671
01682 static Industry *CreateNewIndustryHelper(TileIndex tile, IndustryType type, DoCommandFlag flags, const IndustrySpec *indspec, uint itspec_index, uint32 seed, Owner founder)
01683 {
01684 assert(itspec_index < indspec->num_table);
01685 const IndustryTileTable *it = indspec->table[itspec_index];
01686 bool custom_shape_check = false;
01687
01688 if (!CheckIfIndustryTilesAreFree(tile, it, itspec_index, type, &custom_shape_check)) return NULL;
01689
01690 if (HasBit(GetIndustrySpec(type)->callback_mask, CBM_IND_LOCATION)) {
01691 if (!CheckIfCallBackAllowsCreation(tile, type, itspec_index, seed)) return NULL;
01692 } else {
01693 if (!_check_new_industry_procs[indspec->check_proc](tile)) return NULL;
01694 }
01695
01696 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;
01697 if (!CheckIfFarEnoughFromConflictingIndustry(tile, type)) return NULL;
01698
01699 const Town *t = FindTownForIndustry(tile, type);
01700 if (t == NULL) return NULL;
01701
01702 if (!CheckIfIndustryIsAllowed(tile, type, t)) return NULL;
01703
01704 if (!Industry::CanAllocateItem()) return NULL;
01705
01706 if (flags & DC_EXEC) {
01707 Industry *i = new Industry(tile);
01708 if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER | DC_EXEC, it, type);
01709 DoCreateNewIndustry(i, tile, type, it, itspec_index, t, OWNER_NONE, founder);
01710
01711 return i;
01712 }
01713
01714
01715
01716 return (Industry *)-1;
01717 }
01718
01729 CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01730 {
01731 IndustryType it = GB(p1, 0, 8);
01732 if (it >= NUM_INDUSTRYTYPES) return CMD_ERROR;
01733
01734 const IndustrySpec *indspec = GetIndustrySpec(it);
01735
01736
01737 if (!indspec->enabled || indspec->num_table == 0) return CMD_ERROR;
01738
01739
01740
01741 if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
01742 return CMD_ERROR;
01743 }
01744
01745 if (_game_mode != GM_EDITOR && !CheckIfCallBackAllowsAvailability(it, IACT_USERCREATION)) {
01746 return CMD_ERROR;
01747 }
01748
01749 const Industry *ind = NULL;
01750 if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indspec->IsRawIndustry()) {
01751 if (flags & DC_EXEC) {
01752
01753 CompanyID founder = _current_company;
01754 _current_company = OWNER_TOWN;
01755
01756
01757
01758 if (Random() <= indspec->prospecting_chance) {
01759 for (int i = 0; i < 5000; i++) {
01760
01761
01762
01763 tile = RandomTile();
01764 ind = CreateNewIndustryHelper(tile, it, flags, indspec, RandomRange(indspec->num_table), p2, founder);
01765 if (ind != NULL) {
01766 break;
01767 }
01768 }
01769 }
01770 _current_company = founder;
01771 }
01772 } else {
01773 int count = indspec->num_table;
01774 const IndustryTileTable * const *itt = indspec->table;
01775 int num = GB(p1, 8, 8);
01776 if (num >= count) return CMD_ERROR;
01777
01778 _error_message = STR_ERROR_SITE_UNSUITABLE;
01779 do {
01780 if (--count < 0) return CMD_ERROR;
01781 if (--num < 0) num = indspec->num_table - 1;
01782 } while (!CheckIfIndustryTilesAreFree(tile, itt[num], num, it));
01783
01784 ind = CreateNewIndustryHelper(tile, it, flags, indspec, num, p2, _current_company);
01785 if (ind == NULL) return CMD_ERROR;
01786 }
01787
01788 if ((flags & DC_EXEC) && _game_mode != GM_EDITOR && ind != NULL) {
01789 SetDParam(0, indspec->name);
01790 if (indspec->new_industry_text > STR_LAST_STRINGID) {
01791 SetDParam(1, STR_TOWN_NAME);
01792 SetDParam(2, ind->town->index);
01793 } else {
01794 SetDParam(1, ind->town->index);
01795 }
01796 AddIndustryNewsItem(indspec->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
01797 AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
01798 }
01799
01800 return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost());
01801 }
01802
01803
01804 static Industry *CreateNewIndustry(TileIndex tile, IndustryType type)
01805 {
01806 const IndustrySpec *indspec = GetIndustrySpec(type);
01807
01808 uint32 seed = Random();
01809 return CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, RandomRange(indspec->num_table), seed, OWNER_NONE);
01810 }
01811
01818 static uint32 GetScaledIndustryProbability(IndustryType it, bool *force_at_least_one)
01819 {
01820 const IndustrySpec *ind_spc = GetIndustrySpec(it);
01821 uint32 chance = ind_spc->appear_creation[_settings_game.game_creation.landscape] * 16;
01822 if (!ind_spc->enabled || chance == 0 || ind_spc->num_table == 0 ||
01823 !CheckIfCallBackAllowsAvailability(it, IACT_MAPGENERATION) ||
01824 (_game_mode != GM_EDITOR && _settings_game.difficulty.number_industries == 0)) {
01825 *force_at_least_one = false;
01826 return 0;
01827 } else {
01828
01829
01830 chance = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(chance) : ScaleByMapSize(chance);
01831
01832 *force_at_least_one = (chance > 0) && !(ind_spc->behaviour & INDUSTRYBEH_NOBUILT_MAPCREATION) && (_game_mode != GM_EDITOR);
01833 return chance;
01834 }
01835 }
01836
01838 static const byte _numof_industry_table[]= {
01839 0,
01840 10,
01841 25,
01842 55,
01843 80,
01844 };
01845
01851 static void PlaceInitialIndustry(IndustryType type, bool try_hard)
01852 {
01853 CompanyID old_company = _current_company;
01854 _current_company = OWNER_NONE;
01855
01856 IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
01857
01858 for (uint i = 0; i < (try_hard ? 10000u : 2000u); i++) {
01859 if (CreateNewIndustry(RandomTile(), type) != NULL) break;
01860 }
01861
01862 _current_company = old_company;
01863 }
01864
01869 void GenerateIndustries()
01870 {
01871 assert(_settings_game.difficulty.number_industries < lengthof(_numof_industry_table));
01872 uint difficulty = (_game_mode != GM_EDITOR) ? _settings_game.difficulty.number_industries : 1;
01873 uint total_amount = ScaleByMapSize(_numof_industry_table[difficulty]);
01874
01875
01876 if (total_amount == 0) return;
01877
01878 uint32 industry_probs[NUM_INDUSTRYTYPES];
01879 bool force_at_least_one[NUM_INDUSTRYTYPES];
01880 uint32 total_prob = 0;
01881 uint num_forced = 0;
01882
01883 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
01884 industry_probs[it] = GetScaledIndustryProbability(it, force_at_least_one + it);
01885 total_prob += industry_probs[it];
01886 if (force_at_least_one[it]) num_forced++;
01887 }
01888
01889 if (total_prob == 0 || total_amount < num_forced) {
01890
01891 total_amount = num_forced;
01892 }
01893
01894 SetGeneratingWorldProgress(GWP_INDUSTRY, total_amount);
01895
01896
01897 for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
01898 if (force_at_least_one[it]) {
01899 assert(total_amount > 0);
01900 total_amount--;
01901 PlaceInitialIndustry(it, true);
01902 }
01903 }
01904
01905
01906 for (uint i = 0; i < total_amount; i++) {
01907 uint32 r = RandomRange(total_prob);
01908 IndustryType it = 0;
01909 while (it < NUM_INDUSTRYTYPES && r >= industry_probs[it]) {
01910 r -= industry_probs[it];
01911 it++;
01912 }
01913 assert(it < NUM_INDUSTRYTYPES && industry_probs[it] > 0);
01914 PlaceInitialIndustry(it, false);
01915 }
01916 }
01917
01918 static void UpdateIndustryStatistics(Industry *i)
01919 {
01920 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
01921 if (i->produced_cargo[j] != CT_INVALID) {
01922 byte pct = 0;
01923 if (i->this_month_production[j] != 0) {
01924 i->last_prod_year = _cur_year;
01925 pct = min(i->this_month_transported[j] * 256 / i->this_month_production[j], 255);
01926 }
01927 i->last_month_pct_transported[j] = pct;
01928
01929 i->last_month_production[j] = i->this_month_production[j];
01930 i->this_month_production[j] = 0;
01931
01932 i->last_month_transported[j] = i->this_month_transported[j];
01933 i->this_month_transported[j] = 0;
01934 }
01935 }
01936 }
01937
01939 struct ProbabilityHelper {
01940 uint16 prob;
01941 IndustryType ind;
01942 };
01943
01947 static void MaybeNewIndustry()
01948 {
01949 Industry *ind;
01950 IndustryType rndtype, j;
01951 const IndustrySpec *ind_spc;
01952 uint num = 0;
01953 ProbabilityHelper cumulative_probs[NUM_INDUSTRYTYPES];
01954 uint16 probability_max = 0;
01955
01956
01957 for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
01958 ind_spc = GetIndustrySpec(j);
01959 byte chance = ind_spc->appear_ingame[_settings_game.game_creation.landscape];
01960
01961 if (!ind_spc->enabled || chance == 0 || ind_spc->num_table == 0) continue;
01962
01963
01964
01965 if (CheckIfCallBackAllowsAvailability(j, IACT_RANDOMCREATION)) {
01966 probability_max += chance;
01967
01968 cumulative_probs[num].ind = j;
01969 cumulative_probs[num++].prob = probability_max;
01970 }
01971 }
01972
01973
01974 if (probability_max == 0) return;
01975
01976
01977 rndtype = RandomRange(probability_max);
01978 for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
01979
01980 if (cumulative_probs[j].prob >= rndtype) break;
01981 }
01982
01983 ind_spc = GetIndustrySpec(cumulative_probs[j].ind);
01984
01985 if ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) return;
01986 if ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) return;
01987
01988
01989 num = 2000;
01990 for (;;) {
01991 ind = CreateNewIndustry(RandomTile(), cumulative_probs[j].ind);
01992 if (ind != NULL) break;
01993 if (--num == 0) return;
01994 }
01995
01996 SetDParam(0, ind_spc->name);
01997 if (ind_spc->new_industry_text > STR_LAST_STRINGID) {
01998 SetDParam(1, STR_TOWN_NAME);
01999 SetDParam(2, ind->town->index);
02000 } else {
02001 SetDParam(1, ind->town->index);
02002 }
02003 AddIndustryNewsItem(ind_spc->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
02004 AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
02005 }
02006
02015 static bool CheckIndustryCloseDownProtection(IndustryType type)
02016 {
02017 const IndustrySpec *indspec = GetIndustrySpec(type);
02018
02019
02020 if ((indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE) return false;
02021 return (indspec->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) == 0 && GetIndustryTypeCount(type) <= 1;
02022 }
02023
02033 static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces)
02034 {
02035 const IndustrySpec *indspec = GetIndustrySpec(ind->type);
02036
02037
02038 for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) {
02039 if (ind->accepts_cargo[j] == CT_INVALID) continue;
02040 if (cargo == ind->accepts_cargo[j]) {
02041 if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) {
02042 uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO,
02043 0, GetReverseCargoTranslation(cargo, indspec->grf_prop.grffile),
02044 ind, ind->type, ind->location.tile);
02045 if (res == 0) continue;
02046 }
02047 *c_accepts = true;
02048 break;
02049 }
02050 }
02051
02052
02053 for (byte j = 0; j < lengthof(ind->produced_cargo); j++) {
02054 if (ind->produced_cargo[j] == CT_INVALID) continue;
02055 if (cargo == ind->produced_cargo[j]) {
02056 *c_produces = true;
02057 break;
02058 }
02059 }
02060 }
02061
02075 static int WhoCanServiceIndustry(Industry *ind)
02076 {
02077
02078 StationList stations;
02079 FindStationsAroundTiles(ind->location, &stations);
02080
02081 if (stations.Length() == 0) return 0;
02082
02083 const Vehicle *v;
02084 int result = 0;
02085 FOR_ALL_VEHICLES(v) {
02086
02087 if (v->owner != _local_company && result != 0) continue;
02088
02089
02090 bool c_accepts = false;
02091 bool c_produces = false;
02092 if (v->type == VEH_TRAIN && Train::From(v)->IsFrontEngine()) {
02093 for (const Vehicle *u = v; u != NULL; u = u->Next()) {
02094 CanCargoServiceIndustry(u->cargo_type, ind, &c_accepts, &c_produces);
02095 }
02096 } else if (v->type == VEH_ROAD || v->type == VEH_SHIP || v->type == VEH_AIRCRAFT) {
02097 CanCargoServiceIndustry(v->cargo_type, ind, &c_accepts, &c_produces);
02098 } else {
02099 continue;
02100 }
02101 if (!c_accepts && !c_produces) continue;
02102
02103
02104
02105
02106
02107 const Order *o;
02108 FOR_VEHICLE_ORDERS(v, o) {
02109 if (o->IsType(OT_GOTO_STATION) && !(o->GetUnloadType() & OUFB_TRANSFER)) {
02110
02111 Station *st = Station::Get(o->GetDestination());
02112 assert(st != NULL);
02113
02114
02115 if ((o->GetUnloadType() & OUFB_UNLOAD) && !c_accepts) break;
02116
02117 if (stations.Contains(st)) {
02118 if (v->owner == _local_company) return 2;
02119 result = 1;
02120 }
02121 }
02122 }
02123 }
02124 return result;
02125 }
02126
02134 static void ReportNewsProductionChangeIndustry(Industry *ind, CargoID type, int percent)
02135 {
02136 NewsSubtype ns;
02137
02138 switch (WhoCanServiceIndustry(ind)) {
02139 case 0: ns = NS_INDUSTRY_NOBODY; break;
02140 case 1: ns = NS_INDUSTRY_OTHER; break;
02141 case 2: ns = NS_INDUSTRY_COMPANY; break;
02142 default: NOT_REACHED();
02143 }
02144 SetDParam(2, abs(percent));
02145 SetDParam(0, CargoSpec::Get(type)->name);
02146 SetDParam(1, ind->index);
02147 AddIndustryNewsItem(
02148 percent >= 0 ? STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_SMOOTH : STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_SMOOTH,
02149 ns,
02150 ind->index
02151 );
02152 }
02153
02154 enum {
02155 PERCENT_TRANSPORTED_60 = 153,
02156 PERCENT_TRANSPORTED_80 = 204,
02157 };
02158
02163 static void ChangeIndustryProduction(Industry *i, bool monthly)
02164 {
02165 StringID str = STR_NULL;
02166 bool closeit = false;
02167 const IndustrySpec *indspec = GetIndustrySpec(i->type);
02168 bool standard = false;
02169 bool suppress_message = false;
02170 bool recalculate_multipliers = false;
02171
02172 bool smooth_economy = _settings_game.economy.smooth_economy &&
02173 !(HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_256_TICKS) || HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) &&
02174 !(HasBit(indspec->callback_mask, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_CHANGE));
02175 byte div = 0;
02176 byte mul = 0;
02177 int8 increment = 0;
02178
02179 bool callback_enabled = HasBit(indspec->callback_mask, monthly ? CBM_IND_MONTHLYPROD_CHANGE : CBM_IND_PRODUCTION_CHANGE);
02180 if (callback_enabled) {
02181 uint16 res = GetIndustryCallback(monthly ? CBID_INDUSTRY_MONTHLYPROD_CHANGE : CBID_INDUSTRY_PRODUCTION_CHANGE, 0, Random(), i, i->type, i->location.tile);
02182 if (res != CALLBACK_FAILED) {
02183 suppress_message = HasBit(res, 7);
02184
02185 if (HasBit(res, 8)) str = MapGRFStringID(indspec->grf_prop.grffile->grfid, GB(GetRegister(0x100), 0, 16));
02186 res = GB(res, 0, 4);
02187 switch (res) {
02188 default: NOT_REACHED();
02189 case 0x0: break;
02190 case 0x1: div = 1; break;
02191 case 0x2: mul = 1; break;
02192 case 0x3: closeit = true; break;
02193 case 0x4: standard = true; break;
02194 case 0x5: case 0x6: case 0x7:
02195 case 0x8: div = res - 0x3; break;
02196 case 0x9: case 0xA: case 0xB:
02197 case 0xC: mul = res - 0x7; break;
02198 case 0xD:
02199 case 0xE:
02200 increment = res == 0x0D ? -1 : 1;
02201 break;
02202 case 0xF:
02203 i->prod_level = Clamp(GB(GetRegister(0x100), 16, 8), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
02204 recalculate_multipliers = true;
02205 break;
02206 }
02207 }
02208 } else {
02209 if (monthly != smooth_economy) return;
02210 if (indspec->life_type == INDUSTRYLIFE_BLACK_HOLE) return;
02211 }
02212
02213 if (standard || (!callback_enabled && (indspec->life_type & (INDUSTRYLIFE_ORGANIC | INDUSTRYLIFE_EXTRACTIVE)) != 0)) {
02214
02215 bool only_decrease = (indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE;
02216
02217 if (smooth_economy) {
02218 closeit = true;
02219 for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
02220 if (i->produced_cargo[j] == CT_INVALID) continue;
02221 uint32 r = Random();
02222 int old_prod, new_prod, percent;
02223
02224 int mult = (i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_60) ? 1 : -1;
02225
02226 new_prod = old_prod = i->production_rate[j];
02227
02228
02229
02230 if (only_decrease) {
02231 mult = -1;
02232
02233
02234 } else if (Chance16I(1, ((i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_80) ? 6 : 3), r)) {
02235 mult *= -1;
02236 }
02237
02238
02239
02240 if (Chance16I(1, 22, r >> 16)) {
02241 new_prod += mult * (max(((RandomRange(50) + 10) * old_prod) >> 8, 1U));
02242 }
02243
02244
02245 new_prod = Clamp(new_prod, 1, 255);
02246
02247 if (((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0) && j == 1)
02248 new_prod = Clamp(new_prod, 0, 16);
02249
02250
02251 if (new_prod == old_prod && old_prod > 1) {
02252 closeit = false;
02253 continue;
02254 }
02255
02256 percent = (old_prod == 0) ? 100 : (new_prod * 100 / old_prod - 100);
02257 i->production_rate[j] = new_prod;
02258
02259
02260 if (new_prod > 1) closeit = false;
02261
02262 if (abs(percent) >= 10) {
02263 ReportNewsProductionChangeIndustry(i, i->produced_cargo[j], percent);
02264 }
02265 }
02266 } else {
02267 if (only_decrease || Chance16(1, 3)) {
02268
02269 if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != Chance16(1, 3)) {
02270 mul = 1;
02271 } else {
02272 div = 1;
02273 }
02274 }
02275 }
02276 }
02277
02278 if (!callback_enabled && (indspec->life_type & INDUSTRYLIFE_PROCESSING)) {
02279 if ( (byte)(_cur_year - i->last_prod_year) >= 5 && Chance16(1, smooth_economy ? 180 : 2)) {
02280 closeit = true;
02281 }
02282 }
02283
02284
02285 while (mul-- != 0 && i->prod_level < PRODLEVEL_MAXIMUM) {
02286 i->prod_level = min(i->prod_level * 2, PRODLEVEL_MAXIMUM);
02287 recalculate_multipliers = true;
02288 if (str == STR_NULL) str = indspec->production_up_text;
02289 }
02290
02291
02292 while (div-- != 0 && !closeit) {
02293 if (i->prod_level == PRODLEVEL_MINIMUM) {
02294 closeit = true;
02295 } else {
02296 i->prod_level = max(i->prod_level / 2, (int)PRODLEVEL_MINIMUM);
02297 recalculate_multipliers = true;
02298 if (str == STR_NULL) str = indspec->production_down_text;
02299 }
02300 }
02301
02302
02303 if (increment != 0) {
02304 if (increment < 0 && i->prod_level == PRODLEVEL_MINIMUM) {
02305 closeit = true;
02306 } else {
02307 i->prod_level = ClampU(i->prod_level + increment, PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
02308 recalculate_multipliers = true;
02309 }
02310 }
02311
02312
02313
02314 if (recalculate_multipliers) {
02315
02316 i->production_rate[0] = min((indspec->production_rate[0] * i->prod_level + PRODLEVEL_DEFAULT - 1) / PRODLEVEL_DEFAULT, 0xFF);
02317 i->production_rate[1] = min((indspec->production_rate[1] * i->prod_level + PRODLEVEL_DEFAULT - 1) / PRODLEVEL_DEFAULT, 0xFF);
02318 }
02319
02320
02321 if (closeit && !CheckIndustryCloseDownProtection(i->type)) {
02322 i->prod_level = PRODLEVEL_CLOSURE;
02323 str = indspec->closure_text;
02324 }
02325
02326 if (!suppress_message && str != STR_NULL) {
02327 NewsSubtype ns;
02328
02329 if (closeit) {
02330 ns = NS_INDUSTRY_CLOSE;
02331 AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
02332 } else {
02333 switch (WhoCanServiceIndustry(i)) {
02334 case 0: ns = NS_INDUSTRY_NOBODY; break;
02335 case 1: ns = NS_INDUSTRY_OTHER; break;
02336 case 2: ns = NS_INDUSTRY_COMPANY; break;
02337 default: NOT_REACHED();
02338 }
02339 }
02340
02341 if (str > STR_LAST_STRINGID) {
02342 SetDParam(0, STR_TOWN_NAME);
02343 SetDParam(1, i->town->index);
02344 SetDParam(2, indspec->name);
02345 } else if (closeit) {
02346 SetDParam(0, STR_FORMAT_INDUSTRY_NAME);
02347 SetDParam(1, i->town->index);
02348 SetDParam(2, indspec->name);
02349 } else {
02350 SetDParam(0, i->index);
02351 }
02352
02353 AddNewsItem(str,
02354 ns,
02355 closeit ? NR_TILE : NR_INDUSTRY,
02356 closeit ? i->location.tile + TileDiffXY(1, 1) : i->index);
02357 }
02358 }
02359
02365 void IndustryDailyLoop()
02366 {
02367 _economy.industry_daily_change_counter += _economy.industry_daily_increment;
02368
02369
02370
02371
02372 uint16 change_loop = _economy.industry_daily_change_counter >> 16;
02373
02374
02375 _economy.industry_daily_change_counter &= 0xFFFF;
02376
02377 if (change_loop == 0) {
02378 return;
02379 }
02380
02381 CompanyID old_company = _current_company;
02382 _current_company = OWNER_NONE;
02383
02384
02385 for (uint16 j = 0; j < change_loop; j++) {
02386
02387 if (Chance16(3, 100)) {
02388 MaybeNewIndustry();
02389 } else {
02390 Industry *i = Industry::GetRandom();
02391 if (i != NULL) {
02392 ChangeIndustryProduction(i, false);
02393 SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
02394 }
02395 }
02396 }
02397
02398 _current_company = old_company;
02399
02400
02401 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
02402 }
02403
02404 void IndustryMonthlyLoop()
02405 {
02406 Industry *i;
02407 CompanyID old_company = _current_company;
02408 _current_company = OWNER_NONE;
02409
02410 FOR_ALL_INDUSTRIES(i) {
02411 UpdateIndustryStatistics(i);
02412 if (i->prod_level == PRODLEVEL_CLOSURE) {
02413 delete i;
02414 } else {
02415 ChangeIndustryProduction(i, true);
02416 SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
02417 }
02418 }
02419
02420 _current_company = old_company;
02421
02422
02423 InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
02424 }
02425
02426
02427 void InitializeIndustries()
02428 {
02429 _industry_pool.CleanPool();
02430
02431 ResetIndustryCounts();
02432 _industry_sound_tile = 0;
02433 }
02434
02435 bool IndustrySpec::IsRawIndustry() const
02436 {
02437
02438 return (this->life_type & (INDUSTRYLIFE_EXTRACTIVE | INDUSTRYLIFE_ORGANIC)) != 0 &&
02439 (this->behaviour & INDUSTRYBEH_CUT_TREES) == 0;
02440 }
02441
02442 Money IndustrySpec::GetConstructionCost() const
02443 {
02444
02445 return (_price[(_settings_game.construction.raw_industry_construction == 1 && this->IsRawIndustry()) ?
02446 PR_BUILD_INDUSTRY_RAW : PR_BUILD_INDUSTRY] * this->cost_multiplier) >> 8;
02447 }
02448
02449 Money IndustrySpec::GetRemovalCost() const
02450 {
02451 return (_price[PR_CLEAR_INDUSTRY] * this->removal_cost_multiplier) >> 8;
02452 }
02453
02454 static CommandCost TerraformTile_Industry(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
02455 {
02456 if (AutoslopeEnabled()) {
02457
02458
02459
02460
02461
02462
02463 Slope tileh_old = GetTileSlope(tile, NULL);
02464
02465 if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
02466 const IndustryGfx gfx = GetIndustryGfx(tile);
02467 const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
02468
02469
02470 if (HasBit(itspec->callback_mask, CBM_INDT_AUTOSLOPE)) {
02471
02472 uint16 res = GetIndustryTileCallback(CBID_INDUSTRY_AUTOSLOPE, 0, 0, gfx, Industry::GetByTile(tile), tile);
02473 if ((res == 0) || (res == CALLBACK_FAILED)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
02474 } else {
02475
02476 return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
02477 }
02478 }
02479 }
02480 return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
02481 }
02482
02483 extern const TileTypeProcs _tile_type_industry_procs = {
02484 DrawTile_Industry,
02485 GetSlopeZ_Industry,
02486 ClearTile_Industry,
02487 AddAcceptedCargo_Industry,
02488 GetTileDesc_Industry,
02489 GetTileTrackStatus_Industry,
02490 ClickTile_Industry,
02491 AnimateTile_Industry,
02492 TileLoop_Industry,
02493 ChangeTileOwner_Industry,
02494 NULL,
02495 NULL,
02496 GetFoundation_Industry,
02497 TerraformTile_Industry,
02498 };