(svn r12552) [NoAI] -Change: do not force the use of StringIDs when setting errors. noai
authorrubidium
Thu, 03 Apr 2008 22:18:52 +0000
branchnoai
changeset 9863 73647fe2e301
parent 9862 26f6a061eae6
child 9864 4a9b7b610b13
(svn r12552) [NoAI] -Change: do not force the use of StringIDs when setting errors.
src/ai/api/ai_error.cpp
src/ai/api/ai_error.hpp
src/ai/api/ai_error.hpp.sq
src/ai/api/ai_object.cpp
src/ai/api/ai_object.hpp
--- a/src/ai/api/ai_error.cpp	Thu Apr 03 22:09:54 2008 +0000
+++ b/src/ai/api/ai_error.cpp	Thu Apr 03 22:18:52 2008 +0000
@@ -10,11 +10,7 @@
 
 /* static */ uint AIError::GetLastError()
 {
-	if (AIObject::GetLastError() == STR_NULL) return ERR_NONE;
-
-	AIErrorMap::iterator it = error_map.find(AIObject::GetLastError());
-	if (it == error_map.end()) return ERR_UNKNOWN;
-	return (*it).second;
+	return AIObject::GetLastError();
 }
 
 /* static */ const char *AIError::GetLastErrorString()
@@ -22,6 +18,13 @@
 	return (*error_map_string.find(AIError::GetLastError())).second;
 }
 
+/* static */ uint AIError::StringToError(uint internal_string_id)
+{
+	AIErrorMap::iterator it = error_map.find(internal_string_id);
+	if (it == error_map.end()) return ERR_UNKNOWN;
+	return (*it).second;
+}
+
 /* static */ void AIError::RegisterErrorMap(uint internal_string_id, uint ai_error_msg)
 {
 	error_map[internal_string_id] = ai_error_msg;
--- a/src/ai/api/ai_error.hpp	Thu Apr 03 22:09:54 2008 +0000
+++ b/src/ai/api/ai_error.hpp	Thu Apr 03 22:18:52 2008 +0000
@@ -38,6 +38,8 @@
 		ERR_NONE = ERR_CAT_NONE << ERR_CAT_BIT_SIZE,  // []
 		/** If an error occured and the error wasn't mapped */
 		ERR_UNKNOWN,                                  // []
+		/** If a precondition is not met */
+		ERR_PRECONDITION_FAILED,                      // []
 
 		/** Base for general errors */
 		ERR_GENERAL_BASE = ERR_CAT_GENERAL << ERR_CAT_BIT_SIZE,
@@ -78,6 +80,14 @@
 	static const char *GetLastErrorString();
 
 	/**
+	 * Get the error based on the OpenTTD StringID.
+	 * @note DO NOT INVOKE THIS METHOD YOURSELF!
+	 * @param internal_string_id The string to convert.
+	 * @return The NoAI equivalent error message.
+	 */
+	static uint StringToError(uint internal_string_id);
+
+	/**
 	 * Map an internal OpenTTD error message to it's NoAI equivalent.
 	 * @note DO NOT INVOKE THIS METHOD YOURSELF! The calls are autogenerated.
 	 * @param internal_string_id The OpenTTD StringID used for an error.
--- a/src/ai/api/ai_error.hpp.sq	Thu Apr 03 22:09:54 2008 +0000
+++ b/src/ai/api/ai_error.hpp.sq	Thu Apr 03 22:18:52 2008 +0000
@@ -26,6 +26,7 @@
 	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");
+	SQAIError.DefSQConst(engine, AIError::ERR_PRECONDITION_FAILED,              "ERR_PRECONDITION_FAILED");
 	SQAIError.DefSQConst(engine, AIError::ERR_GENERAL_BASE,                     "ERR_GENERAL_BASE");
 	SQAIError.DefSQConst(engine, AIError::ERR_NOT_ENOUGH_CASH,                  "ERR_NOT_ENOUGH_CASH");
 	SQAIError.DefSQConst(engine, AIError::ERR_LOCAL_AUTHORITY_REFUSES,          "ERR_LOCAL_AUTHORITY_REFUSES");
@@ -41,6 +42,7 @@
 
 	AIError::RegisterErrorMapString(AIError::ERR_NONE,                             "ERR_NONE");
 	AIError::RegisterErrorMapString(AIError::ERR_UNKNOWN,                          "ERR_UNKNOWN");
+	AIError::RegisterErrorMapString(AIError::ERR_PRECONDITION_FAILED,              "ERR_PRECONDITION_FAILED");
 	AIError::RegisterErrorMapString(AIError::ERR_NOT_ENOUGH_CASH,                  "ERR_NOT_ENOUGH_CASH");
 	AIError::RegisterErrorMapString(AIError::ERR_LOCAL_AUTHORITY_REFUSES,          "ERR_LOCAL_AUTHORITY_REFUSES");
 	AIError::RegisterErrorMapString(AIError::ERR_ALREADY_BUILT,                    "ERR_ALREADY_BUILT");
@@ -51,6 +53,7 @@
 	SQAIError.DefSQStaticMethod(engine, &AIError::GetErrorCategory,       "GetErrorCategory",       1, "x");
 	SQAIError.DefSQStaticMethod(engine, &AIError::GetLastError,           "GetLastError",           1, "x");
 	SQAIError.DefSQStaticMethod(engine, &AIError::GetLastErrorString,     "GetLastErrorString",     1, "x");
+	SQAIError.DefSQStaticMethod(engine, &AIError::StringToError,          "StringToError",          2, "xi");
 	SQAIError.DefSQStaticMethod(engine, &AIError::RegisterErrorMap,       "RegisterErrorMap",       3, "xii");
 	SQAIError.DefSQStaticMethod(engine, &AIError::RegisterErrorMapString, "RegisterErrorMapString", 3, "xis");
 
--- a/src/ai/api/ai_object.cpp	Thu Apr 03 22:09:54 2008 +0000
+++ b/src/ai/api/ai_object.cpp	Thu Apr 03 22:18:52 2008 +0000
@@ -4,6 +4,7 @@
 
 #include "ai_object.hpp"
 #include "ai_log.hpp"
+#include "ai_error.hpp"
 #include "table/strings.h"
 #include "../../command_func.h"
 #include "../../network/network.h"
@@ -55,12 +56,12 @@
 	return GetDoCommandStruct(_current_player)->costs.GetCost();
 }
 
-void AIObject::SetLastError(const StringID last_error)
+void AIObject::SetLastError(uint last_error)
 {
 	GetDoCommandStruct(_current_player)->last_error = last_error;
 }
 
-StringID AIObject::GetLastError()
+uint AIObject::GetLastError()
 {
 	return GetDoCommandStruct(_current_player)->last_error;
 }
@@ -140,7 +141,7 @@
 	const char *tmp_cmdtext;
 
 	/* Make sure the last error is reset, so we don't give faulty warnings */
-	SetLastError(STR_NULL);
+	SetLastError(AIError::ERR_NONE);
 
 	if (procc != CMD_LANDSCAPE_CLEAR) flags |= DC_AUTO;
 	if (water_protection) flags |= DC_NO_WATER;
@@ -152,7 +153,7 @@
 	res = ::DoCommand(tile, p1, p2, flags, procc);
 	/* The command failed, so return */
 	if (::CmdFailed(res)) {
-		SetLastError(_error_message);
+		AIObject::SetLastError(AIError::StringToError(_error_message));
 		return false;
 	}
 
@@ -198,7 +199,7 @@
 	}
 
 	if (::CmdFailed(res)) {
-		SetLastError(_error_message);
+		AIObject::SetLastError(AIError::StringToError(_error_message));
 		return false;
 	}
 
--- a/src/ai/api/ai_object.hpp	Thu Apr 03 22:09:54 2008 +0000
+++ b/src/ai/api/ai_object.hpp	Thu Apr 03 22:18:52 2008 +0000
@@ -40,7 +40,7 @@
 		AIObject *mode_instance;
 		uint delay;
 		CommandCost costs;
-		StringID last_error;
+		uint last_error;
 		bool last_command_res;
 		VehicleID new_vehicle_id;
 		SignID new_sign_id;
@@ -76,13 +76,14 @@
 
 	/**
 	 * Set the DoCommand last error.
+	 * @note last_error is an ERR_* from a ErrorMessages enum.
 	 */
-	static void SetLastError(const StringID last_error);
+	static void SetLastError(uint last_error);
 
 	/**
 	 * Get the DoCommand last error.
 	 */
-	static StringID GetLastError();
+	static uint GetLastError();
 
 	/**
 	 * Set the current mode of your AI to this proc.