# group_cmd.cpp File Reference

Handling of the engine groups. More...

`#include "stdafx.h"`
`#include "openttd.h"`
`#include "variables.h"`
`#include "command_func.h"`
`#include "saveload.h"`
`#include "debug.h"`
`#include "group.h"`
`#include "train.h"`
`#include "aircraft.h"`
`#include "vehicle_gui.h"`
`#include "strings_func.h"`
`#include "functions.h"`
`#include "window_func.h"`
`#include "vehicle_func.h"`
`#include "autoreplace_base.h"`
`#include "autoreplace_func.h"`
`#include "string_func.h"`
`#include "player_func.h"`
`#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 (void)
static WindowClass | GetWCForVT (VehicleType vt)
CommandCost | CmdCreateGroup (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Create a new vehicle group.
CommandCost | CmdDeleteGroup (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| 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, uint32 flags, uint32 p1, uint32 p2)
| Rename a group.
CommandCost | CmdAddVehicleGroup (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Add a vehicle to a group.
CommandCost | CmdAddSharedVehicleGroup (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Add all shared vehicles of all vehicles from a group.
CommandCost | CmdRemoveAllVehiclesGroup (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| Remove all vehicles from a group.
CommandCost | CmdSetGroupReplaceProtection (TileIndex tile, uint32 flags, uint32 p1, uint32 p2)
| (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 (Vehicle *v, GroupID new_g)
| Affect the groupID of a train to new_g.
void | UpdateTrainGroupID (Vehicle *v)
| Recalculates the groupID of a train.
uint | GetGroupNumEngines (PlayerID p, GroupID id_g, EngineID id_e)
| Get the number of engines with EngineID id_e in the group with GroupID id_g.
void | RemoveAllGroupsForPlayer (const PlayerID p)
static void | Save_GROUP (void)
static void | Load_GROUP (void)
## Variables
static const SaveLoad | _group_desc []
const ChunkHandler | _group_chunk_handlers []

## Detailed Description

Handling of the engine groups.

Definition in file group_cmd.cpp.

## Function Documentation

CommandCost CmdAddSharedVehicleGroup | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Add all shared vehicles of all vehicles from a group.

Parameters:

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

Definition at line 254 of file group_cmd.cpp.

References CMD_ERROR, CmdAddVehicleGroup(), DC_EXEC, and GetFirstVehicleFromSharedList().

CommandCost CmdAddVehicleGroup | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Add a vehicle to a group.

Parameters:

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

Definition at line 207 of file group_cmd.cpp.

References CMD_ERROR, DC_EXEC, IsEngineCountable(), IsValidVehicleID(), SetTrainGroupID(), and UpdateNumEngineGroup().

Referenced by CmdAddSharedVehicleGroup(), and CmdRemoveAllVehiclesGroup().

CommandCost CmdCreateGroup | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Create a new vehicle group.

Parameters:

  | tile | unused
  | p1 | vehicle type
  | p2 | unused

Definition at line 89 of file group_cmd.cpp.

References CMD_ERROR, and DC_EXEC.

CommandCost CmdDeleteGroup | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

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

Parameters:

  | tile | unused
  | p1 | index of array group p1 bit 0-15 : GroupID
  | p2 | unused

Definition at line 115 of file group_cmd.cpp.

References CMD_ERROR, DC_EXEC, DEFAULT_GROUP, EngineRenew::from, EngineRenew::group_id, MAX_PLAYERS, and RemoveEngineReplacementForPlayer().

CommandCost CmdRemoveAllVehiclesGroup | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Remove all vehicles from a group.

Parameters:

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

Definition at line 291 of file group_cmd.cpp.

References CMD_ERROR, CmdAddVehicleGroup(), DC_EXEC, and DEFAULT_GROUP.

CommandCost CmdRenameGroup | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

Rename a group.

Parameters:

  | tile | unused
  | p1 | index of array group p1 bit 0-15 : GroupID
  | p2 | unused

Definition at line 177 of file group_cmd.cpp.

References _cmd_text, CMD_ERROR, DC_EXEC, and return_cmd_error.

CommandCost CmdSetGroupReplaceProtection | ( | TileIndex | tile,
|  | uint32 | flags,
|  | uint32 | p1,
|  | uint32 | p2 |
| ) |  |  |

(Un)set global replace protection from a group

Parameters:

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

Definition at line 328 of file group_cmd.cpp.

References CMD_ERROR, DC_EXEC, and HasBit().

uint GetGroupNumEngines | ( | PlayerID | p,
|  | GroupID | id_g,
|  | EngineID | id_e |
| ) |  |  |

Get the number of engines with EngineID id_e in the group with GroupID id_g.

Parameters:

  | 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 402 of file group_cmd.cpp.

Referenced by DrawEngineList(), GenerateReplaceVehList(), 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 349 of file group_cmd.cpp.

Referenced by CmdMoveRailVehicle().

void SetTrainGroupID | ( | Vehicle * | 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 363 of file group_cmd.cpp.

References IsEngineCountable(), IsFrontEngine(), and UpdateNumEngineGroup().

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 33 of file group_cmd.cpp.

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

void UpdateTrainGroupID | ( | Vehicle * | 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 387 of file group_cmd.cpp.

References DEFAULT_GROUP, IsEngineCountable(), IsFreeWagon(), IsFrontEngine(), and UpdateNumEngineGroup().

Referenced by CmdBuildRailVehicle(), CmdBuildRailWagon(), CmdMoveRailVehicle(), and CmdSellRailWagon().

## Variable Documentation

const ChunkHandler _group_chunk_handlers[]

Initial value:

     {
      { 'GRPS', Save_GROUP, Load_GROUP, CH_ARRAY | CH_LAST},
    }

const SaveLoad _group_desc[]`[static]`

Initial value:

     {
      SLE_CONDVAR(Group, name,           SLE_NAME,    0, 83),
      SLE_CONDSTR(Group, name,           SLE_STR, 0, 84, SL_MAX_VERSION),
      SLE_VAR(Group, num_vehicle,        SLE_UINT16),
      SLE_VAR(Group, owner,              SLE_UINT8),
      SLE_VAR(Group, vehicle_type,       SLE_UINT8),
      SLE_VAR(Group, replace_protection, SLE_BOOL),

    }

Definition at line 426 of file group_cmd.cpp.