summaryrefslogtreecommitdiff
path: root/src/ai/api/ai_station.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_station.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_station.cpp')
-rw-r--r--src/ai/api/ai_station.cpp146
1 files changed, 146 insertions, 0 deletions
diff --git a/src/ai/api/ai_station.cpp b/src/ai/api/ai_station.cpp
new file mode 100644
index 000000000..ab171a307
--- /dev/null
+++ b/src/ai/api/ai_station.cpp
@@ -0,0 +1,146 @@
+/* $Id$ */
+
+/** @file ai_station.cpp Implementation of AIStation. */
+
+#include "ai_station.hpp"
+#include "ai_cargo.hpp"
+#include "ai_map.hpp"
+#include "ai_town.hpp"
+#include "../../openttd.h"
+#include "../../command_func.h"
+#include "../../debug.h"
+#include "../../station_map.h"
+#include "../../variables.h"
+#include "../../string_func.h"
+#include "../../strings_func.h"
+#include "../../core/alloc_func.hpp"
+#include "../../company_func.h"
+#include "../../settings_type.h"
+#include "../../town.h"
+#include "table/strings.h"
+
+/* static */ bool AIStation::IsValidStation(StationID station_id)
+{
+ return ::IsValidStationID(station_id) && ::GetStation(station_id)->owner == _current_company;
+}
+
+/* static */ StationID AIStation::GetStationID(TileIndex tile)
+{
+ if (!::IsTileType(tile, MP_STATION)) return INVALID_STATION;
+ return ::GetStationIndex(tile);
+}
+
+/* static */ const char *AIStation::GetName(StationID station_id)
+{
+ if (!IsValidStation(station_id)) return NULL;
+
+ static const int len = 64;
+ char *station_name = MallocT<char>(len);
+
+ ::SetDParam(0, GetStation(station_id)->index);
+ ::GetString(station_name, STR_STATION, &station_name[len - 1]);
+ return station_name;
+}
+
+/* static */ bool AIStation::SetName(StationID station_id, const char *name)
+{
+ EnforcePrecondition(false, IsValidStation(station_id));
+ EnforcePrecondition(false, !::StrEmpty(name));
+ EnforcePreconditionCustomError(false, ::strlen(name) < MAX_LENGTH_STATION_NAME_BYTES, AIError::ERR_PRECONDITION_STRING_TOO_LONG);
+
+ return AIObject::DoCommand(0, station_id, 0, CMD_RENAME_STATION, name);
+}
+
+/* static */ TileIndex AIStation::GetLocation(StationID station_id)
+{
+ if (!IsValidStation(station_id)) return INVALID_TILE;
+
+ return ::GetStation(station_id)->xy;
+}
+
+/* static */ int32 AIStation::GetCargoWaiting(StationID station_id, CargoID cargo_id)
+{
+ if (!IsValidStation(station_id)) return -1;
+ if (!AICargo::IsValidCargo(cargo_id)) return -1;
+
+ return ::GetStation(station_id)->goods[cargo_id].cargo.Count();
+}
+
+/* static */ int32 AIStation::GetCargoRating(StationID station_id, CargoID cargo_id)
+{
+ if (!IsValidStation(station_id)) return -1;
+ if (!AICargo::IsValidCargo(cargo_id)) return -1;
+
+ return ::GetStation(station_id)->goods[cargo_id].rating * 101 >> 8;
+}
+
+/* static */ int32 AIStation::GetCoverageRadius(AIStation::StationType station_type)
+{
+ if (station_type == STATION_AIRPORT) {
+ DEBUG(ai, 0, "GetCoverageRadius(): coverage radius of airports needs to be requested via AIAirport::GetAirportCoverageRadius(), as it requires AirportType");
+ return -1;
+ }
+ if (CountBits(station_type) != 1) return -1;
+ if (!_settings_game.station.modified_catchment) return CA_UNMODIFIED;
+
+ switch (station_type) {
+ case STATION_TRAIN: return CA_TRAIN;
+ case STATION_TRUCK_STOP: return CA_TRUCK;
+ case STATION_BUS_STOP: return CA_BUS;
+ case STATION_DOCK: return CA_DOCK;
+ default: return CA_NONE;
+ }
+}
+
+/* static */ int32 AIStation::GetDistanceManhattanToTile(StationID station_id, TileIndex tile)
+{
+ if (!IsValidStation(station_id)) return -1;
+
+ return AIMap::DistanceManhattan(tile, GetLocation(station_id));
+}
+
+/* static */ int32 AIStation::GetDistanceSquareToTile(StationID station_id, TileIndex tile)
+{
+ if (!IsValidStation(station_id)) return -1;
+
+ return AIMap::DistanceSquare(tile, GetLocation(station_id));
+}
+
+/* static */ bool AIStation::IsWithinTownInfluence(StationID station_id, TownID town_id)
+{
+ if (!IsValidStation(station_id)) return false;
+
+ return AITown::IsWithinTownInfluence(town_id, GetLocation(station_id));
+}
+
+/* static */ bool AIStation::HasStationType(StationID station_id, StationType station_type)
+{
+ if (!IsValidStation(station_id)) return false;
+ if (CountBits(station_type) != 1) return false;
+
+ return (::GetStation(station_id)->facilities & station_type) != 0;
+}
+
+/* static */ bool AIStation::HasRoadType(StationID station_id, AIRoad::RoadType road_type)
+{
+ if (!IsValidStation(station_id)) return false;
+ if (!AIRoad::IsRoadTypeAvailable(road_type)) return false;
+
+ ::RoadTypes r = RoadTypeToRoadTypes((::RoadType)road_type);
+
+ for (const RoadStop *rs = ::GetStation(station_id)->GetPrimaryRoadStop(ROADSTOP_BUS); rs != NULL; rs = rs->next) {
+ if ((::GetRoadTypes(rs->xy) & r) != 0) return true;
+ }
+ for (const RoadStop *rs = ::GetStation(station_id)->GetPrimaryRoadStop(ROADSTOP_TRUCK); rs != NULL; rs = rs->next) {
+ if ((::GetRoadTypes(rs->xy) & r) != 0) return true;
+ }
+
+ return false;
+}
+
+/* static */ TownID AIStation::GetNearestTown(StationID station_id)
+{
+ if (!IsValidStation(station_id)) return INVALID_TOWN;
+
+ return ::GetStation(station_id)->town->index;
+}