(svn r20406) -Codechange: make StationClass use the new generic class

This commit is contained in:
rubidium
2010-08-07 22:07:07 +00:00
parent cb4ab6a231
commit edbadde301
9 changed files with 60 additions and 208 deletions

View File

@@ -29,10 +29,28 @@
#include "tunnelbridge_map.h"
#include "newgrf.h"
#include "core/random_func.hpp"
#include "newgrf_class_func.h"
#include "table/strings.h"
static StationClass _station_classes[STAT_CLASS_MAX];
template <typename Tspec, typename Tid, Tid Tmax>
/* static */ void NewGRFClass<Tspec, Tid, Tmax>::InsertDefaults()
{
/* Set up initial data */
classes[0].global_id = 'DFLT';
classes[0].name = STR_STATION_CLASS_DFLT;
classes[0].count = 1;
classes[0].spec = MallocT<StationSpec*>(1);
classes[0].spec[0] = NULL;
classes[1].global_id = 'WAYP';
classes[1].name = STR_STATION_CLASS_WAYP;
classes[1].count = 1;
classes[1].spec = MallocT<StationSpec*>(1);
classes[1].spec[0] = NULL;
}
INSTANTIATE_NEWGRF_CLASS_METHODS(StationClass, StationSpec, StationClassID, STAT_CLASS_MAX)
static const uint MAX_SPECLIST = 255;
@@ -76,154 +94,6 @@ struct ETileArea : TileArea {
};
/**
* Reset station classes to their default state.
* This includes initialising the Default and Waypoint classes with an empty
* entry, for standard stations and waypoints.
*/
void ResetStationClasses()
{
for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) {
_station_classes[i].id = 0;
_station_classes[i].name = STR_EMPTY;
_station_classes[i].stations = 0;
free(_station_classes[i].spec);
_station_classes[i].spec = NULL;
}
/* Set up initial data */
_station_classes[0].id = 'DFLT';
_station_classes[0].name = STR_STATION_CLASS_DFLT;
_station_classes[0].stations = 1;
_station_classes[0].spec = MallocT<StationSpec*>(1);
_station_classes[0].spec[0] = NULL;
_station_classes[1].id = 'WAYP';
_station_classes[1].name = STR_STATION_CLASS_WAYP;
_station_classes[1].stations = 1;
_station_classes[1].spec = MallocT<StationSpec*>(1);
_station_classes[1].spec[0] = NULL;
}
/**
* Allocate a station class for the given class id.
* @param cls A 32 bit value identifying the class.
* @return Index into _station_classes of allocated class.
*/
StationClassID AllocateStationClass(uint32 cls)
{
for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) {
if (_station_classes[i].id == cls) {
/* ClassID is already allocated, so reuse it. */
return i;
} else if (_station_classes[i].id == 0) {
/* This class is empty, so allocate it to the ClassID. */
_station_classes[i].id = cls;
return i;
}
}
grfmsg(2, "StationClassAllocate: already allocated %d classes, using default", STAT_CLASS_MAX);
return STAT_CLASS_DFLT;
}
/** Set the name of a custom station class */
void SetStationClassName(StationClassID sclass, StringID name)
{
assert(sclass < STAT_CLASS_MAX);
_station_classes[sclass].name = name;
}
/** Retrieve the name of a custom station class */
StringID GetStationClassName(StationClassID sclass)
{
assert(sclass < STAT_CLASS_MAX);
return _station_classes[sclass].name;
}
/**
* Get the number of station classes in use.
* @return Number of station classes.
*/
uint GetNumStationClasses()
{
uint i;
for (i = 0; i < STAT_CLASS_MAX && _station_classes[i].id != 0; i++) {}
return i;
}
/**
* Return the number of stations for the given station class.
* @param sclass Index of the station class.
* @return Number of stations in the class.
*/
uint GetNumCustomStations(StationClassID sclass)
{
assert(sclass < STAT_CLASS_MAX);
return _station_classes[sclass].stations;
}
/**
* Tie a station spec to its station class.
* @param statspec The station spec.
*/
void SetCustomStationSpec(StationSpec *statspec)
{
StationClass *station_class;
int i;
assert(statspec->cls_id < STAT_CLASS_MAX);
station_class = &_station_classes[statspec->cls_id];
i = station_class->stations++;
station_class->spec = ReallocT(station_class->spec, station_class->stations);
station_class->spec[i] = statspec;
}
/**
* Retrieve a station spec from a class.
* @param sclass Index of the station class.
* @param station The station index with the class.
* @return The station spec.
*/
const StationSpec *GetCustomStationSpec(StationClassID sclass, uint station)
{
assert(sclass < STAT_CLASS_MAX);
if (station < _station_classes[sclass].stations) return _station_classes[sclass].spec[station];
/* If the custom station isn't defined any more, then the GRF file
* probably was not loaded. */
return NULL;
}
/**
* 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;
for (StationClassID i = STAT_CLASS_BEGIN; i < STAT_CLASS_MAX; i++) {
for (j = 0; j < _station_classes[i].stations; j++) {
const StationSpec *statspec = _station_classes[i].spec[j];
if (statspec == NULL) continue;
if (statspec->grf_prop.grffile->grfid == grfid && statspec->grf_prop.local_id == localidx) {
if (index != NULL) *index = j;
return statspec;
}
}
}
return NULL;
}
/* Evaluate a tile's position within a station, and return the result a bit-stuffed format.
* if not centered: .TNLcCpP, if centered: .TNL..CP
* T = Tile layout number (#GetStationGfx), N = Number of platforms, L = Length of platforms
@@ -877,7 +747,7 @@ bool DrawStationTile(int x, int y, RailType railtype, Axis axis, StationClassID
PaletteID palette = COMPANY_SPRITE_COLOUR(_local_company);
uint tile = 2;
statspec = GetCustomStationSpec(sclass, station);
statspec = StationClass::Get(sclass, station);
if (statspec == NULL) return false;
uint relocation = GetCustomStationRelocation(statspec, NULL, INVALID_TILE);