(svn r12614) [NoAI] -Add: support for GetLastError in AISign. Patch by Morloth. noai
authorrubidium
Mon, 07 Apr 2008 18:41:49 +0000
branchnoai
changeset 10090 d6a6cac2fb25
parent 10089 e351169bf3a5
child 10091 e4feb2f9fedf
(svn r12614) [NoAI] -Add: support for GetLastError in AISign. Patch by Morloth.
src/ai/api/ai_error.hpp
src/ai/api/ai_error.hpp.sq
src/ai/api/ai_sign.cpp
src/ai/api/ai_sign.hpp
src/ai/api/ai_sign.hpp.sq
--- a/src/ai/api/ai_error.hpp	Mon Apr 07 18:37:23 2008 +0000
+++ b/src/ai/api/ai_error.hpp	Mon Apr 07 18:41:49 2008 +0000
@@ -37,6 +37,7 @@
 		ERR_CAT_BRIDGE,   //!< Error messages related to building / removing bridges.
 		ERR_CAT_TUNNEL,   //!< Error messages related to building / removing tunnels.
 		ERR_CAT_TILE,     //!< Error messages related to raising / lowering and demolishing tiles.
+		ERR_CAT_SIGN,     //!< Error messages related to building / removing signs.
 
 		/**
 		 * 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:37:23 2008 +0000
+++ b/src/ai/api/ai_error.hpp.sq	Mon Apr 07 18:41:49 2008 +0000
@@ -26,6 +26,8 @@
 	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_TILE,                         "ERR_CAT_TILE");
+	SQAIError.DefSQConst(engine, AIError::ERR_CAT_SIGN,                         "ERR_CAT_SIGN");
 	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_sign.cpp	Mon Apr 07 18:37:23 2008 +0000
+++ b/src/ai/api/ai_sign.cpp	Mon Apr 07 18:41:49 2008 +0000
@@ -7,6 +7,7 @@
 #include "../../command_func.h"
 #include "../../core/alloc_func.hpp"
 #include "../../signs_base.h"
+#include "../../string_func.h"
 #include "../../strings_func.h"
 #include "../../tile_map.h"
 
@@ -46,13 +47,15 @@
 
 /* static */ bool AISign::RemoveSign(SignID sign_id)
 {
+	EnforcePrecondition(false, IsValidSign(sign_id));
 	_cmd_text = "";
 	return AIObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN);
 }
 
 /* static */ SignID AISign::BuildSign(TileIndex location, const char *text)
 {
-	if (!::IsValidTile(location)) return INVALID_SIGN;
+	EnforcePrecondition(INVALID_SIGN, ::IsValidTile(location));
+	EnforcePrecondition(INVALID_SIGN, !StrEmpty(text));
 
 	/* Reset the internal NewSignID in case we are in TestMode */
 	AIObject::SetNewSignID(0);
--- a/src/ai/api/ai_sign.hpp	Mon Apr 07 18:37:23 2008 +0000
+++ b/src/ai/api/ai_sign.hpp	Mon Apr 07 18:41:49 2008 +0000
@@ -6,6 +6,7 @@
 #define AI_SIGN_HPP
 
 #include "ai_object.hpp"
+#include "ai_error.hpp"
 
 /**
  * Class that handles all sign related functions.
@@ -14,6 +15,15 @@
 public:
 	static const char *GetClassName() { return "AISign"; }
 
+	enum ErrorMessages {
+
+		/** Base for sign building related errors */
+		ERR_SIGN_BASE = AIError::ERR_CAT_SIGN << AIError::ERR_CAT_BIT_SIZE,
+
+		/** Too many signs have been placed */
+		ERR_SIGN_TOO_MANY_SIGNS,             // [STR_2808_TOO_MANY_SIGNS]
+	};
+
 	/**
 	 * Gets the maximum sign index; there are no valid signs with a higher index.
 	 * @return The maximum sign index.
@@ -57,6 +67,8 @@
 	 * @param location The place to build the sign.
 	 * @param text The text to place on the sign.
 	 * @pre AIMap::IsValidTile(location).
+	 * @pre text is of non-zero length.
+	 * @exception ERR_SIGN_TOO_MANY_SIGNS
 	 * @return The SignID of the build sign (use IsValidSign() to check for validity).
 	 *   In test-mode it returns 0 if successful, or any other value to indicate
 	 *   failure.
--- a/src/ai/api/ai_sign.hpp.sq	Mon Apr 07 18:37:23 2008 +0000
+++ b/src/ai/api/ai_sign.hpp.sq	Mon Apr 07 18:41:49 2008 +0000
@@ -1,6 +1,10 @@
 #include "ai_sign.hpp"
 
 namespace SQConvert {
+	/* Allow enums to be used as Squirrel parameters */
+	template <> AISign::ErrorMessages GetParam(ForceType<AISign::ErrorMessages>, HSQUIRRELVM vm, int index) { SQInteger tmp; sq_getinteger(vm, index, &tmp); return (AISign::ErrorMessages)tmp; }
+	template <> int Return<AISign::ErrorMessages>(HSQUIRRELVM vm, AISign::ErrorMessages res) { sq_pushinteger(vm, (int32)res); return 1; }
+
 	/* Allow AISign to be used as Squirrel parameter */
 	template <> AISign *GetParam(ForceType<AISign *>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return  (AISign *)instance; }
 	template <> AISign &GetParam(ForceType<AISign &>, HSQUIRRELVM vm, int index) { SQUserPointer instance; sq_getinstanceup(vm, index, &instance, 0); return *(AISign *)instance; }
@@ -14,6 +18,13 @@
 	SQAISign.PreRegister(engine);
 	SQAISign.AddConstructor<void (AISign::*)(), 1>(engine, "x");
 
+	SQAISign.DefSQConst(engine, AISign::ERR_SIGN_BASE,           "ERR_SIGN_BASE");
+	SQAISign.DefSQConst(engine, AISign::ERR_SIGN_TOO_MANY_SIGNS, "ERR_SIGN_TOO_MANY_SIGNS");
+
+	AIError::RegisterErrorMap(STR_2808_TOO_MANY_SIGNS, AISign::ERR_SIGN_TOO_MANY_SIGNS);
+
+	AIError::RegisterErrorMapString(AISign::ERR_SIGN_TOO_MANY_SIGNS, "ERR_SIGN_TOO_MANY_SIGNS");
+
 	SQAISign.DefSQStaticMethod(engine, &AISign::GetClassName, "GetClassName", 1, "x");
 	SQAISign.DefSQStaticMethod(engine, &AISign::GetMaxSignID, "GetMaxSignID", 1, "x");
 	SQAISign.DefSQStaticMethod(engine, &AISign::GetSignCount, "GetSignCount", 1, "x");