ai_error.cpp

Go to the documentation of this file.
00001 /* $Id: ai_error.cpp 17692 2009-10-04 17:10:57Z smatz $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
00006  * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
00007  * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
00008  */
00009 
00012 #include "ai_error.hpp"
00013 #include "../../core/bitmath_func.hpp"
00014 
00015 AIError::AIErrorMap AIError::error_map = AIError::AIErrorMap();
00016 AIError::AIErrorMapString AIError::error_map_string = AIError::AIErrorMapString();
00017 
00018 /* static */ AIErrorType AIError::GetLastError()
00019 {
00020   return AIObject::GetLastError();
00021 }
00022 
00023 /* static */ char *AIError::GetLastErrorString()
00024 {
00025   return strdup((*error_map_string.find(AIError::GetLastError())).second);
00026 }
00027 
00028 /* static */ AIErrorType AIError::StringToError(StringID internal_string_id)
00029 {
00030   uint index = GB(internal_string_id, 11, 5);
00031   switch (GB(internal_string_id, 11, 5)) {
00032     case 26: case 28: case 29: case 30: // NewGRF strings.
00033       return ERR_NEWGRF_SUPPLIED_ERROR;
00034 
00035     /* DO NOT SWAP case 14 and 4 because that will break StringToError due
00036      * to the index dependency that relies on FALL THROUGHs. */
00037     case 14: if (index < 0xE4) break; // Player name
00038     case 4:  if (index < 0xC0) break; // Town name
00039     case 15: // Custom name
00040     case 31: // Dynamic strings
00041       /* These strings are 'random' and have no meaning.
00042        * They actually shouldn't even be returned as error messages. */
00043       return ERR_UNKNOWN;
00044 
00045     default:
00046       break;
00047   }
00048 
00049   AIErrorMap::iterator it = error_map.find(internal_string_id);
00050   if (it == error_map.end()) return ERR_UNKNOWN;
00051   return (*it).second;
00052 }
00053 
00054 /* static */ void AIError::RegisterErrorMap(StringID internal_string_id, AIErrorType ai_error_msg)
00055 {
00056   error_map[internal_string_id] = ai_error_msg;
00057 }
00058 
00059 /* static */ void AIError::RegisterErrorMapString(AIErrorType ai_error_msg, const char *message)
00060 {
00061   error_map_string[ai_error_msg] = message;
00062 }
00063 
00064 /* static */ AIError::ErrorCategories AIError::GetErrorCategory()
00065 {
00066   return (AIError::ErrorCategories)(GetLastError() >> (uint)ERR_CAT_BIT_SIZE);
00067 }

Generated on Fri Dec 31 17:15:28 2010 for OpenTTD by  doxygen 1.6.1