src/ai/api/ai_sign.cpp
author truebrain
Fri, 13 Jun 2008 13:07:31 +0000
branchnoai
changeset 10952 d16212cc2394
parent 10855 90904faa1890
permissions -rw-r--r--
(svn r13506) [NoAI] -Change [API CHANGE]: like AIVehicle, AIStation, .. restrict AISign to only your own signs
[NoAI] -Remove [API CHANGE]: the change above makes AISign::GetOwner() obsolete, and is therefor removed
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
     1
/* $Id$ */
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
     2
9833
89a64246458f (svn r12496) [NoAI] -Documentation: give the .cpp files a nice uniform format too
truebrain
parents: 9800
diff changeset
     3
/** @file ai_sign.cpp Implementation of AISign. */
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
     4
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
     5
#include "ai_sign.hpp"
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
     6
#include "table/strings.h"
10339
ce6cd68d9eb8 (svn r12880) [NoAI] -Add: introduces ai_types.hpp, which has all NNNId like VehicleID. This simplifies the include-mess, and avoids including tons of _type.h for just a single typedef.
truebrain
parents: 10090
diff changeset
     7
#include "../../openttd.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9599
diff changeset
     8
#include "../../command_func.h"
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9599
diff changeset
     9
#include "../../core/alloc_func.hpp"
9837
c9ec4f82e0d0 (svn r12503) [NoAI] -Sync: with trunk r12461:12501.
rubidium
parents: 9833
diff changeset
    10
#include "../../signs_base.h"
10090
d6a6cac2fb25 (svn r12614) [NoAI] -Add: support for GetLastError in AISign. Patch by Morloth.
rubidium
parents: 9837
diff changeset
    11
#include "../../string_func.h"
9723
eee46cb39750 (svn r11796) [NoAI] -Sync: with trunk r11502:11795.
rubidium
parents: 9599
diff changeset
    12
#include "../../strings_func.h"
9800
ab08ca2a2018 (svn r12306) [NoAI] -Sync with trunk r12209:12304
glx
parents: 9737
diff changeset
    13
#include "../../tile_map.h"
10952
d16212cc2394 (svn r13506) [NoAI] -Change [API CHANGE]: like AIVehicle, AIStation, .. restrict AISign to only your own signs
truebrain
parents: 10855
diff changeset
    14
#include "../../player_func.h"
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    15
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    16
/* static */ SignID AISign::GetMaxSignID()
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    17
{
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    18
	return ::GetMaxSignIndex();
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    19
}
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    20
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    21
/* static */ bool AISign::IsValidSign(SignID sign_id)
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    22
{
10952
d16212cc2394 (svn r13506) [NoAI] -Change [API CHANGE]: like AIVehicle, AIStation, .. restrict AISign to only your own signs
truebrain
parents: 10855
diff changeset
    23
	return ::IsValidSignID(sign_id) && ::GetSign(sign_id)->owner == _current_player;
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    24
}
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    25
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    26
/* static */ char *AISign::GetText(SignID sign_id)
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    27
{
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9724
diff changeset
    28
	if (!IsValidSign(sign_id)) return NULL;
10952
d16212cc2394 (svn r13506) [NoAI] -Change [API CHANGE]: like AIVehicle, AIStation, .. restrict AISign to only your own signs
truebrain
parents: 10855
diff changeset
    29
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    30
	static const int len = 64;
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    31
	char *sign_name = MallocT<char>(len);
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    32
9724
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    33
	::SetDParam(0, sign_id);
b39bc69bb2f2 (svn r12051) [NoAI] -Sync: with trunk (r11795:12050).
rubidium
parents: 9723
diff changeset
    34
	::GetString(sign_name, STR_SIGN_NAME, &sign_name[len - 1]);
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    35
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    36
	return sign_name;
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    37
}
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    38
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    39
/* static */ TileIndex AISign::GetLocation(SignID sign_id)
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    40
{
9736
183b38e0a480 (svn r12215) [NoAI] -Codechange: added '::' if a function comes from non-AI-API functions (was inconsistant till now)
truebrain
parents: 9724
diff changeset
    41
	if (!IsValidSign(sign_id)) return INVALID_TILE;
10952
d16212cc2394 (svn r13506) [NoAI] -Change [API CHANGE]: like AIVehicle, AIStation, .. restrict AISign to only your own signs
truebrain
parents: 10855
diff changeset
    42
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    43
	const Sign *sign = ::GetSign(sign_id);
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    44
	return ::TileVirtXY(sign->x, sign->y);
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    45
}
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    46
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    47
/* static */ bool AISign::RemoveSign(SignID sign_id)
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    48
{
10090
d6a6cac2fb25 (svn r12614) [NoAI] -Add: support for GetLastError in AISign. Patch by Morloth.
rubidium
parents: 9837
diff changeset
    49
	EnforcePrecondition(false, IsValidSign(sign_id));
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    50
	_cmd_text = "";
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    51
	return AIObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN);
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    52
}
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    53
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    54
/* static */ SignID AISign::BuildSign(TileIndex location, const char *text)
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    55
{
10090
d6a6cac2fb25 (svn r12614) [NoAI] -Add: support for GetLastError in AISign. Patch by Morloth.
rubidium
parents: 9837
diff changeset
    56
	EnforcePrecondition(INVALID_SIGN, ::IsValidTile(location));
d6a6cac2fb25 (svn r12614) [NoAI] -Add: support for GetLastError in AISign. Patch by Morloth.
rubidium
parents: 9837
diff changeset
    57
	EnforcePrecondition(INVALID_SIGN, !StrEmpty(text));
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    58
9560
ff2dde050a4c (svn r9498) [NoAI] -Add: added regression for AITransactionMode()
truelight
parents: 9511
diff changeset
    59
	/* Reset the internal NewSignID in case we are in TestMode */
ff2dde050a4c (svn r9498) [NoAI] -Add: added regression for AITransactionMode()
truelight
parents: 9511
diff changeset
    60
	AIObject::SetNewSignID(0);
ff2dde050a4c (svn r9498) [NoAI] -Add: added regression for AITransactionMode()
truelight
parents: 9511
diff changeset
    61
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    62
	bool ret = AIObject::DoCommand(location, 0, 0, CMD_PLACE_SIGN);
9561
33b2f192bb34 (svn r9499) [NoAI] -Revert r9498: reverted part of ai_sign.cpp, as by policy this is the road we take
truelight
parents: 9560
diff changeset
    63
	if (!ret) return INVALID_SIGN;
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    64
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    65
	SignID new_sign_id = AIObject::GetNewSignID();
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    66
	_cmd_text = text;
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    67
	ret = AIObject::DoCommand(0, new_sign_id, 0, CMD_RENAME_SIGN);
9561
33b2f192bb34 (svn r9499) [NoAI] -Revert r9498: reverted part of ai_sign.cpp, as by policy this is the road we take
truelight
parents: 9560
diff changeset
    68
	if (!ret) {
9737
ee408edf3851 (svn r12216) [NoAI] -Codechange: made most functions 'static', which removes the need to create an instance to get, for example, engine information, and therefor heavily simplifying AI creation (Morloth)
truebrain
parents: 9736
diff changeset
    69
		RemoveSign(new_sign_id);
9511
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    70
		return INVALID_SIGN;
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    71
	}
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    72
	return new_sign_id;
f767ad06e86b (svn r9407) [NoAI] -Add: placing of signs.
rubidium
parents:
diff changeset
    73
}