industry_cmd.cpp

Go to the documentation of this file.
00001 /* $Id: industry_cmd.cpp 26328 2014-02-10 17:13:54Z frosch $ */
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 "landscape.h"
00017 #include "viewport_func.h"
00018 #include "command_func.h"
00019 #include "town.h"
00020 #include "news_func.h"
00021 #include "cheat_type.h"
00022 #include "genworld.h"
00023 #include "tree_map.h"
00024 #include "newgrf_cargo.h"
00025 #include "newgrf_debug.h"
00026 #include "newgrf_industrytiles.h"
00027 #include "autoslope.h"
00028 #include "water.h"
00029 #include "strings_func.h"
00030 #include "window_func.h"
00031 #include "date_func.h"
00032 #include "vehicle_func.h"
00033 #include "sound_func.h"
00034 #include "animated_tile_func.h"
00035 #include "effectvehicle_func.h"
00036 #include "effectvehicle_base.h"
00037 #include "ai/ai.hpp"
00038 #include "core/pool_func.hpp"
00039 #include "subsidy_func.h"
00040 #include "core/backup_type.hpp"
00041 #include "object_base.h"
00042 #include "game/game.hpp"
00043 
00044 #include "table/strings.h"
00045 #include "table/industry_land.h"
00046 #include "table/build_industry.h"
00047 
00048 IndustryPool _industry_pool("Industry");
00049 INSTANTIATE_POOL_METHODS(Industry)
00050 
00051 void ShowIndustryViewWindow(int industry);
00052 void BuildOilRig(TileIndex tile);
00053 
00054 static byte _industry_sound_ctr;
00055 static TileIndex _industry_sound_tile;
00056 
00057 uint16 Industry::counts[NUM_INDUSTRYTYPES];
00058 
00059 IndustrySpec _industry_specs[NUM_INDUSTRYTYPES];
00060 IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES];
00061 IndustryBuildData _industry_builder; 
00062 
00069 void ResetIndustries()
00070 {
00071   memset(&_industry_specs, 0, sizeof(_industry_specs));
00072   memcpy(&_industry_specs, &_origin_industry_specs, sizeof(_origin_industry_specs));
00073 
00074   /* once performed, enable only the current climate industries */
00075   for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
00076     _industry_specs[i].enabled = i < NEW_INDUSTRYOFFSET &&
00077         HasBit(_origin_industry_specs[i].climate_availability, _settings_game.game_creation.landscape);
00078   }
00079 
00080   memset(&_industry_tile_specs, 0, sizeof(_industry_tile_specs));
00081   memcpy(&_industry_tile_specs, &_origin_industry_tile_specs, sizeof(_origin_industry_tile_specs));
00082 
00083   /* Reset any overrides that have been set. */
00084   _industile_mngr.ResetOverride();
00085   _industry_mngr.ResetOverride();
00086 }
00087 
00096 IndustryType GetIndustryType(TileIndex tile)
00097 {
00098   assert(IsTileType(tile, MP_INDUSTRY));
00099 
00100   const Industry *ind = Industry::GetByTile(tile);
00101   assert(ind != NULL);
00102   return ind->type;
00103 }
00104 
00113 const IndustrySpec *GetIndustrySpec(IndustryType thistype)
00114 {
00115   assert(thistype < NUM_INDUSTRYTYPES);
00116   return &_industry_specs[thistype];
00117 }
00118 
00127 const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
00128 {
00129   assert(gfx < INVALID_INDUSTRYTILE);
00130   return &_industry_tile_specs[gfx];
00131 }
00132 
00133 Industry::~Industry()
00134 {
00135   if (CleaningPool()) return;
00136 
00137   /* Industry can also be destroyed when not fully initialized.
00138    * This means that we do not have to clear tiles either.
00139    * Also we must not decrement industry counts in that case. */
00140   if (this->location.w == 0) return;
00141 
00142   TILE_AREA_LOOP(tile_cur, this->location) {
00143     if (IsTileType(tile_cur, MP_INDUSTRY)) {
00144       if (GetIndustryIndex(tile_cur) == this->index) {
00145         DeleteNewGRFInspectWindow(GSF_INDUSTRYTILES, tile_cur);
00146 
00147         /* MakeWaterKeepingClass() can also handle 'land' */
00148         MakeWaterKeepingClass(tile_cur, OWNER_NONE);
00149       }
00150     } else if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
00151       DeleteOilRig(tile_cur);
00152     }
00153   }
00154 
00155   if (GetIndustrySpec(this->type)->behaviour & INDUSTRYBEH_PLANT_FIELDS) {
00156     TileArea ta(this->location.tile - TileDiffXY(min(TileX(this->location.tile), 21), min(TileY(this->location.tile), 21)), 42, 42);
00157     ta.ClampToMap();
00158 
00159     /* Remove the farmland and convert it to regular tiles over time. */
00160     TILE_AREA_LOOP(tile_cur, ta) {
00161       if (IsTileType(tile_cur, MP_CLEAR) && IsClearGround(tile_cur, CLEAR_FIELDS) &&
00162           GetIndustryIndexOfField(tile_cur) == this->index) {
00163         SetIndustryIndexOfField(tile_cur, INVALID_INDUSTRY);
00164       }
00165     }
00166   }
00167 
00168   /* don't let any disaster vehicle target invalid industry */
00169   ReleaseDisastersTargetingIndustry(this->index);
00170 
00171   /* Clear the persistent storage. */
00172   delete this->psa;
00173 
00174   DecIndustryTypeCount(this->type);
00175 
00176   DeleteIndustryNews(this->index);
00177   DeleteWindowById(WC_INDUSTRY_VIEW, this->index);
00178   DeleteNewGRFInspectWindow(GSF_INDUSTRIES, this->index);
00179 
00180   DeleteSubsidyWith(ST_INDUSTRY, this->index);
00181   CargoPacket::InvalidateAllFrom(ST_INDUSTRY, this->index);
00182 }
00183 
00188 void Industry::PostDestructor(size_t index)
00189 {
00190   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
00191   Station::RecomputeIndustriesNearForAll();
00192 }
00193 
00194 
00199 /* static */ Industry *Industry::GetRandom()
00200 {
00201   if (Industry::GetNumItems() == 0) return NULL;
00202   int num = RandomRange((uint16)Industry::GetNumItems());
00203   size_t index = MAX_UVALUE(size_t);
00204 
00205   while (num >= 0) {
00206     num--;
00207     index++;
00208 
00209     /* Make sure we have a valid industry */
00210     while (!Industry::IsValidID(index)) {
00211       index++;
00212       assert(index < Industry::GetPoolSize());
00213     }
00214   }
00215 
00216   return Industry::Get(index);
00217 }
00218 
00219 
00220 static void IndustryDrawSugarMine(const TileInfo *ti)
00221 {
00222   if (!IsIndustryCompleted(ti->tile)) return;
00223 
00224   const DrawIndustryAnimationStruct *d = &_draw_industry_spec1[GetAnimationFrame(ti->tile)];
00225 
00226   AddChildSpriteScreen(SPR_IT_SUGAR_MINE_SIEVE + d->image_1, PAL_NONE, d->x, 0);
00227 
00228   if (d->image_2 != 0) {
00229     AddChildSpriteScreen(SPR_IT_SUGAR_MINE_CLOUDS + d->image_2 - 1, PAL_NONE, 8, 41);
00230   }
00231 
00232   if (d->image_3 != 0) {
00233     AddChildSpriteScreen(SPR_IT_SUGAR_MINE_PILE + d->image_3 - 1, PAL_NONE,
00234       _drawtile_proc1[d->image_3 - 1].x, _drawtile_proc1[d->image_3 - 1].y);
00235   }
00236 }
00237 
00238 static void IndustryDrawToffeeQuarry(const TileInfo *ti)
00239 {
00240   uint8 x = 0;
00241 
00242   if (IsIndustryCompleted(ti->tile)) {
00243     x = _industry_anim_offs_toffee[GetAnimationFrame(ti->tile)];
00244     if (x == 0xFF) {
00245       x = 0;
00246     }
00247   }
00248 
00249   AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_SHOVEL, PAL_NONE, 22 - x, 24 + x);
00250   AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_TOFFEE, PAL_NONE, 6, 14);
00251 }
00252 
00253 static void IndustryDrawBubbleGenerator( const TileInfo *ti)
00254 {
00255   if (IsIndustryCompleted(ti->tile)) {
00256     AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_BUBBLE, PAL_NONE, 5, _industry_anim_offs_bubbles[GetAnimationFrame(ti->tile)]);
00257   }
00258   AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_SPRING, PAL_NONE, 3, 67);
00259 }
00260 
00261 static void IndustryDrawToyFactory(const TileInfo *ti)
00262 {
00263   const DrawIndustryAnimationStruct *d = &_industry_anim_offs_toys[GetAnimationFrame(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 = GetAnimationFrame(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 
00307   /* Retrieve pointer to the draw industry tile struct */
00308   if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00309     /* Draw the tile using the specialized method of newgrf industrytile.
00310      * DrawNewIndustry will return false if ever the resolver could not
00311      * find any sprite to display.  So in this case, we will jump on the
00312      * substitute gfx instead. */
00313     if (indts->grf_prop.spritegroup[0] != NULL && DrawNewIndustryTile(ti, ind, gfx, indts)) {
00314       return;
00315     } else {
00316       /* No sprite group (or no valid one) found, meaning no graphics associated.
00317        * Use the substitute one instead */
00318       if (indts->grf_prop.subst_id != INVALID_INDUSTRYTILE) {
00319         gfx = indts->grf_prop.subst_id;
00320         /* And point the industrytile spec accordingly */
00321         indts = GetIndustryTileSpec(gfx);
00322       }
00323     }
00324   }
00325 
00326   const DrawBuildingsTileStruct *dits = &_industry_draw_tile_data[gfx << 2 | (indts->anim_state ?
00327       GetAnimationFrame(ti->tile) & INDUSTRY_COMPLETED :
00328       GetIndustryConstructionStage(ti->tile))];
00329 
00330   SpriteID image = dits->ground.sprite;
00331 
00332   /* DrawFoundation() modifies ti->z and ti->tileh */
00333   if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
00334 
00335   /* If the ground sprite is the default flat water sprite, draw also canal/river borders.
00336    * Do not do this if the tile's WaterClass is 'land'. */
00337   if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
00338     DrawWaterClassGround(ti);
00339   } else {
00340     DrawGroundSprite(image, GroundSpritePaletteTransform(image, dits->ground.pal, GENERAL_SPRITE_COLOUR(ind->random_colour)));
00341   }
00342 
00343   /* If industries are transparent and invisible, do not draw the upper part */
00344   if (IsInvisibilitySet(TO_INDUSTRIES)) return;
00345 
00346   /* Add industry on top of the ground? */
00347   image = dits->building.sprite;
00348   if (image != 0) {
00349     AddSortableSpriteToDraw(image, SpriteLayoutPaletteTransform(image, dits->building.pal, GENERAL_SPRITE_COLOUR(ind->random_colour)),
00350       ti->x + dits->subtile_x,
00351       ti->y + dits->subtile_y,
00352       dits->width,
00353       dits->height,
00354       dits->dz,
00355       ti->z,
00356       IsTransparencySet(TO_INDUSTRIES));
00357 
00358     if (IsTransparencySet(TO_INDUSTRIES)) return;
00359   }
00360 
00361   {
00362     int proc = dits->draw_proc - 1;
00363     if (proc >= 0) _industry_draw_tile_procs[proc](ti);
00364   }
00365 }
00366 
00367 static int GetSlopePixelZ_Industry(TileIndex tile, uint x, uint y)
00368 {
00369   return GetTileMaxPixelZ(tile);
00370 }
00371 
00372 static Foundation GetFoundation_Industry(TileIndex tile, Slope tileh)
00373 {
00374   IndustryGfx gfx = GetIndustryGfx(tile);
00375 
00376   /* For NewGRF industry tiles we might not be drawing a foundation. We need to
00377    * account for this, as other structures should
00378    * draw the wall of the foundation in this case.
00379    */
00380   if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00381     const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
00382     if (indts->grf_prop.spritegroup[0] != NULL && HasBit(indts->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
00383       uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, Industry::GetByTile(tile), tile);
00384       if (callback_res != CALLBACK_FAILED && !ConvertBooleanCallback(indts->grf_prop.grffile, CBID_INDTILE_DRAW_FOUNDATIONS, callback_res)) return FOUNDATION_NONE;
00385     }
00386   }
00387   return FlatteningFoundation(tileh);
00388 }
00389 
00390 static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray &acceptance, uint32 *always_accepted)
00391 {
00392   IndustryGfx gfx = GetIndustryGfx(tile);
00393   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00394 
00395   /* When we have to use a callback, we put our data in the next two variables */
00396   CargoID raw_accepts_cargo[lengthof(itspec->accepts_cargo)];
00397   uint8 raw_cargo_acceptance[lengthof(itspec->acceptance)];
00398 
00399   /* And then these will always point to a same sized array with the required data */
00400   const CargoID *accepts_cargo = itspec->accepts_cargo;
00401   const uint8 *cargo_acceptance = itspec->acceptance;
00402 
00403   if (HasBit(itspec->callback_mask, CBM_INDT_ACCEPT_CARGO)) {
00404     uint16 res = GetIndustryTileCallback(CBID_INDTILE_ACCEPT_CARGO, 0, 0, gfx, Industry::GetByTile(tile), tile);
00405     if (res != CALLBACK_FAILED) {
00406       accepts_cargo = raw_accepts_cargo;
00407       for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_accepts_cargo[i] = GetCargoTranslation(GB(res, i * 5, 5), itspec->grf_prop.grffile);
00408     }
00409   }
00410 
00411   if (HasBit(itspec->callback_mask, CBM_INDT_CARGO_ACCEPTANCE)) {
00412     uint16 res = GetIndustryTileCallback(CBID_INDTILE_CARGO_ACCEPTANCE, 0, 0, gfx, Industry::GetByTile(tile), tile);
00413     if (res != CALLBACK_FAILED) {
00414       cargo_acceptance = raw_cargo_acceptance;
00415       for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_cargo_acceptance[i] = GB(res, i * 4, 4);
00416     }
00417   }
00418 
00419   const Industry *ind = Industry::GetByTile(tile);
00420   for (byte i = 0; i < lengthof(itspec->accepts_cargo); i++) {
00421     CargoID a = accepts_cargo[i];
00422     if (a == CT_INVALID || cargo_acceptance[i] == 0) continue; // work only with valid cargoes
00423 
00424     /* Add accepted cargo */
00425     acceptance[a] += cargo_acceptance[i];
00426 
00427     /* Maybe set 'always accepted' bit (if it's not set already) */
00428     if (HasBit(*always_accepted, a)) continue;
00429 
00430     bool accepts = false;
00431     for (uint cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
00432       /* Test whether the industry itself accepts the cargo type */
00433       if (ind->accepts_cargo[cargo_index] == a) {
00434         accepts = true;
00435         break;
00436       }
00437     }
00438 
00439     if (accepts) continue;
00440 
00441     /* If the industry itself doesn't accept this cargo, set 'always accepted' bit */
00442     SetBit(*always_accepted, a);
00443   }
00444 }
00445 
00446 static void GetTileDesc_Industry(TileIndex tile, TileDesc *td)
00447 {
00448   const Industry *i = Industry::GetByTile(tile);
00449   const IndustrySpec *is = GetIndustrySpec(i->type);
00450 
00451   td->owner[0] = i->owner;
00452   td->str = is->name;
00453   if (!IsIndustryCompleted(tile)) {
00454     SetDParamX(td->dparam, 0, td->str);
00455     td->str = STR_LAI_TOWN_INDUSTRY_DESCRIPTION_UNDER_CONSTRUCTION;
00456   }
00457 
00458   if (is->grf_prop.grffile != NULL) {
00459     td->grf = GetGRFConfig(is->grf_prop.grffile->grfid)->GetName();
00460   }
00461 }
00462 
00463 static CommandCost ClearTile_Industry(TileIndex tile, DoCommandFlag flags)
00464 {
00465   Industry *i = Industry::GetByTile(tile);
00466   const IndustrySpec *indspec = GetIndustrySpec(i->type);
00467 
00468   /* water can destroy industries
00469    * in editor you can bulldoze industries
00470    * with magic_bulldozer cheat you can destroy industries
00471    * (area around OILRIG is water, so water shouldn't flood it
00472    */
00473   if ((_current_company != OWNER_WATER && _game_mode != GM_EDITOR &&
00474       !_cheats.magic_bulldozer.value) ||
00475       ((flags & DC_AUTO) != 0) ||
00476       (_current_company == OWNER_WATER &&
00477         ((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) ||
00478         HasBit(GetIndustryTileSpec(GetIndustryGfx(tile))->slopes_refused, 5)))) {
00479     SetDParam(1, indspec->name);
00480     return_cmd_error(flags & DC_AUTO ? STR_ERROR_GENERIC_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
00481   }
00482 
00483   if (flags & DC_EXEC) {
00484     AI::BroadcastNewEvent(new ScriptEventIndustryClose(i->index));
00485     Game::NewEvent(new ScriptEventIndustryClose(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 (EconomyIsInRecession()) 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   IndustryGfx gfx = GetIndustryGfx(tile);
00532 
00533   if (GetIndustryTileSpec(gfx)->animation.status != ANIM_STATUS_NO_ANIMATION) {
00534     AnimateNewIndustryTile(tile);
00535     return;
00536   }
00537 
00538   switch (gfx) {
00539   case GFX_SUGAR_MINE_SIEVE:
00540     if ((_tick_counter & 1) == 0) {
00541       byte m = GetAnimationFrame(tile) + 1;
00542 
00543       if (_settings_client.sound.ambient) {
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 
00550       if (m >= 96) {
00551         m = 0;
00552         DeleteAnimatedTile(tile);
00553       }
00554       SetAnimationFrame(tile, m);
00555 
00556       MarkTileDirtyByTile(tile);
00557     }
00558     break;
00559 
00560   case GFX_TOFFEE_QUARY:
00561     if ((_tick_counter & 3) == 0) {
00562       byte m = GetAnimationFrame(tile);
00563 
00564       if (_industry_anim_offs_toffee[m] == 0xFF && _settings_client.sound.ambient) {
00565         SndPlayTileFx(SND_30_CARTOON_SOUND, tile);
00566       }
00567 
00568       if (++m >= 70) {
00569         m = 0;
00570         DeleteAnimatedTile(tile);
00571       }
00572       SetAnimationFrame(tile, m);
00573 
00574       MarkTileDirtyByTile(tile);
00575     }
00576     break;
00577 
00578   case GFX_BUBBLE_CATCHER:
00579     if ((_tick_counter & 1) == 0) {
00580       byte m = GetAnimationFrame(tile);
00581 
00582       if (++m >= 40) {
00583         m = 0;
00584         DeleteAnimatedTile(tile);
00585       }
00586       SetAnimationFrame(tile, m);
00587 
00588       MarkTileDirtyByTile(tile);
00589     }
00590     break;
00591 
00592   /* Sparks on a coal plant */
00593   case GFX_POWERPLANT_SPARKS:
00594     if ((_tick_counter & 3) == 0) {
00595       byte m = GetAnimationFrame(tile);
00596       if (m == 6) {
00597         SetAnimationFrame(tile, 0);
00598         DeleteAnimatedTile(tile);
00599       } else {
00600         SetAnimationFrame(tile, m + 1);
00601         MarkTileDirtyByTile(tile);
00602       }
00603     }
00604     break;
00605 
00606   case GFX_TOY_FACTORY:
00607     if ((_tick_counter & 1) == 0) {
00608       byte m = GetAnimationFrame(tile) + 1;
00609 
00610       switch (m) {
00611         case  1: if (_settings_client.sound.ambient) SndPlayTileFx(SND_2C_MACHINERY, tile); break;
00612         case 23: if (_settings_client.sound.ambient) SndPlayTileFx(SND_2B_COMEDY_HIT, tile); break;
00613         case 28: if (_settings_client.sound.ambient) SndPlayTileFx(SND_2A_EXTRACT_AND_POP, tile); break;
00614         default:
00615           if (m >= 50) {
00616             int n = GetIndustryAnimationLoop(tile) + 1;
00617             m = 0;
00618             if (n >= 8) {
00619               n = 0;
00620               DeleteAnimatedTile(tile);
00621             }
00622             SetIndustryAnimationLoop(tile, n);
00623           }
00624       }
00625 
00626       SetAnimationFrame(tile, m);
00627       MarkTileDirtyByTile(tile);
00628     }
00629     break;
00630 
00631   case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
00632   case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
00633   case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
00634   case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
00635     if ((_tick_counter & 3) == 0) {
00636       IndustryGfx gfx = GetIndustryGfx(tile);
00637 
00638       gfx = (gfx < 155) ? gfx + 1 : 148;
00639       SetIndustryGfx(tile, gfx);
00640       MarkTileDirtyByTile(tile);
00641     }
00642     break;
00643 
00644   case GFX_OILWELL_ANIMATED_1:
00645   case GFX_OILWELL_ANIMATED_2:
00646   case GFX_OILWELL_ANIMATED_3:
00647     if ((_tick_counter & 7) == 0) {
00648       bool b = Chance16(1, 7);
00649       IndustryGfx gfx = GetIndustryGfx(tile);
00650 
00651       byte m = GetAnimationFrame(tile) + 1;
00652       if (m == 4 && (m = 0, ++gfx) == GFX_OILWELL_ANIMATED_3 + 1 && (gfx = GFX_OILWELL_ANIMATED_1, b)) {
00653         SetIndustryGfx(tile, GFX_OILWELL_NOT_ANIMATED);
00654         SetIndustryConstructionStage(tile, 3);
00655         DeleteAnimatedTile(tile);
00656       } else {
00657         SetAnimationFrame(tile, m);
00658         SetIndustryGfx(tile, gfx);
00659         MarkTileDirtyByTile(tile);
00660       }
00661     }
00662     break;
00663 
00664   case GFX_COAL_MINE_TOWER_ANIMATED:
00665   case GFX_COPPER_MINE_TOWER_ANIMATED:
00666   case GFX_GOLD_MINE_TOWER_ANIMATED: {
00667       int state = _tick_counter & 0x7FF;
00668 
00669       if ((state -= 0x400) < 0) return;
00670 
00671       if (state < 0x1A0) {
00672         if (state < 0x20 || state >= 0x180) {
00673           byte m = GetAnimationFrame(tile);
00674           if (!(m & 0x40)) {
00675             SetAnimationFrame(tile, m | 0x40);
00676             if (_settings_client.sound.ambient) SndPlayTileFx(SND_0B_MINING_MACHINERY, tile);
00677           }
00678           if (state & 7) return;
00679         } else {
00680           if (state & 3) return;
00681         }
00682         byte m = (GetAnimationFrame(tile) + 1) | 0x40;
00683         if (m > 0xC2) m = 0xC0;
00684         SetAnimationFrame(tile, m);
00685         MarkTileDirtyByTile(tile);
00686       } else if (state >= 0x200 && state < 0x3A0) {
00687         int i = (state < 0x220 || state >= 0x380) ? 7 : 3;
00688         if (state & i) return;
00689 
00690         byte m = (GetAnimationFrame(tile) & 0xBF) - 1;
00691         if (m < 0x80) m = 0x82;
00692         SetAnimationFrame(tile, m);
00693         MarkTileDirtyByTile(tile);
00694       }
00695       break;
00696     }
00697   }
00698 }
00699 
00700 static void CreateChimneySmoke(TileIndex tile)
00701 {
00702   uint x = TileX(tile) * TILE_SIZE;
00703   uint y = TileY(tile) * TILE_SIZE;
00704   int z = GetTileMaxPixelZ(tile);
00705 
00706   CreateEffectVehicle(x + 15, y + 14, z + 59, EV_CHIMNEY_SMOKE);
00707 }
00708 
00709 static void MakeIndustryTileBigger(TileIndex tile)
00710 {
00711   byte cnt = GetIndustryConstructionCounter(tile) + 1;
00712   if (cnt != 4) {
00713     SetIndustryConstructionCounter(tile, cnt);
00714     return;
00715   }
00716 
00717   byte stage = GetIndustryConstructionStage(tile) + 1;
00718   SetIndustryConstructionCounter(tile, 0);
00719   SetIndustryConstructionStage(tile, stage);
00720   StartStopIndustryTileAnimation(tile, IAT_CONSTRUCTION_STATE_CHANGE);
00721   if (stage == INDUSTRY_COMPLETED) SetIndustryCompleted(tile, true);
00722 
00723   MarkTileDirtyByTile(tile);
00724 
00725   if (!IsIndustryCompleted(tile)) return;
00726 
00727   IndustryGfx gfx = GetIndustryGfx(tile);
00728   if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00729     /* New industries are already animated on construction. */
00730     return;
00731   }
00732 
00733   switch (gfx) {
00734   case GFX_POWERPLANT_CHIMNEY:
00735     CreateChimneySmoke(tile);
00736     break;
00737 
00738   case GFX_OILRIG_1: {
00739     /* Do not require an industry tile to be after the first two GFX_OILRIG_1
00740      * tiles (like the default oil rig). Do a proper check to ensure the
00741      * tiles belong to the same industry and based on that build the oil rig's
00742      * station. */
00743     TileIndex other = tile + TileDiffXY(0, 1);
00744 
00745     if (IsTileType(other, MP_INDUSTRY) &&
00746         GetIndustryGfx(other) == GFX_OILRIG_1 &&
00747         GetIndustryIndex(tile) == GetIndustryIndex(other)) {
00748       BuildOilRig(tile);
00749     }
00750     break;
00751   }
00752 
00753   case GFX_TOY_FACTORY:
00754   case GFX_BUBBLE_CATCHER:
00755   case GFX_TOFFEE_QUARY:
00756     SetAnimationFrame(tile, 0);
00757     SetIndustryAnimationLoop(tile, 0);
00758     break;
00759 
00760   case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
00761   case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
00762   case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
00763   case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
00764     AddAnimatedTile(tile);
00765     break;
00766   }
00767 }
00768 
00769 static void TileLoopIndustry_BubbleGenerator(TileIndex tile)
00770 {
00771   static const int8 _bubble_spawn_location[3][4] = {
00772     { 11,   0, -4, -14 },
00773     { -4, -10, -4,   1 },
00774     { 49,  59, 60,  65 },
00775   };
00776 
00777   if (_settings_client.sound.ambient) SndPlayTileFx(SND_2E_EXTRACT_AND_POP, tile);
00778 
00779   int dir = Random() & 3;
00780 
00781   EffectVehicle *v = CreateEffectVehicleAbove(
00782     TileX(tile) * TILE_SIZE + _bubble_spawn_location[0][dir],
00783     TileY(tile) * TILE_SIZE + _bubble_spawn_location[1][dir],
00784     _bubble_spawn_location[2][dir],
00785     EV_BUBBLE
00786   );
00787 
00788   if (v != NULL) v->animation_substate = dir;
00789 }
00790 
00791 static void TileLoop_Industry(TileIndex tile)
00792 {
00793   if (IsTileOnWater(tile)) TileLoop_Water(tile);
00794 
00795   /* Normally this doesn't happen, but if an industry NewGRF is removed
00796    * an industry that was previously build on water can now be flooded.
00797    * If this happens the tile is no longer an industry tile after
00798    * returning from TileLoop_Water. */
00799   if (!IsTileType(tile, MP_INDUSTRY)) return;
00800 
00801   TriggerIndustryTile(tile, INDTILE_TRIGGER_TILE_LOOP);
00802 
00803   if (!IsIndustryCompleted(tile)) {
00804     MakeIndustryTileBigger(tile);
00805     return;
00806   }
00807 
00808   if (_game_mode == GM_EDITOR) return;
00809 
00810   TransportIndustryGoods(tile);
00811 
00812   if (StartStopIndustryTileAnimation(tile, IAT_TILELOOP)) return;
00813 
00814   IndustryGfx newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_next;
00815   if (newgfx != INDUSTRYTILE_NOANIM) {
00816     ResetIndustryConstructionStage(tile);
00817     SetIndustryGfx(tile, newgfx);
00818     MarkTileDirtyByTile(tile);
00819     return;
00820   }
00821 
00822   IndustryGfx gfx = GetIndustryGfx(tile);
00823   switch (gfx) {
00824   case GFX_COAL_MINE_TOWER_NOT_ANIMATED:
00825   case GFX_COPPER_MINE_TOWER_NOT_ANIMATED:
00826   case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:
00827     if (!(_tick_counter & 0x400) && Chance16(1, 2)) {
00828       switch (gfx) {
00829         case GFX_COAL_MINE_TOWER_NOT_ANIMATED:   gfx = GFX_COAL_MINE_TOWER_ANIMATED;   break;
00830         case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_ANIMATED; break;
00831         case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:   gfx = GFX_GOLD_MINE_TOWER_ANIMATED;   break;
00832       }
00833       SetIndustryGfx(tile, gfx);
00834       SetAnimationFrame(tile, 0x80);
00835       AddAnimatedTile(tile);
00836     }
00837     break;
00838 
00839   case GFX_OILWELL_NOT_ANIMATED:
00840     if (Chance16(1, 6)) {
00841       SetIndustryGfx(tile, GFX_OILWELL_ANIMATED_1);
00842       SetAnimationFrame(tile, 0);
00843       AddAnimatedTile(tile);
00844     }
00845     break;
00846 
00847   case GFX_COAL_MINE_TOWER_ANIMATED:
00848   case GFX_COPPER_MINE_TOWER_ANIMATED:
00849   case GFX_GOLD_MINE_TOWER_ANIMATED:
00850     if (!(_tick_counter & 0x400)) {
00851       switch (gfx) {
00852         case GFX_COAL_MINE_TOWER_ANIMATED:   gfx = GFX_COAL_MINE_TOWER_NOT_ANIMATED;   break;
00853         case GFX_COPPER_MINE_TOWER_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_NOT_ANIMATED; break;
00854         case GFX_GOLD_MINE_TOWER_ANIMATED:   gfx = GFX_GOLD_MINE_TOWER_NOT_ANIMATED;   break;
00855       }
00856       SetIndustryGfx(tile, gfx);
00857       SetIndustryCompleted(tile, true);
00858       SetIndustryConstructionStage(tile, 3);
00859       DeleteAnimatedTile(tile);
00860     }
00861     break;
00862 
00863   case GFX_POWERPLANT_SPARKS:
00864     if (Chance16(1, 3)) {
00865       if (_settings_client.sound.ambient) SndPlayTileFx(SND_0C_ELECTRIC_SPARK, tile);
00866       AddAnimatedTile(tile);
00867     }
00868     break;
00869 
00870   case GFX_COPPER_MINE_CHIMNEY:
00871     CreateEffectVehicleAbove(TileX(tile) * TILE_SIZE + 6, TileY(tile) * TILE_SIZE + 6, 43, EV_COPPER_MINE_SMOKE);
00872     break;
00873 
00874 
00875   case GFX_TOY_FACTORY: {
00876       Industry *i = Industry::GetByTile(tile);
00877       if (i->was_cargo_delivered) {
00878         i->was_cargo_delivered = false;
00879         SetIndustryAnimationLoop(tile, 0);
00880         AddAnimatedTile(tile);
00881       }
00882     }
00883     break;
00884 
00885   case GFX_BUBBLE_GENERATOR:
00886     TileLoopIndustry_BubbleGenerator(tile);
00887     break;
00888 
00889   case GFX_TOFFEE_QUARY:
00890     AddAnimatedTile(tile);
00891     break;
00892 
00893   case GFX_SUGAR_MINE_SIEVE:
00894     if (Chance16(1, 3)) AddAnimatedTile(tile);
00895     break;
00896   }
00897 }
00898 
00899 static bool ClickTile_Industry(TileIndex tile)
00900 {
00901   ShowIndustryViewWindow(GetIndustryIndex(tile));
00902   return true;
00903 }
00904 
00905 static TrackStatus GetTileTrackStatus_Industry(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
00906 {
00907   return 0;
00908 }
00909 
00910 static void ChangeTileOwner_Industry(TileIndex tile, Owner old_owner, Owner new_owner)
00911 {
00912   /* If the founder merges, the industry was created by the merged company */
00913   Industry *i = Industry::GetByTile(tile);
00914   if (i->founder == old_owner) i->founder = (new_owner == INVALID_OWNER) ? OWNER_NONE : new_owner;
00915 }
00916 
00922 bool IsTileForestIndustry(TileIndex tile)
00923 {
00924   /* Check for industry tile */
00925   if (!IsTileType(tile, MP_INDUSTRY)) return false;
00926 
00927   const Industry *ind = Industry::GetByTile(tile);
00928 
00929   /* Check for organic industry (i.e. not processing or extractive) */
00930   if ((GetIndustrySpec(ind->type)->life_type & INDUSTRYLIFE_ORGANIC) == 0) return false;
00931 
00932   /* Check for wood production */
00933   for (uint i = 0; i < lengthof(ind->produced_cargo); i++) {
00934     /* The industry produces wood. */
00935     if (ind->produced_cargo[i] != CT_INVALID && CargoSpec::Get(ind->produced_cargo[i])->label == 'WOOD') return true;
00936   }
00937 
00938   return false;
00939 }
00940 
00941 static const byte _plantfarmfield_type[] = {1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6};
00942 
00950 static bool IsSuitableForFarmField(TileIndex tile, bool allow_fields)
00951 {
00952   switch (GetTileType(tile)) {
00953     case MP_CLEAR: return !IsClearGround(tile, CLEAR_SNOW) && !IsClearGround(tile, CLEAR_DESERT) && (allow_fields || !IsClearGround(tile, CLEAR_FIELDS));
00954     case MP_TREES: return GetTreeGround(tile) != TREE_GROUND_SHORE;
00955     default:       return false;
00956   }
00957 }
00958 
00966 static void SetupFarmFieldFence(TileIndex tile, int size, byte type, DiagDirection side)
00967 {
00968   TileIndexDiff diff = (DiagDirToAxis(side) == AXIS_Y ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00969 
00970   do {
00971     tile = TILE_MASK(tile);
00972 
00973     if (IsTileType(tile, MP_CLEAR) && IsClearGround(tile, CLEAR_FIELDS)) {
00974       byte or_ = type;
00975 
00976       if (or_ == 1 && Chance16(1, 7)) or_ = 2;
00977 
00978       SetFence(tile, side, or_);
00979     }
00980 
00981     tile += diff;
00982   } while (--size);
00983 }
00984 
00985 static void PlantFarmField(TileIndex tile, IndustryID industry)
00986 {
00987   if (_settings_game.game_creation.landscape == LT_ARCTIC) {
00988     if (GetTileZ(tile) + 2 >= GetSnowLine()) return;
00989   }
00990 
00991   /* determine field size */
00992   uint32 r = (Random() & 0x303) + 0x404;
00993   if (_settings_game.game_creation.landscape == LT_ARCTIC) r += 0x404;
00994   uint size_x = GB(r, 0, 8);
00995   uint size_y = GB(r, 8, 8);
00996 
00997   TileArea ta(tile - TileDiffXY(min(TileX(tile), size_x / 2), min(TileY(tile), size_y / 2)), size_x, size_y);
00998   ta.ClampToMap();
00999 
01000   if (ta.w == 0 || ta.h == 0) return;
01001 
01002   /* check the amount of bad tiles */
01003   int count = 0;
01004   TILE_AREA_LOOP(cur_tile, ta) {
01005     assert(cur_tile < MapSize());
01006     count += IsSuitableForFarmField(cur_tile, false);
01007   }
01008   if (count * 2 < ta.w * ta.h) return;
01009 
01010   /* determine type of field */
01011   r = Random();
01012   uint counter = GB(r, 5, 3);
01013   uint field_type = GB(r, 8, 8) * 9 >> 8;
01014 
01015   /* make field */
01016   TILE_AREA_LOOP(cur_tile, ta) {
01017     assert(cur_tile < MapSize());
01018     if (IsSuitableForFarmField(cur_tile, true)) {
01019       MakeField(cur_tile, field_type, industry);
01020       SetClearCounter(cur_tile, counter);
01021       MarkTileDirtyByTile(cur_tile);
01022     }
01023   }
01024 
01025   int type = 3;
01026   if (_settings_game.game_creation.landscape != LT_ARCTIC && _settings_game.game_creation.landscape != LT_TROPIC) {
01027     type = _plantfarmfield_type[Random() & 0xF];
01028   }
01029 
01030   SetupFarmFieldFence(ta.tile, ta.h, type, DIAGDIR_NE);
01031   SetupFarmFieldFence(ta.tile, ta.w, type, DIAGDIR_NW);
01032   SetupFarmFieldFence(ta.tile + TileDiffXY(ta.w - 1, 0), ta.h, type, DIAGDIR_SW);
01033   SetupFarmFieldFence(ta.tile + TileDiffXY(0, ta.h - 1), ta.w, type, DIAGDIR_SE);
01034 }
01035 
01036 void PlantRandomFarmField(const Industry *i)
01037 {
01038   int x = i->location.w / 2 + Random() % 31 - 16;
01039   int y = i->location.h / 2 + Random() % 31 - 16;
01040 
01041   TileIndex tile = TileAddWrap(i->location.tile, x, y);
01042 
01043   if (tile != INVALID_TILE) PlantFarmField(tile, i->index);
01044 }
01045 
01052 static bool SearchLumberMillTrees(TileIndex tile, void *user_data)
01053 {
01054   if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) { 
01055     /* found a tree */
01056 
01057     Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
01058 
01059     _industry_sound_ctr = 1;
01060     _industry_sound_tile = tile;
01061     if (_settings_client.sound.ambient) SndPlayTileFx(SND_38_CHAINSAW, tile);
01062 
01063     DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
01064 
01065     cur_company.Restore();
01066     return true;
01067   }
01068   return false;
01069 }
01070 
01075 static void ChopLumberMillTrees(Industry *i)
01076 {
01077   /* We only want to cut trees if all tiles are completed. */
01078   TILE_AREA_LOOP(tile_cur, i->location) {
01079     if (i->TileBelongsToIndustry(tile_cur)) {
01080       if (!IsIndustryCompleted(tile_cur)) return;
01081     }
01082   }
01083 
01084   TileIndex tile = i->location.tile;
01085   if (CircularTileSearch(&tile, 40, SearchLumberMillTrees, NULL)) { // 40x40 tiles  to search.
01086     i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + 45); // Found a tree, add according value to waiting cargo.
01087   }
01088 }
01089 
01090 static void ProduceIndustryGoods(Industry *i)
01091 {
01092   const IndustrySpec *indsp = GetIndustrySpec(i->type);
01093 
01094   /* play a sound? */
01095   if ((i->counter & 0x3F) == 0) {
01096     uint32 r;
01097     uint num;
01098     if (Chance16R(1, 14, r) && (num = indsp->number_of_sounds) != 0 && _settings_client.sound.ambient) {
01099       SndPlayTileFx(
01100         (SoundFx)(indsp->random_sounds[((r >> 16) * num) >> 16]),
01101         i->location.tile);
01102     }
01103   }
01104 
01105   i->counter--;
01106 
01107   /* produce some cargo */
01108   if ((i->counter % INDUSTRY_PRODUCE_TICKS) == 0) {
01109     if (HasBit(indsp->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) IndustryProductionCallback(i, 1);
01110 
01111     IndustryBehaviour indbehav = indsp->behaviour;
01112     i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + i->production_rate[0]);
01113     i->produced_cargo_waiting[1] = min(0xffff, i->produced_cargo_waiting[1] + i->production_rate[1]);
01114 
01115     if ((indbehav & INDUSTRYBEH_PLANT_FIELDS) != 0) {
01116       uint16 cb_res = CALLBACK_FAILED;
01117       if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
01118         cb_res = GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, Random(), 0, i, i->type, i->location.tile);
01119       }
01120 
01121       bool plant;
01122       if (cb_res != CALLBACK_FAILED) {
01123         plant = ConvertBooleanCallback(indsp->grf_prop.grffile, CBID_INDUSTRY_SPECIAL_EFFECT, cb_res);
01124       } else {
01125         plant = Chance16(1, 8);
01126       }
01127 
01128       if (plant) PlantRandomFarmField(i);
01129     }
01130     if ((indbehav & INDUSTRYBEH_CUT_TREES) != 0) {
01131       uint16 cb_res = CALLBACK_FAILED;
01132       if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
01133         cb_res = GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, Random(), 1, i, i->type, i->location.tile);
01134       }
01135 
01136       bool cut;
01137       if (cb_res != CALLBACK_FAILED) {
01138         cut = ConvertBooleanCallback(indsp->grf_prop.grffile, CBID_INDUSTRY_SPECIAL_EFFECT, cb_res);
01139       } else {
01140         cut = ((i->counter % INDUSTRY_CUT_TREE_TICKS) == 0);
01141       }
01142 
01143       if (cut) ChopLumberMillTrees(i);
01144     }
01145 
01146     TriggerIndustry(i, INDUSTRY_TRIGGER_INDUSTRY_TICK);
01147     StartStopIndustryTileAnimation(i, IAT_INDUSTRY_TICK);
01148   }
01149 }
01150 
01151 void OnTick_Industry()
01152 {
01153   if (_industry_sound_ctr != 0) {
01154     _industry_sound_ctr++;
01155 
01156     if (_industry_sound_ctr == 75) {
01157       if (_settings_client.sound.ambient) SndPlayTileFx(SND_37_BALLOON_SQUEAK, _industry_sound_tile);
01158     } else if (_industry_sound_ctr == 160) {
01159       _industry_sound_ctr = 0;
01160       if (_settings_client.sound.ambient) SndPlayTileFx(SND_36_CARTOON_CRASH, _industry_sound_tile);
01161     }
01162   }
01163 
01164   if (_game_mode == GM_EDITOR) return;
01165 
01166   Industry *i;
01167   FOR_ALL_INDUSTRIES(i) {
01168     ProduceIndustryGoods(i);
01169   }
01170 }
01171 
01177 static CommandCost CheckNewIndustry_NULL(TileIndex tile)
01178 {
01179   return CommandCost();
01180 }
01181 
01187 static CommandCost CheckNewIndustry_Forest(TileIndex tile)
01188 {
01189   if (_settings_game.game_creation.landscape == LT_ARCTIC) {
01190     if (GetTileZ(tile) < HighestSnowLine() + 2) {
01191       return_cmd_error(STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED);
01192     }
01193   }
01194   return CommandCost();
01195 }
01196 
01202 static CommandCost CheckNewIndustry_OilRefinery(TileIndex tile)
01203 {
01204   if (_game_mode == GM_EDITOR) return CommandCost();
01205   if (DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return CommandCost();
01206 
01207   return_cmd_error(STR_ERROR_CAN_ONLY_BE_POSITIONED);
01208 }
01209 
01210 extern bool _ignore_restrictions;
01211 
01217 static CommandCost CheckNewIndustry_OilRig(TileIndex tile)
01218 {
01219   if (_game_mode == GM_EDITOR && _ignore_restrictions) return CommandCost();
01220   if (TileHeight(tile) == 0 &&
01221       DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return CommandCost();
01222 
01223   return_cmd_error(STR_ERROR_CAN_ONLY_BE_POSITIONED);
01224 }
01225 
01231 static CommandCost CheckNewIndustry_Farm(TileIndex tile)
01232 {
01233   if (_settings_game.game_creation.landscape == LT_ARCTIC) {
01234     if (GetTileZ(tile) + 2 >= HighestSnowLine()) {
01235       return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01236     }
01237   }
01238   return CommandCost();
01239 }
01240 
01246 static CommandCost CheckNewIndustry_Plantation(TileIndex tile)
01247 {
01248   if (GetTropicZone(tile) == TROPICZONE_DESERT) {
01249     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01250   }
01251   return CommandCost();
01252 }
01253 
01259 static CommandCost CheckNewIndustry_Water(TileIndex tile)
01260 {
01261   if (GetTropicZone(tile) != TROPICZONE_DESERT) {
01262     return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT);
01263   }
01264   return CommandCost();
01265 }
01266 
01272 static CommandCost CheckNewIndustry_Lumbermill(TileIndex tile)
01273 {
01274   if (GetTropicZone(tile) != TROPICZONE_RAINFOREST) {
01275     return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST);
01276   }
01277   return CommandCost();
01278 }
01279 
01285 static CommandCost CheckNewIndustry_BubbleGen(TileIndex tile)
01286 {
01287   if (GetTileZ(tile) > 4) {
01288     return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_LOW_AREAS);
01289   }
01290   return CommandCost();
01291 }
01292 
01298 typedef CommandCost CheckNewIndustryProc(TileIndex tile);
01299 
01301 static CheckNewIndustryProc * const _check_new_industry_procs[CHECK_END] = {
01302   CheckNewIndustry_NULL,        
01303   CheckNewIndustry_Forest,      
01304   CheckNewIndustry_OilRefinery, 
01305   CheckNewIndustry_Farm,        
01306   CheckNewIndustry_Plantation,  
01307   CheckNewIndustry_Water,       
01308   CheckNewIndustry_Lumbermill,  
01309   CheckNewIndustry_BubbleGen,   
01310   CheckNewIndustry_OilRig,      
01311 };
01312 
01323 static CommandCost FindTownForIndustry(TileIndex tile, int type, Town **t)
01324 {
01325   *t = ClosestTownFromTile(tile, UINT_MAX);
01326 
01327   if (_settings_game.economy.multiple_industry_per_town) return CommandCost();
01328 
01329   const Industry *i;
01330   FOR_ALL_INDUSTRIES(i) {
01331     if (i->type == (byte)type && i->town == *t) {
01332       *t = NULL;
01333       return_cmd_error(STR_ERROR_ONLY_ONE_ALLOWED_PER_TOWN);
01334     }
01335   }
01336 
01337   return CommandCost();
01338 }
01339 
01340 bool IsSlopeRefused(Slope current, Slope refused)
01341 {
01342   if (IsSteepSlope(current)) return true;
01343   if (current != SLOPE_FLAT) {
01344     if (IsSteepSlope(refused)) return true;
01345 
01346     Slope t = ComplementSlope(current);
01347 
01348     if ((refused & SLOPE_W) && (t & SLOPE_NW)) return true;
01349     if ((refused & SLOPE_S) && (t & SLOPE_NE)) return true;
01350     if ((refused & SLOPE_E) && (t & SLOPE_SW)) return true;
01351     if ((refused & SLOPE_N) && (t & SLOPE_SE)) return true;
01352   }
01353 
01354   return false;
01355 }
01356 
01369 static CommandCost CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTileTable *it, uint itspec_index, int type, uint16 initial_random_bits, Owner founder, IndustryAvailabilityCallType creation_type, bool *custom_shape_check = NULL)
01370 {
01371   bool refused_slope = false;
01372   bool custom_shape = false;
01373 
01374   do {
01375     IndustryGfx gfx = GetTranslatedIndustryTileID(it->gfx);
01376     TileIndex cur_tile = TileAddWrap(tile, it->ti.x, it->ti.y);
01377 
01378     if (!IsValidTile(cur_tile)) {
01379       return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01380     }
01381 
01382     if (gfx == GFX_WATERTILE_SPECIALCHECK) {
01383       if (!IsTileType(cur_tile, MP_WATER) ||
01384           !IsTileFlat(cur_tile)) {
01385         return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01386       }
01387     } else {
01388       CommandCost ret = EnsureNoVehicleOnGround(cur_tile);
01389       if (ret.Failed()) return ret;
01390       if (MayHaveBridgeAbove(cur_tile) && IsBridgeAbove(cur_tile)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01391 
01392       const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
01393 
01394       IndustryBehaviour ind_behav = GetIndustrySpec(type)->behaviour;
01395 
01396       /* Perform land/water check if not disabled */
01397       if (!HasBit(its->slopes_refused, 5) && ((HasTileWaterClass(cur_tile) && IsTileOnWater(cur_tile)) == !(ind_behav & INDUSTRYBEH_BUILT_ONWATER))) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01398 
01399       if (HasBit(its->callback_mask, CBM_INDT_SHAPE_CHECK)) {
01400         custom_shape = true;
01401         CommandCost ret = PerformIndustryTileSlopeCheck(tile, cur_tile, its, type, gfx, itspec_index, initial_random_bits, founder, creation_type);
01402         if (ret.Failed()) return ret;
01403       } else {
01404         Slope tileh = GetTileSlope(cur_tile);
01405         refused_slope |= IsSlopeRefused(tileh, its->slopes_refused);
01406       }
01407 
01408       if ((ind_behav & (INDUSTRYBEH_ONLY_INTOWN | INDUSTRYBEH_TOWN1200_MORE)) || // Tile must be a house
01409           ((ind_behav & INDUSTRYBEH_ONLY_NEARTOWN) && IsTileType(cur_tile, MP_HOUSE))) { // Tile is allowed to be a house (and it is a house)
01410         if (!IsTileType(cur_tile, MP_HOUSE)) {
01411           return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS);
01412         }
01413 
01414         /* Clear the tiles as OWNER_TOWN to not affect town rating, and to not clear protected buildings */
01415         Backup<CompanyByte> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
01416         CommandCost ret = DoCommand(cur_tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
01417         cur_company.Restore();
01418 
01419         if (ret.Failed()) return ret;
01420       } else {
01421         /* Clear the tiles, but do not affect town ratings */
01422         CommandCost ret = DoCommand(cur_tile, 0, 0, DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
01423 
01424         if (ret.Failed()) return ret;
01425       }
01426     }
01427   } while ((++it)->ti.x != -0x80);
01428 
01429   if (custom_shape_check != NULL) *custom_shape_check = custom_shape;
01430 
01431   /* It is almost impossible to have a fully flat land in TG, so what we
01432    *  do is that we check if we can make the land flat later on. See
01433    *  CheckIfCanLevelIndustryPlatform(). */
01434   if (!refused_slope || (_settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !custom_shape && !_ignore_restrictions)) {
01435     return CommandCost();
01436   }
01437   return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01438 }
01439 
01447 static CommandCost CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t)
01448 {
01449   if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_TOWN1200_MORE) && t->cache.population < 1200) {
01450     return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS_WITH_POPULATION_OF_1200);
01451   }
01452 
01453   if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_ONLY_NEARTOWN) && DistanceMax(t->xy, tile) > 9) {
01454     return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_NEAR_TOWN_CENTER);
01455   }
01456 
01457   return CommandCost();
01458 }
01459 
01460 static bool CheckCanTerraformSurroundingTiles(TileIndex tile, uint height, int internal)
01461 {
01462   /* Check if we don't leave the map */
01463   if (TileX(tile) == 0 || TileY(tile) == 0 || GetTileType(tile) == MP_VOID) return false;
01464 
01465   TileArea ta(tile - TileDiffXY(1, 1), 2, 2);
01466   TILE_AREA_LOOP(tile_walk, ta) {
01467     uint curh = TileHeight(tile_walk);
01468     /* Is the tile clear? */
01469     if ((GetTileType(tile_walk) != MP_CLEAR) && (GetTileType(tile_walk) != MP_TREES)) return false;
01470 
01471     /* Don't allow too big of a change if this is the sub-tile check */
01472     if (internal != 0 && Delta(curh, height) > 1) return false;
01473 
01474     /* Different height, so the surrounding tiles of this tile
01475      *  has to be correct too (in level, or almost in level)
01476      *  else you get a chain-reaction of terraforming. */
01477     if (internal == 0 && curh != height) {
01478       if (TileX(tile_walk) == 0 || TileY(tile_walk) == 0 || !CheckCanTerraformSurroundingTiles(tile_walk + TileDiffXY(-1, -1), height, internal + 1)) {
01479         return false;
01480       }
01481     }
01482   }
01483 
01484   return true;
01485 }
01486 
01491 static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlag flags, const IndustryTileTable *it, int type)
01492 {
01493   const int MKEND = -0x80;   // used for last element in an IndustryTileTable (see build_industry.h)
01494   int max_x = 0;
01495   int max_y = 0;
01496 
01497   /* Finds dimensions of largest variant of this industry */
01498   do {
01499     if (it->gfx == 0xFF) continue;  //  FF been a marquer for a check on clear water, skip it
01500     if (it->ti.x > max_x) max_x = it->ti.x;
01501     if (it->ti.y > max_y) max_y = it->ti.y;
01502   } while ((++it)->ti.x != MKEND);
01503 
01504   /* Remember level height */
01505   uint h = TileHeight(tile);
01506 
01507   if (TileX(tile) <= _settings_game.construction.industry_platform + 1U || TileY(tile) <= _settings_game.construction.industry_platform + 1U) return false;
01508   /* Check that all tiles in area and surrounding are clear
01509    * this determines that there are no obstructing items */
01510 
01511   TileArea ta(tile + TileDiffXY(-_settings_game.construction.industry_platform, -_settings_game.construction.industry_platform),
01512       max_x + 2 + 2 * _settings_game.construction.industry_platform, max_y + 2 + 2 * _settings_game.construction.industry_platform);
01513 
01514   if (TileX(ta.tile) + ta.w >= MapMaxX() || TileY(ta.tile) + ta.h >= MapMaxY()) return false;
01515 
01516   /* _current_company is OWNER_NONE for randomly generated industries and in editor, or the company who funded or prospected the industry.
01517    * Perform terraforming as OWNER_TOWN to disable autoslope and town ratings. */
01518   Backup<CompanyByte> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
01519 
01520   TILE_AREA_LOOP(tile_walk, ta) {
01521     uint curh = TileHeight(tile_walk);
01522     if (curh != h) {
01523       /* This tile needs terraforming. Check if we can do that without
01524        *  damaging the surroundings too much. */
01525       if (!CheckCanTerraformSurroundingTiles(tile_walk, h, 0)) {
01526         cur_company.Restore();
01527         return false;
01528       }
01529       /* This is not 100% correct check, but the best we can do without modifying the map.
01530        *  What is missing, is if the difference in height is more than 1.. */
01531       if (DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND).Failed()) {
01532         cur_company.Restore();
01533         return false;
01534       }
01535     }
01536   }
01537 
01538   if (flags & DC_EXEC) {
01539     /* Terraform the land under the industry */
01540     TILE_AREA_LOOP(tile_walk, ta) {
01541       uint curh = TileHeight(tile_walk);
01542       while (curh != h) {
01543         /* We give the terraforming for free here, because we can't calculate
01544          *  exact cost in the test-round, and as we all know, that will cause
01545          *  a nice assert if they don't match ;) */
01546         DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
01547         curh += (curh > h) ? -1 : 1;
01548       }
01549     }
01550   }
01551 
01552   cur_company.Restore();
01553   return true;
01554 }
01555 
01556 
01563 static CommandCost CheckIfFarEnoughFromConflictingIndustry(TileIndex tile, int type)
01564 {
01565   const IndustrySpec *indspec = GetIndustrySpec(type);
01566   const Industry *i = NULL;
01567 
01568   /* On a large map with many industries, it may be faster to check an area. */
01569   static const int dmax = 14;
01570   if (Industry::GetNumItems() > (size_t) (dmax * dmax * 2)) {
01571     const int tx = TileX(tile);
01572     const int ty = TileY(tile);
01573     TileArea tile_area = TileArea(TileXY(max(0, tx - dmax), max(0, ty - dmax)), TileXY(min(MapMaxX(), tx + dmax), min(MapMaxY(), ty + dmax)));
01574     TILE_AREA_LOOP(atile, tile_area) {
01575       if (GetTileType(atile) == MP_INDUSTRY) {
01576         const Industry *i2 = Industry::GetByTile(atile);
01577         if (i == i2) continue;
01578         i = i2;
01579         if (DistanceMax(tile, i->location.tile) > (uint)dmax) continue;
01580         if (i->type == indspec->conflicting[0] ||
01581             i->type == indspec->conflicting[1] ||
01582             i->type == indspec->conflicting[2]) {
01583           return_cmd_error(STR_ERROR_INDUSTRY_TOO_CLOSE);
01584         }
01585       }
01586     }
01587     return CommandCost();
01588   }
01589 
01590   FOR_ALL_INDUSTRIES(i) {
01591     /* Within 14 tiles from another industry is considered close */
01592     if (DistanceMax(tile, i->location.tile) > 14) continue;
01593 
01594     /* check if there are any conflicting industry types around */
01595     if (i->type == indspec->conflicting[0] ||
01596         i->type == indspec->conflicting[1] ||
01597         i->type == indspec->conflicting[2]) {
01598       return_cmd_error(STR_ERROR_INDUSTRY_TOO_CLOSE);
01599     }
01600   }
01601   return CommandCost();
01602 }
01603 
01608 static void AdvertiseIndustryOpening(const Industry *ind)
01609 {
01610   const IndustrySpec *ind_spc = GetIndustrySpec(ind->type);
01611   SetDParam(0, ind_spc->name);
01612   if (ind_spc->new_industry_text > STR_LAST_STRINGID) {
01613     SetDParam(1, STR_TOWN_NAME);
01614     SetDParam(2, ind->town->index);
01615   } else {
01616     SetDParam(1, ind->town->index);
01617   }
01618   AddIndustryNewsItem(ind_spc->new_industry_text, NT_INDUSTRY_OPEN, ind->index);
01619   AI::BroadcastNewEvent(new ScriptEventIndustryOpen(ind->index));
01620   Game::NewEvent(new ScriptEventIndustryOpen(ind->index));
01621 }
01622 
01634 static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type, const IndustryTileTable *it, byte layout, Town *t, Owner founder, uint16 initial_random_bits)
01635 {
01636   const IndustrySpec *indspec = GetIndustrySpec(type);
01637 
01638   i->location = TileArea(tile, 1, 1);
01639   i->type = type;
01640   Industry::IncIndustryTypeCount(type);
01641 
01642   i->produced_cargo[0] = indspec->produced_cargo[0];
01643   i->produced_cargo[1] = indspec->produced_cargo[1];
01644   i->accepts_cargo[0] = indspec->accepts_cargo[0];
01645   i->accepts_cargo[1] = indspec->accepts_cargo[1];
01646   i->accepts_cargo[2] = indspec->accepts_cargo[2];
01647   i->production_rate[0] = indspec->production_rate[0];
01648   i->production_rate[1] = indspec->production_rate[1];
01649 
01650   /* don't use smooth economy for industries using production related callbacks */
01651   if (indspec->UsesSmoothEconomy()) {
01652     i->production_rate[0] = min((RandomRange(256) + 128) * i->production_rate[0] >> 8, 255);
01653     i->production_rate[1] = min((RandomRange(256) + 128) * i->production_rate[1] >> 8, 255);
01654   }
01655 
01656   i->town = t;
01657   i->owner = OWNER_NONE;
01658 
01659   uint16 r = Random();
01660   i->random_colour = GB(r, 0, 4);
01661   i->counter = GB(r, 4, 12);
01662   i->random = initial_random_bits;
01663   i->produced_cargo_waiting[0] = 0;
01664   i->produced_cargo_waiting[1] = 0;
01665   i->incoming_cargo_waiting[0] = 0;
01666   i->incoming_cargo_waiting[1] = 0;
01667   i->incoming_cargo_waiting[2] = 0;
01668   i->this_month_production[0] = 0;
01669   i->this_month_production[1] = 0;
01670   i->this_month_transported[0] = 0;
01671   i->this_month_transported[1] = 0;
01672   i->last_month_pct_transported[0] = 0;
01673   i->last_month_pct_transported[1] = 0;
01674   i->last_month_transported[0] = 0;
01675   i->last_month_transported[1] = 0;
01676   i->was_cargo_delivered = false;
01677   i->last_prod_year = _cur_year;
01678   i->founder = founder;
01679 
01680   i->construction_date = _date;
01681   i->construction_type = (_game_mode == GM_EDITOR) ? ICT_SCENARIO_EDITOR :
01682       (_generating_world ? ICT_MAP_GENERATION : ICT_NORMAL_GAMEPLAY);
01683 
01684   /* Adding 1 here makes it conform to specs of var44 of varaction2 for industries
01685    * 0 = created prior of newindustries
01686    * else, chosen layout + 1 */
01687   i->selected_layout = layout + 1;
01688 
01689   i->prod_level = PRODLEVEL_DEFAULT;
01690 
01691   /* Call callbacks after the regular fields got initialised. */
01692 
01693   if (HasBit(indspec->callback_mask, CBM_IND_PROD_CHANGE_BUILD)) {
01694     uint16 res = GetIndustryCallback(CBID_INDUSTRY_PROD_CHANGE_BUILD, 0, Random(), i, type, INVALID_TILE);
01695     if (res != CALLBACK_FAILED) {
01696       if (res < PRODLEVEL_MINIMUM || res > PRODLEVEL_MAXIMUM) {
01697         ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_PROD_CHANGE_BUILD, res);
01698       } else {
01699         i->prod_level = res;
01700         i->RecomputeProductionMultipliers();
01701       }
01702     }
01703   }
01704 
01705   if (_generating_world) {
01706     i->last_month_production[0] = i->production_rate[0] * 8;
01707     i->last_month_production[1] = i->production_rate[1] * 8;
01708   } else {
01709     i->last_month_production[0] = i->last_month_production[1] = 0;
01710   }
01711 
01712   if (HasBit(indspec->callback_mask, CBM_IND_DECIDE_COLOUR)) {
01713     uint16 res = GetIndustryCallback(CBID_INDUSTRY_DECIDE_COLOUR, 0, 0, i, type, INVALID_TILE);
01714     if (res != CALLBACK_FAILED) {
01715       if (GB(res, 4, 11) != 0) ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_DECIDE_COLOUR, res);
01716       i->random_colour = GB(res, 0, 4);
01717     }
01718   }
01719 
01720   if (HasBit(indspec->callback_mask, CBM_IND_INPUT_CARGO_TYPES)) {
01721     for (uint j = 0; j < lengthof(i->accepts_cargo); j++) i->accepts_cargo[j] = CT_INVALID;
01722     for (uint j = 0; j < lengthof(i->accepts_cargo); j++) {
01723       uint16 res = GetIndustryCallback(CBID_INDUSTRY_INPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
01724       if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
01725       if (indspec->grf_prop.grffile->grf_version >= 8 && res >= 0x100) {
01726         ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_INPUT_CARGO_TYPES, res);
01727         break;
01728       }
01729       i->accepts_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
01730     }
01731   }
01732 
01733   if (HasBit(indspec->callback_mask, CBM_IND_OUTPUT_CARGO_TYPES)) {
01734     for (uint j = 0; j < lengthof(i->produced_cargo); j++) i->produced_cargo[j] = CT_INVALID;
01735     for (uint j = 0; j < lengthof(i->produced_cargo); j++) {
01736       uint16 res = GetIndustryCallback(CBID_INDUSTRY_OUTPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
01737       if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
01738       if (indspec->grf_prop.grffile->grf_version >= 8 && res >= 0x100) {
01739         ErrorUnknownCallbackResult(indspec->grf_prop.grffile->grfid, CBID_INDUSTRY_OUTPUT_CARGO_TYPES, res);
01740         break;
01741       }
01742       i->produced_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
01743     }
01744   }
01745 
01746   /* Plant the tiles */
01747 
01748   do {
01749     TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
01750 
01751     if (it->gfx != GFX_WATERTILE_SPECIALCHECK) {
01752       i->location.Add(cur_tile);
01753 
01754       WaterClass wc = (IsWaterTile(cur_tile) ? GetWaterClass(cur_tile) : WATER_CLASS_INVALID);
01755 
01756       DoCommand(cur_tile, 0, 0, DC_EXEC | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
01757 
01758       MakeIndustry(cur_tile, i->index, it->gfx, Random(), wc);
01759 
01760       if (_generating_world) {
01761         SetIndustryConstructionCounter(cur_tile, 3);
01762         SetIndustryConstructionStage(cur_tile, 2);
01763       }
01764 
01765       /* it->gfx is stored in the map. But the translated ID cur_gfx is the interesting one */
01766       IndustryGfx cur_gfx = GetTranslatedIndustryTileID(it->gfx);
01767       const IndustryTileSpec *its = GetIndustryTileSpec(cur_gfx);
01768       if (its->animation.status != ANIM_STATUS_NO_ANIMATION) AddAnimatedTile(cur_tile);
01769     }
01770   } while ((++it)->ti.x != -0x80);
01771 
01772   if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
01773     for (uint j = 0; j != 50; j++) PlantRandomFarmField(i);
01774   }
01775   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
01776 
01777   Station::RecomputeIndustriesNearForAll();
01778 }
01779 
01796 static CommandCost CreateNewIndustryHelper(TileIndex tile, IndustryType type, DoCommandFlag flags, const IndustrySpec *indspec, uint itspec_index, uint32 random_var8f, uint16 random_initial_bits, Owner founder, IndustryAvailabilityCallType creation_type, Industry **ip)
01797 {
01798   assert(itspec_index < indspec->num_table);
01799   const IndustryTileTable *it = indspec->table[itspec_index];
01800   bool custom_shape_check = false;
01801 
01802   *ip = NULL;
01803 
01804   SmallVector<ClearedObjectArea, 1> object_areas(_cleared_object_areas);
01805   CommandCost ret = CheckIfIndustryTilesAreFree(tile, it, itspec_index, type, random_initial_bits, founder, creation_type, &custom_shape_check);
01806   _cleared_object_areas = object_areas;
01807   if (ret.Failed()) return ret;
01808 
01809   if (HasBit(GetIndustrySpec(type)->callback_mask, CBM_IND_LOCATION)) {
01810     ret = CheckIfCallBackAllowsCreation(tile, type, itspec_index, random_var8f, random_initial_bits, founder, creation_type);
01811   } else {
01812     ret = _check_new_industry_procs[indspec->check_proc](tile);
01813   }
01814   if (ret.Failed()) return ret;
01815 
01816   if (!custom_shape_check && _settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world &&
01817       !_ignore_restrictions && !CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER, it, type)) {
01818     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01819   }
01820 
01821   ret = CheckIfFarEnoughFromConflictingIndustry(tile, type);
01822   if (ret.Failed()) return ret;
01823 
01824   Town *t = NULL;
01825   ret = FindTownForIndustry(tile, type, &t);
01826   if (ret.Failed()) return ret;
01827   assert(t != NULL);
01828 
01829   ret = CheckIfIndustryIsAllowed(tile, type, t);
01830   if (ret.Failed()) return ret;
01831 
01832   if (!Industry::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_INDUSTRIES);
01833 
01834   if (flags & DC_EXEC) {
01835     *ip = new Industry(tile);
01836     if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER | DC_EXEC, it, type);
01837     DoCreateNewIndustry(*ip, tile, type, it, itspec_index, t, founder, random_initial_bits);
01838   }
01839 
01840   return CommandCost();
01841 }
01842 
01855 CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01856 {
01857   IndustryType it = GB(p1, 0, 8);
01858   if (it >= NUM_INDUSTRYTYPES) return CMD_ERROR;
01859 
01860   const IndustrySpec *indspec = GetIndustrySpec(it);
01861 
01862   /* Check if the to-be built/founded industry is available for this climate. */
01863   if (!indspec->enabled || indspec->num_table == 0) return CMD_ERROR;
01864 
01865   /* If the setting for raw-material industries is not on, you cannot build raw-material industries.
01866    * Raw material industries are industries that do not accept cargo (at least for now) */
01867   if (_game_mode != GM_EDITOR && _current_company != OWNER_DEITY && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
01868     return CMD_ERROR;
01869   }
01870 
01871   if (_game_mode != GM_EDITOR && GetIndustryProbabilityCallback(it, _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_USERCREATION, 1) == 0) {
01872     return CMD_ERROR;
01873   }
01874 
01875   Randomizer randomizer;
01876   randomizer.SetSeed(p2);
01877   uint16 random_initial_bits = GB(p2, 0, 16);
01878   uint32 random_var8f = randomizer.Next();
01879   int num_layouts = indspec->num_table;
01880   CommandCost ret = CommandCost(STR_ERROR_SITE_UNSUITABLE);
01881   const bool deity_prospect = _current_company == OWNER_DEITY && !HasBit(p1, 16);
01882 
01883   Industry *ind = NULL;
01884   if (deity_prospect || (_game_mode != GM_EDITOR && _current_company != OWNER_DEITY && _settings_game.construction.raw_industry_construction == 2 && indspec->IsRawIndustry())) {
01885     if (flags & DC_EXEC) {
01886       /* Prospected industries are build as OWNER_TOWN to not e.g. be build on owned land of the founder */
01887       Backup<CompanyByte> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
01888       /* Prospecting has a chance to fail, however we cannot guarantee that something can
01889        * be built on the map, so the chance gets lower when the map is fuller, but there
01890        * is nothing we can really do about that. */
01891       if (deity_prospect || Random() <= indspec->prospecting_chance) {
01892         for (int i = 0; i < 5000; i++) {
01893           /* We should not have more than one Random() in a function call
01894            * because parameter evaluation order is not guaranteed in the c++ standard
01895            */
01896           tile = RandomTile();
01897           /* Start with a random layout */
01898           int layout = RandomRange(num_layouts);
01899           /* Check now each layout, starting with the random one */
01900           for (int j = 0; j < num_layouts; j++) {
01901             layout = (layout + 1) % num_layouts;
01902             ret = CreateNewIndustryHelper(tile, it, flags, indspec, layout, random_var8f, random_initial_bits, cur_company.GetOriginalValue(), _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_PROSPECTCREATION, &ind);
01903             if (ret.Succeeded()) break;
01904           }
01905           if (ret.Succeeded()) break;
01906         }
01907       }
01908       cur_company.Restore();
01909     }
01910   } else {
01911     int layout = GB(p1, 8, 8);
01912     if (layout >= num_layouts) return CMD_ERROR;
01913 
01914     /* Check subsequently each layout, starting with the given layout in p1 */
01915     for (int i = 0; i < num_layouts; i++) {
01916       layout = (layout + 1) % num_layouts;
01917       ret = CreateNewIndustryHelper(tile, it, flags, indspec, layout, random_var8f, random_initial_bits, _current_company, _current_company == OWNER_DEITY ? IACT_RANDOMCREATION : IACT_USERCREATION, &ind);
01918       if (ret.Succeeded()) break;
01919     }
01920 
01921     /* If it still failed, there's no suitable layout to build here, return the error */
01922     if (ret.Failed()) return ret;
01923   }
01924 
01925   if ((flags & DC_EXEC) && ind != NULL && _game_mode != GM_EDITOR) {
01926     AdvertiseIndustryOpening(ind);
01927   }
01928 
01929   return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost());
01930 }
01931 
01932 
01940 static Industry *CreateNewIndustry(TileIndex tile, IndustryType type, IndustryAvailabilityCallType creation_type)
01941 {
01942   const IndustrySpec *indspec = GetIndustrySpec(type);
01943 
01944   uint32 seed = Random();
01945   uint32 seed2 = Random();
01946   Industry *i = NULL;
01947   CommandCost ret = CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, RandomRange(indspec->num_table), seed, GB(seed2, 0, 16), OWNER_NONE, creation_type, &i);
01948   assert(i != NULL || ret.Failed());
01949   return i;
01950 }
01951 
01958 static uint32 GetScaledIndustryGenerationProbability(IndustryType it, bool *force_at_least_one)
01959 {
01960   const IndustrySpec *ind_spc = GetIndustrySpec(it);
01961   uint32 chance = ind_spc->appear_creation[_settings_game.game_creation.landscape] * 16; // * 16 to increase precision
01962   if (!ind_spc->enabled || ind_spc->num_table == 0 ||
01963       (_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY) ||
01964       (chance = GetIndustryProbabilityCallback(it, IACT_MAPGENERATION, chance)) == 0) {
01965     *force_at_least_one = false;
01966     return 0;
01967   } else {
01968     /* We want industries appearing at coast to appear less often on bigger maps, as length of coast increases slower than map area.
01969      * For simplicity we scale in both cases, though scaling the probabilities of all industries has no effect. */
01970     chance = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(chance) : ScaleByMapSize(chance);
01971 
01972     *force_at_least_one = (chance > 0) && !(ind_spc->behaviour & INDUSTRYBEH_NOBUILT_MAPCREATION) && (_game_mode != GM_EDITOR);
01973     return chance;
01974   }
01975 }
01976 
01983 static uint16 GetIndustryGamePlayProbability(IndustryType it, byte *min_number)
01984 {
01985   if (_settings_game.difficulty.industry_density == ID_FUND_ONLY) {
01986     *min_number = 0;
01987     return 0;
01988   }
01989 
01990   const IndustrySpec *ind_spc = GetIndustrySpec(it);
01991   byte chance = ind_spc->appear_ingame[_settings_game.game_creation.landscape];
01992   if (!ind_spc->enabled || ind_spc->num_table == 0 ||
01993       ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) ||
01994       ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) ||
01995       (chance = GetIndustryProbabilityCallback(it, IACT_RANDOMCREATION, chance)) == 0) {
01996     *min_number = 0;
01997     return 0;
01998   }
01999   *min_number = (ind_spc->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) ? 1 : 0;
02000   return chance;
02001 }
02002 
02007 static uint GetNumberOfIndustries()
02008 {
02009   /* Number of industries on a 256x256 map. */
02010   static const uint16 numof_industry_table[] = {
02011     0,    // none
02012     0,    // minimal
02013     10,   // very low
02014     25,   // low
02015     55,   // normal
02016     80,   // high
02017   };
02018 
02019   assert(lengthof(numof_industry_table) == ID_END);
02020   uint difficulty = (_game_mode != GM_EDITOR) ? _settings_game.difficulty.industry_density : (uint)ID_VERY_LOW;
02021   return min(IndustryPool::MAX_SIZE, ScaleByMapSize(numof_industry_table[difficulty]));
02022 }
02023 
02032 static Industry *PlaceIndustry(IndustryType type, IndustryAvailabilityCallType creation_type, bool try_hard)
02033 {
02034   uint tries = try_hard ? 10000u : 2000u;
02035   for (; tries > 0; tries--) {
02036     Industry *ind = CreateNewIndustry(RandomTile(), type, creation_type);
02037     if (ind != NULL) return ind;
02038   }
02039   return NULL;
02040 }
02041 
02047 static void PlaceInitialIndustry(IndustryType type, bool try_hard)
02048 {
02049   Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
02050 
02051   IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
02052   PlaceIndustry(type, IACT_MAPGENERATION, try_hard);
02053 
02054   cur_company.Restore();
02055 }
02056 
02061 static uint GetCurrentTotalNumberOfIndustries()
02062 {
02063   int total = 0;
02064   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) total += Industry::GetIndustryTypeCount(it);
02065   return total;
02066 }
02067 
02068 
02070 void IndustryTypeBuildData::Reset()
02071 {
02072   this->probability  = 0;
02073   this->min_number   = 0;
02074   this->target_count = 0;
02075   this->max_wait     = 1;
02076   this->wait_count   = 0;
02077 }
02078 
02080 void IndustryBuildData::Reset()
02081 {
02082   this->wanted_inds = GetCurrentTotalNumberOfIndustries() << 16;
02083 
02084   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02085     this->builddata[it].Reset();
02086   }
02087 }
02088 
02090 void IndustryBuildData::MonthlyLoop()
02091 {
02092   static const int NEWINDS_PER_MONTH = 0x38000 / (10 * 12); // lower 16 bits is a float fraction, 3.5 industries per decade, divided by 10 * 12 months.
02093   if (_settings_game.difficulty.industry_density == ID_FUND_ONLY) return; // 'no industries' setting.
02094 
02095   /* To prevent running out of unused industries for the player to connect,
02096    * add a fraction of new industries each month, but only if the manager can keep up. */
02097   uint max_behind = 1 + min(99u, ScaleByMapSize(3)); // At most 2 industries for small maps, and 100 at the biggest map (about 6 months industry build attempts).
02098   if (GetCurrentTotalNumberOfIndustries() + max_behind >= (this->wanted_inds >> 16)) {
02099     this->wanted_inds += ScaleByMapSize(NEWINDS_PER_MONTH);
02100   }
02101 }
02102 
02107 void GenerateIndustries()
02108 {
02109   if (_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY) return; // No industries in the game.
02110 
02111   uint32 industry_probs[NUM_INDUSTRYTYPES];
02112   bool force_at_least_one[NUM_INDUSTRYTYPES];
02113   uint32 total_prob = 0;
02114   uint num_forced = 0;
02115 
02116   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02117     industry_probs[it] = GetScaledIndustryGenerationProbability(it, force_at_least_one + it);
02118     total_prob += industry_probs[it];
02119     if (force_at_least_one[it]) num_forced++;
02120   }
02121 
02122   uint total_amount = GetNumberOfIndustries();
02123   if (total_prob == 0 || total_amount < num_forced) {
02124     /* Only place the forced ones */
02125     total_amount = num_forced;
02126   }
02127 
02128   SetGeneratingWorldProgress(GWP_INDUSTRY, total_amount);
02129 
02130   /* Try to build one industry per type independent of any probabilities */
02131   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02132     if (force_at_least_one[it]) {
02133       assert(total_amount > 0);
02134       total_amount--;
02135       PlaceInitialIndustry(it, true);
02136     }
02137   }
02138 
02139   /* Add the remaining industries according to their probabilities */
02140   for (uint i = 0; i < total_amount; i++) {
02141     uint32 r = RandomRange(total_prob);
02142     IndustryType it = 0;
02143     while (r >= industry_probs[it]) {
02144       r -= industry_probs[it];
02145       it++;
02146       assert(it < NUM_INDUSTRYTYPES);
02147     }
02148     assert(industry_probs[it] > 0);
02149     PlaceInitialIndustry(it, false);
02150   }
02151   _industry_builder.Reset();
02152 }
02153 
02158 static void UpdateIndustryStatistics(Industry *i)
02159 {
02160   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
02161     if (i->produced_cargo[j] != CT_INVALID) {
02162       byte pct = 0;
02163       if (i->this_month_production[j] != 0) {
02164         i->last_prod_year = _cur_year;
02165         pct = min(i->this_month_transported[j] * 256 / i->this_month_production[j], 255);
02166       }
02167       i->last_month_pct_transported[j] = pct;
02168 
02169       i->last_month_production[j] = i->this_month_production[j];
02170       i->this_month_production[j] = 0;
02171 
02172       i->last_month_transported[j] = i->this_month_transported[j];
02173       i->this_month_transported[j] = 0;
02174     }
02175   }
02176 }
02177 
02182 void Industry::RecomputeProductionMultipliers()
02183 {
02184   const IndustrySpec *indspec = GetIndustrySpec(this->type);
02185   assert(!indspec->UsesSmoothEconomy());
02186 
02187   /* Rates are rounded up, so e.g. oilrig always produces some passengers */
02188   this->production_rate[0] = min(CeilDiv(indspec->production_rate[0] * this->prod_level, PRODLEVEL_DEFAULT), 0xFF);
02189   this->production_rate[1] = min(CeilDiv(indspec->production_rate[1] * this->prod_level, PRODLEVEL_DEFAULT), 0xFF);
02190 }
02191 
02192 
02198 bool IndustryTypeBuildData::GetIndustryTypeData(IndustryType it)
02199 {
02200   byte min_number;
02201   uint32 probability = GetIndustryGamePlayProbability(it, &min_number);
02202   bool changed = min_number != this->min_number || probability != this->probability;
02203   this->min_number = min_number;
02204   this->probability = probability;
02205   return changed;
02206 }
02207 
02209 void IndustryBuildData::SetupTargetCount()
02210 {
02211   bool changed = false;
02212   uint num_planned = 0; // Number of industries planned in the industry build data.
02213   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02214     changed |= this->builddata[it].GetIndustryTypeData(it);
02215     num_planned += this->builddata[it].target_count;
02216   }
02217   uint total_amount = this->wanted_inds >> 16; // Desired total number of industries.
02218   changed |= num_planned != total_amount;
02219   if (!changed) return; // All industries are still the same, no need to re-randomize.
02220 
02221   /* Initialize the target counts. */
02222   uint force_build = 0;  // Number of industries that should always be available.
02223   uint32 total_prob = 0; // Sum of probabilities.
02224   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02225     IndustryTypeBuildData *ibd = this->builddata + it;
02226     force_build += ibd->min_number;
02227     ibd->target_count = ibd->min_number;
02228     total_prob += ibd->probability;
02229   }
02230 
02231   if (total_prob == 0) return; // No buildable industries.
02232 
02233   /* Subtract forced industries from the number of industries available for construction. */
02234   total_amount = (total_amount <= force_build) ? 0 : total_amount - force_build;
02235 
02236   /* Assign number of industries that should be aimed for, by using the probability as a weight. */
02237   while (total_amount > 0) {
02238     uint32 r = RandomRange(total_prob);
02239     IndustryType it = 0;
02240     while (r >= this->builddata[it].probability) {
02241       r -= this->builddata[it].probability;
02242       it++;
02243       assert(it < NUM_INDUSTRYTYPES);
02244     }
02245     assert(this->builddata[it].probability > 0);
02246     this->builddata[it].target_count++;
02247     total_amount--;
02248   }
02249 }
02250 
02254 void IndustryBuildData::TryBuildNewIndustry()
02255 {
02256   this->SetupTargetCount();
02257 
02258   int missing = 0;       // Number of industries that need to be build.
02259   uint count = 0;        // Number of industry types eligible for build.
02260   uint32 total_prob = 0; // Sum of probabilities.
02261   IndustryType forced_build = NUM_INDUSTRYTYPES; // Industry type that should be forcibly build.
02262   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02263     int difference = this->builddata[it].target_count - Industry::GetIndustryTypeCount(it);
02264     missing += difference;
02265     if (this->builddata[it].wait_count > 0) continue; // This type may not be built now.
02266     if (difference > 0) {
02267       if (Industry::GetIndustryTypeCount(it) == 0 && this->builddata[it].min_number > 0) {
02268         /* An industry that should exist at least once, is not available. Force it, trying the most needed one first. */
02269         if (forced_build == NUM_INDUSTRYTYPES ||
02270             difference > this->builddata[forced_build].target_count - Industry::GetIndustryTypeCount(forced_build)) {
02271           forced_build = it;
02272         }
02273       }
02274       total_prob += difference;
02275       count++;
02276     }
02277   }
02278 
02279   if (EconomyIsInRecession() || (forced_build == NUM_INDUSTRYTYPES && (missing <= 0 || total_prob == 0))) count = 0; // Skip creation of an industry.
02280 
02281   if (count >= 1) {
02282     /* If not forced, pick a weighted random industry to build.
02283      * For the case that count == 1, there is no need to draw a random number. */
02284     IndustryType it;
02285     if (forced_build != NUM_INDUSTRYTYPES) {
02286       it = forced_build;
02287     } else {
02288       /* Non-forced, select an industry type to build (weighted random). */
02289       uint32 r = 0; // Initialized to silence the compiler.
02290       if (count > 1) r = RandomRange(total_prob);
02291       for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
02292         if (this->builddata[it].wait_count > 0) continue; // Type may not be built now.
02293         int difference = this->builddata[it].target_count - Industry::GetIndustryTypeCount(it);
02294         if (difference <= 0) continue; // Too many of this kind.
02295         if (count == 1) break;
02296         if (r < (uint)difference) break;
02297         r -= difference;
02298       }
02299       assert(it < NUM_INDUSTRYTYPES && this->builddata[it].target_count > Industry::GetIndustryTypeCount(it));
02300     }
02301 
02302     /* Try to create the industry. */
02303     const Industry *ind = PlaceIndustry(it, IACT_RANDOMCREATION, false);
02304     if (ind == NULL) {
02305       this->builddata[it].wait_count = this->builddata[it].max_wait + 1; // Compensate for decrementing below.
02306       this->builddata[it].max_wait = min(1000, this->builddata[it].max_wait + 2);
02307     } else {
02308       AdvertiseIndustryOpening(ind);
02309       this->builddata[it].max_wait = max(this->builddata[it].max_wait / 2, 1); // Reduce waiting time of the industry type.
02310     }
02311   }
02312 
02313   /* Decrement wait counters. */
02314   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02315     if (this->builddata[it].wait_count > 0) this->builddata[it].wait_count--;
02316   }
02317 }
02318 
02327 static bool CheckIndustryCloseDownProtection(IndustryType type)
02328 {
02329   const IndustrySpec *indspec = GetIndustrySpec(type);
02330 
02331   /* oil wells (or the industries with that flag set) are always allowed to closedown */
02332   if ((indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE) return false;
02333   return (indspec->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) == 0 && Industry::GetIndustryTypeCount(type) <= 1;
02334 }
02335 
02345 static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces)
02346 {
02347   if (cargo == CT_INVALID) return;
02348 
02349   /* Check for acceptance of cargo */
02350   for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) {
02351     if (cargo == ind->accepts_cargo[j] && !IndustryTemporarilyRefusesCargo(ind, cargo)) {
02352       *c_accepts = true;
02353       break;
02354     }
02355   }
02356 
02357   /* Check for produced cargo */
02358   for (byte j = 0; j < lengthof(ind->produced_cargo); j++) {
02359     if (cargo == ind->produced_cargo[j]) {
02360       *c_produces = true;
02361       break;
02362     }
02363   }
02364 }
02365 
02379 static int WhoCanServiceIndustry(Industry *ind)
02380 {
02381   /* Find all stations within reach of the industry */
02382   StationList stations;
02383   FindStationsAroundTiles(ind->location, &stations);
02384 
02385   if (stations.Length() == 0) return 0; // No stations found at all => nobody services
02386 
02387   const Vehicle *v;
02388   int result = 0;
02389   FOR_ALL_VEHICLES(v) {
02390     /* Is it worthwhile to try this vehicle? */
02391     if (v->owner != _local_company && result != 0) continue;
02392 
02393     /* Check whether it accepts the right kind of cargo */
02394     bool c_accepts = false;
02395     bool c_produces = false;
02396     if (v->type == VEH_TRAIN && v->IsFrontEngine()) {
02397       for (const Vehicle *u = v; u != NULL; u = u->Next()) {
02398         CanCargoServiceIndustry(u->cargo_type, ind, &c_accepts, &c_produces);
02399       }
02400     } else if (v->type == VEH_ROAD || v->type == VEH_SHIP || v->type == VEH_AIRCRAFT) {
02401       CanCargoServiceIndustry(v->cargo_type, ind, &c_accepts, &c_produces);
02402     } else {
02403       continue;
02404     }
02405     if (!c_accepts && !c_produces) continue; // Wrong cargo
02406 
02407     /* Check orders of the vehicle.
02408      * We cannot check the first of shared orders only, since the first vehicle in such a chain
02409      * may have a different cargo type.
02410      */
02411     const Order *o;
02412     FOR_VEHICLE_ORDERS(v, o) {
02413       if (o->IsType(OT_GOTO_STATION) && !(o->GetUnloadType() & OUFB_TRANSFER)) {
02414         /* Vehicle visits a station to load or unload */
02415         Station *st = Station::Get(o->GetDestination());
02416         assert(st != NULL);
02417 
02418         /* Same cargo produced by industry is dropped here => not serviced by vehicle v */
02419         if ((o->GetUnloadType() & OUFB_UNLOAD) && !c_accepts) break;
02420 
02421         if (stations.Contains(st)) {
02422           if (v->owner == _local_company) return 2; // Company services industry
02423           result = 1; // Competitor services industry
02424         }
02425       }
02426     }
02427   }
02428   return result;
02429 }
02430 
02438 static void ReportNewsProductionChangeIndustry(Industry *ind, CargoID type, int percent)
02439 {
02440   NewsType nt;
02441 
02442   switch (WhoCanServiceIndustry(ind)) {
02443     case 0: nt = NT_INDUSTRY_NOBODY;  break;
02444     case 1: nt = NT_INDUSTRY_OTHER;   break;
02445     case 2: nt = NT_INDUSTRY_COMPANY; break;
02446     default: NOT_REACHED();
02447   }
02448   SetDParam(2, abs(percent));
02449   SetDParam(0, CargoSpec::Get(type)->name);
02450   SetDParam(1, ind->index);
02451   AddIndustryNewsItem(
02452     percent >= 0 ? STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_SMOOTH : STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_SMOOTH,
02453     nt,
02454     ind->index
02455   );
02456 }
02457 
02458 static const uint PERCENT_TRANSPORTED_60 = 153;
02459 static const uint PERCENT_TRANSPORTED_80 = 204;
02460 
02466 static void ChangeIndustryProduction(Industry *i, bool monthly)
02467 {
02468   StringID str = STR_NULL;
02469   bool closeit = false;
02470   const IndustrySpec *indspec = GetIndustrySpec(i->type);
02471   bool standard = false;
02472   bool suppress_message = false;
02473   bool recalculate_multipliers = false; 
02474   /* don't use smooth economy for industries using production related callbacks */
02475   bool smooth_economy = indspec->UsesSmoothEconomy();
02476   byte div = 0;
02477   byte mul = 0;
02478   int8 increment = 0;
02479 
02480   bool callback_enabled = HasBit(indspec->callback_mask, monthly ? CBM_IND_MONTHLYPROD_CHANGE : CBM_IND_PRODUCTION_CHANGE);
02481   if (callback_enabled) {
02482     uint16 res = GetIndustryCallback(monthly ? CBID_INDUSTRY_MONTHLYPROD_CHANGE : CBID_INDUSTRY_PRODUCTION_CHANGE, 0, Random(), i, i->type, i->location.tile);
02483     if (res != CALLBACK_FAILED) { // failed callback means "do nothing"
02484       suppress_message = HasBit(res, 7);
02485       /* Get the custom message if any */
02486       if (HasBit(res, 8)) str = MapGRFStringID(indspec->grf_prop.grffile->grfid, GB(GetRegister(0x100), 0, 16));
02487       res = GB(res, 0, 4);
02488       switch (res) {
02489         default: NOT_REACHED();
02490         case 0x0: break;                  // Do nothing, but show the custom message if any
02491         case 0x1: div = 1; break;         // Halve industry production. If production reaches the quarter of the default, the industry is closed instead.
02492         case 0x2: mul = 1; break;         // Double industry production if it hasn't reached eight times of the original yet.
02493         case 0x3: closeit = true; break;  // The industry announces imminent closure, and is physically removed from the map next month.
02494         case 0x4: standard = true; break; // Do the standard random production change as if this industry was a primary one.
02495         case 0x5: case 0x6: case 0x7:     // Divide production by 4, 8, 16
02496         case 0x8: div = res - 0x3; break; // Divide production by 32
02497         case 0x9: case 0xA: case 0xB:     // Multiply production by 4, 8, 16
02498         case 0xC: mul = res - 0x7; break; // Multiply production by 32
02499         case 0xD:                         // decrement production
02500         case 0xE:                         // increment production
02501           increment = res == 0x0D ? -1 : 1;
02502           break;
02503         case 0xF:                         // Set production to third byte of register 0x100
02504           i->prod_level = Clamp(GB(GetRegister(0x100), 16, 8), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
02505           recalculate_multipliers = true;
02506           break;
02507       }
02508     }
02509   } else {
02510     if (monthly != smooth_economy) return;
02511     if (indspec->life_type == INDUSTRYLIFE_BLACK_HOLE) return;
02512   }
02513 
02514   if (standard || (!callback_enabled && (indspec->life_type & (INDUSTRYLIFE_ORGANIC | INDUSTRYLIFE_EXTRACTIVE)) != 0)) {
02515     /* decrease or increase */
02516     bool only_decrease = (indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE;
02517 
02518     if (smooth_economy) {
02519       closeit = true;
02520       for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
02521         if (i->produced_cargo[j] == CT_INVALID) continue;
02522         uint32 r = Random();
02523         int old_prod, new_prod, percent;
02524         /* If over 60% is transported, mult is 1, else mult is -1. */
02525         int mult = (i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_60) ? 1 : -1;
02526 
02527         new_prod = old_prod = i->production_rate[j];
02528 
02529         /* For industries with only_decrease flags (temperate terrain Oil Wells),
02530          * the multiplier will always be -1 so they will only decrease. */
02531         if (only_decrease) {
02532           mult = -1;
02533         /* For normal industries, if over 60% is transported, 33% chance for decrease.
02534          * Bonus for very high station ratings (over 80%): 16% chance for decrease. */
02535         } else if (Chance16I(1, ((i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_80) ? 6 : 3), r)) {
02536           mult *= -1;
02537         }
02538 
02539         /* 4.5% chance for 3-23% (or 1 unit for very low productions) production change,
02540          * determined by mult value. If mult = 1 prod. increases, else (-1) it decreases. */
02541         if (Chance16I(1, 22, r >> 16)) {
02542           new_prod += mult * (max(((RandomRange(50) + 10) * old_prod) >> 8, 1U));
02543         }
02544 
02545         /* Prevent production to overflow or Oil Rig passengers to be over-"produced" */
02546         new_prod = Clamp(new_prod, 1, 255);
02547 
02548         if (((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0) && j == 1) {
02549           new_prod = Clamp(new_prod, 0, 16);
02550         }
02551 
02552         /* Do not stop closing the industry when it has the lowest possible production rate */
02553         if (new_prod == old_prod && old_prod > 1) {
02554           closeit = false;
02555           continue;
02556         }
02557 
02558         percent = (old_prod == 0) ? 100 : (new_prod * 100 / old_prod - 100);
02559         i->production_rate[j] = new_prod;
02560 
02561         /* Close the industry when it has the lowest possible production rate */
02562         if (new_prod > 1) closeit = false;
02563 
02564         if (abs(percent) >= 10) {
02565           ReportNewsProductionChangeIndustry(i, i->produced_cargo[j], percent);
02566         }
02567       }
02568     } else {
02569       if (only_decrease || Chance16(1, 3)) {
02570         /* If more than 60% transported, 66% chance of increase, else 33% chance of increase */
02571         if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != Chance16(1, 3)) {
02572           mul = 1; // Increase production
02573         } else {
02574           div = 1; // Decrease production
02575         }
02576       }
02577     }
02578   }
02579 
02580   if (!callback_enabled && (indspec->life_type & INDUSTRYLIFE_PROCESSING)) {
02581     if ( (byte)(_cur_year - i->last_prod_year) >= 5 && Chance16(1, smooth_economy ? 180 : 2)) {
02582       closeit = true;
02583     }
02584   }
02585 
02586   /* Increase if needed */
02587   while (mul-- != 0 && i->prod_level < PRODLEVEL_MAXIMUM) {
02588     i->prod_level = min(i->prod_level * 2, PRODLEVEL_MAXIMUM);
02589     recalculate_multipliers = true;
02590     if (str == STR_NULL) str = indspec->production_up_text;
02591   }
02592 
02593   /* Decrease if needed */
02594   while (div-- != 0 && !closeit) {
02595     if (i->prod_level == PRODLEVEL_MINIMUM) {
02596       closeit = true;
02597     } else {
02598       i->prod_level = max(i->prod_level / 2, (int)PRODLEVEL_MINIMUM); // typecast to int required to please MSVC
02599       recalculate_multipliers = true;
02600       if (str == STR_NULL) str = indspec->production_down_text;
02601     }
02602   }
02603 
02604   /* Increase or Decreasing the production level if needed */
02605   if (increment != 0) {
02606     if (increment < 0 && i->prod_level == PRODLEVEL_MINIMUM) {
02607       closeit = true;
02608     } else {
02609       i->prod_level = ClampU(i->prod_level + increment, PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
02610       recalculate_multipliers = true;
02611     }
02612   }
02613 
02614   /* Recalculate production_rate
02615    * For non-smooth economy these should always be synchronized with prod_level */
02616   if (recalculate_multipliers) i->RecomputeProductionMultipliers();
02617 
02618   /* Close if needed and allowed */
02619   if (closeit && !CheckIndustryCloseDownProtection(i->type)) {
02620     i->prod_level = PRODLEVEL_CLOSURE;
02621     SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
02622     str = indspec->closure_text;
02623   }
02624 
02625   if (!suppress_message && str != STR_NULL) {
02626     NewsType nt;
02627     /* Compute news category */
02628     if (closeit) {
02629       nt = NT_INDUSTRY_CLOSE;
02630       AI::BroadcastNewEvent(new ScriptEventIndustryClose(i->index));
02631       Game::NewEvent(new ScriptEventIndustryClose(i->index));
02632     } else {
02633       switch (WhoCanServiceIndustry(i)) {
02634         case 0: nt = NT_INDUSTRY_NOBODY;  break;
02635         case 1: nt = NT_INDUSTRY_OTHER;   break;
02636         case 2: nt = NT_INDUSTRY_COMPANY; break;
02637         default: NOT_REACHED();
02638       }
02639     }
02640     /* Set parameters of news string */
02641     if (str > STR_LAST_STRINGID) {
02642       SetDParam(0, STR_TOWN_NAME);
02643       SetDParam(1, i->town->index);
02644       SetDParam(2, indspec->name);
02645     } else if (closeit) {
02646       SetDParam(0, STR_FORMAT_INDUSTRY_NAME);
02647       SetDParam(1, i->town->index);
02648       SetDParam(2, indspec->name);
02649     } else {
02650       SetDParam(0, i->index);
02651     }
02652     /* and report the news to the user */
02653     if (closeit) {
02654       AddTileNewsItem(str, nt, i->location.tile + TileDiffXY(1, 1));
02655     } else {
02656       AddIndustryNewsItem(str, nt, i->index);
02657     }
02658   }
02659 }
02660 
02668 void IndustryDailyLoop()
02669 {
02670   _economy.industry_daily_change_counter += _economy.industry_daily_increment;
02671 
02672   /* Bits 16-31 of industry_construction_counter contain the number of industries to change/create today,
02673    * the lower 16 bit are a fractional part that might accumulate over several days until it
02674    * is sufficient for an industry. */
02675   uint16 change_loop = _economy.industry_daily_change_counter >> 16;
02676 
02677   /* Reset the active part of the counter, just keeping the "fractional part" */
02678   _economy.industry_daily_change_counter &= 0xFFFF;
02679 
02680   if (change_loop == 0) {
02681     return;  // Nothing to do? get out
02682   }
02683 
02684   Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
02685 
02686   /* perform the required industry changes for the day */
02687 
02688   uint perc = 3; // Between 3% and 9% chance of creating a new industry.
02689   if ((_industry_builder.wanted_inds >> 16) > GetCurrentTotalNumberOfIndustries()) {
02690     perc = min(9u, perc + (_industry_builder.wanted_inds >> 16) - GetCurrentTotalNumberOfIndustries());
02691   }
02692   for (uint16 j = 0; j < change_loop; j++) {
02693     if (Chance16(perc, 100)) {
02694       _industry_builder.TryBuildNewIndustry();
02695     } else {
02696       Industry *i = Industry::GetRandom();
02697       if (i != NULL) {
02698         ChangeIndustryProduction(i, false);
02699         SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
02700       }
02701     }
02702   }
02703 
02704   cur_company.Restore();
02705 
02706   /* production-change */
02707   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
02708 }
02709 
02710 void IndustryMonthlyLoop()
02711 {
02712   Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
02713 
02714   _industry_builder.MonthlyLoop();
02715 
02716   Industry *i;
02717   FOR_ALL_INDUSTRIES(i) {
02718     UpdateIndustryStatistics(i);
02719     if (i->prod_level == PRODLEVEL_CLOSURE) {
02720       delete i;
02721     } else {
02722       ChangeIndustryProduction(i, true);
02723       SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
02724     }
02725   }
02726 
02727   cur_company.Restore();
02728 
02729   /* production-change */
02730   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
02731 }
02732 
02733 
02734 void InitializeIndustries()
02735 {
02736   Industry::ResetIndustryCounts();
02737   _industry_sound_tile = 0;
02738 
02739   _industry_builder.Reset();
02740 }
02741 
02746 bool IndustrySpec::IsRawIndustry() const
02747 {
02748   return (this->life_type & (INDUSTRYLIFE_EXTRACTIVE | INDUSTRYLIFE_ORGANIC)) != 0;
02749 }
02750 
02755 bool IndustrySpec::IsProcessingIndustry() const
02756 {
02757   /* Lumber mills are neither raw nor processing */
02758   return (this->life_type & INDUSTRYLIFE_PROCESSING) != 0 &&
02759       (this->behaviour & INDUSTRYBEH_CUT_TREES) == 0;
02760 }
02761 
02766 Money IndustrySpec::GetConstructionCost() const
02767 {
02768   /* Building raw industries like secondary uses different price base */
02769   return (_price[(_settings_game.construction.raw_industry_construction == 1 && this->IsRawIndustry()) ?
02770       PR_BUILD_INDUSTRY_RAW : PR_BUILD_INDUSTRY] * this->cost_multiplier) >> 8;
02771 }
02772 
02779 Money IndustrySpec::GetRemovalCost() const
02780 {
02781   return (_price[PR_CLEAR_INDUSTRY] * this->removal_cost_multiplier) >> 8;
02782 }
02783 
02788 bool IndustrySpec::UsesSmoothEconomy() const
02789 {
02790   return _settings_game.economy.smooth_economy &&
02791     !(HasBit(this->callback_mask, CBM_IND_PRODUCTION_256_TICKS) || HasBit(this->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) && // production callbacks
02792     !(HasBit(this->callback_mask, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(this->callback_mask, CBM_IND_PRODUCTION_CHANGE) || HasBit(this->callback_mask, CBM_IND_PROD_CHANGE_BUILD)); // production change callbacks
02793 }
02794 
02795 static CommandCost TerraformTile_Industry(TileIndex tile, DoCommandFlag flags, int z_new, Slope tileh_new)
02796 {
02797   if (AutoslopeEnabled()) {
02798     /* We imitate here TTDP's behaviour:
02799      *  - Both new and old slope must not be steep.
02800      *  - TileMaxZ must not be changed.
02801      *  - Allow autoslope by default.
02802      *  - Disallow autoslope if callback succeeds and returns non-zero.
02803      */
02804     Slope tileh_old = GetTileSlope(tile);
02805     /* TileMaxZ must not be changed. Slopes must not be steep. */
02806     if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
02807       const IndustryGfx gfx = GetIndustryGfx(tile);
02808       const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
02809 
02810       /* Call callback 3C 'disable autosloping for industry tiles'. */
02811       if (HasBit(itspec->callback_mask, CBM_INDT_AUTOSLOPE)) {
02812         /* If the callback fails, allow autoslope. */
02813         uint16 res = GetIndustryTileCallback(CBID_INDTILE_AUTOSLOPE, 0, 0, gfx, Industry::GetByTile(tile), tile);
02814         if (res == CALLBACK_FAILED || !ConvertBooleanCallback(itspec->grf_prop.grffile, CBID_INDTILE_AUTOSLOPE, res)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
02815       } else {
02816         /* allow autoslope */
02817         return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
02818       }
02819     }
02820   }
02821   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
02822 }
02823 
02824 extern const TileTypeProcs _tile_type_industry_procs = {
02825   DrawTile_Industry,           // draw_tile_proc
02826   GetSlopePixelZ_Industry,     // get_slope_z_proc
02827   ClearTile_Industry,          // clear_tile_proc
02828   AddAcceptedCargo_Industry,   // add_accepted_cargo_proc
02829   GetTileDesc_Industry,        // get_tile_desc_proc
02830   GetTileTrackStatus_Industry, // get_tile_track_status_proc
02831   ClickTile_Industry,          // click_tile_proc
02832   AnimateTile_Industry,        // animate_tile_proc
02833   TileLoop_Industry,           // tile_loop_proc
02834   ChangeTileOwner_Industry,    // change_tile_owner_proc
02835   NULL,                        // add_produced_cargo_proc
02836   NULL,                        // vehicle_enter_tile_proc
02837   GetFoundation_Industry,      // get_foundation_proc
02838   TerraformTile_Industry,      // terraform_tile_proc
02839 };