ai_group.cpp

Go to the documentation of this file.
00001 /* $Id: ai_group.cpp 18930 2010-01-27 20:51:11Z frosch $ */
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_group.hpp"
00013 #include "ai_engine.hpp"
00014 #include "../ai_instance.hpp"
00015 #include "../../company_func.h"
00016 #include "../../group.h"
00017 #include "../../string_func.h"
00018 #include "../../strings_func.h"
00019 #include "../../autoreplace_func.h"
00020 #include "../../settings_func.h"
00021 #include "table/strings.h"
00022 
00023 /* static */ bool AIGroup::IsValidGroup(GroupID group_id)
00024 {
00025   const Group *g = ::Group::GetIfValid(group_id);
00026   return g != NULL && g->owner == _current_company;
00027 }
00028 
00029 /* static */ AIGroup::GroupID AIGroup::CreateGroup(AIVehicle::VehicleType vehicle_type)
00030 {
00031   if (!AIObject::DoCommand(0, (::VehicleType)vehicle_type, 0, CMD_CREATE_GROUP, NULL, &AIInstance::DoCommandReturnGroupID)) return GROUP_INVALID;
00032 
00033   /* In case of test-mode, we return GroupID 0 */
00034   return (AIGroup::GroupID)0;
00035 }
00036 
00037 /* static */ bool AIGroup::DeleteGroup(GroupID group_id)
00038 {
00039   EnforcePrecondition(false, IsValidGroup(group_id));
00040 
00041   return AIObject::DoCommand(0, group_id, 0, CMD_DELETE_GROUP);
00042 }
00043 
00044 /* static */ AIVehicle::VehicleType AIGroup::GetVehicleType(GroupID group_id)
00045 {
00046   if (!IsValidGroup(group_id)) return AIVehicle::VT_INVALID;
00047 
00048   return (AIVehicle::VehicleType)((::VehicleType)::Group::Get(group_id)->vehicle_type);
00049 }
00050 
00051 /* static */ bool AIGroup::SetName(GroupID group_id, const char *name)
00052 {
00053   EnforcePrecondition(false, IsValidGroup(group_id));
00054   EnforcePrecondition(false, !::StrEmpty(name));
00055   EnforcePreconditionCustomError(false, ::strlen(name) < MAX_LENGTH_GROUP_NAME_BYTES, AIError::ERR_PRECONDITION_STRING_TOO_LONG);
00056 
00057   return AIObject::DoCommand(0, group_id, 0, CMD_RENAME_GROUP, name);
00058 }
00059 
00060 /* static */ char *AIGroup::GetName(GroupID group_id)
00061 {
00062   if (!IsValidGroup(group_id)) return NULL;
00063 
00064   static const int len = 64;
00065   char *group_name = MallocT<char>(len);
00066 
00067 	::SetDParam(0, group_id);
00068   ::GetString(group_name, STR_GROUP_NAME, &group_name[len - 1]);
00069   return group_name;
00070 }
00071 
00072 /* static */ bool AIGroup::EnableAutoReplaceProtection(GroupID group_id, bool enable)
00073 {
00074   EnforcePrecondition(false, IsValidGroup(group_id));
00075 
00076   return AIObject::DoCommand(0, group_id, enable ? 1 : 0, CMD_SET_GROUP_REPLACE_PROTECTION);
00077 }
00078 
00079 /* static */ bool AIGroup::GetAutoReplaceProtection(GroupID group_id)
00080 {
00081   if (!IsValidGroup(group_id)) return false;
00082 
00083   return ::Group::Get(group_id)->replace_protection;
00084 }
00085 
00086 /* static */ int32 AIGroup::GetNumEngines(GroupID group_id, EngineID engine_id)
00087 {
00088   if (!IsValidGroup(group_id) && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return -1;
00089 
00090   return GetGroupNumEngines(_current_company, group_id, engine_id);
00091 }
00092 
00093 /* static */ bool AIGroup::MoveVehicle(GroupID group_id, VehicleID vehicle_id)
00094 {
00095   EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT);
00096   EnforcePrecondition(false, AIVehicle::IsValidVehicle(vehicle_id));
00097 
00098   return AIObject::DoCommand(0, group_id, vehicle_id, CMD_ADD_VEHICLE_GROUP);
00099 }
00100 
00101 /* static */ bool AIGroup::EnableWagonRemoval(bool enable_removal)
00102 {
00103   if (HasWagonRemoval() == enable_removal) return true;
00104 
00105   return AIObject::DoCommand(0, ::GetCompanySettingIndex("company.renew_keep_length"), enable_removal ? 1 : 0, CMD_CHANGE_COMPANY_SETTING);
00106 }
00107 
00108 /* static */ bool AIGroup::HasWagonRemoval()
00109 {
00110   return ::Company::Get(_current_company)->settings.renew_keep_length;
00111 }
00112 
00113 /* static */ bool AIGroup::SetAutoReplace(GroupID group_id, EngineID engine_id_old, EngineID engine_id_new)
00114 {
00115   EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL);
00116   EnforcePrecondition(false, AIEngine::IsBuildable(engine_id_new));
00117 
00118   return AIObject::DoCommand(0, group_id << 16, (engine_id_new << 16) | engine_id_old, CMD_SET_AUTOREPLACE);
00119 }
00120 
00121 /* static */ EngineID AIGroup::GetEngineReplacement(GroupID group_id, EngineID engine_id)
00122 {
00123   if (!IsValidGroup(group_id) && group_id != GROUP_DEFAULT && group_id != GROUP_ALL) return ::INVALID_ENGINE;
00124 
00125   return ::EngineReplacementForCompany(Company::Get(_current_company), engine_id, group_id);
00126 }
00127 
00128 /* static */ bool AIGroup::StopAutoReplace(GroupID group_id, EngineID engine_id)
00129 {
00130   EnforcePrecondition(false, IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL);
00131 
00132   return AIObject::DoCommand(0, group_id << 16, (::INVALID_ENGINE << 16) | engine_id, CMD_SET_AUTOREPLACE);
00133 }

Generated on Tue Sep 14 17:06:46 2010 for OpenTTD by  doxygen 1.6.1