# industry_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: industry_cmd.cpp 17677 2009-10-01 10:55:05Z rubidium $ */
    00002
    00005 #include "stdafx.h"
    00006 #include "openttd.h"
    00007 #include "clear_map.h"
    00008 #include "industry_map.h"
    00009 #include "station_map.h"
    00010 #include "train.h"
    00011 #include "landscape.h"
    00012 #include "viewport_func.h"
    00013 #include "command_func.h"
    00014 #include "town.h"
    00015 #include "news_func.h"
    00016 #include "variables.h"
    00017 #include "cheat_type.h"
    00018 #include "genworld.h"
    00019 #include "tree_map.h"
    00020 #include "newgrf.h"
    00021 #include "newgrf_commons.h"
    00022 #include "newgrf_industries.h"
    00023 #include "newgrf_industrytiles.h"
    00024 #include "autoslope.h"
    00025 #include "transparency.h"
    00026 #include "water.h"
    00027 #include "strings_func.h"
    00028 #include "functions.h"
    00029 #include "window_func.h"
    00030 #include "date_func.h"
    00031 #include "vehicle_func.h"
    00032 #include "sound_func.h"
    00033 #include "oldpool_func.h"
    00034 #include "animated_tile_func.h"
    00035 #include "effectvehicle_func.h"
    00036 #include "ai/ai.hpp"
    00037
    00038 #include "table/strings.h"
    00039 #include "table/industry_land.h"
    00040 #include "table/build_industry.h"
    00041
    00042 void ShowIndustryViewWindow(int industry);
    00043 void BuildOilRig(TileIndex tile);
    00044
    00045 static byte _industry_sound_ctr;
    00046 static TileIndex _industry_sound_tile;
    00047
    00048 int _total_industries;
    00049 uint16 _industry_counts[NUM_INDUSTRYTYPES];
    00050
    00051 IndustrySpec _industry_specs[NUM_INDUSTRYTYPES];
    00052 IndustryTileSpec _industry_tile_specs[NUM_INDUSTRYTILES];
    00053
    00058 void ResetIndustries()
    00059 {
    00060   memset(&_industry_specs, 0, sizeof(_industry_specs));
    00061   memcpy(&_industry_specs, &_origin_industry_specs, sizeof(_origin_industry_specs));
    00062
    00063   /* once performed, enable only the current climate industries */
    00064   for (IndustryType i = 0; i < NUM_INDUSTRYTYPES; i++) {
    00065     _industry_specs[i].enabled = i < NEW_INDUSTRYOFFSET &&
    00066         HasBit(_origin_industry_specs[i].climate_availability, _settings_game.game_creation.landscape);
    00067   }
    00068
    00069   memset(&_industry_tile_specs, 0, sizeof(_industry_tile_specs));
    00070   memcpy(&_industry_tile_specs, &_origin_industry_tile_specs, sizeof(_origin_industry_tile_specs));
    00071
    00072   /* Reset any overrides that have been set. */
    00073   _industile_mngr.ResetOverride();
    00074   _industry_mngr.ResetOverride();
    00075 }
    00076
    00077 void ResetIndustryCreationProbility(IndustryType type)
    00078 {
    00079   assert(type < INVALID_INDUSTRYTYPE);
    00080   _industry_specs[type].appear_creation[_settings_game.game_creation.landscape] = 0;
    00081 }
    00082
    00083 DEFINE_OLD_POOL_GENERIC(Industry, Industry)
    00084
    00085
    00093 IndustryType GetIndustryType(TileIndex tile)
    00094 {
    00095   assert(IsTileType(tile, MP_INDUSTRY));
    00096
    00097   const Industry *ind = GetIndustryByTile(tile);
    00098   return ind->IsValid() ? ind->type : (IndustryType)IT_INVALID;
    00099 }
    00100
    00109 const IndustrySpec *GetIndustrySpec(IndustryType thistype)
    00110 {
    00111   assert(thistype < NUM_INDUSTRYTYPES);
    00112   return &_industry_specs[thistype];
    00113 }
    00114
    00123 const IndustryTileSpec *GetIndustryTileSpec(IndustryGfx gfx)
    00124 {
    00125   assert(gfx < INVALID_INDUSTRYTILE);
    00126   return &_industry_tile_specs[gfx];
    00127 }
    00128
    00129 Industry::~Industry()
    00130 {
    00131   if (CleaningPool()) return;
    00132
    00133   /* Industry can also be destroyed when not fully initialized.
    00134    * This means that we do not have to clear tiles either. */
    00135   if (this->width == 0) {
    00136     this->xy = INVALID_TILE;
    00137     return;
    00138   }
    00139
    00140   BEGIN_TILE_LOOP(tile_cur, this->width, this->height, this->xy);
    00141     if (IsTileType(tile_cur, MP_INDUSTRY)) {
    00142       if (GetIndustryIndex(tile_cur) == this->index) {
    00143         /* MakeWaterKeepingClass() can also handle 'land' */
    00144         MakeWaterKeepingClass(tile_cur, OWNER_NONE);
    00145
    00146         /* MakeWaterKeepingClass() doesn't remove animation if the tiles
    00147          * become watery, but be on the safe side an always remote it. */
    00148         DeleteAnimatedTile(tile_cur);
    00149       }
    00150     } else if (IsTileType(tile_cur, MP_STATION) && IsOilRig(tile_cur)) {
    00151       DeleteOilRig(tile_cur);
    00152     }
    00153   END_TILE_LOOP(tile_cur, this->width, this->height, this->xy);
    00154
    00155   if (GetIndustrySpec(this->type)->behaviour & INDUSTRYBEH_PLANT_FIELDS) {
    00156     /* Remove the farmland and convert it to regular tiles over time. */
    00157     BEGIN_TILE_LOOP(tile_cur, 42, 42, this->xy - TileDiffXY(21, 21)) {
    00158       tile_cur = TILE_MASK(tile_cur);
    00159       if (IsTileType(tile_cur, MP_CLEAR) && IsClearGround(tile_cur, CLEAR_FIELDS) &&
    00160           GetIndustryIndexOfField(tile_cur) == this->index) {
    00161         SetIndustryIndexOfField(tile_cur, INVALID_INDUSTRY);
    00162       }
    00163     } END_TILE_LOOP(tile_cur, 42, 42, this->xy - TileDiff(21, 21))
    00164   }
    00165
    00166   /* don't let any disaster vehicle target invalid industry */
    00167   ReleaseDisastersTargetingIndustry(this->index);
    00168
    00169   DecIndustryTypeCount(this->type);
    00170
    00171   DeleteSubsidyWithIndustry(this->index);
    00172   DeleteWindowById(WC_INDUSTRY_VIEW, this->index);
    00173   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
    00174   this->xy = INVALID_TILE;
    00175 }
    00176
    00177 static void IndustryDrawSugarMine(const TileInfo *ti)
    00178 {
    00179   const DrawIndustryAnimationStruct *d;
    00180
    00181   if (!IsIndustryCompleted(ti->tile)) return;
    00182
    00183   d = &_draw_industry_spec1[GetIndustryAnimationState(ti->tile)];
    00184
    00185   AddChildSpriteScreen(SPR_IT_SUGAR_MINE_SIEVE + d->image_1, PAL_NONE, d->x, 0);
    00186
    00187   if (d->image_2 != 0) {
    00188     AddChildSpriteScreen(SPR_IT_SUGAR_MINE_CLOUDS + d->image_2 - 1, PAL_NONE, 8, 41);
    00189   }
    00190
    00191   if (d->image_3 != 0) {
    00192     AddChildSpriteScreen(SPR_IT_SUGAR_MINE_PILE + d->image_3 - 1, PAL_NONE,
    00193       _drawtile_proc1[d->image_3 - 1].x, _drawtile_proc1[d->image_3 - 1].y);
    00194   }
    00195 }
    00196
    00197 static void IndustryDrawToffeeQuarry(const TileInfo *ti)
    00198 {
    00199   uint8 x = 0;
    00200
    00201   if (IsIndustryCompleted(ti->tile)) {
    00202     x = _industry_anim_offs_toffee[GetIndustryAnimationState(ti->tile)];
    00203     if (x == 0xFF)
    00204       x = 0;
    00205   }
    00206
    00207   AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_SHOVEL, PAL_NONE, 22 - x, 24 + x);
    00208   AddChildSpriteScreen(SPR_IT_TOFFEE_QUARRY_TOFFEE, PAL_NONE, 6, 14);
    00209 }
    00210
    00211 static void IndustryDrawBubbleGenerator( const TileInfo *ti)
    00212 {
    00213   if (IsIndustryCompleted(ti->tile)) {
    00214     AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_BUBBLE, PAL_NONE, 5, _industry_anim_offs_bubbles[GetIndustryAnimationState(ti->tile)]);
    00215   } else {
    00216     AddChildSpriteScreen(SPR_IT_BUBBLE_GENERATOR_SPRING, PAL_NONE, 3, 67);
    00217   }
    00218 }
    00219
    00220 static void IndustryDrawToyFactory(const TileInfo *ti)
    00221 {
    00222   const DrawIndustryAnimationStruct *d;
    00223
    00224   d = &_industry_anim_offs_toys[GetIndustryAnimationState(ti->tile)];
    00225
    00226   if (d->image_1 != 0xFF) {
    00227     AddChildSpriteScreen(SPR_IT_TOY_FACTORY_CLAY, PAL_NONE, d->x, 96 + d->image_1);
    00228   }
    00229
    00230   if (d->image_2 != 0xFF) {
    00231     AddChildSpriteScreen(SPR_IT_TOY_FACTORY_ROBOT, PAL_NONE, 16 - d->image_2 * 2, 100 + d->image_2);
    00232   }
    00233
    00234   AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP, PAL_NONE, 7, d->image_3);
    00235   AddChildSpriteScreen(SPR_IT_TOY_FACTORY_STAMP_HOLDER, PAL_NONE, 0, 42);
    00236 }
    00237
    00238 static void IndustryDrawCoalPlantSparks(const TileInfo *ti)
    00239 {
    00240   if (IsIndustryCompleted(ti->tile)) {
    00241     uint8 image = GetIndustryAnimationState(ti->tile);
    00242
    00243     if (image != 0 && image < 7) {
    00244       AddChildSpriteScreen(image + SPR_IT_POWER_PLANT_TRANSFORMERS,
    00245         PAL_NONE,
    00246         _coal_plant_sparks[image - 1].x,
    00247         _coal_plant_sparks[image - 1].y
    00248       );
    00249     }
    00250   }
    00251 }
    00252
    00253 typedef void IndustryDrawTileProc(const TileInfo *ti);
    00254 static IndustryDrawTileProc * const _industry_draw_tile_procs[5] = {
    00255   IndustryDrawSugarMine,
    00256   IndustryDrawToffeeQuarry,
    00257   IndustryDrawBubbleGenerator,
    00258   IndustryDrawToyFactory,
    00259   IndustryDrawCoalPlantSparks,
    00260 };
    00261
    00262 static void DrawTile_Industry(TileInfo *ti)
    00263 {
    00264   IndustryGfx gfx = GetIndustryGfx(ti->tile);
    00265   Industry *ind = GetIndustryByTile(ti->tile);
    00266   const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
    00267   const DrawBuildingsTileStruct *dits;
    00268   SpriteID image;
    00269   SpriteID pal;
    00270
    00271   /* Retrieve pointer to the draw industry tile struct */
    00272   if (gfx >= NEW_INDUSTRYTILEOFFSET) {
    00273     /* Draw the tile using the specialized method of newgrf industrytile.
    00274      * DrawNewIndustry will return false if ever the resolver could not
    00275      * find any sprite to display.  So in this case, we will jump on the
    00276      * substitute gfx instead. */
    00277     if (indts->grf_prop.spritegroup != NULL && DrawNewIndustryTile(ti, ind, gfx, indts)) {
    00278       return;
    00279     } else {
    00280       /* No sprite group (or no valid one) found, meaning no graphics associated.
    00281        * Use the substitute one instead */
    00282       if (indts->grf_prop.subst_id != INVALID_INDUSTRYTILE) {
    00283         gfx = indts->grf_prop.subst_id;
    00284         /* And point the industrytile spec accordingly */
    00285         indts = GetIndustryTileSpec(gfx);
    00286       }
    00287     }
    00288   }
    00289
    00290   dits = &_industry_draw_tile_data[gfx << 2 | (indts->anim_state ?
    00291       GetIndustryAnimationState(ti->tile) & INDUSTRY_COMPLETED :
    00292       GetIndustryConstructionStage(ti->tile))];
    00293
    00294   image = dits->ground.sprite;
    00295   if (HasBit(image, PALETTE_MODIFIER_COLOUR) && dits->ground.pal == PAL_NONE) {
    00296     pal = GENERAL_SPRITE_COLOUR(ind->random_colour);
    00297   } else {
    00298     pal = dits->ground.pal;
    00299   }
    00300
    00301   /* DrawFoundation() modifes ti->z and ti->tileh */
    00302   if (ti->tileh != SLOPE_FLAT) DrawFoundation(ti, FOUNDATION_LEVELED);
    00303
    00304   /* If the ground sprite is the default flat water sprite, draw also canal/river borders.
    00305    * Do not do this if the tile's WaterClass is 'land'. */
    00306   if (image == SPR_FLAT_WATER_TILE && IsIndustryTileOnWater(ti->tile)) {
    00307     DrawWaterClassGround(ti);
    00308   } else {
    00309     DrawGroundSprite(image, pal);
    00310   }
    00311
    00312   /* If industries are transparent and invisible, do not draw the upper part */
    00313   if (IsInvisibilitySet(TO_INDUSTRIES)) return;
    00314
    00315   /* Add industry on top of the ground? */
    00316   image = dits->building.sprite;
    00317   if (image != 0) {
    00318     AddSortableSpriteToDraw(image,
    00319       (HasBit(image, PALETTE_MODIFIER_COLOUR) && dits->building.pal == PAL_NONE) ? GENERAL_SPRITE_COLOUR(ind->random_colour) : dits->building.pal,
    00320       ti->x + dits->subtile_x,
    00321       ti->y + dits->subtile_y,
    00322       dits->width,
    00323       dits->height,
    00324       dits->dz,
    00325       ti->z,
    00326       IsTransparencySet(TO_INDUSTRIES));
    00327
    00328     if (IsTransparencySet(TO_INDUSTRIES)) return;
    00329   }
    00330
    00331   {
    00332     int proc = dits->draw_proc - 1;
    00333     if (proc >= 0) _industry_draw_tile_procs[proc](ti);
    00334   }
    00335 }
    00336
    00337 static uint GetSlopeZ_Industry(TileIndex tile, uint x, uint y)
    00338 {
    00339   return GetTileMaxZ(tile);
    00340 }
    00341
    00342 static Foundation GetFoundation_Industry(TileIndex tile, Slope tileh)
    00343 {
    00344   IndustryGfx gfx = GetIndustryGfx(tile);
    00345
    00346   /* For NewGRF industry tiles we might not be drawing a foundation. We need to
    00347    * account for this, as other structures should
    00348    * draw the wall of the foundation in this case.
    00349    */
    00350   if (gfx >= NEW_INDUSTRYTILEOFFSET) {
    00351     const IndustryTileSpec *indts = GetIndustryTileSpec(gfx);
    00352     if (indts->grf_prop.spritegroup != NULL && HasBit(indts->callback_flags, CBM_INDT_DRAW_FOUNDATIONS)) {
    00353       uint32 callback_res = GetIndustryTileCallback(CBID_INDUSTRY_DRAW_FOUNDATIONS, 0, 0, gfx, GetIndustryByTile(tile), tile);
    00354       if (callback_res == 0) return FOUNDATION_NONE;
    00355     }
    00356   }
    00357   return FlatteningFoundation(tileh);
    00358 }
    00359
    00360 static void GetAcceptedCargo_Industry(TileIndex tile, AcceptedCargo ac)
    00361 {
    00362   IndustryGfx gfx = GetIndustryGfx(tile);
    00363   const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
    00364
    00365   /* When we have to use a callback, we put our data in the next two variables */
    00366   CargoID raw_accepts_cargo[lengthof(itspec->accepts_cargo)];
    00367   uint8 raw_acceptance[lengthof(itspec->acceptance)];
    00368
    00369   /* And then these will always point to a same sized array with the required data */
    00370   const CargoID *accepts_cargo = itspec->accepts_cargo;
    00371   const uint8 *acceptance = itspec->acceptance;
    00372
    00373   if (HasBit(itspec->callback_flags, CBM_INDT_ACCEPT_CARGO)) {
    00374     uint16 res = GetIndustryTileCallback(CBID_INDTILE_ACCEPT_CARGO, 0, 0, gfx, GetIndustryByTile(tile), tile);
    00375     if (res != CALLBACK_FAILED) {
    00376       accepts_cargo = raw_accepts_cargo;
    00377       for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_accepts_cargo[i] = GetCargoTranslation(GB(res, i * 5, 5), itspec->grf_prop.grffile);
    00378     }
    00379   }
    00380
    00381   if (HasBit(itspec->callback_flags, CBM_INDT_CARGO_ACCEPTANCE)) {
    00382     uint16 res = GetIndustryTileCallback(CBID_INDTILE_CARGO_ACCEPTANCE, 0, 0, gfx, GetIndustryByTile(tile), tile);
    00383     if (res != CALLBACK_FAILED) {
    00384       acceptance = raw_acceptance;
    00385       for (uint i = 0; i < lengthof(itspec->accepts_cargo); i++) raw_acceptance[i] = GB(res, i * 4, 4);
    00386     }
    00387   }
    00388
    00389   for (byte i = 0; i < lengthof(itspec->accepts_cargo); i++) {
    00390     CargoID a = accepts_cargo[i];
    00391     /* Only set the value once. */
    00392     if (a != CT_INVALID && ac[a] == 0) ac[a] = acceptance[i];
    00393   }
    00394 }
    00395
    00396 static void GetTileDesc_Industry(TileIndex tile, TileDesc *td)
    00397 {
    00398   const Industry *i = GetIndustryByTile(tile);
    00399   const IndustrySpec *is = GetIndustrySpec(i->type);
    00400
    00401   td->owner[0] = i->owner;
    00402   td->str = is->name;
    00403   if (!IsIndustryCompleted(tile)) {
    00404     SetDParamX(td->dparam, 0, td->str);
    00405     td->str = STR_2058_UNDER_CONSTRUCTION;
    00406   }
    00407
    00408   if (is->grf_prop.grffile != NULL) {
    00409     td->grf = GetGRFConfig(is->grf_prop.grffile->grfid)->name;
    00410   }
    00411 }
    00412
    00413 static CommandCost ClearTile_Industry(TileIndex tile, DoCommandFlag flags)
    00414 {
    00415   Industry *i = GetIndustryByTile(tile);
    00416   const IndustrySpec *indspec = GetIndustrySpec(i->type);
    00417
    00418   /* water can destroy industries
    00419    * in editor you can bulldoze industries
    00420    * with magic_bulldozer cheat you can destroy industries
    00421    * (area around OILRIG is water, so water shouldn't flood it
    00422    */
    00423   if ((_current_company != OWNER_WATER && _game_mode != GM_EDITOR &&
    00424       !_cheats.magic_bulldozer.value) ||
    00425       ((flags & DC_AUTO) != 0) ||
    00426       (_current_company == OWNER_WATER &&
    00427         ((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) ||
    00428         HasBit(GetIndustryTileSpec(GetIndustryGfx(tile))->slopes_refused, 5)))) {
    00429     SetDParam(0, indspec->name);
    00430     return_cmd_error(flags & DC_AUTO ? STR_4800_IN_THE_WAY : INVALID_STRING_ID);
    00431   }
    00432
    00433   if (flags & DC_EXEC) {
    00434     AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
    00435     delete i;
    00436   }
    00437   return CommandCost(EXPENSES_CONSTRUCTION, indspec->GetRemovalCost());
    00438 }
    00439
    00440 static void TransportIndustryGoods(TileIndex tile)
    00441 {
    00442   Industry *i = GetIndustryByTile(tile);
    00443   const IndustrySpec *indspec = GetIndustrySpec(i->type);
    00444   bool moved_cargo = false;
    00445
    00446   for (uint j = 0; j < lengthof(i->produced_cargo_waiting); j++) {
    00447     uint cw = min(i->produced_cargo_waiting[j], 255);
    00448     if (cw > indspec->minimal_cargo && i->produced_cargo[j] != CT_INVALID) {
    00449       i->produced_cargo_waiting[j] -= cw;
    00450
    00451       /* fluctuating economy? */
    00452       if (_economy.fluct <= 0) cw = (cw + 1) / 2;
    00453
    00454       i->this_month_production[j] += cw;
    00455
    00456       uint am = MoveGoodsToStation(i->xy, i->width, i->height, i->produced_cargo[j], cw);
    00457       i->this_month_transported[j] += am;
    00458
    00459       moved_cargo |= (am != 0);
    00460     }
    00461   }
    00462
    00463   if (moved_cargo && !StartStopIndustryTileAnimation(i, IAT_INDUSTRY_DISTRIBUTES_CARGO)) {
    00464     uint newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_production;
    00465
    00466     if (newgfx != INDUSTRYTILE_NOANIM) {
    00467       ResetIndustryConstructionStage(tile);
    00468       SetIndustryCompleted(tile, true);
    00469       SetIndustryGfx(tile, newgfx);
    00470       MarkTileDirtyByTile(tile);
    00471     }
    00472   }
    00473 }
    00474
    00475
    00476 static void AnimateTile_Industry(TileIndex tile)
    00477 {
    00478   byte m;
    00479   IndustryGfx gfx = GetIndustryGfx(tile);
    00480
    00481   if (GetIndustryTileSpec(gfx)->animation_info != 0xFFFF) {
    00482     AnimateNewIndustryTile(tile);
    00483     return;
    00484   }
    00485
    00486   switch (gfx) {
    00487   case GFX_SUGAR_MINE_SIEVE:
    00488     if ((_tick_counter & 1) == 0) {
    00489       m = GetIndustryAnimationState(tile) + 1;
    00490
    00491       switch (m & 7) {
    00492       case 2: SndPlayTileFx(SND_2D_RIP_2, tile); break;
    00493       case 6: SndPlayTileFx(SND_29_RIP, tile); break;
    00494       }
    00495
    00496       if (m >= 96) {
    00497         m = 0;
    00498         DeleteAnimatedTile(tile);
    00499       }
    00500       SetIndustryAnimationState(tile, m);
    00501
    00502       MarkTileDirtyByTile(tile);
    00503     }
    00504     break;
    00505
    00506   case GFX_TOFFEE_QUARY:
    00507     if ((_tick_counter & 3) == 0) {
    00508       m = GetIndustryAnimationState(tile);
    00509
    00510       if (_industry_anim_offs_toffee[m] == 0xFF) {
    00511         SndPlayTileFx(SND_30_CARTOON_SOUND, tile);
    00512       }
    00513
    00514       if (++m >= 70) {
    00515         m = 0;
    00516         DeleteAnimatedTile(tile);
    00517       }
    00518       SetIndustryAnimationState(tile, m);
    00519
    00520       MarkTileDirtyByTile(tile);
    00521     }
    00522     break;
    00523
    00524   case GFX_BUBBLE_CATCHER:
    00525     if ((_tick_counter & 1) == 0) {
    00526       m = GetIndustryAnimationState(tile);
    00527
    00528       if (++m >= 40) {
    00529         m = 0;
    00530         DeleteAnimatedTile(tile);
    00531       }
    00532       SetIndustryAnimationState(tile, m);
    00533
    00534       MarkTileDirtyByTile(tile);
    00535     }
    00536     break;
    00537
    00538   /* Sparks on a coal plant */
    00539   case GFX_POWERPLANT_SPARKS:
    00540     if ((_tick_counter & 3) == 0) {
    00541       m = GetIndustryAnimationState(tile);
    00542       if (m == 6) {
    00543         SetIndustryAnimationState(tile, 0);
    00544         DeleteAnimatedTile(tile);
    00545       } else {
    00546         SetIndustryAnimationState(tile, m + 1);
    00547         MarkTileDirtyByTile(tile);
    00548       }
    00549     }
    00550     break;
    00551
    00552   case GFX_TOY_FACTORY:
    00553     if ((_tick_counter & 1) == 0) {
    00554       m = GetIndustryAnimationState(tile) + 1;
    00555
    00556       switch (m) {
    00557         case  1: SndPlayTileFx(SND_2C_MACHINERY, tile); break;
    00558         case 23: SndPlayTileFx(SND_2B_COMEDY_HIT, tile); break;
    00559         case 28: SndPlayTileFx(SND_2A_EXTRACT_AND_POP, tile); break;
    00560         default:
    00561           if (m >= 50) {
    00562             int n = GetIndustryAnimationLoop(tile) + 1;
    00563             m = 0;
    00564             if (n >= 8) {
    00565               n = 0;
    00566               DeleteAnimatedTile(tile);
    00567             }
    00568             SetIndustryAnimationLoop(tile, n);
    00569           }
    00570       }
    00571
    00572       SetIndustryAnimationState(tile, m);
    00573       MarkTileDirtyByTile(tile);
    00574     }
    00575     break;
    00576
    00577   case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
    00578   case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
    00579   case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
    00580   case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
    00581     if ((_tick_counter & 3) == 0) {
    00582       IndustryGfx gfx = GetIndustryGfx(tile);
    00583
    00584       gfx = (gfx < 155) ? gfx + 1 : 148;
    00585       SetIndustryGfx(tile, gfx);
    00586       MarkTileDirtyByTile(tile);
    00587     }
    00588     break;
    00589
    00590   case GFX_OILWELL_ANIMATED_1:
    00591   case GFX_OILWELL_ANIMATED_2:
    00592   case GFX_OILWELL_ANIMATED_3:
    00593     if ((_tick_counter & 7) == 0) {
    00594       bool b = Chance16(1, 7);
    00595       IndustryGfx gfx = GetIndustryGfx(tile);
    00596
    00597       m = GetIndustryAnimationState(tile) + 1;
    00598       if (m == 4 && (m = 0, ++gfx) == GFX_OILWELL_ANIMATED_3 + 1 && (gfx = GFX_OILWELL_ANIMATED_1, b)) {
    00599         SetIndustryGfx(tile, GFX_OILWELL_NOT_ANIMATED);
    00600         SetIndustryConstructionStage(tile, 3);
    00601         DeleteAnimatedTile(tile);
    00602       } else {
    00603         SetIndustryAnimationState(tile, m);
    00604         SetIndustryGfx(tile, gfx);
    00605         MarkTileDirtyByTile(tile);
    00606       }
    00607     }
    00608     break;
    00609
    00610   case GFX_COAL_MINE_TOWER_ANIMATED:
    00611   case GFX_COPPER_MINE_TOWER_ANIMATED:
    00612   case GFX_GOLD_MINE_TOWER_ANIMATED: {
    00613       int state = _tick_counter & 0x7FF;
    00614
    00615       if ((state -= 0x400) < 0)
    00616         return;
    00617
    00618       if (state < 0x1A0) {
    00619         if (state < 0x20 || state >= 0x180) {
    00620           m = GetIndustryAnimationState(tile);
    00621           if (!(m & 0x40)) {
    00622             SetIndustryAnimationState(tile, m | 0x40);
    00623             SndPlayTileFx(SND_0B_MINING_MACHINERY, tile);
    00624           }
    00625           if (state & 7)
    00626             return;
    00627         } else {
    00628           if (state & 3)
    00629             return;
    00630         }
    00631         m = (GetIndustryAnimationState(tile) + 1) | 0x40;
    00632         if (m > 0xC2) m = 0xC0;
    00633         SetIndustryAnimationState(tile, m);
    00634         MarkTileDirtyByTile(tile);
    00635       } else if (state >= 0x200 && state < 0x3A0) {
    00636         int i;
    00637         i = (state < 0x220 || state >= 0x380) ? 7 : 3;
    00638         if (state & i)
    00639           return;
    00640
    00641         m = (GetIndustryAnimationState(tile) & 0xBF) - 1;
    00642         if (m < 0x80) m = 0x82;
    00643         SetIndustryAnimationState(tile, m);
    00644         MarkTileDirtyByTile(tile);
    00645       }
    00646     } break;
    00647   }
    00648 }
    00649
    00650 static void CreateChimneySmoke(TileIndex tile)
    00651 {
    00652   uint x = TileX(tile) * TILE_SIZE;
    00653   uint y = TileY(tile) * TILE_SIZE;
    00654   uint z = GetTileMaxZ(tile);
    00655
    00656   CreateEffectVehicle(x + 15, y + 14, z + 59, EV_CHIMNEY_SMOKE);
    00657 }
    00658
    00659 static void MakeIndustryTileBigger(TileIndex tile)
    00660 {
    00661   byte cnt = GetIndustryConstructionCounter(tile) + 1;
    00662   byte stage;
    00663
    00664   if (cnt != 4) {
    00665     SetIndustryConstructionCounter(tile, cnt);
    00666     return;
    00667   }
    00668
    00669   stage = GetIndustryConstructionStage(tile) + 1;
    00670   SetIndustryConstructionCounter(tile, 0);
    00671   SetIndustryConstructionStage(tile, stage);
    00672   StartStopIndustryTileAnimation(tile, IAT_CONSTRUCTION_STATE_CHANGE);
    00673   if (stage == INDUSTRY_COMPLETED) SetIndustryCompleted(tile, true);
    00674
    00675   MarkTileDirtyByTile(tile);
    00676
    00677   if (!IsIndustryCompleted(tile)) return;
    00678
    00679   IndustryGfx gfx = GetIndustryGfx(tile);
    00680   if (gfx >= NEW_INDUSTRYTILEOFFSET) {
    00681     /* New industries are already animated on construction. */
    00682     return;
    00683   }
    00684
    00685   switch (gfx) {
    00686   case GFX_POWERPLANT_CHIMNEY:
    00687     CreateChimneySmoke(tile);
    00688     break;
    00689
    00690   case GFX_OILRIG_1:
    00691     if (GetIndustryGfx(tile + TileDiffXY(0, 1)) == GFX_OILRIG_1) BuildOilRig(tile);
    00692     break;
    00693
    00694   case GFX_TOY_FACTORY:
    00695   case GFX_BUBBLE_CATCHER:
    00696   case GFX_TOFFEE_QUARY:
    00697     SetIndustryAnimationState(tile, 0);
    00698     SetIndustryAnimationLoop(tile, 0);
    00699     break;
    00700
    00701   case GFX_PLASTIC_FOUNTAIN_ANIMATED_1: case GFX_PLASTIC_FOUNTAIN_ANIMATED_2:
    00702   case GFX_PLASTIC_FOUNTAIN_ANIMATED_3: case GFX_PLASTIC_FOUNTAIN_ANIMATED_4:
    00703   case GFX_PLASTIC_FOUNTAIN_ANIMATED_5: case GFX_PLASTIC_FOUNTAIN_ANIMATED_6:
    00704   case GFX_PLASTIC_FOUNTAIN_ANIMATED_7: case GFX_PLASTIC_FOUNTAIN_ANIMATED_8:
    00705     AddAnimatedTile(tile);
    00706     break;
    00707   }
    00708 }
    00709
    00710 static void TileLoopIndustry_BubbleGenerator(TileIndex tile)
    00711 {
    00712   int dir;
    00713   Vehicle *v;
    00714   static const int8 _bubble_spawn_location[3][4] = {
    00715     { 11,   0, -4, -14 },
    00716     { -4, -10, -4,   1 },
    00717     { 49,  59, 60,  65 },
    00718   };
    00719
    00720   SndPlayTileFx(SND_2E_EXTRACT_AND_POP, tile);
    00721
    00722   dir = Random() & 3;
    00723
    00724   v = CreateEffectVehicleAbove(
    00725     TileX(tile) * TILE_SIZE + _bubble_spawn_location[0][dir],
    00726     TileY(tile) * TILE_SIZE + _bubble_spawn_location[1][dir],
    00727     _bubble_spawn_location[2][dir],
    00728     EV_BUBBLE
    00729   );
    00730
    00731   if (v != NULL) v->u.effect.animation_substate = dir;
    00732 }
    00733
    00734 static void TileLoop_Industry(TileIndex tile)
    00735 {
    00736   IndustryGfx newgfx;
    00737   IndustryGfx gfx;
    00738
    00739   if (IsIndustryTileOnWater(tile)) TileLoop_Water(tile);
    00740
    00741   TriggerIndustryTile(tile, INDTILE_TRIGGER_TILE_LOOP);
    00742
    00743   if (!IsIndustryCompleted(tile)) {
    00744     MakeIndustryTileBigger(tile);
    00745     return;
    00746   }
    00747
    00748   if (_game_mode == GM_EDITOR) return;
    00749
    00750   TransportIndustryGoods(tile);
    00751
    00752   if (StartStopIndustryTileAnimation(tile, IAT_TILELOOP)) return;
    00753
    00754   newgfx = GetIndustryTileSpec(GetIndustryGfx(tile))->anim_next;
    00755   if (newgfx != INDUSTRYTILE_NOANIM) {
    00756     ResetIndustryConstructionStage(tile);
    00757     SetIndustryGfx(tile, newgfx);
    00758     MarkTileDirtyByTile(tile);
    00759     return;
    00760   }
    00761
    00762   gfx = GetIndustryGfx(tile);
    00763
    00764   switch (gfx) {
    00765   case GFX_COAL_MINE_TOWER_NOT_ANIMATED:
    00766   case GFX_COPPER_MINE_TOWER_NOT_ANIMATED:
    00767   case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:
    00768     if (!(_tick_counter & 0x400) && Chance16(1, 2)) {
    00769       switch (gfx) {
    00770         case GFX_COAL_MINE_TOWER_NOT_ANIMATED:   gfx = GFX_COAL_MINE_TOWER_ANIMATED;   break;
    00771         case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_ANIMATED; break;
    00772         case GFX_GOLD_MINE_TOWER_NOT_ANIMATED:   gfx = GFX_GOLD_MINE_TOWER_ANIMATED;   break;
    00773       }
    00774       SetIndustryGfx(tile, gfx);
    00775       SetIndustryAnimationState(tile, 0x80);
    00776       AddAnimatedTile(tile);
    00777     }
    00778     break;
    00779
    00780   case GFX_OILWELL_NOT_ANIMATED:
    00781     if (Chance16(1, 6)) {
    00782       SetIndustryGfx(tile, GFX_OILWELL_ANIMATED_1);
    00783       SetIndustryAnimationState(tile, 0);
    00784       AddAnimatedTile(tile);
    00785     }
    00786     break;
    00787
    00788   case GFX_COAL_MINE_TOWER_ANIMATED:
    00789   case GFX_COPPER_MINE_TOWER_ANIMATED:
    00790   case GFX_GOLD_MINE_TOWER_ANIMATED:
    00791     if (!(_tick_counter & 0x400)) {
    00792       switch (gfx) {
    00793         case GFX_COAL_MINE_TOWER_ANIMATED:   gfx = GFX_COAL_MINE_TOWER_NOT_ANIMATED;   break;
    00794         case GFX_COPPER_MINE_TOWER_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_NOT_ANIMATED; break;
    00795         case GFX_GOLD_MINE_TOWER_ANIMATED:   gfx = GFX_GOLD_MINE_TOWER_NOT_ANIMATED;   break;
    00796       }
    00797       SetIndustryGfx(tile, gfx);
    00798       SetIndustryCompleted(tile, true);
    00799       SetIndustryConstructionStage(tile, 3);
    00800       DeleteAnimatedTile(tile);
    00801     }
    00802     break;
    00803
    00804   case GFX_POWERPLANT_SPARKS:
    00805     if (Chance16(1, 3)) {
    00806       SndPlayTileFx(SND_0C_ELECTRIC_SPARK, tile);
    00807       AddAnimatedTile(tile);
    00808     }
    00809     break;
    00810
    00811   case GFX_COPPER_MINE_CHIMNEY:
    00812     CreateEffectVehicleAbove(TileX(tile) * TILE_SIZE + 6, TileY(tile) * TILE_SIZE + 6, 43, EV_SMOKE);
    00813     break;
    00814
    00815
    00816   case GFX_TOY_FACTORY: {
    00817       Industry *i = GetIndustryByTile(tile);
    00818       if (i->was_cargo_delivered) {
    00819         i->was_cargo_delivered = false;
    00820         SetIndustryAnimationLoop(tile, 0);
    00821         AddAnimatedTile(tile);
    00822       }
    00823     }
    00824     break;
    00825
    00826   case GFX_BUBBLE_GENERATOR:
    00827     TileLoopIndustry_BubbleGenerator(tile);
    00828     break;
    00829
    00830   case GFX_TOFFEE_QUARY:
    00831     AddAnimatedTile(tile);
    00832     break;
    00833
    00834   case GFX_SUGAR_MINE_SIEVE:
    00835     if (Chance16(1, 3)) AddAnimatedTile(tile);
    00836     break;
    00837   }
    00838 }
    00839
    00840 static bool ClickTile_Industry(TileIndex tile)
    00841 {
    00842   ShowIndustryViewWindow(GetIndustryIndex(tile));
    00843   return true;
    00844 }
    00845
    00846 static TrackStatus GetTileTrackStatus_Industry(TileIndex tile, TransportType mode, uint sub_mode, DiagDirection side)
    00847 {
    00848   return 0;
    00849 }
    00850
    00851 static void GetProducedCargo_Industry(TileIndex tile, CargoID *b)
    00852 {
    00853   const Industry *i = GetIndustryByTile(tile);
    00854
    00855   b[0] = i->produced_cargo[0];
    00856   b[1] = i->produced_cargo[1];
    00857 }
    00858
    00859 static void ChangeTileOwner_Industry(TileIndex tile, Owner old_owner, Owner new_owner)
    00860 {
    00861   /* If the founder merges, the industry was created by the merged company */
    00862   Industry *i = GetIndustryByTile(tile);
    00863   if (i->founder == old_owner) i->founder = (new_owner == INVALID_OWNER) ? OWNER_NONE : new_owner;
    00864 }
    00865
    00866 static const byte _plantfarmfield_type[] = {1, 1, 1, 1, 1, 3, 3, 4, 4, 4, 5, 5, 5, 6, 6, 6};
    00867
    00868 static bool IsBadFarmFieldTile(TileIndex tile)
    00869 {
    00870   switch (GetTileType(tile)) {
    00871     case MP_CLEAR: return IsClearGround(tile, CLEAR_FIELDS) || IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
    00872     case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
    00873     default:       return true;
    00874   }
    00875 }
    00876
    00877 static bool IsBadFarmFieldTile2(TileIndex tile)
    00878 {
    00879   switch (GetTileType(tile)) {
    00880     case MP_CLEAR: return IsClearGround(tile, CLEAR_SNOW) || IsClearGround(tile, CLEAR_DESERT);
    00881     case MP_TREES: return (GetTreeGround(tile) == TREE_GROUND_SHORE);
    00882     default:       return true;
    00883   }
    00884 }
    00885
    00886 static void SetupFarmFieldFence(TileIndex tile, int size, byte type, Axis direction)
    00887 {
    00888   do {
    00889     tile = TILE_MASK(tile);
    00890
    00891     if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) {
    00892       byte or_ = type;
    00893
    00894       if (or_ == 1 && Chance16(1, 7)) or_ = 2;
    00895
    00896       if (direction == AXIS_X) {
    00897         SetFenceSE(tile, or_);
    00898       } else {
    00899         SetFenceSW(tile, or_);
    00900       }
    00901     }
    00902
    00903     tile += (direction == AXIS_X ? TileDiffXY(1, 0) : TileDiffXY(0, 1));
    00904   } while (--size);
    00905 }
    00906
    00907 static void PlantFarmField(TileIndex tile, IndustryID industry)
    00908 {
    00909   uint size_x, size_y;
    00910   uint32 r;
    00911   uint count;
    00912   uint counter;
    00913   uint field_type;
    00914   int type;
    00915
    00916   if (_settings_game.game_creation.landscape == LT_ARCTIC) {
    00917     if (GetTileZ(tile) + TILE_HEIGHT * 2 >= GetSnowLine())
    00918       return;
    00919   }
    00920
    00921   /* determine field size */
    00922   r = (Random() & 0x303) + 0x404;
    00923   if (_settings_game.game_creation.landscape == LT_ARCTIC) r += 0x404;
    00924   size_x = GB(r, 0, 8);
    00925   size_y = GB(r, 8, 8);
    00926
    00927   /* offset tile to match size */
    00928   tile -= TileDiffXY(size_x / 2, size_y / 2);
    00929
    00930   if (TileX(tile) + size_x >= MapSizeX() || TileY(tile) + size_y >= MapSizeY()) return;
    00931
    00932   /* check the amount of bad tiles */
    00933   count = 0;
    00934   BEGIN_TILE_LOOP(cur_tile, size_x, size_y, tile)
    00935     assert(cur_tile < MapSize());
    00936     count += IsBadFarmFieldTile(cur_tile);
    00937   END_TILE_LOOP(cur_tile, size_x, size_y, tile)
    00938   if (count * 2 >= size_x * size_y) return;
    00939
    00940   /* determine type of field */
    00941   r = Random();
    00942   counter = GB(r, 5, 3);
    00943   field_type = GB(r, 8, 8) * 9 >> 8;
    00944
    00945   /* make field */
    00946   BEGIN_TILE_LOOP(cur_tile, size_x, size_y, tile)
    00947     assert(cur_tile < MapSize());
    00948     if (!IsBadFarmFieldTile2(cur_tile)) {
    00949       MakeField(cur_tile, field_type, industry);
    00950       SetClearCounter(cur_tile, counter);
    00951       MarkTileDirtyByTile(cur_tile);
    00952     }
    00953   END_TILE_LOOP(cur_tile, size_x, size_y, tile)
    00954
    00955   type = 3;
    00956   if (_settings_game.game_creation.landscape != LT_ARCTIC && _settings_game.game_creation.landscape != LT_TROPIC) {
    00957     type = _plantfarmfield_type[Random() & 0xF];
    00958   }
    00959
    00960   SetupFarmFieldFence(tile - TileDiffXY(1, 0), size_y, type, AXIS_Y);
    00961   SetupFarmFieldFence(tile - TileDiffXY(0, 1), size_x, type, AXIS_X);
    00962   SetupFarmFieldFence(tile + TileDiffXY(size_x - 1, 0), size_y, type, AXIS_Y);
    00963   SetupFarmFieldFence(tile + TileDiffXY(0, size_y - 1), size_x, type, AXIS_X);
    00964 }
    00965
    00966 void PlantRandomFarmField(const Industry *i)
    00967 {
    00968   int x = i->width  / 2 + Random() % 31 - 16;
    00969   int y = i->height / 2 + Random() % 31 - 16;
    00970
    00971   TileIndex tile = TileAddWrap(i->xy, x, y);
    00972
    00973   if (tile != INVALID_TILE) PlantFarmField(tile, i->index);
    00974 }
    00975
    00982 static bool SearchLumberMillTrees(TileIndex tile, void *user_data)
    00983 {
    00984   if (IsTileType(tile, MP_TREES) && GetTreeGrowth(tile) > 2) {
    00985     CompanyID old_company = _current_company;
    00986     /* found a tree */
    00987
    00988     _current_company = OWNER_NONE;
    00989     _industry_sound_ctr = 1;
    00990     _industry_sound_tile = tile;
    00991     SndPlayTileFx(SND_38_CHAINSAW, tile);
    00992
    00993     DoCommand(tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR);
    00994
    00995     _current_company = old_company;
    00996     return true;
    00997   }
    00998   return false;
    00999 }
    01000
    01005 static void ChopLumberMillTrees(Industry *i)
    01006 {
    01007   TileIndex tile = i->xy;
    01008
    01009   if (!IsIndustryCompleted(tile)) return;
    01010
    01011   if (CircularTileSearch(&tile, 40, SearchLumberMillTrees, NULL))
    01012     i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + 45);
    01013 }
    01014
    01015 static void ProduceIndustryGoods(Industry *i)
    01016 {
    01017   uint32 r;
    01018   uint num;
    01019   const IndustrySpec *indsp = GetIndustrySpec(i->type);
    01020
    01021   /* play a sound? */
    01022   if ((i->counter & 0x3F) == 0) {
    01023     if (Chance16R(1, 14, r) && (num = indsp->number_of_sounds) != 0) {
    01024       SndPlayTileFx(
    01025         (SoundFx)(indsp->random_sounds[((r >> 16) * num) >> 16]),
    01026         i->xy);
    01027     }
    01028   }
    01029
    01030   i->counter--;
    01031
    01032   /* produce some cargo */
    01033   if ((i->counter & 0xFF) == 0) {
    01034     if (HasBit(indsp->callback_flags, CBM_IND_PRODUCTION_256_TICKS)) IndustryProductionCallback(i, 1);
    01035
    01036     IndustryBehaviour indbehav = indsp->behaviour;
    01037     i->produced_cargo_waiting[0] = min(0xffff, i->produced_cargo_waiting[0] + i->production_rate[0]);
    01038     i->produced_cargo_waiting[1] = min(0xffff, i->produced_cargo_waiting[1] + i->production_rate[1]);
    01039
    01040     if ((indbehav & INDUSTRYBEH_PLANT_FIELDS) != 0) {
    01041       bool plant;
    01042       if (HasBit(indsp->callback_flags, CBM_IND_SPECIAL_EFFECT)) {
    01043         plant = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, Random(), 0, i, i->type, i->xy) != 0);
    01044       } else {
    01045         plant = Chance16(1, 8);
    01046       }
    01047
    01048       if (plant) PlantRandomFarmField(i);
    01049     }
    01050     if ((indbehav & INDUSTRYBEH_CUT_TREES) != 0) {
    01051       bool cut = ((i->counter & 0x1FF) == 0);
    01052       if (HasBit(indsp->callback_flags, CBM_IND_SPECIAL_EFFECT)) {
    01053         cut = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, 0, 1, i, i->type, i->xy) != 0);
    01054       }
    01055
    01056       if (cut) ChopLumberMillTrees(i);
    01057     }
    01058
    01059     TriggerIndustry(i, INDUSTRY_TRIGGER_INDUSTRY_TICK);
    01060     StartStopIndustryTileAnimation(i, IAT_INDUSTRY_TICK);
    01061   }
    01062 }
    01063
    01064 void OnTick_Industry()
    01065 {
    01066   Industry *i;
    01067
    01068   if (_industry_sound_ctr != 0) {
    01069     _industry_sound_ctr++;
    01070
    01071     if (_industry_sound_ctr == 75) {
    01072       SndPlayTileFx(SND_37_BALLOON_SQUEAK, _industry_sound_tile);
    01073     } else if (_industry_sound_ctr == 160) {
    01074       _industry_sound_ctr = 0;
    01075       SndPlayTileFx(SND_36_CARTOON_CRASH, _industry_sound_tile);
    01076     }
    01077   }
    01078
    01079   if (_game_mode == GM_EDITOR) return;
    01080
    01081   FOR_ALL_INDUSTRIES(i) {
    01082     ProduceIndustryGoods(i);
    01083   }
    01084 }
    01085
    01086 static bool CheckNewIndustry_NULL(TileIndex tile)
    01087 {
    01088   return true;
    01089 }
    01090
    01091 static bool CheckNewIndustry_Forest(TileIndex tile)
    01092 {
    01093   if (_settings_game.game_creation.landscape == LT_ARCTIC) {
    01094     if (GetTileZ(tile) < HighestSnowLine() + TILE_HEIGHT * 2U) {
    01095       _error_message = STR_4831_FOREST_CAN_ONLY_BE_PLANTED;
    01096       return false;
    01097     }
    01098   }
    01099   return true;
    01100 }
    01101
    01102 static bool CheckNewIndustry_OilRefinery(TileIndex tile)
    01103 {
    01104   if (_game_mode == GM_EDITOR) return true;
    01105   if (DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return true;
    01106
    01107   _error_message = STR_483B_CAN_ONLY_BE_POSITIONED;
    01108   return false;
    01109 }
    01110
    01111 extern bool _ignore_restrictions;
    01112
    01113 static bool CheckNewIndustry_OilRig(TileIndex tile)
    01114 {
    01115   if (_game_mode == GM_EDITOR && _ignore_restrictions) return true;
    01116   if (TileHeight(tile) == 0 &&
    01117       DistanceFromEdge(TILE_ADDXY(tile, 1, 1)) < _settings_game.game_creation.oil_refinery_limit) return true;
    01118
    01119   _error_message = STR_483B_CAN_ONLY_BE_POSITIONED;
    01120   return false;
    01121 }
    01122
    01123 static bool CheckNewIndustry_Farm(TileIndex tile)
    01124 {
    01125   if (_settings_game.game_creation.landscape == LT_ARCTIC) {
    01126     if (GetTileZ(tile) + TILE_HEIGHT * 2 >= HighestSnowLine()) {
    01127       _error_message = STR_0239_SITE_UNSUITABLE;
    01128       return false;
    01129     }
    01130   }
    01131   return true;
    01132 }
    01133
    01134 static bool CheckNewIndustry_Plantation(TileIndex tile)
    01135 {
    01136   if (GetTropicZone(tile) == TROPICZONE_DESERT) {
    01137     _error_message = STR_0239_SITE_UNSUITABLE;
    01138     return false;
    01139   }
    01140
    01141   return true;
    01142 }
    01143
    01144 static bool CheckNewIndustry_Water(TileIndex tile)
    01145 {
    01146   if (GetTropicZone(tile) != TROPICZONE_DESERT) {
    01147     _error_message = STR_0318_CAN_ONLY_BE_BUILT_IN_DESERT;
    01148     return false;
    01149   }
    01150
    01151   return true;
    01152 }
    01153
    01154 static bool CheckNewIndustry_Lumbermill(TileIndex tile)
    01155 {
    01156   if (GetTropicZone(tile) != TROPICZONE_RAINFOREST) {
    01157     _error_message = STR_0317_CAN_ONLY_BE_BUILT_IN_RAINFOREST;
    01158     return false;
    01159   }
    01160   return true;
    01161 }
    01162
    01163 static bool CheckNewIndustry_BubbleGen(TileIndex tile)
    01164 {
    01165   return GetTileZ(tile) <= TILE_HEIGHT * 4;
    01166 }
    01167
    01168 typedef bool CheckNewIndustryProc(TileIndex tile);
    01169 static CheckNewIndustryProc * const _check_new_industry_procs[CHECK_END] = {
    01170   CheckNewIndustry_NULL,
    01171   CheckNewIndustry_Forest,
    01172   CheckNewIndustry_OilRefinery,
    01173   CheckNewIndustry_Farm,
    01174   CheckNewIndustry_Plantation,
    01175   CheckNewIndustry_Water,
    01176   CheckNewIndustry_Lumbermill,
    01177   CheckNewIndustry_BubbleGen,
    01178   CheckNewIndustry_OilRig
    01179 };
    01180
    01181 static const Town *CheckMultipleIndustryInTown(TileIndex tile, int type)
    01182 {
    01183   const Town *t;
    01184   const Industry *i;
    01185
    01186   t = ClosestTownFromTile(tile, UINT_MAX);
    01187
    01188   if (_settings_game.economy.multiple_industry_per_town) return t;
    01189
    01190   FOR_ALL_INDUSTRIES(i) {
    01191     if (i->type == (byte)type &&
    01192         i->town == t) {
    01193       _error_message = STR_0287_ONLY_ONE_ALLOWED_PER_TOWN;
    01194       return NULL;
    01195     }
    01196   }
    01197
    01198   return t;
    01199 }
    01200
    01201 bool IsSlopeRefused(Slope current, Slope refused)
    01202 {
    01203   if (IsSteepSlope(current)) return true;
    01204   if (current != SLOPE_FLAT) {
    01205     if (IsSteepSlope(refused)) return true;
    01206
    01207     Slope t = ComplementSlope(current);
    01208
    01209     if (refused & SLOPE_W && (t & SLOPE_NW)) return true;
    01210     if (refused & SLOPE_S && (t & SLOPE_NE)) return true;
    01211     if (refused & SLOPE_E && (t & SLOPE_SW)) return true;
    01212     if (refused & SLOPE_N && (t & SLOPE_SE)) return true;
    01213   }
    01214
    01215   return false;
    01216 }
    01217
    01218 static bool CheckIfIndustryTilesAreFree(TileIndex tile, const IndustryTileTable *it, uint itspec_index, int type, bool *custom_shape_check = NULL)
    01219 {
    01220   _error_message = STR_0239_SITE_UNSUITABLE;
    01221   bool refused_slope = false;
    01222   bool custom_shape = false;
    01223
    01224   do {
    01225     IndustryGfx gfx = GetTranslatedIndustryTileID(it->gfx);
    01226     if (TileX(tile) + it->ti.x >= MapSizeX()) return false;
    01227     if (TileY(tile) + it->ti.y >= MapSizeY()) return false;
    01228     TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
    01229
    01230     if (!IsValidTile(cur_tile)) {
    01231       if (gfx == GFX_WATERTILE_SPECIALCHECK) continue;
    01232       return false;
    01233     }
    01234
    01235     if (gfx == GFX_WATERTILE_SPECIALCHECK) {
    01236       if (!IsTileType(cur_tile, MP_WATER) ||
    01237           GetTileSlope(cur_tile, NULL) != SLOPE_FLAT) {
    01238         return false;
    01239       }
    01240     } else {
    01241       if (!EnsureNoVehicleOnGround(cur_tile)) return false;
    01242       if (MayHaveBridgeAbove(cur_tile) && IsBridgeAbove(cur_tile)) return false;
    01243
    01244       const IndustryTileSpec *its = GetIndustryTileSpec(gfx);
    01245
    01246       IndustryBehaviour ind_behav = GetIndustrySpec(type)->behaviour;
    01247
    01248       /* Perform land/water check if not disabled */
    01249       if (!HasBit(its->slopes_refused, 5) && (IsWaterTile(cur_tile) == !(ind_behav & INDUSTRYBEH_BUILT_ONWATER))) return false;
    01250
    01251       if (HasBit(its->callback_flags, CBM_INDT_SHAPE_CHECK)) {
    01252         custom_shape = true;
    01253         if (!PerformIndustryTileSlopeCheck(tile, cur_tile, its, type, gfx, itspec_index)) return false;
    01254       } else {
    01255         Slope tileh = GetTileSlope(cur_tile, NULL);
    01256         refused_slope |= IsSlopeRefused(tileh, its->slopes_refused);
    01257       }
    01258
    01259       if ((ind_behav & (INDUSTRYBEH_ONLY_INTOWN | INDUSTRYBEH_TOWN1200_MORE)) || // Tile must be a house
    01260           ((ind_behav & INDUSTRYBEH_ONLY_NEARTOWN) && IsTileType(cur_tile, MP_HOUSE))) { // Tile is allowed to be a house (and it is a house)
    01261         if (!IsTileType(cur_tile, MP_HOUSE)) {
    01262           _error_message = STR_030D_CAN_ONLY_BE_BUILT_IN_TOWNS;
    01263           return false;
    01264         }
    01265
    01266         /* Clear the tiles as OWNER_TOWN to not affect town rating, and to not clear protected buildings */
    01267         CompanyID old_company = _current_company;
    01268         _current_company = OWNER_TOWN;
    01269         bool not_clearable = CmdFailed(DoCommand(cur_tile, 0, 0, DC_NONE, CMD_LANDSCAPE_CLEAR));
    01270         _current_company = old_company;
    01271
    01272         if (not_clearable) return false;
    01273       } else {
    01274         /* Clear the tiles, but do not affect town ratings */
    01275         bool not_clearable = CmdFailed(DoCommand(cur_tile, 0, 0, DC_AUTO | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR));
    01276
    01277         if (not_clearable) return false;
    01278       }
    01279     }
    01280   } while ((++it)->ti.x != -0x80);
    01281
    01282   if (custom_shape_check != NULL) *custom_shape_check = custom_shape;
    01283
    01284   /* It is almost impossible to have a fully flat land in TG, so what we
    01285    *  do is that we check if we can make the land flat later on. See
    01286    *  CheckIfCanLevelIndustryPlatform(). */
    01287   return !refused_slope || (_settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !custom_shape && !_ignore_restrictions);
    01288 }
    01289
    01290 static bool CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t)
    01291 {
    01292   if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_TOWN1200_MORE) && t->population < 1200) {
    01293     _error_message = STR_029D_CAN_ONLY_BE_BUILT_IN_TOWNS;
    01294     return false;
    01295   }
    01296
    01297   if ((GetIndustrySpec(type)->behaviour & INDUSTRYBEH_ONLY_NEARTOWN) && DistanceMax(t->xy, tile) > 9) {
    01298     _error_message = STR_0239_SITE_UNSUITABLE;
    01299     return false;
    01300   }
    01301
    01302   return true;
    01303 }
    01304
    01305 static bool CheckCanTerraformSurroundingTiles(TileIndex tile, uint height, int internal)
    01306 {
    01307   int size_x, size_y;
    01308   uint curh;
    01309
    01310   size_x = 2;
    01311   size_y = 2;
    01312
    01313   /* Check if we don't leave the map */
    01314   if (TileX(tile) == 0 || TileY(tile) == 0 || GetTileType(tile) == MP_VOID) return false;
    01315
    01316   tile += TileDiffXY(-1, -1);
    01317   BEGIN_TILE_LOOP(tile_walk, size_x, size_y, tile) {
    01318     curh = TileHeight(tile_walk);
    01319     /* Is the tile clear? */
    01320     if ((GetTileType(tile_walk) != MP_CLEAR) && (GetTileType(tile_walk) != MP_TREES))
    01321       return false;
    01322
    01323     /* Don't allow too big of a change if this is the sub-tile check */
    01324     if (internal != 0 && Delta(curh, height) > 1) return false;
    01325
    01326     /* Different height, so the surrounding tiles of this tile
    01327      *  has to be correct too (in level, or almost in level)
    01328      *  else you get a chain-reaction of terraforming. */
    01329     if (internal == 0 && curh != height) {
    01330       if (TileX(tile_walk) == 0 || TileY(tile_walk) == 0 || !CheckCanTerraformSurroundingTiles(tile_walk + TileDiffXY(-1, -1), height, internal + 1))
    01331         return false;
    01332     }
    01333   } END_TILE_LOOP(tile_walk, size_x, size_y, tile);
    01334
    01335   return true;
    01336 }
    01337
    01342 static bool CheckIfCanLevelIndustryPlatform(TileIndex tile, DoCommandFlag flags, const IndustryTileTable *it, int type)
    01343 {
    01344   const int MKEND = -0x80;   // used for last element in an IndustryTileTable (see build_industry.h)
    01345   int max_x = 0;
    01346   int max_y = 0;
    01347   TileIndex cur_tile;
    01348   uint size_x, size_y;
    01349   uint h, curh;
    01350
    01351   /* Finds dimensions of largest variant of this industry */
    01352   do {
    01353     if (it->gfx == 0xFF) continue;  //  FF been a marquer for a check on clear water, skip it
    01354     if (it->ti.x > max_x) max_x = it->ti.x;
    01355     if (it->ti.y > max_y) max_y = it->ti.y;
    01356   } while ((++it)->ti.x != MKEND);
    01357
    01358   /* Remember level height */
    01359   h = TileHeight(tile);
    01360
    01361   if (TileX(tile) <= 1 || TileY(tile) <= 1) return false;
    01362   /* Check that all tiles in area and surrounding are clear
    01363    * this determines that there are no obstructing items */
    01364   cur_tile = tile + TileDiffXY(-1, -1);
    01365   size_x = max_x + 4;
    01366   size_y = max_y + 4;
    01367
    01368   /* Check if we don't leave the map */
    01369   if (TileX(cur_tile) + size_x >= MapMaxX() || TileY(cur_tile) + size_y >= MapMaxY()) return false;
    01370
    01371   /* _current_company is OWNER_NONE for randomly generated industries and in editor, or the company who funded or prospected the industry.
    01372    * Perform terraforming as OWNER_TOWN to disable autoslope and town ratings. */
    01373   CompanyID old_company = _current_company;
    01374   _current_company = OWNER_TOWN;
    01375
    01376   BEGIN_TILE_LOOP(tile_walk, size_x, size_y, cur_tile) {
    01377     curh = TileHeight(tile_walk);
    01378     if (curh != h) {
    01379       /* This tile needs terraforming. Check if we can do that without
    01380        *  damaging the surroundings too much. */
    01381       if (!CheckCanTerraformSurroundingTiles(tile_walk, h, 0)) {
    01382         _current_company = old_company;
    01383         return false;
    01384       }
    01385       /* This is not 100% correct check, but the best we can do without modifying the map.
    01386        *  What is missing, is if the difference in height is more than 1.. */
    01387       if (CmdFailed(DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags & ~DC_EXEC, CMD_TERRAFORM_LAND))) {
    01388         _current_company = old_company;
    01389         return false;
    01390       }
    01391     }
    01392   } END_TILE_LOOP(tile_walk, size_x, size_y, cur_tile)
    01393
    01394   if (flags & DC_EXEC) {
    01395     /* Terraform the land under the industry */
    01396     BEGIN_TILE_LOOP(tile_walk, size_x, size_y, cur_tile) {
    01397       curh = TileHeight(tile_walk);
    01398       while (curh != h) {
    01399         /* We give the terraforming for free here, because we can't calculate
    01400          *  exact cost in the test-round, and as we all know, that will cause
    01401          *  a nice assert if they don't match ;) */
    01402         DoCommand(tile_walk, SLOPE_N, (curh > h) ? 0 : 1, flags, CMD_TERRAFORM_LAND);
    01403         curh += (curh > h) ? -1 : 1;
    01404       }
    01405     } END_TILE_LOOP(tile_walk, size_x, size_y, cur_tile)
    01406   }
    01407
    01408   _current_company = old_company;
    01409   return true;
    01410 }
    01411
    01412
    01413 static bool CheckIfFarEnoughFromIndustry(TileIndex tile, int type)
    01414 {
    01415   const IndustrySpec *indspec = GetIndustrySpec(type);
    01416   const Industry *i;
    01417
    01418   if (_settings_game.economy.same_industry_close && indspec->IsRawIndustry())
    01419     /* Allow primary industries to be placed close to any other industry */
    01420     return true;
    01421
    01422   FOR_ALL_INDUSTRIES(i) {
    01423     /* Within 14 tiles from another industry is considered close */
    01424     bool in_low_distance = DistanceMax(tile, i->xy) <= 14;
    01425
    01426     /* check if an industry that accepts the same goods is nearby */
    01427     if (in_low_distance &&
    01428         !indspec->IsRawIndustry() && // not a primary industry?
    01429         indspec->accepts_cargo[0] == i->accepts_cargo[0] && (
    01430         /* at least one of those options must be true */
    01431         _game_mode != GM_EDITOR || // editor must not be stopped
    01432         !_settings_game.economy.same_industry_close ||
    01433         !_settings_game.economy.multiple_industry_per_town)) {
    01434       _error_message = STR_INDUSTRY_TOO_CLOSE;
    01435       return false;
    01436     }
    01437
    01438     /* check if there are any conflicting industry types around */
    01439     if ((i->type == indspec->conflicting[0] ||
    01440         i->type == indspec->conflicting[1] ||
    01441         i->type == indspec->conflicting[2]) &&
    01442         in_low_distance) {
    01443       _error_message = STR_INDUSTRY_TOO_CLOSE;
    01444       return false;
    01445     }
    01446   }
    01447   return true;
    01448 }
    01449
    01453 enum ProductionLevels {
    01454   PRODLEVEL_CLOSURE = 0x00,
    01455   PRODLEVEL_MINIMUM = 0x04,
    01456   PRODLEVEL_DEFAULT = 0x10,
    01457   PRODLEVEL_MAXIMUM = 0x80,
    01458 };
    01459
    01460 static void DoCreateNewIndustry(Industry *i, TileIndex tile, int type, const IndustryTileTable *it, byte layout, const Town *t, Owner owner, Owner founder)
    01461 {
    01462   const IndustrySpec *indspec = GetIndustrySpec(type);
    01463   uint32 r;
    01464   uint j;
    01465
    01466   i->xy = tile;
    01467   i->width = i->height = 0;
    01468   i->type = type;
    01469   IncIndustryTypeCount(type);
    01470
    01471   i->produced_cargo[0] = indspec->produced_cargo[0];
    01472   i->produced_cargo[1] = indspec->produced_cargo[1];
    01473   i->accepts_cargo[0] = indspec->accepts_cargo[0];
    01474   i->accepts_cargo[1] = indspec->accepts_cargo[1];
    01475   i->accepts_cargo[2] = indspec->accepts_cargo[2];
    01476   i->production_rate[0] = indspec->production_rate[0];
    01477   i->production_rate[1] = indspec->production_rate[1];
    01478
    01479   /* don't use smooth economy for industries using production related callbacks */
    01480   if (_settings_game.economy.smooth_economy &&
    01481       !(HasBit(indspec->callback_flags, CBM_IND_PRODUCTION_256_TICKS) || HasBit(indspec->callback_flags, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) && // production callbacks
    01482       !(HasBit(indspec->callback_flags, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(indspec->callback_flags, CBM_IND_PRODUCTION_CHANGE))             // production change callbacks
    01483   ) {
    01484     i->production_rate[0] = min((RandomRange(256) + 128) * i->production_rate[0] >> 8 , 255);
    01485     i->production_rate[1] = min((RandomRange(256) + 128) * i->production_rate[1] >> 8 , 255);
    01486   }
    01487
    01488   i->town = t;
    01489   i->owner = owner;
    01490
    01491   r = Random();
    01492   i->random_colour = GB(r, 0, 4);
    01493   i->counter = GB(r, 4, 12);
    01494   i->random = GB(r, 16, 16);
    01495   i->produced_cargo_waiting[0] = 0;
    01496   i->produced_cargo_waiting[1] = 0;
    01497   i->incoming_cargo_waiting[0] = 0;
    01498   i->incoming_cargo_waiting[1] = 0;
    01499   i->incoming_cargo_waiting[2] = 0;
    01500   i->this_month_production[0] = 0;
    01501   i->this_month_production[1] = 0;
    01502   i->this_month_transported[0] = 0;
    01503   i->this_month_transported[1] = 0;
    01504   i->last_month_pct_transported[0] = 0;
    01505   i->last_month_pct_transported[1] = 0;
    01506   i->last_month_transported[0] = 0;
    01507   i->last_month_transported[1] = 0;
    01508   i->was_cargo_delivered = false;
    01509   i->last_prod_year = _cur_year;
    01510   i->last_month_production[0] = i->production_rate[0] * 8;
    01511   i->last_month_production[1] = i->production_rate[1] * 8;
    01512   i->founder = founder;
    01513
    01514   if (HasBit(indspec->callback_flags, CBM_IND_DECIDE_COLOUR)) {
    01515     uint16 res = GetIndustryCallback(CBID_INDUSTRY_DECIDE_COLOUR, 0, 0, i, type, INVALID_TILE);
    01516     if (res != CALLBACK_FAILED) i->random_colour = GB(res, 0, 4);
    01517   }
    01518
    01519   if (HasBit(indspec->callback_flags, CBM_IND_INPUT_CARGO_TYPES)) {
    01520     for (j = 0; j < lengthof(i->accepts_cargo); j++) i->accepts_cargo[j] = CT_INVALID;
    01521     for (j = 0; j < lengthof(i->accepts_cargo); j++) {
    01522       uint16 res = GetIndustryCallback(CBID_INDUSTRY_INPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
    01523       if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
    01524       i->accepts_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
    01525     }
    01526   }
    01527
    01528   if (HasBit(indspec->callback_flags, CBM_IND_OUTPUT_CARGO_TYPES)) {
    01529     for (j = 0; j < lengthof(i->produced_cargo); j++) i->produced_cargo[j] = CT_INVALID;
    01530     for (j = 0; j < lengthof(i->produced_cargo); j++) {
    01531       uint16 res = GetIndustryCallback(CBID_INDUSTRY_OUTPUT_CARGO_TYPES, j, 0, i, type, INVALID_TILE);
    01532       if (res == CALLBACK_FAILED || GB(res, 0, 8) == CT_INVALID) break;
    01533       i->produced_cargo[j] = GetCargoTranslation(GB(res, 0, 8), indspec->grf_prop.grffile);
    01534     }
    01535   }
    01536
    01537   i->construction_date = _date;
    01538   i->construction_type = (_game_mode == GM_EDITOR) ? ICT_SCENARIO_EDITOR :
    01539       (_generating_world ? ICT_MAP_GENERATION : ICT_NORMAL_GAMEPLAY);
    01540
    01541   /* Adding 1 here makes it conform to specs of var44 of varaction2 for industries
    01542    * 0 = created prior of newindustries
    01543    * else, chosen layout + 1 */
    01544   i->selected_layout = layout + 1;
    01545
    01546   if (!_generating_world) i->last_month_production[0] = i->last_month_production[1] = 0;
    01547
    01548   i->prod_level = PRODLEVEL_DEFAULT;
    01549
    01550   do {
    01551     TileIndex cur_tile = tile + ToTileIndexDiff(it->ti);
    01552
    01553     if (it->gfx != GFX_WATERTILE_SPECIALCHECK) {
    01554       byte size;
    01555
    01556       size = it->ti.x;
    01557       if (size > i->width) i->width = size;
    01558       size = it->ti.y;
    01559       if (size > i->height)i->height = size;
    01560
    01561       WaterClass wc = (IsWaterTile(cur_tile) ? GetWaterClass(cur_tile) : WATER_CLASS_INVALID);
    01562
    01563       DoCommand(cur_tile, 0, 0, DC_EXEC | DC_NO_TEST_TOWN_RATING | DC_NO_MODIFY_TOWN_RATING, CMD_LANDSCAPE_CLEAR);
    01564
    01565       MakeIndustry(cur_tile, i->index, it->gfx, Random(), wc);
    01566
    01567       if (_generating_world) {
    01568         SetIndustryConstructionCounter(cur_tile, 3);
    01569         SetIndustryConstructionStage(cur_tile, 2);
    01570       }
    01571
    01572       /* it->gfx is stored in the map. But the translated ID cur_gfx is the interesting one */
    01573       IndustryGfx cur_gfx = GetTranslatedIndustryTileID(it->gfx);
    01574       const IndustryTileSpec *its = GetIndustryTileSpec(cur_gfx);
    01575       if (its->animation_info != 0xFFFF) AddAnimatedTile(cur_tile);
    01576     }
    01577   } while ((++it)->ti.x != -0x80);
    01578
    01579   i->width++;
    01580   i->height++;
    01581
    01582   if (GetIndustrySpec(i->type)->behaviour & INDUSTRYBEH_PLANT_ON_BUILT) {
    01583     for (j = 0; j != 50; j++) PlantRandomFarmField(i);
    01584   }
    01585   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 0);
    01586 }
    01587
    01598 static Industry *CreateNewIndustryHelper(TileIndex tile, IndustryType type, DoCommandFlag flags, const IndustrySpec *indspec, uint itspec_index, uint32 seed, Owner founder)
    01599 {
    01600   assert(itspec_index < indspec->num_table);
    01601   const IndustryTileTable *it = indspec->table[itspec_index];
    01602   bool custom_shape_check = false;
    01603
    01604   if (!CheckIfIndustryTilesAreFree(tile, it, itspec_index, type, &custom_shape_check)) return NULL;
    01605
    01606   if (HasBit(GetIndustrySpec(type)->callback_flags, CBM_IND_LOCATION)) {
    01607     if (!CheckIfCallBackAllowsCreation(tile, type, itspec_index, seed)) return NULL;
    01608   } else {
    01609     if (!_check_new_industry_procs[indspec->check_proc](tile)) return NULL;
    01610   }
    01611
    01612   if (!custom_shape_check && _settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !_ignore_restrictions && !CheckIfCanLevelIndustryPlatform(tile, DC_NONE, it, type)) return NULL;
    01613   if (!CheckIfFarEnoughFromIndustry(tile, type)) return NULL;
    01614
    01615   const Town *t = CheckMultipleIndustryInTown(tile, type);
    01616   if (t == NULL) return NULL;
    01617
    01618   if (!CheckIfIndustryIsAllowed(tile, type, t)) return NULL;
    01619
    01620   if (!Industry::CanAllocateItem()) return NULL;
    01621
    01622   if (flags & DC_EXEC) {
    01623     Industry *i = new Industry(tile);
    01624     if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_EXEC, it, type);
    01625     DoCreateNewIndustry(i, tile, type, it, itspec_index, t, OWNER_NONE, founder);
    01626
    01627     return i;
    01628   }
    01629
    01630   /* We need to return a non-NULL pointer to tell we have created an industry.
    01631    * However, we haven't created a real one (no DC_EXEC), so return a fake one. */
    01632   return GetIndustry(0);
    01633 }
    01634
    01644 CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01645 {
    01646   IndustryType it = GB(p1, 0, 16);
    01647   if (it >= NUM_INDUSTRYTYPES) return CMD_ERROR;
    01648
    01649   const IndustrySpec *indspec = GetIndustrySpec(it);
    01650   const Industry *ind = NULL;
    01651
    01652   /* Check if the to-be built/founded industry is available for this climate. */
    01653   if (!indspec->enabled || indspec->num_table == 0) return CMD_ERROR;
    01654
    01655   /* If the setting for raw-material industries is not on, you cannot build raw-material industries.
    01656    * Raw material industries are industries that do not accept cargo (at least for now) */
    01657   if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
    01658     return CMD_ERROR;
    01659   }
    01660
    01661   if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indspec->IsRawIndustry()) {
    01662     if (flags & DC_EXEC) {
    01663       /* Prospected industries are build as OWNER_TOWN to not e.g. be build on owned land of the founder */
    01664       CompanyID founder = _current_company;
    01665       _current_company = OWNER_TOWN;
    01666       /* Prospecting has a chance to fail, however we cannot guarantee that something can
    01667        * be built on the map, so the chance gets lower when the map is fuller, but there
    01668        * is nothing we can really do about that. */
    01669       if (Random() <= indspec->prospecting_chance) {
    01670         for (int i = 0; i < 5000; i++) {
    01671           /* We should not have more than one Random() in a function call
    01672            * because parameter evaluation order is not guaranteed in the c++ standard
    01673            */
    01674           tile = RandomTile();
    01675           ind = CreateNewIndustryHelper(tile, p1, flags, indspec, RandomRange(indspec->num_table), p2, founder);
    01676           if (ind != NULL) {
    01677             break;
    01678           }
    01679         }
    01680       }
    01681       _current_company = founder;
    01682     }
    01683   } else {
    01684     int count = indspec->num_table;
    01685     const IndustryTileTable * const *itt = indspec->table;
    01686     int num = GB(p1, 16, 16);
    01687     if (num >= count) return CMD_ERROR;
    01688
    01689     _error_message = STR_0239_SITE_UNSUITABLE;
    01690     do {
    01691       if (--count < 0) return CMD_ERROR;
    01692       if (--num < 0) num = indspec->num_table - 1;
    01693     } while (!CheckIfIndustryTilesAreFree(tile, itt[num], num, p1));
    01694
    01695     ind = CreateNewIndustryHelper(tile, p1, flags, indspec, num, p2, _current_company);
    01696     if (ind == NULL) return CMD_ERROR;
    01697   }
    01698
    01699   if (flags & DC_EXEC && _game_mode != GM_EDITOR && ind != NULL) {
    01700     SetDParam(0, indspec->name);
    01701     if (indspec->new_industry_text > STR_LAST_STRINGID) {
    01702       SetDParam(1, STR_TOWN);
    01703       SetDParam(2, ind->town->index);
    01704     } else {
    01705       SetDParam(1, ind->town->index);
    01706     }
    01707     AddNewsItem(indspec->new_industry_text, NS_INDUSTRY_OPEN, ind->xy, 0);
    01708     AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
    01709   }
    01710
    01711   return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost());
    01712 }
    01713
    01714
    01715 static Industry *CreateNewIndustry(TileIndex tile, IndustryType type)
    01716 {
    01717   const IndustrySpec *indspec = GetIndustrySpec(type);
    01718
    01719   uint32 seed = Random();
    01720   return CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, RandomRange(indspec->num_table), seed, OWNER_NONE);
    01721 }
    01722
    01723 enum {
    01724   NB_NUMOFINDUSTRY = 11,
    01725   NB_DIFFICULTY_LEVEL = 5,
    01726 };
    01727
    01728 static const byte _numof_industry_table[NB_DIFFICULTY_LEVEL][NB_NUMOFINDUSTRY] = {
    01729   /* difficulty settings for number of industries */
    01730   {0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0},   // none
    01731   {0, 1, 1, 1, 1, 1, 1, 1,  1,  1,  1},   // very low
    01732   {0, 1, 1, 1, 2, 2, 3, 3,  4,  4,  5},   // low
    01733   {0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10},   // normal
    01734   {0, 2, 3, 4, 6, 7, 8, 9, 10, 10, 10},   // high
    01735 };
    01736
    01741 static void PlaceInitialIndustry(IndustryType type, int amount)
    01742 {
    01743   /* We need to bypass the amount given in parameter if it exceeds the maximum dimension of the
    01744    * _numof_industry_table.  newgrf can specify a big amount */
    01745   int num = (amount > NB_NUMOFINDUSTRY) ? amount : _numof_industry_table[_settings_game.difficulty.number_industries][amount];
    01746   const IndustrySpec *ind_spc = GetIndustrySpec(type);
    01747
    01748   /* These are always placed next to the coastline, so we scale by the perimeter instead. */
    01749   num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
    01750
    01751   if (_settings_game.difficulty.number_industries != 0) {
    01752     CompanyID old_company = _current_company;
    01753     _current_company = OWNER_NONE;
    01754     assert(num > 0);
    01755
    01756     do {
    01757       uint i;
    01758
    01759       IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
    01760
    01761       for (i = 0; i < 2000; i++) {
    01762         if (CreateNewIndustry(RandomTile(), type) != NULL) break;
    01763       }
    01764     } while (--num);
    01765
    01766     _current_company = old_company;
    01767   }
    01768 }
    01769
    01772 void GenerateIndustries()
    01773 {
    01774   uint i = 0;
    01775   uint8 chance;
    01776   IndustryType it;
    01777   const IndustrySpec *ind_spc;
    01778
    01779   /* Find the total amount of industries */
    01780   if (_settings_game.difficulty.number_industries > 0) {
    01781     for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
    01782
    01783       ind_spc = GetIndustrySpec(it);
    01784
    01785       if (!CheckIfCallBackAllowsAvailability(it, IACT_MAPGENERATION)) {
    01786         ResetIndustryCreationProbility(it);
    01787       }
    01788
    01789       chance = ind_spc->appear_creation[_settings_game.game_creation.landscape];
    01790       if (ind_spc->enabled && chance > 0 && ind_spc->num_table > 0) {
    01791         /* once the chance of appearance is determind, it have to be scaled by
    01792          * the difficulty level. The "chance" in question is more an index into
    01793          * the _numof_industry_table,in fact */
    01794         int num = (chance > NB_NUMOFINDUSTRY) ? chance : _numof_industry_table[_settings_game.difficulty.number_industries][chance];
    01795
    01796         /* These are always placed next to the coastline, so we scale by the perimeter instead. */
    01797         num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
    01798         i += num;
    01799       }
    01800     }
    01801   }
    01802
    01803   SetGeneratingWorldProgress(GWP_INDUSTRY, i);
    01804
    01805   if (_settings_game.difficulty.number_industries > 0) {
    01806     for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
    01807       /* Once the number of industries has been determined, let's really create them.
    01808        * The test for chance allows us to try create industries that are available only
    01809        * for this landscape.
    01810        * @todo :  Do we really have to pass chance as un-scaled value, since we've already
    01811        *          processed that scaling above? No, don't think so.  Will find a way. */
    01812       ind_spc = GetIndustrySpec(it);
    01813       if (ind_spc->enabled && ind_spc->num_table > 0) {
    01814         chance = ind_spc->appear_creation[_settings_game.game_creation.landscape];
    01815         if (chance > 0) PlaceInitialIndustry(it, chance);
    01816       }
    01817     }
    01818   }
    01819 }
    01820
    01821 static void UpdateIndustryStatistics(Industry *i)
    01822 {
    01823   byte pct;
    01824   bool refresh = false;
    01825
    01826   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    01827     if (i->produced_cargo[j] != CT_INVALID) {
    01828       pct = 0;
    01829       if (i->this_month_production[j] != 0) {
    01830         i->last_prod_year = _cur_year;
    01831         pct = min(i->this_month_transported[j] * 256 / i->this_month_production[j], 255);
    01832       }
    01833       i->last_month_pct_transported[j] = pct;
    01834
    01835       i->last_month_production[j] = i->this_month_production[j];
    01836       i->this_month_production[j] = 0;
    01837
    01838       i->last_month_transported[j] = i->this_month_transported[j];
    01839       i->this_month_transported[j] = 0;
    01840       refresh = true;
    01841     }
    01842   }
    01843
    01844   if (refresh) InvalidateWindow(WC_INDUSTRY_VIEW, i->index);
    01845 }
    01846
    01848 struct ProbabilityHelper {
    01849   uint16 prob;
    01850   IndustryType ind;
    01851 };
    01852
    01856 static void MaybeNewIndustry(void)
    01857 {
    01858   Industry *ind;               // will receive the industry's creation pointer
    01859   IndustryType rndtype, j;     // Loop controlers
    01860   const IndustrySpec *ind_spc;
    01861   uint num = 0;
    01862   ProbabilityHelper cumulative_probs[NUM_INDUSTRYTYPES]; // probability collector
    01863   uint16 probability_max = 0;
    01864
    01865   /* Generate a list of all possible industries that can be built. */
    01866   for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
    01867     ind_spc = GetIndustrySpec(j);
    01868     byte chance = ind_spc->appear_ingame[_settings_game.game_creation.landscape];
    01869
    01870     if (!ind_spc->enabled || chance == 0 || ind_spc->num_table == 0) continue;
    01871
    01872     /* If there is no Callback CBID_INDUSTRY_AVAILABLE or if this one did anot failed,
    01873      * and if appearing chance for this landscape is above 0, this industry can be chosen */
    01874     if (CheckIfCallBackAllowsAvailability(j, IACT_RANDOMCREATION)) {
    01875       probability_max += chance;
    01876       /* adds the result for this industry */
    01877       cumulative_probs[num].ind = j;
    01878       cumulative_probs[num++].prob = probability_max;
    01879     }
    01880   }
    01881
    01882   /* Abort if there is no industry buildable */
    01883   if (probability_max == 0) return;
    01884
    01885   /* Find a random type, with maximum being what has been evaluate above*/
    01886   rndtype = RandomRange(probability_max);
    01887   for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
    01888     /* and choose the index of the industry that matches as close as possible this random type */
    01889     if (cumulative_probs[j].prob >= rndtype) break;
    01890   }
    01891
    01892   ind_spc = GetIndustrySpec(cumulative_probs[j].ind);
    01893   /*  Check if it is allowed */
    01894   if ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) return;
    01895   if ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) return;
    01896
    01897   /* try to create 2000 times this industry */
    01898   num = 2000;
    01899   for (;;) {
    01900     ind = CreateNewIndustry(RandomTile(), cumulative_probs[j].ind);
    01901     if (ind != NULL) break;
    01902     if (--num == 0) return;
    01903   }
    01904
    01905   SetDParam(0, ind_spc->name);
    01906   if (ind_spc->new_industry_text > STR_LAST_STRINGID) {
    01907     SetDParam(1, STR_TOWN);
    01908     SetDParam(2, ind->town->index);
    01909   } else {
    01910     SetDParam(1, ind->town->index);
    01911   }
    01912   AddNewsItem(ind_spc->new_industry_text, NS_INDUSTRY_OPEN, ind->xy, 0);
    01913   AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
    01914 }
    01915
    01924 static bool CheckIndustryCloseDownProtection(IndustryType type)
    01925 {
    01926   const IndustrySpec *indspec = GetIndustrySpec(type);
    01927
    01928   /* oil wells (or the industries with that flag set) are always allowed to closedown */
    01929   if (indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD && _settings_game.game_creation.landscape == LT_TEMPERATE) return false;
    01930   return (indspec->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) == 0 && GetIndustryTypeCount(type) <= 1;
    01931 }
    01932
    01942 static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces)
    01943 {
    01944   const IndustrySpec *indspec = GetIndustrySpec(ind->type);
    01945
    01946   /* Check for acceptance of cargo */
    01947   for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) {
    01948     if (ind->accepts_cargo[j] == CT_INVALID) continue;
    01949     if (cargo == ind->accepts_cargo[j]) {
    01950       if (HasBit(indspec->callback_flags, CBM_IND_REFUSE_CARGO)) {
    01951         uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO,
    01952             0, GetReverseCargoTranslation(cargo, indspec->grf_prop.grffile),
    01953             ind, ind->type, ind->xy);
    01954         if (res == 0) continue;
    01955       }
    01956       *c_accepts = true;
    01957       break;
    01958     }
    01959   }
    01960
    01961   /* Check for produced cargo */
    01962   for (byte j = 0; j < lengthof(ind->produced_cargo); j++) {
    01963     if (ind->produced_cargo[j] == CT_INVALID) continue;
    01964     if (cargo == ind->produced_cargo[j]) {
    01965       *c_produces = true;
    01966       break;
    01967     }
    01968   }
    01969 }
    01970
    01984 int WhoCanServiceIndustry(Industry *ind)
    01985 {
    01986   /* Find all stations within reach of the industry */
    01987   StationList stations;
    01988   FindStationsAroundTiles(ind->xy, ind->width, ind->height, &stations);
    01989
    01990   if (stations.Length() == 0) return 0; // No stations found at all => nobody services
    01991
    01992   const Vehicle *v;
    01993   int result = 0;
    01994   FOR_ALL_VEHICLES(v) {
    01995     /* Is it worthwhile to try this vehicle? */
    01996     if (v->owner != _local_company && result != 0) continue;
    01997
    01998     /* Check whether it accepts the right kind of cargo */
    01999     bool c_accepts = false;
    02000     bool c_produces = false;
    02001     if (v->type == VEH_TRAIN && IsFrontEngine(v)) {
    02002       for (const Vehicle *u = v; u != NULL; u = u->Next()) {
    02003         CanCargoServiceIndustry(u->cargo_type, ind, &c_accepts, &c_produces);
    02004       }
    02005     } else if (v->type == VEH_ROAD || v->type == VEH_SHIP || v->type == VEH_AIRCRAFT) {
    02006       CanCargoServiceIndustry(v->cargo_type, ind, &c_accepts, &c_produces);
    02007     } else {
    02008       continue;
    02009     }
    02010     if (!c_accepts && !c_produces) continue; // Wrong cargo
    02011
    02012     /* Check orders of the vehicle.
    02013      * We cannot check the first of shared orders only, since the first vehicle in such a chain
    02014      * may have a different cargo type.
    02015      */
    02016     const Order *o;
    02017     FOR_VEHICLE_ORDERS(v, o) {
    02018       if (o->IsType(OT_GOTO_STATION) && !(o->GetUnloadType() & OUFB_TRANSFER)) {
    02019         /* Vehicle visits a station to load or unload */
    02020         Station *st = GetStation(o->GetDestination());
    02021         if (!st->IsValid()) continue;
    02022
    02023         /* Same cargo produced by industry is dropped here => not serviced by vehicle v */
    02024         if ((o->GetUnloadType() & OUFB_UNLOAD) && !c_accepts) break;
    02025
    02026         if (stations.Contains(st)) {
    02027           if (v->owner == _local_company) return 2; // Company services industry
    02028           result = 1; // Competitor services industry
    02029         }
    02030       }
    02031     }
    02032   }
    02033   return result;
    02034 }
    02035
    02043 static void ReportNewsProductionChangeIndustry(Industry *ind, CargoID type, int percent)
    02044 {
    02045   NewsSubtype ns;
    02046
    02047   switch (WhoCanServiceIndustry(ind)) {
    02048     case 0: ns = NS_INDUSTRY_NOBODY;  break;
    02049     case 1: ns = NS_INDUSTRY_OTHER;   break;
    02050     case 2: ns = NS_INDUSTRY_COMPANY; break;
    02051     default: NOT_REACHED(); break;
    02052   }
    02053   SetDParam(2, abs(percent));
    02054   SetDParam(0, GetCargo(type)->name);
    02055   SetDParam(1, ind->index);
    02056   AddNewsItem(
    02057     percent >= 0 ? STR_INDUSTRY_PROD_GOUP : STR_INDUSTRY_PROD_GODOWN,
    02058     ns,
    02059     ind->xy + TileDiffXY(1, 1), 0
    02060   );
    02061 }
    02062
    02063 enum {
    02064   PERCENT_TRANSPORTED_60 = 153,
    02065   PERCENT_TRANSPORTED_80 = 204,
    02066 };
    02067
    02072 static void ChangeIndustryProduction(Industry *i, bool monthly)
    02073 {
    02074   StringID str = STR_NULL;
    02075   bool closeit = false;
    02076   const IndustrySpec *indspec = GetIndustrySpec(i->type);
    02077   bool standard = false;
    02078   bool suppress_message = false;
    02079   bool recalculate_multipliers = false;
    02080   /* don't use smooth economy for industries using production related callbacks */
    02081   bool smooth_economy = _settings_game.economy.smooth_economy &&
    02082                         !(HasBit(indspec->callback_flags, CBM_IND_PRODUCTION_256_TICKS) || HasBit(indspec->callback_flags, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) && // production callbacks
    02083                         !(HasBit(indspec->callback_flags, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(indspec->callback_flags, CBM_IND_PRODUCTION_CHANGE));            // production change callbacks
    02084   byte div = 0;
    02085   byte mul = 0;
    02086   int8 increment = 0;
    02087
    02088   bool callback_enabled = HasBit(indspec->callback_flags, monthly ? CBM_IND_MONTHLYPROD_CHANGE : CBM_IND_PRODUCTION_CHANGE);
    02089   if (callback_enabled) {
    02090     uint16 res = GetIndustryCallback(monthly ? CBID_INDUSTRY_MONTHLYPROD_CHANGE : CBID_INDUSTRY_PRODUCTION_CHANGE, 0, Random(), i, i->type, i->xy);
    02091     if (res != CALLBACK_FAILED) { // failed callback means "do nothing"
    02092       suppress_message = HasBit(res, 7);
    02093       /* Get the custom message if any */
    02094       if (HasBit(res, 8)) str = MapGRFStringID(indspec->grf_prop.grffile->grfid, GB(GetRegister(0x100), 0, 16));
    02095       res = GB(res, 0, 4);
    02096       switch(res) {
    02097         default: NOT_REACHED();
    02098         case 0x0: break;                  // Do nothing, but show the custom message if any
    02099         case 0x1: div = 1; break;         // Halve industry production. If production reaches the quarter of the default, the industry is closed instead.
    02100         case 0x2: mul = 1; break;         // Double industry production if it hasn't reached eight times of the original yet.
    02101         case 0x3: closeit = true; break;  // The industry announces imminent closure, and is physically removed from the map next month.
    02102         case 0x4: standard = true; break; // Do the standard random production change as if this industry was a primary one.
    02103         case 0x5: case 0x6: case 0x7:     // Divide production by 4, 8, 16
    02104         case 0x8: div = res - 0x3; break; // Divide production by 32
    02105         case 0x9: case 0xA: case 0xB:     // Multiply production by 4, 8, 16
    02106         case 0xC: mul = res - 0x7; break; // Multiply production by 32
    02107         case 0xD:                         // decrement production
    02108         case 0xE:                         // increment production
    02109           increment = res == 0x0D ? -1 : 1;
    02110           break;
    02111         case 0xF:                         // Set production to third byte of register 0x100
    02112           i->prod_level = Clamp(GB(GetRegister(0x100), 16, 8), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
    02113           recalculate_multipliers = true;
    02114           break;
    02115       }
    02116     }
    02117   } else {
    02118     if (monthly != smooth_economy) return;
    02119     if (indspec->life_type == INDUSTRYLIFE_BLACK_HOLE) return;
    02120   }
    02121
    02122   if (standard || (!callback_enabled && (indspec->life_type & (INDUSTRYLIFE_ORGANIC | INDUSTRYLIFE_EXTRACTIVE)) != 0)) {
    02123     /* decrease or increase */
    02124     bool only_decrease = (indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE;
    02125
    02126     if (smooth_economy) {
    02127       closeit = true;
    02128       for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    02129         if (i->produced_cargo[j] == CT_INVALID) continue;
    02130         uint32 r = Random();
    02131         int old_prod, new_prod, percent;
    02132         /* If over 60% is transported, mult is 1, else mult is -1. */
    02133         int mult = (i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_60) ? 1 : -1;
    02134
    02135         new_prod = old_prod = i->production_rate[j];
    02136
    02137         /* For industries with only_decrease flags (temperate terrain Oil Wells),
    02138          * the multiplier will always be -1 so they will only decrease. */
    02139         if (only_decrease) {
    02140           mult = -1;
    02141         /* For normal industries, if over 60% is transported, 33% chance for decrease.
    02142          * Bonus for very high station ratings (over 80%): 16% chance for decrease. */
    02143         } else if (Chance16I(1, ((i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_80) ? 6 : 3), r)) {
    02144           mult *= -1;
    02145         }
    02146
    02147         /* 4.5% chance for 3-23% (or 1 unit for very low productions) production change,
    02148          * determined by mult value. If mult = 1 prod. increases, else (-1) it decreases. */
    02149         if (Chance16I(1, 22, r >> 16)) {
    02150           new_prod += mult * (max(((RandomRange(50) + 10) * old_prod) >> 8, 1U));
    02151         }
    02152
    02153         /* Prevent production to overflow or Oil Rig passengers to be over-"produced" */
    02154         new_prod = Clamp(new_prod, 1, 255);
    02155
    02156         if (((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0) && j == 1)
    02157           new_prod = Clamp(new_prod, 0, 16);
    02158
    02159         /* Do not stop closing the industry when it has the lowest possible production rate */
    02160         if (new_prod == old_prod && old_prod > 1) {
    02161           closeit = false;
    02162           continue;
    02163         }
    02164
    02165         percent = (old_prod == 0) ? 100 : (new_prod * 100 / old_prod - 100);
    02166         i->production_rate[j] = new_prod;
    02167
    02168         /* Close the industry when it has the lowest possible production rate */
    02169         if (new_prod > 1) closeit = false;
    02170
    02171         if (abs(percent) >= 10) {
    02172           ReportNewsProductionChangeIndustry(i, i->produced_cargo[j], percent);
    02173         }
    02174       }
    02175     } else {
    02176       if (only_decrease || Chance16(1, 3)) {
    02177         /* If more than 60% transported, 66% chance of increase, else 33% chance of increase */
    02178         if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != Chance16(1, 3)) {
    02179           mul = 1; // Increase production
    02180         } else {
    02181           div = 1; // Decrease production
    02182         }
    02183       }
    02184     }
    02185   }
    02186
    02187   if (!callback_enabled && indspec->life_type & INDUSTRYLIFE_PROCESSING) {
    02188     if ( (byte)(_cur_year - i->last_prod_year) >= 5 && Chance16(1, smooth_economy ? 180 : 2)) {
    02189       closeit = true;
    02190     }
    02191   }
    02192
    02193   /* Increase if needed */
    02194   while (mul-- != 0 && i->prod_level < PRODLEVEL_MAXIMUM) {
    02195     i->prod_level = min(i->prod_level * 2, PRODLEVEL_MAXIMUM);
    02196     recalculate_multipliers = true;
    02197     if (str == STR_NULL) str = indspec->production_up_text;
    02198   }
    02199
    02200   /* Decrease if needed */
    02201   while (div-- != 0 && !closeit) {
    02202     if (i->prod_level == PRODLEVEL_MINIMUM) {
    02203       closeit = true;
    02204     } else {
    02205       i->prod_level = max(i->prod_level / 2, (int)PRODLEVEL_MINIMUM); // typecast to int required to please MSVC
    02206       recalculate_multipliers = true;
    02207       if (str == STR_NULL) str = indspec->production_down_text;
    02208     }
    02209   }
    02210
    02211   /* Increase or Decreasing the production level if needed */
    02212   if (increment != 0) {
    02213     if (increment < 0 && i->prod_level == PRODLEVEL_MINIMUM) {
    02214       closeit = true;
    02215     } else {
    02216       i->prod_level = ClampU(i->prod_level + increment, PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
    02217       recalculate_multipliers = true;
    02218     }
    02219   }
    02220
    02221   /* Recalculate production_rate
    02222    * For non-smooth economy these should always be synchronized with prod_level */
    02223   if (recalculate_multipliers) {
    02224     /* Rates are rounded up, so e.g. oilrig always produces some passengers */
    02225     i->production_rate[0] = min((indspec->production_rate[0] * i->prod_level + PRODLEVEL_DEFAULT - 1) / PRODLEVEL_DEFAULT, 0xFF);
    02226     i->production_rate[1] = min((indspec->production_rate[1] * i->prod_level + PRODLEVEL_DEFAULT - 1) / PRODLEVEL_DEFAULT, 0xFF);
    02227   }
    02228
    02229   /* Close if needed and allowed */
    02230   if (closeit && !CheckIndustryCloseDownProtection(i->type)) {
    02231     i->prod_level = PRODLEVEL_CLOSURE;
    02232     str = indspec->closure_text;
    02233   }
    02234
    02235   if (!suppress_message && str != STR_NULL) {
    02236     NewsSubtype ns;
    02237     /* Compute news category */
    02238     if (closeit) {
    02239       ns = NS_INDUSTRY_CLOSE;
    02240       AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
    02241     } else {
    02242       switch (WhoCanServiceIndustry(i)) {
    02243         case 0: ns = NS_INDUSTRY_NOBODY;  break;
    02244         case 1: ns = NS_INDUSTRY_OTHER;   break;
    02245         case 2: ns = NS_INDUSTRY_COMPANY; break;
    02246         default: NOT_REACHED(); break;
    02247       }
    02248     }
    02249     /* Set parameters of news string */
    02250     if (str > STR_LAST_STRINGID) {
    02251       SetDParam(0, STR_TOWN);
    02252       SetDParam(1, i->town->index);
    02253       SetDParam(2, indspec->name);
    02254     } else if (closeit) {
    02255       SetDParam(0, STR_INDUSTRY_FORMAT);
    02256       SetDParam(1, i->town->index);
    02257       SetDParam(2, indspec->name);
    02258     } else {
    02259       SetDParam(0, i->index);
    02260     }
    02261     /* and report the news to the user */
    02262     AddNewsItem(str,
    02263       ns,
    02264       i->xy + TileDiffXY(1, 1), 0);
    02265   }
    02266 }
    02267
    02273 void IndustryDailyLoop()
    02274 {
    02275   _economy.industry_daily_change_counter += _economy.industry_daily_increment;
    02276
    02277   /* Bits 16-31 of industry_construction_counter contain the number of industries to change/create today,
    02278    * the lower 16 bit are a fractional part that might accumulate over several days until it
    02279    * is sufficient for an industry. */
    02280   uint16 change_loop = _economy.industry_daily_change_counter >> 16;
    02281
    02282   /* Reset the active part of the counter, just keeping the "factional part" */
    02283   _economy.industry_daily_change_counter &= 0xFFFF;
    02284
    02285   if (change_loop == 0) {
    02286     return;  // Nothing to do? get out
    02287   }
    02288
    02289   CompanyID old_company = _current_company;
    02290   _current_company = OWNER_NONE;
    02291
    02292   /* perform the required industry changes for the day */
    02293   for (uint16 j = 0; j < change_loop; j++) {
    02294     /* 3% chance that we start a new industry */
    02295     if (Chance16(3, 100)) {
    02296       MaybeNewIndustry();
    02297     } else {
    02298       Industry *i = GetRandomIndustry();
    02299       if (i != NULL) ChangeIndustryProduction(i, false);
    02300     }
    02301   }
    02302
    02303   _current_company = old_company;
    02304
    02305   /* production-change */
    02306   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
    02307 }
    02308
    02309 void IndustryMonthlyLoop()
    02310 {
    02311   Industry *i;
    02312   CompanyID old_company = _current_company;
    02313   _current_company = OWNER_NONE;
    02314
    02315   FOR_ALL_INDUSTRIES(i) {
    02316     UpdateIndustryStatistics(i);
    02317     if (i->prod_level == PRODLEVEL_CLOSURE) {
    02318       delete i;
    02319     } else {
    02320       ChangeIndustryProduction(i, true);
    02321     }
    02322   }
    02323
    02324   _current_company = old_company;
    02325
    02326   /* production-change */
    02327   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
    02328 }
    02329
    02330
    02331 void InitializeIndustries()
    02332 {
    02333   _Industry_pool.CleanPool();
    02334   _Industry_pool.AddBlockToPool();
    02335
    02336   ResetIndustryCounts();
    02337   _industry_sound_tile = 0;
    02338 }
    02339
    02340 bool IndustrySpec::IsRawIndustry() const
    02341 {
    02342   /* Lumber mills are extractive/organic, but can always be built like a non-raw industry */
    02343   return (this->life_type & (INDUSTRYLIFE_EXTRACTIVE | INDUSTRYLIFE_ORGANIC)) != 0 &&
    02344       (this->behaviour & INDUSTRYBEH_CUT_TREES) == 0;
    02345 }
    02346
    02347 Money IndustrySpec::GetConstructionCost() const
    02348 {
    02349   return (_price.build_industry *
    02350       (_settings_game.construction.raw_industry_construction == 1 && this->IsRawIndustry() ?
    02351           this->raw_industry_cost_multiplier :
    02352           this->cost_multiplier
    02353       )) >> 8;
    02354 }
    02355
    02356 Money IndustrySpec::GetRemovalCost() const
    02357 {
    02358   return (_price.remove_house * this->removal_cost_multiplier) >> 8;
    02359 }
    02360
    02361 static CommandCost TerraformTile_Industry(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
    02362 {
    02363   if (AutoslopeEnabled()) {
    02364     /* We imitate here TTDP's behaviour:
    02365      *  - Both new and old slope must not be steep.
    02366      *  - TileMaxZ must not be changed.
    02367      *  - Allow autoslope by default.
    02368      *  - Disallow autoslope if callback succeeds and returns non-zero.
    02369      */
    02370     Slope tileh_old = GetTileSlope(tile, NULL);
    02371     /* TileMaxZ must not be changed. Slopes must not be steep. */
    02372     if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
    02373       const IndustryGfx gfx = GetIndustryGfx(tile);
    02374       const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
    02375
    02376       /* Call callback 3C 'disable autosloping for industry tiles'. */
    02377       if (HasBit(itspec->callback_flags, CBM_INDT_AUTOSLOPE)) {
    02378         /* If the callback fails, allow autoslope. */
    02379         uint16 res = GetIndustryTileCallback(CBID_INDUSTRY_AUTOSLOPE, 0, 0, gfx, GetIndustryByTile(tile), tile);
    02380         if ((res == 0) || (res == CALLBACK_FAILED)) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
    02381       } else {
    02382         /* allow autoslope */
    02383         return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
    02384       }
    02385     }
    02386   }
    02387   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    02388 }
    02389
    02390 extern const TileTypeProcs _tile_type_industry_procs = {
    02391   DrawTile_Industry,           // draw_tile_proc
    02392   GetSlopeZ_Industry,          // get_slope_z_proc
    02393   ClearTile_Industry,          // clear_tile_proc
    02394   GetAcceptedCargo_Industry,   // get_accepted_cargo_proc
    02395   GetTileDesc_Industry,        // get_tile_desc_proc
    02396   GetTileTrackStatus_Industry, // get_tile_track_status_proc
    02397   ClickTile_Industry,          // click_tile_proc
    02398   AnimateTile_Industry,        // animate_tile_proc
    02399   TileLoop_Industry,           // tile_loop_proc
    02400   ChangeTileOwner_Industry,    // change_tile_owner_proc
    02401   GetProducedCargo_Industry,   // get_produced_cargo_proc
    02402   NULL,                        // vehicle_enter_tile_proc
    02403   GetFoundation_Industry,      // get_foundation_proc
    02404   TerraformTile_Industry,      // terraform_tile_proc
    02405 };