# group_cmd.cpp File Reference

Handling of the engine groups. More...

`#include "stdafx.h"`
`#include "cmd_helper.h"`
`#include "command_func.h"`
`#include "group.h"`
`#include "train.h"`
`#include "engine_base.h"`
`#include "vehicle_gui.h"`
`#include "window_func.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 "table/strings.h"`

Go to the source code of this file.

## Functions
static void | UpdateNumEngineGroup (EngineID i, GroupID old_g, GroupID new_g)
| Update the num engines of a groupID.
void | InitializeGroup ()
CommandCost | CmdCreateGroup (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Create a new vehicle group.
CommandCost | CmdDeleteGroup (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Add all vehicles in the given group to the default group and then deletes the group.
static bool | IsUniqueGroupName (const char *name)
CommandCost | CmdRenameGroup (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Rename a group.
CommandCost | CmdAddVehicleGroup (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Add a vehicle to a group.
CommandCost | CmdAddSharedVehicleGroup (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Add all shared vehicles of all vehicles from a group.
CommandCost | CmdRemoveAllVehiclesGroup (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Remove all vehicles from a group.
CommandCost | CmdSetGroupReplaceProtection (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| (Un)set global replace protection from a group
void | RemoveVehicleFromGroup (const Vehicle *v)
| Decrease the num_vehicle variable before delete an front engine 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.
void | RemoveAllGroupsForCompany (const CompanyID company)
## Variables
GroupID | _new_group_id
GroupPool | _group_pool ("Group")

## Detailed Description

Handling of the engine groups.

Definition in file group_cmd.cpp.

## Function Documentation

CommandCost CmdAddSharedVehicleGroup | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Add all shared vehicles of all vehicles from a group.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | index of group array p1 bit 0-15 : GroupID
  | p2 | type of vehicles
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 260 of file group_cmd.cpp.

References CMD_ERROR, CmdAddVehicleGroup(), DC_EXEC, Vehicle::FirstShared(), GetWindowClassForVehicleType(), Vehicle::group_id, InvalidateWindowData(), Vehicle::IsPrimaryVehicle(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_group_pool >::IsValidID(), Vehicle::NextShared(), and BaseVehicle::type.

CommandCost CmdAddVehicleGroup | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Add a vehicle to a group.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | index of array group p1 bit 0-15 : GroupID
  | p2 | vehicle to add to a group p2 bit 0-15 : VehicleID
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 211 of file group_cmd.cpp.

References CMD_ERROR, DC_EXEC, SpecializedVehicle< Train, VEH_TRAIN >::From(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_group_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_vehicle_pool >::GetIfValid(), GetWindowClassForVehicleType(), Vehicle::group_id, InvalidateWindowData(), Vehicle::IsEngineCountable(), Vehicle::IsPrimaryVehicle(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_group_pool >::IsValidID(), Group::owner, SetTrainGroupID(), SetWindowDirty(), BaseVehicle::type, UpdateNumEngineGroup(), VEH_AIRCRAFT, VEH_ROAD, VEH_SHIP, VEH_TRAIN, and Group::vehicle_type.

Referenced by CmdAddSharedVehicleGroup(), and CmdRemoveAllVehiclesGroup().

CommandCost CmdCreateGroup | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Create a new vehicle group.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | vehicle type
  | p2 | unused
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 84 of file group_cmd.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_group_pool >::CanAllocateItem(), CMD_ERROR, DC_EXEC, GetWindowClassForVehicleType(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, InvalidateWindowData(), Group::replace_protection, and Group::vehicle_type.

CommandCost CmdDeleteGroup | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Add all vehicles in the given group to the default group and then deletes the group.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | index of array group p1 bit 0-15 : GroupID
  | p2 | unused
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 115 of file group_cmd.cpp.

References CMD_ERROR, DC_EXEC, DEFAULT_GROUP, DeleteWindowById(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), GetWindowClassForVehicleType(), Vehicle::group_id, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, InvalidateWindowData(), MAX_COMPANIES, Group::owner, RemoveEngineReplacementForCompany(), BaseVehicle::type, and Group::vehicle_type.

CommandCost CmdRemoveAllVehiclesGroup | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Remove all vehicles from a group.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | index of group array p1 bit 0-15 : GroupID
  | p2 | type of vehicles
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 299 of file group_cmd.cpp.

References CMD_ERROR, CmdAddVehicleGroup(), DC_EXEC, DEFAULT_GROUP, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), GetWindowClassForVehicleType(), Vehicle::group_id, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem< Tpool >::index, InvalidateWindowData(), Vehicle::IsPrimaryVehicle(), Group::owner, and BaseVehicle::type.

CommandCost CmdRenameGroup | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

Rename a group.

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | index of array group p1 bit 0-15 : GroupID
  | p2 | unused
  | text | the new name or an empty string when resetting to the default

Returns:
  the cost of this operation or an error

Definition at line 175 of file group_cmd.cpp.

References CMD_ERROR, DC_EXEC, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), GetWindowClassForVehicleType(), InvalidateWindowData(), MAX_LENGTH_GROUP_NAME_BYTES, Group::name, Group::owner, return_cmd_error, StrEmpty(), and Group::vehicle_type.

CommandCost CmdSetGroupReplaceProtection | ( | TileIndex | tile,
|  | DoCommandFlag | flags,
|  | uint32 | p1,
|  | uint32 | p2,
|  | const char * | text |
| ) |  |  |

(Un)set global replace protection from a group

Parameters:

  | tile | unused
  | flags | type of operation
  | p1 | index of group array p1 bit 0-15 : GroupID
  | p2 | p2 bit 0 : 1 to set or 0 to clear protection.
  | text | unused

Returns:
  the cost of this operation or an error

Definition at line 338 of file group_cmd.cpp.

References CMD_ERROR, DC_EXEC, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), GetWindowClassForVehicleType(), HasBit(), InvalidateWindowData(), Group::owner, Group::replace_protection, and Group::vehicle_type.

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.

Parameters:

  | company | The company the group belongs to
  | id_g | The GroupID of the group used
  | id_e | The EngineID of the engine to count

Returns:
  The number of engines with EngineID id_e in the group

Definition at line 411 of file group_cmd.cpp.

References Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_group_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tcache, Tzero >::PoolItem<&_group_pool >::IsValidID(), Group::num_engines, and Group::owner.

Referenced by DrawEngineList(), ReplaceVehicleWindow::GenerateReplaceVehList(), AIGroup::GetNumEngines(), and InvalidateAutoreplaceWindow().

void RemoveVehicleFromGroup | ( | const Vehicle * | v | ) |

Decrease the num_vehicle variable before delete an front engine from a group.

Note:
  Called in CmdSellRailWagon and DeleteLasWagon,

Parameters:

  | v | FrontEngine of the train we want to remove.

Definition at line 358 of file group_cmd.cpp.

References Vehicle::group_id, and Vehicle::IsPrimaryVehicle().

Referenced by CmdMoveRailVehicle().

void SetTrainGroupID | ( | Train * | v,
|  | GroupID | new_g |
| ) |  |  |

Affect the groupID of a train to new_g.

Note:
  called in CmdAddVehicleGroup and CmdMoveRailVehicle

Parameters:

  | v | First vehicle of the chain.
  | new_g | index of array group

Definition at line 372 of file group_cmd.cpp.

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

Referenced by CmdAddVehicleGroup(), and CmdMoveRailVehicle().

static void UpdateNumEngineGroup | ( | EngineID | i,
|  | GroupID | old_g,
|  | GroupID | new_g |
| ) |  |  | `[inline, static]`

Update the num engines of a groupID.

Decrease the old one and increase the new one

Note:
  called in SetTrainGroupID and UpdateTrainGroupID

Parameters:

  | i | EngineID we have to update
  | old_g | index of the old group
  | new_g | index of the new group

Definition at line 41 of file group_cmd.cpp.

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

Referenced by CmdAddVehicleGroup(), SetTrainGroupID(), and 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:

  | v | First vehicle of the chain.

Definition at line 396 of file group_cmd.cpp.

References DEFAULT_GROUP, Vehicle::group_id, Train::IsFreeWagon(), Train::IsFrontEngine(), SpecializedVehicle< T, Type >::Next(), SetWindowDirty(), UpdateNumEngineGroup(), and VEH_TRAIN.

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