changeset 4812:f520062e491e draft

(svn r6736) - Add a function to get the list position of an engine ID, for use with sorting. This is basically a reversed version of GetRailVehAtPosition()
author peter1138 <peter1138@openttd.org>
date Wed, 11 Oct 2006 18:35:40 +0000
parents 04822ed462c7
children 1cdd00fcc76c
files newgrf_engine.c newgrf_engine.h
diffstat 2 files changed, 28 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/newgrf_engine.c
+++ b/newgrf_engine.c
@@ -1124,6 +1124,7 @@
 // Functions for changing the order of vehicle purchase lists
 // This is currently only implemented for rail vehicles.
 static EngineID _engine_list_order[NUM_TRAIN_ENGINES];
+static byte _engine_list_position[NUM_TRAIN_ENGINES];
 
 void ResetEngineListOrder(void)
 {
@@ -1131,12 +1132,32 @@
 
 	for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
 		_engine_list_order[i] = i;
+		_engine_list_position[i] = i;
 	}
 }
 
+/**
+ * Get the EngineID at position pos.
+ * Used when drawing a(n unsorted) list of engines.
+ * @param pos List position/
+ * @return The EngineID at the requested position.
+ */
 EngineID GetRailVehAtPosition(EngineID pos)
 {
-	return _engine_list_order[pos];
+	if (pos < NUM_TRAIN_ENGINES) return _engine_list_order[pos];
+	return pos;
+}
+
+/**
+ * Get the list position of an engine.
+ * Used when sorting a list of engines.
+ * @param engine ID of the engine.
+ * @return The list position of the engine.
+ */
+uint16 ListPositionOfEngine(EngineID engine)
+{
+	if (engine < NUM_TRAIN_ENGINES) return _engine_list_position[engine];
+	return engine;
 }
 
 void AlterRailVehListOrder(EngineID engine, EngineID target)
@@ -1160,4 +1181,9 @@
 			break;
 		}
 	}
+
+	// Update the engine list position (a reverse of engine list order)
+	for (i = 0; i < NUM_TRAIN_ENGINES; i++) {
+		_engine_list_position[_engine_list_order[i]] = i;
+	}
 }
--- a/newgrf_engine.h
+++ b/newgrf_engine.h
@@ -62,6 +62,7 @@
 
 void ResetEngineListOrder(void);
 EngineID GetRailVehAtPosition(EngineID pos);
+uint16 ListPositionOfEngine(EngineID engine);
 void AlterRailVehListOrder(EngineID engine, EngineID target);
 
 #endif /* NEWGRF_ENGINE_H */