industry_cmd.cpp

Go to the documentation of this file.
00001 /* $Id$ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "stdafx.h"
00013 #include "clear_map.h"
00014 #include "industry.h"
00015 #include "station_base.h"
00016 #include "train.h"
00017 #include "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 "tgp.h"
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   DecIndustryTypeCount(this->type);
00172 
00173   DeleteIndustryNews(this->index);
00174   DeleteWindowById(WC_INDUSTRY_VIEW, this->index);
00175   DeleteNewGRFInspectWindow(GSF_INDUSTRIES, this->index);
00176 
00177   DeleteSubsidyWith(ST_INDUSTRY, this->index);
00178   CargoPacket::InvalidateAllFrom(ST_INDUSTRY, this->index);
00179 }
00180 
00185 void Industry::PostDestructor(size_t index)
00186 {
00187   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
00188   Station::RecomputeIndustriesNearForAll();
00189 }
00190 
00191 
00196 /* static */ Industry *Industry::GetRandom()
00197 {
00198   if (Industry::GetNumItems() == 0) return NULL;
00199   int num = RandomRange((uint16)Industry::GetNumItems());
00200   size_t index = MAX_UVALUE(size_t);
00201 
00202   while (num >= 0) {
00203     num--;
00204     index++;
00205 
00206     /* Make sure we have a valid industry */
00207     while (!Industry::IsValidID(index)) {
00208       index++;
00209       assert(index < Industry::GetPoolSize());
00210     }
00211   }
00212 
00213   return Industry::Get(index);
00214 }
00215 
00216 
00217 static void IndustryDrawSugarMine(const TileInfo *ti)
00218 {
00219   if (!IsIndustryCompleted(ti->tile)) return;
00220 
00221   const DrawIndustryAnimationStruct *d = &_draw_industry_spec1[GetAnimationFrame(ti->tile)];
00222 
00223   AddChildSpriteScreen(SPR_IT_SUGAR_MINE_SIEVE + d->image_1, PAL_NONE, d->x, 0);
00224 
00225   if (d->image_2 != 0) {
00226     AddChildSpriteScreen(SPR_IT_SUGAR_MINE_CLOUDS + d->image_2 - 1, PAL_NONE, 8, 41);
00227   }
00228 
00229   if (d->image_3 != 0) {
00230     AddChildSpriteScreen(SPR_IT_SUGAR_MINE_PILE + d->image_3 - 1, PAL_NONE,
00231       _drawtile_proc1[d->image_3 - 1].x, _drawtile_proc1[d->image_3 - 1].y);
00232   }
00233 }
00234 
00235 static void IndustryDrawToffeeQuarry(const TileInfo *ti)
00236 {
00237   uint8 x = 0;
00238 
00239   if (IsIndustryCompleted(ti->tile)) {
00240     x = _industry_anim_offs_toffee[GetAnimationFrame(ti->tile)];
00241     if (x == 0xFF) {
00242       x = 0;
00243     }
00244   }
00245 
00246   AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_SHOVEL, PAL_NONE, 22 - x, 24 + x);
00247   AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_TOFFEE, PAL_NONE, 6, 14);
00248 }
00249 
00250 static void IndustryDrawBubbleGenerator( const TileInfo *ti)
00251 {
00252   if (IsIndustryCompleted(ti->tile)) {
00253     AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_BUBBLE, PAL_NONE, 5, _industry_anim_offs_bubbles[GetAnimationFrame(ti->tile)]);
00254   } else {
00255     AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_SPRING, PAL_NONE, 3, 67);
00256   }
00257 }
00258 
00259 static void IndustryDrawToyFactory(const TileInfo *ti)
00260 {
00261   const DrawIndustryAnimationStruct *d = &_industry_anim_offs_toys[GetAnimationFrame(ti->tile)];
00262 
00263   if (d->image_1 != 0xFF) {
00264     AddChildSpriteScreen(SPR_IT_TOY_FACTORY_CLAY, PAL_NONE, d->x, 96 + d->image_1);
00265   }
00266 
00267   if (d->image_2 != 0xFF) {
00268     AddChildSpriteScreen(SPR_IT_TOY_FACTORY_ROBOT, PAL_NONE, 16 - d->image_2 * 2, 100 + d->image_2);
00269   }
00270 
00271   AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP, PAL_NONE, 7, d->image_3);
00272   AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP_HOLDER, PAL_NONE, 0, 42);
00273 }
00274 
00275 static void IndustryDrawCoalPlantSparks(const TileInfo *ti)
00276 {
00277   if (IsIndustryCompleted(ti->tile)) {
00278     uint8 image = GetAnimationFrame(ti->tile);
00279 
00280     if (image != 0 && image < 7) {
00281       AddChildSpriteScreen(image + SPR_IT_POWER_PLANT_TRANSFORMERS,
00282         PAL_NONE,
00283         _coal_plant_sparks[image - 1].x,
00284         _coal_plant_sparks[image - 1].y
00285       );
00286     }
00287   }
00288 }
00289 
00290 typedef void IndustryDrawTileProc(const TileInfo *ti);
00291 static IndustryDrawTileProc * const _industry_draw_tile_procs[5] = {
00292   IndustryDrawSugarMine,
00293   IndustryDrawToffeeQuarry,
00294   IndustryDrawBubbleGenerator,
00295   IndustryDrawToyFactory,
00296   IndustryDrawCoalPlantSparks,
00297 };
00298 
00299 static void DrawTile_Industry(TileInfo *ti)
00300 {
00301   IndustryGfx gfx = GetIndustryGfx(ti->tile);
00302   Industry *ind = Industry::GetByTile(ti->tile);
00303   const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
00304 
00305   /* Retrieve pointer to the draw industry tile struct */
00306   if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00307     /* Draw the tile using the specialized method of newgrf industrytile.
00308      * DrawNewIndustry will return false if ever the resolver could not
00309      * find any sprite to display.  So in this case, we will jump on the
00310      * substitute gfx instead. */
00311     if (indts->grf_prop.spritegroup[0] != NULL && DrawNewIndustryTile(ti, ind, gfx, indts)) {
00312       return;
00313     } else {
00314       /* No sprite group (or no valid one) found, meaning no graphics associated.
00315        * Use the substitute one instead */
00316       if (indts->grf_prop.subst_id != INVALID_INDUSTRYTILE) {
00317         gfx = indts->grf_prop.subst_id;
00318         /* And point the industrytile spec accordingly */
00319         indts = GetIndustryTileSpec(gfx);
00320       }
00321     }
00322   }
00323 
00324   const DrawBuildingsTileStruct *dits = &_industry_draw_tile_data[gfx << 2 | (indts->anim_state ?
00325       GetAnimationFrame(ti->tile) & INDUSTRY_COMPLETED :
00326       GetIndustryConstructionStage(ti->tile))];
00327 
00328   SpriteID image = dits->ground.sprite;
00329 
00330   /* DrawFoundation() modifes ti->z and ti->tileh */
00331   if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
00332 
00333   /* If the ground sprite is the default flat water sprite, draw also canal/river borders.
00334    * Do not do this if the tile's WaterClass is 'land'. */
00335   if (image == SPR_FLAT_WATER_TILE && IsTileOnWater(ti->tile)) {
00336     DrawWaterClassGround(ti);
00337   } else {
00338     DrawGroundSprite(image, GroundSpritePaletteTransform(image, dits->ground.pal, GENERAL_SPRITE_COLOUR(ind->random_colour)));
00339   }
00340 
00341   /* If industries are transparent and invisible, do not draw the upper part */
00342   if (IsInvisibilitySet(TO_INDUSTRIES)) return;
00343 
00344   /* Add industry on top of the ground? */
00345   image = dits->building.sprite;
00346   if (image != 0) {
00347     AddSortableSpriteToDraw(image, SpriteLayoutPaletteTransform(image, dits->building.pal, GENERAL_SPRITE_COLOUR(ind->random_colour)),
00348       ti->x + dits->subtile_x,
00349       ti->y + dits->subtile_y,
00350       dits->width,
00351       dits->height,
00352       dits->dz,
00353       ti->z,
00354       IsTransparencySet(TO_INDUSTRIES));
00355 
00356     if (IsTransparencySet(TO_INDUSTRIES)) return;
00357   }
00358 
00359   {
00360     int proc = dits->draw_proc - 1;
00361     if (proc >= 0) _industry_draw_tile_procs[proc](ti);
00362   }
00363 }
00364 
00365 static uint GetSlopeZ_Industry(TileIndex tile, uint x, uint y)
00366 {
00367   return GetTileMaxZ(tile);
00368 }
00369 
00370 static Foundation GetFoundation_Industry(TileIndex tile, Slope tileh)
00371 {
00372   IndustryGfx gfx = GetIndustryGfx(tile);
00373 
00374   /* For NewGRF industry tiles we might not be drawing a foundation. We need to
00375    * account for this, as other structures should
00376    * draw the wall of the foundation in this case.
00377    */
00378   if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00379     const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
00380     if (indts->grf_prop.spritegroup[0] != NULL && HasBit(indts->callback_mask, CBM_INDT_DRAW_FOUNDATIONS)) {
00381       uint32 callback_res = GetIndustryTileCallback(CBID_INDTILE_DRAW_FOUNDATIONS, 0, 0, gfx, Industry::GetByTile(tile), tile);
00382       if (callback_res == 0) return FOUNDATION_NONE;
00383     }
00384   }
00385   return FlatteningFoundation(tileh);
00386 }
00387 
00388 static void AddAcceptedCargo_Industry(TileIndex tile, CargoArray &acceptance, uint32 *always_accepted)
00389 {
00390   IndustryGfx gfx = GetIndustryGfx(tile);
00391   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
00392 
00393   /* When we have to use a callback, we put our data in the next two variables */
00394   CargoID raw_accepts_cargo[lengthof(itspec->accepts_cargo)];
00395   uint8 raw_cargo_acceptance[lengthof(itspec->acceptance)];
00396 
00397   /* And then these will always point to a same sized array with the required data */
00398   const CargoID *accepts_cargo = itspec->accepts_cargo;
00399   const uint8 *cargo_acceptance = itspec->acceptance;
00400 
00401   if (HasBit(itspec->callback_mask, CBM_INDT_ACCEPT_CARGO)) {
00402     uint16 res = GetIndustryTileCallback(CBID_INDTILE_ACCEPT_CARGO, 0, 0, gfx, Industry::GetByTile(tile), tile);
00403     if (res != CALLBACK_FAILED) {
00404       accepts_cargo = raw_accepts_cargo;
00405       for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_accepts_cargo[i] = GetCargoTranslation(GB(res, i * 5, 5), itspec->grf_prop.grffile);
00406     }
00407   }
00408 
00409   if (HasBit(itspec->callback_mask, CBM_INDT_CARGO_ACCEPTANCE)) {
00410     uint16 res = GetIndustryTileCallback(CBID_INDTILE_CARGO_ACCEPTANCE, 0, 0, gfx, Industry::GetByTile(tile), tile);
00411     if (res != CALLBACK_FAILED) {
00412       cargo_acceptance = raw_cargo_acceptance;
00413       for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_cargo_acceptance[i] = GB(res, i * 4, 4);
00414     }
00415   }
00416 
00417   const Industry *ind = Industry::GetByTile(tile);
00418   for (byte i = 0; i < lengthof(itspec->accepts_cargo); i++) {
00419     CargoID a = accepts_cargo[i];
00420     if (a == CT_INVALID || cargo_acceptance[i] == 0) continue; // work only with valid cargos
00421 
00422     /* Add accepted cargo */
00423     acceptance[a] += cargo_acceptance[i];
00424 
00425     /* Maybe set 'always accepted' bit (if it's not set already) */
00426     if (HasBit(*always_accepted, a)) continue;
00427 
00428     bool accepts = false;
00429     for (uint cargo_index = 0; cargo_index < lengthof(ind->accepts_cargo); cargo_index++) {
00430       /* Test whether the industry itself accepts the cargo type */
00431       if (ind->accepts_cargo[cargo_index] == a) {
00432         accepts = true;
00433         break;
00434       }
00435     }
00436 
00437     if (accepts) continue;
00438 
00439     /* If the industry itself doesn't accept this cargo, set 'always accepted' bit */
00440     SetBit(*always_accepted, a);
00441   }
00442 }
00443 
00444 static void GetTileDesc_Industry(TileIndex tile, TileDesc *td)
00445 {
00446   const Industry *i = Industry::GetByTile(tile);
00447   const IndustrySpec *is = GetIndustrySpec(i->type);
00448 
00449   td->owner[0] = i->owner;
00450   td->str = is->name;
00451   if (!IsIndustryCompleted(tile)) {
00452     SetDParamX(td->dparam, 0, td->str);
00453     td->str = STR_LAI_TOWN_INDUSTRY_DESCRIPTION_UNDER_CONSTRUCTION;
00454   }
00455 
00456   if (is->grf_prop.grffile != NULL) {
00457     td->grf = GetGRFConfig(is->grf_prop.grffile->grfid)->GetName();
00458   }
00459 }
00460 
00461 static CommandCost ClearTile_Industry(TileIndex tile, DoCommandFlag flags)
00462 {
00463   Industry *i = Industry::GetByTile(tile);
00464   const IndustrySpec *indspec = GetIndustrySpec(i->type);
00465 
00466   /* water can destroy industries
00467    * in editor you can bulldoze industries
00468    * with magic_bulldozer cheat you can destroy industries
00469    * (area around OILRIG is water, so water shouldn't flood it
00470    */
00471   if ((_current_company != OWNER_WATER && _game_mode != GM_EDITOR &&
00472       !_cheats.magic_bulldozer.value) ||
00473       ((flags & DC_AUTO) != 0) ||
00474       (_current_company == OWNER_WATER &&
00475         ((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) ||
00476         HasBit(GetIndustryTileSpec(GetIndustryGfx(tile))->slopes_refused, 5)))) {
00477     SetDParam(1, indspec->name);
00478     return_cmd_error(flags & DC_AUTO ? STR_ERROR_GENERIC_OBJECT_IN_THE_WAY : INVALID_STRING_ID);
00479   }
00480 
00481   if (flags & DC_EXEC) {
00482     AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
00483     delete i;
00484   }
00485   return CommandCost(EXPENSES_CONSTRUCTION, indspec->GetRemovalCost());
00486 }
00487 
00488 static void TransportIndustryGoods(TileIndex tile)
00489 {
00490   Industry *i = Industry::GetByTile(tile);
00491   const IndustrySpec *indspec = GetIndustrySpec(i->type);
00492   bool moved_cargo = false;
00493 
00494   StationFinder stations(i->location);
00495 
00496   for (uint j = 0; j < lengthof(i->produced_cargo_waiting); j++) {
00497     uint cw = min(i->produced_cargo_waiting[j], 255);
00498     if (cw > indspec->minimal_cargo && i->produced_cargo[j] != CT_INVALID) {
00499       i->produced_cargo_waiting[j] -= cw;
00500 
00501       /* fluctuating economy? */
00502       if (EconomyIsInRecession()) cw = (cw + 1) / 2;
00503 
00504       i->this_month_production[j] += cw;
00505 
00506       uint am = MoveGoodsToStation(i->produced_cargo[j], cw, ST_INDUSTRY, i->index, stations.GetStations());
00507       i->this_month_transported[j] += am;
00508 
00509       moved_cargo |= (am != 0);
00510     }
00511   }
00512 
00513   if (moved_cargo && !StartStopIndustryTileAnimation(i, IAT_INDUSTRY_DISTRIBUTES_CARGO)) {
00514     uint newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_production;
00515 
00516     if (newgfx != INDUSTRYTILE_NOANIM) {
00517       ResetIndustryConstructionStage(tile);
00518       SetIndustryCompleted(tile, true);
00519       SetIndustryGfx(tile, newgfx);
00520       MarkTileDirtyByTile(tile);
00521     }
00522   }
00523 }
00524 
00525 
00526 static void AnimateTile_Industry(TileIndex tile)
00527 {
00528   IndustryGfx gfx = GetIndustryGfx(tile);
00529 
00530   if (GetIndustryTileSpec(gfx)->animation.status != ANIM_STATUS_NO_ANIMATION) {
00531     AnimateNewIndustryTile(tile);
00532     return;
00533   }
00534 
00535   switch (gfx) {
00536   case GFX_SUGAR_MINE_SIEVE:
00537     if ((_tick_counter & 1) == 0) {
00538       byte m = GetAnimationFrame(tile) + 1;
00539 
00540       switch (m & 7) {
00541       case 2: SndPlayTileFx(SND_2D_RIP_2, tile); break;
00542       case 6: SndPlayTileFx(SND_29_RIP, tile); break;
00543       }
00544 
00545       if (m >= 96) {
00546         m = 0;
00547         DeleteAnimatedTile(tile);
00548       }
00549       SetAnimationFrame(tile, m);
00550 
00551       MarkTileDirtyByTile(tile);
00552     }
00553     break;
00554 
00555   case GFX_TOFFEE_QUARY:
00556     if ((_tick_counter & 3) == 0) {
00557       byte m = GetAnimationFrame(tile);
00558 
00559       if (_industry_anim_offs_toffee[m] == 0xFF) {
00560         SndPlayTileFx(SND_30_CARTOON_SOUND, tile);
00561       }
00562 
00563       if (++m >= 70) {
00564         m = 0;
00565         DeleteAnimatedTile(tile);
00566       }
00567       SetAnimationFrame(tile, m);
00568 
00569       MarkTileDirtyByTile(tile);
00570     }
00571     break;
00572 
00573   case GFX_BUBBLE_CATCHER:
00574     if ((_tick_counter & 1) == 0) {
00575       byte m = GetAnimationFrame(tile);
00576 
00577       if (++m >= 40) {
00578         m = 0;
00579         DeleteAnimatedTile(tile);
00580       }
00581       SetAnimationFrame(tile, m);
00582 
00583       MarkTileDirtyByTile(tile);
00584     }
00585     break;
00586 
00587   /* Sparks on a coal plant */
00588   case GFX_POWERPLANT_SPARKS:
00589     if ((_tick_counter & 3) == 0) {
00590       byte m = GetAnimationFrame(tile);
00591       if (m == 6) {
00592         SetAnimationFrame(tile, 0);
00593         DeleteAnimatedTile(tile);
00594       } else {
00595         SetAnimationFrame(tile, m + 1);
00596         MarkTileDirtyByTile(tile);
00597       }
00598     }
00599     break;
00600 
00601   case GFX_TOY_FACTORY:
00602     if ((_tick_counter & 1) == 0) {
00603       byte m = GetAnimationFrame(tile) + 1;
00604 
00605       switch (m) {
00606         case  1: SndPlayTileFx(SND_2C_MACHINERY, tile); break;
00607         case 23: SndPlayTileFx(SND_2B_COMEDY_HIT, tile); break;
00608         case 28: SndPlayTileFx(SND_2A_EXTRACT_AND_POP, tile); break;
00609         default:
00610           if (m >= 50) {
00611             int n = GetIndustryAnimationLoop(tile) + 1;
00612             m = 0;
00613             if (n >= 8) {
00614               n = 0;
00615               DeleteAnimatedTile(tile);
00616             }
00617             SetIndustryAnimationLoop(tile, n);
00618           }
00619       }
00620 
00621       SetAnimationFrame(tile, m);
00622       MarkTileDirtyByTile(tile);
00623     }
00624     break;
00625 
00626   case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
00627   case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
00628   case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
00629   case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
00630     if ((_tick_counter & 3) == 0) {
00631       IndustryGfx gfx = GetIndustryGfx(tile);
00632 
00633       gfx = (gfx < 155) ? gfx + 1 : 148;
00634       SetIndustryGfx(tile, gfx);
00635       MarkTileDirtyByTile(tile);
00636     }
00637     break;
00638 
00639   case GFX_OILWELL_ANIMATED_1:
00640   case GFX_OILWELL_ANIMATED_2:
00641   case GFX_OILWELL_ANIMATED_3:
00642     if ((_tick_counter & 7) == 0) {
00643       bool b = Chance16(1, 7);
00644       IndustryGfx gfx = GetIndustryGfx(tile);
00645 
00646       byte m = GetAnimationFrame(tile) + 1;
00647       if (m == 4 && (m = 0, ++gfx) == GFX_OILWELL_ANIMATED_3 + 1 && (gfx = GFX_OILWELL_ANIMATED_1, b)) {
00648         SetIndustryGfx(tile, GFX_OILWELL_NOT_ANIMATED);
00649         SetIndustryConstructionStage(tile, 3);
00650         DeleteAnimatedTile(tile);
00651       } else {
00652         SetAnimationFrame(tile, m);
00653         SetIndustryGfx(tile, gfx);
00654         MarkTileDirtyByTile(tile);
00655       }
00656     }
00657     break;
00658 
00659   case GFX_COAL_MINE_TOWER_ANIMATED:
00660   case GFX_COPPER_MINE_TOWER_ANIMATED:
00661   case GFX_GOLD_MINE_TOWER_ANIMATED: {
00662       int state = _tick_counter & 0x7FF;
00663 
00664       if ((state -= 0x400) < 0) return;
00665 
00666       if (state < 0x1A0) {
00667         if (state < 0x20 || state >= 0x180) {
00668           byte m = GetAnimationFrame(tile);
00669           if (!(m & 0x40)) {
00670             SetAnimationFrame(tile, m | 0x40);
00671             SndPlayTileFx(SND_0B_MINING_MACHINERY, tile);
00672           }
00673           if (state & 7) return;
00674         } else {
00675           if (state & 3) return;
00676         }
00677         byte m = (GetAnimationFrame(tile) + 1) | 0x40;
00678         if (m > 0xC2) m = 0xC0;
00679         SetAnimationFrame(tile, m);
00680         MarkTileDirtyByTile(tile);
00681       } else if (state >= 0x200 && state < 0x3A0) {
00682         int i = (state < 0x220 || state >= 0x380) ? 7 : 3;
00683         if (state & i) return;
00684 
00685         byte m = (GetAnimationFrame(tile) & 0xBF) - 1;
00686         if (m < 0x80) m = 0x82;
00687         SetAnimationFrame(tile, m);
00688         MarkTileDirtyByTile(tile);
00689       }
00690       break;
00691     }
00692   }
00693 }
00694 
00695 static void CreateChimneySmoke(TileIndex tile)
00696 {
00697   uint x = TileX(tile) * TILE_SIZE;
00698   uint y = TileY(tile) * TILE_SIZE;
00699   uint z = GetTileMaxZ(tile);
00700 
00701   CreateEffectVehicle(x + 15, y + 14, z + 59, EV_CHIMNEY_SMOKE);
00702 }
00703 
00704 static void MakeIndustryTileBigger(TileIndex tile)
00705 {
00706   byte cnt = GetIndustryConstructionCounter(tile) + 1;
00707   if (cnt != 4) {
00708     SetIndustryConstructionCounter(tile, cnt);
00709     return;
00710   }
00711 
00712   byte stage = GetIndustryConstructionStage(tile) + 1;
00713   SetIndustryConstructionCounter(tile, 0);
00714   SetIndustryConstructionStage(tile, stage);
00715   StartStopIndustryTileAnimation(tile, IAT_CONSTRUCTION_STATE_CHANGE);
00716   if (stage == INDUSTRY_COMPLETED) SetIndustryCompleted(tile, true);
00717 
00718   MarkTileDirtyByTile(tile);
00719 
00720   if (!IsIndustryCompleted(tile)) return;
00721 
00722   IndustryGfx gfx = GetIndustryGfx(tile);
00723   if (gfx >= NEW_INDUSTRYTILEOFFSET) {
00724     /* New industries are already animated on construction. */
00725     return;
00726   }
00727 
00728   switch (gfx) {
00729   case GFX_POWERPLANT_CHIMNEY:
00730     CreateChimneySmoke(tile);
00731     break;
00732 
00733   case GFX_OILRIG_1: {
00734     /* Do not require an industry tile to be after the first two GFX_OILRIG_1
00735      * tiles (like the default oil rig). Do a proper check to ensure the
00736      * tiles belong to the same industry and based on that build the oil rig's
00737      * station. */
00738     TileIndex other = tile + TileDiffXY(0, 1);
00739 
00740     if (IsTileType(other, MP_INDUSTRY) &&
00741         GetIndustryGfx(other) == GFX_OILRIG_1 &&
00742         GetIndustryIndex(tile) == GetIndustryIndex(other)) {
00743       BuildOilRig(tile);
00744     }
00745     break;
00746   }
00747 
00748   case GFX_TOY_FACTORY:
00749   case GFX_BUBBLE_CATCHER:
00750   case GFX_TOFFEE_QUARY:
00751     SetAnimationFrame(tile, 0);
00752     SetIndustryAnimationLoop(tile, 0);
00753     break;
00754 
00755   case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
00756   case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
00757   case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
00758   case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
00759     AddAnimatedTile(tile);
00760     break;
00761   }
00762 }
00763 
00764 static void TileLoopIndustry_BubbleGenerator(TileIndex tile)
00765 {
00766   static const int8 _bubble_spawn_location[3][4] = {
00767     { 11,   0, -4, -14 },
00768     { -4, -10, -4,   1 },
00769     { 49,  59, 60,  65 },
00770   };
00771 
00772   SndPlayTileFx(SND_2E_EXTRACT_AND_POP, tile);
00773 
00774   int dir = Random() & 3;
00775 
00776   EffectVehicle *v = CreateEffectVehicleAbove(
00777     TileX(tile) * TILE_SIZE + _bubble_spawn_location[0][dir],
00778     TileY(tile) * TILE_SIZE + _bubble_spawn_location[1][dir],
00779     _bubble_spawn_location[2][dir],
00780     EV_BUBBLE
00781   );
00782 
00783   if (v != NULL) v->animation_substate = dir;
00784 }
00785 
00786 static void TileLoop_Industry(TileIndex tile)
00787 {
00788   if (IsTileOnWater(tile)) TileLoop_Water(tile);
00789 
00790   /* Normally this doesn't happen, but if an industry NewGRF is removed
00791    * an industry that was previously build on water can now be flooded.
00792    * If this happens the tile is no longer an industry tile after
00793    * returning from TileLoop_Water. */
00794   if (!IsTileType(tile, MP_INDUSTRY)) return;
00795 
00796   TriggerIndustryTile(tile, INDTILE_TRIGGER_TILE_LOOP);
00797 
00798   if (!IsIndustryCompleted(tile)) {
00799     MakeIndustryTileBigger(tile);
00800     return;
00801   }
00802 
00803   if (_game_mode == GM_EDITOR) return;
00804 
00805   TransportIndustryGoods(tile);
00806 
00807   if (StartStopIndustryTileAnimation(tile, IAT_TILELOOP)) return;
00808 
00809   IndustryGfx newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_next;
00810   if (newgfx != INDUSTRYTILE_NOANIM) {
00811     ResetIndustryConstructionStage(tile);
00812     SetIndustryGfx(tile, newgfx);
00813     MarkTileDirtyByTile(tile);
00814     return;
00815   }
00816 
00817   IndustryGfx gfx = GetIndustryGfx(tile);
00818   switch (gfx) {
00819   case GFX_COAL_MINE_TOWER_NOT_ANIMATED:
00820   case GFX_COPPER_MINE_TOWER_NOT_ANIMATED:
00821   case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:
00822     if (!(_tick_counter & 0x400) && Chance16(1, 2)) {
00823       switch (gfx) {
00824         case GFX_COAL_MINE_TOWER_NOT_ANIMATED:   gfx = GFX_COAL_MINE_TOWER_ANIMATED;   break;
00825         case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_ANIMATED; break;
00826         case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:   gfx = GFX_GOLD_MINE_TOWER_ANIMATED;   break;
00827       }
00828       SetIndustryGfx(tile, gfx);
00829       SetAnimationFrame(tile, 0x80);
00830       AddAnimatedTile(tile);
00831     }
00832     break;
00833 
00834   case GFX_OILWELL_NOT_ANIMATED:
00835     if (Chance16(1, 6)) {
00836       SetIndustryGfx(tile, GFX_OILWELL_ANIMATED_1);
00837       SetAnimationFrame(tile, 0);
00838       AddAnimatedTile(tile);
00839     }
00840     break;
00841 
00842   case GFX_COAL_MINE_TOWER_ANIMATED:
00843   case GFX_COPPER_MINE_TOWER_ANIMATED:
00844   case GFX_GOLD_MINE_TOWER_ANIMATED:
00845     if (!(_tick_counter & 0x400)) {
00846       switch (gfx) {
00847         case GFX_COAL_MINE_TOWER_ANIMATED:   gfx = GFX_COAL_MINE_TOWER_NOT_ANIMATED;   break;
00848         case GFX_COPPER_MINE_TOWER_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_NOT_ANIMATED; break;
00849         case GFX_GOLD_MINE_TOWER_ANIMATED:   gfx = GFX_GOLD_MINE_TOWER_NOT_ANIMATED;   break;
00850       }
00851       SetIndustryGfx(tile, gfx);
00852       SetIndustryCompleted(tile, true);
00853       SetIndustryConstructionStage(tile, 3);
00854       DeleteAnimatedTile(tile);
00855     }
00856     break;
00857 
00858   case GFX_POWERPLANT_SPARKS:
00859     if (Chance16(1, 3)) {
00860       SndPlayTileFx(SND_0C_ELECTRIC_SPARK, tile);
00861       AddAnimatedTile(tile);
00862     }
00863     break;
00864 
00865   case GFX_COPPER_MINE_CHIMNEY:
00866     CreateEffectVehicleAbove(TileX(tile) * TILE_SIZE + 6, TileY(tile) * TILE_SIZE + 6, 43, EV_COPPER_MINE_SMOKE);
00867     break;
00868 
00869 
00870   case GFX_TOY_FACTORY: {
00871       Industry *i = Industry::GetByTile(tile);
00872       if (i->was_cargo_delivered) {
00873         i->was_cargo_delivered = false;
00874         SetIndustryAnimationLoop(tile, 0);
00875         AddAnimatedTile(tile);
00876       }
00877     }
00878     break;
00879 
00880   case GFX_BUBBLE_GENERATOR:
00881     TileLoopIndustry_BubbleGenerator(tile);
00882     break;
00883 
00884   case GFX_TOFFEE_QUARY:
00885     AddAnimatedTile(tile);
00886     break;
00887 
00888   case GFX_SUGAR_MINE_SIEVE:
00889     if (Chance16(1, 3)) AddAnimatedTile(tile);
00890     break;
00891   }
00892 }
00893 
00894 static bool ClickTile_Industry(TileIndex tile)
00895 {
00896   ShowIndustryViewWindow(GetIndustryIndex(tile));
00897   return true;
00898 }
00899 
00900 static TrackStatus GetTileTrackStatus_Industry(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
00901 {
00902   return 0;
00903 }
00904 
00905 static void ChangeTileOwner_Industry(TileIndex tile, Owner old_owner, Owner new_owner)
00906 {
00907   /* If the founder merges, the industry was created by the merged company */
00908   Industry *i = Industry::GetByTile(tile);
00909   if (i->founder == old_owner) i->founder = (new_owner == INVALID_OWNER) ? OWNER_NONE : new_owner;
00910 }
00911 
00912 static const byte _plantfarmfield_type[] = {1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6};
00913 
00914 static bool IsBadFarmFieldTile(TileIndex tile)
00915 {
00916   switch (GetTileType(tile)) {
00917     case MP_CLEAR: return IsClearGround(tile, CLEAR_FIELDS) || IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
00918     case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
00919     default:       return true;
00920   }
00921 }
00922 
00923 static bool IsBadFarmFieldTile2(TileIndex tile)
00924 {
00925   switch (GetTileType(tile)) {
00926     case MP_CLEAR: return IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
00927     case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
00928     default:       return true;
00929   }
00930 }
00931 
00932 static void SetupFarmFieldFence(TileIndex tile, int size, byte type, Axis direction)
00933 {
00934   do {
00935     tile = TILE_MASK(tile);
00936 
00937     if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) {
00938       byte or_ = type;
00939 
00940       if (or_ == 1 && Chance16(1, 7)) or_ = 2;
00941 
00942       if (direction == AXIS_X) {
00943         SetFenceSE(tile, or_);
00944       } else {
00945         SetFenceSW(tile, or_);
00946       }
00947     }
00948 
00949     tile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
00950   } while (--size);
00951 }
00952 
00953 static void PlantFarmField(TileIndex tile, IndustryID industry)
00954 {
00955   if (_settings_game.game_creation.landscape == LT_ARCTIC) {
00956     if (GetTileZ(tile) + TILE_HEIGHT * 2 >= GetSnowLine()) return;
00957   }
00958 
00959   /* determine field size */
00960   uint32 r = (Random() & 0x303) + 0x404;
00961   if (_settings_game.game_creation.landscape == LT_ARCTIC) r += 0x404;
00962   uint size_x = GB(r, 0, 8);
00963   uint size_y = GB(r, 8, 8);
00964 
00965   TileArea ta(tile - TileDiffXY(min(TileX(tile), size_x / 2), min(TileY(tile), size_y / 2)), size_x, size_y);
00966   ta.ClampToMap();
00967 
00968   if (ta.w == 0 || ta.h == 0) return;
00969 
00970   /* check the amount of bad tiles */
00971   int count = 0;
00972   TILE_AREA_LOOP(cur_tile, ta) {
00973     assert(cur_tile < MapSize());
00974     count += IsBadFarmFieldTile(cur_tile);
00975   }
00976   if (count * 2 >= ta.w * ta.h) return;
00977 
00978   /* determine type of field */
00979   r = Random();
00980   uint counter = GB(r, 5, 3);
00981   uint field_type = GB(r, 8, 8) * 9 >> 8;
00982 
00983   /* make field */
00984   TILE_AREA_LOOP(cur_tile, ta) {
00985     assert(cur_tile < MapSize());
00986     if (!IsBadFarmFieldTile2(cur_tile)) {
00987       MakeField(cur_tile, field_type, industry);
00988       SetClearCounter(cur_tile, counter);
00989       MarkTileDirtyByTile(cur_tile);
00990     }
00991   }
00992 
00993   int type = 3;
00994   if (_settings_game.game_creation.landscape != LT_ARCTIC && _settings_game.game_creation.landscape != LT_TROPIC) {
00995     type = _plantfarmfield_type[Random() & 0xF];
00996   }
00997 
00998   SetupFarmFieldFence(ta.tile - TileDiffXY(1, 0), ta.h, type, AXIS_Y);
00999   SetupFarmFieldFence(ta.tile - TileDiffXY(0, 1), ta.w, type, AXIS_X);
01000   SetupFarmFieldFence(ta.tile + TileDiffXY(ta.w - 1, 0), ta.h, type, AXIS_Y);
01001   SetupFarmFieldFence(ta.tile + TileDiffXY(0, ta.h - 1), ta.w, type, AXIS_X);
01002 }
01003 
01004 void PlantRandomFarmField(const Industry *i)
01005 {
01006   int x = i->location.w / 2 + Random() % 31 - 16;
01007   int y = i->location.h / 2 + Random() % 31 - 16;
01008 
01009   TileIndex tile = TileAddWrap(i->location.tile, x, y);
01010 
01011   if (tile != INVALID_TILE) PlantFarmField(tile, i->index);
01012 }
01013 
01020 static bool SearchLumberMillTrees(TileIndex tile, void *user_data)
01021 {
01022   if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) { 
01023     /* found a tree */
01024 
01025     Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
01026 
01027     _industry_sound_ctr = 1;
01028     _industry_sound_tile = tile;
01029     SndPlayTileFx(SND_38_CHAINSAW, tile);
01030 
01031     DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
01032 
01033     cur_company.Restore();
01034     return true;
01035   }
01036   return false;
01037 }
01038 
01043 static void ChopLumberMillTrees(Industry *i)
01044 {
01045   TileIndex tile = i->location.tile;
01046 
01047   if (!IsIndustryCompleted(tile)) return;  // Can't proceed if not completed.
01048 
01049   if (CircularTileSearch(&tile, 40, SearchLumberMillTrees, NULL)) { // 40x40 tiles  to search.
01050     i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + 45); // Found a tree, add according value to waiting cargo.
01051   }
01052 }
01053 
01054 static void ProduceIndustryGoods(Industry *i)
01055 {
01056   const IndustrySpec *indsp = GetIndustrySpec(i->type);
01057 
01058   /* play a sound? */
01059   if ((i->counter & 0x3F) == 0) {
01060     uint32 r;
01061     uint num;
01062     if (Chance16R(1, 14, r) && (num = indsp->number_of_sounds) != 0) {
01063       SndPlayTileFx(
01064         (SoundFx)(indsp->random_sounds[((r >> 16) * num) >> 16]),
01065         i->location.tile);
01066     }
01067   }
01068 
01069   i->counter--;
01070 
01071   /* produce some cargo */
01072   if ((i->counter % INDUSTRY_PRODUCE_TICKS) == 0) {
01073     if (HasBit(indsp->callback_mask, CBM_IND_PRODUCTION_256_TICKS)) IndustryProductionCallback(i, 1);
01074 
01075     IndustryBehaviour indbehav = indsp->behaviour;
01076     i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + i->production_rate[0]);
01077     i->produced_cargo_waiting[1] = min(0xffff, i->produced_cargo_waiting[1] + i->production_rate[1]);
01078 
01079     if ((indbehav & INDUSTRYBEH_PLANT_FIELDS) != 0) {
01080       bool plant;
01081       if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
01082         plant = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, Random(), 0, i, i->type, i->location.tile) != 0);
01083       } else {
01084         plant = Chance16(1, 8);
01085       }
01086 
01087       if (plant) PlantRandomFarmField(i);
01088     }
01089     if ((indbehav & INDUSTRYBEH_CUT_TREES) != 0) {
01090       bool cut = ((i->counter % INDUSTRY_CUT_TREE_TICKS) == 0);
01091       if (HasBit(indsp->callback_mask, CBM_IND_SPECIAL_EFFECT)) {
01092         cut = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, 0, 1, i, i->type, i->location.tile) != 0);
01093       }
01094 
01095       if (cut) ChopLumberMillTrees(i);
01096     }
01097 
01098     TriggerIndustry(i, INDUSTRY_TRIGGER_INDUSTRY_TICK);
01099     StartStopIndustryTileAnimation(i, IAT_INDUSTRY_TICK);
01100   }
01101 }
01102 
01103 void OnTick_Industry()
01104 {
01105   if (_industry_sound_ctr != 0) {
01106     _industry_sound_ctr++;
01107 
01108     if (_industry_sound_ctr == 75) {
01109       SndPlayTileFx(SND_37_BALLOON_SQUEAK, _industry_sound_tile);
01110     } else if (_industry_sound_ctr == 160) {
01111       _industry_sound_ctr = 0;
01112       SndPlayTileFx(SND_36_CARTOON_CRASH, _industry_sound_tile);
01113     }
01114   }
01115 
01116   if (_game_mode == GM_EDITOR) return;
01117 
01118   Industry *i;
01119   FOR_ALL_INDUSTRIES(i) {
01120     ProduceIndustryGoods(i);
01121   }
01122 }
01123 
01129 static CommandCost CheckNewIndustry_NULL(TileIndex tile)
01130 {
01131   return CommandCost();
01132 }
01133 
01139 static CommandCost CheckNewIndustry_Forest(TileIndex tile)
01140 {
01141   if (_settings_game.game_creation.landscape == LT_ARCTIC) {
01142     if (GetTileZ(tile) < HighestSnowLine() + TILE_HEIGHT * 2U) {
01143       return_cmd_error(STR_ERROR_FOREST_CAN_ONLY_BE_PLANTED);
01144     }
01145   }
01146   return CommandCost();
01147 }
01148 
01154 static CommandCost CheckNewIndustry_OilRefinery(TileIndex tile)
01155 {
01156   if (_game_mode == GM_EDITOR) return CommandCost();
01157   if (DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return CommandCost();
01158 
01159   return_cmd_error(STR_ERROR_CAN_ONLY_BE_POSITIONED);
01160 }
01161 
01162 extern bool _ignore_restrictions;
01163 
01169 static CommandCost CheckNewIndustry_OilRig(TileIndex tile)
01170 {
01171   if (_game_mode == GM_EDITOR && _ignore_restrictions) return CommandCost();
01172   if (TileHeight(tile) == 0 &&
01173       DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return CommandCost();
01174 
01175   return_cmd_error(STR_ERROR_CAN_ONLY_BE_POSITIONED);
01176 }
01177 
01183 static CommandCost CheckNewIndustry_Farm(TileIndex tile)
01184 {
01185   if (_settings_game.game_creation.landscape == LT_ARCTIC) {
01186     if (GetTileZ(tile) + TILE_HEIGHT * 2 >= HighestSnowLine()) {
01187       return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01188     }
01189   }
01190   return CommandCost();
01191 }
01192 
01198 static CommandCost CheckNewIndustry_Plantation(TileIndex tile)
01199 {
01200   if (GetTropicZone(tile) == TROPICZONE_DESERT) {
01201     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01202   }
01203   return CommandCost();
01204 }
01205 
01211 static CommandCost CheckNewIndustry_Water(TileIndex tile)
01212 {
01213   if (GetTropicZone(tile) != TROPICZONE_DESERT) {
01214     return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_DESERT);
01215   }
01216   return CommandCost();
01217 }
01218 
01224 static CommandCost CheckNewIndustry_Lumbermill(TileIndex tile)
01225 {
01226   if (GetTropicZone(tile) != TROPICZONE_RAINFOREST) {
01227     return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_RAINFOREST);
01228   }
01229   return CommandCost();
01230 }
01231 
01237 static CommandCost CheckNewIndustry_BubbleGen(TileIndex tile)
01238 {
01239   if (GetTileZ(tile) > TILE_HEIGHT * 4) {
01240     return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_LOW_AREAS);
01241   }
01242   return CommandCost();
01243 }
01244 
01250 typedef CommandCost CheckNewIndustryProc(TileIndex tile);
01251 
01253 static CheckNewIndustryProc * const _check_new_industry_procs[CHECK_END] = {
01254   CheckNewIndustry_NULL,        
01255   CheckNewIndustry_Forest,      
01256   CheckNewIndustry_OilRefinery, 
01257   CheckNewIndustry_Farm,        
01258   CheckNewIndustry_Plantation,  
01259   CheckNewIndustry_Water,       
01260   CheckNewIndustry_Lumbermill,  
01261   CheckNewIndustry_BubbleGen,   
01262   CheckNewIndustry_OilRig,      
01263 };
01264 
01275 static CommandCost FindTownForIndustry(TileIndex tile, int type, const Town **t)
01276 {
01277   *t = ClosestTownFromTile(tile, UINT_MAX);
01278 
01279   if (_settings_game.economy.multiple_industry_per_town) return CommandCost();
01280 
01281   const Industry *i;
01282   FOR_ALL_INDUSTRIES(i) {
01283     if (i->type == (byte)type && i->town == *t) {
01284       *t = NULL;
01285       return_cmd_error(STR_ERROR_ONLY_ONE_ALLOWED_PER_TOWN);
01286     }
01287   }
01288 
01289   return CommandCost();
01290 }
01291 
01292 bool IsSlopeRefused(Slope current, Slope refused)
01293 {
01294   if (IsSteepSlope(current)) return true;
01295   if (current != SLOPE_FLAT) {
01296     if (IsSteepSlope(refused)) return true;
01297 
01298     Slope t = ComplementSlope(current);
01299 
01300     if ((refused & SLOPE_W) && (t & SLOPE_NW)) return true;
01301     if ((refused & SLOPE_S) && (t & SLOPE_NE)) return true;
01302     if ((refused & SLOPE_E) && (t & SLOPE_SW)) return true;
01303     if ((refused & SLOPE_N) && (t & SLOPE_SE)) return true;
01304   }
01305 
01306   return false;
01307 }
01308 
01321 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)
01322 {
01323   bool refused_slope = false;
01324   bool custom_shape = false;
01325 
01326   do {
01327     IndustryGfx gfx = GetTranslatedIndustryTileID(it->gfx);
01328     TileIndex cur_tile = TileAddWrap(tile, it->ti.x, it->ti.y);
01329 
01330     if (!IsValidTile(cur_tile)) {
01331       return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01332     }
01333 
01334     if (gfx == GFX_WATERTILE_SPECIALCHECK) {
01335       if (!IsTileType(cur_tile, MP_WATER) ||
01336           GetTileSlope(cur_tile, NULL) != SLOPE_FLAT) {
01337         return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01338       }
01339     } else {
01340       CommandCost ret = EnsureNoVehicleOnGround(cur_tile);
01341       if (ret.Failed()) return ret;
01342       if (MayHaveBridgeAbove(cur_tile) && IsBridgeAbove(cur_tile)) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01343 
01344       const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
01345 
01346       IndustryBehaviour ind_behav = GetIndustrySpec(type)->behaviour;
01347 
01348       /* Perform land/water check if not disabled */
01349       if (!HasBit(its->slopes_refused, 5) && ((HasTileWaterClass(cur_tile) && IsTileOnWater(cur_tile)) == !(ind_behav & INDUSTRYBEH_BUILT_ONWATER))) return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01350 
01351       if (HasBit(its->callback_mask, CBM_INDT_SHAPE_CHECK)) {
01352         custom_shape = true;
01353         CommandCost ret = PerformIndustryTileSlopeCheck(tile, cur_tile, its, type, gfx, itspec_index, initial_random_bits, founder, creation_type);
01354         if (ret.Failed()) return ret;
01355       } else {
01356         Slope tileh = GetTileSlope(cur_tile, NULL);
01357         refused_slope |= IsSlopeRefused(tileh, its->slopes_refused);
01358       }
01359 
01360       if ((ind_behav & (INDUSTRYBEH_ONLY_INTOWN | INDUSTRYBEH_TOWN1200_MORE)) || // Tile must be a house
01361           ((ind_behav & INDUSTRYBEH_ONLY_NEARTOWN) && IsTileType(cur_tile, MP_HOUSE))) { // Tile is allowed to be a house (and it is a house)
01362         if (!IsTileType(cur_tile, MP_HOUSE)) {
01363           return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS);
01364         }
01365 
01366         /* Clear the tiles as OWNER_TOWN to not affect town rating, and to not clear protected buildings */
01367         Backup<CompanyByte> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
01368         CommandCost ret = DoCommand(cur_tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR);
01369         cur_company.Restore();
01370 
01371         if (ret.Failed()) return ret;
01372       } else {
01373         /* Clear the tiles, but do not affect town ratings */
01374         CommandCost ret = DoCommand(cur_tile, 0, 0, DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
01375 
01376         if (ret.Failed()) return ret;
01377       }
01378     }
01379   } while ((++it)->ti.x != -0x80);
01380 
01381   if (custom_shape_check != NULL) *custom_shape_check = custom_shape;
01382 
01383   /* It is almost impossible to have a fully flat land in TG, so what we
01384    *  do is that we check if we can make the land flat later on. See
01385    *  CheckIfCanLevelIndustryPlatform(). */
01386   if (!refused_slope || (_settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !custom_shape && !_ignore_restrictions)) {
01387     return CommandCost();
01388   }
01389   return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01390 }
01391 
01399 static CommandCost CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t)
01400 {
01401   if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_TOWN1200_MORE) && t->population < 1200) {
01402     return_cmd_error(STR_ERROR_CAN_ONLY_BE_BUILT_IN_TOWNS_WITH_POPULATION_OF_1200);
01403   }
01404 
01405   if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_ONLY_NEARTOWN) && DistanceMax(t->xy, tile) > 9) {
01406     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01407   }
01408 
01409   return CommandCost();
01410 }
01411 
01412 static bool CheckCanTerraformSurroundingTiles(TileIndex tile, uint height, int internal)
01413 {
01414   /* Max. allowed delta when levelling land.
01415    * @see comment in industry_type.h for more information why we need this. */
01416   uint max_delta;
01417 
01418   if (_settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world) {
01419     if (_settings_game.game_creation.tgen_smoothness == SMOOTHNESS_VERY_ROUGH) {
01420       max_delta = IND_MAX_LEVELLING_VERY_ROUGH;
01421     } else if (_settings_game.game_creation.tgen_smoothness == SMOOTHNESS_CEREALLY_ROUGH) {
01422       max_delta = IND_MAX_LEVELLING_CEREALLY_ROUGH;
01423     } else {
01424       max_delta = IND_MAX_LEVELLING_NORMAL;
01425     }
01426   } else {
01427     max_delta = IND_MAX_LEVELLING_NORMAL;
01428   }
01429 
01430   /* Check if we don't leave the map */
01431   if (TileX(tile) == 0 || TileY(tile) == 0 || GetTileType(tile) == MP_VOID) return false;
01432 
01433   TileArea ta(tile - TileDiffXY(1, 1), 2, 2);
01434   TILE_AREA_LOOP(tile_walk, ta) {
01435     uint curh = TileHeight(tile_walk);
01436     /* Is the tile clear? */
01437     if ((GetTileType(tile_walk) != MP_CLEAR) && (GetTileType(tile_walk) != MP_TREES)) return false;
01438 
01439     /* Don't allow too big of a change if this is the sub-tile check */
01440     uint8 IND_LEVELLING_MAX_HEIGHT  = _settings_game.construction.extra_industry_placement_logic; 
01441     if (internal != 0 && ((height < IND_LEVELLING_MAX_HEIGHT && Delta(curh, height) > max_delta)
01442         ||  (height >= IND_LEVELLING_MAX_HEIGHT && Delta(curh, height) > IND_MAX_LEVELLING_NORMAL))) {
01443       return false;
01444     }
01445 
01446     /* Different height, so the surrounding tiles of this tile
01447      *  has to be correct too (in level, or almost in level)
01448      *  else you get a chain-reaction of terraforming. */
01449     if (internal == 0 && curh != height) {
01450       if (TileX(tile_walk) == 0 || TileY(tile_walk) == 0 || !CheckCanTerraformSurroundingTiles(tile_walk + TileDiffXY(-1, -1), height, internal + 1)) {
01451         return false;
01452       }
01453     }
01454   }
01455 
01456   return true;
01457 }
01458 
01463 static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlag flags, const IndustryTileTable *it, int type)
01464 {
01465   const int MKEND = -0x80;   // used for last element in an IndustryTileTable (see build_industry.h)
01466   int max_x = 0;
01467   int max_y = 0;
01468 
01469   /* Finds dimensions of largest variant of this industry */
01470   do {
01471     if (it->gfx == 0xFF) continue;  //  FF been a marquer for a check on clear water, skip it
01472     if (it->ti.x > max_x) max_x = it->ti.x;
01473     if (it->ti.y > max_y) max_y = it->ti.y;
01474   } while ((++it)->ti.x != MKEND);
01475 
01476   /* Remember level height */
01477   uint h = TileHeight(tile);
01478 
01479   if (TileX(tile) <= _settings_game.construction.industry_platform + 1U || TileY(tile) <= _settings_game.construction.industry_platform + 1U) return false;
01480   /* Check that all tiles in area and surrounding are clear
01481    * this determines that there are no obstructing items */
01482 
01483   TileArea ta(tile + TileDiffXY(-_settings_game.construction.industry_platform, -_settings_game.construction.industry_platform),
01484       max_x + 2 + 2 * _settings_game.construction.industry_platform, max_y + 2 + 2 * _settings_game.construction.industry_platform);
01485 
01486   if (TileX(ta.tile) + ta.w >= MapMaxX() || TileY(ta.tile) + ta.h >= MapMaxY()) return false;
01487 
01488   /* _current_company is OWNER_NONE for randomly generated industries and in editor, or the company who funded or prospected the industry.
01489    * Perform terraforming as OWNER_TOWN to disable autoslope and town ratings. */
01490   Backup<CompanyByte> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
01491 
01492   TILE_AREA_LOOP(tile_walk, ta) {
01493     uint curh = TileHeight(tile_walk);
01494     if (curh != h) {
01495       /* This tile needs terraforming. Check if we can do that without
01496        *  damaging the surroundings too much. */
01497       if (!CheckCanTerraformSurroundingTiles(tile_walk, h, 0)) {
01498         cur_company.Restore();
01499         return false;
01500       }
01501       /* This is not 100% correct check, but the best we can do without modifying the map.
01502        *  What is missing, is if the difference in height is more than 1.. */
01503       if (DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND).Failed()) {
01504         cur_company.Restore();
01505         return false;
01506       }
01507     }
01508   }
01509 
01510   if (flags & DC_EXEC) {
01511     /* Terraform the land under the industry */
01512     TILE_AREA_LOOP(tile_walk, ta) {
01513       uint curh = TileHeight(tile_walk);
01514       while (curh != h) {
01515         /* We give the terraforming for free here, because we can't calculate
01516          *  exact cost in the test-round, and as we all know, that will cause
01517          *  a nice assert if they don't match ;) */
01518         DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
01519         curh += (curh > h) ? -1 : 1;
01520       }
01521     }
01522   }
01523 
01524   cur_company.Restore();
01525   return true;
01526 }
01527 
01528 
01535 static CommandCost CheckIfFarEnoughFromConflictingIndustry(TileIndex tile, int type)
01536 {
01537   const IndustrySpec *indspec = GetIndustrySpec(type);
01538   const Industry *i;
01539   FOR_ALL_INDUSTRIES(i) {
01540     /* Within 14 tiles from another industry is considered close */
01541     if (DistanceMax(tile, i->location.tile) > 14) continue;
01542 
01543     /* check if there are any conflicting industry types around */
01544     if (i->type == indspec->conflicting[0] ||
01545         i->type == indspec->conflicting[1] ||
01546         i->type == indspec->conflicting[2]) {
01547       return_cmd_error(STR_ERROR_INDUSTRY_TOO_CLOSE);
01548     }
01549   }
01550   return CommandCost();
01551 }
01552 
01564 static void DoCreateNewIndustry(Industry *i, TileIndex tile, IndustryType type, const IndustryTileTable *it, byte layout, const Town *t, Owner founder, uint16 initial_random_bits)
01565 {
01566   const IndustrySpec *indspec = GetIndustrySpec(type);
01567 
01568   i->location = TileArea(tile, 1, 1);
01569   i->type = type;
01570   Industry::IncIndustryTypeCount(type);
01571 
01572   i->produced_cargo[0] = indspec->produced_cargo[0];
01573   i->produced_cargo[1] = indspec->produced_cargo[1];
01574   i->accepts_cargo[0] = indspec->accepts_cargo[0];
01575   i->accepts_cargo[1] = indspec->accepts_cargo[1];
01576   i->accepts_cargo[2] = indspec->accepts_cargo[2];
01577   i->production_rate[0] = indspec->production_rate[0];
01578   i->production_rate[1] = indspec->production_rate[1];
01579 
01580   /* don't use smooth economy for industries using production related callbacks */
01581   if (indspec->UsesSmoothEconomy()) {
01582     i->production_rate[0] = min((RandomRange(256) + 128) * i->production_rate[0] >> 8, 255);
01583     i->production_rate[1] = min((RandomRange(256) + 128) * i->production_rate[1] >> 8, 255);
01584   }
01585 
01586   i->town = t;
01587   i->owner = OWNER_NONE;
01588 
01589   uint16 r = Random();
01590   i->random_colour = GB(r, 0, 4);
01591   i->counter = GB(r, 4, 12);
01592   i->random = initial_random_bits;
01593   i->produced_cargo_waiting[0] = 0;
01594   i->produced_cargo_waiting[1] = 0;
01595   i->incoming_cargo_waiting[0] = 0;
01596   i->incoming_cargo_waiting[1] = 0;
01597   i->incoming_cargo_waiting[2] = 0;
01598   i->this_month_production[0] = 0;
01599   i->this_month_production[1] = 0;
01600   i->this_month_transported[0] = 0;
01601   i->this_month_transported[1] = 0;
01602   i->last_month_pct_transported[0] = 0;
01603   i->last_month_pct_transported[1] = 0;
01604   i->last_month_transported[0] = 0;
01605   i->last_month_transported[1] = 0;
01606   i->was_cargo_delivered = false;
01607   i->last_prod_year = _cur_year;
01608   i->last_month_production[0] = i->production_rate[0] * 8;
01609   i->last_month_production[1] = i->production_rate[1] * 8;
01610   i->founder = founder;
01611 
01612   i->construction_date = _date;
01613   i->construction_type = (_game_mode == GM_EDITOR) ? ICT_SCENARIO_EDITOR :
01614       (_generating_world ? ICT_MAP_GENERATION : ICT_NORMAL_GAMEPLAY);
01615 
01616   /* Adding 1 here makes it conform to specs of var44 of varaction2 for industries
01617    * 0 = created prior of newindustries
01618    * else, chosen layout + 1 */
01619   i->selected_layout = layout + 1;
01620 
01621   if (!_generating_world) i->last_month_production[0] = i->last_month_production[1] = 0;
01622 
01623   i->prod_level = PRODLEVEL_DEFAULT;
01624 
01625   /* Call callbacks after the regular fields got initialised. */
01626 
01627   if (HasBit(indspec->callback_mask, CBM_IND_DECIDE_COLOUR)) {
01628     uint16 res = GetIndustryCallback(CBID_INDUSTRY_DECIDE_COLOUR, 0, 0, i, type, INVALID_TILE);
01629     if (res != CALLBACK_FAILED) i->random_colour = GB(res, 0, 4);
01630   }
01631 
01632   if (HasBit(indspec->callback_mask, CBM_IND_INPUT_CARGO_TYPES)) {
01633     for (uint j = 0; j < lengthof(i->accepts_cargo); j++) i->accepts_cargo[j] = CT_INVALID;
01634     for (uint j = 0; j < lengthof(i->accepts_cargo); j++) {
01635       uint16 res = GetIndustryCallback(CBID_INDUSTRY_INPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
01636       if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
01637       i->accepts_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
01638     }
01639   }
01640 
01641   if (HasBit(indspec->callback_mask, CBM_IND_OUTPUT_CARGO_TYPES)) {
01642     for (uint j = 0; j < lengthof(i->produced_cargo); j++) i->produced_cargo[j] = CT_INVALID;
01643     for (uint j = 0; j < lengthof(i->produced_cargo); j++) {
01644       uint16 res = GetIndustryCallback(CBID_INDUSTRY_OUTPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
01645       if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
01646       i->produced_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
01647     }
01648   }
01649 
01650   /* Plant the tiles */
01651 
01652   do {
01653     TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
01654 
01655     if (it->gfx != GFX_WATERTILE_SPECIALCHECK) {
01656       i->location.Add(cur_tile);
01657 
01658       WaterClass wc = (IsWaterTile(cur_tile) ? GetWaterClass(cur_tile) : WATER_CLASS_INVALID);
01659 
01660       DoCommand(cur_tile, 0, 0, DC_EXEC | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
01661 
01662       MakeIndustry(cur_tile, i->index, it->gfx, Random(), wc);
01663 
01664       if (_generating_world) {
01665         SetIndustryConstructionCounter(cur_tile, 3);
01666         SetIndustryConstructionStage(cur_tile, 2);
01667       }
01668 
01669       /* it->gfx is stored in the map. But the translated ID cur_gfx is the interesting one */
01670       IndustryGfx cur_gfx = GetTranslatedIndustryTileID(it->gfx);
01671       const IndustryTileSpec *its = GetIndustryTileSpec(cur_gfx);
01672       if (its->animation.status != ANIM_STATUS_NO_ANIMATION) AddAnimatedTile(cur_tile);
01673     }
01674   } while ((++it)->ti.x != -0x80);
01675 
01676   if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
01677     for (uint j = 0; j != 50; j++) PlantRandomFarmField(i);
01678   }
01679   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
01680 
01681   Station::RecomputeIndustriesNearForAll();
01682 }
01683 
01700 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)
01701 {
01702   assert(itspec_index < indspec->num_table);
01703   const IndustryTileTable *it = indspec->table[itspec_index];
01704   bool custom_shape_check = false;
01705 
01706   *ip = NULL;
01707 
01708   SmallVector<ClearedObjectArea, 1> object_areas(_cleared_object_areas);
01709   CommandCost ret = CheckIfIndustryTilesAreFree(tile, it, itspec_index, type, random_initial_bits, founder, creation_type, &custom_shape_check);
01710   _cleared_object_areas = object_areas;
01711   if (ret.Failed()) return ret;
01712 
01713   if (HasBit(GetIndustrySpec(type)->callback_mask, CBM_IND_LOCATION)) {
01714     ret = CheckIfCallBackAllowsCreation(tile, type, itspec_index, random_var8f, random_initial_bits, founder, creation_type);
01715   } else {
01716     ret = _check_new_industry_procs[indspec->check_proc](tile);
01717   }
01718   if (ret.Failed()) return ret;
01719 
01720   if (!custom_shape_check && _settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world &&
01721       !_ignore_restrictions && !CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER, it, type)) {
01722     return_cmd_error(STR_ERROR_SITE_UNSUITABLE);
01723   }
01724 
01725   ret = CheckIfFarEnoughFromConflictingIndustry(tile, type);
01726   if (ret.Failed()) return ret;
01727 
01728   const Town *t = NULL;
01729   ret = FindTownForIndustry(tile, type, &t);
01730   if (ret.Failed()) return ret;
01731   assert(t != NULL);
01732 
01733   ret = CheckIfIndustryIsAllowed(tile, type, t);
01734   if (ret.Failed()) return ret;
01735 
01736   if (!Industry::CanAllocateItem()) return_cmd_error(STR_ERROR_TOO_MANY_INDUSTRIES);
01737 
01738   if (flags & DC_EXEC) {
01739     *ip = new Industry(tile);
01740     if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_NO_WATER | DC_EXEC, it, type);
01741     DoCreateNewIndustry(*ip, tile, type, it, itspec_index, t, founder, random_initial_bits);
01742   }
01743 
01744   return CommandCost();
01745 }
01746 
01758 CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
01759 {
01760   IndustryType it = GB(p1, 0, 8);
01761   if (it >= NUM_INDUSTRYTYPES) return CMD_ERROR;
01762 
01763   const IndustrySpec *indspec = GetIndustrySpec(it);
01764 
01765   /* Check if the to-be built/founded industry is available for this climate. */
01766   if (!indspec->enabled || indspec->num_table == 0) return CMD_ERROR;
01767 
01768   /* If the setting for raw-material industries is not on, you cannot build raw-material industries.
01769    * Raw material industries are industries that do not accept cargo (at least for now) */
01770   if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
01771     return CMD_ERROR;
01772   }
01773 
01774   if (_game_mode != GM_EDITOR && !CheckIfCallBackAllowsAvailability(it, IACT_USERCREATION)) {
01775     return CMD_ERROR;
01776   }
01777 
01778   Randomizer randomizer;
01779   randomizer.SetSeed(p2);
01780   uint16 random_initial_bits = GB(p2, 0, 16);
01781   uint32 random_var8f = randomizer.Next();
01782   int num_layouts = indspec->num_table;
01783   CommandCost ret = CommandCost(STR_ERROR_SITE_UNSUITABLE);
01784 
01785   Industry *ind = NULL;
01786   if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indspec->IsRawIndustry()) {
01787     if (flags & DC_EXEC) {
01788       /* Prospected industries are build as OWNER_TOWN to not e.g. be build on owned land of the founder */
01789       Backup<CompanyByte> cur_company(_current_company, OWNER_TOWN, FILE_LINE);
01790       /* Prospecting has a chance to fail, however we cannot guarantee that something can
01791        * be built on the map, so the chance gets lower when the map is fuller, but there
01792        * is nothing we can really do about that. */
01793       if (Random() <= indspec->prospecting_chance) {
01794         for (int i = 0; i < 5000; i++) {
01795           /* We should not have more than one Random() in a function call
01796            * because parameter evaluation order is not guaranteed in the c++ standard
01797            */
01798           tile = RandomTile();
01799           /* Start with a random layout */
01800           int layout = RandomRange(num_layouts);
01801           /* Check now each layout, starting with the random one */
01802           for (int j = 0; j < num_layouts; j++) {
01803             layout = (layout + 1) % num_layouts;
01804             ret = CreateNewIndustryHelper(tile, it, flags, indspec, layout, random_var8f, random_initial_bits, cur_company.GetOriginalValue(), IACT_PROSPECTCREATION, &ind);
01805             if (ret.Succeeded()) break;
01806           }
01807           if (ret.Succeeded()) break;
01808         }
01809       }
01810       cur_company.Restore();
01811     }
01812   } else {
01813     int layout = GB(p1, 8, 8);
01814     if (layout >= num_layouts) return CMD_ERROR;
01815 
01816     /* Check subsequently each layout, starting with the given layout in p1 */
01817     for (int i = 0; i < num_layouts; i++) {
01818       layout = (layout + 1) % num_layouts;
01819       ret = CreateNewIndustryHelper(tile, it, flags, indspec, layout, random_var8f, random_initial_bits, _current_company, IACT_USERCREATION, &ind);
01820       if (ret.Succeeded()) break;
01821     }
01822 
01823     /* If it still failed, there's no suitable layout to build here, return the error */
01824     if (ret.Failed()) return ret;
01825   }
01826 
01827   if ((flags & DC_EXEC) && ind != NULL && _game_mode != GM_EDITOR) {
01828     /* Created a new industry in-game, advertise the event. */
01829     SetDParam(0, indspec->name);
01830     if (indspec->new_industry_text > STR_LAST_STRINGID) {
01831       SetDParam(1, STR_TOWN_NAME);
01832       SetDParam(2, ind->town->index);
01833     } else {
01834       SetDParam(1, ind->town->index);
01835     }
01836     AddIndustryNewsItem(indspec->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
01837     AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
01838   }
01839 
01840   return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost());
01841 }
01842 
01843 
01851 static Industry *CreateNewIndustry(TileIndex tile, IndustryType type, IndustryAvailabilityCallType creation_type)
01852 {
01853   const IndustrySpec *indspec = GetIndustrySpec(type);
01854 
01855   uint32 seed = Random();
01856   uint32 seed2 = Random();
01857   Industry *i = NULL;
01858   CommandCost ret = CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, RandomRange(indspec->num_table), seed, GB(seed2, 0, 16), OWNER_NONE, creation_type, &i);
01859   assert(i != NULL || ret.Failed());
01860   return i;
01861 }
01862 
01869 static uint32 GetScaledIndustryGenerationProbability(IndustryType it, bool *force_at_least_one)
01870 {
01871   const IndustrySpec *ind_spc = GetIndustrySpec(it);
01872   uint32 chance = ind_spc->appear_creation[_settings_game.game_creation.landscape] * 16; // * 16 to increase precision
01873   if (!ind_spc->enabled || chance == 0 || ind_spc->num_table == 0 ||
01874       !CheckIfCallBackAllowsAvailability(it, IACT_MAPGENERATION) ||
01875       (_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY)) {
01876     *force_at_least_one = false;
01877     return 0;
01878   } else {
01879     /* We want industries appearing at coast to appear less often on bigger maps, as length of coast increases slower than map area.
01880      * For simplicity we scale in both cases, though scaling the probabilities of all industries has no effect. */
01881     chance = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(chance) : ScaleByMapSize(chance);
01882 
01883     *force_at_least_one = (chance > 0) && !(ind_spc->behaviour & INDUSTRYBEH_NOBUILT_MAPCREATION) && (_game_mode != GM_EDITOR);
01884     return chance;
01885   }
01886 }
01887 
01894 static uint16 GetIndustryGamePlayProbability(IndustryType it, byte *min_number)
01895 {
01896   if (_settings_game.difficulty.industry_density == ID_FUND_ONLY) {
01897     *min_number = 0;
01898     return 0;
01899   }
01900 
01901   const IndustrySpec *ind_spc = GetIndustrySpec(it);
01902   byte chance = ind_spc->appear_ingame[_settings_game.game_creation.landscape];
01903   if (!ind_spc->enabled || chance == 0 || ind_spc->num_table == 0 ||
01904       ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) ||
01905       ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) ||
01906       !CheckIfCallBackAllowsAvailability(it, IACT_RANDOMCREATION)) {
01907     *min_number = 0;
01908     return 0;
01909   }
01910   *min_number = (ind_spc->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) ? 1 : 0;
01911   return chance;
01912 }
01913 
01918 static uint GetNumberOfIndustries()
01919 {
01920   /* Number of industries on a 256x256 map. */
01921   static const uint16 numof_industry_table[] = {
01922     0,    // none
01923     0,    // minimal
01924     10,   // very low
01925     25,   // low
01926     55,   // normal
01927     80,   // high
01928   };
01929 
01930   assert(lengthof(numof_industry_table) == ID_END);
01931   uint difficulty = (_game_mode != GM_EDITOR) ? _settings_game.difficulty.industry_density : (uint)ID_VERY_LOW;
01932   return ScaleByMapSize(numof_industry_table[difficulty]);
01933 }
01934 
01939 static void AdvertiseIndustryOpening(const Industry *ind)
01940 {
01941   const IndustrySpec *ind_spc = GetIndustrySpec(ind->type);
01942   SetDParam(0, ind_spc->name);
01943   if (ind_spc->new_industry_text > STR_LAST_STRINGID) {
01944     SetDParam(1, STR_TOWN_NAME);
01945     SetDParam(2, ind->town->index);
01946   } else {
01947     SetDParam(1, ind->town->index);
01948   }
01949   AddIndustryNewsItem(ind_spc->new_industry_text, NS_INDUSTRY_OPEN, ind->index);
01950   AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
01951 }
01952 
01961 static Industry *PlaceIndustry(IndustryType type, IndustryAvailabilityCallType creation_type, bool try_hard)
01962 {
01963   uint tries = try_hard ? 10000u : 2000u;
01964   for (; tries > 0; tries--) {
01965     Industry *ind = CreateNewIndustry(RandomTile(), type, creation_type);
01966     if (ind != NULL) return ind;
01967   }
01968   return NULL;
01969 }
01970 
01976 static void PlaceInitialIndustry(IndustryType type, bool try_hard)
01977 {
01978   Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
01979 
01980   IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
01981   PlaceIndustry(type, IACT_MAPGENERATION, try_hard);
01982 
01983   cur_company.Restore();
01984 }
01985 
01990 static uint GetCurrentTotalNumberOfIndustries()
01991 {
01992   int total = 0;
01993   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) total += Industry::GetIndustryTypeCount(it);
01994   return total;
01995 }
01996 
01997 
01999 void IndustryTypeBuildData::Reset()
02000 {
02001   this->probability  = 0;
02002   this->min_number   = 0;
02003   this->target_count = 0;
02004   this->max_wait     = 1;
02005   this->wait_count   = 0;
02006 }
02007 
02009 void IndustryBuildData::Reset()
02010 {
02011   this->wanted_inds = GetCurrentTotalNumberOfIndustries() << 16;
02012 
02013   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02014     this->builddata[it].Reset();
02015   }
02016 }
02017 
02019 void IndustryBuildData::MonthlyLoop()
02020 {
02021   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.
02022   if (_settings_game.difficulty.industry_density == ID_FUND_ONLY) return; // 'no industries' setting,
02023 
02024   /* To prevent running out of unused industries for the player to connect,
02025    * add a fraction of new industries each month, but only if the manager can keep up. */
02026   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).
02027   if (GetCurrentTotalNumberOfIndustries() + max_behind >= (this->wanted_inds >> 16)) {
02028     this->wanted_inds += ScaleByMapSize(NEWINDS_PER_MONTH);
02029   }
02030 }
02031 
02036 void GenerateIndustries()
02037 {
02038   if (_game_mode != GM_EDITOR && _settings_game.difficulty.industry_density == ID_FUND_ONLY) return; // No industries in the game.
02039 
02040   uint32 industry_probs[NUM_INDUSTRYTYPES];
02041   bool force_at_least_one[NUM_INDUSTRYTYPES];
02042   uint32 total_prob = 0;
02043   uint num_forced = 0;
02044 
02045   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02046     industry_probs[it] = GetScaledIndustryGenerationProbability(it, force_at_least_one + it);
02047     total_prob += industry_probs[it];
02048     if (force_at_least_one[it]) num_forced++;
02049   }
02050 
02051   uint total_amount = GetNumberOfIndustries();
02052   if (total_prob == 0 || total_amount < num_forced) {
02053     /* Only place the forced ones */
02054     total_amount = num_forced;
02055   }
02056 
02057   SetGeneratingWorldProgress(GWP_INDUSTRY, total_amount);
02058 
02059   /* Try to build one industry per type independent of any probabilities */
02060   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02061     if (force_at_least_one[it]) {
02062       assert(total_amount > 0);
02063       total_amount--;
02064       PlaceInitialIndustry(it, true);
02065     }
02066   }
02067 
02068   /* Add the remaining industries according to their probabilities */
02069   for (uint i = 0; i < total_amount; i++) {
02070     uint32 r = RandomRange(total_prob);
02071     IndustryType it = 0;
02072     while (r >= industry_probs[it]) {
02073       r -= industry_probs[it];
02074       it++;
02075       assert(it < NUM_INDUSTRYTYPES);
02076     }
02077     assert(industry_probs[it] > 0);
02078     PlaceInitialIndustry(it, false);
02079   }
02080   _industry_builder.Reset();
02081 }
02082 
02087 static void UpdateIndustryStatistics(Industry *i)
02088 {
02089   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
02090     if (i->produced_cargo[j] != CT_INVALID) {
02091       byte pct = 0;
02092       if (i->this_month_production[j] != 0) {
02093         i->last_prod_year = _cur_year;
02094         pct = min(i->this_month_transported[j] * 256 / i->this_month_production[j], 255);
02095       }
02096       i->last_month_pct_transported[j] = pct;
02097 
02098       i->last_month_production[j] = i->this_month_production[j];
02099       i->this_month_production[j] = 0;
02100 
02101       i->last_month_transported[j] = i->this_month_transported[j];
02102       i->this_month_transported[j] = 0;
02103     }
02104   }
02105 }
02106 
02111 void Industry::RecomputeProductionMultipliers()
02112 {
02113   const IndustrySpec *indspec = GetIndustrySpec(this->type);
02114   assert(!indspec->UsesSmoothEconomy());
02115 
02116   /* Rates are rounded up, so e.g. oilrig always produces some passengers */
02117   this->production_rate[0] = min(CeilDiv(indspec->production_rate[0] * this->prod_level, PRODLEVEL_DEFAULT), 0xFF);
02118   this->production_rate[1] = min(CeilDiv(indspec->production_rate[1] * this->prod_level, PRODLEVEL_DEFAULT), 0xFF);
02119 }
02120 
02121 
02127 bool IndustryTypeBuildData::GetIndustryTypeData(IndustryType it)
02128 {
02129   byte min_number;
02130   uint32 probability = GetIndustryGamePlayProbability(it, &min_number);
02131   bool changed = min_number != this->min_number || probability != this->probability;
02132   this->min_number = min_number;
02133   this->probability = probability;
02134   return changed;
02135 }
02136 
02138 void IndustryBuildData::SetupTargetCount()
02139 {
02140   bool changed = false;
02141   uint num_planned = 0; // Number of industries planned in the industry build data.
02142   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02143     changed |= this->builddata[it].GetIndustryTypeData(it);
02144     num_planned += this->builddata[it].target_count;
02145   }
02146   uint total_amount = this->wanted_inds >> 16; // Desired total number of industries.
02147   changed |= num_planned != total_amount;
02148   if (!changed) return; // All industries are still the same, no need to re-randomize.
02149 
02150   /* Initialize the target counts. */
02151   uint force_build = 0;  // Number of industries that should always be available.
02152   uint32 total_prob = 0; // Sum of probabilities.
02153   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02154     IndustryTypeBuildData *ibd = this->builddata + it;
02155     force_build += ibd->min_number;
02156     ibd->target_count = ibd->min_number;
02157     total_prob += ibd->probability;
02158   }
02159 
02160   if (total_prob == 0) return; // No buildable industries.
02161 
02162   /* Subtract forced industries from the number of industries available for construction. */
02163   total_amount = (total_amount <= force_build) ? 0 : total_amount - force_build;
02164 
02165   /* Assign number of industries that should be aimed for, by using the probability as a weight. */
02166   while (total_amount > 0) {
02167     uint32 r = RandomRange(total_prob);
02168     IndustryType it = 0;
02169     while (r >= this->builddata[it].probability) {
02170       r -= this->builddata[it].probability;
02171       it++;
02172       assert(it < NUM_INDUSTRYTYPES);
02173     }
02174     assert(this->builddata[it].probability > 0);
02175     this->builddata[it].target_count++;
02176     total_amount--;
02177   }
02178 }
02179 
02183 void IndustryBuildData::TryBuildNewIndustry()
02184 {
02185   this->SetupTargetCount();
02186 
02187   int missing = 0;       // Number of industries that need to be build.
02188   uint count = 0;        // Number of industry types eligible for build.
02189   uint32 total_prob = 0; // Sum of probabilities.
02190   IndustryType forced_build = NUM_INDUSTRYTYPES; // Industry type that should be forcibly build.
02191   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02192     int difference = this->builddata[it].target_count - Industry::GetIndustryTypeCount(it);
02193     missing += difference;
02194     if (this->builddata[it].wait_count > 0) continue; // This type may not be built now.
02195     if (difference > 0) {
02196       if (Industry::GetIndustryTypeCount(it) == 0 && this->builddata[it].min_number > 0) {
02197         /* An industry that should exist at least once, is not available. Force it, trying the most needed one first. */
02198         if (forced_build == NUM_INDUSTRYTYPES ||
02199             difference > this->builddata[forced_build].target_count - Industry::GetIndustryTypeCount(forced_build)) {
02200           forced_build = it;
02201         }
02202       }
02203       total_prob += difference;
02204       count++;
02205     }
02206   }
02207 
02208   if (EconomyIsInRecession() || (forced_build == NUM_INDUSTRYTYPES && (missing <= 0 || total_prob == 0))) count = 0; // Skip creation of an industry.
02209 
02210   if (count >= 1) {
02211     /* If not forced, pick a weighted random industry to build.
02212      * For the case that count == 1, there is no need to draw a random number. */
02213     IndustryType it;
02214     if (forced_build != NUM_INDUSTRYTYPES) {
02215       it = forced_build;
02216     } else {
02217       /* Non-forced, select an industry type to build (weighted random). */
02218       uint32 r = 0; // Initialized to silence the compiler.
02219       if (count > 1) r = RandomRange(total_prob);
02220       for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
02221         if (this->builddata[it].wait_count > 0) continue; // Type may not be built now.
02222         int difference = this->builddata[it].target_count - Industry::GetIndustryTypeCount(it);
02223         if (difference <= 0) continue; // Too many of this kind.
02224         if (count == 1) break;
02225         if (r < (uint)difference) break;
02226         r -= difference;
02227       }
02228       assert(it < NUM_INDUSTRYTYPES && this->builddata[it].target_count > Industry::GetIndustryTypeCount(it));
02229     }
02230 
02231     /* Try to create the industry. */
02232     const Industry *ind = PlaceIndustry(it, IACT_RANDOMCREATION, false);
02233     if (ind == NULL) {
02234       this->builddata[it].wait_count = this->builddata[it].max_wait + 1; // Compensate for decrementing below.
02235       this->builddata[it].max_wait = min(1000, this->builddata[it].max_wait + 2);
02236     } else {
02237       AdvertiseIndustryOpening(ind);
02238       this->builddata[it].max_wait = max(this->builddata[it].max_wait / 2, 1); // Reduce waiting time of the industry type.
02239     }
02240   }
02241 
02242   /* Decrement wait counters. */
02243   for (IndustryType it = 0; it < NUM_INDUSTRYTYPES; it++) {
02244     if (this->builddata[it].wait_count > 0) this->builddata[it].wait_count--;
02245   }
02246 }
02247 
02256 static bool CheckIndustryCloseDownProtection(IndustryType type)
02257 {
02258   const IndustrySpec *indspec = GetIndustrySpec(type);
02259 
02260   /* oil wells (or the industries with that flag set) are always allowed to closedown */
02261   if ((indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE) return false;
02262   return (indspec->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) == 0 && Industry::GetIndustryTypeCount(type) <= 1;
02263 }
02264 
02274 static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces)
02275 {
02276   if (cargo == CT_INVALID) return;
02277 
02278   /* Check for acceptance of cargo */
02279   for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) {
02280     if (cargo == ind->accepts_cargo[j] && !IndustryTemporarilyRefusesCargo(ind, cargo)) {
02281       *c_accepts = true;
02282       break;
02283     }
02284   }
02285 
02286   /* Check for produced cargo */
02287   for (byte j = 0; j < lengthof(ind->produced_cargo); j++) {
02288     if (cargo == ind->produced_cargo[j]) {
02289       *c_produces = true;
02290       break;
02291     }
02292   }
02293 }
02294 
02308 static int WhoCanServiceIndustry(Industry *ind)
02309 {
02310   /* Find all stations within reach of the industry */
02311   StationList stations;
02312   FindStationsAroundTiles(ind->location, &stations);
02313 
02314   if (stations.Length() == 0) return 0; // No stations found at all => nobody services
02315 
02316   const Vehicle *v;
02317   int result = 0;
02318   FOR_ALL_VEHICLES(v) {
02319     /* Is it worthwhile to try this vehicle? */
02320     if (v->owner != _local_company && result != 0) continue;
02321 
02322     /* Check whether it accepts the right kind of cargo */
02323     bool c_accepts = false;
02324     bool c_produces = false;
02325     if (v->type == VEH_TRAIN && v->IsFrontEngine()) {
02326       for (const Vehicle *u = v; u != NULL; u = u->Next()) {
02327         CanCargoServiceIndustry(u->cargo_type, ind, &c_accepts, &c_produces);
02328       }
02329     } else if (v->type == VEH_ROAD || v->type == VEH_SHIP || v->type == VEH_AIRCRAFT) {
02330       CanCargoServiceIndustry(v->cargo_type, ind, &c_accepts, &c_produces);
02331     } else {
02332       continue;
02333     }
02334     if (!c_accepts && !c_produces) continue; // Wrong cargo
02335 
02336     /* Check orders of the vehicle.
02337      * We cannot check the first of shared orders only, since the first vehicle in such a chain
02338      * may have a different cargo type.
02339      */
02340     const Order *o;
02341     FOR_VEHICLE_ORDERS(v, o) {
02342       if (o->IsType(OT_GOTO_STATION) && !(o->GetUnloadType() & OUFB_TRANSFER)) {
02343         /* Vehicle visits a station to load or unload */
02344         Station *st = Station::Get(o->GetDestination());
02345         assert(st != NULL);
02346 
02347         /* Same cargo produced by industry is dropped here => not serviced by vehicle v */
02348         if ((o->GetUnloadType() & OUFB_UNLOAD) && !c_accepts) break;
02349 
02350         if (stations.Contains(st)) {
02351           if (v->owner == _local_company) return 2; // Company services industry
02352           result = 1; // Competitor services industry
02353         }
02354       }
02355     }
02356   }
02357   return result;
02358 }
02359 
02367 static void ReportNewsProductionChangeIndustry(Industry *ind, CargoID type, int percent)
02368 {
02369   NewsSubtype ns;
02370 
02371   switch (WhoCanServiceIndustry(ind)) {
02372     case 0: ns = NS_INDUSTRY_NOBODY;  break;
02373     case 1: ns = NS_INDUSTRY_OTHER;   break;
02374     case 2: ns = NS_INDUSTRY_COMPANY; break;
02375     default: NOT_REACHED();
02376   }
02377   SetDParam(2, abs(percent));
02378   SetDParam(0, CargoSpec::Get(type)->name);
02379   SetDParam(1, ind->index);
02380   AddIndustryNewsItem(
02381     percent >= 0 ? STR_NEWS_INDUSTRY_PRODUCTION_INCREASE_SMOOTH : STR_NEWS_INDUSTRY_PRODUCTION_DECREASE_SMOOTH,
02382     ns,
02383     ind->index
02384   );
02385 }
02386 
02387 static const uint PERCENT_TRANSPORTED_60 = 153;
02388 static const uint PERCENT_TRANSPORTED_80 = 204;
02389 
02395 static void ChangeIndustryProduction(Industry *i, bool monthly)
02396 {
02397   StringID str = STR_NULL;
02398   bool closeit = false;
02399   const IndustrySpec *indspec = GetIndustrySpec(i->type);
02400   bool standard = false;
02401   bool suppress_message = false;
02402   bool recalculate_multipliers = false; 
02403   /* don't use smooth economy for industries using production related callbacks */
02404   bool smooth_economy = indspec->UsesSmoothEconomy();
02405   byte div = 0;
02406   byte mul = 0;
02407   int8 increment = 0;
02408 
02409   bool callback_enabled = HasBit(indspec->callback_mask, monthly ? CBM_IND_MONTHLYPROD_CHANGE : CBM_IND_PRODUCTION_CHANGE);
02410   if (callback_enabled) {
02411     uint16 res = GetIndustryCallback(monthly ? CBID_INDUSTRY_MONTHLYPROD_CHANGE : CBID_INDUSTRY_PRODUCTION_CHANGE, 0, Random(), i, i->type, i->location.tile);
02412     if (res != CALLBACK_FAILED) { // failed callback means "do nothing"
02413       suppress_message = HasBit(res, 7);
02414       /* Get the custom message if any */
02415       if (HasBit(res, 8)) str = MapGRFStringID(indspec->grf_prop.grffile->grfid, GB(GetRegister(0x100), 0, 16));
02416       res = GB(res, 0, 4);
02417       switch (res) {
02418         default: NOT_REACHED();
02419         case 0x0: break;                  // Do nothing, but show the custom message if any
02420         case 0x1: div = 1; break;         // Halve industry production. If production reaches the quarter of the default, the industry is closed instead.
02421         case 0x2: mul = 1; break;         // Double industry production if it hasn't reached eight times of the original yet.
02422         case 0x3: closeit = true; break;  // The industry announces imminent closure, and is physically removed from the map next month.
02423         case 0x4: standard = true; break; // Do the standard random production change as if this industry was a primary one.
02424         case 0x5: case 0x6: case 0x7:     // Divide production by 4, 8, 16
02425         case 0x8: div = res - 0x3; break; // Divide production by 32
02426         case 0x9: case 0xA: case 0xB:     // Multiply production by 4, 8, 16
02427         case 0xC: mul = res - 0x7; break; // Multiply production by 32
02428         case 0xD:                         // decrement production
02429         case 0xE:                         // increment production
02430           increment = res == 0x0D ? -1 : 1;
02431           break;
02432         case 0xF:                         // Set production to third byte of register 0x100
02433           i->prod_level = Clamp(GB(GetRegister(0x100), 16, 8), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
02434           recalculate_multipliers = true;
02435           break;
02436       }
02437     }
02438   } else {
02439     if (monthly != smooth_economy) return;
02440     if (indspec->life_type == INDUSTRYLIFE_BLACK_HOLE) return;
02441   }
02442 
02443   if (standard || (!callback_enabled && (indspec->life_type & (INDUSTRYLIFE_ORGANIC | INDUSTRYLIFE_EXTRACTIVE)) != 0)) {
02444     /* decrease or increase */
02445     bool only_decrease = (indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE;
02446 
02447     if (smooth_economy) {
02448       closeit = true;
02449       for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
02450         if (i->produced_cargo[j] == CT_INVALID) continue;
02451         uint32 r = Random();
02452         int old_prod, new_prod, percent;
02453         /* If over 60% is transported, mult is 1, else mult is -1. */
02454         int mult = (i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_60) ? 1 : -1;
02455 
02456         new_prod = old_prod = i->production_rate[j];
02457 
02458         /* For industries with only_decrease flags (temperate terrain Oil Wells),
02459          * the multiplier will always be -1 so they will only decrease. */
02460         if (only_decrease) {
02461           mult = -1;
02462         /* For normal industries, if over 60% is transported, 33% chance for decrease.
02463          * Bonus for very high station ratings (over 80%): 16% chance for decrease. */
02464         } else if (Chance16I(1, ((i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_80) ? 6 : 3), r)) {
02465           mult *= -1;
02466         }
02467 
02468         /* 4.5% chance for 3-23% (or 1 unit for very low productions) production change,
02469          * determined by mult value. If mult = 1 prod. increases, else (-1) it decreases. */
02470         if (Chance16I(1, 22, r >> 16)) {
02471           new_prod += mult * (max(((RandomRange(50) + 10) * old_prod) >> 8, 1U));
02472         }
02473 
02474         /* Prevent production to overflow or Oil Rig passengers to be over-"produced" */
02475         new_prod = Clamp(new_prod, 1, 255);
02476 
02477         if (((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0) && j == 1) {
02478           new_prod = Clamp(new_prod, 0, 16);
02479         }
02480 
02481         /* Do not stop closing the industry when it has the lowest possible production rate */
02482         if (new_prod == old_prod && old_prod > 1) {
02483           closeit = false;
02484           continue;
02485         }
02486 
02487         percent = (old_prod == 0) ? 100 : (new_prod * 100 / old_prod - 100);
02488         i->production_rate[j] = new_prod;
02489 
02490         /* Close the industry when it has the lowest possible production rate */
02491         if (new_prod > 1) closeit = false;
02492 
02493         if (abs(percent) >= 10) {
02494           ReportNewsProductionChangeIndustry(i, i->produced_cargo[j], percent);
02495         }
02496       }
02497     } else {
02498       if (only_decrease || Chance16(1, 3)) {
02499         /* If more than 60% transported, 66% chance of increase, else 33% chance of increase */
02500         if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != Chance16(1, 3)) {
02501           mul = 1; // Increase production
02502         } else {
02503           div = 1; // Decrease production
02504         }
02505       }
02506     }
02507   }
02508 
02509   if (!callback_enabled && (indspec->life_type & INDUSTRYLIFE_PROCESSING)) {
02510     if ( (byte)(_cur_year - i->last_prod_year) >= 5 && Chance16(1, smooth_economy ? 180 : 2)) {
02511       closeit = true;
02512     }
02513   }
02514 
02515   /* Increase if needed */
02516   while (mul-- != 0 && i->prod_level < PRODLEVEL_MAXIMUM) {
02517     i->prod_level = min(i->prod_level * 2, PRODLEVEL_MAXIMUM);
02518     recalculate_multipliers = true;
02519     if (str == STR_NULL) str = indspec->production_up_text;
02520   }
02521 
02522   /* Decrease if needed */
02523   while (div-- != 0 && !closeit) {
02524     if (i->prod_level == PRODLEVEL_MINIMUM) {
02525       closeit = true;
02526     } else {
02527       i->prod_level = max(i->prod_level / 2, (int)PRODLEVEL_MINIMUM); // typecast to int required to please MSVC
02528       recalculate_multipliers = true;
02529       if (str == STR_NULL) str = indspec->production_down_text;
02530     }
02531   }
02532 
02533   /* Increase or Decreasing the production level if needed */
02534   if (increment != 0) {
02535     if (increment < 0 && i->prod_level == PRODLEVEL_MINIMUM) {
02536       closeit = true;
02537     } else {
02538       i->prod_level = ClampU(i->prod_level + increment, PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
02539       recalculate_multipliers = true;
02540     }
02541   }
02542 
02543   /* Recalculate production_rate
02544    * For non-smooth economy these should always be synchronized with prod_level */
02545   if (recalculate_multipliers) i->RecomputeProductionMultipliers();
02546 
02547   /* Close if needed and allowed */
02548   if (closeit && !CheckIndustryCloseDownProtection(i->type)) {
02549     i->prod_level = PRODLEVEL_CLOSURE;
02550     str = indspec->closure_text;
02551   }
02552 
02553   if (!suppress_message && str != STR_NULL) {
02554     NewsSubtype ns;
02555     /* Compute news category */
02556     if (closeit) {
02557       ns = NS_INDUSTRY_CLOSE;
02558       AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
02559     } else {
02560       switch (WhoCanServiceIndustry(i)) {
02561         case 0: ns = NS_INDUSTRY_NOBODY;  break;
02562         case 1: ns = NS_INDUSTRY_OTHER;   break;
02563         case 2: ns = NS_INDUSTRY_COMPANY; break;
02564         default: NOT_REACHED();
02565       }
02566     }
02567     /* Set parameters of news string */
02568     if (str > STR_LAST_STRINGID) {
02569       SetDParam(0, STR_TOWN_NAME);
02570       SetDParam(1, i->town->index);
02571       SetDParam(2, indspec->name);
02572     } else if (closeit) {
02573       SetDParam(0, STR_FORMAT_INDUSTRY_NAME);
02574       SetDParam(1, i->town->index);
02575       SetDParam(2, indspec->name);
02576     } else {
02577       SetDParam(0, i->index);
02578     }
02579     /* and report the news to the user */
02580     AddNewsItem(str,
02581       ns,
02582       closeit ? NR_TILE : NR_INDUSTRY,
02583       closeit ? i->location.tile + TileDiffXY(1, 1) : i->index);
02584   }
02585 }
02586 
02594 void IndustryDailyLoop()
02595 {
02596   _economy.industry_daily_change_counter += _economy.industry_daily_increment;
02597 
02598   /* Bits 16-31 of industry_construction_counter contain the number of industries to change/create today,
02599    * the lower 16 bit are a fractional part that might accumulate over several days until it
02600    * is sufficient for an industry. */
02601   uint16 change_loop = _economy.industry_daily_change_counter >> 16;
02602 
02603   /* Reset the active part of the counter, just keeping the "factional part" */
02604   _economy.industry_daily_change_counter &= 0xFFFF;
02605 
02606   if (change_loop == 0) {
02607     return;  // Nothing to do? get out
02608   }
02609 
02610   Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
02611 
02612   /* perform the required industry changes for the day */
02613 
02614   uint perc = 3; // Between 3% and 9% chance of creating a new industry.
02615   if ((_industry_builder.wanted_inds >> 16) > GetCurrentTotalNumberOfIndustries()) {
02616     perc = min(9u, perc + (_industry_builder.wanted_inds >> 16) - GetCurrentTotalNumberOfIndustries());
02617   }
02618   for (uint16 j = 0; j < change_loop; j++) {
02619     if (Chance16(perc, 100)) {
02620       _industry_builder.TryBuildNewIndustry();
02621     } else {
02622       Industry *i = Industry::GetRandom();
02623       if (i != NULL) {
02624         ChangeIndustryProduction(i, false);
02625         SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
02626       }
02627     }
02628   }
02629 
02630   cur_company.Restore();
02631 
02632   /* production-change */
02633   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
02634 }
02635 
02636 void IndustryMonthlyLoop()
02637 {
02638   Backup<CompanyByte> cur_company(_current_company, OWNER_NONE, FILE_LINE);
02639 
02640   _industry_builder.MonthlyLoop();
02641 
02642   Industry *i;
02643   FOR_ALL_INDUSTRIES(i) {
02644     UpdateIndustryStatistics(i);
02645     if (i->prod_level == PRODLEVEL_CLOSURE) {
02646       delete i;
02647     } else {
02648       ChangeIndustryProduction(i, true);
02649       SetWindowDirty(WC_INDUSTRY_VIEW, i->index);
02650     }
02651   }
02652 
02653   cur_company.Restore();
02654 
02655   /* production-change */
02656   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
02657 }
02658 
02659 
02660 void InitializeIndustries()
02661 {
02662   Industry::ResetIndustryCounts();
02663   _industry_sound_tile = 0;
02664 
02665   _industry_builder.Reset();
02666 }
02667 
02672 bool IndustrySpec::IsRawIndustry() const
02673 {
02674   /* Lumber mills are extractive/organic, but can always be built like a non-raw industry */
02675   return (this->life_type & (INDUSTRYLIFE_EXTRACTIVE | INDUSTRYLIFE_ORGANIC)) != 0 &&
02676       (this->behaviour & INDUSTRYBEH_CUT_TREES) == 0;
02677 }
02678 
02683 Money IndustrySpec::GetConstructionCost() const
02684 {
02685   /* Building raw industries like secondary uses different price base */
02686   return (_price[(_settings_game.construction.raw_industry_construction == 1 && this->IsRawIndustry()) ?
02687       PR_BUILD_INDUSTRY_RAW : PR_BUILD_INDUSTRY] * this->cost_multiplier) >> 8;
02688 }
02689 
02696 Money IndustrySpec::GetRemovalCost() const
02697 {
02698   return (_price[PR_CLEAR_INDUSTRY] * this->removal_cost_multiplier) >> 8;
02699 }
02700 
02705 bool IndustrySpec::UsesSmoothEconomy() const
02706 {
02707   return _settings_game.economy.smooth_economy &&
02708     !(HasBit(this->callback_mask, CBM_IND_PRODUCTION_256_TICKS) || HasBit(this->callback_mask, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) && // production callbacks
02709     !(HasBit(this->callback_mask, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(this->callback_mask, CBM_IND_PRODUCTION_CHANGE));            // production change callbacks
02710 }
02711 
02712 static CommandCost TerraformTile_Industry(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
02713 {
02714   if (AutoslopeEnabled()) {
02715     /* We imitate here TTDP's behaviour:
02716      *  - Both new and old slope must not be steep.
02717      *  - TileMaxZ must not be changed.
02718      *  - Allow autoslope by default.
02719      *  - Disallow autoslope if callback succeeds and returns non-zero.
02720      */
02721     Slope tileh_old = GetTileSlope(tile, NULL);
02722     /* TileMaxZ must not be changed. Slopes must not be steep. */
02723     if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
02724       const IndustryGfx gfx = GetIndustryGfx(tile);
02725       const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
02726 
02727       /* Call callback 3C 'disable autosloping for industry tiles'. */
02728       if (HasBit(itspec->callback_mask, CBM_INDT_AUTOSLOPE)) {
02729         /* If the callback fails, allow autoslope. */
02730         uint16 res = GetIndustryTileCallback(CBID_INDTILE_AUTOSLOPE, 0, 0, gfx, Industry::GetByTile(tile), tile);
02731         if ((res == 0) || (res == CALLBACK_FAILED)) return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
02732       } else {
02733         /* allow autoslope */
02734         return CommandCost(EXPENSES_CONSTRUCTION, _price[PR_BUILD_FOUNDATION]);
02735       }
02736     }
02737   }
02738   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
02739 }
02740 
02741 extern const TileTypeProcs _tile_type_industry_procs = {
02742   DrawTile_Industry,           // draw_tile_proc
02743   GetSlopeZ_Industry,          // get_slope_z_proc
02744   ClearTile_Industry,          // clear_tile_proc
02745   AddAcceptedCargo_Industry,   // add_accepted_cargo_proc
02746   GetTileDesc_Industry,        // get_tile_desc_proc
02747   GetTileTrackStatus_Industry, // get_tile_track_status_proc
02748   ClickTile_Industry,          // click_tile_proc
02749   AnimateTile_Industry,        // animate_tile_proc
02750   TileLoop_Industry,           // tile_loop_proc
02751   ChangeTileOwner_Industry,    // change_tile_owner_proc
02752   NULL,                        // add_produced_cargo_proc
02753   NULL,                        // vehicle_enter_tile_proc
02754   GetFoundation_Industry,      // get_foundation_proc
02755   TerraformTile_Industry,      // terraform_tile_proc
02756 };