From be4aea0dadf6d6e917df99256bd7c13eeb26f08d Mon Sep 17 00:00:00 2001 From: Jonathan G Rennison Date: Sun, 25 Feb 2024 13:01:08 +0000 Subject: [PATCH] Rationalise handling of train too heavy advice messages --- src/ground_vehicle.cpp | 13 +++++++------ src/train.h | 1 - src/vehicle.cpp | 35 ++++++++++++++--------------------- src/vehicle_func.h | 2 ++ 4 files changed, 23 insertions(+), 28 deletions(-) diff --git a/src/ground_vehicle.cpp b/src/ground_vehicle.cpp index 08f4672b25..bc481b0a33 100644 --- a/src/ground_vehicle.cpp +++ b/src/ground_vehicle.cpp @@ -13,6 +13,8 @@ #include "depot_map.h" #include "tunnel_base.h" #include "slope_type.h" +#include "company_func.h" +#include "vehicle_func.h" #include "safeguards.h" @@ -290,7 +292,7 @@ GroundVehicleAcceleration GroundVehicle::GetAcceleration() int accel = ClampTo((force - resistance) / (mass * 4)); accel = force < resistance ? std::min(-1, accel) : std::max(1, accel); if (this->type == VEH_TRAIN) { - if(_settings_game.vehicle.train_acceleration_model == AM_ORIGINAL && + 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 */ uint32_t power; @@ -299,13 +301,12 @@ GroundVehicleAcceleration GroundVehicle::GetAcceleration() accel -= this->acceleration >> 1; } - if (this->IsFrontEngine() && !(this->current_order_time & 0x1FF) && + if (this->cur_speed < 3 && accel < 5 && + this->IsFrontEngine() && !(this->current_order_time & 0x3FF) && !(this->current_order.IsType(OT_LOADING)) && !(Train::From(this)->flags & (VRF_IS_BROKEN | (1 << VRF_TRAIN_STUCK))) && - this->cur_speed < 3 && accel < 5) { - SetBit(Train::From(this)->flags, VRF_TOO_HEAVY); - extern std::vector _tick_train_too_heavy_cache; - _tick_train_too_heavy_cache.push_back(Train::From(this)); + this->owner == _local_company) { + ShowTrainTooHeavyAdviceMessage(this); } if (Train::From(this)->UsingRealisticBraking() && _settings_game.vehicle.limit_train_acceleration) { diff --git a/src/train.h b/src/train.h index 733ff3e392..7f6781244b 100644 --- a/src/train.h +++ b/src/train.h @@ -39,7 +39,6 @@ enum VehicleRailFlags { VRF_BREAKDOWN_SPEED = 12,///< used to mark a train that has a reduced maximum speed because of a breakdown VRF_BREAKDOWN_STOPPED = 13,///< used to mark a train that is stopped because of a breakdown VRF_NEED_REPAIR = 14,///< used to mark a train that has a reduced maximum speed because of a critical breakdown - VRF_TOO_HEAVY = 15, VRF_BEYOND_PLATFORM_END = 16, VRF_NOT_YET_IN_PLATFORM = 17, VRF_ADVANCE_IN_PLATFORM = 18, diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 77f660681b..2dc716b5c4 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1426,8 +1426,19 @@ static void ShowAutoReplaceAdviceMessage(const CommandCost &res, const Vehicle * AddVehicleAdviceNewsItem(message, v->index); } +static std::vector _train_news_too_heavy_this_tick; + +void ShowTrainTooHeavyAdviceMessage(const Vehicle *v) +{ + if (find_index(_train_news_too_heavy_this_tick, v->index) < 0) { + _train_news_too_heavy_this_tick.push_back(v->index); + SetDParam(0, v->index); + AddNewsItem(STR_ERROR_TRAIN_TOO_HEAVY, NT_ADVICE, NF_INCOLOUR | NF_SMALL | NF_VEHICLE_PARAM0, + NR_VEHICLE, v->index); + } +} + bool _tick_caches_valid = false; -std::vector _tick_train_too_heavy_cache; std::vector _tick_train_front_cache; std::vector _tick_road_veh_front_cache; std::vector _tick_aircraft_front_cache; @@ -1439,7 +1450,6 @@ btree::btree_set _tick_effect_veh_cache; void ClearVehicleTickCaches() { - _tick_train_too_heavy_cache.clear(); _tick_train_front_cache.clear(); _tick_road_veh_front_cache.clear(); _tick_aircraft_front_cache.clear(); @@ -1471,7 +1481,6 @@ void RebuildVehicleTickCaches() break; case VEH_TRAIN: - if (HasBit(Train::From(v)->flags, VRF_TOO_HEAVY)) _tick_train_too_heavy_cache.push_back(Train::From(v)); if (v->Previous() == nullptr) _tick_train_front_cache.push_back(Train::From(v)); break; @@ -1499,11 +1508,6 @@ void ValidateVehicleTickCaches() { if (!_tick_caches_valid) return; - std::vector saved_tick_train_too_heavy_cache = std::move(_tick_train_too_heavy_cache); - std::sort(saved_tick_train_too_heavy_cache.begin(), saved_tick_train_too_heavy_cache.end(), [&](const Vehicle *a, const Vehicle *b) { - return a->index < b->index; - }); - saved_tick_train_too_heavy_cache.erase(std::unique(saved_tick_train_too_heavy_cache.begin(), saved_tick_train_too_heavy_cache.end()), saved_tick_train_too_heavy_cache.end()); std::vector saved_tick_train_front_cache = std::move(_tick_train_front_cache); std::vector saved_tick_road_veh_front_cache = std::move(_tick_road_veh_front_cache); std::vector saved_tick_aircraft_front_cache = std::move(_tick_aircraft_front_cache); @@ -1517,7 +1521,6 @@ void ValidateVehicleTickCaches() RebuildVehicleTickCaches(); - assert(saved_tick_train_too_heavy_cache == _tick_train_too_heavy_cache); assert(saved_tick_train_front_cache == saved_tick_train_front_cache); assert(saved_tick_road_veh_front_cache == _tick_road_veh_front_cache); assert(saved_tick_aircraft_front_cache == _tick_aircraft_front_cache); @@ -1566,6 +1569,8 @@ void CallVehicleTicks() _vehicles_to_pay_repair.clear(); _vehicles_to_sell.clear(); + _train_news_too_heavy_this_tick.clear(); + if (TickSkipCounter() == 0) RunVehicleDayProc(); if (EconTime::UsingWallclockUnits() && !CalTime::IsCalendarFrozen() && CalTime::CurSubDateFract() == 0) { @@ -1643,17 +1648,6 @@ void CallVehicleTicks() if (!_tick_effect_veh_cache.empty()) RecordSyncEvent(NSRE_VEH_EFFECT); { PerformanceMeasurer framerate(PFE_GL_TRAINS); - for (Train *t : _tick_train_too_heavy_cache) { - if (HasBit(t->flags, VRF_TOO_HEAVY)) { - if (t->owner == _local_company) { - SetDParam(0, t->index); - AddNewsItem(STR_ERROR_TRAIN_TOO_HEAVY, NT_ADVICE, NF_INCOLOUR | NF_SMALL | NF_VEHICLE_PARAM0, - NR_VEHICLE, t->index); - } - ClrBit(t->flags, VRF_TOO_HEAVY); - } - } - _tick_train_too_heavy_cache.clear(); for (Train *front : _tick_train_front_cache) { v = front; if (!front->Train::Tick()) continue; @@ -4475,7 +4469,6 @@ void DumpVehicleFlagsGeneric(const Vehicle *v, T dump, U dump_header) dump('v', "VRF_BREAKDOWN_SPEED", HasBit(t->flags, VRF_BREAKDOWN_SPEED)); dump('z', "VRF_BREAKDOWN_STOPPED", HasBit(t->flags, VRF_BREAKDOWN_STOPPED)); dump('F', "VRF_NEED_REPAIR", HasBit(t->flags, VRF_NEED_REPAIR)); - dump('H', "VRF_TOO_HEAVY", HasBit(t->flags, VRF_TOO_HEAVY)); dump('B', "VRF_BEYOND_PLATFORM_END", HasBit(t->flags, VRF_BEYOND_PLATFORM_END)); dump('Y', "VRF_NOT_YET_IN_PLATFORM", HasBit(t->flags, VRF_NOT_YET_IN_PLATFORM)); dump('A', "VRF_ADVANCE_IN_PLATFORM", HasBit(t->flags, VRF_ADVANCE_IN_PLATFORM)); diff --git a/src/vehicle_func.h b/src/vehicle_func.h index ec53ec4954..2bdb33130b 100644 --- a/src/vehicle_func.h +++ b/src/vehicle_func.h @@ -272,4 +272,6 @@ bool VehiclesHaveSameOrderList(const Vehicle *v1, const Vehicle *v2); bool IsUniqueVehicleName(const char *name); +void ShowTrainTooHeavyAdviceMessage(const Vehicle *v); + #endif /* VEHICLE_FUNC_H */