summaryrefslogtreecommitdiff
path: root/src/saveload
diff options
context:
space:
mode:
authorpeter1138 <peter1138@openttd.org>2009-01-25 21:55:18 +0000
committerpeter1138 <peter1138@openttd.org>2009-01-25 21:55:18 +0000
commit7afd506301766c49289eb313bf8a1912cd50c2c0 (patch)
treee80faf5c1dd6673203220d8466df7caef421c85c /src/saveload
parentc4ccf1a28d415a9035718d3126241cc459f63c79 (diff)
downloadopenttd-7afd506301766c49289eb313bf8a1912cd50c2c0.tar.xz
(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.
Diffstat (limited to 'src/saveload')
-rw-r--r--src/saveload/vehicle_sl.cpp39
1 files changed, 39 insertions, 0 deletions
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);