ai_sign.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00012 #include "ai_sign.hpp"
00013 #include "table/strings.h"
00014 #include "../ai_instance.hpp"
00015 #include "../../command_func.h"
00016 #include "../../core/alloc_func.hpp"
00017 #include "../../signs_base.h"
00018 #include "../../string_func.h"
00019 #include "../../strings_func.h"
00020 #include "../../tile_map.h"
00021 #include "../../company_func.h"
00022
00023 bool AISign::IsValidSign(SignID sign_id)
00024 {
00025 const Sign *si = ::Sign::GetIfValid(sign_id);
00026 return si != NULL && si->owner == _current_company;
00027 }
00028
00029 bool AISign::SetName(SignID sign_id, const char *name)
00030 {
00031 EnforcePrecondition(false, IsValidSign(sign_id));
00032 EnforcePrecondition(false, !::StrEmpty(name));
00033 EnforcePreconditionCustomError(false, ::Utf8StringLength(name) < MAX_LENGTH_SIGN_NAME_CHARS, AIError::ERR_PRECONDITION_STRING_TOO_LONG);
00034
00035 return AIObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, name);
00036 }
00037
00038 char *AISign::GetName(SignID sign_id)
00039 {
00040 if (!IsValidSign(sign_id)) return NULL;
00041
00042 static const int len = 64;
00043 char *sign_name = MallocT<char>(len);
00044
00045 ::SetDParam(0, sign_id);
00046 ::GetString(sign_name, STR_SIGN_NAME, &sign_name[len - 1]);
00047
00048 return sign_name;
00049 }
00050
00051 TileIndex AISign::GetLocation(SignID sign_id)
00052 {
00053 if (!IsValidSign(sign_id)) return INVALID_TILE;
00054
00055 const Sign *sign = ::Sign::Get(sign_id);
00056 return ::TileVirtXY(sign->x, sign->y);
00057 }
00058
00059 bool AISign::RemoveSign(SignID sign_id)
00060 {
00061 EnforcePrecondition(false, IsValidSign(sign_id));
00062 return AIObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, "");
00063 }
00064
00065 SignID AISign::BuildSign(TileIndex location, const char *text)
00066 {
00067 EnforcePrecondition(INVALID_SIGN, ::IsValidTile(location));
00068 EnforcePrecondition(INVALID_SIGN, !::StrEmpty(text));
00069 EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_SIGN_NAME_CHARS, AIError::ERR_PRECONDITION_STRING_TOO_LONG);
00070
00071 if (!AIObject::DoCommand(location, 0, 0, CMD_PLACE_SIGN, text, &AIInstance::DoCommandReturnSignID)) return INVALID_SIGN;
00072
00073
00074 return 0;
00075 }