ai_sign.cpp

Go to the documentation of this file.
00001 /* $Id: ai_sign.cpp 15299 2009-01-31 20:16:06Z smatz $ */
00002 
00005 #include "ai_sign.hpp"
00006 #include "table/strings.h"
00007 #include "../ai_instance.hpp"
00008 #include "../../command_func.h"
00009 #include "../../core/alloc_func.hpp"
00010 #include "../../signs_base.h"
00011 #include "../../string_func.h"
00012 #include "../../strings_func.h"
00013 #include "../../tile_map.h"
00014 #include "../../company_func.h"
00015 
00016 /* static */ SignID AISign::GetMaxSignID()
00017 {
00018   return ::GetMaxSignIndex();
00019 }
00020 
00021 /* static */ bool AISign::IsValidSign(SignID sign_id)
00022 {
00023   return ::IsValidSignID(sign_id) && ::GetSign(sign_id)->owner == _current_company;
00024 }
00025 
00026 /* static */ bool AISign::SetName(SignID sign_id, const char *name)
00027 {
00028   EnforcePrecondition(false, IsValidSign(sign_id));
00029   EnforcePrecondition(false, !::StrEmpty(name));
00030   EnforcePreconditionCustomError(false, ::strlen(name) < MAX_LENGTH_SIGN_NAME_BYTES, AIError::ERR_PRECONDITION_STRING_TOO_LONG);
00031 
00032   return AIObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, name);
00033 }
00034 
00035 /* static */ char *AISign::GetName(SignID sign_id)
00036 {
00037   if (!IsValidSign(sign_id)) return NULL;
00038 
00039   static const int len = 64;
00040   char *sign_name = MallocT<char>(len);
00041 
00042   ::SetDParam(0, sign_id);
00043   ::GetString(sign_name, STR_SIGN_NAME, &sign_name[len - 1]);
00044 
00045   return sign_name;
00046 }
00047 
00048 /* static */ TileIndex AISign::GetLocation(SignID sign_id)
00049 {
00050   if (!IsValidSign(sign_id)) return INVALID_TILE;
00051 
00052   const Sign *sign = ::GetSign(sign_id);
00053   return ::TileVirtXY(sign->x, sign->y);
00054 }
00055 
00056 /* static */ bool AISign::RemoveSign(SignID sign_id)
00057 {
00058   EnforcePrecondition(false, IsValidSign(sign_id));
00059   return AIObject::DoCommand(0, sign_id, 0, CMD_RENAME_SIGN, "");
00060 }
00061 
00062 /* static */ SignID AISign::BuildSign(TileIndex location, const char *text)
00063 {
00064   EnforcePrecondition(INVALID_SIGN, ::IsValidTile(location));
00065   EnforcePrecondition(INVALID_SIGN, !::StrEmpty(text));
00066   EnforcePreconditionCustomError(false, ::strlen(text) < MAX_LENGTH_SIGN_NAME_BYTES, AIError::ERR_PRECONDITION_STRING_TOO_LONG);
00067 
00068   if (!AIObject::DoCommand(location, 0, 0, CMD_PLACE_SIGN, text, &AIInstance::DoCommandReturnSignID)) return INVALID_SIGN;
00069 
00070   /* In case of test-mode, we return SignID 0 */
00071   return 0;
00072 }

Generated on Thu Oct 1 11:03:11 2009 for OpenTTD by  doxygen 1.5.6