OpenTTD Source: src/airport.cpp Source File

OpenTTD Source 15.3

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
10#include "stdafx.h"
11#include "station_base.h"
12
13#include "table/strings.h"
14#include "table/airport_movement.h"
15#include "table/airporttile_ids.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
50 AIRPORT(country, 0, true)
51 AIRPORT(city, 0, false)
52 HELIPORT(heliport, 1, 60)
53 AIRPORT(metropolitan, 0, false)
54 AIRPORT(international, 2, false)
55 AIRPORT(commuter, 2, true)
56 HELIPORT(helidepot, 1, 0)
57 AIRPORT(intercontinental, 2, false)
58 HELIPORT(helistation, 3, 0)
59 HELIPORT(oilrig, 1, 54)
60 AIRPORT_GENERIC(dummy, nullptr, 0, AirportFTAClass::Flags({AirportFTAClass::Flag::Airplanes, AirportFTAClass::Flag::Helicopters}), 0)
61
62#undef HELIPORT
63#undef AIRPORT
64#undef AIRPORT_GENERIC
65
66#include "table/airport_defaults.h"
67
68
69static uint16_t AirportGetNofElements(const AirportFTAbuildup *apFA);
70static void AirportBuildAutomata(std::vector<AirportFTA> &layout, uint8_t nofelements, const AirportFTAbuildup *apFA);
71
72
81 AirportMovingData RotateAirportMovingData(const AirportMovingData *orig, Direction rotation, uint num_tiles_x, uint num_tiles_y)
82{
83 AirportMovingData amd;
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
138 static 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
163 static 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
186 const 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}
AIRPORT
#define AIRPORT(name, num_helipads, short_strip)
Define an airport.
Definition airport.cpp:38
AIRPORT_GENERIC
#define AIRPORT_GENERIC(name, terminals, num_helipads, flags, delta_z)
Define a generic airport.
Definition airport.cpp:28
AirportGetNofElements
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
GetVehiclePosOnBuild
uint8_t GetVehiclePosOnBuild(TileIndex hangar_tile)
Get the vehicle position when an aircraft is build at the given tile.
Definition airport.cpp:197
GetAirport
const AirportFTAClass * GetAirport(const uint8_t airport_type)
Get the finite state machine of an airport type.
Definition airport.cpp:186
RotateAirportMovingData
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
HELIPORT
#define HELIPORT(name, num_helipads, delta_z)
Define a heliport.
Definition airport.cpp:47
AirportBuildAutomata
static void AirportBuildAutomata(std::vector< AirportFTA > &layout, uint8_t nofelements, const AirportFTAbuildup *apFA)
Construct the FTA given a description.
Definition airport.cpp:163
MAX_ELEMENTS
static const uint MAX_ELEMENTS
maximum number of aircraft positions at airport
Definition airport.h:19
HANGAR
@ HANGAR
Heading for hangar.
Definition airport.h:64
AT_DUMMY
@ AT_DUMMY
Dummy airport.
Definition airport.h:43
airport_defaults.h
Tables with default values for airports and airport tiles.
airport_movement.h
Heart of the airports and their finite state machines.
airporttile_ids.h
Enum of the default airport tiles.
ChangeDir
Direction ChangeDir(Direction d, DirDiff delta)
Change a direction by a given difference.
Definition direction_func.h:104
DirDiff
DirDiff
Enumeration for the difference between two directions.
Definition direction_type.h:63
Direction
Direction
Defines the 8 directions on the map.
Definition direction_type.h:24
DIR_N
@ DIR_N
North.
Definition direction_type.h:26
DIR_S
@ DIR_S
South.
Definition direction_type.h:30
DIR_W
@ DIR_W
West.
Definition direction_type.h:32
DIR_E
@ DIR_E
East.
Definition direction_type.h:28
safeguards.h
A number of safeguards to prevent using unsafe methods.
station_base.h
Base classes/functions for stations.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
AirportFTAClass
Finite sTate mAchine (FTA) of an airport.
Definition airport.h:159
AirportFTAClass::Flag::Airplanes
@ Airplanes
Can planes land on this airport type?
AirportFTAClass::Flag::Helicopters
@ Helicopters
Can helicopters land on this airport type?
AirportFTAClass::layout
std::vector< AirportFTA > layout
state machine for airport
Definition airport.h:191
AirportFTA
Internal structure used in openttd - Finite sTate mAchine --> FTA.
Definition airport.h:148
AirportFTA::next
std::unique_ptr< AirportFTA > next
possible extra movement choices from this position
Definition airport.h:151
AirportFTA::position
uint8_t position
the position that an airplane is at
Definition airport.h:153
AirportFTAbuildup
State machine input struct (from external file, etc.) Finite sTate mAchine --> FTA.
Definition airport_movement.h:20
AirportFTAbuildup::position
uint8_t position
The position that an airplane is at.
Definition airport_movement.h:21
AirportMovingData
A single location on an airport where aircraft can move to.
Definition airport.h:136
AirportMovingData::x
int16_t x
x-coordinate of the destination.
Definition airport.h:137
AirportMovingData::flags
AirportMovingDataFlags flags
special flags when moving towards the destination.
Definition airport.h:139
AirportMovingData::y
int16_t y
y-coordinate of the destination.
Definition airport.h:138
AirportMovingData::direction
Direction direction
Direction to turn the aircraft after reaching the destination.
Definition airport.h:140
AirportSpec::fsm
const struct AirportFTAClass * fsm
the finite statemachine for the default airports
Definition newgrf_airport.h:110
AirportSpec::Get
static const AirportSpec * Get(uint8_t type)
Retrieve airport spec for the given airport.
Definition newgrf_airport.cpp:59
Airport::GetFTA
const AirportFTAClass * GetFTA() const
Get the finite-state machine for this airport or the finite-state machine for the dummy airport in ca...
Definition station_base.h:398
Airport::GetHangarTile
TileIndex GetHangarTile(uint hangar_num) const
Get the first tile of the given hangar.
Definition station_base.h:439
BaseStation::GetByTile
static BaseStation * GetByTile(TileIndex tile)
Get the base station belonging to a specific tile.
Definition base_station_base.h:160
Station
Station data structure.
Definition station_base.h:520
Station::airport
Airport airport
Tile area the airport covers.
Definition station_base.h:534
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
TILE_SIZE
static constexpr uint TILE_SIZE
Tile size in world coordinates.
Definition tile_type.h:15