OpenTTD Source: src/group_cmd.cpp File Reference

OpenTTD Source 1.11.0-RC1

Functions | Variables
group_cmd.cpp File Reference
`#include "stdafx.h"`
`#include "cmd_helper.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 "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. More...
const Livery * | GetParentLivery (const Group *g)
void | PropagateChildLivery (const Group *g)
| Propagate a livery change to a group's children. More...
CommandCost | CmdCreateGroup (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Create a new vehicle group. More...
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. More...
CommandCost | CmdAlterGroup (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Alter a group. More...
static void | AddVehicleToGroup (Vehicle *v, GroupID new_g)
| Do add a vehicle to a group. More...
CommandCost | CmdAddVehicleGroup (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Add a vehicle to a group. More...
CommandCost | CmdAddSharedVehicleGroup (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Add all shared vehicles of all vehicles from a group. More...
CommandCost | CmdRemoveAllVehiclesGroup (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Remove all vehicles from a group. More...
CommandCost | CmdSetGroupLivery (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| Set the livery for a vehicle group. More...
static void | SetGroupReplaceProtection (Group *g, bool protect)
| Set replace protection for a group and its sub-groups. More...
CommandCost | CmdSetGroupReplaceProtection (TileIndex tile, DoCommandFlag flags, uint32 p1, uint32 p2, const char *text)
| (Un)set global replace protection from a group More...
void | RemoveVehicleFromGroup (const Vehicle *v)
| Decrease the num_vehicle variable before delete an front engine from a group. More...
void | SetTrainGroupID (Train *v, GroupID new_g)
| Affect the groupID of a train to new_g. More...
void | UpdateTrainGroupID (Train *v)
| Recalculates the groupID of a train. More...
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. More...
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. More...
uint | GetGroupNumProfitVehicle (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. More...
Money | GetGroupProfitLastYear (CompanyID company, GroupID id_g, VehicleType type)
| Get last year's profit for the group with GroupID id_g and its sub-groups. More...
void | RemoveAllGroupsForCompany (const CompanyID company)
bool | GroupIsInGroup (GroupID search, GroupID group)
| Test if GroupID group is a descendant of (or is) GroupID search. More...

## Variables
GroupID | _new_group_id
GroupPool | _group_pool ("Group")
| Pool of groups.

## 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

Do add a vehicle to a group.

Parameters

  v | Vehicle to add.
  new_g | Group to add to.

Definition at line 472 of file group_cmd.cpp.

References GroupStatistics::CountVehicle(), and BaseVehicle::type.

## ◆ CmdAddSharedVehicleGroup()

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

References CMD_ADD_VEHICLE_GROUP, CMD_ERROR, DC_EXEC, DoCommand(), Vehicle::FirstShared(), GetWindowClassForVehicleType(), InvalidateWindowData(), IsCompanyBuildableVehicleType(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::IsValidID(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::Iterate(), and Vehicle::NextShared().

## ◆ CmdAddVehicleGroup()

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-19 : VehicleID p2 bit 31 : Add shared vehicles as well.
  text | unused

Returns
  the cost of this operation or an error

Definition at line 510 of file group_cmd.cpp.

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

## ◆ CmdAlterGroup()

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

Alter a group.

Parameters

  tile | unused
  flags | type of operation
  p1 | index of array group p1 bit 0-15 : GroupID p1 bit 16: 0 - Rename grouop 1 - Set group parent
  p2 | parent group index
  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 406 of file group_cmd.cpp.

References _current_company, CMD_ERROR, DC_EXEC, GB(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), GroupIsInGroup(), HasBit(), Livery::in_use, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, INVALID_GROUP, Group::livery, MAX_LENGTH_GROUP_NAME_CHARS, Group::name, Group::owner, Group::parent, return_cmd_error, StrEmpty(), GroupStatistics::UpdateAutoreplace(), Utf8StringLength(), and Group::vehicle_type.

## ◆ CmdCreateGroup()

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 | parent groupid
  text | unused

Returns
  the cost of this operation or an error

Definition at line 303 of file group_cmd.cpp.

References _current_company, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::CanAllocateItem(), CMD_ERROR, Livery::colour1, DC_EXEC, GB(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), INVALID_GROUP, IsCompanyBuildableVehicleType(), Group::livery, Group::owner, Group::parent, Group::replace_protection, and Group::vehicle_type.

## ◆ CmdDeleteGroup()

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

References _current_company, OrderBackup::ClearGroup(), CMD_DELETE_GROUP, CMD_ERROR, CMD_REMOVE_ALL_VEHICLES_GROUP, DC_EXEC, DeleteWindowById(), DoCommand(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_company_pool >::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), GetWindowClassForVehicleType(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem< Tpool >::index, InvalidateWindowData(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_enginerenew_pool >::Iterate(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::Iterate(), MAX_COMPANIES, Group::owner, RemoveEngineReplacementForCompany(), Group::vehicle_type, and WC_REPLACE_VEHICLE.

## ◆ CmdRemoveAllVehiclesGroup()

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 | unused
  text | unused

Returns
  the cost of this operation or an error

Definition at line 604 of file group_cmd.cpp.

References _current_company, CMD_ADD_VEHICLE_GROUP, CMD_ERROR, DC_EXEC, DEFAULT_GROUP, DoCommand(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), GetWindowClassForVehicleType(), InvalidateWindowData(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_vehicle_pool >::Iterate(), Group::owner, and Group::vehicle_type.

## ◆ CmdSetGroupLivery()

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

Set the livery for a vehicle group.

Parameters

  tile | Unused.
  flags | Command flags.
  p1 | p1 bit 0-15 Group ID.
  p2 | p2 bit 8 Set secondary instead of primary colour p2 bit 16-23 Colour.

Definition at line 638 of file group_cmd.cpp.

References _current_company, CMD_ERROR, Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_group_pool >::GetIfValid(), HasBit(), and Group::owner.

## ◆ CmdSetGroupReplaceProtection()

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. p2 bit 1 : 1 to apply to sub-groups.
  text | unused

Returns
  the cost of this operation or an error

Definition at line 692 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(), HasBit(), Group::owner, Group::replace_protection, SetGroupReplaceProtection(), SetWindowDirty(), and Group::vehicle_type.

## ◆ GetGroupNumEngines()

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.

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

References GroupStatistics::Get(), Pool< Titem, Tindex, Tgrowth_step, Tmax_size, Tpool_type, Tcache, Tzero >::PoolItem<&_engine_pool >::Get(), 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_engines, Group::parent, and Engine::type.

Referenced by ReplaceVehicleWindow::GenerateReplaceVehList(), and InvalidateAutoreplaceWindow().

## ◆ GetGroupNumProfitVehicle()

uint GetGroupNumProfitVehicle | ( | 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

  company | The company the group belongs to
  id_g | The GroupID of the group used
  type | The vehicle type of the group

Returns
  The number of vehicles above profit minimum age in the group

Definition at line 819 of file group_cmd.cpp.

References GroupStatistics::Get(), 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_profit_vehicle, and Group::parent.

## ◆ 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

  company | The company the group belongs to
  id_g | The GroupID of the group used
  type | The vehicle type of the group

Returns
  The number of vehicles in the group

Definition at line 802 of file group_cmd.cpp.

References GroupStatistics::Get(), 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.

## ◆ GetGroupProfitLastYear()

Money GetGroupProfitLastYear | ( | CompanyID | company,
|  | GroupID | id_g,
|  | VehicleType | type
| ) |  |

Get last year's profit for the group with GroupID id_g and its sub-groups.

Parameters

  company | The company the group belongs to
  id_g | The GroupID of the group used
  type | The vehicle type of the group

Returns
  Last year's profit for the group

Definition at line 836 of file group_cmd.cpp.

References GroupStatistics::Get(), 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.

## ◆ GroupIsInGroup()

bool GroupIsInGroup | ( | GroupID | search,
|  | GroupID | group
| ) |  |

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

Parameters

  search | The GroupID to search in
  group | The GroupID to search for

Returns
  True iff group is search or a descendant of search

Definition at line 859 of file group_cmd.cpp.

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

Referenced by CmdAlterGroup(), and GetEngineReplacement().

## ◆ PropagateChildLivery()

void PropagateChildLivery | ( | const Group * | g | ) |

Propagate a livery change to a group's children.

Parameters

  g | Group.

Definition at line 265 of file group_cmd.cpp.

References 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<&_vehicle_pool >::Iterate(), and Vehicle::Next().

## ◆ RemoveVehicleFromGroup()

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

References Vehicle::IsPrimaryVehicle().

## ◆ SetGroupReplaceProtection()

static void SetGroupReplaceProtection | ( | Group * | g,
|  | bool | protect
| ) |  |
| static

Set replace protection for a group and its sub-groups.

Parameters

  g | initial group.
  protect | 1 to set or 0 to clear protection.

Definition at line 671 of file group_cmd.cpp.

References 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(), and Group::replace_protection.

Referenced by CmdSetGroupReplaceProtection().

## ◆ SetTrainGroupID()

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

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

## ◆ 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

  v | Vehicle we have to update
  old_g | index of the old group
  new_g | index of the new group

Definition at line 237 of file group_cmd.cpp.

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

Referenced by 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

  v | First vehicle of the chain.

Definition at line 758 of file group_cmd.cpp.

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

Referenced by NormaliseTrainHead().