OpenTTD GameScript API: GSGroup Class Reference

OpenTTD GameScript API 15.0-RC2

Static Public Member Functions | Static Public Attributes
GSGroup Class Reference

Class that handles all group related functions. More...

`#include <script_group.hpp>`

Inheritance diagram for GSGroup:

## Static Public Member Functions
static bool | IsValidGroup (GroupID group_id)
| Checks whether the given group is valid.
static GroupID | CreateGroup (GSVehicle::VehicleType vehicle_type, GroupID parent_group_id)
| Create a new group.
static bool | DeleteGroup (GroupID group_id)
| Delete the given group.
static GSVehicle::VehicleType | GetVehicleType (GroupID group_id)
| Get the vehicle type of a group.
static bool | SetName (GroupID group_id, Text *name)
| Set the name of a group.
static string | GetName (GroupID group_id)
| Get the name of a group.
static bool | SetParent (GroupID group_id, GroupID parent_group_id)
| Set parent group of a group.
static GroupID | GetParent (GroupID group_id)
| Get parent group of a group.
static bool | EnableAutoReplaceProtection (GroupID group_id, bool enable)
| Enable or disable autoreplace protected.
static bool | GetAutoReplaceProtection (GroupID group_id)
| Get the autoreplace protection status.
static int | GetNumEngines (GroupID group_id, EngineID engine_id)
| Get the number of engines in a given group.
static int | GetNumVehicles (GroupID group_id, GSVehicle::VehicleType vehicle_type)
| Get the total number of vehicles in a given group and its sub-groups.
static bool | MoveVehicle (GroupID group_id, VehicleID vehicle_id)
| Move a vehicle to a group.
static bool | EnableWagonRemoval (bool keep_length)
| Enable or disable the removal of wagons when a (part of a) vehicle is (auto)replaced with a longer variant (longer wagons or longer engines) If enabled, wagons are removed from the end of the vehicle until it fits in the same number of tiles as it did before.
static bool | HasWagonRemoval ()
| Get the current status of wagon removal.
static bool | SetAutoReplace (GroupID group_id, EngineID engine_id_old, EngineID engine_id_new)
| Start replacing all vehicles with a specified engine with another engine.
static EngineID | GetEngineReplacement (GroupID group_id, EngineID engine_id)
| Get the EngineID the given EngineID is replaced with.
static bool | StopAutoReplace (GroupID group_id, EngineID engine_id)
| Stop replacing a certain engine in the specified group.
static Money | GetProfitThisYear (GroupID group_id)
| Get the current profit of a group.
static Money | GetProfitLastYear (GroupID group_id)
| Get the profit of last year of a group.
static int | GetCurrentUsage (GroupID group_id)
| Get the current vehicle usage of a group.
static bool | SetPrimaryColour (GroupID group_id, GSCompany::Colours colour)
| Set primary colour for a group.
static bool | SetSecondaryColour (GroupID group_id, GSCompany::Colours colour)
| Set secondary colour for a group.
static GSCompany::Colours | GetPrimaryColour (GroupID group_id)
| Get primary colour of a group.
static GSCompany::Colours | GetSecondaryColour (GroupID group_id)
| Get secondary colour for a group.

## Static Public Attributes
static constexpr GroupID | GROUP_ALL = ::ALL_GROUP
| All vehicles are in this group.
static constexpr GroupID | GROUP_DEFAULT = ::DEFAULT_GROUP
| Vehicles not put in any other group are in this one.
static constexpr GroupID | GROUP_INVALID = ::GroupID::Invalid()
| An invalid group id.

## Detailed Description

Class that handles all group related functions.

## Member Function Documentation

## ◆ CreateGroup()

static GroupID GSGroup::CreateGroup | ( | GSVehicle::VehicleType | vehicle_type,
|  | GroupID | parent_group_id
| ) |  |
| static

Create a new group.

Parameters

  vehicle_type | The type of vehicle to create a group for.
  parent_group_id | The parent group id to create this group under, INVALID_GROUP for top-level.

Precondition
  GSCompanyMode::IsValid().

Returns
  The GroupID of the new group, or an invalid GroupID when it failed. Check the return value using IsValidGroup(). In test-mode 0 is returned if it was successful; any other value indicates failure.

## ◆ DeleteGroup()

static bool GSGroup::DeleteGroup | ( | GroupID | group_id | ) |
| static

Delete the given group.

When the deletion succeeds all vehicles in the given group will move to the GROUP_DEFAULT.

Parameters

  group_id | The group to delete.

Precondition
  IsValidGroup(group_id).
  GSCompanyMode::IsValid().

Returns
  True if and only if the group was successfully deleted.

## ◆ EnableAutoReplaceProtection()

static bool GSGroup::EnableAutoReplaceProtection | ( | GroupID | group_id,
|  | bool | enable
| ) |  |
| static

Enable or disable autoreplace protected.

If the protection is enabled, global autoreplace won't affect vehicles in this group.

Parameters

  group_id | The group to change the protection for.
  enable | True if protection should be enabled.

Precondition
  IsValidGroup(group_id).
  GSCompanyMode::IsValid().

Returns
  True if and only if the protection was successfully changed.

## ◆ EnableWagonRemoval()

static bool GSGroup::EnableWagonRemoval | ( | bool | keep_length | ) |
| static

Enable or disable the removal of wagons when a (part of a) vehicle is (auto)replaced with a longer variant (longer wagons or longer engines) If enabled, wagons are removed from the end of the vehicle until it fits in the same number of tiles as it did before.

Parameters

  keep_length | If true, wagons will be removed if the new engine is longer.

Precondition
  GSCompanyMode::IsValid().

Returns
  True if and only if the value was successfully changed.

## ◆ GetAutoReplaceProtection()

static bool GSGroup::GetAutoReplaceProtection | ( | GroupID | group_id | ) |
| static

Get the autoreplace protection status.

Parameters

  group_id | The group to get the protection status for.

Precondition
  IsValidGroup(group_id).

Returns
  The autoreplace protection status for the given group.

## ◆ GetCurrentUsage()

static int GSGroup::GetCurrentUsage | ( | GroupID | group_id | ) |
| static

Get the current vehicle usage of a group.

Parameters

  group_id | The group to get the current usage of.

Precondition
  IsValidGroup(group_id).

Returns
  The current usage of the group.

## ◆ GetEngineReplacement()

static EngineID GSGroup::GetEngineReplacement | ( | GroupID | group_id,
|  | EngineID | engine_id
| ) |  |
| static

Get the EngineID the given EngineID is replaced with.

Parameters

  group_id | The group to get the replacement from.
  engine_id | The engine that is being replaced.

Precondition
  IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL.
  GSCompanyMode::IsValid().

Returns
  The EngineID that is replacing engine_id or an invalid EngineID in case engine_id is not begin replaced.

## ◆ GetName()

static string GSGroup::GetName | ( | GroupID | group_id | ) |
| static

Get the name of a group.

Parameters

  group_id | The group to get the name of.

Precondition
  IsValidGroup(group_id).

Returns
  The name the group has.

## ◆ GetNumEngines()

static int GSGroup::GetNumEngines | ( | GroupID | group_id,
|  | EngineID | engine_id
| ) |  |
| static

Get the number of engines in a given group.

Parameters

  group_id | The group to get the number of engines in.
  engine_id | The engine id to count.

Precondition
  GSEngine::IsValidEngine(engine_id).
  (IsValidGroup(group_id) && GSEngine::GetVehicleType(engine_id) == GetVehicleType(group_id)) || group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
  GSCompanyMode::IsValid().

Returns
  The number of engines with id engine_id in the group with id group_id.

## ◆ GetNumVehicles()

static int GSGroup::GetNumVehicles | ( | GroupID | group_id,
|  | GSVehicle::VehicleType | vehicle_type
| ) |  |
| static

Get the total number of vehicles in a given group and its sub-groups.

Parameters

  group_id | The group to get the number of vehicles in.
  vehicle_type | The type of vehicle of the group.

Precondition
  IsValidGroup(group_id) || group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
  IsValidGroup(group_id) || vehicle_type == GSVehicle::VT_ROAD || vehicle_type == GSVehicle::VT_RAIL || vehicle_type == GSVehicle::VT_WATER || vehicle_type == GSVehicle::VT_AIR
  GSCompanyMode::IsValid().

Returns
  The total number of vehicles in the group with id group_id and it's sub-groups.

Note
  If the group is valid (neither GROUP_ALL nor GROUP_DEFAULT), the value of vehicle_type is retrieved from the group itself and not from the input value. But if the group is GROUP_ALL or GROUP_DEFAULT, then vehicle_type must be valid.

## ◆ GetParent()

static GroupID GSGroup::GetParent | ( | GroupID | group_id | ) |
| static

Get parent group of a group.

Parameters

  group_id | The group to get the parent of.

Precondition
  IsValidGroup(group_id).

Returns
  The group id of the parent group.

## ◆ GetPrimaryColour()

static GSCompany::Colours GSGroup::GetPrimaryColour | ( | GroupID | group_id | ) |
| static

Get primary colour of a group.

Parameters

  group_id | The group id to get the colour of.

Precondition
  IsValidGroup(group_id).

Returns
  The primary colour of the group.

## ◆ GetProfitLastYear()

static Money GSGroup::GetProfitLastYear | ( | GroupID | group_id | ) |
| static

Get the profit of last year of a group.

Parameters

  group_id | The group to get the profit of.

Precondition
  IsValidGroup(group_id).

Returns
  The profit the vehicles in this group made in the previous economy-year.

See also
  GSEconomyTime

## ◆ GetProfitThisYear()

static Money GSGroup::GetProfitThisYear | ( | GroupID | group_id | ) |
| static

Get the current profit of a group.

Parameters

  group_id | The group to get the profit of.

Precondition
  IsValidGroup(group_id).

Returns
  The profit the vehicles in this group have made this economy-year so far.

See also
  GSEconomyTime

## ◆ GetSecondaryColour()

static GSCompany::Colours GSGroup::GetSecondaryColour | ( | GroupID | group_id | ) |
| static

Get secondary colour for a group.

Parameters

  group_id | The group id to get the colour of.

Precondition
  IsValidGroup(group_id).

Returns
  The secondary colour of the group.

## ◆ GetVehicleType()

static GSVehicle::VehicleType GSGroup::GetVehicleType | ( | GroupID | group_id | ) |
| static

Get the vehicle type of a group.

Parameters

  group_id | The group to get the type from.

Precondition
  IsValidGroup(group_id).

Returns
  The vehicletype of the given group.

## ◆ HasWagonRemoval()

static bool GSGroup::HasWagonRemoval | ( |  | ) |
| static

Get the current status of wagon removal.

Precondition
  GSCompanyMode::IsValid().

Returns
  Whether or not wagon removal is enabled.

## ◆ IsValidGroup()

static bool GSGroup::IsValidGroup | ( | GroupID | group_id | ) |
| static

Checks whether the given group is valid.

Parameters

  group_id | The group to check.

Precondition
  group_id != GROUP_DEFAULT && group_id != GROUP_ALL.

Returns
  True if and only if the group is valid.

## ◆ MoveVehicle()

static bool GSGroup::MoveVehicle | ( | GroupID | group_id,
|  | VehicleID | vehicle_id
| ) |  |
| static

Move a vehicle to a group.

Parameters

  group_id | The group to move the vehicle to.
  vehicle_id | The vehicle to move to the group.

Precondition
  IsValidGroup(group_id) || group_id == GROUP_DEFAULT.
  GSVehicle::IsPrimaryVehicle(vehicle_id).
  GSCompanyMode::IsValid().

Returns
  True if and only if the vehicle was successfully moved to the group.

Note
  A vehicle can be in only one group at the same time. To remove it from a group, move it to another or to GROUP_DEFAULT. Moving the vehicle to the given group means removing it from another group.

## ◆ SetAutoReplace()

static bool GSGroup::SetAutoReplace | ( | GroupID | group_id,
|  | EngineID | engine_id_old,
|  | EngineID | engine_id_new
| ) |  |
| static

Start replacing all vehicles with a specified engine with another engine.

Parameters

  group_id | The group to replace vehicles from. Use ALL_GROUP to replace vehicles from all groups that haven't set autoreplace protection.
  engine_id_old | The engine id to start replacing.
  engine_id_new | The engine id to replace with.

Precondition
  IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL.
  GSEngine.IsBuildable(engine_id_new).
  GSCompanyMode::IsValid().

Returns
  True if and if the replacing was successfully started.

Note
  To stop autoreplacing engine_id_old, call StopAutoReplace(group_id, engine_id_old).

## ◆ SetName()

static bool GSGroup::SetName | ( | GroupID | group_id,
|  | Text * | name
| ) |  |
| static

Set the name of a group.

Parameters

  group_id | The group to set the name for.
  name | The name for the group (can be either a raw string, or a GSText object).

Precondition
  IsValidGroup(group_id).
  name != null && len(name) != 0
  GSCompanyMode::IsValid().

Exceptions

  GSError::ERR_NAME_IS_NOT_UNIQUE |

Returns
  True if and only if the name was changed.

## ◆ SetParent()

static bool GSGroup::SetParent | ( | GroupID | group_id,
|  | GroupID | parent_group_id
| ) |  |
| static

Set parent group of a group.

Parameters

  group_id | The group to set the parent for.
  parent_group_id | The parent group to set.

Precondition
  IsValidGroup(group_id).
  IsValidGroup(parent_group_id).
  GSCompanyMode::IsValid().

Returns
  True if and only if the parent group was changed.

## ◆ SetPrimaryColour()

static bool GSGroup::SetPrimaryColour | ( | GroupID | group_id,
|  | GSCompany::Colours | colour
| ) |  |
| static

Set primary colour for a group.

Parameters

  group_id | The group id to set the colour of.
  colour | Colour to set.

Precondition
  IsValidGroup(group_id).
  GSCompanyMode::IsValid().

Returns
  True iff the colour was set successfully.

## ◆ SetSecondaryColour()

static bool GSGroup::SetSecondaryColour | ( | GroupID | group_id,
|  | GSCompany::Colours | colour
| ) |  |
| static

Set secondary colour for a group.

Parameters

  group_id | The group id to set the colour of.
  colour | Colour to set.

Precondition
  IsValidGroup(group_id).
  GSCompanyMode::IsValid().

Returns
  True iff the colour was set successfully.

## ◆ StopAutoReplace()

static bool GSGroup::StopAutoReplace | ( | GroupID | group_id,
|  | EngineID | engine_id
| ) |  |
| static

Stop replacing a certain engine in the specified group.

Parameters

  group_id | The group to stop replacing the engine in.
  engine_id | The engine id to stop replacing with another engine.

Precondition
  IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL.
  GSCompanyMode::IsValid().

Returns
  True if and if the replacing was successfully stopped.