# industry_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: industry_cmd.cpp 19446 2010-03-17 22:21:03Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
    00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
    00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
    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   /* once performed, enable only the current climate industries */
    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   /* Reset any overrides that have been set. */
    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   /* Industry can also be destroyed when not fully initialized.
    00137    * This means that we do not have to clear tiles either. */
    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         /* MakeWaterKeepingClass() can also handle 'land' */
    00144         MakeWaterKeepingClass(tile_cur, OWNER_NONE);
    00145
    00146         /* MakeWaterKeepingClass() doesn't remove animation if the tiles
    00147          * become watery, but be on the safe side an always remote it. */
    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     /* Remove the farmland and convert it to regular tiles over time. */
    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   /* don't let any disaster vehicle target invalid industry */
    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 /* static */ 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     /* Make sure we have a valid industry */
    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   /* Retrieve pointer to the draw industry tile struct */
    00309   if (gfx >= NEW_INDUSTRYTILEOFFSET) {
    00310     /* Draw the tile using the specialized method of newgrf industrytile.
    00311      * DrawNewIndustry will return false if ever the resolver could not
    00312      * find any sprite to display.  So in this case, we will jump on the
    00313      * substitute gfx instead. */
    00314     if (indts->grf_prop.spritegroup != NULL && DrawNewIndustryTile(ti, ind, gfx, indts)) {
    00315       return;
    00316     } else {
    00317       /* No sprite group (or no valid one) found, meaning no graphics associated.
    00318        * Use the substitute one instead */
    00319       if (indts->grf_prop.subst_id != INVALID_INDUSTRYTILE) {
    00320         gfx = indts->grf_prop.subst_id;
    00321         /* And point the industrytile spec accordingly */
    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   /* DrawFoundation() modifes ti->z and ti->tileh */
    00334   if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
    00335
    00336   /* If the ground sprite is the default flat water sprite, draw also canal/river borders.
    00337    * Do not do this if the tile's WaterClass is 'land'. */
    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   /* If industries are transparent and invisible, do not draw the upper part */
    00345   if (IsInvisibilitySet(TO_INDUSTRIES)) return;
    00346
    00347   /* Add industry on top of the ground? */
    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   /* For NewGRF industry tiles we might not be drawing a foundation. We need to
    00378    * account for this, as other structures should
    00379    * draw the wall of the foundation in this case.
    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   /* When we have to use a callback, we put our data in the next two variables */
    00397   CargoID raw_accepts_cargo[lengthof(itspec->accepts_cargo)];
    00398   uint8 raw_cargo_acceptance[lengthof(itspec->acceptance)];
    00399
    00400   /* And then these will always point to a same sized array with the required data */
    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; // work only with valid cargos
    00424
    00425     /* Add accepted cargo */
    00426     acceptance[a] += cargo_acceptance[i];
    00427
    00428     /* Maybe set 'always accepted' bit (if it's not set already) */
    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       /* Test whether the industry itself accepts the cargo type */
    00434       if (ind->accepts_cargo[cargo_index] == a) {
    00435         accepts = true;
    00436         break;
    00437       }
    00438     }
    00439
    00440     if (accepts) continue;
    00441
    00442     /* If the industry itself doesn't accept this cargo, set 'always accepted' bit */
    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   /* water can destroy industries
    00470    * in editor you can bulldoze industries
    00471    * with magic_bulldozer cheat you can destroy industries
    00472    * (area around OILRIG is water, so water shouldn't flood it
    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       /* fluctuating economy? */
    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   /* Sparks on a coal plant */
    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     /* New industries are already animated on construction. */
    00735     return;
    00736   }
    00737
    00738   switch (gfx) {
    00739   case GFX_POWERPLANT_CHIMNEY:
    00740     CreateChimneySmoke(tile);
    00741     break;
    00742
    00743   case GFX_OILRIG_1: {
    00744     /* Do not require an industry tile to be after the first two GFX_OILRIG_1
    00745      * tiles (like the default oil rig). Do a proper check to ensure the
    00746      * tiles belong to the same industry and based on that build the oil rig's
    00747      * station. */
    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   TriggerIndustryTile(tile, INDTILE_TRIGGER_TILE_LOOP);
    00803
    00804   if (!IsIndustryCompleted(tile)) {
    00805     MakeIndustryTileBigger(tile);
    00806     return;
    00807   }
    00808
    00809   if (_game_mode == GM_EDITOR) return;
    00810
    00811   TransportIndustryGoods(tile);
    00812
    00813   if (StartStopIndustryTileAnimation(tile, IAT_TILELOOP)) return;
    00814
    00815   newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_next;
    00816   if (newgfx != INDUSTRYTILE_NOANIM) {
    00817     ResetIndustryConstructionStage(tile);
    00818     SetIndustryGfx(tile, newgfx);
    00819     MarkTileDirtyByTile(tile);
    00820     return;
    00821   }
    00822
    00823   gfx = GetIndustryGfx(tile);
    00824
    00825   switch (gfx) {
    00826   case GFX_COAL_MINE_TOWER_NOT_ANIMATED:
    00827   case GFX_COPPER_MINE_TOWER_NOT_ANIMATED:
    00828   case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:
    00829     if (!(_tick_counter & 0x400) && Chance16(1, 2)) {
    00830       switch (gfx) {
    00831         case GFX_COAL_MINE_TOWER_NOT_ANIMATED:   gfx = GFX_COAL_MINE_TOWER_ANIMATED;   break;
    00832         case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_ANIMATED; break;
    00833         case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:   gfx = GFX_GOLD_MINE_TOWER_ANIMATED;   break;
    00834       }
    00835       SetIndustryGfx(tile, gfx);
    00836       SetIndustryAnimationState(tile, 0x80);
    00837       AddAnimatedTile(tile);
    00838     }
    00839     break;
    00840
    00841   case GFX_OILWELL_NOT_ANIMATED:
    00842     if (Chance16(1, 6)) {
    00843       SetIndustryGfx(tile, GFX_OILWELL_ANIMATED_1);
    00844       SetIndustryAnimationState(tile, 0);
    00845       AddAnimatedTile(tile);
    00846     }
    00847     break;
    00848
    00849   case GFX_COAL_MINE_TOWER_ANIMATED:
    00850   case GFX_COPPER_MINE_TOWER_ANIMATED:
    00851   case GFX_GOLD_MINE_TOWER_ANIMATED:
    00852     if (!(_tick_counter & 0x400)) {
    00853       switch (gfx) {
    00854         case GFX_COAL_MINE_TOWER_ANIMATED:   gfx = GFX_COAL_MINE_TOWER_NOT_ANIMATED;   break;
    00855         case GFX_COPPER_MINE_TOWER_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_NOT_ANIMATED; break;
    00856         case GFX_GOLD_MINE_TOWER_ANIMATED:   gfx = GFX_GOLD_MINE_TOWER_NOT_ANIMATED;   break;
    00857       }
    00858       SetIndustryGfx(tile, gfx);
    00859       SetIndustryCompleted(tile, true);
    00860       SetIndustryConstructionStage(tile, 3);
    00861       DeleteAnimatedTile(tile);
    00862     }
    00863     break;
    00864
    00865   case GFX_POWERPLANT_SPARKS:
    00866     if (Chance16(1, 3)) {
    00867       SndPlayTileFx(SND_0C_ELECTRIC_SPARK, tile);
    00868       AddAnimatedTile(tile);
    00869     }
    00870     break;
    00871
    00872   case GFX_COPPER_MINE_CHIMNEY:
    00873     CreateEffectVehicleAbove(TileX(tile) * TILE_SIZE + 6, TileY(tile) * TILE_SIZE + 6, 43, EV_SMOKE);
    00874     break;
    00875
    00876
    00877   case GFX_TOY_FACTORY: {
    00878       Industry *i = Industry::GetByTile(tile);
    00879       if (i->was_cargo_delivered) {
    00880         i->was_cargo_delivered = false;
    00881         SetIndustryAnimationLoop(tile, 0);
    00882         AddAnimatedTile(tile);
    00883       }
    00884     }
    00885     break;
    00886
    00887   case GFX_BUBBLE_GENERATOR:
    00888     TileLoopIndustry_BubbleGenerator(tile);
    00889     break;
    00890
    00891   case GFX_TOFFEE_QUARY:
    00892     AddAnimatedTile(tile);
    00893     break;
    00894
    00895   case GFX_SUGAR_MINE_SIEVE:
    00896     if (Chance16(1, 3)) AddAnimatedTile(tile);
    00897     break;
    00898   }
    00899 }
    00900
    00901 static bool ClickTile_Industry(TileIndex tile)
    00902 {
    00903   ShowIndustryViewWindow(GetIndustryIndex(tile));
    00904   return true;
    00905 }
    00906
    00907 static TrackStatus GetTileTrackStatus_Industry(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
    00908 {
    00909   return 0;
    00910 }
    00911
    00912 static void ChangeTileOwner_Industry(TileIndex tile, Owner old_owner, Owner new_owner)
    00913 {
    00914   /* If the founder merges, the industry was created by the merged company */
    00915   Industry *i = Industry::GetByTile(tile);
    00916   if (i->founder == old_owner) i->founder = (new_owner == INVALID_OWNER) ? OWNER_NONE : new_owner;
    00917 }
    00918
    00919 static const byte _plantfarmfield_type[] = {1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6};
    00920
    00921 static bool IsBadFarmFieldTile(TileIndex tile)
    00922 {
    00923   switch (GetTileType(tile)) {
    00924     case MP_CLEAR: return IsClearGround(tile, CLEAR_FIELDS) || IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
    00925     case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
    00926     default:       return true;
    00927   }
    00928 }
    00929
    00930 static bool IsBadFarmFieldTile2(TileIndex tile)
    00931 {
    00932   switch (GetTileType(tile)) {
    00933     case MP_CLEAR: return IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
    00934     case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
    00935     default:       return true;
    00936   }
    00937 }
    00938
    00939 static void SetupFarmFieldFence(TileIndex tile, int size, byte type, Axis direction)
    00940 {
    00941   do {
    00942     tile = TILE_MASK(tile);
    00943
    00944     if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) {
    00945       byte or_ = type;
    00946
    00947       if (or_ == 1 && Chance16(1, 7)) or_ = 2;
    00948
    00949       if (direction == AXIS_X) {
    00950         SetFenceSE(tile, or_);
    00951       } else {
    00952         SetFenceSW(tile, or_);
    00953       }
    00954     }
    00955
    00956     tile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
    00957   } while (--size);
    00958 }
    00959
    00960 static void PlantFarmField(TileIndex tile, IndustryID industry)
    00961 {
    00962   uint size_x, size_y;
    00963   uint32 r;
    00964   uint count;
    00965   uint counter;
    00966   uint field_type;
    00967   int type;
    00968
    00969   if (_settings_game.game_creation.landscape == LT_ARCTIC) {
    00970     if (GetTileZ(tile) + TILE_HEIGHT * 2 >= GetSnowLine())
    00971       return;
    00972   }
    00973
    00974   /* determine field size */
    00975   r = (Random() & 0x303) + 0x404;
    00976   if (_settings_game.game_creation.landscape == LT_ARCTIC) r += 0x404;
    00977   size_x = GB(r, 0, 8);
    00978   size_y = GB(r, 8, 8);
    00979
    00980   /* offset tile to match size */
    00981   tile -= TileDiffXY(size_x / 2, size_y / 2);
    00982
    00983   if (TileX(tile) + size_x >= MapSizeX() || TileY(tile) + size_y >= MapSizeY()) return;
    00984
    00985   /* check the amount of bad tiles */
    00986   count = 0;
    00987   TILE_LOOP(cur_tile, size_x, size_y, tile) {
    00988     assert(cur_tile < MapSize());
    00989     count += IsBadFarmFieldTile(cur_tile);
    00990   }
    00991   if (count * 2 >= size_x * size_y) return;
    00992
    00993   /* determine type of field */
    00994   r = Random();
    00995   counter = GB(r, 5, 3);
    00996   field_type = GB(r, 8, 8) * 9 >> 8;
    00997
    00998   /* make field */
    00999   TILE_LOOP(cur_tile, size_x, size_y, tile) {
    01000     assert(cur_tile < MapSize());
    01001     if (!IsBadFarmFieldTile2(cur_tile)) {
    01002       MakeField(cur_tile, field_type, industry);
    01003       SetClearCounter(cur_tile, counter);
    01004       MarkTileDirtyByTile(cur_tile);
    01005     }
    01006   }
    01007
    01008   type = 3;
    01009   if (_settings_game.game_creation.landscape != LT_ARCTIC && _settings_game.game_creation.landscape != LT_TROPIC) {
    01010     type = _plantfarmfield_type[Random() & 0xF];
    01011   }
    01012
    01013   SetupFarmFieldFence(tile - TileDiffXY(1, 0), size_y, type, AXIS_Y);
    01014   SetupFarmFieldFence(tile - TileDiffXY(0, 1), size_x, type, AXIS_X);
    01015   SetupFarmFieldFence(tile + TileDiffXY(size_x - 1, 0), size_y, type, AXIS_Y);
    01016   SetupFarmFieldFence(tile + TileDiffXY(0, size_y - 1), size_x, type, AXIS_X);
    01017 }
    01018
    01019 void PlantRandomFarmField(const Industry *i)
    01020 {
    01021   int x = i->location.w / 2 + Random() % 31 - 16;
    01022   int y = i->location.h / 2 + Random() % 31 - 16;
    01023
    01024   TileIndex tile = TileAddWrap(i->location.tile, x, y);
    01025
    01026   if (tile != INVALID_TILE) PlantFarmField(tile, i->index);
    01027 }
    01028
    01035 static bool SearchLumberMillTrees(TileIndex tile, void *user_data)
    01036 {
    01037   if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) {
    01038     CompanyID old_company = _current_company;
    01039     /* found a tree */
    01040
    01041     _current_company = OWNER_NONE;
    01042     _industry_sound_ctr = 1;
    01043     _industry_sound_tile = tile;
    01044     SndPlayTileFx(SND_38_CHAINSAW, tile);
    01045
    01046     DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
    01047
    01048     _current_company = old_company;
    01049     return true;
    01050   }
    01051   return false;
    01052 }
    01053
    01058 static void ChopLumberMillTrees(Industry *i)
    01059 {
    01060   TileIndex tile = i->location.tile;
    01061
    01062   if (!IsIndustryCompleted(tile)) return;
    01063
    01064   if (CircularTileSearch(&tile, 40, SearchLumberMillTrees, NULL))
    01065     i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + 45);
    01066 }
    01067
    01068 static void ProduceIndustryGoods(Industry *i)
    01069 {
    01070   uint32 r;
    01071   uint num;
    01072   const IndustrySpec *indsp = GetIndustrySpec(i->type);
    01073
    01074   /* play a sound? */
    01075   if ((i->counter & 0x3F) == 0) {
    01076     if (Chance16R(1, 14, r) && (num = indsp->number_of_sounds) != 0) {
    01077       SndPlayTileFx(
    01078         (SoundFx)(indsp->random_sounds[((r >> 16) * num) >> 16]),
    01079         i->location.tile);
    01080     }
    01081   }
    01082
    01083   i->counter--;
    01084
    01085   /* produce some cargo */
    01086   if ((i->counter & 0xFF) == 0) {
    01087     if (HasBit(indsp->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) IndustryProductionCallback(i, 1);
    01088
    01089     IndustryBehaviour indbehav = indsp->behaviour;
    01090     i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + i->production_rate[0]);
    01091     i->produced_cargo_waiting[1] = min(0xffff, i->produced_cargo_waiting[1] + i->production_rate[1]);
    01092
    01093     if ((indbehav & INDUSTRYBEH_PLANT_FIELDS) != 0) {
    01094       bool plant;
    01095       if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
    01096         plant = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, Random(), 0, i, i->type, i->location.tile) != 0);
    01097       } else {
    01098         plant = Chance16(1, 8);
    01099       }
    01100
    01101       if (plant) PlantRandomFarmField(i);
    01102     }
    01103     if ((indbehav & INDUSTRYBEH_CUT_TREES) != 0) {
    01104       bool cut = ((i->counter & 0x1FF) == 0);
    01105       if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
    01106         cut = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, 0, 1, i, i->type, i->location.tile) != 0);
    01107       }
    01108
    01109       if (cut) ChopLumberMillTrees(i);
    01110     }
    01111
    01112     TriggerIndustry(i, INDUSTRY_TRIGGER_INDUSTRY_TICK);
    01113     StartStopIndustryTileAnimation(i, IAT_INDUSTRY_TICK);
    01114   }
    01115 }
    01116
    01117 void OnTick_Industry()
    01118 {
    01119   Industry *i;
    01120
    01121   if (_industry_sound_ctr != 0) {
    01122     _industry_sound_ctr++;
    01123
    01124     if (_industry_sound_ctr == 75) {
    01125       SndPlayTileFx(SND_37_BALLOON_SQUEAK, _industry_sound_tile);
    01126     } else if (_industry_sound_ctr == 160) {
    01127       _industry_sound_ctr = 0;
    01128       SndPlayTileFx(SND_36_CARTOON_CRASH, _industry_sound_tile);
    01129     }
    01130   }
    01131
    01132   if (_game_mode == GM_EDITOR) return;
    01133
    01134   FOR_ALL_INDUSTRIES(i) {
    01135     ProduceIndustryGoods(i);
    01136   }
    01137 }
    01138
    01143 static bool CheckNewIndustry_NULL(TileIndex tile)
    01144 {
    01145   return true;
    01146 }
    01147
    01152 static bool CheckNewIndustry_Forest(TileIndex tile)
    01153 {
    01154   if (_settings_game.game_creation.landscape == LT_ARCTIC) {
    01155     if (GetTileZ(tile) < HighestSnowLine() + TILE_HEIGHT * 2U) {
    01156       _error_message = STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED;
    01157       return false;
    01158     }
    01159   }
    01160   return true;
    01161 }
    01162
    01167 static bool CheckNewIndustry_OilRefinery(TileIndex tile)
    01168 {
    01169   if (_game_mode == GM_EDITOR) return true;
    01170   if (DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return true;
    01171
    01172   _error_message = STR_ERROR_CAN_ONLY_BE_POSITIONED;
    01173   return false;
    01174 }
    01175
    01176 extern bool _ignore_restrictions;
    01177
    01182 static bool CheckNewIndustry_OilRig(TileIndex tile)
    01183 {
    01184   if (_game_mode == GM_EDITOR && _ignore_restrictions) return true;
    01185   if (TileHeight(tile) == 0 &&
    01186       DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return true;
    01187
    01188   _error_message = STR_ERROR_CAN_ONLY_BE_POSITIONED;
    01189   return false;
    01190 }
    01191
    01196 static bool CheckNewIndustry_Farm(TileIndex tile)
    01197 {
    01198   if (_settings_game.game_creation.landscape == LT_ARCTIC) {
    01199     if (GetTileZ(tile) + TILE_HEIGHT * 2 >= HighestSnowLine()) {
    01200       _error_message = STR_ERROR_SITE_UNSUITABLE;
    01201       return false;
    01202     }
    01203   }
    01204   return true;
    01205 }
    01206
    01211 static bool CheckNewIndustry_Plantation(TileIndex tile)
    01212 {
    01213   if (GetTropicZone(tile) == TROPICZONE_DESERT) {
    01214     _error_message = STR_ERROR_SITE_UNSUITABLE;
    01215     return false;
    01216   }
    01217
    01218   return true;
    01219 }
    01220
    01225 static bool CheckNewIndustry_Water(TileIndex tile)
    01226 {
    01227   if (GetTropicZone(tile) != TROPICZONE_DESERT) {
    01228     _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT;
    01229     return false;
    01230   }
    01231
    01232   return true;
    01233 }
    01234
    01239 static bool CheckNewIndustry_Lumbermill(TileIndex tile)
    01240 {
    01241   if (GetTropicZone(tile) != TROPICZONE_RAINFOREST) {
    01242     _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST;
    01243     return false;
    01244   }
    01245   return true;
    01246 }
    01247
    01252 static bool CheckNewIndustry_BubbleGen(TileIndex tile)
    01253 {
    01254   if (GetTileZ(tile) > TILE_HEIGHT * 4) {
    01255     _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_LOW_AREAS;
    01256     return false;
    01257   }
    01258   return true;
    01259 }
    01260
    01265 typedef bool CheckNewIndustryProc(TileIndex tile);
    01266
    01268 static CheckNewIndustryProc * const _check_new_industry_procs[CHECK_END] = {
    01269   CheckNewIndustry_NULL,
    01270   CheckNewIndustry_Forest,
    01271   CheckNewIndustry_OilRefinery,
    01272   CheckNewIndustry_Farm,
    01273   CheckNewIndustry_Plantation,
    01274   CheckNewIndustry_Water,
    01275   CheckNewIndustry_Lumbermill,
    01276   CheckNewIndustry_BubbleGen,
    01277   CheckNewIndustry_OilRig,
    01278 };
    01279
    01286 static const Town *FindTownForIndustry(TileIndex tile, int type)
    01287 {
    01288   const Town *t;
    01289   const Industry *i;
    01290
    01291   t = ClosestTownFromTile(tile, UINT_MAX);
    01292
    01293   if (_settings_game.economy.multiple_industry_per_town) return t;
    01294
    01295   FOR_ALL_INDUSTRIES(i) {
    01296     if (i->type == (byte)type &&
    01297         i->town == t) {
    01298       _error_message = STR_ERROR_ONLY_ONE_ALLOWED_PER_TOWN;
    01299       return NULL;
    01300     }
    01301   }
    01302
    01303   return t;
    01304 }
    01305
    01306 bool IsSlopeRefused(Slope current, Slope refused)
    01307 {
    01308   if (IsSteepSlope(current)) return true;
    01309   if (current != SLOPE_FLAT) {
    01310     if (IsSteepSlope(refused)) return true;
    01311
    01312     Slope t = ComplementSlope(current);
    01313
    01314     if ((refused & SLOPE_W) && (t & SLOPE_NW)) return true;
    01315     if ((refused & SLOPE_S) && (t & SLOPE_NE)) return true;
    01316     if ((refused & SLOPE_E) && (t & SLOPE_SW)) return true;
    01317     if ((refused & SLOPE_N) && (t & SLOPE_SE)) return true;
    01318   }
    01319
    01320   return false;
    01321 }
    01322
    01323 static bool CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTileTable *it, uint itspec_index, int type, bool *custom_shape_check = NULL)
    01324 {
    01325   _error_message = STR_ERROR_SITE_UNSUITABLE;
    01326   bool refused_slope = false;
    01327   bool custom_shape = false;
    01328
    01329   do {
    01330     IndustryGfx gfx = GetTranslatedIndustryTileID(it->gfx);
    01331     TileIndex cur_tile = TileAddWrap(tile, it->ti.x, it->ti.y);
    01332
    01333     if (!IsValidTile(cur_tile)) {
    01334       return false;
    01335     }
    01336
    01337     if (gfx == GFX_WATERTILE_SPECIALCHECK) {
    01338       if (!IsTileType(cur_tile, MP_WATER) ||
    01339           GetTileSlope(cur_tile, NULL) != SLOPE_FLAT) {
    01340         return false;
    01341       }
    01342     } else {
    01343       if (!EnsureNoVehicleOnGround(cur_tile)) return false;
    01344       if (MayHaveBridgeAbove(cur_tile) && IsBridgeAbove(cur_tile)) return false;
    01345
    01346       const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
    01347
    01348       IndustryBehaviour ind_behav = GetIndustrySpec(type)->behaviour;
    01349
    01350       /* Perform land/water check if not disabled */
    01351       if (!HasBit(its->slopes_refused, 5) && (IsWaterTile(cur_tile) == !(ind_behav & INDUSTRYBEH_BUILT_ONWATER))) return false;
    01352
    01353       if (HasBit(its->callback_mask, CBM_INDT_SHAPE_CHECK)) {
    01354         custom_shape = true;
    01355         if (!PerformIndustryTileSlopeCheck(tile, cur_tile, its, type, gfx, itspec_index)) return false;
    01356       } else {
    01357         Slope tileh = GetTileSlope(cur_tile, NULL);
    01358         refused_slope |= IsSlopeRefused(tileh, its->slopes_refused);
    01359       }
    01360
    01361       if ((ind_behav & (INDUSTRYBEH_ONLY_INTOWN | INDUSTRYBEH_TOWN1200_MORE)) || // Tile must be a house
    01362           ((ind_behav & INDUSTRYBEH_ONLY_NEARTOWN) && IsTileType(cur_tile, MP_HOUSE))) { // Tile is allowed to be a house (and it is a house)
    01363         if (!IsTileType(cur_tile, MP_HOUSE)) {
    01364           _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS;
    01365           return false;
    01366         }
    01367
    01368         /* Clear the tiles as OWNER_TOWN to not affect town rating, and to not clear protected buildings */
    01369         CompanyID old_company = _current_company;
    01370         _current_company = OWNER_TOWN;
    01371         bool not_clearable = DoCommand(cur_tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR).Failed();
    01372         _current_company = old_company;
    01373
    01374         if (not_clearable) return false;
    01375       } else {
    01376         /* Clear the tiles, but do not affect town ratings */
    01377         bool not_clearable = DoCommand(cur_tile, 0, 0, DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR).Failed();
    01378
    01379         if (not_clearable) return false;
    01380       }
    01381     }
    01382   } while ((++it)->ti.x != -0x80);
    01383
    01384   if (custom_shape_check != NULL) *custom_shape_check = custom_shape;
    01385
    01386   /* It is almost impossible to have a fully flat land in TG, so what we
    01387    *  do is that we check if we can make the land flat later on. See
    01388    *  CheckIfCanLevelIndustryPlatform(). */
    01389   return !refused_slope || (_settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !custom_shape && !_ignore_restrictions);
    01390 }
    01391
    01392 static bool CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t)
    01393 {
    01394   if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_TOWN1200_MORE) && t->population < 1200) {
    01395     _error_message = STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS_WITH_POPULATION_OF_1200;
    01396     return false;
    01397   }
    01398
    01399   if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_ONLY_NEARTOWN) && DistanceMax(t->xy, tile) > 9) {
    01400     _error_message = STR_ERROR_SITE_UNSUITABLE;
    01401     return false;
    01402   }
    01403
    01404   return true;
    01405 }
    01406
    01407 static bool CheckCanTerraformSurroundingTiles(TileIndex tile, uint height, int internal)
    01408 {
    01409   int size_x, size_y;
    01410   uint curh;
    01411
    01412   size_x = 2;
    01413   size_y = 2;
    01414
    01415   /* Check if we don't leave the map */
    01416   if (TileX(tile) == 0 || TileY(tile) == 0 || GetTileType(tile) == MP_VOID) return false;
    01417
    01418   tile += TileDiffXY(-1, -1);
    01419   TILE_LOOP(tile_walk, size_x, size_y, tile) {
    01420     curh = TileHeight(tile_walk);
    01421     /* Is the tile clear? */
    01422     if ((GetTileType(tile_walk) != MP_CLEAR) && (GetTileType(tile_walk) != MP_TREES))
    01423       return false;
    01424
    01425     /* Don't allow too big of a change if this is the sub-tile check */
    01426     if (internal != 0 && Delta(curh, height) > 1) return false;
    01427
    01428     /* Different height, so the surrounding tiles of this tile
    01429      *  has to be correct too (in level, or almost in level)
    01430      *  else you get a chain-reaction of terraforming. */
    01431     if (internal == 0 && curh != height) {
    01432       if (TileX(tile_walk) == 0 || TileY(tile_walk) == 0 || !CheckCanTerraformSurroundingTiles(tile_walk + TileDiffXY(-1, -1), height, internal + 1))
    01433         return false;
    01434     }
    01435   }
    01436
    01437   return true;
    01438 }
    01439
    01444 static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlag flags, const IndustryTileTable *it, int type)
    01445 {
    01446   const int MKEND = -0x80;   // used for last element in an IndustryTileTable (see build_industry.h)
    01447   int max_x = 0;
    01448   int max_y = 0;
    01449   TileIndex cur_tile;
    01450   uint size_x, size_y;
    01451   uint h, curh;
    01452
    01453   /* Finds dimensions of largest variant of this industry */
    01454   do {
    01455     if (it->gfx == 0xFF) continue;  //  FF been a marquer for a check on clear water, skip it
    01456     if (it->ti.x > max_x) max_x = it->ti.x;
    01457     if (it->ti.y > max_y) max_y = it->ti.y;
    01458   } while ((++it)->ti.x != MKEND);
    01459
    01460   /* Remember level height */
    01461   h = TileHeight(tile);
    01462
    01463   if (TileX(tile) <= 1 || TileY(tile) <= 1) return false;
    01464   /* Check that all tiles in area and surrounding are clear
    01465    * this determines that there are no obstructing items */
    01466   cur_tile = tile + TileDiffXY(-1, -1);
    01467   size_x = max_x + 4;
    01468   size_y = max_y + 4;
    01469
    01470   /* Check if we don't leave the map */
    01471   if (TileX(cur_tile) + size_x >= MapMaxX() || TileY(cur_tile) + size_y >= MapMaxY()) return false;
    01472
    01473   /* _current_company is OWNER_NONE for randomly generated industries and in editor, or the company who funded or prospected the industry.
    01474    * Perform terraforming as OWNER_TOWN to disable autoslope and town ratings. */
    01475   CompanyID old_company = _current_company;
    01476   _current_company = OWNER_TOWN;
    01477
    01478   TILE_LOOP(tile_walk, size_x, size_y, cur_tile) {
    01479     curh = TileHeight(tile_walk);
    01480     if (curh != h) {
    01481       /* This tile needs terraforming. Check if we can do that without
    01482        *  damaging the surroundings too much. */
    01483       if (!CheckCanTerraformSurroundingTiles(tile_walk, h, 0)) {
    01484         _current_company = old_company;
    01485         return false;
    01486       }
    01487       /* This is not 100% correct check, but the best we can do without modifying the map.
    01488        *  What is missing, is if the difference in height is more than 1.. */
    01489       if (DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND).Failed()) {
    01490         _current_company = old_company;
    01491         return false;
    01492       }
    01493     }
    01494   }
    01495
    01496   if (flags & DC_EXEC) {
    01497     /* Terraform the land under the industry */
    01498     TILE_LOOP(tile_walk, size_x, size_y, cur_tile) {
    01499       curh = TileHeight(tile_walk);
    01500       while (curh != h) {
    01501         /* We give the terraforming for free here, because we can't calculate
    01502          *  exact cost in the test-round, and as we all know, that will cause
    01503          *  a nice assert if they don't match ;) */
    01504         DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
    01505         curh += (curh > h) ? -1 : 1;
    01506       }
    01507     }
    01508   }
    01509
    01510   _current_company = old_company;
    01511   return true;
    01512 }
    01513
    01514
    01515 static bool CheckIfFarEnoughFromIndustry(TileIndex tile, int type)
    01516 {
    01517   const IndustrySpec *indspec = GetIndustrySpec(type);
    01518   const Industry *i;
    01519
    01520   if (_settings_game.economy.same_industry_close && indspec->IsRawIndustry())
    01521     /* Allow primary industries to be placed close to any other industry */
    01522     return true;
    01523
    01524   FOR_ALL_INDUSTRIES(i) {
    01525     /* Within 14 tiles from another industry is considered close */
    01526     bool in_low_distance = DistanceMax(tile, i->location.tile) <= 14;
    01527
    01528     /* check if an industry that accepts the same goods is nearby */
    01529     if (in_low_distance &&
    01530         !indspec->IsRawIndustry() && // not a primary industry?
    01531         indspec->accepts_cargo[0] == i->accepts_cargo[0] && (
    01532         /* at least one of those options must be true */
    01533         _game_mode != GM_EDITOR || // editor must not be stopped
    01534         !_settings_game.economy.same_industry_close ||
    01535         !_settings_game.economy.multiple_industry_per_town)) {
    01536       _error_message = STR_ERROR_INDUSTRY_TOO_CLOSE;
    01537       return false;
    01538     }
    01539
    01540     /* check if there are any conflicting industry types around */
    01541     if ((i->type == indspec->conflicting[0] ||
    01542         i->type == indspec->conflicting[1] ||
    01543         i->type == indspec->conflicting[2]) &&
    01544         in_low_distance) {
    01545       _error_message = STR_ERROR_INDUSTRY_TOO_CLOSE;
    01546       return false;
    01547     }
    01548   }
    01549   return true;
    01550 }
    01551
    01555 enum ProductionLevels {
    01556   PRODLEVEL_CLOSURE = 0x00,
    01557   PRODLEVEL_MINIMUM = 0x04,
    01558   PRODLEVEL_DEFAULT = 0x10,
    01559   PRODLEVEL_MAXIMUM = 0x80,
    01560 };
    01561
    01562 static void DoCreateNewIndustry(Industry *i, TileIndex tile, int type, const IndustryTileTable *it, byte layout, const Town *t, Owner owner, Owner founder)
    01563 {
    01564   const IndustrySpec *indspec = GetIndustrySpec(type);
    01565   uint32 r;
    01566   uint j;
    01567
    01568   i->location = TileArea(tile, 1, 1);
    01569   i->type = type;
    01570   IncIndustryTypeCount(type);
    01571
    01572   i->produced_cargo[0] = indspec->produced_cargo[0];
    01573   i->produced_cargo[1] = indspec->produced_cargo[1];
    01574   i->accepts_cargo[0] = indspec->accepts_cargo[0];
    01575   i->accepts_cargo[1] = indspec->accepts_cargo[1];
    01576   i->accepts_cargo[2] = indspec->accepts_cargo[2];
    01577   i->production_rate[0] = indspec->production_rate[0];
    01578   i->production_rate[1] = indspec->production_rate[1];
    01579
    01580   /* don't use smooth economy for industries using production related callbacks */
    01581   if (_settings_game.economy.smooth_economy &&
    01582       !(HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_256_TICKS) || HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) && // production callbacks
    01583       !(HasBit(indspec->callback_mask, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_CHANGE))             // production change callbacks
    01584   ) {
    01585     i->production_rate[0] = min((RandomRange(256) + 128) * i->production_rate[0] >> 8, 255);
    01586     i->production_rate[1] = min((RandomRange(256) + 128) * i->production_rate[1] >> 8, 255);
    01587   }
    01588
    01589   i->town = t;
    01590   i->owner = owner;
    01591
    01592   r = Random();
    01593   i->random_colour = GB(r, 0, 4);
    01594   i->counter = GB(r, 4, 12);
    01595   i->random = GB(r, 16, 16);
    01596   i->produced_cargo_waiting[0] = 0;
    01597   i->produced_cargo_waiting[1] = 0;
    01598   i->incoming_cargo_waiting[0] = 0;
    01599   i->incoming_cargo_waiting[1] = 0;
    01600   i->incoming_cargo_waiting[2] = 0;
    01601   i->this_month_production[0] = 0;
    01602   i->this_month_production[1] = 0;
    01603   i->this_month_transported[0] = 0;
    01604   i->this_month_transported[1] = 0;
    01605   i->last_month_pct_transported[0] = 0;
    01606   i->last_month_pct_transported[1] = 0;
    01607   i->last_month_transported[0] = 0;
    01608   i->last_month_transported[1] = 0;
    01609   i->was_cargo_delivered = false;
    01610   i->last_prod_year = _cur_year;
    01611   i->last_month_production[0] = i->production_rate[0] * 8;
    01612   i->last_month_production[1] = i->production_rate[1] * 8;
    01613   i->founder = founder;
    01614
    01615   if (HasBit(indspec->callback_mask, CBM_IND_DECIDE_COLOUR)) {
    01616     uint16 res = GetIndustryCallback(CBID_INDUSTRY_DECIDE_COLOUR, 0, 0, i, type, INVALID_TILE);
    01617     if (res != CALLBACK_FAILED) i->random_colour = GB(res, 0, 4);
    01618   }
    01619
    01620   if (HasBit(indspec->callback_mask, CBM_IND_INPUT_CARGO_TYPES)) {
    01621     for (j = 0; j < lengthof(i->accepts_cargo); j++) i->accepts_cargo[j] = CT_INVALID;
    01622     for (j = 0; j < lengthof(i->accepts_cargo); j++) {
    01623       uint16 res = GetIndustryCallback(CBID_INDUSTRY_INPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
    01624       if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
    01625       i->accepts_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
    01626     }
    01627   }
    01628
    01629   if (HasBit(indspec->callback_mask, CBM_IND_OUTPUT_CARGO_TYPES)) {
    01630     for (j = 0; j < lengthof(i->produced_cargo); j++) i->produced_cargo[j] = CT_INVALID;
    01631     for (j = 0; j < lengthof(i->produced_cargo); j++) {
    01632       uint16 res = GetIndustryCallback(CBID_INDUSTRY_OUTPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
    01633       if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
    01634       i->produced_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
    01635     }
    01636   }
    01637
    01638   i->construction_date = _date;
    01639   i->construction_type = (_game_mode == GM_EDITOR) ? ICT_SCENARIO_EDITOR :
    01640       (_generating_world ? ICT_MAP_GENERATION : ICT_NORMAL_GAMEPLAY);
    01641
    01642   /* Adding 1 here makes it conform to specs of var44 of varaction2 for industries
    01643    * 0 = created prior of newindustries
    01644    * else, chosen layout + 1 */
    01645   i->selected_layout = layout + 1;
    01646
    01647   if (!_generating_world) i->last_month_production[0] = i->last_month_production[1] = 0;
    01648
    01649   i->prod_level = PRODLEVEL_DEFAULT;
    01650
    01651   do {
    01652     TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
    01653
    01654     if (it->gfx != GFX_WATERTILE_SPECIALCHECK) {
    01655       i->location.Add(cur_tile);
    01656
    01657       WaterClass wc = (IsWaterTile(cur_tile) ? GetWaterClass(cur_tile) : WATER_CLASS_INVALID);
    01658
    01659       DoCommand(cur_tile, 0, 0, DC_EXEC | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
    01660
    01661       MakeIndustry(cur_tile, i->index, it->gfx, Random(), wc);
    01662
    01663       if (_generating_world) {
    01664         SetIndustryConstructionCounter(cur_tile, 3);
    01665         SetIndustryConstructionStage(cur_tile, 2);
    01666       }
    01667
    01668       /* it->gfx is stored in the map. But the translated ID cur_gfx is the interesting one */
    01669       IndustryGfx cur_gfx = GetTranslatedIndustryTileID(it->gfx);
    01670       const IndustryTileSpec *its = GetIndustryTileSpec(cur_gfx);
    01671       if (its->animation_info != 0xFFFF) AddAnimatedTile(cur_tile);
    01672     }
    01673   } while ((++it)->ti.x != -0x80);
    01674
    01675   if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
    01676     for (j = 0; j != 50; j++) PlantRandomFarmField(i);
    01677   }
    01678   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
    01679
    01680   Station::RecomputeIndustriesNearForAll();
    01681 }
    01682
    01693 static Industry *CreateNewIndustryHelper(TileIndex tile, IndustryType type, DoCommandFlag flags, const IndustrySpec *indspec, uint itspec_index, uint32 seed, Owner founder)
    01694 {
    01695   assert(itspec_index < indspec->num_table);
    01696   const IndustryTileTable *it = indspec->table[itspec_index];
    01697   bool custom_shape_check = false;
    01698
    01699   if (!CheckIfIndustryTilesAreFree(tile, it, itspec_index, type, &custom_shape_check)) return NULL;
    01700
    01701   if (HasBit(GetIndustrySpec(type)->callback_mask, CBM_IND_LOCATION)) {
    01702     if (!CheckIfCallBackAllowsCreation(tile, type, itspec_index, seed)) return NULL;
    01703   } else {
    01704     if (!_check_new_industry_procs[indspec->check_proc](tile)) return NULL;
    01705   }
    01706
    01707   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;
    01708   if (!CheckIfFarEnoughFromIndustry(tile, type)) return NULL;
    01709
    01710   const Town *t = FindTownForIndustry(tile, type);
    01711   if (t == NULL) return NULL;
    01712
    01713   if (!CheckIfIndustryIsAllowed(tile, type, t)) return NULL;
    01714
    01715   if (!Industry::CanAllocateItem()) return NULL;
    01716
    01717   if (flags & DC_EXEC) {
    01718     Industry *i = new Industry(tile);
    01719     if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER | DC_EXEC, it, type);
    01720     DoCreateNewIndustry(i, tile, type, it, itspec_index, t, OWNER_NONE, founder);
    01721
    01722     return i;
    01723   }
    01724
    01725   /* We need to return a non-NULL pointer to tell we have created an industry.
    01726    * However, we haven't created a real one (no DC_EXEC), so return a fake one. */
    01727   return (Industry *)-1;
    01728 }
    01729
    01740 CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01741 {
    01742   IndustryType it = GB(p1, 0, 8);
    01743   if (it >= NUM_INDUSTRYTYPES) return CMD_ERROR;
    01744
    01745   const IndustrySpec *indspec = GetIndustrySpec(it);
    01746
    01747   /* Check if the to-be built/founded industry is available for this climate. */
    01748   if (!indspec->enabled || indspec->num_table == 0) return CMD_ERROR;
    01749
    01750   /* If the setting for raw-material industries is not on, you cannot build raw-material industries.
    01751    * Raw material industries are industries that do not accept cargo (at least for now) */
    01752   if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
    01753     return CMD_ERROR;
    01754   }
    01755
    01756   const Industry *ind = NULL;
    01757   if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indspec->IsRawIndustry()) {
    01758     if (flags & DC_EXEC) {
    01759       /* Prospected industries are build as OWNER_TOWN to not e.g. be build on owned land of the founder */
    01760       CompanyID founder = _current_company;
    01761       _current_company = OWNER_TOWN;
    01762       /* Prospecting has a chance to fail, however we cannot guarantee that something can
    01763        * be built on the map, so the chance gets lower when the map is fuller, but there
    01764        * is nothing we can really do about that. */
    01765       if (Random() <= indspec->prospecting_chance) {
    01766         for (int i = 0; i < 5000; i++) {
    01767           /* We should not have more than one Random() in a function call
    01768            * because parameter evaluation order is not guaranteed in the c++ standard
    01769            */
    01770           tile = RandomTile();
    01771           ind = CreateNewIndustryHelper(tile, it, flags, indspec, RandomRange(indspec->num_table), p2, founder);
    01772           if (ind != NULL) {
    01773             break;
    01774           }
    01775         }
    01776       }
    01777       _current_company = founder;
    01778     }
    01779   } else {
    01780     int count = indspec->num_table;
    01781     const IndustryTileTable * const *itt = indspec->table;
    01782     int num = GB(p1, 8, 8);
    01783     if (num >= count) return CMD_ERROR;
    01784
    01785     _error_message = STR_ERROR_SITE_UNSUITABLE;
    01786     do {
    01787       if (--count < 0) return CMD_ERROR;
    01788       if (--num < 0) num = indspec->num_table - 1;
    01789     } while (!CheckIfIndustryTilesAreFree(tile, itt[num], num, it));
    01790
    01791     ind = CreateNewIndustryHelper(tile, it, flags, indspec, num, p2, _current_company);
    01792     if (ind == NULL) return CMD_ERROR;
    01793   }
    01794
    01795   if ((flags & DC_EXEC) && _game_mode != GM_EDITOR && ind != NULL) {
    01796     SetDParam(0, indspec->name);
    01797     if (indspec->new_industry_text > STR_LAST_STRINGID) {
    01798       SetDParam(1, STR_TOWN_NAME);
    01799       SetDParam(2, ind->town->index);
    01800     } else {
    01801       SetDParam(1, ind->town->index);
    01802     }
    01803     AddIndustryNewsItem(indspec->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
    01804     AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
    01805   }
    01806
    01807   return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost());
    01808 }
    01809
    01810
    01811 static Industry *CreateNewIndustry(TileIndex tile, IndustryType type)
    01812 {
    01813   const IndustrySpec *indspec = GetIndustrySpec(type);
    01814
    01815   uint32 seed = Random();
    01816   return CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, RandomRange(indspec->num_table), seed, OWNER_NONE);
    01817 }
    01818
    01825 static uint32 GetScaledIndustryProbability(IndustryType it, bool *force_at_least_one)
    01826 {
    01827   const IndustrySpec *ind_spc = GetIndustrySpec(it);
    01828   uint32 chance = ind_spc->appear_creation[_settings_game.game_creation.landscape] * 16; // * 16 to increase precision
    01829   if (!ind_spc->enabled || chance == 0 || ind_spc->num_table == 0 ||
    01830       !CheckIfCallBackAllowsAvailability(it, IACT_MAPGENERATION) || _settings_game.difficulty.number_industries == 0) {
    01831     *force_at_least_one = false;
    01832     return 0;
    01833   } else {
    01834     /* We want industries appearing at coast to appear less often on bigger maps, as length of coast increases slower than map area.
    01835      * For simplicity we scale in both cases, though scaling the probabilities of all industries has no effect. */
    01836     chance = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(chance) : ScaleByMapSize(chance);
    01837
    01838     *force_at_least_one = (chance > 0) && !(ind_spc->behaviour & INDUSTRYBEH_NOBUILT_MAPCREATION);
    01839     return chance;
    01840   }
    01841 }
    01842
    01844 static const byte _numof_industry_table[]= {
    01845   0,    // none
    01846   10,   // very low
    01847   25,   // low
    01848   55,   // normal
    01849   80,   // high
    01850 };
    01851
    01857 static void PlaceInitialIndustry(IndustryType type, bool try_hard)
    01858 {
    01859   CompanyID old_company = _current_company;
    01860   _current_company = OWNER_NONE;
    01861
    01862   IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
    01863
    01864   for (uint i = 0; i < (try_hard ? 10000u : 2000u); i++) {
    01865     if (CreateNewIndustry(RandomTile(), type) != NULL) break;
    01866   }
    01867
    01868   _current_company = old_company;
    01869 }
    01870
    01875 void GenerateIndustries()
    01876 {
    01877   assert(_settings_game.difficulty.number_industries < lengthof(_numof_industry_table));
    01878   uint total_amount = ScaleByMapSize(_numof_industry_table[_settings_game.difficulty.number_industries]);
    01879
    01880   /* Do not create any industries? */
    01881   if (total_amount == 0) return;
    01882
    01883   uint32 industry_probs[NUM_INDUSTRYTYPES];
    01884   bool force_at_least_one[NUM_INDUSTRYTYPES];
    01885   uint32 total_prob = 0;
    01886   uint num_forced = 0;
    01887
    01888   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
    01889     industry_probs[it] = GetScaledIndustryProbability(it, force_at_least_one + it);
    01890     total_prob += industry_probs[it];
    01891     if (force_at_least_one[it]) num_forced++;
    01892   }
    01893
    01894   if (total_prob == 0 || total_amount < num_forced) {
    01895     /* Only place the forced ones */
    01896     total_amount = num_forced;
    01897   }
    01898
    01899   SetGeneratingWorldProgress(GWP_INDUSTRY, total_amount);
    01900
    01901   /* Try to build one industry per type independent of any probabilities */
    01902   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
    01903     if (force_at_least_one[it]) {
    01904       assert(total_amount > 0);
    01905       total_amount--;
    01906       PlaceInitialIndustry(it, true);
    01907     }
    01908   }
    01909
    01910   /* Add the remaining industries according to their probabilities */
    01911   for (uint i = 0; i < total_amount; i++) {
    01912     uint32 r = RandomRange(total_prob);
    01913     IndustryType it = 0;
    01914     while (it < NUM_INDUSTRYTYPES && r >= industry_probs[it]) {
    01915       r -= industry_probs[it];
    01916       it++;
    01917     }
    01918     assert(it < NUM_INDUSTRYTYPES && industry_probs[it] > 0);
    01919     PlaceInitialIndustry(it, false);
    01920   }
    01921 }
    01922
    01923 static void UpdateIndustryStatistics(Industry *i)
    01924 {
    01925   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    01926     if (i->produced_cargo[j] != CT_INVALID) {
    01927       byte pct = 0;
    01928       if (i->this_month_production[j] != 0) {
    01929         i->last_prod_year = _cur_year;
    01930         pct = min(i->this_month_transported[j] * 256 / i->this_month_production[j], 255);
    01931       }
    01932       i->last_month_pct_transported[j] = pct;
    01933
    01934       i->last_month_production[j] = i->this_month_production[j];
    01935       i->this_month_production[j] = 0;
    01936
    01937       i->last_month_transported[j] = i->this_month_transported[j];
    01938       i->this_month_transported[j] = 0;
    01939     }
    01940   }
    01941 }
    01942
    01944 struct ProbabilityHelper {
    01945   uint16 prob;
    01946   IndustryType ind;
    01947 };
    01948
    01952 static void MaybeNewIndustry()
    01953 {
    01954   Industry *ind;               // will receive the industry's creation pointer
    01955   IndustryType rndtype, j;     // Loop controlers
    01956   const IndustrySpec *ind_spc;
    01957   uint num = 0;
    01958   ProbabilityHelper cumulative_probs[NUM_INDUSTRYTYPES]; // probability collector
    01959   uint16 probability_max = 0;
    01960
    01961   /* Generate a list of all possible industries that can be built. */
    01962   for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
    01963     ind_spc = GetIndustrySpec(j);
    01964     byte chance = ind_spc->appear_ingame[_settings_game.game_creation.landscape];
    01965
    01966     if (!ind_spc->enabled || chance == 0 || ind_spc->num_table == 0) continue;
    01967
    01968     /* If there is no Callback CBID_INDUSTRY_AVAILABLE or if this one did anot failed,
    01969      * and if appearing chance for this landscape is above 0, this industry can be chosen */
    01970     if (CheckIfCallBackAllowsAvailability(j, IACT_RANDOMCREATION)) {
    01971       probability_max += chance;
    01972       /* adds the result for this industry */
    01973       cumulative_probs[num].ind = j;
    01974       cumulative_probs[num++].prob = probability_max;
    01975     }
    01976   }
    01977
    01978   /* Abort if there is no industry buildable */
    01979   if (probability_max == 0) return;
    01980
    01981   /* Find a random type, with maximum being what has been evaluate above*/
    01982   rndtype = RandomRange(probability_max);
    01983   for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
    01984     /* and choose the index of the industry that matches as close as possible this random type */
    01985     if (cumulative_probs[j].prob >= rndtype) break;
    01986   }
    01987
    01988   ind_spc = GetIndustrySpec(cumulative_probs[j].ind);
    01989   /*  Check if it is allowed */
    01990   if ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) return;
    01991   if ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) return;
    01992
    01993   /* try to create 2000 times this industry */
    01994   num = 2000;
    01995   for (;;) {
    01996     ind = CreateNewIndustry(RandomTile(), cumulative_probs[j].ind);
    01997     if (ind != NULL) break;
    01998     if (--num == 0) return;
    01999   }
    02000
    02001   SetDParam(0, ind_spc->name);
    02002   if (ind_spc->new_industry_text > STR_LAST_STRINGID) {
    02003     SetDParam(1, STR_TOWN_NAME);
    02004     SetDParam(2, ind->town->index);
    02005   } else {
    02006     SetDParam(1, ind->town->index);
    02007   }
    02008   AddIndustryNewsItem(ind_spc->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
    02009   AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
    02010 }
    02011
    02020 static bool CheckIndustryCloseDownProtection(IndustryType type)
    02021 {
    02022   const IndustrySpec *indspec = GetIndustrySpec(type);
    02023
    02024   /* oil wells (or the industries with that flag set) are always allowed to closedown */
    02025   if ((indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE) return false;
    02026   return (indspec->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) == 0 && GetIndustryTypeCount(type) <= 1;
    02027 }
    02028
    02038 static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces)
    02039 {
    02040   const IndustrySpec *indspec = GetIndustrySpec(ind->type);
    02041
    02042   /* Check for acceptance of cargo */
    02043   for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) {
    02044     if (ind->accepts_cargo[j] == CT_INVALID) continue;
    02045     if (cargo == ind->accepts_cargo[j]) {
    02046       if (HasBit(indspec->callback_mask, CBM_IND_REFUSE_CARGO)) {
    02047         uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO,
    02048             0, GetReverseCargoTranslation(cargo, indspec->grf_prop.grffile),
    02049             ind, ind->type, ind->location.tile);
    02050         if (res == 0) continue;
    02051       }
    02052       *c_accepts = true;
    02053       break;
    02054     }
    02055   }
    02056
    02057   /* Check for produced cargo */
    02058   for (byte j = 0; j < lengthof(ind->produced_cargo); j++) {
    02059     if (ind->produced_cargo[j] == CT_INVALID) continue;
    02060     if (cargo == ind->produced_cargo[j]) {
    02061       *c_produces = true;
    02062       break;
    02063     }
    02064   }
    02065 }
    02066
    02080 static int WhoCanServiceIndustry(Industry *ind)
    02081 {
    02082   /* Find all stations within reach of the industry */
    02083   StationList stations;
    02084   FindStationsAroundTiles(ind->location, &stations);
    02085
    02086   if (stations.Length() == 0) return 0; // No stations found at all => nobody services
    02087
    02088   const Vehicle *v;
    02089   int result = 0;
    02090   FOR_ALL_VEHICLES(v) {
    02091     /* Is it worthwhile to try this vehicle? */
    02092     if (v->owner != _local_company && result != 0) continue;
    02093
    02094     /* Check whether it accepts the right kind of cargo */
    02095     bool c_accepts = false;
    02096     bool c_produces = false;
    02097     if (v->type == VEH_TRAIN && Train::From(v)->IsFrontEngine()) {
    02098       for (const Vehicle *u = v; u != NULL; u = u->Next()) {
    02099         CanCargoServiceIndustry(u->cargo_type, ind, &c_accepts, &c_produces);
    02100       }
    02101     } else if (v->type == VEH_ROAD || v->type == VEH_SHIP || v->type == VEH_AIRCRAFT) {
    02102       CanCargoServiceIndustry(v->cargo_type, ind, &c_accepts, &c_produces);
    02103     } else {
    02104       continue;
    02105     }
    02106     if (!c_accepts && !c_produces) continue; // Wrong cargo
    02107
    02108     /* Check orders of the vehicle.
    02109      * We cannot check the first of shared orders only, since the first vehicle in such a chain
    02110      * may have a different cargo type.
    02111      */
    02112     const Order *o;
    02113     FOR_VEHICLE_ORDERS(v, o) {
    02114       if (o->IsType(OT_GOTO_STATION) && !(o->GetUnloadType() & OUFB_TRANSFER)) {
    02115         /* Vehicle visits a station to load or unload */
    02116         Station *st = Station::Get(o->GetDestination());
    02117         assert(st != NULL);
    02118
    02119         /* Same cargo produced by industry is dropped here => not serviced by vehicle v */
    02120         if ((o->GetUnloadType() & OUFB_UNLOAD) && !c_accepts) break;
    02121
    02122         if (stations.Contains(st)) {
    02123           if (v->owner == _local_company) return 2; // Company services industry
    02124           result = 1; // Competitor services industry
    02125         }
    02126       }
    02127     }
    02128   }
    02129   return result;
    02130 }
    02131
    02139 static void ReportNewsProductionChangeIndustry(Industry *ind, CargoID type, int percent)
    02140 {
    02141   NewsSubtype ns;
    02142
    02143   switch (WhoCanServiceIndustry(ind)) {
    02144     case 0: ns = NS_INDUSTRY_NOBODY;  break;
    02145     case 1: ns = NS_INDUSTRY_OTHER;   break;
    02146     case 2: ns = NS_INDUSTRY_COMPANY; break;
    02147     default: NOT_REACHED();
    02148   }
    02149   SetDParam(2, abs(percent));
    02150   SetDParam(0, CargoSpec::Get(type)->name);
    02151   SetDParam(1, ind->index);
    02152   AddIndustryNewsItem(
    02153     percent >= 0 ? STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_SMOOTH : STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_SMOOTH,
    02154     ns,
    02155     ind->index
    02156   );
    02157 }
    02158
    02159 enum {
    02160   PERCENT_TRANSPORTED_60 = 153,
    02161   PERCENT_TRANSPORTED_80 = 204,
    02162 };
    02163
    02168 static void ChangeIndustryProduction(Industry *i, bool monthly)
    02169 {
    02170   StringID str = STR_NULL;
    02171   bool closeit = false;
    02172   const IndustrySpec *indspec = GetIndustrySpec(i->type);
    02173   bool standard = false;
    02174   bool suppress_message = false;
    02175   bool recalculate_multipliers = false;
    02176   /* don't use smooth economy for industries using production related callbacks */
    02177   bool smooth_economy = _settings_game.economy.smooth_economy &&
    02178                         !(HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_256_TICKS) || HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) && // production callbacks
    02179                         !(HasBit(indspec->callback_mask, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(indspec->callback_mask, CBM_IND_PRODUCTION_CHANGE));            // production change callbacks
    02180   byte div = 0;
    02181   byte mul = 0;
    02182   int8 increment = 0;
    02183
    02184   bool callback_enabled = HasBit(indspec->callback_mask, monthly ? CBM_IND_MONTHLYPROD_CHANGE : CBM_IND_PRODUCTION_CHANGE);
    02185   if (callback_enabled) {
    02186     uint16 res = GetIndustryCallback(monthly ? CBID_INDUSTRY_MONTHLYPROD_CHANGE : CBID_INDUSTRY_PRODUCTION_CHANGE, 0, Random(), i, i->type, i->location.tile);
    02187     if (res != CALLBACK_FAILED) { // failed callback means "do nothing"
    02188       suppress_message = HasBit(res, 7);
    02189       /* Get the custom message if any */
    02190       if (HasBit(res, 8)) str = MapGRFStringID(indspec->grf_prop.grffile->grfid, GB(GetRegister(0x100), 0, 16));
    02191       res = GB(res, 0, 4);
    02192       switch (res) {
    02193         default: NOT_REACHED();
    02194         case 0x0: break;                  // Do nothing, but show the custom message if any
    02195         case 0x1: div = 1; break;         // Halve industry production. If production reaches the quarter of the default, the industry is closed instead.
    02196         case 0x2: mul = 1; break;         // Double industry production if it hasn't reached eight times of the original yet.
    02197         case 0x3: closeit = true; break;  // The industry announces imminent closure, and is physically removed from the map next month.
    02198         case 0x4: standard = true; break; // Do the standard random production change as if this industry was a primary one.
    02199         case 0x5: case 0x6: case 0x7:     // Divide production by 4, 8, 16
    02200         case 0x8: div = res - 0x3; break; // Divide production by 32
    02201         case 0x9: case 0xA: case 0xB:     // Multiply production by 4, 8, 16
    02202         case 0xC: mul = res - 0x7; break; // Multiply production by 32
    02203         case 0xD:                         // decrement production
    02204         case 0xE:                         // increment production
    02205           increment = res == 0x0D ? -1 : 1;
    02206           break;
    02207         case 0xF:                         // Set production to third byte of register 0x100
    02208           i->prod_level = Clamp(GB(GetRegister(0x100), 16, 8), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
    02209           recalculate_multipliers = true;
    02210           break;
    02211       }
    02212     }
    02213   } else {
    02214     if (monthly != smooth_economy) return;
    02215     if (indspec->life_type == INDUSTRYLIFE_BLACK_HOLE) return;
    02216   }
    02217
    02218   if (standard || (!callback_enabled && (indspec->life_type & (INDUSTRYLIFE_ORGANIC | INDUSTRYLIFE_EXTRACTIVE)) != 0)) {
    02219     /* decrease or increase */
    02220     bool only_decrease = (indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE;
    02221
    02222     if (smooth_economy) {
    02223       closeit = true;
    02224       for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    02225         if (i->produced_cargo[j] == CT_INVALID) continue;
    02226         uint32 r = Random();
    02227         int old_prod, new_prod, percent;
    02228         /* If over 60% is transported, mult is 1, else mult is -1. */
    02229         int mult = (i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_60) ? 1 : -1;
    02230
    02231         new_prod = old_prod = i->production_rate[j];
    02232
    02233         /* For industries with only_decrease flags (temperate terrain Oil Wells),
    02234          * the multiplier will always be -1 so they will only decrease. */
    02235         if (only_decrease) {
    02236           mult = -1;
    02237         /* For normal industries, if over 60% is transported, 33% chance for decrease.
    02238          * Bonus for very high station ratings (over 80%): 16% chance for decrease. */
    02239         } else if (Chance16I(1, ((i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_80) ? 6 : 3), r)) {
    02240           mult *= -1;
    02241         }
    02242
    02243         /* 4.5% chance for 3-23% (or 1 unit for very low productions) production change,
    02244          * determined by mult value. If mult = 1 prod. increases, else (-1) it decreases. */
    02245         if (Chance16I(1, 22, r >> 16)) {
    02246           new_prod += mult * (max(((RandomRange(50) + 10) * old_prod) >> 8, 1U));
    02247         }
    02248
    02249         /* Prevent production to overflow or Oil Rig passengers to be over-"produced" */
    02250         new_prod = Clamp(new_prod, 1, 255);
    02251
    02252         if (((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0) && j == 1)
    02253           new_prod = Clamp(new_prod, 0, 16);
    02254
    02255         /* Do not stop closing the industry when it has the lowest possible production rate */
    02256         if (new_prod == old_prod && old_prod > 1) {
    02257           closeit = false;
    02258           continue;
    02259         }
    02260
    02261         percent = (old_prod == 0) ? 100 : (new_prod * 100 / old_prod - 100);
    02262         i->production_rate[j] = new_prod;
    02263
    02264         /* Close the industry when it has the lowest possible production rate */
    02265         if (new_prod > 1) closeit = false;
    02266
    02267         if (abs(percent) >= 10) {
    02268           ReportNewsProductionChangeIndustry(i, i->produced_cargo[j], percent);
    02269         }
    02270       }
    02271     } else {
    02272       if (only_decrease || Chance16(1, 3)) {
    02273         /* If more than 60% transported, 66% chance of increase, else 33% chance of increase */
    02274         if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != Chance16(1, 3)) {
    02275           mul = 1; // Increase production
    02276         } else {
    02277           div = 1; // Decrease production
    02278         }
    02279       }
    02280     }
    02281   }
    02282
    02283   if (!callback_enabled && (indspec->life_type & INDUSTRYLIFE_PROCESSING)) {
    02284     if ( (byte)(_cur_year - i->last_prod_year) >= 5 && Chance16(1, smooth_economy ? 180 : 2)) {
    02285       closeit = true;
    02286     }
    02287   }
    02288
    02289   /* Increase if needed */
    02290   while (mul-- != 0 && i->prod_level < PRODLEVEL_MAXIMUM) {
    02291     i->prod_level = min(i->prod_level * 2, PRODLEVEL_MAXIMUM);
    02292     recalculate_multipliers = true;
    02293     if (str == STR_NULL) str = indspec->production_up_text;
    02294   }
    02295
    02296   /* Decrease if needed */
    02297   while (div-- != 0 && !closeit) {
    02298     if (i->prod_level == PRODLEVEL_MINIMUM) {
    02299       closeit = true;
    02300     } else {
    02301       i->prod_level = max(i->prod_level / 2, (int)PRODLEVEL_MINIMUM); // typecast to int required to please MSVC
    02302       recalculate_multipliers = true;
    02303       if (str == STR_NULL) str = indspec->production_down_text;
    02304     }
    02305   }
    02306
    02307   /* Increase or Decreasing the production level if needed */
    02308   if (increment != 0) {
    02309     if (increment < 0 && i->prod_level == PRODLEVEL_MINIMUM) {
    02310       closeit = true;
    02311     } else {
    02312       i->prod_level = ClampU(i->prod_level + increment, PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
    02313       recalculate_multipliers = true;
    02314     }
    02315   }
    02316
    02317   /* Recalculate production_rate
    02318    * For non-smooth economy these should always be synchronized with prod_level */
    02319   if (recalculate_multipliers) {
    02320     /* Rates are rounded up, so e.g. oilrig always produces some passengers */
    02321     i->production_rate[0] = min((indspec->production_rate[0] * i->prod_level + PRODLEVEL_DEFAULT - 1) / PRODLEVEL_DEFAULT, 0xFF);
    02322     i->production_rate[1] = min((indspec->production_rate[1] * i->prod_level + PRODLEVEL_DEFAULT - 1) / PRODLEVEL_DEFAULT, 0xFF);
    02323   }
    02324
    02325   /* Close if needed and allowed */
    02326   if (closeit && !CheckIndustryCloseDownProtection(i->type)) {
    02327     i->prod_level = PRODLEVEL_CLOSURE;
    02328     str = indspec->closure_text;
    02329   }
    02330
    02331   if (!suppress_message && str != STR_NULL) {
    02332     NewsSubtype ns;
    02333     /* Compute news category */
    02334     if (closeit) {
    02335       ns = NS_INDUSTRY_CLOSE;
    02336       AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
    02337     } else {
    02338       switch (WhoCanServiceIndustry(i)) {
    02339         case 0: ns = NS_INDUSTRY_NOBODY;  break;
    02340         case 1: ns = NS_INDUSTRY_OTHER;   break;
    02341         case 2: ns = NS_INDUSTRY_COMPANY; break;
    02342         default: NOT_REACHED();
    02343       }
    02344     }
    02345     /* Set parameters of news string */
    02346     if (str > STR_LAST_STRINGID) {
    02347       SetDParam(0, STR_TOWN_NAME);
    02348       SetDParam(1, i->town->index);
    02349       SetDParam(2, indspec->name);
    02350     } else if (closeit) {
    02351       SetDParam(0, STR_FORMAT_INDUSTRY_NAME);
    02352       SetDParam(1, i->town->index);
    02353       SetDParam(2, indspec->name);
    02354     } else {
    02355       SetDParam(0, i->index);
    02356     }
    02357     /* and report the news to the user */
    02358     AddNewsItem(str,
    02359       ns,
    02360       closeit ? NR_TILE : NR_INDUSTRY,
    02361       closeit ? i->location.tile + TileDiffXY(1, 1) : i->index);
    02362   }
    02363 }
    02364
    02370 void IndustryDailyLoop()
    02371 {
    02372   _economy.industry_daily_change_counter += _economy.industry_daily_increment;
    02373
    02374   /* Bits 16-31 of industry_construction_counter contain the number of industries to change/create today,
    02375    * the lower 16 bit are a fractional part that might accumulate over several days until it
    02376    * is sufficient for an industry. */
    02377   uint16 change_loop = _economy.industry_daily_change_counter >> 16;
    02378
    02379   /* Reset the active part of the counter, just keeping the "factional part" */
    02380   _economy.industry_daily_change_counter &= 0xFFFF;
    02381
    02382   if (change_loop == 0) {
    02383     return;  // Nothing to do? get out
    02384   }
    02385
    02386   CompanyID old_company = _current_company;
    02387   _current_company = OWNER_NONE;
    02388
    02389   /* perform the required industry changes for the day */
    02390   for (uint16 j = 0; j < change_loop; j++) {
    02391     /* 3% chance that we start a new industry */
    02392     if (Chance16(3, 100)) {
    02393       MaybeNewIndustry();
    02394     } else {
    02395       Industry *i = Industry::GetRandom();
    02396       if (i != NULL) {
    02397         ChangeIndustryProduction(i, false);
    02398         SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
    02399       }
    02400     }
    02401   }
    02402
    02403   _current_company = old_company;
    02404
    02405   /* production-change */
    02406   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
    02407 }
    02408
    02409 void IndustryMonthlyLoop()
    02410 {
    02411   Industry *i;
    02412   CompanyID old_company = _current_company;
    02413   _current_company = OWNER_NONE;
    02414
    02415   FOR_ALL_INDUSTRIES(i) {
    02416     UpdateIndustryStatistics(i);
    02417     if (i->prod_level == PRODLEVEL_CLOSURE) {
    02418       delete i;
    02419     } else {
    02420       ChangeIndustryProduction(i, true);
    02421       SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
    02422     }
    02423   }
    02424
    02425   _current_company = old_company;
    02426
    02427   /* production-change */
    02428   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
    02429 }
    02430
    02431
    02432 void InitializeIndustries()
    02433 {
    02434   _industry_pool.CleanPool();
    02435
    02436   ResetIndustryCounts();
    02437   _industry_sound_tile = 0;
    02438 }
    02439
    02440 bool IndustrySpec::IsRawIndustry() const
    02441 {
    02442   /* Lumber mills are extractive/organic, but can always be built like a non-raw industry */
    02443   return (this->life_type & (INDUSTRYLIFE_EXTRACTIVE | INDUSTRYLIFE_ORGANIC)) != 0 &&
    02444       (this->behaviour & INDUSTRYBEH_CUT_TREES) == 0;
    02445 }
    02446
    02447 Money IndustrySpec::GetConstructionCost() const
    02448 {
    02449   /* Building raw industries like secondary uses different price base */
    02450   return (_price[(_settings_game.construction.raw_industry_construction == 1 && this->IsRawIndustry()) ?
    02451       PR_BUILD_INDUSTRY_RAW : PR_BUILD_INDUSTRY] * this->cost_multiplier) >> 8;
    02452 }
    02453
    02454 Money IndustrySpec::GetRemovalCost() const
    02455 {
    02456   return (_price[PR_CLEAR_INDUSTRY] * this->removal_cost_multiplier) >> 8;
    02457 }
    02458
    02459 static CommandCost TerraformTile_Industry(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
    02460 {
    02461   if (AutoslopeEnabled()) {
    02462     /* We imitate here TTDP's behaviour:
    02463      *  - Both new and old slope must not be steep.
    02464      *  - TileMaxZ must not be changed.
    02465      *  - Allow autoslope by default.
    02466      *  - Disallow autoslope if callback succeeds and returns non-zero.
    02467      */
    02468     Slope tileh_old = GetTileSlope(tile, NULL);
    02469     /* TileMaxZ must not be changed. Slopes must not be steep. */
    02470     if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
    02471       const IndustryGfx gfx = GetIndustryGfx(tile);
    02472       const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
    02473
    02474       /* Call callback 3C 'disable autosloping for industry tiles'. */
    02475       if (HasBit(itspec->callback_mask, CBM_INDT_AUTOSLOPE)) {
    02476         /* If the callback fails, allow autoslope. */
    02477         uint16 res = GetIndustryTileCallback(CBID_INDUSTRY_AUTOSLOPE, 0, 0, gfx, Industry::GetByTile(tile), tile);
    02478         if ((res == 0) || (res == CALLBACK_FAILED)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    02479       } else {
    02480         /* allow autoslope */
    02481         return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
    02482       }
    02483     }
    02484   }
    02485   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    02486 }
    02487
    02488 extern const TileTypeProcs _tile_type_industry_procs = {
    02489   DrawTile_Industry,           // draw_tile_proc
    02490   GetSlopeZ_Industry,          // get_slope_z_proc
    02491   ClearTile_Industry,          // clear_tile_proc
    02492   AddAcceptedCargo_Industry,   // add_accepted_cargo_proc
    02493   GetTileDesc_Industry,        // get_tile_desc_proc
    02494   GetTileTrackStatus_Industry, // get_tile_track_status_proc
    02495   ClickTile_Industry,          // click_tile_proc
    02496   AnimateTile_Industry,        // animate_tile_proc
    02497   TileLoop_Industry,           // tile_loop_proc
    02498   ChangeTileOwner_Industry,    // change_tile_owner_proc
    02499   NULL,                        // add_produced_cargo_proc
    02500   NULL,                        // vehicle_enter_tile_proc
    02501   GetFoundation_Industry,      // get_foundation_proc
    02502   TerraformTile_Industry,      // terraform_tile_proc
    02503 };