src/ai/api/ai_sign.cpp
branchnoai
changeset 9737 ee408edf3851
parent 9736 183b38e0a480
child 9800 ab08ca2a2018
equal deleted inserted replaced
9736:183b38e0a480 9737:ee408edf3851
     9 #include "../../landscape.h"
     9 #include "../../landscape.h"
    10 #include "../../signs.h"
    10 #include "../../signs.h"
    11 #include "../../strings_func.h"
    11 #include "../../strings_func.h"
    12 #include "../../map_func.h"
    12 #include "../../map_func.h"
    13 
    13 
    14 SignID AISign::GetMaxSignID()
    14 /* static */ SignID AISign::GetMaxSignID()
    15 {
    15 {
    16 	return ::GetMaxSignIndex();
    16 	return ::GetMaxSignIndex();
    17 }
    17 }
    18 
    18 
    19 int32 AISign::GetSignCount()
    19 /* static */ int32 AISign::GetSignCount()
    20 {
    20 {
    21 	return ::GetNumSigns();
    21 	return ::GetNumSigns();
    22 }
    22 }
    23 
    23 
    24 /* static */ bool AISign::IsValidSign(SignID sign_id)
    24 /* static */ bool AISign::IsValidSign(SignID sign_id)
    25 {
    25 {
    26 	return ::IsValidSignID(sign_id);
    26 	return ::IsValidSignID(sign_id);
    27 }
    27 }
    28 
    28 
    29 char *AISign::GetText(SignID sign_id)
    29 /* static */ char *AISign::GetText(SignID sign_id)
    30 {
    30 {
    31 	if (!IsValidSign(sign_id)) return NULL;
    31 	if (!IsValidSign(sign_id)) return NULL;
    32 	static const int len = 64;
    32 	static const int len = 64;
    33 	char *sign_name = MallocT<char>(len);
    33 	char *sign_name = MallocT<char>(len);
    34 
    34 
    36 	::GetString(sign_name, STR_SIGN_NAME, &sign_name[len - 1]);
    36 	::GetString(sign_name, STR_SIGN_NAME, &sign_name[len - 1]);
    37 
    37 
    38 	return sign_name;
    38 	return sign_name;
    39 }
    39 }
    40 
    40 
    41 TileIndex AISign::GetLocation(SignID sign_id)
    41 /* static */ TileIndex AISign::GetLocation(SignID sign_id)
    42 {
    42 {
    43 	if (!IsValidSign(sign_id)) return INVALID_TILE;
    43 	if (!IsValidSign(sign_id)) return INVALID_TILE;
    44 	const Sign *sign = ::GetSign(sign_id);
    44 	const Sign *sign = ::GetSign(sign_id);
    45 	return ::TileVirtXY(sign->x, sign->y);
    45 	return ::TileVirtXY(sign->x, sign->y);
    46 }
    46 }
    47 
    47 
    48 bool AISign::RemoveSign(SignID sign_id)
    48 /* static */ bool AISign::RemoveSign(SignID sign_id)
    49 {
    49 {
    50 	_cmd_text = "";
    50 	_cmd_text = "";
    51 	return this->DoCommand(0, sign_id, 0, CMD_RENAME_SIGN);
    51 	return AIObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN);
    52 }
    52 }
    53 
    53 
    54 SignID AISign::BuildSign(TileIndex location, const char *text)
    54 /* static */ SignID AISign::BuildSign(TileIndex location, const char *text)
    55 {
    55 {
    56 	if (!::IsValidTile(location)) return INVALID_SIGN;
    56 	if (!::IsValidTile(location)) return INVALID_SIGN;
    57 
    57 
    58 	/* Reset the internal NewSignID in case we are in TestMode */
    58 	/* Reset the internal NewSignID in case we are in TestMode */
    59 	AIObject::SetNewSignID(0);
    59 	AIObject::SetNewSignID(0);
    60 
    60 
    61 	bool ret = this->DoCommand(location, 0, 0, CMD_PLACE_SIGN);
    61 	bool ret = AIObject::DoCommand(location, 0, 0, CMD_PLACE_SIGN);
    62 	if (!ret) return INVALID_SIGN;
    62 	if (!ret) return INVALID_SIGN;
    63 
    63 
    64 	SignID new_sign_id = AIObject::GetNewSignID();
    64 	SignID new_sign_id = AIObject::GetNewSignID();
    65 	_cmd_text = text;
    65 	_cmd_text = text;
    66 	ret = this->DoCommand(0, new_sign_id, 0, CMD_RENAME_SIGN);
    66 	ret = AIObject::DoCommand(0, new_sign_id, 0, CMD_RENAME_SIGN);
    67 	if (!ret) {
    67 	if (!ret) {
    68 		this->RemoveSign(new_sign_id);
    68 		RemoveSign(new_sign_id);
    69 		return INVALID_SIGN;
    69 		return INVALID_SIGN;
    70 	}
    70 	}
    71 	return new_sign_id;
    71 	return new_sign_id;
    72 }
    72 }