industry_cmd.cpp

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