# ai_group.cpp

Go to the documentation of this file.

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