summaryrefslogtreecommitdiff
path: root/src/ai/api/ai_sign.cpp
blob: 4683e2d32283a47fff15f95a49764dd13330c884 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
/* $Id$ */

/** @file ai_sign.cpp Implementation of AISign. */

#include "ai_sign.hpp"
#include "table/strings.h"
#include "../ai_instance.hpp"
#include "../../openttd.h"
#include "../../command_func.h"
#include "../../core/alloc_func.hpp"
#include "../../signs_base.h"
#include "../../string_func.h"
#include "../../strings_func.h"
#include "../../tile_map.h"
#include "../../company_func.h"

/* static */ SignID AISign::GetMaxSignID()
{
	return ::GetMaxSignIndex();
}

/* static */ bool AISign::IsValidSign(SignID sign_id)
{
	return ::IsValidSignID(sign_id) && ::GetSign(sign_id)->owner == _current_company;
}

/* static */ bool AISign::SetName(SignID sign_id, const char *name)
{
	EnforcePrecondition(false, IsValidSign(sign_id));
	EnforcePrecondition(false, !::StrEmpty(name));
	EnforcePreconditionCustomError(false, ::strlen(name) < MAX_LENGTH_SIGN_NAME_BYTES, AIError::ERR_PRECONDITION_STRING_TOO_LONG);

	return AIObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, name);
}

/* static */ char *AISign::GetName(SignID sign_id)
{
	if (!IsValidSign(sign_id)) return NULL;

	static const int len = 64;
	char *sign_name = MallocT<char>(len);

	::SetDParam(0, sign_id);
	::GetString(sign_name, STR_SIGN_NAME, &sign_name[len - 1]);

	return sign_name;
}

/* static */ TileIndex AISign::GetLocation(SignID sign_id)
{
	if (!IsValidSign(sign_id)) return INVALID_TILE;

	const Sign *sign = ::GetSign(sign_id);
	return ::TileVirtXY(sign->x, sign->y);
}

/* static */ bool AISign::RemoveSign(SignID sign_id)
{
	EnforcePrecondition(false, IsValidSign(sign_id));
	return AIObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, "");
}

/* static */ SignID AISign::BuildSign(TileIndex location, const char *text)
{
	EnforcePrecondition(INVALID_SIGN, ::IsValidTile(location));
	EnforcePrecondition(INVALID_SIGN, !::StrEmpty(text));
	EnforcePreconditionCustomError(false, ::strlen(text) < MAX_LENGTH_SIGN_NAME_BYTES, AIError::ERR_PRECONDITION_STRING_TOO_LONG);

	if (!AIObject::DoCommand(location, 0, 0, CMD_PLACE_SIGN, text, &AIInstance::DoCommandReturnSignID)) return INVALID_SIGN;

	/* In case of test-mode, we return SignID 0 */
	return 0;
}