src/ai/api/ai_error.cpp
author truebrain
Wed, 23 Apr 2008 12:05:32 +0000
branchnoai
changeset 10308 0c81dfce3e9b
parent 9871 dfafb0cf20ee
permissions -rw-r--r--
(svn r12849) [NoAI] -Fix: when .hpp.sq doesn't exists, don't run diff (tnx to Yexo)
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
     1
/* $Id$ */
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
     2
9849
edf90dda4ec4 (svn r12517) [NoAI] -Fix: type in @file of ai_error.cpp
truebrain
parents: 9844
diff changeset
     3
/** @file ai_error.cpp Implementation of AIError. */
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
     4
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
     5
#include "ai_error.hpp"
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
     6
#include "table/strings.h"
9871
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
     7
#include "../../core/bitmath_func.hpp"
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
     8
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
     9
AIError::AIErrorMap AIError::error_map = AIError::AIErrorMap();
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    10
AIError::AIErrorMapString AIError::error_map_string = AIError::AIErrorMapString();
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    11
9864
4a9b7b610b13 (svn r12554) [NoAI] -Codechange: add more typing information.
rubidium
parents: 9863
diff changeset
    12
/* static */ AIErrorType AIError::GetLastError()
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    13
{
9863
73647fe2e301 (svn r12552) [NoAI] -Change: do not force the use of StringIDs when setting errors.
rubidium
parents: 9861
diff changeset
    14
	return AIObject::GetLastError();
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    15
}
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    16
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    17
/* static */ const char *AIError::GetLastErrorString()
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    18
{
9861
53fc03195e31 (svn r12544) [NoAI] -Codechange: do not force dependency on OTTD internal strings when defining errors.
rubidium
parents: 9849
diff changeset
    19
	return (*error_map_string.find(AIError::GetLastError())).second;
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    20
}
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    21
9864
4a9b7b610b13 (svn r12554) [NoAI] -Codechange: add more typing information.
rubidium
parents: 9863
diff changeset
    22
/* static */ AIErrorType AIError::StringToError(StringID internal_string_id)
9863
73647fe2e301 (svn r12552) [NoAI] -Change: do not force the use of StringIDs when setting errors.
rubidium
parents: 9861
diff changeset
    23
{
9871
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    24
	uint index = GB(internal_string_id, 11, 5);
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    25
	switch (GB(internal_string_id, 11, 5)) {
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    26
		case 26: case 28: case 29: case 30: // NewGRF strings.
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    27
			return ERR_NEWGRF_SUPPLIED_ERROR;
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    28
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    29
		/* DO NOT SWAP case 14 and 4 because that will break StringToError due
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    30
		 * to the index dependency that relies on FALL THROUGHs. */
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    31
		case 14: if (index < 0xE4) break; // Player name
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    32
		case 4:  if (index < 0xC0) break; // Town name
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    33
		case 15: // Custom name
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    34
		case 31: // Dynamic strings
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    35
			/* These strings are 'random' and have no meaning.
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    36
			 * They actually shouldn't even be returned as error messages. */
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    37
			return ERR_UNKNOWN;
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    38
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    39
		default:
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    40
			break;
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    41
	}
dfafb0cf20ee (svn r12603) [NoAI] -Add: 'support' for telling AIs that the last error message was a NewGRF generated message, so we can't say anything sensible about the real error message.
rubidium
parents: 9864
diff changeset
    42
9863
73647fe2e301 (svn r12552) [NoAI] -Change: do not force the use of StringIDs when setting errors.
rubidium
parents: 9861
diff changeset
    43
	AIErrorMap::iterator it = error_map.find(internal_string_id);
73647fe2e301 (svn r12552) [NoAI] -Change: do not force the use of StringIDs when setting errors.
rubidium
parents: 9861
diff changeset
    44
	if (it == error_map.end()) return ERR_UNKNOWN;
73647fe2e301 (svn r12552) [NoAI] -Change: do not force the use of StringIDs when setting errors.
rubidium
parents: 9861
diff changeset
    45
	return (*it).second;
73647fe2e301 (svn r12552) [NoAI] -Change: do not force the use of StringIDs when setting errors.
rubidium
parents: 9861
diff changeset
    46
}
73647fe2e301 (svn r12552) [NoAI] -Change: do not force the use of StringIDs when setting errors.
rubidium
parents: 9861
diff changeset
    47
9864
4a9b7b610b13 (svn r12554) [NoAI] -Codechange: add more typing information.
rubidium
parents: 9863
diff changeset
    48
/* static */ void AIError::RegisterErrorMap(StringID internal_string_id, AIErrorType ai_error_msg)
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    49
{
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    50
	error_map[internal_string_id] = ai_error_msg;
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    51
}
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    52
9864
4a9b7b610b13 (svn r12554) [NoAI] -Codechange: add more typing information.
rubidium
parents: 9863
diff changeset
    53
/* static */ void AIError::RegisterErrorMapString(AIErrorType ai_error_msg, const char *message)
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    54
{
9861
53fc03195e31 (svn r12544) [NoAI] -Codechange: do not force dependency on OTTD internal strings when defining errors.
rubidium
parents: 9849
diff changeset
    55
	error_map_string[ai_error_msg] = message;
9844
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    56
}
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    57
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    58
/* static */ AIError::ErrorCategories AIError::GetErrorCategory() {
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    59
	return (AIError::ErrorCategories)(GetLastError() >> (uint)ERR_CAT_BIT_SIZE);
738b8f69675f (svn r12510) [NoAI] -Add: added AIError, which allows you to catch errors triggered by commands (Morloth)
truebrain
parents:
diff changeset
    60
}