ai_sign.cpp

Go to the documentation of this file.
00001 /* $Id: ai_sign.cpp 21413 2010-12-05 22:25:08Z rubidium $ */
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 "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 /* static */ 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 /* static */ 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 /* static */ 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 /* static */ 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 /* static */ 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 /* static */ 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   /* In case of test-mode, we return SignID 0 */
00074   return 0;
00075 }

Generated on Sun Jan 9 16:01:51 2011 for OpenTTD by  doxygen 1.6.1