# ai_error.cpp

Go to the documentation of this file.

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