From a3dd7506d377b1434f913bd65c019eed52b64b6e Mon Sep 17 00:00:00 2001 From: truebrain Date: Mon, 12 Jan 2009 17:11:45 +0000 Subject: (svn r15027) -Merge: tomatos and bananas left to be, here is NoAI for all to see. NoAI is an API (a framework) to build your own AIs in. See: http://wiki.openttd.org/wiki/index.php/AI:Main_Page With many thanks to: - glx and Rubidium for their syncing, feedback and hard work - Yexo for his feedback, patches, and AIs which tested the system very deep - Morloth for his feedback and patches - TJIP for hosting a challenge which kept NoAI on track - All AI authors for testing our AI API, and all other people who helped in one way or another -Remove: all old AIs and their cheats/hacks --- src/ai/api/ai_sign.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 src/ai/api/ai_sign.cpp (limited to 'src/ai/api/ai_sign.cpp') diff --git a/src/ai/api/ai_sign.cpp b/src/ai/api/ai_sign.cpp new file mode 100644 index 000000000..561b5c857 --- /dev/null +++ b/src/ai/api/ai_sign.cpp @@ -0,0 +1,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 */ const char *AISign::GetName(SignID sign_id) +{ + if (!IsValidSign(sign_id)) return NULL; + + static const int len = 64; + char *sign_name = MallocT(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; +} -- cgit v1.2.3-54-g00ecf