diff options
author | yexo <yexo@openttd.org> | 2010-03-05 23:21:35 +0000 |
---|---|---|
committer | yexo <yexo@openttd.org> | 2010-03-05 23:21:35 +0000 |
commit | a71462bb94e0b1551cdf74734d205eb807915bf7 (patch) | |
tree | 628217c62ed5a163f964920ab534a87f20692a09 | |
parent | d03d990f33d9224295e90a1111394206a7cbfd42 (diff) | |
download | openttd-a71462bb94e0b1551cdf74734d205eb807915bf7.tar.xz |
(svn r19325) -Codechange: introduce airport classes and split the default airports in 4 classes
-rw-r--r-- | src/lang/english.txt | 5 | ||||
-rw-r--r-- | src/newgrf.cpp | 2 | ||||
-rw-r--r-- | src/newgrf_airport.cpp | 147 | ||||
-rw-r--r-- | src/newgrf_airport.h | 33 | ||||
-rw-r--r-- | src/table/airport_defaults.h | 30 |
5 files changed, 200 insertions, 17 deletions
diff --git a/src/lang/english.txt b/src/lang/english.txt index e9f64e728..e9bf0c003 100644 --- a/src/lang/english.txt +++ b/src/lang/english.txt @@ -2030,6 +2030,11 @@ STR_STATION_BUILD_AIRPORT_LARGE_AIRPORTS :{BLACK}Large ai STR_STATION_BUILD_AIRPORT_HUB_AIRPORTS :{BLACK}Hub airports STR_STATION_BUILD_AIRPORT_HELIPORTS :{BLACK}Helicopter airports +STR_AIRPORT_CLASS_SMALL :Small airports +STR_AIRPORT_CLASS_LARGE :Large airports +STR_AIRPORT_CLASS_HUB :Hub airports +STR_AIRPORT_CLASS_HELIPORTS :Helicopter airports + STR_STATION_BUILD_NOISE :{BLACK}Noise generated: {GOLD}{COMMA} # Landscaping toolbar diff --git a/src/newgrf.cpp b/src/newgrf.cpp index aca51117d..788e77cec 100644 --- a/src/newgrf.cpp +++ b/src/newgrf.cpp @@ -5907,6 +5907,7 @@ static void ResetNewGRFData() ResetCustomStations(); /* Reset airport-related structures */ + ResetAirportClasses(); ResetCustomAirports(); AirportSpec::ResetAirports(); AirportTileSpec::ResetAirportTiles(); @@ -6633,6 +6634,7 @@ static void AfterLoadGRFs() /* Add all new airports to the airports array. */ FinaliseAirportsArray(); + BindAirportSpecs(); /* Update the townname generators list */ InitGRFTownGeneratorNames(); diff --git a/src/newgrf_airport.cpp b/src/newgrf_airport.cpp index 20c0ba0e7..bec9f6146 100644 --- a/src/newgrf_airport.cpp +++ b/src/newgrf_airport.cpp @@ -14,9 +14,14 @@ #include "newgrf_airport.h" #include "date_func.h" #include "settings_type.h" +#include "core/alloc_type.hpp" +#include "newgrf.h" +#include "table/strings.h" -AirportSpec AirportSpec::dummy = {NULL, NULL, 0, 0, 0, 0, 0, MIN_YEAR, MIN_YEAR, ATP_TTDP_LARGE}; -AirportSpec AirportSpec::oilrig = {NULL, NULL, 0, 1, 1, 0, 4, MIN_YEAR, MIN_YEAR, ATP_TTDP_OILRIG}; +static AirportClass _airport_classes[APC_MAX]; + +AirportSpec AirportSpec::dummy = {NULL, NULL, 0, 0, 0, 0, 0, MIN_YEAR, MIN_YEAR, ATP_TTDP_LARGE, APC_BEGIN}; +AirportSpec AirportSpec::oilrig = {NULL, NULL, 0, 1, 1, 0, 4, MIN_YEAR, MIN_YEAR, ATP_TTDP_OILRIG, APC_BEGIN}; AirportSpec AirportSpec::specs[NUM_AIRPORTS]; @@ -62,3 +67,141 @@ void AirportSpec::ResetAirports() memset(&AirportSpec::specs, 0, sizeof(AirportSpec::specs)); memcpy(&AirportSpec::specs, &_origin_airport_specs, sizeof(AirportSpec) * NUM_AIRPORTS); } + +/** + * Allocate an airport class for the given class id + * @param cls A 32 bit value identifying the class + * @return Index into _airport_classes of allocated class + */ +AirportClassID AllocateAirportClass(uint32 cls) +{ + for (AirportClassID i = APC_BEGIN; i < APC_MAX; i++) { + if (_airport_classes[i].id == cls) { + /* ClassID is already allocated, so reuse it. */ + return i; + } else if (_airport_classes[i].id == 0) { + /* This class is empty, so allocate it to the ClassID. */ + _airport_classes[i].id = cls; + return i; + } + } + + grfmsg(2, "AllocateAirportClass: already allocated %d classes, using small airports class", APC_MAX); + return APC_SMALL; +} + +/** + * Set the name of an airport class + * @param id The id of the class to change the name from + * @param name The new name for the class + */ +void SetAirportClassName(AirportClassID id, StringID name) +{ + assert(id < APC_MAX); + _airport_classes[id].name = name; +} + +/** + * Retrieve the name of an airport class + * @param id The id of the airport class to get the name from + * @return The name of the given class + */ +StringID GetAirportClassName(AirportClassID id) +{ + assert(id < APC_MAX); + return _airport_classes[id].name; +} + +/** + * Get the number of airport classes in use + * @return The number of airport classes + */ +uint GetNumAirportClasses() +{ + uint i; + for (i = APC_BEGIN; i < APC_MAX && _airport_classes[i].id != 0; i++) {} + return i; +} + +/** + * Return the number of airports for the given airport class. + * @param id Index of the airport class. + * @return Number of airports in the class. + */ +uint GetNumAirportsInClass(AirportClassID id) +{ + assert(id < APC_MAX); + return _airport_classes[id].airports; +} + +/** + * Tie an airport spec to its airport class. + * @param statspec The airport spec. + */ +static void BindAirportSpecToClass(AirportSpec *as) +{ + assert(as->aclass < APC_MAX); + AirportClass *airport_class = &_airport_classes[as->aclass]; + + int i = airport_class->airports++; + airport_class->spec = ReallocT(airport_class->spec, airport_class->airports); + + airport_class->spec[i] = as; +} + +/** + * Tie all airportspecs to their class. + */ +void BindAirportSpecs() +{ + for (int i = 0; i < NUM_AIRPORTS; i++) { + AirportSpec *as = AirportSpec::GetWithoutOverride(i); + BindAirportSpecToClass(as); + } +} + + +/** + * Retrieve an airport spec from a class. + * @param aclass Index of the airport class. + * @param airport The airport index with the class. + * @return The station spec. + */ +const AirportSpec *GetAirportSpecFromClass(AirportClassID aclass, uint airport) +{ + assert(aclass < APC_MAX); + assert(airport < _airport_classes[aclass].airports); + + return _airport_classes[aclass].spec[airport]; +} + +/** + * Reset airport classes to their default state. + * This includes initialising the defaults classes with an empty + * entry, for standard airports. + */ +void ResetAirportClasses() +{ + for (AirportClassID i = APC_BEGIN; i < APC_MAX; i++) { + _airport_classes[i].id = 0; + _airport_classes[i].name = STR_EMPTY; + _airport_classes[i].airports = 0; + + free(_airport_classes[i].spec); + _airport_classes[i].spec = NULL; + } + + /* Set up initial data */ + AirportClassID id = AllocateAirportClass('SMAL'); + SetAirportClassName(id, STR_AIRPORT_CLASS_SMALL); + + id = AllocateAirportClass('LARG'); + SetAirportClassName(id, STR_AIRPORT_CLASS_LARGE); + + id = AllocateAirportClass('HUB_'); + SetAirportClassName(id, STR_AIRPORT_CLASS_HUB); + + id = AllocateAirportClass('HELI'); + SetAirportClassName(id, STR_AIRPORT_CLASS_HELIPORTS); +} + diff --git a/src/newgrf_airport.h b/src/newgrf_airport.h index bdbaafd64..52bdd2e99 100644 --- a/src/newgrf_airport.h +++ b/src/newgrf_airport.h @@ -14,6 +14,7 @@ #include "date_type.h" #include "map_type.h" +#include "strings_type.h" /* Copy from station_map.h */ typedef byte StationGfx; @@ -23,6 +24,19 @@ struct AirportTileTable { StationGfx gfx; }; +/** List of default airport classes. */ +enum AirportClassID { + APC_BEGIN = 0, ///< Lowest valid airport class id + APC_SMALL = 0, ///< id for small airports class + APC_LARGE, ///< id for large airports class + APC_HUB, ///< id for hub airports class + APC_HELIPORT, ///< id for heliports + APC_MAX = 16, ///< maximum number of airport classes +}; + +/** Allow incrementing of AirportClassID variables */ +DECLARE_POSTFIX_INCREMENT(AirportClassID); + /** TTDP airport types. Used to map our types to TTDPatch's */ enum TTDPAirportType { ATP_TTDP_SMALL, ///< Same as AT_SMALL @@ -45,6 +59,7 @@ struct AirportSpec { Year min_year; ///< first year the airport is available Year max_year; ///< last year the airport is available TTDPAirportType ttd_airport_type; ///< ttdpatch airport type (Small/Large/Helipad/Oilrig) + AirportClassID aclass; ///< the class to which this airport type belongs static const AirportSpec *Get(byte type); static AirportSpec *GetWithoutOverride(byte type); @@ -60,5 +75,23 @@ private: static AirportSpec specs[NUM_AIRPORTS]; }; +/** Information related to airport classes. */ +struct AirportClass { + uint32 id; ///< ID of this class, e.g. 'SMAL', 'LARG', 'HUB_', 'HELI', etc. + StringID name; ///< name of this class + uint airports; ///< number of airports in this class + AirportSpec **spec; ///< array of airport specifications +}; + +void ResetAirportClasses(); +AirportClassID AllocateAirportClass(uint32 cls); +void SetAirportClassName(AirportClassID id, StringID name); +StringID GetAirportClassName(AirportClassID id); + +uint GetNumAirportClasses(); +uint GetNumAirportsInClass(AirportClassID id); + +void BindAirportSpecs(); +const AirportSpec *GetAirportSpecFromClass(AirportClassID aclass, uint airport); #endif /* NEWGRF_AIRPORT_H */ diff --git a/src/table/airport_defaults.h b/src/table/airport_defaults.h index 97870522a..80404f078 100644 --- a/src/table/airport_defaults.h +++ b/src/table/airport_defaults.h @@ -377,28 +377,28 @@ static AirportTileTable *_tile_table_helistation[] = { #undef MKEND /** General AirportSpec definition. */ -#define AS_GENERIC(att, depot_tbl, num_depots, size_x, size_y, noise, catchment, min_year, max_year, ttdpatch_type) \ - {att, depot_tbl, num_depots, size_x, size_y, noise, catchment, min_year, max_year, ttdpatch_type} +#define AS_GENERIC(att, depot_tbl, num_depots, size_x, size_y, noise, catchment, min_year, max_year, ttdpatch_type, class_id) \ + {att, depot_tbl, num_depots, size_x, size_y, noise, catchment, min_year, max_year, ttdpatch_type, class_id} /** AirportSpec definition for airports without any depot. */ -#define AS_ND(ap_name, size_x, size_y, min_year, max_year, catchment, noise, ttdpatch_type) \ - AS_GENERIC(_tile_table_##ap_name, NULL, 0, size_x, size_y, noise, catchment, min_year, max_year, ttdpatch_type) +#define AS_ND(ap_name, size_x, size_y, min_year, max_year, catchment, noise, ttdpatch_type, class_id) \ + AS_GENERIC(_tile_table_##ap_name, NULL, 0, size_x, size_y, noise, catchment, min_year, max_year, ttdpatch_type, class_id) /** AirportSpec definition for airports with at least one depot. */ -#define AS(ap_name, size_x, size_y, min_year, max_year, catchment, noise, ttdpatch_type) \ - AS_GENERIC(_tile_table_##ap_name, _airport_depots_##ap_name, lengthof(_airport_depots_##ap_name), size_x, size_y, noise, catchment, min_year, max_year, ttdpatch_type) +#define AS(ap_name, size_x, size_y, min_year, max_year, catchment, noise, ttdpatch_type, class_id) \ + AS_GENERIC(_tile_table_##ap_name, _airport_depots_##ap_name, lengthof(_airport_depots_##ap_name), size_x, size_y, noise, catchment, min_year, max_year, ttdpatch_type, class_id) /* The helidepot and helistation have ATP_TTDP_SMALL because they are at ground level */ extern const AirportSpec _origin_airport_specs[] = { - AS(country, 4, 3, 0, 1959, 4, 3, ATP_TTDP_SMALL), - AS(city, 6, 6, 1955, MAX_YEAR, 5, 5, ATP_TTDP_LARGE), - AS_ND(heliport, 1, 1, 1963, MAX_YEAR, 4, 1, ATP_TTDP_HELIPORT), - AS(metropolitan, 6, 6, 1980, MAX_YEAR, 6, 8, ATP_TTDP_LARGE), - AS(international, 7, 7, 1990, MAX_YEAR, 8, 17, ATP_TTDP_LARGE), - AS(commuter, 5, 4, 1983, MAX_YEAR, 4, 4, ATP_TTDP_SMALL), - AS(helidepot, 2, 2, 1976, MAX_YEAR, 4, 2, ATP_TTDP_SMALL), - AS(intercontinental, 9, 11, 2002, MAX_YEAR, 10, 25, ATP_TTDP_LARGE), - AS(helistation, 4, 2, 1980, MAX_YEAR, 4, 3, ATP_TTDP_SMALL), + AS(country, 4, 3, 0, 1959, 4, 3, ATP_TTDP_SMALL, APC_SMALL), + AS(city, 6, 6, 1955, MAX_YEAR, 5, 5, ATP_TTDP_LARGE, APC_LARGE), + AS_ND(heliport, 1, 1, 1963, MAX_YEAR, 4, 1, ATP_TTDP_HELIPORT, APC_HELIPORT), + AS(metropolitan, 6, 6, 1980, MAX_YEAR, 6, 8, ATP_TTDP_LARGE, APC_LARGE), + AS(international, 7, 7, 1990, MAX_YEAR, 8, 17, ATP_TTDP_LARGE, APC_HUB), + AS(commuter, 5, 4, 1983, MAX_YEAR, 4, 4, ATP_TTDP_SMALL, APC_SMALL), + AS(helidepot, 2, 2, 1976, MAX_YEAR, 4, 2, ATP_TTDP_SMALL, APC_HELIPORT), + AS(intercontinental, 9, 11, 2002, MAX_YEAR, 10, 25, ATP_TTDP_LARGE, APC_HUB), + AS(helistation, 4, 2, 1980, MAX_YEAR, 4, 3, ATP_TTDP_SMALL, APC_HELIPORT), }; assert_compile(NUM_AIRPORTS == lengthof(_origin_airport_specs)); |