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/>.
8 /** @file script_airport.cpp Implementation of ScriptAirport. */
10 #include "../../stdafx.h"
11 #include "script_airport.hpp"
12 #include "script_station.hpp"
13 #include "../../station_base.h"
14 #include "../../town.h"
15 #include "../../landscape_cmd.h"
16 #include "../../station_cmd.h"
18 #include "../../safeguards.h"
20 /* static */ bool ScriptAirport::IsValidAirportType(AirportType type
)
22 return IsAirportInformationAvailable(type
) && ::AirportSpec::Get(type
)->IsAvailable();
25 /* static */ bool ScriptAirport::IsAirportInformationAvailable(AirportType type
)
27 return type
>= 0 && type
< (AirportType
)NUM_AIRPORTS
&& AirportSpec::Get(type
)->enabled
;
30 /* static */ Money
ScriptAirport::GetPrice(AirportType type
)
32 if (!IsValidAirportType(type
)) return -1;
34 const AirportSpec
*as
= ::AirportSpec::Get(type
);
35 return _price
[PR_BUILD_STATION_AIRPORT
] * as
->size_x
* as
->size_y
;
38 /* static */ bool ScriptAirport::IsHangarTile(TileIndex tile
)
40 if (!::IsValidTile(tile
)) return false;
42 return ::IsTileType(tile
, MP_STATION
) && ::IsHangar(tile
);
45 /* static */ bool ScriptAirport::IsAirportTile(TileIndex tile
)
47 if (!::IsValidTile(tile
)) return false;
49 return ::IsTileType(tile
, MP_STATION
) && ::IsAirport(tile
);
52 /* static */ SQInteger
ScriptAirport::GetAirportWidth(AirportType type
)
54 if (!IsAirportInformationAvailable(type
)) return -1;
56 return ::AirportSpec::Get(type
)->size_x
;
59 /* static */ SQInteger
ScriptAirport::GetAirportHeight(AirportType type
)
61 if (!IsAirportInformationAvailable(type
)) return -1;
63 return ::AirportSpec::Get(type
)->size_y
;
66 /* static */ SQInteger
ScriptAirport::GetAirportCoverageRadius(AirportType type
)
68 if (!IsAirportInformationAvailable(type
)) return -1;
70 return _settings_game
.station
.modified_catchment
? ::AirportSpec::Get(type
)->catchment
: (uint
)CA_UNMODIFIED
;
73 /* static */ bool ScriptAirport::BuildAirport(TileIndex tile
, AirportType type
, StationID station_id
)
75 EnforceCompanyModeValid(false);
76 EnforcePrecondition(false, ::IsValidTile(tile
));
77 EnforcePrecondition(false, IsValidAirportType(type
));
78 EnforcePrecondition(false, station_id
== ScriptStation::STATION_NEW
|| station_id
== ScriptStation::STATION_JOIN_ADJACENT
|| ScriptStation::IsValidStation(station_id
));
80 return ScriptObject::Command
<CMD_BUILD_AIRPORT
>::Do(tile
, type
, 0, (ScriptStation::IsValidStation(station_id
) ? station_id
: INVALID_STATION
), station_id
!= ScriptStation::STATION_JOIN_ADJACENT
);
83 /* static */ bool ScriptAirport::RemoveAirport(TileIndex tile
)
85 EnforceCompanyModeValid(false);
86 EnforcePrecondition(false, ::IsValidTile(tile
))
87 EnforcePrecondition(false, IsAirportTile(tile
) || IsHangarTile(tile
));
89 return ScriptObject::Command
<CMD_LANDSCAPE_CLEAR
>::Do(tile
);
92 /* static */ SQInteger
ScriptAirport::GetNumHangars(TileIndex tile
)
94 EnforceDeityOrCompanyModeValid(-1);
95 if (!::IsValidTile(tile
)) return -1;
96 if (!::IsTileType(tile
, MP_STATION
)) return -1;
98 const Station
*st
= ::Station::GetByTile(tile
);
99 if (st
->owner
!= ScriptObject::GetCompany() && ScriptCompanyMode::IsValid()) return -1;
100 if ((st
->facilities
& FACIL_AIRPORT
) == 0) return -1;
102 return st
->airport
.GetNumHangars();
105 /* static */ TileIndex
ScriptAirport::GetHangarOfAirport(TileIndex tile
)
107 EnforceDeityOrCompanyModeValid(INVALID_TILE
);
108 if (!::IsValidTile(tile
)) return INVALID_TILE
;
109 if (!::IsTileType(tile
, MP_STATION
)) return INVALID_TILE
;
110 if (GetNumHangars(tile
) < 1) return INVALID_TILE
;
112 const Station
*st
= ::Station::GetByTile(tile
);
113 if (st
->owner
!= ScriptObject::GetCompany() && ScriptCompanyMode::IsValid()) return INVALID_TILE
;
114 if ((st
->facilities
& FACIL_AIRPORT
) == 0) return INVALID_TILE
;
116 return st
->airport
.GetHangarTile(0);
119 /* static */ ScriptAirport::AirportType
ScriptAirport::GetAirportType(TileIndex tile
)
121 if (!ScriptTile::IsStationTile(tile
)) return AT_INVALID
;
123 StationID station_id
= ::GetStationIndex(tile
);
125 if (!ScriptStation::HasStationType(station_id
, ScriptStation::STATION_AIRPORT
)) return AT_INVALID
;
127 return (AirportType
)::Station::Get(station_id
)->airport
.type
;
131 /* static */ SQInteger
ScriptAirport::GetNoiseLevelIncrease(TileIndex tile
, AirportType type
)
133 if (!::IsValidTile(tile
)) return -1;
134 if (!IsAirportInformationAvailable(type
)) return -1;
136 const AirportSpec
*as
= ::AirportSpec::Get(type
);
137 if (!as
->IsWithinMapBounds(0, tile
)) return -1;
139 if (_settings_game
.economy
.station_noise_level
) {
141 const auto &layout
= as
->layouts
[0];
142 AirportGetNearestTown(as
, layout
.rotation
, tile
, AirportTileTableIterator(layout
.tiles
.data(), tile
), dist
);
143 return GetAirportNoiseLevelForDistance(as
, dist
);
149 /* static */ TownID
ScriptAirport::GetNearestTown(TileIndex tile
, AirportType type
)
151 if (!::IsValidTile(tile
)) return INVALID_TOWN
;
152 if (!IsAirportInformationAvailable(type
)) return INVALID_TOWN
;
154 const AirportSpec
*as
= AirportSpec::Get(type
);
155 if (!as
->IsWithinMapBounds(0, tile
)) return INVALID_TOWN
;
158 const auto &layout
= as
->layouts
[0];
159 return AirportGetNearestTown(as
, layout
.rotation
, tile
, AirportTileTableIterator(layout
.tiles
.data(), tile
), dist
)->index
;
162 /* static */ SQInteger
ScriptAirport::GetMaintenanceCostFactor(AirportType type
)
164 if (!IsAirportInformationAvailable(type
)) return 0;
166 return AirportSpec::Get(type
)->maintenance_cost
;
169 /* static */ Money
ScriptAirport::GetMonthlyMaintenanceCost(AirportType type
)
171 if (!IsAirportInformationAvailable(type
)) return -1;
173 return (int64_t)GetMaintenanceCostFactor(type
) * _price
[PR_INFRASTRUCTURE_AIRPORT
] >> 3;
176 /* static */ SQInteger
ScriptAirport::GetAirportNumHelipads(AirportType type
)
178 if (!IsAirportInformationAvailable(type
)) return -1;
180 return ::AirportSpec::Get(type
)->fsm
->num_helipads
;