(svn r12559) [NoAI] -Add: SetLastError support for AIAirport.
--- a/src/ai/api/ai_airport.cpp Thu Apr 03 23:01:54 2008 +0000
+++ b/src/ai/api/ai_airport.cpp Fri Apr 04 10:43:13 2008 +0000
@@ -3,6 +3,7 @@
/** @file ai_airport.cpp Implementation of AIAirport. */
#include "ai_airport.hpp"
+#include "ai_error.hpp"
#include "../../openttd.h"
#include "../../variables.h"
#include "../../station_map.h"
@@ -49,16 +50,20 @@
/* static */ bool AIAirport::BuildAirport(TileIndex tile, AirportType type)
{
- if (!::IsValidTile(tile)) return false;
- if (type > AT_HELISTATION) return 0;
+ if (!::IsValidTile(tile) || type > AT_HELISTATION) {
+ AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
+ return false;
+ }
return AIObject::DoCommand(tile, type, 0, CMD_BUILD_AIRPORT);
}
/* static */ bool AIAirport::RemoveAirport(TileIndex tile)
{
- if (!::IsValidTile(tile)) return false;
- if (!IsAirportTile(tile) && !IsHangarTile(tile)) return false;
+ if (!::IsValidTile(tile) || !(IsAirportTile(tile) || IsHangarTile(tile))) {
+ AIObject::SetLastError(AIError::ERR_PRECONDITION_FAILED);
+ return false;
+ }
return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
}
--- a/src/ai/api/ai_airport.hpp Thu Apr 03 23:01:54 2008 +0000
+++ b/src/ai/api/ai_airport.hpp Fri Apr 04 10:43:13 2008 +0000
@@ -91,6 +91,12 @@
* @param tile The topleft corner of the airport.
* @param type The type of airport to build.
* @pre AIMap::IsValidTile(tile).
+ * @pre AirportAvailable(type).
+ * @exception AIError::ERR_AREA_NOT_CLEAR
+ * @exception AIError::ERR_FLAT_LAND_REQUIRED
+ * @exception AIError::ERR_LOCAL_AUTHORITY_REFUSES
+ * @exception AIStation::ERR_STATION_TOO_LARGE
+ * @exception AIStation::ERR_STATION_TOO_CLOSE_TO_OTHER_STATION
* @return Whether the airport has been/can be build or not.
*/
static bool BuildAirport(TileIndex tile, AirportType type);
@@ -99,6 +105,7 @@
* Removes a airport.
* @param tile Any tile of the airport.
* @pre AIMap::IsValidTile(tile).
+ * @exception AIError::ERR_AREA_IS_OWNED_BY_ANOTHER_COMPANY
* @return Whether the airport has been/can be removed or not.
*/
static bool RemoveAirport(TileIndex tile);
--- a/src/ai/api/ai_company.hpp Thu Apr 03 23:01:54 2008 +0000
+++ b/src/ai/api/ai_company.hpp Fri Apr 04 10:43:13 2008 +0000
@@ -130,6 +130,7 @@
* @param tile The tile to build your HQ on, this tile is the most nothern tile of your HQ.
* @pre AIMap::IsValidTile(tile).
* @exception AIError::ERR_AREA_NOT_CLEAR
+ * @exception AIError::ERR_FLAT_LAND_REQUIRED
* @return True if the HQ could be build.
* @note An HQ can not be removed, only by water or rebuilding; If an HQ is
* build again, the old one is removed.
--- a/src/ai/api/ai_error.hpp Thu Apr 03 23:01:54 2008 +0000
+++ b/src/ai/api/ai_error.hpp Fri Apr 04 10:43:13 2008 +0000
@@ -22,6 +22,7 @@
ERR_CAT_NONE = 0, //!< Error messages not related to any category.
ERR_CAT_GENERAL, //!< Error messages related to general things.
ERR_CAT_VEHICLE, //!< Error messages related to building / maintaining vehicles.
+ ERR_CAT_STATION, //!< Error messages related to building / maintaining stations.
/**
* DO NOT USE! The error bitsize determines how many errors can be stored in
--- a/src/ai/api/ai_station.hpp Thu Apr 03 23:01:54 2008 +0000
+++ b/src/ai/api/ai_station.hpp Fri Apr 04 10:43:13 2008 +0000
@@ -6,6 +6,7 @@
#define AI_STATION_HPP
#include "ai_object.hpp"
+#include "ai_error.hpp"
#include "../../station_type.h"
/**
@@ -16,6 +17,20 @@
static const char *GetClassName() { return "AIStation"; }
/**
+ * All station related errors.
+ */
+ enum ErrorMessages {
+ /** Base for station related errors */
+ ERR_STATION_BASE = AIError::ERR_CAT_STATION << AIError::ERR_CAT_BIT_SIZE,
+
+ /** The station size exceeds the station spread */
+ ERR_STATION_TOO_LARGE, // [STR_306C_STATION_TOO_SPREAD_OUT]
+
+ /** The station is build too close to another station */
+ ERR_STATION_TOO_CLOSE_TO_OTHER_STATION, // [STR_300D_TOO_CLOSE_TO_ANOTHER_AIRPORT, STR_3009_TOO_CLOSE_TO_ANOTHER_STATION]
+ };
+
+ /**
* Type of stations known in the game.
*/
enum StationType {
--- a/src/ai/api/ai_station.hpp.sq Thu Apr 03 23:01:54 2008 +0000
+++ b/src/ai/api/ai_station.hpp.sq Fri Apr 04 10:43:13 2008 +0000
@@ -2,6 +2,8 @@
namespace SQConvert {
/* Allow enums to be used as Squirrel parameters */
+ template <> AIStation::ErrorMessages GetParam(ForceType<AIStation::ErrorMessages>, HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AIStation::ErrorMessages)tmp; }
+ template <> int Return<AIStation::ErrorMessages>(HSQUIRRELVM vm, AIStation::ErrorMessages res) { sq_pushinteger(vm, (int32)res); return 1; }
template <> AIStation::StationType GetParam(ForceType<AIStation::StationType>, HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AIStation::StationType)tmp; }
template <> int Return<AIStation::StationType>(HSQUIRRELVM vm, AIStation::StationType res) { sq_pushinteger(vm, (int32)res); return 1; }
@@ -18,12 +20,22 @@
SQAIStation.PreRegister(engine);
SQAIStation.AddConstructor<void (AIStation::*)(), 1>(engine, "x");
- SQAIStation.DefSQConst(engine, AIStation::STATION_ANY, "STATION_ANY");
- SQAIStation.DefSQConst(engine, AIStation::STATION_TRAIN, "STATION_TRAIN");
- SQAIStation.DefSQConst(engine, AIStation::STATION_TRUCK_STOP, "STATION_TRUCK_STOP");
- SQAIStation.DefSQConst(engine, AIStation::STATION_BUS_STOP, "STATION_BUS_STOP");
- SQAIStation.DefSQConst(engine, AIStation::STATION_AIRPORT, "STATION_AIRPORT");
- SQAIStation.DefSQConst(engine, AIStation::STATION_DOCK, "STATION_DOCK");
+ SQAIStation.DefSQConst(engine, AIStation::ERR_STATION_BASE, "ERR_STATION_BASE");
+ SQAIStation.DefSQConst(engine, AIStation::ERR_STATION_TOO_LARGE, "ERR_STATION_TOO_LARGE");
+ SQAIStation.DefSQConst(engine, AIStation::ERR_STATION_TOO_CLOSE_TO_OTHER_STATION, "ERR_STATION_TOO_CLOSE_TO_OTHER_STATION");
+ SQAIStation.DefSQConst(engine, AIStation::STATION_ANY, "STATION_ANY");
+ SQAIStation.DefSQConst(engine, AIStation::STATION_TRAIN, "STATION_TRAIN");
+ SQAIStation.DefSQConst(engine, AIStation::STATION_TRUCK_STOP, "STATION_TRUCK_STOP");
+ SQAIStation.DefSQConst(engine, AIStation::STATION_BUS_STOP, "STATION_BUS_STOP");
+ SQAIStation.DefSQConst(engine, AIStation::STATION_AIRPORT, "STATION_AIRPORT");
+ SQAIStation.DefSQConst(engine, AIStation::STATION_DOCK, "STATION_DOCK");
+
+ AIError::RegisterErrorMap(STR_306C_STATION_TOO_SPREAD_OUT, AIStation::ERR_STATION_TOO_LARGE);
+ AIError::RegisterErrorMap(STR_300D_TOO_CLOSE_TO_ANOTHER_AIRPORT, AIStation::ERR_STATION_TOO_CLOSE_TO_OTHER_STATION);
+ AIError::RegisterErrorMap(STR_3009_TOO_CLOSE_TO_ANOTHER_STATION, AIStation::ERR_STATION_TOO_CLOSE_TO_OTHER_STATION);
+
+ AIError::RegisterErrorMapString(AIStation::ERR_STATION_TOO_LARGE, "ERR_STATION_TOO_LARGE");
+ AIError::RegisterErrorMapString(AIStation::ERR_STATION_TOO_CLOSE_TO_OTHER_STATION, "ERR_STATION_TOO_CLOSE_TO_OTHER_STATION");
SQAIStation.DefSQStaticMethod(engine, &AIStation::GetClassName, "GetClassName", 1, "x");
SQAIStation.DefSQStaticMethod(engine, &AIStation::IsValidStation, "IsValidStation", 2, "xi");