Codechange: Un-bitstuff landscape commands.
[openttd-github.git] / src / script / api / script_airport.cpp
blob4416a4bcda99f62f22783222afc443dcbb5f13a1
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 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 */ int32 ScriptAirport::GetAirportWidth(AirportType type)
54 if (!IsAirportInformationAvailable(type)) return -1;
56 return ::AirportSpec::Get(type)->size_x;
59 /* static */ int32 ScriptAirport::GetAirportHeight(AirportType type)
61 if (!IsAirportInformationAvailable(type)) return -1;
63 return ::AirportSpec::Get(type)->size_y;
66 /* static */ int32 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 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
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 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
86 EnforcePrecondition(false, ::IsValidTile(tile))
87 EnforcePrecondition(false, IsAirportTile(tile) || IsHangarTile(tile));
89 return ScriptObject::Command<CMD_LANDSCAPE_CLEAR>::Do(tile);
92 /* static */ int32 ScriptAirport::GetNumHangars(TileIndex tile)
94 if (!::IsValidTile(tile)) return -1;
95 if (!::IsTileType(tile, MP_STATION)) return -1;
97 const Station *st = ::Station::GetByTile(tile);
98 if (st->owner != ScriptObject::GetCompany() && ScriptObject::GetCompany() != OWNER_DEITY) return -1;
99 if ((st->facilities & FACIL_AIRPORT) == 0) return -1;
101 return st->airport.GetNumHangars();
104 /* static */ TileIndex ScriptAirport::GetHangarOfAirport(TileIndex tile)
106 if (!::IsValidTile(tile)) return INVALID_TILE;
107 if (!::IsTileType(tile, MP_STATION)) return INVALID_TILE;
108 if (GetNumHangars(tile) < 1) return INVALID_TILE;
110 const Station *st = ::Station::GetByTile(tile);
111 if (st->owner != ScriptObject::GetCompany() && ScriptObject::GetCompany() != OWNER_DEITY) return INVALID_TILE;
112 if ((st->facilities & FACIL_AIRPORT) == 0) return INVALID_TILE;
114 return st->airport.GetHangarTile(0);
117 /* static */ ScriptAirport::AirportType ScriptAirport::GetAirportType(TileIndex tile)
119 if (!ScriptTile::IsStationTile(tile)) return AT_INVALID;
121 StationID station_id = ::GetStationIndex(tile);
123 if (!ScriptStation::HasStationType(station_id, ScriptStation::STATION_AIRPORT)) return AT_INVALID;
125 return (AirportType)::Station::Get(station_id)->airport.type;
129 /* static */ int ScriptAirport::GetNoiseLevelIncrease(TileIndex tile, AirportType type)
131 extern Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint &mindist);
132 extern uint8 GetAirportNoiseLevelForDistance(const AirportSpec *as, uint distance);
134 if (!::IsValidTile(tile)) return -1;
135 if (!IsAirportInformationAvailable(type)) return -1;
137 const AirportSpec *as = ::AirportSpec::Get(type);
138 if (!as->IsWithinMapBounds(0, tile)) return -1;
140 if (_settings_game.economy.station_noise_level) {
141 AirportTileTableIterator it(as->table[0], tile);
142 uint dist;
143 AirportGetNearestTown(as, it, dist);
144 return GetAirportNoiseLevelForDistance(as, dist);
147 return 1;
150 /* static */ TownID ScriptAirport::GetNearestTown(TileIndex tile, AirportType type)
152 extern Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint &mindist);
154 if (!::IsValidTile(tile)) return INVALID_TOWN;
155 if (!IsAirportInformationAvailable(type)) return INVALID_TOWN;
157 const AirportSpec *as = AirportSpec::Get(type);
158 if (!as->IsWithinMapBounds(0, tile)) return INVALID_TOWN;
160 uint dist;
161 return AirportGetNearestTown(as, AirportTileTableIterator(as->table[0], tile), dist)->index;
164 /* static */ uint16 ScriptAirport::GetMaintenanceCostFactor(AirportType type)
166 if (!IsAirportInformationAvailable(type)) return INVALID_TOWN;
168 return AirportSpec::Get(type)->maintenance_cost;
171 /* static */ Money ScriptAirport::GetMonthlyMaintenanceCost(AirportType type)
173 if (!IsAirportInformationAvailable(type)) return -1;
175 return (int64)GetMaintenanceCostFactor(type) * _price[PR_INFRASTRUCTURE_AIRPORT] >> 3;