diff -r ebf0ece7d8f6 -r eee46cb39750 src/industry_cmd.cpp --- a/src/industry_cmd.cpp Fri Nov 23 16:59:30 2007 +0000 +++ b/src/industry_cmd.cpp Wed Jan 09 18:11:12 2008 +0000 @@ -5,30 +5,22 @@ #include "stdafx.h" #include "openttd.h" #include "clear_map.h" -#include "functions.h" #include "industry_map.h" #include "station_map.h" #include "table/strings.h" -#include "strings.h" #include "table/sprites.h" -#include "map.h" -#include "tile.h" #include "train.h" #include "landscape.h" -#include "viewport.h" -#include "command.h" +#include "viewport_func.h" +#include "command_func.h" #include "industry.h" #include "town.h" -#include "vehicle.h" #include "news.h" #include "saveload.h" -#include "economy.h" -#include "sound.h" #include "variables.h" #include "table/industry_land.h" #include "table/build_industry.h" #include "genworld.h" -#include "date.h" #include "water_map.h" #include "tree_map.h" #include "cargotype.h" @@ -40,6 +32,14 @@ #include "misc/autoptr.hpp" #include "autoslope.h" #include "transparency.h" +#include "water.h" +#include "strings_func.h" +#include "tile_cmd.h" +#include "functions.h" +#include "window_func.h" +#include "date_func.h" +#include "vehicle_func.h" +#include "sound_func.h" void ShowIndustryViewWindow(int industry); void BuildOilRig(TileIndex tile); @@ -400,7 +400,7 @@ } if (flags & DC_EXEC) delete i; - return CommandCost(); + return CommandCost(EXPENSES_CONSTRUCTION, indspec->GetRemovalCost()); } static void TransportIndustryGoods(TileIndex tile) @@ -557,7 +557,7 @@ case GFX_OILWELL_ANIMATED_2: case GFX_OILWELL_ANIMATED_3: if ((_tick_counter & 7) == 0) { - bool b = CHANCE16(1, 7); + bool b = Chance16(1, 7); IndustryGfx gfx = GetIndustryGfx(tile); m = GetIndustryAnimationState(tile) + 1; @@ -737,7 +737,7 @@ case GFX_COAL_MINE_TOWER_NOT_ANIMATED: case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: case GFX_GOLD_MINE_TOWER_NOT_ANIMATED: - if (!(_tick_counter & 0x400) && CHANCE16(1, 2)) { + if (!(_tick_counter & 0x400) && Chance16(1, 2)) { switch (gfx) { case GFX_COAL_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COAL_MINE_TOWER_ANIMATED; break; case GFX_COPPER_MINE_TOWER_NOT_ANIMATED: gfx = GFX_COPPER_MINE_TOWER_ANIMATED; break; @@ -750,7 +750,7 @@ break; case GFX_OILWELL_NOT_ANIMATED: - if (CHANCE16(1, 6)) { + if (Chance16(1, 6)) { SetIndustryGfx(tile, GFX_OILWELL_ANIMATED_1); SetIndustryAnimationState(tile, 0); AddAnimatedTile(tile); @@ -774,7 +774,7 @@ break; case GFX_POWERPLANT_SPARKS: - if (CHANCE16(1, 3)) { + if (Chance16(1, 3)) { SndPlayTileFx(SND_0C_ELECTRIC_SPARK, tile); AddAnimatedTile(tile); } @@ -804,7 +804,7 @@ break; case GFX_SUGAR_MINE_SIEVE: - if (CHANCE16(1, 3)) AddAnimatedTile(tile); + if (Chance16(1, 3)) AddAnimatedTile(tile); break; } } @@ -862,7 +862,7 @@ if (IsTileType(tile, MP_CLEAR) || IsTileType(tile, MP_TREES)) { byte or_ = type; - if (or_ == 1 && CHANCE16(1, 7)) or_ = 2; + if (or_ == 1 && Chance16(1, 7)) or_ = 2; if (direction == AXIS_X) { SetFenceSE(tile, or_); @@ -989,7 +989,7 @@ /* play a sound? */ if ((i->counter & 0x3F) == 0) { - if (CHANCE16R(1, 14, r) && (num = indsp->number_of_sounds) != 0) { + if (Chance16R(1, 14, r) && (num = indsp->number_of_sounds) != 0) { SndPlayTileFx( (SoundFx)(indsp->random_sounds[((r >> 16) * num) >> 16]), i->xy); @@ -1011,7 +1011,7 @@ if (HasBit(indsp->callback_flags, CBM_IND_SPECIAL_EFFECT)) { plant = (GetIndustryCallback(CBID_INDUSTRY_SPECIAL_EFFECT, Random(), 0, i, i->type, i->xy) != 0); } else { - plant = CHANCE16(1, 8); + plant = Chance16(1, 8); } if (plant) PlantRandomFarmField(i); @@ -1183,14 +1183,14 @@ { if (IsSteepSlope(current)) return true; if (current != SLOPE_FLAT) { - if (refused & SLOPE_STEEP) return true; + if (IsSteepSlope(refused)) return true; Slope t = ComplementSlope(current); - if (refused & 1 && (t & SLOPE_NW)) return false; - if (refused & 2 && (t & SLOPE_NE)) return false; - if (refused & 4 && (t & SLOPE_SW)) return false; - if (refused & 8 && (t & SLOPE_SE)) return false; + if (refused & SLOPE_W && (t & SLOPE_NW)) return true; + if (refused & SLOPE_S && (t & SLOPE_NE)) return true; + if (refused & SLOPE_E && (t & SLOPE_SW)) return true; + if (refused & SLOPE_N && (t & SLOPE_SE)) return true; } return false; @@ -1237,7 +1237,7 @@ if (ind_behav & (INDUSTRYBEH_ONLY_INTOWN | INDUSTRYBEH_TOWN1200_MORE)) { if (!IsTileType(cur_tile, MP_HOUSE)) { - _error_message = STR_029D_CAN_ONLY_BE_BUILT_IN_TOWNS; + _error_message = STR_030D_CAN_ONLY_BE_BUILT_IN_TOWNS; return false; } if (CmdFailed(DoCommand(cur_tile, 0, 0, 0, CMD_LANDSCAPE_CLEAR))) return false; @@ -1252,7 +1252,7 @@ /* It is almost impossible to have a fully flat land in TG, so what we * do is that we check if we can make the land flat later on. See * CheckIfCanLevelIndustryPlatform(). */ - return !refused_slope || (_patches.land_generator == LG_TERRAGENESIS && _generating_world && !custom_shape); + return !refused_slope || (_patches.land_generator == LG_TERRAGENESIS && _generating_world && !custom_shape && !_ignore_restrictions); } static bool CheckIfIndustryIsAllowed(TileIndex tile, int type, const Town *t) @@ -1289,7 +1289,7 @@ return false; /* Don't allow too big of a change if this is the sub-tile check */ - if (internal != 0 && delta(curh, height) > 1) return false; + if (internal != 0 && Delta(curh, height) > 1) return false; /* Different height, so the surrounding tiles of this tile * has to be correct too (in level, or almost in level) @@ -1512,7 +1512,7 @@ DoCommand(cur_tile, 0, 0, DC_EXEC, CMD_LANDSCAPE_CLEAR); - MakeIndustry(cur_tile, i->index, it->gfx); + MakeIndustry(cur_tile, i->index, it->gfx, Random()); if (_generating_world) { SetIndustryConstructionCounter(cur_tile, 3); @@ -1557,7 +1557,7 @@ if (!_check_new_industry_procs[indspec->check_proc](tile)) return NULL; } - if (!custom_shape_check && _patches.land_generator == LG_TERRAGENESIS && _generating_world && !CheckIfCanLevelIndustryPlatform(tile, 0, it, type)) return NULL; + if (!custom_shape_check && _patches.land_generator == LG_TERRAGENESIS && _generating_world && !_ignore_restrictions && !CheckIfCanLevelIndustryPlatform(tile, 0, it, type)) return NULL; if (!CheckIfTooCloseToIndustry(tile, type)) return NULL; const Town *t = CheckMultipleIndustryInTown(tile, type); @@ -1590,8 +1590,6 @@ { const IndustrySpec *indspec; - SET_EXPENSES_TYPE(EXPENSES_OTHER); - indspec = GetIndustrySpec(p1); /* Check if the to-be built/founded industry is available for this climate. */ @@ -1612,7 +1610,11 @@ * is nothing we can really do about that. */ if (Random() <= indspec->prospecting_chance) { for (int i = 0; i < 5000; i++) { - const Industry *ind = CreateNewIndustryHelper(RandomTile(), p1, flags, indspec, RandomRange(indspec->num_table)); + /* We should not have more than one Random() in a function call + * because parameter evaluation order is not guaranteed in the c++ standard + */ + tile = RandomTile(); + const Industry *ind = CreateNewIndustryHelper(tile, p1, flags, indspec, RandomRange(indspec->num_table)); if (ind != NULL) { SetDParam(0, indspec->name); if (indspec->new_industry_text > STR_LAST_STRINGID) { @@ -1642,7 +1644,7 @@ if (CreateNewIndustryHelper(tile, p1, flags, indspec, num) == NULL) return CMD_ERROR; } - return CommandCost(indspec->GetConstructionCost()); + return CommandCost(EXPENSES_OTHER, indspec->GetConstructionCost()); } @@ -1990,6 +1992,7 @@ enum { PERCENT_TRANSPORTED_60 = 153, + PERCENT_TRANSPORTED_80 = 204, }; /** Change industry production or do closure @@ -2010,6 +2013,7 @@ !(HasBit(indspec->callback_flags, CBM_IND_MONTHLYPROD_CHANGE) || HasBit(indspec->callback_flags, CBM_IND_PRODUCTION_CHANGE)); // production change callbacks byte div = 0; byte mul = 0; + int8 increment = 0; if (HasBit(indspec->callback_flags, monthly ? CBM_IND_MONTHLYPROD_CHANGE : CBM_IND_PRODUCTION_CHANGE)) { uint16 res = GetIndustryCallback(monthly ? CBID_INDUSTRY_MONTHLYPROD_CHANGE : CBID_INDUSTRY_PRODUCTION_CHANGE, 0, Random(), i, i->type, i->xy); @@ -2031,6 +2035,10 @@ case 0x8: div = res - 0x3; break; // Divide production by 32 case 0x9: case 0xA: case 0xB: // Multiply production by 4, 8, 16 case 0xC: mul = res - 0x7; break; // Multiply production by 32 + case 0xD: // decrement production + case 0xE: // increment production + increment = res == 0x0D ? -1 : 1; + break; } } } @@ -2046,14 +2054,26 @@ if (smooth_economy) { closeit = true; for (byte j = 0; j < 2 && i->produced_cargo[j] != CT_INVALID; j++){ + uint32 r = Random(); int old_prod, new_prod, percent; + /* If over 60% is transported, mult is 1, else mult is -1. */ int mult = (i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_60) ? 1 : -1; new_prod = old_prod = i->production_rate[j]; - if (only_decrease || CHANCE16(1, 3)) mult *= -1; + /* For industries with only_decrease flags (temperate terrain Oil Wells), + * the multiplier will always be -1 so they will only decrease. */ + if (only_decrease) { + mult = -1; + /* For normal industries, if over 60% is transported, 33% chance for decrease. + * Bonus for very high station ratings (over 80%): 16% chance for decrease. */ + } else if (Chance16I(1, ((i->last_month_pct_transported[j] > PERCENT_TRANSPORTED_80) ? 6 : 3), r)) { + mult *= -1; + } - if (CHANCE16(1, 22)) { + /* 4.5% chance for 3-23% (or 1 unit for very low productions) production change, + * determined by mult value. If mult = 1 prod. increases, else (-1) it decreases. */ + if (Chance16I(1, 22, r >> 16)) { new_prod += mult * (max(((RandomRange(50) + 10) * old_prod) >> 8, 1U)); } @@ -2080,9 +2100,9 @@ } } } else { - if (only_decrease || CHANCE16(1, 3)) { - /* If you transport > 60%, 66% chance we increase, else 33% chance we increase */ - if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != CHANCE16(1, 3)) { + if (only_decrease || Chance16(1, 3)) { + /* If more than 60% transported, 66% chance of increase, else 33% chance of increase */ + if (!only_decrease && (i->last_month_pct_transported[0] > PERCENT_TRANSPORTED_60) != Chance16(1, 3)) { mul = 1; // Increase production } else { div = 1; // Decrease production @@ -2092,7 +2112,7 @@ } if (standard && indspec->life_type & INDUSTRYLIFE_PROCESSING) { - if ( (byte)(_cur_year - i->last_prod_year) >= 5 && CHANCE16(1, smooth_economy ? 180 : 2)) { + if ( (byte)(_cur_year - i->last_prod_year) >= 5 && Chance16(1, smooth_economy ? 180 : 2)) { closeit = true; } } @@ -2117,6 +2137,11 @@ } } + if (increment != 0) { + i->prod_level = ClampU(i->prod_level + increment, 4, 0x80); + if (i->prod_level == 4) closeit = true; + } + /* Close if needed and allowed */ if (closeit && !CheckIndustryCloseDownProtection(i->type)) { i->prod_level = 0; @@ -2171,7 +2196,7 @@ } /* 3% chance that we start a new industry */ - if (CHANCE16(3, 100)) { + if (Chance16(3, 100)) { MaybeNewIndustry(); } else { i = GetRandomIndustry(); @@ -2212,6 +2237,11 @@ )) >> 8; } +Money IndustrySpec::GetRemovalCost() const +{ + return (_price.remove_house * this->removal_cost_multiplier) >> 8; +} + static CommandCost TerraformTile_Industry(TileIndex tile, uint32 flags, uint z_new, Slope tileh_new) { if (AutoslopeEnabled()) { @@ -2231,10 +2261,10 @@ if (HasBit(itspec->callback_flags, CBM_INDT_AUTOSLOPE)) { /* If the callback fails, allow autoslope. */ uint16 res = GetIndustryTileCallback(CBID_INDUSTRY_AUTOSLOPE, 0, 0, gfx, GetIndustryByTile(tile), tile); - if ((res == 0) || (res == CALLBACK_FAILED)) return _price.terraform; + if ((res == 0) || (res == CALLBACK_FAILED)) return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); } else { /* allow autoslope */ - return _price.terraform; + return CommandCost(EXPENSES_CONSTRUCTION, _price.terraform); } } }