Remove costly recalculation of a date format we already have.
[openttd-joker.git] / src / script / api / script_sign.cpp
blob218a791634cb841c4a2a74e696cbf8a014f985cd
1 /* $Id: script_sign.cpp 25488 2013-06-27 19:57:41Z rubidium $ */
3 /*
4 * This file is part of OpenTTD.
5 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
6 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
7 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <http://www.gnu.org/licenses/>.
8 */
10 /** @file script_sign.cpp Implementation of ScriptSign. */
12 #include "../../stdafx.h"
13 #include "script_sign.hpp"
14 #include "table/strings.h"
15 #include "../script_instance.hpp"
16 #include "../../signs_base.h"
17 #include "../../string_func.h"
18 #include "../../strings_func.h"
19 #include "../../tile_map.h"
21 #include "../../safeguards.h"
23 /* static */ bool ScriptSign::IsValidSign(SignID sign_id)
25 const Sign *si = ::Sign::GetIfValid(sign_id);
26 return si != NULL && (si->owner == ScriptObject::GetCompany() || si->owner == OWNER_DEITY);
29 /* static */ ScriptCompany::CompanyID ScriptSign::GetOwner(SignID sign_id)
31 if (!IsValidSign(sign_id)) return ScriptCompany::COMPANY_INVALID;
33 return static_cast<ScriptCompany::CompanyID>((int)::Sign::Get(sign_id)->owner);
36 /* static */ bool ScriptSign::SetName(SignID sign_id, Text *name)
38 CCountedPtr<Text> counter(name);
40 EnforcePrecondition(false, IsValidSign(sign_id));
41 EnforcePrecondition(false, name != NULL);
42 const char *text = name->GetDecodedText();
43 EnforcePreconditionEncodedText(false, text);
44 EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_SIGN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
46 return ScriptObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, text);
49 /* static */ char *ScriptSign::GetName(SignID sign_id)
51 if (!IsValidSign(sign_id)) return NULL;
53 ::SetDParam(0, sign_id);
54 return GetString(STR_SIGN_NAME);
57 /* static */ TileIndex ScriptSign::GetLocation(SignID sign_id)
59 if (!IsValidSign(sign_id)) return INVALID_TILE;
61 const Sign *sign = ::Sign::Get(sign_id);
62 return ::TileVirtXY(sign->x, sign->y);
65 /* static */ bool ScriptSign::RemoveSign(SignID sign_id)
67 EnforcePrecondition(false, IsValidSign(sign_id));
68 return ScriptObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, "");
71 /* static */ SignID ScriptSign::BuildSign(TileIndex location, Text *name)
73 CCountedPtr<Text> counter(name);
75 EnforcePrecondition(INVALID_SIGN, ::IsValidTile(location));
76 EnforcePrecondition(INVALID_SIGN, name != NULL);
77 const char *text = name->GetDecodedText();
78 EnforcePreconditionEncodedText(INVALID_SIGN, text);
79 EnforcePreconditionCustomError(INVALID_SIGN, ::Utf8StringLength(text) < MAX_LENGTH_SIGN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
81 if (!ScriptObject::DoCommand(location, 0, 0, CMD_PLACE_SIGN, text, &ScriptInstance::DoCommandReturnSignID)) return INVALID_SIGN;
83 /* In case of test-mode, we return SignID 0 */
84 return 0;