script_sign.cpp

Go to the documentation of this file.
00001 /* $Id: script_sign.cpp 23636 2011-12-19 21:06:06Z truebrain $ */
00002 
00003 /*
00004  * This file is part of OpenTTD.
00005  * 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.
00006  * 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.
00007  * 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/>.
00008  */
00009 
00012 #include "../../stdafx.h"
00013 #include "script_sign.hpp"
00014 #include "table/strings.h"
00015 #include "../script_instance.hpp"
00016 #include "../../command_func.h"
00017 #include "../../core/alloc_func.hpp"
00018 #include "../../signs_base.h"
00019 #include "../../string_func.h"
00020 #include "../../strings_func.h"
00021 #include "../../tile_map.h"
00022 
00023 /* static */ bool ScriptSign::IsValidSign(SignID sign_id)
00024 {
00025   const Sign *si = ::Sign::GetIfValid(sign_id);
00026   return si != NULL && (si->owner == ScriptObject::GetCompany() || si->owner == OWNER_DEITY);
00027 }
00028 
00029 /* static */ ScriptCompany::CompanyID ScriptSign::GetOwner(SignID sign_id)
00030 {
00031   if (!IsValidSign(sign_id)) return ScriptCompany::COMPANY_INVALID;
00032 
00033   return static_cast<ScriptCompany::CompanyID>((int)::Sign::Get(sign_id)->owner);
00034 }
00035 
00036 /* static */ bool ScriptSign::SetName(SignID sign_id, Text *name)
00037 {
00038   CCountedPtr<Text> counter(name);
00039 
00040   EnforcePrecondition(false, IsValidSign(sign_id));
00041   EnforcePrecondition(false, name != NULL);
00042   const char *text = name->GetEncodedText();
00043   EnforcePrecondition(false, !::StrEmpty(text));
00044   EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_SIGN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
00045 
00046   return ScriptObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, text);
00047 }
00048 
00049 /* static */ char *ScriptSign::GetName(SignID sign_id)
00050 {
00051   if (!IsValidSign(sign_id)) return NULL;
00052 
00053   static const int len = 64;
00054   char *sign_name = MallocT<char>(len);
00055 
00056 	::SetDParam(0, sign_id);
00057   ::GetString(sign_name, STR_SIGN_NAME, &sign_name[len - 1]);
00058 
00059   return sign_name;
00060 }
00061 
00062 /* static */ TileIndex ScriptSign::GetLocation(SignID sign_id)
00063 {
00064   if (!IsValidSign(sign_id)) return INVALID_TILE;
00065 
00066   const Sign *sign = ::Sign::Get(sign_id);
00067   return ::TileVirtXY(sign->x, sign->y);
00068 }
00069 
00070 /* static */ bool ScriptSign::RemoveSign(SignID sign_id)
00071 {
00072   EnforcePrecondition(false, IsValidSign(sign_id));
00073   return ScriptObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, "");
00074 }
00075 
00076 /* static */ SignID ScriptSign::BuildSign(TileIndex location, Text *name)
00077 {
00078   CCountedPtr<Text> counter(name);
00079 
00080   EnforcePrecondition(INVALID_SIGN, ::IsValidTile(location));
00081   EnforcePrecondition(INVALID_SIGN, name != NULL);
00082   const char *text = name->GetEncodedText();
00083   EnforcePrecondition(INVALID_SIGN, !::StrEmpty(text));
00084   EnforcePreconditionCustomError(INVALID_SIGN, ::Utf8StringLength(text) < MAX_LENGTH_SIGN_NAME_CHARS, ScriptError::ERR_PRECONDITION_STRING_TOO_LONG);
00085 
00086   if (!ScriptObject::DoCommand(location, 0, 0, CMD_PLACE_SIGN, text, &ScriptInstance::DoCommandReturnSignID)) return INVALID_SIGN;
00087 
00088   /* In case of test-mode, we return SignID 0 */
00089   return 0;
00090 }