OpenTTD Source 20241224-master-gf74b0cf984
group_cmd.cpp File Reference

Handling of the engine groups. More...

#include "stdafx.h"
#include "command_func.h"
#include "train.h"
#include "vehiclelist.h"
#include "vehicle_func.h"
#include "autoreplace_base.h"
#include "autoreplace_func.h"
#include "string_func.h"
#include "company_func.h"
#include "core/pool_func.hpp"
#include "order_backup.h"
#include "group_cmd.h"
#include "table/strings.h"
#include "safeguards.h"

Go to the source code of this file.

Functions

static void UpdateNumEngineGroup (const Vehicle *v, GroupID old_g, GroupID new_g)
 Update the num engines of a groupID.
 
const LiveryGetParentLivery (const Group *g)
 
static void PropagateChildLivery (const Group *g, bool reset_cache)
 Propagate a livery change to a group's children, and optionally update cached vehicle colourmaps.
 
void UpdateCompanyGroupLiveries (const Company *c)
 Update group liveries for a company.
 
std::tuple< CommandCost, GroupIDCmdCreateGroup (DoCommandFlag flags, VehicleType vt, GroupID parent_group)
 Create a new vehicle group.
 
CommandCost CmdDeleteGroup (DoCommandFlag flags, GroupID group_id)
 Add all vehicles in the given group to the default group and then deletes the group.
 
CommandCost CmdAlterGroup (DoCommandFlag flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text)
 Alter a group.
 
static void AddVehicleToGroup (Vehicle *v, GroupID new_g)
 Do add a vehicle to a group.
 
std::tuple< CommandCost, GroupIDCmdAddVehicleGroup (DoCommandFlag flags, GroupID group_id, VehicleID veh_id, bool add_shared, const VehicleListIdentifier &vli)
 Add a vehicle to a group.
 
CommandCost CmdAddSharedVehicleGroup (DoCommandFlag flags, GroupID id_g, VehicleType type)
 Add all shared vehicles of all vehicles from a group.
 
CommandCost CmdRemoveAllVehiclesGroup (DoCommandFlag flags, GroupID group_id)
 Remove all vehicles from a group.
 
CommandCost CmdSetGroupLivery (DoCommandFlag flags, GroupID group_id, bool primary, Colours colour)
 Set the livery for a vehicle group.
 
static void SetGroupFlag (Group *g, GroupFlags flag, bool set, bool children)
 Set group flag for a group and its sub-groups.
 
CommandCost CmdSetGroupFlag (DoCommandFlag flags, GroupID group_id, GroupFlags flag, bool value, bool recursive)
 (Un)set group flag from a group
 
void SetTrainGroupID (Train *v, GroupID new_g)
 Affect the groupID of a train to new_g.
 
void UpdateTrainGroupID (Train *v)
 Recalculates the groupID of a train.
 
uint GetGroupNumEngines (CompanyID company, GroupID id_g, EngineID id_e)
 Get the number of engines with EngineID id_e in the group with GroupID id_g and its sub-groups.
 
uint GetGroupNumVehicle (CompanyID company, GroupID id_g, VehicleType type)
 Get the number of vehicles in the group with GroupID id_g and its sub-groups.
 
uint GetGroupNumVehicleMinAge (CompanyID company, GroupID id_g, VehicleType type)
 Get the number of vehicles above profit minimum age in the group with GroupID id_g and its sub-groups.
 
Money GetGroupProfitLastYearMinAge (CompanyID company, GroupID id_g, VehicleType type)
 Get last year's profit of vehicles above minimum age for the group with GroupID id_g and its sub-groups.
 
void RemoveAllGroupsForCompany (const CompanyID company)
 
bool GroupIsInGroup (GroupID search, GroupID group)
 Test if GroupID group is a descendant of (or is) GroupID search.
 

Variables

GroupPool _group_pool ("Group")
 

Detailed Description

Handling of the engine groups.

Definition in file group_cmd.cpp.

Function Documentation

◆ AddVehicleToGroup()

static void AddVehicleToGroup ( Vehicle v,
GroupID  new_g 
)
static

◆ CmdAddSharedVehicleGroup()

CommandCost CmdAddSharedVehicleGroup ( DoCommandFlag  flags,
GroupID  id_g,
VehicleType  type 
)

◆ CmdAddVehicleGroup()

◆ CmdAlterGroup()

◆ CmdCreateGroup()

◆ CmdDeleteGroup()

◆ CmdRemoveAllVehiclesGroup()

◆ CmdSetGroupFlag()

CommandCost CmdSetGroupFlag ( DoCommandFlag  flags,
GroupID  group_id,
GroupFlags  flag,
bool  value,
bool  recursive 
)

(Un)set group flag from a group

Parameters
flagstype of operation
group_idindex of group array
flagflag to set, by value not bit.
valuevalue to set the flag to.
recursiveto apply to sub-groups.
Returns
the cost of this operation or an error

Definition at line 722 of file group_cmd.cpp.

References _current_company, CMD_ERROR, DC_EXEC, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), GetWindowClassForVehicleType(), InvalidateWindowData(), Group::owner, VehicleListIdentifier::Pack(), SetGroupFlag(), SetWindowDirty(), Group::vehicle_type, and WC_REPLACE_VEHICLE.

◆ CmdSetGroupLivery()

CommandCost CmdSetGroupLivery ( DoCommandFlag  flags,
GroupID  group_id,
bool  primary,
Colours  colour 
)

Set the livery for a vehicle group.

Parameters
flagsCommand flags.
group_idGroup ID.
primarySet primary instead of secondary colour
colourColour.

Definition at line 667 of file group_cmd.cpp.

References _current_company, AssignBit(), CMD_ERROR, Livery::colour1, Livery::colour2, DC_EXEC, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), Livery::in_use, Group::livery, MarkWholeScreenDirty(), Group::owner, and PropagateChildLivery().

◆ GetGroupNumEngines()

uint GetGroupNumEngines ( CompanyID  company,
GroupID  id_g,
EngineID  id_e 
)

◆ GetGroupNumVehicle()

uint GetGroupNumVehicle ( CompanyID  company,
GroupID  id_g,
VehicleType  type 
)

Get the number of vehicles in the group with GroupID id_g and its sub-groups.

Parameters
companyThe company the group belongs to
id_gThe GroupID of the group used
typeThe vehicle type of the group
Returns
The number of vehicles in the group

Definition at line 817 of file group_cmd.cpp.

References GroupStatistics::Get(), GetGroupNumVehicle(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::Iterate(), GroupStatistics::num_vehicle, and Group::parent.

Referenced by VehicleGroupWindow::ComputeGroupInfoSize(), VehicleGroupWindow::DrawGroupInfo(), GetGroupNumVehicle(), and VehicleGroupWindow::SetStringParameters().

◆ GetGroupNumVehicleMinAge()

uint GetGroupNumVehicleMinAge ( CompanyID  company,
GroupID  id_g,
VehicleType  type 
)

Get the number of vehicles above profit minimum age in the group with GroupID id_g and its sub-groups.

Parameters
companyThe company the group belongs to
id_gThe GroupID of the group used
typeThe vehicle type of the group
Returns
The number of vehicles above profit minimum age in the group

Definition at line 834 of file group_cmd.cpp.

References GroupStatistics::Get(), GetGroupNumVehicleMinAge(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::Iterate(), GroupStatistics::num_vehicle_min_age, and Group::parent.

Referenced by VehicleGroupWindow::DrawGroupInfo(), and GetGroupNumVehicleMinAge().

◆ GetGroupProfitLastYearMinAge()

Money GetGroupProfitLastYearMinAge ( CompanyID  company,
GroupID  id_g,
VehicleType  type 
)

Get last year's profit of vehicles above minimum age for the group with GroupID id_g and its sub-groups.

Parameters
companyThe company the group belongs to
id_gThe GroupID of the group used
typeThe vehicle type of the group
Returns
Last year's profit of vehicles above minimum age for the group

Definition at line 851 of file group_cmd.cpp.

References GroupStatistics::Get(), GetGroupProfitLastYearMinAge(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::Iterate(), Group::parent, and GroupStatistics::profit_last_year_min_age.

Referenced by VehicleGroupWindow::DrawGroupInfo(), and GetGroupProfitLastYearMinAge().

◆ GetParentLivery()

const Livery * GetParentLivery ( const Group g)

Definition at line 266 of file group_cmd.cpp.

◆ GroupIsInGroup()

bool GroupIsInGroup ( GroupID  search,
GroupID  group 
)

Test if GroupID group is a descendant of (or is) GroupID search.

Parameters
searchThe GroupID to search in
groupThe GroupID to search for
Returns
True iff group is search or a descendant of search

Definition at line 874 of file group_cmd.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::Get(), INVALID_GROUP, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::IsValidID(), and Group::parent.

Referenced by CmdAlterGroup(), GenerateVehicleSortList(), and GetEngineReplacement().

◆ PropagateChildLivery()

static void PropagateChildLivery ( const Group g,
bool  reset_cache 
)
static

◆ RemoveAllGroupsForCompany()

void RemoveAllGroupsForCompany ( const CompanyID  company)

Definition at line 860 of file group_cmd.cpp.

◆ SetGroupFlag()

static void SetGroupFlag ( Group g,
GroupFlags  flag,
bool  set,
bool  children 
)
static

◆ SetTrainGroupID()

void SetTrainGroupID ( Train v,
GroupID  new_g 
)

Affect the groupID of a train to new_g.

Note
called in CmdAddVehicleGroup and CmdMoveRailVehicle
Parameters
vFirst vehicle of the chain.
new_gindex of array group

Definition at line 745 of file group_cmd.cpp.

References Vehicle::IsFrontEngine(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::IsValidID(), SpecializedVehicle< T, Type >::Next(), Vehicle::owner, SetWindowDirty(), GroupStatistics::UpdateAutoreplace(), UpdateNumEngineGroup(), VEH_TRAIN, and WC_REPLACE_VEHICLE.

Referenced by AddVehicleToGroup(), and CmdMoveRailVehicle().

◆ UpdateCompanyGroupLiveries()

void UpdateCompanyGroupLiveries ( const Company c)

◆ UpdateNumEngineGroup()

static void UpdateNumEngineGroup ( const Vehicle v,
GroupID  old_g,
GroupID  new_g 
)
inlinestatic

Update the num engines of a groupID.

Decrease the old one and increase the new one

Note
called in SetTrainGroupID and UpdateTrainGroupID
Parameters
vVehicle we have to update
old_gindex of the old group
new_gindex of the new group

Definition at line 254 of file group_cmd.cpp.

References Vehicle::engine_type, GroupStatistics::Get(), GroupStatistics::num_engines, Vehicle::owner, and BaseVehicle::type.

Referenced by AddVehicleToGroup(), SetTrainGroupID(), and UpdateTrainGroupID().

◆ UpdateTrainGroupID()

void UpdateTrainGroupID ( Train v)

Recalculates the groupID of a train.

Should be called each time a vehicle is added to/removed from the chain,.

Note
this needs to be called too for 'wagon chains' (in the depot, without an engine)
Called in CmdBuildRailVehicle, CmdBuildRailWagon, CmdMoveRailVehicle, CmdSellRailWagon
Parameters
vFirst vehicle of the chain.

Definition at line 773 of file group_cmd.cpp.

References DEFAULT_GROUP, Vehicle::group_id, GroundVehicle< T, Type >::IsFreeWagon(), Vehicle::IsFrontEngine(), SpecializedVehicle< T, Type >::Next(), Vehicle::owner, SetWindowDirty(), GroupStatistics::UpdateAutoreplace(), UpdateNumEngineGroup(), VEH_TRAIN, and WC_REPLACE_VEHICLE.

Referenced by CmdBuildRailVehicle(), CmdBuildRailWagon(), and NormaliseTrainHead().