summaryrefslogtreecommitdiff
path: root/src/ai/api/ai_error.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_error.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_error.cpp')
-rw-r--r--src/ai/api/ai_error.cpp60
1 files changed, 60 insertions, 0 deletions
diff --git a/src/ai/api/ai_error.cpp b/src/ai/api/ai_error.cpp
new file mode 100644
index 000000000..4c21db726
--- /dev/null
+++ b/src/ai/api/ai_error.cpp
@@ -0,0 +1,60 @@
+/* $Id$ */
+
+/** @file ai_error.cpp Implementation of AIError. */
+
+#include "ai_error.hpp"
+#include "table/strings.h"
+#include "../../core/bitmath_func.hpp"
+
+AIError::AIErrorMap AIError::error_map = AIError::AIErrorMap();
+AIError::AIErrorMapString AIError::error_map_string = AIError::AIErrorMapString();
+
+/* static */ AIErrorType AIError::GetLastError()
+{
+ return AIObject::GetLastError();
+}
+
+/* static */ const char *AIError::GetLastErrorString()
+{
+ return (*error_map_string.find(AIError::GetLastError())).second;
+}
+
+/* static */ AIErrorType AIError::StringToError(StringID internal_string_id)
+{
+ uint index = GB(internal_string_id, 11, 5);
+ switch (GB(internal_string_id, 11, 5)) {
+ case 26: case 28: case 29: case 30: // NewGRF strings.
+ return ERR_NEWGRF_SUPPLIED_ERROR;
+
+ /* DO NOT SWAP case 14 and 4 because that will break StringToError due
+ * to the index dependency that relies on FALL THROUGHs. */
+ case 14: if (index < 0xE4) break; // Player name
+ case 4: if (index < 0xC0) break; // Town name
+ case 15: // Custom name
+ case 31: // Dynamic strings
+ /* These strings are 'random' and have no meaning.
+ * They actually shouldn't even be returned as error messages. */
+ return ERR_UNKNOWN;
+
+ default:
+ break;
+ }
+
+ AIErrorMap::iterator it = error_map.find(internal_string_id);
+ if (it == error_map.end()) return ERR_UNKNOWN;
+ return (*it).second;
+}
+
+/* static */ void AIError::RegisterErrorMap(StringID internal_string_id, AIErrorType ai_error_msg)
+{
+ error_map[internal_string_id] = ai_error_msg;
+}
+
+/* static */ void AIError::RegisterErrorMapString(AIErrorType ai_error_msg, const char *message)
+{
+ error_map_string[ai_error_msg] = message;
+}
+
+/* static */ AIError::ErrorCategories AIError::GetErrorCategory() {
+ return (AIError::ErrorCategories)(GetLastError() >> (uint)ERR_CAT_BIT_SIZE);
+}