diff --git a/src/openttd.cpp b/src/openttd.cpp index 4cb5d17752..1e6b2c1d9c 100644 --- a/src/openttd.cpp +++ b/src/openttd.cpp @@ -1494,6 +1494,9 @@ void CheckCaches(bool force_check) FOR_ALL_ORDER_LISTS(order_list) { order_list->DebugCheckSanity(); } + + extern void ValidateVehicleTickCaches(); + ValidateVehicleTickCaches(); } /** diff --git a/src/vehicle.cpp b/src/vehicle.cpp index 0e2f57edc4..050b498b75 100644 --- a/src/vehicle.cpp +++ b/src/vehicle.cpp @@ -1193,6 +1193,35 @@ void RebuildVehicleTickCaches() _tick_caches_valid = true; } +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); + std::vector saved_tick_ship_cache = std::move(_tick_ship_cache); + std::vector saved_tick_effect_veh_cache = std::move(_tick_effect_veh_cache); + std::vector saved_tick_other_veh_cache = std::move(_tick_other_veh_cache); + saved_tick_effect_veh_cache.erase(std::remove(saved_tick_effect_veh_cache.begin(), saved_tick_effect_veh_cache.end(), nullptr), saved_tick_effect_veh_cache.end()); + saved_tick_other_veh_cache.erase(std::remove(saved_tick_other_veh_cache.begin(), saved_tick_other_veh_cache.end(), nullptr), saved_tick_other_veh_cache.end()); + + 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); + assert(saved_tick_ship_cache == _tick_ship_cache); + assert(saved_tick_effect_veh_cache == _tick_effect_veh_cache); + assert(saved_tick_other_veh_cache == _tick_other_veh_cache); +} + void VehicleTickCargoAging(Vehicle *v) { if (v->vcache.cached_cargo_age_period != 0) {