OpenTTD Source 20250312-master-gcdcc6b491d
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#include "table/strings.h"
15
16#include "safeguards.h"
17
18
27#define AIRPORT_GENERIC(name, terminals, num_helipads, flags, delta_z) \
28 static const AirportFTAClass _airportfta_ ## name(_airport_moving_data_ ## name, terminals, \
29 num_helipads, _airport_entries_ ## name, flags, _airport_fta_ ## name, delta_z);
30
37#define AIRPORT(name, num_helipads, short_strip) \
38 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)
39
46#define HELIPORT(name, num_helipads, delta_z) \
47 AIRPORT_GENERIC(name, nullptr, num_helipads, AirportFTAClass::Flag::Helicopters, delta_z)
48
49AIRPORT(country, 0, true)
50AIRPORT(city, 0, false)
51HELIPORT(heliport, 1, 60)
52AIRPORT(metropolitan, 0, false)
53AIRPORT(international, 2, false)
54AIRPORT(commuter, 2, true)
55HELIPORT(helidepot, 1, 0)
56AIRPORT(intercontinental, 2, false)
57HELIPORT(helistation, 3, 0)
58HELIPORT(oilrig, 1, 54)
60
61#undef HELIPORT
62#undef AIRPORT
63#undef AIRPORT_GENERIC
64
66
67
68static uint16_t AirportGetNofElements(const AirportFTAbuildup *apFA);
69static void AirportBuildAutomata(std::vector<AirportFTA> &layout, uint8_t nofelements, const AirportFTAbuildup *apFA);
70
71
80AirportMovingData RotateAirportMovingData(const AirportMovingData *orig, Direction rotation, uint num_tiles_x, uint num_tiles_y)
81{
83 amd.flags = orig->flags;
84 amd.direction = ChangeDir(orig->direction, (DirDiff)rotation);
85 switch (rotation) {
86 case DIR_N:
87 amd.x = orig->x;
88 amd.y = orig->y;
89 break;
90
91 case DIR_E:
92 amd.x = orig->y;
93 amd.y = num_tiles_y * TILE_SIZE - orig->x - 1;
94 break;
95
96 case DIR_S:
97 amd.x = num_tiles_x * TILE_SIZE - orig->x - 1;
98 amd.y = num_tiles_y * TILE_SIZE - orig->y - 1;
99 break;
100
101 case DIR_W:
102 amd.x = num_tiles_x * TILE_SIZE - orig->y - 1;
103 amd.y = orig->x;
104 break;
105
106 default: NOT_REACHED();
107 }
108 return amd;
109}
110
111AirportFTAClass::AirportFTAClass(
112 const AirportMovingData *moving_data_,
113 const uint8_t *terminals_,
114 const uint8_t num_helipads_,
115 const uint8_t *entry_points_,
116 Flags flags_,
117 const AirportFTAbuildup *apFA,
118 uint8_t delta_z_
119) :
120 moving_data(moving_data_),
121 terminals(terminals_),
122 num_helipads(num_helipads_),
123 flags(flags_),
124 nofelements(AirportGetNofElements(apFA)),
125 entry_points(entry_points_),
126 delta_z(delta_z_)
127{
128 /* Build the state machine itself */
129 AirportBuildAutomata(this->layout, this->nofelements, apFA);
130}
131
137static uint16_t AirportGetNofElements(const AirportFTAbuildup *apFA)
138{
139 uint16_t nofelements = 0;
140 int temp = apFA[0].position;
141
142 for (uint i = 0; i < MAX_ELEMENTS; i++) {
143 if (temp != apFA[i].position) {
144 nofelements++;
145 temp = apFA[i].position;
146 }
147 if (apFA[i].position == MAX_ELEMENTS) break;
148 }
149 return nofelements;
150}
151
152AirportFTA::AirportFTA(const AirportFTAbuildup &buildup) : blocks(buildup.blocks), position(buildup.position), next_position(buildup.next), heading(buildup.heading)
153{
154}
155
162static void AirportBuildAutomata(std::vector<AirportFTA> &layout, uint8_t nofelements, const AirportFTAbuildup *apFA)
163{
164 uint16_t internalcounter = 0;
165
166 layout.reserve(nofelements);
167 for (uint i = 0; i < nofelements; i++) {
168 AirportFTA *current = &layout.emplace_back(apFA[internalcounter]);
169
170 /* outgoing nodes from the same position, create linked list */
171 while (current->position == apFA[internalcounter + 1].position) {
172 current->next = std::make_unique<AirportFTA>(apFA[internalcounter + 1]);
173 current = current->next.get();
174 internalcounter++;
175 }
176 internalcounter++;
177 }
178}
179
185const AirportFTAClass *GetAirport(const uint8_t airport_type)
186{
187 if (airport_type == AT_DUMMY) return &_airportfta_dummy;
188 return AirportSpec::Get(airport_type)->fsm;
189}
190
196uint8_t GetVehiclePosOnBuild(TileIndex hangar_tile)
197{
198 const Station *st = Station::GetByTile(hangar_tile);
199 const AirportFTAClass *apc = st->airport.GetFTA();
200 /* When we click on hangar we know the tile it is on. By that we know
201 * its position in the array of depots the airport has.....we can search
202 * layout for #th position of depot. Since layout must start with a listing
203 * of all depots, it is simple */
204 for (uint i = 0;; i++) {
205 if (st->airport.GetHangarTile(i) == hangar_tile) {
206 assert(apc->layout[i].heading == HANGAR);
207 return apc->layout[i].position;
208 }
209 }
210 NOT_REACHED();
211}
#define AIRPORT(name, num_helipads, short_strip)
Define an airport.
Definition airport.cpp:37
#define AIRPORT_GENERIC(name, terminals, num_helipads, flags, delta_z)
Define a generic airport.
Definition airport.cpp:27
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:137
uint8_t GetVehiclePosOnBuild(TileIndex hangar_tile)
Get the vehicle position when an aircraft is build at the given tile.
Definition airport.cpp:196
const AirportFTAClass * GetAirport(const uint8_t airport_type)
Get the finite state machine of an airport type.
Definition airport.cpp:185
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:80
#define HELIPORT(name, num_helipads, delta_z)
Define a heliport.
Definition airport.cpp:46
static void AirportBuildAutomata(std::vector< AirportFTA > &layout, uint8_t nofelements, const AirportFTAbuildup *apFA)
Construct the FTA given a description.
Definition airport.cpp:162
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
Allow incrementing of Direction variables.
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