ai_sign.cpp

Go to the documentation of this file.
00001 /* $Id$ */
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 "ai_sign.hpp"
00014 #include "table/strings.h"
00015 #include "../ai_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 #include "../../company_func.h"
00023 
00024 /* static */ bool AISign::IsValidSign(SignID sign_id)
00025 {
00026   const Sign *si = ::Sign::GetIfValid(sign_id);
00027   return si != NULL && si->owner == _current_company;
00028 }
00029 
00030 /* static */ bool AISign::SetName(SignID sign_id, const char *name)
00031 {
00032   EnforcePrecondition(false, IsValidSign(sign_id));
00033   EnforcePrecondition(false, !::StrEmpty(name));
00034   EnforcePreconditionCustomError(false, ::Utf8StringLength(name) < MAX_LENGTH_SIGN_NAME_CHARS, AIError::ERR_PRECONDITION_STRING_TOO_LONG);
00035 
00036   return AIObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, name);
00037 }
00038 
00039 /* static */ char *AISign::GetName(SignID sign_id)
00040 {
00041   if (!IsValidSign(sign_id)) return NULL;
00042 
00043   static const int len = 64;
00044   char *sign_name = MallocT<char>(len);
00045 
00046 	::SetDParam(0, sign_id);
00047   ::GetString(sign_name, STR_SIGN_NAME, &sign_name[len - 1]);
00048 
00049   return sign_name;
00050 }
00051 
00052 /* static */ TileIndex AISign::GetLocation(SignID sign_id)
00053 {
00054   if (!IsValidSign(sign_id)) return INVALID_TILE;
00055 
00056   const Sign *sign = ::Sign::Get(sign_id);
00057   return ::TileVirtXY(sign->x, sign->y);
00058 }
00059 
00060 /* static */ bool AISign::RemoveSign(SignID sign_id)
00061 {
00062   EnforcePrecondition(false, IsValidSign(sign_id));
00063   return AIObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, "");
00064 }
00065 
00066 /* static */ SignID AISign::BuildSign(TileIndex location, const char *text)
00067 {
00068   EnforcePrecondition(INVALID_SIGN, ::IsValidTile(location));
00069   EnforcePrecondition(INVALID_SIGN, !::StrEmpty(text));
00070   EnforcePreconditionCustomError(false, ::Utf8StringLength(text) < MAX_LENGTH_SIGN_NAME_CHARS, AIError::ERR_PRECONDITION_STRING_TOO_LONG);
00071 
00072   if (!AIObject::DoCommand(location, 0, 0, CMD_PLACE_SIGN, text, &AIInstance::DoCommandReturnSignID)) return INVALID_SIGN;
00073 
00074   /* In case of test-mode, we return SignID 0 */
00075   return 0;
00076 }

Generated on Wed Apr 13 00:47:45 2011 for OpenTTD by  doxygen 1.6.1