From ad30a3c4caaad54f1b0b279bb0fc78254016efe3 Mon Sep 17 00:00:00 2001 From: peter1138 Date: Mon, 9 Feb 2009 22:49:28 +0000 Subject: [PATCH] (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... --- src/ai/api/ai_rail.cpp | 13 +++---------- src/newgrf_station.cpp | 17 +++++++++++++---- src/newgrf_station.h | 2 +- src/saveload/station_sl.cpp | 2 +- 4 files changed, 18 insertions(+), 16 deletions(-) diff --git a/src/ai/api/ai_rail.cpp b/src/ai/api/ai_rail.cpp index 5d08690d94..9ef1c4f53a 100644 --- 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; diff --git a/src/newgrf_station.cpp b/src/newgrf_station.cpp index 5bca75fe2b..26b49cc877 100644 --- 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 @@ const StationSpec *GetCustomStationSpec(StationClassID sclass, uint station) 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 @@ const StationSpec *GetCustomStationSpecByGrf(uint32 grfid, byte localidx) 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; + } } } diff --git a/src/newgrf_station.h b/src/newgrf_station.h index 45552e20bf..9af801f05d 100644 --- a/src/newgrf_station.h +++ b/src/newgrf_station.h @@ -118,7 +118,7 @@ uint GetNumCustomStations(StationClassID sclass); 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); diff --git a/src/saveload/station_sl.cpp b/src/saveload/station_sl.cpp index b03e59c7df..e166e93a0e 100644 --- a/src/saveload/station_sl.cpp +++ b/src/saveload/station_sl.cpp @@ -20,7 +20,7 @@ void AfterLoadStations() 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();