Fix some daylength issues, possible division by zero in main menu.
[openttd-joker.git] / src / script / api / script_airport.cpp
blob925a49b7e13f4b56d1956dcd1dd9ef399483425c
1 /* $Id: script_airport.cpp 23633 2011-12-19 21:05:36Z truebrain $ */
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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 */
10 /** @file script_airport.cpp Implementation of ScriptAirport. */
12 #include "../../stdafx.h"
13 #include "script_airport.hpp"
14 #include "script_station.hpp"
15 #include "../../station_base.h"
16 #include "../../town.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 uint p2 = station_id == ScriptStation::STATION_JOIN_ADJACENT ? 0 : 1;
81 p2 |= (ScriptStation::IsValidStation(station_id) ? station_id : INVALID_STATION) << 16;
82 return ScriptObject::DoCommand(tile, type, p2, CMD_BUILD_AIRPORT);
85 /* static */ bool ScriptAirport::RemoveAirport(TileIndex tile)
87 EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
88 EnforcePrecondition(false, ::IsValidTile(tile))
89 EnforcePrecondition(false, IsAirportTile(tile) || IsHangarTile(tile));
91 return ScriptObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
94 /* static */ int32 ScriptAirport::GetNumHangars(TileIndex tile)
96 if (!::IsValidTile(tile)) return -1;
97 if (!::IsTileType(tile, MP_STATION)) return -1;
99 const Station *st = ::Station::GetByTile(tile);
100 if (st->owner != ScriptObject::GetCompany() && ScriptObject::GetCompany() != OWNER_DEITY) return -1;
101 if ((st->facilities & FACIL_AIRPORT) == 0) return -1;
103 return st->airport.GetNumHangars();
106 /* static */ TileIndex ScriptAirport::GetHangarOfAirport(TileIndex 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() && ScriptObject::GetCompany() != OWNER_DEITY) 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 */ int ScriptAirport::GetNoiseLevelIncrease(TileIndex tile, AirportType type)
133 extern Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it);
134 extern uint8 GetAirportNoiseLevelForTown(const AirportSpec *as, TileIterator &it, TileIndex town_tile);
136 if (!::IsValidTile(tile)) return -1;
137 if (!IsAirportInformationAvailable(type)) return -1;
139 if (_settings_game.economy.station_noise_level) {
140 const AirportSpec *as = ::AirportSpec::Get(type);
141 AirportTileTableIterator it(as->table[0], tile);
142 const Town *t = AirportGetNearestTown(as, it);
143 return GetAirportNoiseLevelForTown(as, it, t->xy);
146 return 1;
149 /* static */ TownID ScriptAirport::GetNearestTown(TileIndex tile, AirportType type)
151 extern Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it);
153 if (!::IsValidTile(tile)) return INVALID_TOWN;
154 if (!IsAirportInformationAvailable(type)) return INVALID_TOWN;
156 const AirportSpec *as = AirportSpec::Get(type);
157 return AirportGetNearestTown(as, AirportTileTableIterator(as->table[0], tile))->index;
160 /* static */ uint16 ScriptAirport::GetMaintenanceCostFactor(AirportType type)
162 if (!IsAirportInformationAvailable(type)) return INVALID_TOWN;
164 return AirportSpec::Get(type)->maintenance_cost;