2009-01-12 17:11:45 +00:00
|
|
|
/* $Id$ */
|
|
|
|
|
2009-08-21 20:21:05 +00:00
|
|
|
/*
|
|
|
|
* This file is part of OpenTTD.
|
|
|
|
* 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.
|
|
|
|
* 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.
|
|
|
|
* 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/>.
|
|
|
|
*/
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/** @file script_sign.cpp Implementation of ScriptSign. */
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-01-22 10:33:16 +00:00
|
|
|
#include "../../stdafx.h"
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "script_sign.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
#include "table/strings.h"
|
2011-11-29 23:07:38 +00:00
|
|
|
#include "../../ai/ai_instance.hpp"
|
2009-01-12 17:11:45 +00:00
|
|
|
#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"
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptSign::IsValidSign(SignID sign_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
2009-05-18 16:21:28 +00:00
|
|
|
const Sign *si = ::Sign::GetIfValid(sign_id);
|
|
|
|
return si != NULL && si->owner == _current_company;
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptSign::SetName(SignID sign_id, const char *name)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
EnforcePrecondition(false, IsValidSign(sign_id));
|
|
|
|
EnforcePrecondition(false, !::StrEmpty(name));
|
2011-11-29 23:15:35 +00:00
|
|
|
EnforcePreconditionCustomError(false, ::Utf8StringLength(name) < MAX_LENGTH_SIGN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, name);
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ char *ScriptSign::GetName(SignID sign_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ TileIndex ScriptSign::GetLocation(SignID sign_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
if (!IsValidSign(sign_id)) return INVALID_TILE;
|
|
|
|
|
2009-05-16 23:34:14 +00:00
|
|
|
const Sign *sign = ::Sign::Get(sign_id);
|
2009-01-12 17:11:45 +00:00
|
|
|
return ::TileVirtXY(sign->x, sign->y);
|
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ bool ScriptSign::RemoveSign(SignID sign_id)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
EnforcePrecondition(false, IsValidSign(sign_id));
|
2011-11-29 23:15:35 +00:00
|
|
|
return ScriptObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, "");
|
2009-01-12 17:11:45 +00:00
|
|
|
}
|
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
/* static */ SignID ScriptSign::BuildSign(TileIndex location, const char *text)
|
2009-01-12 17:11:45 +00:00
|
|
|
{
|
|
|
|
EnforcePrecondition(INVALID_SIGN, ::IsValidTile(location));
|
|
|
|
EnforcePrecondition(INVALID_SIGN, !::StrEmpty(text));
|
2011-11-29 23:15:35 +00:00
|
|
|
EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_SIGN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
|
2009-01-12 17:11:45 +00:00
|
|
|
|
2011-11-29 23:15:35 +00:00
|
|
|
if (!ScriptObject::DoCommand(location, 0, 0, CMD_PLACE_SIGN, text, &AIInstance::DoCommandReturnSignID)) return INVALID_SIGN;
|
2009-01-12 17:11:45 +00:00
|
|
|
|
|
|
|
/* In case of test-mode, we return SignID 0 */
|
|
|
|
return 0;
|
|
|
|
}
|