(svn r12612) [NoAI] -Add: support for GetLastError for AITunnel. Patch by Morloth.
--- a/bin/ai/regression/regression.nut Mon Apr 07 18:29:12 2008 +0000
+++ b/bin/ai/regression/regression.nut Mon Apr 07 18:32:42 2008 +0000
@@ -969,6 +969,12 @@
print(" IsTunnelTile(): " + AITunnel.IsTunnelTile(28026));
print(" RemoveTunnel(): " + AITunnel.RemoveTunnel(29050));
print(" IsTunnelTile(): " + AITunnel.IsTunnelTile(29050));
+
+ print(" --Errors--");
+ print(" BuildTunnel(): " + AITunnel.BuildTunnel(AIVehicle.VEHICLE_ROAD, 7529));
+ print(" BuildTunnel(): " + AITunnel.BuildTunnel(AIVehicle.VEHICLE_ROAD, 8043));
+ print(" GetLastErrorString(): " + AIError.GetLastErrorString());
+ print(" RemoveTunnel(): " + AITunnel.RemoveTunnel(7529));
}
function Regression::Vehicle()
--- a/bin/ai/regression/regression.txt Mon Apr 07 18:29:12 2008 +0000
+++ b/bin/ai/regression/regression.txt Mon Apr 07 18:32:42 2008 +0000
@@ -6151,6 +6151,11 @@
IsTunnelTile(): true
RemoveTunnel(): true
IsTunnelTile(): false
+ --Errors--
+ BuildTunnel(): true
+ BuildTunnel(): false
+ GetLastErrorString(): ERR_TUNNEL_ANOTHER_TUNNEL_IN_THE_WAY
+ RemoveTunnel(): true
--Vehicle--
IsValidVehicle(-1): false
@@ -6179,7 +6184,7 @@
IsStoppedInDepot(): false
--Accounting--
GetCosts(): -5945
- Should be: -5944
+ Should be: -5945
GetName(): Road Vehicle 1
SetName(): true
GetName(): MyVehicleName
@@ -6192,7 +6197,7 @@
GetMaxAge(): 5490
GetAgeLeft(): 5489
GetRunningCost(): 421
- GetProfitThisYear(): -1
+ GetProfitThisYear(): 0
GetProfitLastYear(): 0
GetVehicleType(): 1
IsInDepot(): false
@@ -6257,7 +6262,7 @@
15 => 0
13 => 0
12 => 0
- 11 => -1
+ 11 => 0
ProfitLastYear ListDump:
15 => 0
13 => 0
@@ -6318,25 +6323,9 @@
GetEventType: 3
EventName: SubsidyOffer
CargoID: 0
- GetFromTownID: 24
- GetToTownID: 65535
- GetFromIndustryID: 65535
- GetToIndustryID: 16
- GetNextEvent: instance
- GetEventType: 3
- EventName: SubsidyOffer
- CargoID: 0
- GetFromTownID: 20
+ GetFromTownID: 6
GetToTownID: 65535
GetFromIndustryID: 65535
- GetToIndustryID: 6
- GetNextEvent: instance
- GetEventType: 3
- EventName: SubsidyOffer
- CargoID: 0
- GetFromTownID: 21
- GetToTownID: 65535
- GetFromIndustryID: 65535
- GetToIndustryID: 24
+ GetToIndustryID: 15
IsEventWaiting: false
ERROR: We've got a suicidal AI for player 1
--- a/src/ai/api/ai_error.hpp Mon Apr 07 18:29:12 2008 +0000
+++ b/src/ai/api/ai_error.hpp Mon Apr 07 18:32:42 2008 +0000
@@ -35,6 +35,7 @@
ERR_CAT_VEHICLE, //!< Error messages related to building / maintaining vehicles.
ERR_CAT_STATION, //!< Error messages related to building / maintaining stations.
ERR_CAT_BRIDGE, //!< Error messages related to building / removing bridges.
+ ERR_CAT_TUNNEL, //!< Error messages related to building / removing tunnels.
/**
* DO NOT USE! The error bitsize determines how many errors can be stored in
--- a/src/ai/api/ai_error.hpp.sq Mon Apr 07 18:29:12 2008 +0000
+++ b/src/ai/api/ai_error.hpp.sq Mon Apr 07 18:32:42 2008 +0000
@@ -25,6 +25,7 @@
SQAIError.DefSQConst(engine, AIError::ERR_CAT_VEHICLE, "ERR_CAT_VEHICLE");
SQAIError.DefSQConst(engine, AIError::ERR_CAT_STATION, "ERR_CAT_STATION");
SQAIError.DefSQConst(engine, AIError::ERR_CAT_BRIDGE, "ERR_CAT_BRIDGE");
+ SQAIError.DefSQConst(engine, AIError::ERR_CAT_TUNNEL, "ERR_CAT_TUNNEL");
SQAIError.DefSQConst(engine, AIError::ERR_CAT_BIT_SIZE, "ERR_CAT_BIT_SIZE");
SQAIError.DefSQConst(engine, AIError::ERR_NONE, "ERR_NONE");
SQAIError.DefSQConst(engine, AIError::ERR_UNKNOWN, "ERR_UNKNOWN");
--- a/src/ai/api/ai_tunnel.cpp Mon Apr 07 18:29:12 2008 +0000
+++ b/src/ai/api/ai_tunnel.cpp Mon Apr 07 18:32:42 2008 +0000
@@ -28,8 +28,8 @@
/* static */ bool AITunnel::BuildTunnel(AIVehicle::VehicleType vehicle_type, TileIndex start)
{
- if (!::IsValidTile(start)) return false;
- if (vehicle_type != AIVehicle::VEHICLE_RAIL && vehicle_type != AIVehicle::VEHICLE_ROAD) return false;
+ EnforcePrecondition(false, ::IsValidTile(start));
+ EnforcePrecondition(false, vehicle_type == AIVehicle::VEHICLE_RAIL || vehicle_type == AIVehicle::VEHICLE_ROAD);
uint type = 0;
if (vehicle_type == AIVehicle::VEHICLE_ROAD) {
@@ -45,7 +45,7 @@
/* static */ bool AITunnel::RemoveTunnel(TileIndex tile)
{
- if (!IsTunnelTile(tile)) return false;
+ EnforcePrecondition(false, IsTunnelTile(tile));
return AIObject::DoCommand(tile, 0, 0, CMD_LANDSCAPE_CLEAR);
}
--- a/src/ai/api/ai_tunnel.hpp Mon Apr 07 18:29:12 2008 +0000
+++ b/src/ai/api/ai_tunnel.hpp Mon Apr 07 18:32:42 2008 +0000
@@ -16,6 +16,27 @@
static const char *GetClassName() { return "AITunnel"; }
/**
+ * All tunnel related errors.
+ */
+ enum ErrorMessages {
+
+ /** Base for bridge related errors */
+ ERR_TUNNEL_BASE = AIError::ERR_CAT_TUNNEL << AIError::ERR_CAT_BIT_SIZE,
+
+ /** Can't build tunnels on water */
+ ERR_TUNNEL_CANNOT_BUILD_ON_WATER, // [STR_3807_CAN_T_BUILD_ON_WATER]
+
+ /** The start tile must slope either North, South, West or East */
+ ERR_TUNNEL_START_SITE_UNSUITABLE, // [STR_500B_SITE_UNSUITABLE_FOR_TUNNEL]
+
+ /** An other tunnel is in the way */
+ ERR_TUNNEL_ANOTHER_TUNNEL_IN_THE_WAY, // [STR_5003_ANOTHER_TUNNEL_IN_THE_WAY]
+
+ /** Unable to excavate land at the end to create the tunnel's exit */
+ ERR_TUNNEL_END_SITE_UNSUITABLE, // [STR_5005_UNABLE_TO_EXCAVATE_LAND]
+ };
+
+ /**
* Check whether the tile is an entrance to a tunnel.
* @param tile The tile to check.
* @pre AIMap::IsValidTile(tile).
@@ -41,6 +62,11 @@
* @param vehicle_type The vehicle-type of tunnel to build.
* @pre AIMap::IsValidTile(start).
* @pre 'vehicle_type' is either AIVehicle::VEHICLE_RAIL or AIVEHICLE::VEHICLE_ROAD.
+ * @exception AIError::ERR_AREA_NOT_CLEAR
+ * @exception AITunnel::ERR_TUNNEL_CANNOT_BUILD_ON_WATER
+ * @exception AITunnel::ERR_TUNNEL_START_SITE_UNSUITABLE
+ * @exception AITunnel::ERR_TUNNEL_ANOTHER_TUNNEL_IN_THE_WAY
+ * @exception AITunnel::ERR_TUNNEL_END_SITE_UNSUITABLE
* @return Whether the tunnel has been/can be build or not.
* @note The slope of a tile can be determined by AITile::GetSlope(TileIndex).
*/
--- a/src/ai/api/ai_tunnel.hpp.sq Mon Apr 07 18:29:12 2008 +0000
+++ b/src/ai/api/ai_tunnel.hpp.sq Mon Apr 07 18:32:42 2008 +0000
@@ -1,6 +1,10 @@
#include "ai_tunnel.hpp"
namespace SQConvert {
+ /* Allow enums to be used as Squirrel parameters */
+ template <> AITunnel::ErrorMessages GetParam(ForceType<AITunnel::ErrorMessages>, HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AITunnel::ErrorMessages)tmp; }
+ template <> int Return<AITunnel::ErrorMessages>(HSQUIRRELVM vm, AITunnel::ErrorMessages res) { sq_pushinteger(vm, (int32)res); return 1; }
+
/* Allow AITunnel to be used as Squirrel parameter */
template <> AITunnel *GetParam(ForceType<AITunnel *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return (AITunnel *)instance; }
template <> AITunnel &GetParam(ForceType<AITunnel &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AITunnel *)instance; }
@@ -14,6 +18,22 @@
SQAITunnel.PreRegister(engine);
SQAITunnel.AddConstructor<void (AITunnel::*)(), 1>(engine, "x");
+ SQAITunnel.DefSQConst(engine, AITunnel::ERR_TUNNEL_BASE, "ERR_TUNNEL_BASE");
+ SQAITunnel.DefSQConst(engine, AITunnel::ERR_TUNNEL_CANNOT_BUILD_ON_WATER, "ERR_TUNNEL_CANNOT_BUILD_ON_WATER");
+ SQAITunnel.DefSQConst(engine, AITunnel::ERR_TUNNEL_START_SITE_UNSUITABLE, "ERR_TUNNEL_START_SITE_UNSUITABLE");
+ SQAITunnel.DefSQConst(engine, AITunnel::ERR_TUNNEL_ANOTHER_TUNNEL_IN_THE_WAY, "ERR_TUNNEL_ANOTHER_TUNNEL_IN_THE_WAY");
+ SQAITunnel.DefSQConst(engine, AITunnel::ERR_TUNNEL_END_SITE_UNSUITABLE, "ERR_TUNNEL_END_SITE_UNSUITABLE");
+
+ AIError::RegisterErrorMap(STR_3807_CAN_T_BUILD_ON_WATER, AITunnel::ERR_TUNNEL_CANNOT_BUILD_ON_WATER);
+ AIError::RegisterErrorMap(STR_500B_SITE_UNSUITABLE_FOR_TUNNEL, AITunnel::ERR_TUNNEL_START_SITE_UNSUITABLE);
+ AIError::RegisterErrorMap(STR_5003_ANOTHER_TUNNEL_IN_THE_WAY, AITunnel::ERR_TUNNEL_ANOTHER_TUNNEL_IN_THE_WAY);
+ AIError::RegisterErrorMap(STR_5005_UNABLE_TO_EXCAVATE_LAND, AITunnel::ERR_TUNNEL_END_SITE_UNSUITABLE);
+
+ AIError::RegisterErrorMapString(AITunnel::ERR_TUNNEL_CANNOT_BUILD_ON_WATER, "ERR_TUNNEL_CANNOT_BUILD_ON_WATER");
+ AIError::RegisterErrorMapString(AITunnel::ERR_TUNNEL_START_SITE_UNSUITABLE, "ERR_TUNNEL_START_SITE_UNSUITABLE");
+ AIError::RegisterErrorMapString(AITunnel::ERR_TUNNEL_ANOTHER_TUNNEL_IN_THE_WAY, "ERR_TUNNEL_ANOTHER_TUNNEL_IN_THE_WAY");
+ AIError::RegisterErrorMapString(AITunnel::ERR_TUNNEL_END_SITE_UNSUITABLE, "ERR_TUNNEL_END_SITE_UNSUITABLE");
+
SQAITunnel.DefSQStaticMethod(engine, &AITunnel::GetClassName, "GetClassName", 1, "x");
SQAITunnel.DefSQStaticMethod(engine, &AITunnel::IsTunnelTile, "IsTunnelTile", 2, "xi");
SQAITunnel.DefSQStaticMethod(engine, &AITunnel::GetOtherTunnelEnd, "GetOtherTunnelEnd", 2, "xi");