ai_error.cpp

Go to the documentation of this file.
00001 /* $Id: ai_error.cpp 15299 2009-01-31 20:16:06Z smatz $ */
00002 
00005 #include "ai_error.hpp"
00006 #include "../../core/bitmath_func.hpp"
00007 
00008 AIError::AIErrorMap AIError::error_map = AIError::AIErrorMap();
00009 AIError::AIErrorMapString AIError::error_map_string = AIError::AIErrorMapString();
00010 
00011 /* static */ AIErrorType AIError::GetLastError()
00012 {
00013   return AIObject::GetLastError();
00014 }
00015 
00016 /* static */ char *AIError::GetLastErrorString()
00017 {
00018   return strdup((*error_map_string.find(AIError::GetLastError())).second);
00019 }
00020 
00021 /* static */ AIErrorType AIError::StringToError(StringID internal_string_id)
00022 {
00023   uint index = GB(internal_string_id, 11, 5);
00024   switch (GB(internal_string_id, 11, 5)) {
00025     case 26: case 28: case 29: case 30: // NewGRF strings.
00026       return ERR_NEWGRF_SUPPLIED_ERROR;
00027 
00028     /* DO NOT SWAP case 14 and 4 because that will break StringToError due
00029      * to the index dependency that relies on FALL THROUGHs. */
00030     case 14: if (index < 0xE4) break; // Player name
00031     case 4:  if (index < 0xC0) break; // Town name
00032     case 15: // Custom name
00033     case 31: // Dynamic strings
00034       /* These strings are 'random' and have no meaning.
00035        * They actually shouldn't even be returned as error messages. */
00036       return ERR_UNKNOWN;
00037 
00038     default:
00039       break;
00040   }
00041 
00042   AIErrorMap::iterator it = error_map.find(internal_string_id);
00043   if (it == error_map.end()) return ERR_UNKNOWN;
00044   return (*it).second;
00045 }
00046 
00047 /* static */ void AIError::RegisterErrorMap(StringID internal_string_id, AIErrorType ai_error_msg)
00048 {
00049   error_map[internal_string_id] = ai_error_msg;
00050 }
00051 
00052 /* static */ void AIError::RegisterErrorMapString(AIErrorType ai_error_msg, const char *message)
00053 {
00054   error_map_string[ai_error_msg] = message;
00055 }
00056 
00057 /* static */ AIError::ErrorCategories AIError::GetErrorCategory() {
00058   return (AIError::ErrorCategories)(GetLastError() >> (uint)ERR_CAT_BIT_SIZE);
00059 }

Generated on Mon Dec 14 20:59:57 2009 for OpenTTD by  doxygen 1.5.6