From 7afd506301766c49289eb313bf8a1912cd50c2c0 Mon Sep 17 00:00:00 2001 From: peter1138 Date: Sun, 25 Jan 2009 21:55:18 +0000 Subject: (svn r15272) -Codechange: Check that engine types of vehicles are valid and for the correct type on loading a game. This prevents a crash/assertion failure if required NewGRFs are not available. This won't make the game playable, but will let you see what NewGRFs are required. --- src/saveload/vehicle_sl.cpp | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) (limited to 'src') diff --git a/src/saveload/vehicle_sl.cpp b/src/saveload/vehicle_sl.cpp index 0fe73fe99..90d7ef312 100644 --- a/src/saveload/vehicle_sl.cpp +++ b/src/saveload/vehicle_sl.cpp @@ -200,6 +200,43 @@ void UpdateOldAircraft() } } +/** + * Check all vehicles to ensure their engine type is valid + * for the currently loaded NewGRFs (that includes none...) + * This only makes a difference if NewGRFs are missing, otherwise + * all vehicles will be valid. This does not make such a game + * playable, it only prevents crash. + */ +static void CheckValidVehicles() +{ + uint total_engines = GetEnginePoolSize(); + EngineID first_engine[4] = { INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE, INVALID_ENGINE }; + + Engine *e; + FOR_ALL_ENGINES_OF_TYPE(e, VEH_TRAIN) { first_engine[VEH_TRAIN] = e->index; break; } + FOR_ALL_ENGINES_OF_TYPE(e, VEH_ROAD) { first_engine[VEH_ROAD] = e->index; break; } + FOR_ALL_ENGINES_OF_TYPE(e, VEH_SHIP) { first_engine[VEH_SHIP] = e->index; break; } + FOR_ALL_ENGINES_OF_TYPE(e, VEH_AIRCRAFT) { first_engine[VEH_AIRCRAFT] = e->index; break; } + + Vehicle *v; + FOR_ALL_VEHICLES(v) { + /* Test if engine types match */ + switch (v->type) { + case VEH_TRAIN: + case VEH_ROAD: + case VEH_SHIP: + case VEH_AIRCRAFT: + if (v->engine_type >= total_engines || v->type != GetEngine(v->engine_type)->type) { + v->engine_type = first_engine[v->type]; + } + break; + + default: + break; + } + } +} + /** Called after load to update coordinates */ void AfterLoadVehicles(bool part_of_load) { @@ -262,6 +299,8 @@ void AfterLoadVehicles(bool part_of_load) } } + CheckValidVehicles(); + FOR_ALL_VEHICLES(v) { assert(v->first != NULL); -- cgit v1.2.3-54-g00ecf