Codechange: Use CargoArray for linkgraph refresher. (#13165)
[openttd-github.git] / src / newgrf_airport.h
blob3242e482c58423bafee0be4b94c67a2cbd1d2c99
1 /*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
6 */
8 /** @file newgrf_airport.h NewGRF handling of airports. */
10 #ifndef NEWGRF_AIRPORT_H
11 #define NEWGRF_AIRPORT_H
13 #include "airport.h"
14 #include "timer/timer_game_calendar.h"
15 #include "newgrf_class.h"
16 #include "newgrf_commons.h"
17 #include "newgrf_spritegroup.h"
18 #include "newgrf_town.h"
19 #include "tilearea_type.h"
21 /** Copy from station_map.h */
22 typedef uint8_t StationGfx;
24 /** Tile-offset / AirportTileID pair. */
25 struct AirportTileTable {
26 TileIndexDiffC ti; ///< Tile offset from the top-most airport tile.
27 StationGfx gfx; ///< AirportTile to use for this tile.
30 /** Iterator to iterate over all tiles belonging to an airport spec. */
31 class AirportTileTableIterator : public TileIterator {
32 private:
33 const AirportTileTable *att; ///< The offsets.
34 TileIndex base_tile; ///< The tile we base the offsets off.
36 public:
37 /**
38 * Construct the iterator.
39 * @param att The TileTable we want to iterate over.
40 * @param base_tile The basetile for all offsets.
42 AirportTileTableIterator(const AirportTileTable *att, TileIndex base_tile) : TileIterator(base_tile + ToTileIndexDiff(att->ti)), att(att), base_tile(base_tile)
46 inline TileIterator& operator ++() override
48 this->att++;
49 if (this->att->ti.x == -0x80) {
50 this->tile = INVALID_TILE;
51 } else {
52 this->tile = this->base_tile + ToTileIndexDiff(this->att->ti);
54 return *this;
57 /** Get the StationGfx for the current tile. */
58 StationGfx GetStationGfx() const
60 return this->att->gfx;
63 std::unique_ptr<TileIterator> Clone() const override
65 return std::make_unique<AirportTileTableIterator>(*this);
69 /** List of default airport classes. */
70 enum AirportClassID {
71 APC_BEGIN = 0, ///< Lowest valid airport class id
72 APC_SMALL = 0, ///< id for small airports class
73 APC_LARGE, ///< id for large airports class
74 APC_HUB, ///< id for hub airports class
75 APC_HELIPORT, ///< id for heliports
76 APC_MAX = 16, ///< maximum number of airport classes
79 /** Allow incrementing of AirportClassID variables */
80 DECLARE_POSTFIX_INCREMENT(AirportClassID)
82 /** TTDP airport types. Used to map our types to TTDPatch's */
83 enum TTDPAirportType {
84 ATP_TTDP_SMALL, ///< Same as AT_SMALL
85 ATP_TTDP_LARGE, ///< Same as AT_LARGE
86 ATP_TTDP_HELIPORT, ///< Same as AT_HELIPORT
87 ATP_TTDP_OILRIG, ///< Same as AT_OILRIG
90 /** A list of all hangar tiles in an airport */
91 struct HangarTileTable {
92 TileIndexDiffC ti; ///< Tile offset from the top-most airport tile.
93 Direction dir; ///< Direction of the exit.
94 uint8_t hangar_num; ///< The hangar to which this tile belongs.
97 struct AirportTileLayout {
98 std::vector<AirportTileTable> tiles; ///< List of all tiles in this layout.
99 Direction rotation; ///< The rotation of this layout.
103 * Defines the data structure for an airport.
105 struct AirportSpec : NewGRFSpecBase<AirportClassID> {
106 const struct AirportFTAClass *fsm; ///< the finite statemachine for the default airports
107 std::vector<AirportTileLayout> layouts; ///< List of layouts composing the airport.
108 std::span<const HangarTileTable> depots; ///< Position of the depots on the airports.
109 uint8_t size_x; ///< size of airport in x direction
110 uint8_t size_y; ///< size of airport in y direction
111 uint8_t noise_level; ///< noise that this airport generates
112 uint8_t catchment; ///< catchment area of this airport
113 TimerGameCalendar::Year min_year; ///< first year the airport is available
114 TimerGameCalendar::Year max_year; ///< last year the airport is available
115 StringID name; ///< name of this airport
116 TTDPAirportType ttd_airport_type; ///< ttdpatch airport type (Small/Large/Helipad/Oilrig)
117 SpriteID preview_sprite; ///< preview sprite for this airport
118 uint16_t maintenance_cost; ///< maintenance cost multiplier
119 /* Newgrf data */
120 bool enabled; ///< Entity still available (by default true). Newgrf can disable it, though.
121 struct GRFFileProps grf_prop; ///< Properties related to the grf file.
123 static const AirportSpec *Get(uint8_t type);
124 static AirportSpec *GetWithoutOverride(uint8_t type);
126 bool IsAvailable() const;
127 bool IsWithinMapBounds(uint8_t table, TileIndex index) const;
129 static void ResetAirports();
131 /** Get the index of this spec. */
132 uint8_t GetIndex() const
134 assert(this >= std::begin(specs) && this < std::end(specs));
135 return static_cast<uint8_t>(std::distance(std::cbegin(specs), this));
138 static const AirportSpec dummy; ///< The dummy airport.
140 private:
141 static AirportSpec specs[NUM_AIRPORTS]; ///< Specs of the airports.
144 /** Information related to airport classes. */
145 using AirportClass = NewGRFClass<AirportSpec, AirportClassID, APC_MAX>;
147 void BindAirportSpecs();
149 /** Resolver for the airport scope. */
150 struct AirportScopeResolver : public ScopeResolver {
151 struct Station *st; ///< Station of the airport for which the callback is run, or \c nullptr for build gui.
152 const AirportSpec *spec; ///< AirportSpec for which the callback is run.
153 uint8_t layout; ///< Layout of the airport to build.
154 TileIndex tile; ///< Tile for the callback, only valid for airporttile callbacks.
157 * Constructor of the scope resolver for an airport.
158 * @param ro Surrounding resolver.
159 * @param tile %Tile for the callback, only valid for airporttile callbacks.
160 * @param st %Station of the airport for which the callback is run, or \c nullptr for build gui.
161 * @param spec AirportSpec for which the callback is run.
162 * @param layout Layout of the airport to build.
164 AirportScopeResolver(ResolverObject &ro, TileIndex tile, Station *st, const AirportSpec *spec, uint8_t layout)
165 : ScopeResolver(ro), st(st), spec(spec), layout(layout), tile(tile)
169 uint32_t GetRandomBits() const override;
170 uint32_t GetVariable(uint8_t variable, [[maybe_unused]] uint32_t parameter, bool &available) const override;
171 void StorePSA(uint pos, int32_t value) override;
175 /** Resolver object for airports. */
176 struct AirportResolverObject : public ResolverObject {
177 AirportScopeResolver airport_scope;
178 std::optional<TownScopeResolver> town_scope = std::nullopt; ///< The town scope resolver (created on the first call).
180 AirportResolverObject(TileIndex tile, Station *st, const AirportSpec *spec, uint8_t layout,
181 CallbackID callback = CBID_NO_CALLBACK, uint32_t callback_param1 = 0, uint32_t callback_param2 = 0);
183 TownScopeResolver *GetTown();
185 ScopeResolver *GetScope(VarSpriteGroupScope scope = VSG_SCOPE_SELF, uint8_t relative = 0) override
187 switch (scope) {
188 case VSG_SCOPE_SELF: return &this->airport_scope;
189 case VSG_SCOPE_PARENT:
191 TownScopeResolver *tsr = this->GetTown();
192 if (tsr != nullptr) return tsr;
193 [[fallthrough]];
195 default: return ResolverObject::GetScope(scope, relative);
199 GrfSpecFeature GetFeature() const override;
200 uint32_t GetDebugID() const override;
203 StringID GetAirportTextCallback(const AirportSpec *as, uint8_t layout, uint16_t callback);
205 #endif /* NEWGRF_AIRPORT_H */