peter1138@2625: /* $Id$ */ peter1138@2625: peter1138@2625: /** @file station_newgrf.c Functions for dealing with station classes and custom stations. */ peter1138@2625: peter1138@2625: #include "stdafx.h" peter1138@2625: #include "openttd.h" peter1138@2625: #include "debug.h" peter1138@2625: #include "sprite.h" peter1138@2625: #include "station_newgrf.h" peter1138@2625: peter1138@2625: static StationClass station_classes[STAT_CLASS_MAX]; peter1138@2625: peter1138@2625: /** peter1138@2625: * Reset station classes to their default state. peter1138@2625: * This includes initialising the Default and Waypoint classes with an empty peter1138@2625: * entry, for standard stations and waypoints. peter1138@2625: */ peter1138@2625: void ResetStationClasses(void) peter1138@2625: { peter1138@2625: StationClassID i; peter1138@2625: for (i = 0; i < STAT_CLASS_MAX; i++) { peter1138@2625: station_classes[i].id = 0; peter1138@2625: peter1138@2625: free(station_classes[i].name); peter1138@2625: station_classes[i].name = NULL; peter1138@2625: peter1138@2625: station_classes[i].stations = 0; peter1138@2625: peter1138@2625: free(station_classes[i].spec); peter1138@2625: station_classes[i].spec = NULL; peter1138@2625: } peter1138@2625: peter1138@2625: // Set up initial data peter1138@2625: station_classes[0].id = 'DFLT'; peter1138@2625: station_classes[0].name = strdup("Default"); peter1138@2625: station_classes[0].stations = 1; peter1138@2625: station_classes[0].spec = malloc(sizeof(*station_classes[0].spec)); peter1138@2625: station_classes[0].spec[0] = NULL; peter1138@2625: peter1138@2625: station_classes[1].id = 'WAYP'; peter1138@2625: station_classes[1].name = strdup("Waypoints"); peter1138@2625: station_classes[1].stations = 1; peter1138@2625: station_classes[1].spec = malloc(sizeof(*station_classes[1].spec)); peter1138@2625: station_classes[1].spec[0] = NULL; peter1138@2625: } peter1138@2625: peter1138@2625: /** peter1138@2625: * Allocate a station class for the given class id. peter1138@2625: * @param classid A 32 bit value identifying the class. peter1138@2625: * @return Index into station_classes of allocated class. peter1138@2625: */ peter1138@2625: StationClassID AllocateStationClass(uint32 class) peter1138@2625: { peter1138@2625: StationClassID i; peter1138@2625: peter1138@2625: for (i = 0; i < STAT_CLASS_MAX; i++) { peter1138@2625: if (station_classes[i].id == class) { peter1138@2625: // ClassID is already allocated, so reuse it. peter1138@2625: return i; peter1138@2625: } else if (station_classes[i].id == 0) { peter1138@2625: // This class is empty, so allocate it to the ClassID. peter1138@2625: station_classes[i].id = class; peter1138@2625: return i; peter1138@2625: } peter1138@2625: } peter1138@2625: peter1138@2625: DEBUG(grf, 2)("StationClassAllocate: Already allocated %d classes, using default.", STAT_CLASS_MAX); peter1138@2625: return STAT_CLASS_DFLT; peter1138@2625: } peter1138@2625: peter1138@2625: /** peter1138@2625: * Return the number of stations for the given station class. peter1138@2625: * @param sclass Index of the station class. peter1138@2625: * @return Number of stations in the class. peter1138@2625: */ peter1138@2625: uint GetNumCustomStations(StationClassID sclass) peter1138@2625: { peter1138@2625: assert(sclass < STAT_CLASS_MAX); peter1138@2625: return station_classes[sclass].stations; peter1138@2625: } peter1138@2625: peter1138@2625: /** peter1138@2625: * Tie a station spec to its station class. peter1138@2625: * @param spec The station spec. peter1138@2625: */ peter1138@2625: void SetCustomStation(StationSpec *spec) peter1138@2625: { peter1138@2625: StationClass *station_class; peter1138@2625: int i; peter1138@2625: peter1138@2625: assert(spec->sclass < STAT_CLASS_MAX); peter1138@2625: station_class = &station_classes[spec->sclass]; peter1138@2625: peter1138@2625: i = station_class->stations++; peter1138@2625: station_class->spec = realloc(station_class->spec, station_class->stations * sizeof(*station_class->spec)); peter1138@2625: peter1138@2625: station_class->spec[i] = spec; peter1138@2625: } peter1138@2625: peter1138@2625: /** peter1138@2625: * Retrieve a station spec from a class. peter1138@2625: * @param sclass Index of the station class. peter1138@2625: * @param station The station index with the class. peter1138@2625: * @return The station spec. peter1138@2625: */ peter1138@2625: const StationSpec *GetCustomStation(StationClassID sclass, uint station) peter1138@2625: { peter1138@2625: assert(sclass < STAT_CLASS_MAX); peter1138@2625: if (station < station_classes[sclass].stations) peter1138@2625: return station_classes[sclass].spec[station]; peter1138@2625: peter1138@2625: // If the custom station isn't defined any more, then the GRF file peter1138@2625: // probably was not loaded. peter1138@2625: return NULL; peter1138@2625: }