changeset 11091:c4c35a89bad7 draft

(svn r15436) -Codechange: Return index of station spec within station class as a return parameter of GetCustomStationSpecByGrf(), as the index is already known. Saves on an extra loop and an extern...
author peter1138 <peter1138@openttd.org>
date Mon, 09 Feb 2009 22:49:28 +0000
parents df23c4e04638
children 3ff2dacbbc5e
files src/ai/api/ai_rail.cpp src/newgrf_station.cpp src/newgrf_station.h src/saveload/station_sl.cpp
diffstat 4 files changed, 18 insertions(+), 16 deletions(-) [+]
line wrap: on
line diff
--- a/src/ai/api/ai_rail.cpp
+++ b/src/ai/api/ai_rail.cpp
@@ -155,16 +155,9 @@
 	uint16 res = GetAiPurchaseCallbackResult(GSF_STATION, cargo_id, 0, source_industry, goal_industry, min(255, distance / 2), AICE_STATION_GET_STATION_ID, source_station ? 0 : 1, min(15, num_platforms) << 4 | min(15, platform_length), &file);
 	uint32 p2 = INVALID_STATION << 16;
 	if (res != CALLBACK_FAILED) {
-		extern StationClass _station_classes[STAT_CLASS_MAX];
-		const StationSpec *spec = GetCustomStationSpecByGrf(file->grfid, res);
-		int index = -1;
-		for (uint j = 0; j < _station_classes[spec->sclass].stations; j++) {
-			if (spec == _station_classes[spec->sclass].spec[j]) {
-				index = j;
-				break;
-			}
-		}
-		if (index == -1) {
+		int index = 0;
+		const StationSpec *spec = GetCustomStationSpecByGrf(file->grfid, res, &index);
+		if (spec == NULL) {
 			DEBUG(grf, 1, "%s returned an invalid station ID for 'AI construction/purchase selection (18)' callback", file->filename);
 		} else {
 			p2 |= spec->sclass | index << 8;
--- a/src/newgrf_station.cpp
+++ b/src/newgrf_station.cpp
@@ -23,7 +23,7 @@
 
 #include "table/strings.h"
 
-StationClass _station_classes[STAT_CLASS_MAX];
+static StationClass _station_classes[STAT_CLASS_MAX];
 
 enum {
 	MAX_SPECLIST = 255,
@@ -156,8 +156,14 @@
 	return NULL;
 }
 
-
-const StationSpec *GetCustomStationSpecByGrf(uint32 grfid, byte localidx)
+/**
+ * Retrieve a station spec by GRF location.
+ * @param grfid    GRF ID of station spec.
+ * @param localidx Index within GRF file of station spec.
+ * @param index    Pointer to return the index of the station spec in its station class. If NULL then not used.
+ * @return The station spec.
+ */
+const StationSpec *GetCustomStationSpecByGrf(uint32 grfid, byte localidx, int *index)
 {
 	uint j;
 
@@ -165,7 +171,10 @@
 		for (j = 0; j < _station_classes[i].stations; j++) {
 			const StationSpec *statspec = _station_classes[i].spec[j];
 			if (statspec == NULL) continue;
-			if (statspec->grffile->grfid == grfid && statspec->localidx == localidx) return statspec;
+			if (statspec->grffile->grfid == grfid && statspec->localidx == localidx) {
+				if (index != NULL) *index = j;
+				return statspec;
+			}
 		}
 	}
 
--- a/src/newgrf_station.h
+++ b/src/newgrf_station.h
@@ -118,7 +118,7 @@
 
 void SetCustomStationSpec(StationSpec *statspec);
 const StationSpec *GetCustomStationSpec(StationClassID sclass, uint station);
-const StationSpec *GetCustomStationSpecByGrf(uint32 grfid, byte localidx);
+const StationSpec *GetCustomStationSpecByGrf(uint32 grfid, byte localidx, int *index);
 
 /* Evaluate a tile's position within a station, and return the result a bitstuffed format. */
 uint32 GetPlatformInfo(Axis axis, byte tile, int platforms, int length, int x, int y, bool centred);
--- a/src/saveload/station_sl.cpp
+++ b/src/saveload/station_sl.cpp
@@ -20,7 +20,7 @@
 		for (uint i = 0; i < st->num_specs; i++) {
 			if (st->speclist[i].grfid == 0) continue;
 
-			st->speclist[i].spec = GetCustomStationSpecByGrf(st->speclist[i].grfid, st->speclist[i].localidx);
+			st->speclist[i].spec = GetCustomStationSpecByGrf(st->speclist[i].grfid, st->speclist[i].localidx, NULL);
 		}
 
 		for (CargoID c = 0; c < NUM_CARGO; c++) st->goods[c].cargo.InvalidateCache();