# industry_cmd.cpp

Go to the documentation of this file.

    00001 /* $Id: industry_cmd.cpp 17417 2009-09-04 20:40:15Z 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   const IndustryTileTable *it = indspec->table[itspec_index];
    01601   bool custom_shape_check = false;
    01602
    01603   if (!CheckIfIndustryTilesAreFree(tile, it, itspec_index, type, &custom_shape_check)) return NULL;
    01604
    01605   if (HasBit(GetIndustrySpec(type)->callback_flags, CBM_IND_LOCATION)) {
    01606     if (!CheckIfCallBackAllowsCreation(tile, type, itspec_index, seed)) return NULL;
    01607   } else {
    01608     if (!_check_new_industry_procs[indspec->check_proc](tile)) return NULL;
    01609   }
    01610
    01611   if (!custom_shape_check && _settings_game.game_creation.land_generator == LG_TERRAGENESIS && _generating_world && !_ignore_restrictions && !CheckIfCanLevelIndustryPlatform(tile, DC_NONE, it, type)) return NULL;
    01612   if (!CheckIfFarEnoughFromIndustry(tile, type)) return NULL;
    01613
    01614   const Town *t = CheckMultipleIndustryInTown(tile, type);
    01615   if (t == NULL) return NULL;
    01616
    01617   if (!CheckIfIndustryIsAllowed(tile, type, t)) return NULL;
    01618
    01619   if (!Industry::CanAllocateItem()) return NULL;
    01620
    01621   if (flags & DC_EXEC) {
    01622     Industry *i = new Industry(tile);
    01623     if (!custom_shape_check) CheckIfCanLevelIndustryPlatform(tile, DC_EXEC, it, type);
    01624     DoCreateNewIndustry(i, tile, type, it, itspec_index, t, OWNER_NONE, founder);
    01625
    01626     return i;
    01627   }
    01628
    01629   /* We need to return a non-NULL pointer to tell we have created an industry.
    01630    * However, we haven't created a real one (no DC_EXEC), so return a fake one. */
    01631   return GetIndustry(0);
    01632 }
    01633
    01643 CommandCost CmdBuildIndustry(TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
    01644 {
    01645   IndustryType it = GB(p1, 0, 16);
    01646   if (it >= NUM_INDUSTRYTYPES) return CMD_ERROR;
    01647
    01648   const IndustrySpec *indspec = GetIndustrySpec(it);
    01649   const Industry *ind = NULL;
    01650
    01651   /* Check if the to-be built/founded industry is available for this climate. */
    01652   if (!indspec->enabled) {
    01653     return CMD_ERROR;
    01654   }
    01655
    01656   /* If the setting for raw-material industries is not on, you cannot build raw-material industries.
    01657    * Raw material industries are industries that do not accept cargo (at least for now) */
    01658   if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 0 && indspec->IsRawIndustry()) {
    01659     return CMD_ERROR;
    01660   }
    01661
    01662   if (_game_mode != GM_EDITOR && _settings_game.construction.raw_industry_construction == 2 && indspec->IsRawIndustry()) {
    01663     if (flags & DC_EXEC) {
    01664       /* Prospected industries are build as OWNER_TOWN to not e.g. be build on owned land of the founder */
    01665       CompanyID founder = _current_company;
    01666       _current_company = OWNER_TOWN;
    01667       /* Prospecting has a chance to fail, however we cannot guarantee that something can
    01668        * be built on the map, so the chance gets lower when the map is fuller, but there
    01669        * is nothing we can really do about that. */
    01670       if (Random() <= indspec->prospecting_chance) {
    01671         for (int i = 0; i < 5000; i++) {
    01672           /* We should not have more than one Random() in a function call
    01673            * because parameter evaluation order is not guaranteed in the c++ standard
    01674            */
    01675           tile = RandomTile();
    01676           ind = CreateNewIndustryHelper(tile, p1, flags, indspec, RandomRange(indspec->num_table), p2, founder);
    01677           if (ind != NULL) {
    01678             break;
    01679           }
    01680         }
    01681       }
    01682       _current_company = founder;
    01683     }
    01684   } else {
    01685     int count = indspec->num_table;
    01686     const IndustryTileTable * const *itt = indspec->table;
    01687     int num = GB(p1, 16, 16);
    01688     if (num >= count) return CMD_ERROR;
    01689
    01690     _error_message = STR_0239_SITE_UNSUITABLE;
    01691     do {
    01692       if (--count < 0) return CMD_ERROR;
    01693       if (--num < 0) num = indspec->num_table - 1;
    01694     } while (!CheckIfIndustryTilesAreFree(tile, itt[num], num, p1));
    01695
    01696     ind = CreateNewIndustryHelper(tile, p1, flags, indspec, num, p2, _current_company);
    01697     if (ind == NULL) return CMD_ERROR;
    01698   }
    01699
    01700   if (flags & DC_EXEC && _game_mode != GM_EDITOR && ind != NULL) {
    01701     SetDParam(0, indspec->name);
    01702     if (indspec->new_industry_text > STR_LAST_STRINGID) {
    01703       SetDParam(1, STR_TOWN);
    01704       SetDParam(2, ind->town->index);
    01705     } else {
    01706       SetDParam(1, ind->town->index);
    01707     }
    01708     AddNewsItem(indspec->new_industry_text, NS_INDUSTRY_OPEN, ind->xy, 0);
    01709     AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
    01710   }
    01711
    01712   return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost());
    01713 }
    01714
    01715
    01716 static Industry *CreateNewIndustry(TileIndex tile, IndustryType type)
    01717 {
    01718   const IndustrySpec *indspec = GetIndustrySpec(type);
    01719
    01720   uint32 seed = Random();
    01721   return CreateNewIndustryHelper(tile, type, DC_EXEC, indspec, RandomRange(indspec->num_table), seed, OWNER_NONE);
    01722 }
    01723
    01724 enum {
    01725   NB_NUMOFINDUSTRY = 11,
    01726   NB_DIFFICULTY_LEVEL = 5,
    01727 };
    01728
    01729 static const byte _numof_industry_table[NB_DIFFICULTY_LEVEL][NB_NUMOFINDUSTRY] = {
    01730   /* difficulty settings for number of industries */
    01731   {0, 0, 0, 0, 0, 0, 0, 0,  0,  0,  0},   // none
    01732   {0, 1, 1, 1, 1, 1, 1, 1,  1,  1,  1},   // very low
    01733   {0, 1, 1, 1, 2, 2, 3, 3,  4,  4,  5},   // low
    01734   {0, 1, 2, 3, 4, 5, 6, 7,  8,  9, 10},   // normal
    01735   {0, 2, 3, 4, 6, 7, 8, 9, 10, 10, 10},   // high
    01736 };
    01737
    01742 static void PlaceInitialIndustry(IndustryType type, int amount)
    01743 {
    01744   /* We need to bypass the amount given in parameter if it exceeds the maximum dimension of the
    01745    * _numof_industry_table.  newgrf can specify a big amount */
    01746   int num = (amount > NB_NUMOFINDUSTRY) ? amount : _numof_industry_table[_settings_game.difficulty.number_industries][amount];
    01747   const IndustrySpec *ind_spc = GetIndustrySpec(type);
    01748
    01749   /* These are always placed next to the coastline, so we scale by the perimeter instead. */
    01750   num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
    01751
    01752   if (_settings_game.difficulty.number_industries != 0) {
    01753     CompanyID old_company = _current_company;
    01754     _current_company = OWNER_NONE;
    01755     assert(num > 0);
    01756
    01757     do {
    01758       uint i;
    01759
    01760       IncreaseGeneratingWorldProgress(GWP_INDUSTRY);
    01761
    01762       for (i = 0; i < 2000; i++) {
    01763         if (CreateNewIndustry(RandomTile(), type) != NULL) break;
    01764       }
    01765     } while (--num);
    01766
    01767     _current_company = old_company;
    01768   }
    01769 }
    01770
    01773 void GenerateIndustries()
    01774 {
    01775   uint i = 0;
    01776   uint8 chance;
    01777   IndustryType it;
    01778   const IndustrySpec *ind_spc;
    01779
    01780   /* Find the total amount of industries */
    01781   if (_settings_game.difficulty.number_industries > 0) {
    01782     for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
    01783
    01784       ind_spc = GetIndustrySpec(it);
    01785
    01786       if (!CheckIfCallBackAllowsAvailability(it, IACT_MAPGENERATION)) {
    01787         ResetIndustryCreationProbility(it);
    01788       }
    01789
    01790       chance = ind_spc->appear_creation[_settings_game.game_creation.landscape];
    01791       if (ind_spc->enabled && chance > 0) {
    01792         /* once the chance of appearance is determind, it have to be scaled by
    01793          * the difficulty level. The "chance" in question is more an index into
    01794          * the _numof_industry_table,in fact */
    01795         int num = (chance > NB_NUMOFINDUSTRY) ? chance : _numof_industry_table[_settings_game.difficulty.number_industries][chance];
    01796
    01797         /* These are always placed next to the coastline, so we scale by the perimeter instead. */
    01798         num = (ind_spc->check_proc == CHECK_REFINERY || ind_spc->check_proc == CHECK_OIL_RIG) ? ScaleByMapSize1D(num) : ScaleByMapSize(num);
    01799         i += num;
    01800       }
    01801     }
    01802   }
    01803
    01804   SetGeneratingWorldProgress(GWP_INDUSTRY, i);
    01805
    01806   if (_settings_game.difficulty.number_industries > 0) {
    01807     for (it = 0; it < NUM_INDUSTRYTYPES; it++) {
    01808       /* Once the number of industries has been determined, let's really create them.
    01809        * The test for chance allows us to try create industries that are available only
    01810        * for this landscape.
    01811        * @todo :  Do we really have to pass chance as un-scaled value, since we've already
    01812        *          processed that scaling above? No, don't think so.  Will find a way. */
    01813       ind_spc = GetIndustrySpec(it);
    01814       if (ind_spc->enabled) {
    01815         chance = ind_spc->appear_creation[_settings_game.game_creation.landscape];
    01816         if (chance > 0) PlaceInitialIndustry(it, chance);
    01817       }
    01818     }
    01819   }
    01820 }
    01821
    01822 static void UpdateIndustryStatistics(Industry *i)
    01823 {
    01824   byte pct;
    01825   bool refresh = false;
    01826
    01827   for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    01828     if (i->produced_cargo[j] != CT_INVALID) {
    01829       pct = 0;
    01830       if (i->this_month_production[j] != 0) {
    01831         i->last_prod_year = _cur_year;
    01832         pct = min(i->this_month_transported[j] * 256 / i->this_month_production[j], 255);
    01833       }
    01834       i->last_month_pct_transported[j] = pct;
    01835
    01836       i->last_month_production[j] = i->this_month_production[j];
    01837       i->this_month_production[j] = 0;
    01838
    01839       i->last_month_transported[j] = i->this_month_transported[j];
    01840       i->this_month_transported[j] = 0;
    01841       refresh = true;
    01842     }
    01843   }
    01844
    01845   if (refresh) InvalidateWindow(WC_INDUSTRY_VIEW, i->index);
    01846 }
    01847
    01849 struct ProbabilityHelper {
    01850   uint16 prob;
    01851   IndustryType ind;
    01852 };
    01853
    01857 static void MaybeNewIndustry(void)
    01858 {
    01859   Industry *ind;               // will receive the industry's creation pointer
    01860   IndustryType rndtype, j;     // Loop controlers
    01861   const IndustrySpec *ind_spc;
    01862   uint num = 0;
    01863   ProbabilityHelper cumulative_probs[NUM_INDUSTRYTYPES]; // probability collector
    01864   uint16 probability_max = 0;
    01865
    01866   /* Generate a list of all possible industries that can be built. */
    01867   for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
    01868     ind_spc = GetIndustrySpec(j);
    01869     byte chance = ind_spc->appear_ingame[_settings_game.game_creation.landscape];
    01870
    01871     if (!ind_spc->enabled || chance == 0) continue;
    01872
    01873     /* If there is no Callback CBID_INDUSTRY_AVAILABLE or if this one did anot failed,
    01874      * and if appearing chance for this landscape is above 0, this industry can be chosen */
    01875     if (CheckIfCallBackAllowsAvailability(j, IACT_RANDOMCREATION)) {
    01876       probability_max += chance;
    01877       /* adds the result for this industry */
    01878       cumulative_probs[num].ind = j;
    01879       cumulative_probs[num++].prob = probability_max;
    01880     }
    01881   }
    01882
    01883   /* Abort if there is no industry buildable */
    01884   if (probability_max == 0) return;
    01885
    01886   /* Find a random type, with maximum being what has been evaluate above*/
    01887   rndtype = RandomRange(probability_max);
    01888   for (j = 0; j < NUM_INDUSTRYTYPES; j++) {
    01889     /* and choose the index of the industry that matches as close as possible this random type */
    01890     if (cumulative_probs[j].prob >= rndtype) break;
    01891   }
    01892
    01893   ind_spc = GetIndustrySpec(cumulative_probs[j].ind);
    01894   /*  Check if it is allowed */
    01895   if ((ind_spc->behaviour & INDUSTRYBEH_BEFORE_1950) && _cur_year > 1950) return;
    01896   if ((ind_spc->behaviour & INDUSTRYBEH_AFTER_1960) && _cur_year < 1960) return;
    01897
    01898   /* try to create 2000 times this industry */
    01899   num = 2000;
    01900   for (;;) {
    01901     ind = CreateNewIndustry(RandomTile(), cumulative_probs[j].ind);
    01902     if (ind != NULL) break;
    01903     if (--num == 0) return;
    01904   }
    01905
    01906   SetDParam(0, ind_spc->name);
    01907   if (ind_spc->new_industry_text > STR_LAST_STRINGID) {
    01908     SetDParam(1, STR_TOWN);
    01909     SetDParam(2, ind->town->index);
    01910   } else {
    01911     SetDParam(1, ind->town->index);
    01912   }
    01913   AddNewsItem(ind_spc->new_industry_text, NS_INDUSTRY_OPEN, ind->xy, 0);
    01914   AI::BroadcastNewEvent(new AIEventIndustryOpen(ind->index));
    01915 }
    01916
    01925 static bool CheckIndustryCloseDownProtection(IndustryType type)
    01926 {
    01927   const IndustrySpec *indspec = GetIndustrySpec(type);
    01928
    01929   /* oil wells (or the industries with that flag set) are always allowed to closedown */
    01930   if (indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD && _settings_game.game_creation.landscape == LT_TEMPERATE) return false;
    01931   return (indspec->behaviour & INDUSTRYBEH_CANCLOSE_LASTINSTANCE) == 0 && GetIndustryTypeCount(type) <= 1;
    01932 }
    01933
    01943 static void CanCargoServiceIndustry(CargoID cargo, Industry *ind, bool *c_accepts, bool *c_produces)
    01944 {
    01945   const IndustrySpec *indspec = GetIndustrySpec(ind->type);
    01946
    01947   /* Check for acceptance of cargo */
    01948   for (byte j = 0; j < lengthof(ind->accepts_cargo); j++) {
    01949     if (ind->accepts_cargo[j] == CT_INVALID) continue;
    01950     if (cargo == ind->accepts_cargo[j]) {
    01951       if (HasBit(indspec->callback_flags, CBM_IND_REFUSE_CARGO)) {
    01952         uint16 res = GetIndustryCallback(CBID_INDUSTRY_REFUSE_CARGO,
    01953             0, GetReverseCargoTranslation(cargo, indspec->grf_prop.grffile),
    01954             ind, ind->type, ind->xy);
    01955         if (res == 0) continue;
    01956       }
    01957       *c_accepts = true;
    01958       break;
    01959     }
    01960   }
    01961
    01962   /* Check for produced cargo */
    01963   for (byte j = 0; j < lengthof(ind->produced_cargo); j++) {
    01964     if (ind->produced_cargo[j] == CT_INVALID) continue;
    01965     if (cargo == ind->produced_cargo[j]) {
    01966       *c_produces = true;
    01967       break;
    01968     }
    01969   }
    01970 }
    01971
    01985 int WhoCanServiceIndustry(Industry *ind)
    01986 {
    01987   /* Find all stations within reach of the industry */
    01988   StationList stations;
    01989   FindStationsAroundTiles(ind->xy, ind->width, ind->height, &stations);
    01990
    01991   if (stations.Length() == 0) return 0; // No stations found at all => nobody services
    01992
    01993   const Vehicle *v;
    01994   int result = 0;
    01995   FOR_ALL_VEHICLES(v) {
    01996     /* Is it worthwhile to try this vehicle? */
    01997     if (v->owner != _local_company && result != 0) continue;
    01998
    01999     /* Check whether it accepts the right kind of cargo */
    02000     bool c_accepts = false;
    02001     bool c_produces = false;
    02002     if (v->type == VEH_TRAIN && IsFrontEngine(v)) {
    02003       for (const Vehicle *u = v; u != NULL; u = u->Next()) {
    02004         CanCargoServiceIndustry(u->cargo_type, ind, &c_accepts, &c_produces);
    02005       }
    02006     } else if (v->type == VEH_ROAD || v->type == VEH_SHIP || v->type == VEH_AIRCRAFT) {
    02007       CanCargoServiceIndustry(v->cargo_type, ind, &c_accepts, &c_produces);
    02008     } else {
    02009       continue;
    02010     }
    02011     if (!c_accepts && !c_produces) continue; // Wrong cargo
    02012
    02013     /* Check orders of the vehicle.
    02014      * We cannot check the first of shared orders only, since the first vehicle in such a chain
    02015      * may have a different cargo type.
    02016      */
    02017     const Order *o;
    02018     FOR_VEHICLE_ORDERS(v, o) {
    02019       if (o->IsType(OT_GOTO_STATION) && !(o->GetUnloadType() & OUFB_TRANSFER)) {
    02020         /* Vehicle visits a station to load or unload */
    02021         Station *st = GetStation(o->GetDestination());
    02022         if (!st->IsValid()) continue;
    02023
    02024         /* Same cargo produced by industry is dropped here => not serviced by vehicle v */
    02025         if ((o->GetUnloadType() & OUFB_UNLOAD) && !c_accepts) break;
    02026
    02027         if (stations.Contains(st)) {
    02028           if (v->owner == _local_company) return 2; // Company services industry
    02029           result = 1; // Competitor services industry
    02030         }
    02031       }
    02032     }
    02033   }
    02034   return result;
    02035 }
    02036
    02044 static void ReportNewsProductionChangeIndustry(Industry *ind, CargoID type, int percent)
    02045 {
    02046   NewsSubtype ns;
    02047
    02048   switch (WhoCanServiceIndustry(ind)) {
    02049     case 0: ns = NS_INDUSTRY_NOBODY;  break;
    02050     case 1: ns = NS_INDUSTRY_OTHER;   break;
    02051     case 2: ns = NS_INDUSTRY_COMPANY; break;
    02052     default: NOT_REACHED(); break;
    02053   }
    02054   SetDParam(2, abs(percent));
    02055   SetDParam(0, GetCargo(type)->name);
    02056   SetDParam(1, ind->index);
    02057   AddNewsItem(
    02058     percent >= 0 ? STR_INDUSTRY_PROD_GOUP : STR_INDUSTRY_PROD_GODOWN,
    02059     ns,
    02060     ind->xy + TileDiffXY(1, 1), 0
    02061   );
    02062 }
    02063
    02064 enum {
    02065   PERCENT_TRANSPORTED_60 = 153,
    02066   PERCENT_TRANSPORTED_80 = 204,
    02067 };
    02068
    02073 static void ChangeIndustryProduction(Industry *i, bool monthly)
    02074 {
    02075   StringID str = STR_NULL;
    02076   bool closeit = false;
    02077   const IndustrySpec *indspec = GetIndustrySpec(i->type);
    02078   bool standard = false;
    02079   bool suppress_message = false;
    02080   bool recalculate_multipliers = false;
    02081   /* don't use smooth economy for industries using production related callbacks */
    02082   bool smooth_economy = _settings_game.economy.smooth_economy &&
    02083                         !(HasBit(indspec->callback_flags, CBM_IND_PRODUCTION_256_TICKS) || HasBit(indspec->callback_flags, CBM_IND_PRODUCTION_CARGO_ARRIVAL)) && // production callbacks
    02084                         !(HasBit(indspec->callback_flags, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(indspec->callback_flags, CBM_IND_PRODUCTION_CHANGE));            // production change callbacks
    02085   byte div = 0;
    02086   byte mul = 0;
    02087   int8 increment = 0;
    02088
    02089   bool callback_enabled = HasBit(indspec->callback_flags, monthly ? CBM_IND_MONTHLYPROD_CHANGE : CBM_IND_PRODUCTION_CHANGE);
    02090   if (callback_enabled) {
    02091     uint16 res = GetIndustryCallback(monthly ? CBID_INDUSTRY_MONTHLYPROD_CHANGE : CBID_INDUSTRY_PRODUCTION_CHANGE, 0, Random(), i, i->type, i->xy);
    02092     if (res != CALLBACK_FAILED) { // failed callback means "do nothing"
    02093       suppress_message = HasBit(res, 7);
    02094       /* Get the custom message if any */
    02095       if (HasBit(res, 8)) str = MapGRFStringID(indspec->grf_prop.grffile->grfid, GB(GetRegister(0x100), 0, 16));
    02096       res = GB(res, 0, 4);
    02097       switch(res) {
    02098         default: NOT_REACHED();
    02099         case 0x0: break;                  // Do nothing, but show the custom message if any
    02100         case 0x1: div = 1; break;         // Halve industry production. If production reaches the quarter of the default, the industry is closed instead.
    02101         case 0x2: mul = 1; break;         // Double industry production if it hasn't reached eight times of the original yet.
    02102         case 0x3: closeit = true; break;  // The industry announces imminent closure, and is physically removed from the map next month.
    02103         case 0x4: standard = true; break; // Do the standard random production change as if this industry was a primary one.
    02104         case 0x5: case 0x6: case 0x7:     // Divide production by 4, 8, 16
    02105         case 0x8: div = res - 0x3; break; // Divide production by 32
    02106         case 0x9: case 0xA: case 0xB:     // Multiply production by 4, 8, 16
    02107         case 0xC: mul = res - 0x7; break; // Multiply production by 32
    02108         case 0xD:                         // decrement production
    02109         case 0xE:                         // increment production
    02110           increment = res == 0x0D ? -1 : 1;
    02111           break;
    02112         case 0xF:                         // Set production to third byte of register 0x100
    02113           i->prod_level = Clamp(GB(GetRegister(0x100), 16, 8), PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
    02114           recalculate_multipliers = true;
    02115           break;
    02116       }
    02117     }
    02118   } else {
    02119     if (monthly != smooth_economy) return;
    02120     if (indspec->life_type == INDUSTRYLIFE_BLACK_HOLE) return;
    02121   }
    02122
    02123   if (standard || (!callback_enabled && (indspec->life_type & (INDUSTRYLIFE_ORGANIC | INDUSTRYLIFE_EXTRACTIVE)) != 0)) {
    02124     /* decrease or increase */
    02125     bool only_decrease = (indspec->behaviour & INDUSTRYBEH_DONT_INCR_PROD) && _settings_game.game_creation.landscape == LT_TEMPERATE;
    02126
    02127     if (smooth_economy) {
    02128       closeit = true;
    02129       for (byte j = 0; j < lengthof(i->produced_cargo); j++) {
    02130         if (i->produced_cargo[j] == CT_INVALID) continue;
    02131         uint32 r = Random();
    02132         int old_prod, new_prod, percent;
    02133         /* If over 60% is transported, mult is 1, else mult is -1. */
    02134         int mult = (i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_60) ? 1 : -1;
    02135
    02136         new_prod = old_prod = i->production_rate[j];
    02137
    02138         /* For industries with only_decrease flags (temperate terrain Oil Wells),
    02139          * the multiplier will always be -1 so they will only decrease. */
    02140         if (only_decrease) {
    02141           mult = -1;
    02142         /* For normal industries, if over 60% is transported, 33% chance for decrease.
    02143          * Bonus for very high station ratings (over 80%): 16% chance for decrease. */
    02144         } else if (Chance16I(1, ((i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_80) ? 6 : 3), r)) {
    02145           mult *= -1;
    02146         }
    02147
    02148         /* 4.5% chance for 3-23% (or 1 unit for very low productions) production change,
    02149          * determined by mult value. If mult = 1 prod. increases, else (-1) it decreases. */
    02150         if (Chance16I(1, 22, r >> 16)) {
    02151           new_prod += mult * (max(((RandomRange(50) + 10) * old_prod) >> 8, 1U));
    02152         }
    02153
    02154         /* Prevent production to overflow or Oil Rig passengers to be over-"produced" */
    02155         new_prod = Clamp(new_prod, 1, 255);
    02156
    02157         if (((indspec->behaviour & INDUSTRYBEH_BUILT_ONWATER) != 0) && j == 1)
    02158           new_prod = Clamp(new_prod, 0, 16);
    02159
    02160         /* Do not stop closing the industry when it has the lowest possible production rate */
    02161         if (new_prod == old_prod && old_prod > 1) {
    02162           closeit = false;
    02163           continue;
    02164         }
    02165
    02166         percent = (old_prod == 0) ? 100 : (new_prod * 100 / old_prod - 100);
    02167         i->production_rate[j] = new_prod;
    02168
    02169         /* Close the industry when it has the lowest possible production rate */
    02170         if (new_prod > 1) closeit = false;
    02171
    02172         if (abs(percent) >= 10) {
    02173           ReportNewsProductionChangeIndustry(i, i->produced_cargo[j], percent);
    02174         }
    02175       }
    02176     } else {
    02177       if (only_decrease || Chance16(1, 3)) {
    02178         /* If more than 60% transported, 66% chance of increase, else 33% chance of increase */
    02179         if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != Chance16(1, 3)) {
    02180           mul = 1; // Increase production
    02181         } else {
    02182           div = 1; // Decrease production
    02183         }
    02184       }
    02185     }
    02186   }
    02187
    02188   if (!callback_enabled && indspec->life_type & INDUSTRYLIFE_PROCESSING) {
    02189     if ( (byte)(_cur_year - i->last_prod_year) >= 5 && Chance16(1, smooth_economy ? 180 : 2)) {
    02190       closeit = true;
    02191     }
    02192   }
    02193
    02194   /* Increase if needed */
    02195   while (mul-- != 0 && i->prod_level < PRODLEVEL_MAXIMUM) {
    02196     i->prod_level = min(i->prod_level * 2, PRODLEVEL_MAXIMUM);
    02197     recalculate_multipliers = true;
    02198     if (str == STR_NULL) str = indspec->production_up_text;
    02199   }
    02200
    02201   /* Decrease if needed */
    02202   while (div-- != 0 && !closeit) {
    02203     if (i->prod_level == PRODLEVEL_MINIMUM) {
    02204       closeit = true;
    02205     } else {
    02206       i->prod_level = max(i->prod_level / 2, (int)PRODLEVEL_MINIMUM); // typecast to int required to please MSVC
    02207       recalculate_multipliers = true;
    02208       if (str == STR_NULL) str = indspec->production_down_text;
    02209     }
    02210   }
    02211
    02212   /* Increase or Decreasing the production level if needed */
    02213   if (increment != 0) {
    02214     if (increment < 0 && i->prod_level == PRODLEVEL_MINIMUM) {
    02215       closeit = true;
    02216     } else {
    02217       i->prod_level = ClampU(i->prod_level + increment, PRODLEVEL_MINIMUM, PRODLEVEL_MAXIMUM);
    02218       recalculate_multipliers = true;
    02219     }
    02220   }
    02221
    02222   /* Recalculate production_rate
    02223    * For non-smooth economy these should always be synchronized with prod_level */
    02224   if (recalculate_multipliers) {
    02225     /* Rates are rounded up, so e.g. oilrig always produces some passengers */
    02226     i->production_rate[0] = min((indspec->production_rate[0] * i->prod_level + PRODLEVEL_DEFAULT - 1) / PRODLEVEL_DEFAULT, 0xFF);
    02227     i->production_rate[1] = min((indspec->production_rate[1] * i->prod_level + PRODLEVEL_DEFAULT - 1) / PRODLEVEL_DEFAULT, 0xFF);
    02228   }
    02229
    02230   /* Close if needed and allowed */
    02231   if (closeit && !CheckIndustryCloseDownProtection(i->type)) {
    02232     i->prod_level = PRODLEVEL_CLOSURE;
    02233     str = indspec->closure_text;
    02234   }
    02235
    02236   if (!suppress_message && str != STR_NULL) {
    02237     NewsSubtype ns;
    02238     /* Compute news category */
    02239     if (closeit) {
    02240       ns = NS_INDUSTRY_CLOSE;
    02241       AI::BroadcastNewEvent(new AIEventIndustryClose(i->index));
    02242     } else {
    02243       switch (WhoCanServiceIndustry(i)) {
    02244         case 0: ns = NS_INDUSTRY_NOBODY;  break;
    02245         case 1: ns = NS_INDUSTRY_OTHER;   break;
    02246         case 2: ns = NS_INDUSTRY_COMPANY; break;
    02247         default: NOT_REACHED(); break;
    02248       }
    02249     }
    02250     /* Set parameters of news string */
    02251     if (str > STR_LAST_STRINGID) {
    02252       SetDParam(0, STR_TOWN);
    02253       SetDParam(1, i->town->index);
    02254       SetDParam(2, indspec->name);
    02255     } else if (closeit) {
    02256       SetDParam(0, STR_INDUSTRY_FORMAT);
    02257       SetDParam(1, i->town->index);
    02258       SetDParam(2, indspec->name);
    02259     } else {
    02260       SetDParam(0, i->index);
    02261     }
    02262     /* and report the news to the user */
    02263     AddNewsItem(str,
    02264       ns,
    02265       i->xy + TileDiffXY(1, 1), 0);
    02266   }
    02267 }
    02268
    02274 void IndustryDailyLoop()
    02275 {
    02276   _economy.industry_daily_change_counter += _economy.industry_daily_increment;
    02277
    02278   /* Bits 16-31 of industry_construction_counter contain the number of industries to change/create today,
    02279    * the lower 16 bit are a fractional part that might accumulate over several days until it
    02280    * is sufficient for an industry. */
    02281   uint16 change_loop = _economy.industry_daily_change_counter >> 16;
    02282
    02283   /* Reset the active part of the counter, just keeping the "factional part" */
    02284   _economy.industry_daily_change_counter &= 0xFFFF;
    02285
    02286   if (change_loop == 0) {
    02287     return;  // Nothing to do? get out
    02288   }
    02289
    02290   CompanyID old_company = _current_company;
    02291   _current_company = OWNER_NONE;
    02292
    02293   /* perform the required industry changes for the day */
    02294   for (uint16 j = 0; j < change_loop; j++) {
    02295     /* 3% chance that we start a new industry */
    02296     if (Chance16(3, 100)) {
    02297       MaybeNewIndustry();
    02298     } else {
    02299       Industry *i = GetRandomIndustry();
    02300       if (i != NULL) ChangeIndustryProduction(i, false);
    02301     }
    02302   }
    02303
    02304   _current_company = old_company;
    02305
    02306   /* production-change */
    02307   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
    02308 }
    02309
    02310 void IndustryMonthlyLoop()
    02311 {
    02312   Industry *i;
    02313   CompanyID old_company = _current_company;
    02314   _current_company = OWNER_NONE;
    02315
    02316   FOR_ALL_INDUSTRIES(i) {
    02317     UpdateIndustryStatistics(i);
    02318     if (i->prod_level == PRODLEVEL_CLOSURE) {
    02319       delete i;
    02320     } else {
    02321       ChangeIndustryProduction(i, true);
    02322     }
    02323   }
    02324
    02325   _current_company = old_company;
    02326
    02327   /* production-change */
    02328   InvalidateWindowData(WC_INDUSTRY_DIRECTORY, 0, 1);
    02329 }
    02330
    02331
    02332 void InitializeIndustries()
    02333 {
    02334   _Industry_pool.CleanPool();
    02335   _Industry_pool.AddBlockToPool();
    02336
    02337   ResetIndustryCounts();
    02338   _industry_sound_tile = 0;
    02339 }
    02340
    02341 bool IndustrySpec::IsRawIndustry() const
    02342 {
    02343   /* Lumber mills are extractive/organic, but can always be built like a non-raw industry */
    02344   return (this->life_type & (INDUSTRYLIFE_EXTRACTIVE | INDUSTRYLIFE_ORGANIC)) != 0 &&
    02345       (this->behaviour & INDUSTRYBEH_CUT_TREES) == 0;
    02346 }
    02347
    02348 Money IndustrySpec::GetConstructionCost() const
    02349 {
    02350   return (_price.build_industry *
    02351       (_settings_game.construction.raw_industry_construction == 1 && this->IsRawIndustry() ?
    02352           this->raw_industry_cost_multiplier :
    02353           this->cost_multiplier
    02354       )) >> 8;
    02355 }
    02356
    02357 Money IndustrySpec::GetRemovalCost() const
    02358 {
    02359   return (_price.remove_house * this->removal_cost_multiplier) >> 8;
    02360 }
    02361
    02362 static CommandCost TerraformTile_Industry(TileIndex tile, DoCommandFlag flags, uint z_new, Slope tileh_new)
    02363 {
    02364   if (AutoslopeEnabled()) {
    02365     /* We imitate here TTDP's behaviour:
    02366      *  - Both new and old slope must not be steep.
    02367      *  - TileMaxZ must not be changed.
    02368      *  - Allow autoslope by default.
    02369      *  - Disallow autoslope if callback succeeds and returns non-zero.
    02370      */
    02371     Slope tileh_old = GetTileSlope(tile, NULL);
    02372     /* TileMaxZ must not be changed. Slopes must not be steep. */
    02373     if (!IsSteepSlope(tileh_old) && !IsSteepSlope(tileh_new) && (GetTileMaxZ(tile) == z_new + GetSlopeMaxZ(tileh_new))) {
    02374       const IndustryGfx gfx = GetIndustryGfx(tile);
    02375       const IndustryTileSpec *itspec = GetIndustryTileSpec(gfx);
    02376
    02377       /* Call callback 3C 'disable autosloping for industry tiles'. */
    02378       if (HasBit(itspec->callback_flags, CBM_INDT_AUTOSLOPE)) {
    02379         /* If the callback fails, allow autoslope. */
    02380         uint16 res = GetIndustryTileCallback(CBID_INDUSTRY_AUTOSLOPE, 0, 0, gfx, GetIndustryByTile(tile), tile);
    02381         if ((res == 0) || (res == CALLBACK_FAILED)) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
    02382       } else {
    02383         /* allow autoslope */
    02384         return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform);
    02385       }
    02386     }
    02387   }
    02388   return DoCommand(tile, 0, 0, flags, CMD_LANDSCAPE_CLEAR);
    02389 }
    02390
    02391 extern const TileTypeProcs _tile_type_industry_procs = {
    02392   DrawTile_Industry,           // draw_tile_proc
    02393   GetSlopeZ_Industry,          // get_slope_z_proc
    02394   ClearTile_Industry,          // clear_tile_proc
    02395   GetAcceptedCargo_Industry,   // get_accepted_cargo_proc
    02396   GetTileDesc_Industry,        // get_tile_desc_proc
    02397   GetTileTrackStatus_Industry, // get_tile_track_status_proc
    02398   ClickTile_Industry,          // click_tile_proc
    02399   AnimateTile_Industry,        // animate_tile_proc
    02400   TileLoop_Industry,           // tile_loop_proc
    02401   ChangeTileOwner_Industry,    // change_tile_owner_proc
    02402   GetProducedCargo_Industry,   // get_produced_cargo_proc
    02403   NULL,                        // vehicle_enter_tile_proc
    02404   GetFoundation_Industry,      // get_foundation_proc
    02405   TerraformTile_Industry,      // terraform_tile_proc
    02406 };