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