diff --git a/src/lang/english.txt b/src/lang/english.txt index e39c171ee5..c04fc3a3aa 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -6055,6 +6055,7 @@ STR_GAME_SAVELOAD_ERROR_DATA_INTEGRITY_CHECK_FAILED :Data integrity STR_GAME_SAVELOAD_ERROR_PATCHPACK :Savegame is made with a modified version STR_GAME_SAVELOAD_NOT_AVAILABLE : STR_WARNING_LOADGAME_REMOVED_TRAMS :{WHITE}Game was saved in version without tram support. All trams have been removed +STR_WARNING_LOADGAME_REMOVED_UNCORRECTABLE_VEHICLES :{WHITE}Removed {NUM} vehicles found to be uncorrectably invalid during load STR_GAME_SAVELOAD_FROM_VERSION : from version {PUSH_COLOUR}{ORANGE}{RAW_STRING}{POP_COLOUR}{} STR_GAME_SAVELOAD_ERROR_TOO_NEW_FEATURE_VERSION :Savegame{0:STRING1} has version {PUSH_COLOUR}{ORANGE}{2:NUM}{POP_COLOUR} of feature '{PUSH_COLOUR}{ORANGE}{1:RAW_STRING}{POP_COLOUR}', but the maximum supported version is {PUSH_COLOUR}{ORANGE}{3:NUM}{POP_COLOUR} diff --git a/src/saveload/afterload.cpp b/src/saveload/afterload.cpp index 811ef94404..f071ae95c8 100644 --- a/src/saveload/afterload.cpp +++ b/src/saveload/afterload.cpp @@ -4109,6 +4109,8 @@ bool AfterLoadGame() AfterLoadCompanyStats(); AfterLoadStoryBook(); + AfterLoadVehiclesRemoveAnyFoundInvalid(); + GamelogPrintDebug(1); InitializeWindowsAndCaches(); diff --git a/src/saveload/saveload_internal.h b/src/saveload/saveload_internal.h index 2812edf8ce..41f4a8367d 100644 --- a/src/saveload/saveload_internal.h +++ b/src/saveload/saveload_internal.h @@ -26,6 +26,7 @@ void MoveWaypointsToBaseStations(); SaveLoadTable GetBaseStationDescription(); void AfterLoadVehicles(bool part_of_load); +void AfterLoadVehiclesRemoveAnyFoundInvalid(); void AfterLoadEngines(); void FixupTrainLengths(); void AfterLoadTemplateVehicles(); diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp index 6a82e20e92..76cdba5861 100644 --- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -20,6 +20,8 @@ #include "../disaster_vehicle.h" #include "../scope_info.h" #include "../string_func.h" +#include "../error.h" +#include "../strings_func.h" #include "saveload.h" @@ -238,9 +240,13 @@ static void CheckValidVehicles() extern byte _age_cargo_skip_counter; // From misc_sl.cpp +static std::vector _load_invalid_vehicles_to_delete; + /** Called after load to update coordinates */ void AfterLoadVehicles(bool part_of_load) { + _load_invalid_vehicles_to_delete.clear(); + const Vehicle *si_v = nullptr; SCOPE_INFO_FMT([&si_v], "AfterLoadVehicles: %s", scope_dumper().VehicleInfo(si_v)); for (Vehicle *v : Vehicle::Iterate()) { @@ -427,9 +433,16 @@ void AfterLoadVehicles(bool part_of_load) rv->roadtype = Engine::Get(rv->engine_type)->u.road.roadtype; rv->compatible_roadtypes = GetRoadTypeInfo(rv->roadtype)->powered_roadtypes; + bool is_invalid = false; for (RoadVehicle *u = rv; u != nullptr; u = u->Next()) { u->roadtype = rv->roadtype; u->compatible_roadtypes = rv->compatible_roadtypes; + if (GetRoadType(u->tile, GetRoadTramType(u->roadtype)) == INVALID_ROADTYPE) is_invalid = true; + } + + if (is_invalid && part_of_load) { + _load_invalid_vehicles_to_delete.push_back(rv); + break; } RoadVehUpdateCache(rv); @@ -510,6 +523,21 @@ void AfterLoadVehicles(bool part_of_load) } } +void AfterLoadVehiclesRemoveAnyFoundInvalid() +{ + if (!_load_invalid_vehicles_to_delete.empty()) { + DEBUG(sl, 0, "Removing %u vehicles found to be uncorrectably invalid during load", (uint)_load_invalid_vehicles_to_delete.size()); + SetDParam(0, (uint)_load_invalid_vehicles_to_delete.size()); + ShowErrorMessage(STR_WARNING_LOADGAME_REMOVED_UNCORRECTABLE_VEHICLES, INVALID_STRING_ID, WL_CRITICAL); + GroupStatistics::UpdateAfterLoad(); + } + + for (Vehicle *v : _load_invalid_vehicles_to_delete) { + delete v; + } + _load_invalid_vehicles_to_delete.clear(); +} + bool TrainController(Train *v, Vehicle *nomove, bool reverse = true); // From train_cmd.cpp void ReverseTrainDirection(Train *v); void ReverseTrainSwapVeh(Train *v, int l, int r);