2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/** @file script_airport.cpp Implementation of ScriptAirport. */
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-01-22 10:33:16 +00:00
|
|
|
#include "../../stdafx.h"
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "script_airport.hpp"
|
|
|
|
#include "script_station.hpp"
|
2009-06-24 17:39:54 +00:00
|
|
|
#include "../../station_base.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "../../town.h"
|
2021-11-01 20:30:34 +00:00
|
|
|
#include "../../landscape_cmd.h"
|
|
|
|
#include "../../station_cmd.h"
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2014-04-23 20:13:33 +00:00
|
|
|
#include "../../safeguards.h"
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptAirport::IsValidAirportType(AirportType type)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2010-01-15 12:08:08 +00:00
|
|
|
return IsAirportInformationAvailable(type) && ::AirportSpec::Get(type)->IsAvailable();
|
2009-09-20 18:38:43 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptAirport::IsAirportInformationAvailable(AirportType type)
|
2009-09-20 18:38:43 +00:00
|
|
|
{
|
2010-03-05 23:21:51 +00:00
|
|
|
return type >= 0 && type < (AirportType)NUM_AIRPORTS && AirportSpec::Get(type)->enabled;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ Money ScriptAirport::GetPrice(AirportType type)
|
2009-05-07 21:30:18 +00:00
|
|
|
{
|
|
|
|
if (!IsValidAirportType(type)) return -1;
|
|
|
|
|
2010-01-15 12:08:08 +00:00
|
|
|
const AirportSpec *as = ::AirportSpec::Get(type);
|
|
|
|
return _price[PR_BUILD_STATION_AIRPORT] * as->size_x * as->size_y;
|
2009-05-07 21:30:18 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptAirport::IsHangarTile(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!::IsValidTile(tile)) return false;
|
|
|
|
|
|
|
|
return ::IsTileType(tile, MP_STATION) && ::IsHangar(tile);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptAirport::IsAirportTile(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!::IsValidTile(tile)) return false;
|
|
|
|
|
|
|
|
return ::IsTileType(tile, MP_STATION) && ::IsAirport(tile);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ int32 ScriptAirport::GetAirportWidth(AirportType type)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2009-09-20 18:38:43 +00:00
|
|
|
if (!IsAirportInformationAvailable(type)) return -1;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2010-01-15 12:08:08 +00:00
|
|
|
return ::AirportSpec::Get(type)->size_x;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ int32 ScriptAirport::GetAirportHeight(AirportType type)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2009-09-20 18:38:43 +00:00
|
|
|
if (!IsAirportInformationAvailable(type)) return -1;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2010-01-15 12:08:08 +00:00
|
|
|
return ::AirportSpec::Get(type)->size_y;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ int32 ScriptAirport::GetAirportCoverageRadius(AirportType type)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2009-09-20 18:38:43 +00:00
|
|
|
if (!IsAirportInformationAvailable(type)) return -1;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2010-01-15 12:08:08 +00:00
|
|
|
return _settings_game.station.modified_catchment ? ::AirportSpec::Get(type)->catchment : (uint)CA_UNMODIFIED;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptAirport::BuildAirport(TileIndex tile, AirportType type, StationID station_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-12-19 21:05:36 +00:00
|
|
|
EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
|
2009-01-12 17:11:45 +00:00
|
|
|
EnforcePrecondition(false, ::IsValidTile(tile));
|
|
|
|
EnforcePrecondition(false, IsValidAirportType(type));
|
2011-11-29 23:15:35 +00:00
|
|
|
EnforcePrecondition(false, station_id == ScriptStation::STATION_NEW || station_id == ScriptStation::STATION_JOIN_ADJACENT || ScriptStation::IsValidStation(station_id));
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
uint p2 = station_id == ScriptStation::STATION_JOIN_ADJACENT ? 0 : 1;
|
|
|
|
p2 |= (ScriptStation::IsValidStation(station_id) ? station_id : INVALID_STATION) << 16;
|
2021-11-01 20:30:34 +00:00
|
|
|
return ScriptObject::Command<CMD_BUILD_AIRPORT>::Do(tile, type, p2, {});
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptAirport::RemoveAirport(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-12-19 21:05:36 +00:00
|
|
|
EnforcePrecondition(false, ScriptObject::GetCompany() != OWNER_DEITY);
|
2009-01-12 17:11:45 +00:00
|
|
|
EnforcePrecondition(false, ::IsValidTile(tile))
|
|
|
|
EnforcePrecondition(false, IsAirportTile(tile) || IsHangarTile(tile));
|
|
|
|
|
2021-11-01 20:30:34 +00:00
|
|
|
return ScriptObject::Command<CMD_LANDSCAPE_CLEAR>::Do(tile, 0, 0, {});
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ int32 ScriptAirport::GetNumHangars(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!::IsValidTile(tile)) return -1;
|
|
|
|
if (!::IsTileType(tile, MP_STATION)) return -1;
|
|
|
|
|
2009-06-24 17:39:54 +00:00
|
|
|
const Station *st = ::Station::GetByTile(tile);
|
2011-12-19 21:05:25 +00:00
|
|
|
if (st->owner != ScriptObject::GetCompany() && ScriptObject::GetCompany() != OWNER_DEITY) return -1;
|
2009-01-12 17:11:45 +00:00
|
|
|
if ((st->facilities & FACIL_AIRPORT) == 0) return -1;
|
|
|
|
|
2010-03-19 11:17:52 +00:00
|
|
|
return st->airport.GetNumHangars();
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ TileIndex ScriptAirport::GetHangarOfAirport(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!::IsValidTile(tile)) return INVALID_TILE;
|
|
|
|
if (!::IsTileType(tile, MP_STATION)) return INVALID_TILE;
|
|
|
|
if (GetNumHangars(tile) < 1) return INVALID_TILE;
|
|
|
|
|
2009-06-24 17:39:54 +00:00
|
|
|
const Station *st = ::Station::GetByTile(tile);
|
2011-12-19 21:05:25 +00:00
|
|
|
if (st->owner != ScriptObject::GetCompany() && ScriptObject::GetCompany() != OWNER_DEITY) return INVALID_TILE;
|
2009-01-12 17:11:45 +00:00
|
|
|
if ((st->facilities & FACIL_AIRPORT) == 0) return INVALID_TILE;
|
|
|
|
|
2010-03-19 09:58:46 +00:00
|
|
|
return st->airport.GetHangarTile(0);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ ScriptAirport::AirportType ScriptAirport::GetAirportType(TileIndex tile)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2011-11-29 23:15:35 +00:00
|
|
|
if (!ScriptTile::IsStationTile(tile)) return AT_INVALID;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
StationID station_id = ::GetStationIndex(tile);
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
if (!ScriptStation::HasStationType(station_id, ScriptStation::STATION_AIRPORT)) return AT_INVALID;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2010-03-18 21:02:20 +00:00
|
|
|
return (AirportType)::Station::Get(station_id)->airport.type;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ int ScriptAirport::GetNoiseLevelIncrease(TileIndex tile, AirportType type)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2019-03-29 17:43:06 +00:00
|
|
|
extern Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint &mindist);
|
|
|
|
extern uint8 GetAirportNoiseLevelForDistance(const AirportSpec *as, uint distance);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
if (!::IsValidTile(tile)) return -1;
|
2011-08-01 20:15:18 +00:00
|
|
|
if (!IsAirportInformationAvailable(type)) return -1;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2019-03-30 22:20:26 +00:00
|
|
|
const AirportSpec *as = ::AirportSpec::Get(type);
|
|
|
|
if (!as->IsWithinMapBounds(0, tile)) return -1;
|
|
|
|
|
2009-01-12 17:11:45 +00:00
|
|
|
if (_settings_game.economy.station_noise_level) {
|
2011-12-09 19:30:30 +00:00
|
|
|
AirportTileTableIterator it(as->table[0], tile);
|
2019-03-29 17:43:06 +00:00
|
|
|
uint dist;
|
|
|
|
AirportGetNearestTown(as, it, dist);
|
|
|
|
return GetAirportNoiseLevelForDistance(as, dist);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return 1;
|
|
|
|
}
|
2009-02-05 01:15:54 +00:00
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ TownID ScriptAirport::GetNearestTown(TileIndex tile, AirportType type)
|
2009-02-05 01:15:54 +00:00
|
|
|
{
|
2019-03-29 17:43:06 +00:00
|
|
|
extern Town *AirportGetNearestTown(const AirportSpec *as, const TileIterator &it, uint &mindist);
|
2009-02-05 01:15:54 +00:00
|
|
|
|
|
|
|
if (!::IsValidTile(tile)) return INVALID_TOWN;
|
2009-09-20 18:38:43 +00:00
|
|
|
if (!IsAirportInformationAvailable(type)) return INVALID_TOWN;
|
2009-02-05 01:15:54 +00:00
|
|
|
|
2011-12-09 19:30:30 +00:00
|
|
|
const AirportSpec *as = AirportSpec::Get(type);
|
2019-03-30 22:20:26 +00:00
|
|
|
if (!as->IsWithinMapBounds(0, tile)) return INVALID_TOWN;
|
|
|
|
|
2019-03-29 17:43:06 +00:00
|
|
|
uint dist;
|
|
|
|
return AirportGetNearestTown(as, AirportTileTableIterator(as->table[0], tile), dist)->index;
|
2009-02-05 01:15:54 +00:00
|
|
|
}
|
2011-12-03 23:40:57 +00:00
|
|
|
|
|
|
|
/* static */ uint16 ScriptAirport::GetMaintenanceCostFactor(AirportType type)
|
|
|
|
{
|
|
|
|
if (!IsAirportInformationAvailable(type)) return INVALID_TOWN;
|
|
|
|
|
|
|
|
return AirportSpec::Get(type)->maintenance_cost;
|
|
|
|
}
|
2018-09-14 12:03:52 +00:00
|
|
|
|
|
|
|
/* static */ Money ScriptAirport::GetMonthlyMaintenanceCost(AirportType type)
|
|
|
|
{
|
|
|
|
if (!IsAirportInformationAvailable(type)) return -1;
|
|
|
|
|
|
|
|
return (int64)GetMaintenanceCostFactor(type) * _price[PR_INFRASTRUCTURE_AIRPORT] >> 3;
|
|
|
|
}
|