(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...

This commit is contained in:
peter1138 2009-02-09 22:49:28 +00:00
parent d846eef0b6
commit ad30a3c4ca
4 changed files with 18 additions and 16 deletions

View File

@ -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;

View File

@ -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;
}
}
}

View File

@ -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);

View File

@ -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();