summaryrefslogtreecommitdiff
path: root/src/ai/api/ai_airport.cpp
diff options
context:
space:
mode:
authortruebrain <truebrain@openttd.org>2009-01-12 17:11:45 +0000
committertruebrain <truebrain@openttd.org>2009-01-12 17:11:45 +0000
commita3dd7506d377b1434f913bd65c019eed52b64b6e (patch)
treeced1a262eb143ad6e64ec02f4a4c89835c0c32fd /src/ai/api/ai_airport.cpp
parent9294f9616866b9778c22076c19b5a32b4f85f788 (diff)
downloadopenttd-a3dd7506d377b1434f913bd65c019eed52b64b6e.tar.xz
(svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see.
NoAI is an API (a framework) to build your own AIs in. See: http://wiki.openttd.org/wiki/index.php/AI:Main_Page With many thanks to: - glx and Rubidium for their syncing, feedback and hard work - Yexo for his feedback, patches, and AIs which tested the system very deep - Morloth for his feedback and patches - TJIP for hosting a challenge which kept NoAI on track - All AI authors for testing our AI API, and all other people who helped in one way or another -Remove: all old AIs and their cheats/hacks
Diffstat (limited to 'src/ai/api/ai_airport.cpp')
-rw-r--r--src/ai/api/ai_airport.cpp130
1 files changed, 130 insertions, 0 deletions
diff --git a/src/ai/api/ai_airport.cpp b/src/ai/api/ai_airport.cpp
new file mode 100644
index 000000000..3e435d7bc
--- /dev/null
+++ b/src/ai/api/ai_airport.cpp
@@ -0,0 +1,130 @@
+/* $Id$ */
+
+/** @file ai_airport.cpp Implementation of AIAirport. */
+
+#include "ai_airport.hpp"
+#include "ai_station.hpp"
+#include "ai_error.hpp"
+#include "../../openttd.h"
+#include "../../variables.h"
+#include "../../station_map.h"
+#include "../../company_func.h"
+#include "../../settings_type.h"
+#include "../../command_type.h"
+#include "../../town.h"
+
+/* static */ bool AIAirport::IsValidAirportType(AirportType type)
+{
+ return type >= AT_SMALL && type <= AT_HELISTATION;
+}
+
+/* static */ bool AIAirport::IsHangarTile(TileIndex tile)
+{
+ if (!::IsValidTile(tile)) return false;
+
+ return ::IsTileType(tile, MP_STATION) && ::IsHangar(tile);
+}
+
+/* static */ bool AIAirport::IsAirportTile(TileIndex tile)
+{
+ if (!::IsValidTile(tile)) return false;
+
+ return ::IsTileType(tile, MP_STATION) && ::IsAirport(tile);
+}
+
+/* static */ bool AIAirport::AirportAvailable(AirportType type)
+{
+ if (!IsValidAirportType(type)) return false;
+
+ return HasBit(::GetValidAirports(), type);
+}
+
+/* static */ int32 AIAirport::GetAirportWidth(AirportType type)
+{
+ if (!IsValidAirportType(type)) return -1;
+
+ return ::GetAirport(type)->size_x;
+}
+
+/* static */ int32 AIAirport::GetAirportHeight(AirportType type)
+{
+ if (!IsValidAirportType(type)) return -1;
+
+ return ::GetAirport(type)->size_y;
+}
+
+/* static */ int32 AIAirport::GetAirportCoverageRadius(AirportType type)
+{
+ if (!IsValidAirportType(type)) return -1;
+
+ return _settings_game.station.modified_catchment ? ::GetAirport(type)->catchment : (uint)CA_UNMODIFIED;
+}
+
+/* static */ bool AIAirport::BuildAirport(TileIndex tile, AirportType type, bool join_adjacent)
+{
+ EnforcePrecondition(false, ::IsValidTile(tile));
+ EnforcePrecondition(false, IsValidAirportType(type));
+
+ return AIObject::DoCommand(tile, type, (INVALID_STATION << 16) | (join_adjacent ? 0 : 1), CMD_BUILD_AIRPORT);
+}
+
+/* static */ bool AIAirport::RemoveAirport(TileIndex tile)
+{
+ EnforcePrecondition(false, ::IsValidTile(tile))
+ EnforcePrecondition(false, IsAirportTile(tile) || IsHangarTile(tile));
+
+ return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
+}
+
+/* static */ int32 AIAirport::GetNumHangars(TileIndex tile)
+{
+ if (!::IsValidTile(tile)) return -1;
+ if (!::IsTileType(tile, MP_STATION)) return -1;
+
+ const Station *st = ::GetStationByTile(tile);
+ if (st->owner != _current_company) return -1;
+ if ((st->facilities & FACIL_AIRPORT) == 0) return -1;
+
+ return st->Airport()->nof_depots;
+}
+
+/* static */ TileIndex AIAirport::GetHangarOfAirport(TileIndex tile)
+{
+ if (!::IsValidTile(tile)) return INVALID_TILE;
+ if (!::IsTileType(tile, MP_STATION)) return INVALID_TILE;
+ if (GetNumHangars(tile) < 1) return INVALID_TILE;
+
+ const Station *st = ::GetStationByTile(tile);
+ if (st->owner != _current_company) return INVALID_TILE;
+ if ((st->facilities & FACIL_AIRPORT) == 0) return INVALID_TILE;
+
+ return ::ToTileIndexDiff(st->Airport()->airport_depots[0]) + st->xy;
+}
+
+/* static */ AIAirport::AirportType AIAirport::GetAirportType(TileIndex tile)
+{
+ if (!AITile::IsStationTile(tile)) return AT_INVALID;
+
+ StationID station_id = ::GetStationIndex(tile);
+
+ if (!AIStation::HasStationType(station_id, AIStation::STATION_AIRPORT)) return AT_INVALID;
+
+ return (AirportType)::GetStation(station_id)->airport_type;
+}
+
+
+/* static */ int AIAirport::GetNoiseLevelIncrease(TileIndex tile, AirportType type)
+{
+ extern uint8 GetAirportNoiseLevelForTown(const AirportFTAClass *afc, TileIndex town_tile, TileIndex tile);
+
+ if (!::IsValidTile(tile)) return -1;
+ if (!IsValidAirportType(type)) return -1;
+
+ if (_settings_game.economy.station_noise_level) {
+ const AirportFTAClass *afc = ::GetAirport(type);
+ const Town *t = ::ClosestTownFromTile(tile, UINT_MAX);
+ return GetAirportNoiseLevelForTown(afc, t->xy, tile);
+ }
+
+ return 1;
+}