OpenTTD Source 20250524-master-gc366e6a48e
airport.cpp
Go to the documentation of this file.
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 */
7
10#include "stdafx.h"
11#include "station_base.h"
12
13#include "table/strings.h"
16
17#include "safeguards.h"
18
19
28#define AIRPORT_GENERIC(name, terminals, num_helipads, flags, delta_z) \
29 static const AirportFTAClass _airportfta_ ## name(_airport_moving_data_ ## name, terminals, \
30 num_helipads, _airport_entries_ ## name, flags, _airport_fta_ ## name, delta_z);
31
38#define AIRPORT(name, num_helipads, short_strip) \
39 AIRPORT_GENERIC(name, _airport_terminal_ ## name, num_helipads, AirportFTAClass::Flags({AirportFTAClass::Flag::Airplanes, AirportFTAClass::Flag::Helicopters}) | (short_strip ? AirportFTAClass::Flags{AirportFTAClass::Flag::ShortStrip} : AirportFTAClass::Flags{}), 0)
40
47#define HELIPORT(name, num_helipads, delta_z) \
48 AIRPORT_GENERIC(name, nullptr, num_helipads, AirportFTAClass::Flag::Helicopters, delta_z)
49
50AIRPORT(country, 0, true)
51AIRPORT(city, 0, false)
52HELIPORT(heliport, 1, 60)
53AIRPORT(metropolitan, 0, false)
54AIRPORT(international, 2, false)
55AIRPORT(commuter, 2, true)
56HELIPORT(helidepot, 1, 0)
57AIRPORT(intercontinental, 2, false)
58HELIPORT(helistation, 3, 0)
59HELIPORT(oilrig, 1, 54)
61
62#undef HELIPORT
63#undef AIRPORT
64#undef AIRPORT_GENERIC
65
67
68
69static uint16_t AirportGetNofElements(const AirportFTAbuildup *apFA);
70static void AirportBuildAutomata(std::vector<AirportFTA> &layout, uint8_t nofelements, const AirportFTAbuildup *apFA);
71
72
81AirportMovingData RotateAirportMovingData(const AirportMovingData *orig, Direction rotation, uint num_tiles_x, uint num_tiles_y)
82{
84 amd.flags = orig->flags;
85 amd.direction = ChangeDir(orig->direction, (DirDiff)rotation);
86 switch (rotation) {
87 case DIR_N:
88 amd.x = orig->x;
89 amd.y = orig->y;
90 break;
91
92 case DIR_E:
93 amd.x = orig->y;
94 amd.y = num_tiles_y * TILE_SIZE - orig->x - 1;
95 break;
96
97 case DIR_S:
98 amd.x = num_tiles_x * TILE_SIZE - orig->x - 1;
99 amd.y = num_tiles_y * TILE_SIZE - orig->y - 1;
100 break;
101
102 case DIR_W:
103 amd.x = num_tiles_x * TILE_SIZE - orig->y - 1;
104 amd.y = orig->x;
105 break;
106
107 default: NOT_REACHED();
108 }
109 return amd;
110}
111
112AirportFTAClass::AirportFTAClass(
113 const AirportMovingData *moving_data_,
114 const uint8_t *terminals_,
115 const uint8_t num_helipads_,
116 const uint8_t *entry_points_,
117 Flags flags_,
118 const AirportFTAbuildup *apFA,
119 uint8_t delta_z_
120) :
121 moving_data(moving_data_),
122 terminals(terminals_),
123 num_helipads(num_helipads_),
124 flags(flags_),
125 nofelements(AirportGetNofElements(apFA)),
126 entry_points(entry_points_),
127 delta_z(delta_z_)
128{
129 /* Build the state machine itself */
130 AirportBuildAutomata(this->layout, this->nofelements, apFA);
131}
132
138static uint16_t AirportGetNofElements(const AirportFTAbuildup *apFA)
139{
140 uint16_t nofelements = 0;
141 int temp = apFA[0].position;
142
143 for (uint i = 0; i < MAX_ELEMENTS; i++) {
144 if (temp != apFA[i].position) {
145 nofelements++;
146 temp = apFA[i].position;
147 }
148 if (apFA[i].position == MAX_ELEMENTS) break;
149 }
150 return nofelements;
151}
152
153AirportFTA::AirportFTA(const AirportFTAbuildup &buildup) : blocks(buildup.blocks), position(buildup.position), next_position(buildup.next), heading(buildup.heading)
154{
155}
156
163static void AirportBuildAutomata(std::vector<AirportFTA> &layout, uint8_t nofelements, const AirportFTAbuildup *apFA)
164{
165 uint16_t internalcounter = 0;
166
167 layout.reserve(nofelements);
168 for (uint i = 0; i < nofelements; i++) {
169 AirportFTA *current = &layout.emplace_back(apFA[internalcounter]);
170
171 /* outgoing nodes from the same position, create linked list */
172 while (current->position == apFA[internalcounter + 1].position) {
173 current->next = std::make_unique<AirportFTA>(apFA[internalcounter + 1]);
174 current = current->next.get();
175 internalcounter++;
176 }
177 internalcounter++;
178 }
179}
180
186const AirportFTAClass *GetAirport(const uint8_t airport_type)
187{
188 if (airport_type == AT_DUMMY) return &_airportfta_dummy;
189 return AirportSpec::Get(airport_type)->fsm;
190}
191
197uint8_t GetVehiclePosOnBuild(TileIndex hangar_tile)
198{
199 const Station *st = Station::GetByTile(hangar_tile);
200 const AirportFTAClass *apc = st->airport.GetFTA();
201 /* When we click on hangar we know the tile it is on. By that we know
202 * its position in the array of depots the airport has.....we can search
203 * layout for #th position of depot. Since layout must start with a listing
204 * of all depots, it is simple */
205 for (uint i = 0;; i++) {
206 if (st->airport.GetHangarTile(i) == hangar_tile) {
207 assert(apc->layout[i].heading == HANGAR);
208 return apc->layout[i].position;
209 }
210 }
211 NOT_REACHED();
212}
#define AIRPORT(name, num_helipads, short_strip)
Define an airport.
Definition airport.cpp:38
#define AIRPORT_GENERIC(name, terminals, num_helipads, flags, delta_z)
Define a generic airport.
Definition airport.cpp:28
static uint16_t AirportGetNofElements(const AirportFTAbuildup *apFA)
Get the number of elements of a source Airport state automata Since it is actually just a big array o...
Definition airport.cpp:138
uint8_t GetVehiclePosOnBuild(TileIndex hangar_tile)
Get the vehicle position when an aircraft is build at the given tile.
Definition airport.cpp:197
const AirportFTAClass * GetAirport(const uint8_t airport_type)
Get the finite state machine of an airport type.
Definition airport.cpp:186
AirportMovingData RotateAirportMovingData(const AirportMovingData *orig, Direction rotation, uint num_tiles_x, uint num_tiles_y)
Rotate the airport moving data to another rotation.
Definition airport.cpp:81
#define HELIPORT(name, num_helipads, delta_z)
Define a heliport.
Definition airport.cpp:47
static void AirportBuildAutomata(std::vector< AirportFTA > &layout, uint8_t nofelements, const AirportFTAbuildup *apFA)
Construct the FTA given a description.
Definition airport.cpp:163
static const uint MAX_ELEMENTS
maximum number of aircraft positions at airport
Definition airport.h:19
@ HANGAR
Heading for hangar.
Definition airport.h:64
@ AT_DUMMY
Dummy airport.
Definition airport.h:43
Tables with default values for airports and airport tiles.
Heart of the airports and their finite state machines.
Enum of the default airport tiles.
Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
DirDiff
Enumeration for the difference between two directions.
Direction
Defines the 8 directions on the map.
@ DIR_N
North.
@ DIR_S
South.
@ DIR_W
West.
@ DIR_E
East.
A number of safeguards to prevent using unsafe methods.
Base classes/functions for stations.
Definition of base types and functions in a cross-platform compatible way.
Finite sTate mAchine (FTA) of an airport.
Definition airport.h:158
@ Airplanes
Can planes land on this airport type?
@ Helicopters
Can helicopters land on this airport type?
std::vector< AirportFTA > layout
state machine for airport
Definition airport.h:190
Internal structure used in openttd - Finite sTate mAchine --> FTA.
Definition airport.h:147
std::unique_ptr< AirportFTA > next
possible extra movement choices from this position
Definition airport.h:150
uint8_t position
the position that an airplane is at
Definition airport.h:152
State machine input struct (from external file, etc.) Finite sTate mAchine --> FTA.
uint8_t position
The position that an airplane is at.
A single location on an airport where aircraft can move to.
Definition airport.h:135
int16_t x
x-coordinate of the destination.
Definition airport.h:136
AirportMovingDataFlags flags
special flags when moving towards the destination.
Definition airport.h:138
int16_t y
y-coordinate of the destination.
Definition airport.h:137
Direction direction
Direction to turn the aircraft after reaching the destination.
Definition airport.h:139
const struct AirportFTAClass * fsm
the finite statemachine for the default airports
static const AirportSpec * Get(uint8_t type)
Retrieve airport spec for the given airport.
const AirportFTAClass * GetFTA() const
Get the finite-state machine for this airport or the finite-state machine for the dummy airport in ca...
TileIndex GetHangarTile(uint hangar_num) const
Get the first tile of the given hangar.
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
Station data structure.
Airport airport
Tile area the airport covers.
static const uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15