diff --git a/src/aircraft_cmd.cpp b/src/aircraft_cmd.cpp index 76227e1430..87831dcd04 100644 --- a/src/aircraft_cmd.cpp +++ b/src/aircraft_cmd.cpp @@ -308,7 +308,7 @@ CommandCost CmdBuildAircraft(TileIndex tile, DoCommandFlag flags, const Engine * v->reliability_spd_dec = e->reliability_spd_dec; /* higher speed means higher breakdown chance */ /* to somewhat compensate for the fact that fast aircraft spend less time in the air */ - v->breakdown_chance = Clamp(64 + (AircraftVehInfo(v->engine_type)->max_speed >> 3), 0, 255); + v->breakdown_chance_factor = Clamp(64 + (AircraftVehInfo(v->engine_type)->max_speed >> 3), 0, 255); v->max_age = e->GetLifeLengthInDays(); _new_vehicle_id = v->index; @@ -1333,18 +1333,18 @@ static void MaybeCrashAirplane(Aircraft *v) Station *st = Station::Get(v->targetairport); /* FIXME -- MaybeCrashAirplane -> increase crashing chances of very modern airplanes on smaller than AT_METROPOLITAN airports */ - uint32 prob = (_settings_game.vehicle.improved_breakdowns && _settings_game.difficulty.vehicle_breakdowns) ? - 0x10000 / 10000 : 0x4000 << _settings_game.vehicle.plane_crashes; + uint32 prob = (0x4000 << _settings_game.vehicle.plane_crashes); if ((st->airport.GetFTA()->flags & AirportFTAClass::SHORT_STRIP) && (AircraftVehInfo(v->engine_type)->subtype & AIR_FAST) && !_cheats.no_jetcrash.value) { prob /= 20; - } else if (!_settings_game.vehicle.improved_breakdowns) { + } else { prob /= 1500; - } else if (v->breakdown_ctr == 1 && v->breakdown_type == BREAKDOWN_AIRCRAFT_EM_LANDING) { - /* Airplanes that are attempting an emergency landing have a 2% chance to crash */ - prob = 0x10000 / 50; + } + if (_settings_game.vehicle.improved_breakdowns && v->breakdown_ctr == 1 && v->breakdown_type == BREAKDOWN_AIRCRAFT_EM_LANDING) { + /* Airplanes that are attempting an emergency landing have a 2% chance to crash */ + prob = max(prob, 0x10000 / 50); } if (GB(Random(), 0, 22) > prob) return; diff --git a/src/ground_vehicle.cpp b/src/ground_vehicle.cpp index 071913ac8f..91d1900558 100644 --- a/src/ground_vehicle.cpp +++ b/src/ground_vehicle.cpp @@ -78,14 +78,15 @@ void GroundVehicle::CalculatePower(uint32& total_power, uint32& max_te, for (const T *u = v; u != NULL; u = u->Next()) { uint32 current_power = u->GetPower() + u->GetPoweredPartPower(u); + + if (breakdowns && u->breakdown_ctr == 1 && u->breakdown_type == BREAKDOWN_LOW_POWER) { + current_power = current_power * u->breakdown_severity / 256; + } + total_power += current_power; /* Only powered parts add tractive effort. */ if (current_power > 0) max_te += u->GetWeight() * u->GetTractiveEffort(); - - if (breakdowns && u->breakdown_ctr == 1 && u->breakdown_type == BREAKDOWN_LOW_POWER) { - total_power = total_power * u->breakdown_severity / 256; - } } max_te *= 10000; // Tractive effort in (tonnes * 1000 * 10 =) N. @@ -138,7 +139,6 @@ int GroundVehicle::GetAcceleration() * and km/h to m/s conversion below result in a maxium of * about 1.1E11, way more than 4.3E9 of int32. */ int64 power = this->gcache.cached_power * 746ll; - uint32 max_te = this->gcache.cached_max_te; // [N] /* This is constructed from: * - axle resistance: U16 power * 10 for 128 vehicles. @@ -171,6 +171,7 @@ int GroundVehicle::GetAcceleration() AccelStatus mode = v->GetAccelerationStatus(); /* handle breakdown power reduction */ + uint32 max_te = this->gcache.cached_max_te; // [N] if (Type == VEH_TRAIN && mode == AS_ACCEL && HasBit(Train::From(this)->flags, VRF_BREAKDOWN_POWER)) { /* We'd like to cache this, but changing cached_power has too many unwanted side-effects */ uint32 power_temp; @@ -219,9 +220,7 @@ int GroundVehicle::GetAcceleration() breakdown_factor /= (Train::From(this)->tcache.cached_num_engines + 2); } /* breakdown_chance is at least 5 (5 / 128 = ~4% of the normal chance) */ - this->breakdown_chance = max(breakdown_factor >> 16, (uint64)5); - } else { - this->breakdown_chance = 128; + this->breakdown_chance_factor = max(breakdown_factor >> 16, (uint64)5); } if (mode == AS_ACCEL) { @@ -235,7 +234,7 @@ int GroundVehicle::GetAcceleration() * same (maximum) speed. */ int accel = ClampToI32((force - resistance) / (mass * 4)); accel = force < resistance ? min(-1, accel) : max(1, accel); - if (this->type == VEH_TRAIN ) { + if (this->type == VEH_TRAIN) { if(_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL && HasBit(Train::From(this)->flags, VRF_BREAKDOWN_POWER)) { /* We need to apply the power reducation for non-realistic acceleration here */ diff --git a/src/ground_vehicle.hpp b/src/ground_vehicle.hpp index 86f328fe1e..be1c36781e 100644 --- a/src/ground_vehicle.hpp +++ b/src/ground_vehicle.hpp @@ -370,22 +370,23 @@ protected: uint spd = this->subspeed + accel; this->subspeed = (byte)spd; + int tempmax = max_speed; + /* When we are going faster than the maximum speed, reduce the speed * somewhat gradually. But never lower than the maximum speed. */ - int tempmax = ((this->breakdown_ctr == 1) ? this->cur_speed : max_speed); - if (this->breakdown_ctr == 1) { if (this->breakdown_type == BREAKDOWN_LOW_POWER) { - if ((this->tick_counter & 0x7) == 0) { + if ((this->tick_counter & 0x7) == 0 && _settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) { if (this->cur_speed > (this->breakdown_severity * max_speed) >> 8) { tempmax = this->cur_speed - (this->cur_speed / 10) - 1; } else { tempmax = (this->breakdown_severity * max_speed) >> 8; } } - } - if (this->breakdown_type == BREAKDOWN_LOW_SPEED) { + } else if (this->breakdown_type == BREAKDOWN_LOW_SPEED) { tempmax = min(max_speed, this->breakdown_severity); + } else { + tempmax = this->cur_speed; } } diff --git a/src/roadveh_cmd.cpp b/src/roadveh_cmd.cpp index 3747ad8854..68e020f124 100644 --- a/src/roadveh_cmd.cpp +++ b/src/roadveh_cmd.cpp @@ -298,7 +298,7 @@ CommandCost CmdBuildRoadVehicle(TileIndex tile, DoCommandFlag flags, const Engin v->reliability = e->reliability; v->reliability_spd_dec = e->reliability_spd_dec; - v->breakdown_chance = 128; + v->breakdown_chance_factor = 128; v->max_age = e->GetLifeLengthInDays(); _new_vehicle_id = v->index; diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index ac8fa33455..4208c3cdb6 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -2835,14 +2835,15 @@ bool AfterLoadGame() v->reliability = min(v->First()->reliability, e->reliability); } } + /* FALL THROUGH */ case VEH_ROAD: - v->breakdown_chance = 128; + v->breakdown_chance_factor = 128; break; case VEH_SHIP: - v->breakdown_chance = 64; + v->breakdown_chance_factor = 64; break; case VEH_AIRCRAFT: - v->breakdown_chance = Clamp(64 + (AircraftVehInfo(v->engine_type)->max_speed >> 3), 0, 255); + v->breakdown_chance_factor = Clamp(64 + (AircraftVehInfo(v->engine_type)->max_speed >> 3), 0, 255); v->breakdown_severity = 40; break; default: break; diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp index def7ada99d..6612292c4d 100644 --- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -671,6 +671,7 @@ const SaveLoad *GetVehicleDescription(VehicleType vt) SLE_VAR(Vehicle, breakdown_delay, SLE_UINT8), SLE_VAR(Vehicle, breakdowns_since_last_service, SLE_UINT8), SLE_VAR(Vehicle, breakdown_chance, SLE_UINT8), + SLE_CONDVAR(Vehicle, breakdown_chance_factor, SLE_UINT8, SL_IB, SL_MAX_VERSION), SLE_CONDVAR(Vehicle, breakdown_type, SLE_UINT8, SL_IB, SL_MAX_VERSION), SLE_CONDVAR(Vehicle, breakdown_severity, SLE_UINT8, SL_IB, SL_MAX_VERSION), SLE_CONDVAR(Vehicle, build_year, SLE_FILE_U8 | SLE_VAR_I32, 0, 30), diff --git a/src/settings.cpp b/src/settings.cpp index 41a7491067..fce350e8b3 100644 --- a/src/settings.cpp +++ b/src/settings.cpp @@ -1020,7 +1020,7 @@ static bool RoadVehAccelerationModelChanged(int32 p1) RoadVehicle *rv; FOR_ALL_ROADVEHICLES(rv) { if (rv->IsFrontEngine()) { - rv->breakdown_chance = 128; + rv->breakdown_chance_factor = 128; } } } @@ -1319,6 +1319,32 @@ static bool MaxVehiclesChanged(int32 p1) return true; } +static bool ImprovedBreakdownsSettingChanged(int32 p1) +{ + if (!_settings_game.vehicle.improved_breakdowns) return true; + + Vehicle *v; + FOR_ALL_VEHICLES(v) { + switch(v->type) { + case VEH_TRAIN: + if (v->IsFrontEngine()) { + v->breakdown_chance_factor = 128; + Train::From(v)->UpdateAcceleration(); + } + break; + + case VEH_ROAD: + if (v->IsFrontEngine()) { + v->breakdown_chance_factor = 128; + } + break; + + default: + break; + } + } + return true; +} #ifdef ENABLE_NETWORK diff --git a/src/ship_cmd.cpp b/src/ship_cmd.cpp index 9bd985c536..6f8ba28cc0 100644 --- a/src/ship_cmd.cpp +++ b/src/ship_cmd.cpp @@ -714,7 +714,7 @@ CommandCost CmdBuildShip(TileIndex tile, DoCommandFlag flags, const Engine *e, u v->reliability = e->reliability; v->reliability_spd_dec = e->reliability_spd_dec; - v->breakdown_chance = 64; // ships have a 50% lower breakdown chance than normal + v->breakdown_chance_factor = 64; // ships have a 50% lower breakdown chance than normal v->max_age = e->GetLifeLengthInDays(); _new_vehicle_id = v->index; diff --git a/src/table/settings.ini b/src/table/settings.ini index ec407c15db..e9c8d31b37 100644 --- a/src/table/settings.ini +++ b/src/table/settings.ini @@ -42,6 +42,7 @@ static bool InvalidateCompanyInfrastructureWindow(int32 p1); static bool InvalidateCompanyWindow(int32 p1); static bool ZoomMinMaxChanged(int32 p1); static bool MaxVehiclesChanged(int32 p1); +static bool ImprovedBreakdownsSettingChanged(int32 p1); #ifdef ENABLE_NETWORK static bool UpdateClientName(int32 p1); @@ -1143,7 +1144,7 @@ from = SL_IB guiflags = SGF_NO_NETWORK def = false str = STR_CONFIG_SETTING_IMPROVED_BREAKDOWNS - +proc = ImprovedBreakdownsSettingChanged ; station.join_stations [SDT_NULL] diff --git a/src/train.h b/src/train.h index e6acea7981..e41ca63c34 100644 --- a/src/train.h +++ b/src/train.h @@ -292,7 +292,7 @@ protected: // These functions should not be called outside acceleration code. */ inline AccelStatus GetAccelerationStatus() const { - return (this->vehstatus & VS_STOPPED) || HasBit(this->flags, VRF_REVERSING) || HasBit(this->flags, VRF_TRAIN_STUCK ) || HasBit(this->flags, VRF_BREAKDOWN_BRAKING) ? AS_BRAKE : AS_ACCEL; + return ((this->vehstatus & VS_STOPPED) || HasBit(this->flags, VRF_REVERSING) || HasBit(this->flags, VRF_TRAIN_STUCK) || HasBit(this->flags, VRF_BREAKDOWN_BRAKING)) ? AS_BRAKE : AS_ACCEL; } /** diff --git a/src/train_cmd.cpp b/src/train_cmd.cpp index 6cc941f562..a8261f22d3 100644 --- a/src/train_cmd.cpp +++ b/src/train_cmd.cpp @@ -129,7 +129,7 @@ void CheckTrainsLengths() void CheckBreakdownFlags(Train *v) { assert(v->IsFrontEngine()); - /* clear the flags we're gonna check first, we'll set them again later (if applicable ) */ + /* clear the flags we're gonna check first, we'll set them again later (if applicable) */ CLRBITS(v->flags, (1 << VRF_BREAKDOWN_BRAKING) | VRF_IS_BROKEN); for (const Train *w = v; w != NULL; w = w->Next()) { @@ -492,10 +492,8 @@ void Train::UpdateAcceleration() if (_settings_game.vehicle.improved_breakdowns) { if (_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL) { - this->breakdown_chance = max(128 * 3 / (this->tcache.cached_num_engines + 2), 5); + this->breakdown_chance_factor = max(128 * 3 / (this->tcache.cached_num_engines + 2), 5); } - } else { - this->breakdown_chance = 128; } } @@ -4055,7 +4053,6 @@ void Train::OnNewDay() if ((++this->day_counter & 7) == 0) DecreaseVehicleValue(this); if (this->IsFrontEngine()) { - CheckIfTrainNeedsService(this); CheckOrders(this); diff --git a/src/vehicle.cpp b/src/vehicle.cpp index d357d2c9f9..854ccd6458 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -123,6 +123,8 @@ void VehicleServiceInDepot(Vehicle *v) v->date_of_last_service = _date; v->breakdowns_since_last_service = 0; v->reliability = v->GetEngine()->reliability; + /* Prevent vehicles from breaking down directly after exiting the depot. */ + v->breakdown_chance = 0; v = v->Next(); } while (v != NULL && v->HasEngineType()); } @@ -144,7 +146,7 @@ bool Vehicle::NeedsServicing() const if ((this->ServiceIntervalIsPercent() ? (this->reliability >= this->GetEngine()->reliability * (100 - this->service_interval) / 100) : (this->date_of_last_service + this->service_interval >= _date)) - && !(this->type == VEH_TRAIN && HasBit(Train::From(this)->flags ,VRF_NEED_REPAIR))) { + && !(this->type == VEH_TRAIN && HasBit(Train::From(this)->flags, VRF_NEED_REPAIR))) { return false; } @@ -1277,15 +1279,21 @@ void CheckVehicleBreakdown(Vehicle *v) return; } - uint32 r1 = Random(); - uint32 r2 = Random(); + uint32 r = Random(); + + /* increase chance of failure */ + int chance = v->breakdown_chance + 1; + if (Chance16I(1, 25, r)) chance += 25; + chance = min(255, chance); + v->breakdown_chance = chance; - byte chance = 128; if (_settings_game.vehicle.improved_breakdowns) { - /* Dual engines have their breakdown chances reduced to 70% of the normal value */ - chance = (v->type == VEH_TRAIN && Train::From(v)->IsMultiheaded()) ? v->First()->breakdown_chance * 7 / 10 : v->First()->breakdown_chance; - } else if(v->type == VEH_SHIP) { - chance = 64; + if (v->type == VEH_TRAIN && Train::From(v)->IsMultiheaded()) { + /* Dual engines have their breakdown chances reduced to 70% of the normal value */ + chance = chance * 7 / 10; + } + chance *= v->First()->breakdown_chance_factor; + chance >>= 7; } /** * Chance is (1 - reliability) * breakdown_setting * breakdown_chance / 10. @@ -1295,9 +1303,12 @@ void CheckVehicleBreakdown(Vehicle *v) * However, because breakdowns are no longer by definition a complete stop, * their impact will be significantly less. */ + uint32 r1 = Random(); if ((uint32) (0xffff - v->reliability) * _settings_game.difficulty.vehicle_breakdowns * chance > GB(r1, 0, 24) * 10) { + uint32 r2 = Random(); v->breakdown_ctr = GB(r1, 24, 6) + 0xF; v->breakdown_delay = GB(r2, 0, 7) + 0x80; + v->breakdown_chance = 0; DetermineBreakdownType(v, r2); } } diff --git a/src/vehicle_base.h b/src/vehicle_base.h index ddf162b944..19d9b4a58f 100644 --- a/src/vehicle_base.h +++ b/src/vehicle_base.h @@ -195,6 +195,7 @@ public: byte breakdown_severity; ///< severity of the breakdown. Note that lower means more severe byte breakdown_type; ///< Type of breakdown + byte breakdown_chance_factor; ///< Improved breakdowns: current multiplier for breakdown_chance * 128, used for head vehicle only SpriteID colourmap; ///< NOSAVE: cached colour mapping /* Related to age and service time */ diff --git a/src/vehicle_gui.cpp b/src/vehicle_gui.cpp index 9df4badc16..f1f057f78b 100644 --- a/src/vehicle_gui.cpp +++ b/src/vehicle_gui.cpp @@ -2632,7 +2632,15 @@ public: if (w->breakdown_type == BREAKDOWN_LOW_SPEED) { SetDParam(1, min( w->First()->GetDisplayMaxSpeed(), w->breakdown_severity >> ((v->type == VEH_TRAIN) ? 0 : 1))); } else if (w->breakdown_type == BREAKDOWN_LOW_POWER) { - SetDParam(1, w->breakdown_severity * 100 / 256); + int percent; + if (v->type == VEH_TRAIN) { + uint32 power, te; + Train::From(v)->CalculatePower(power, te, true); + percent = (100 * power) / Train::From(v)->gcache.cached_power; + } else { + percent = w->breakdown_severity * 100 / 256; + } + SetDParam(1, percent); } } } else if (v->vehstatus & VS_STOPPED) {