OpenTTD AI API 20241015-master-gd5b57a56f4
Public Types | Static Public Member Functions
AIGroup Class Reference

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

#include <script_group.hpp>

Inheritance diagram for AIGroup:

Public Types

enum  GroupID {
  GROUP_ALL ,
  GROUP_DEFAULT ,
  GROUP_INVALID
}
 The group IDs of some special groups. More...
 

Static Public Member Functions

static bool IsValidGroup (GroupID group_id)
 Checks whether the given group is valid.
 
static GroupID CreateGroup (AIVehicle::VehicleType vehicle_type, GroupID parent_group_id)
 Create a new group.
 
static bool DeleteGroup (GroupID group_id)
 Delete the given group.
 
static AIVehicle::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, AIVehicle::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, AICompany::Colours colour)
 Set primary colour for a group.
 
static bool SetSecondaryColour (GroupID group_id, AICompany::Colours colour)
 Set secondary colour for a group.
 
static AICompany::Colours GetPrimaryColour (GroupID group_id)
 Get primary colour of a group.
 
static AICompany::Colours GetSecondaryColour (GroupID group_id)
 Get secondary colour for a group.
 

Detailed Description

Class that handles all group related functions.

Member Enumeration Documentation

◆ GroupID

The group IDs of some special groups.

Enumerator
GROUP_ALL 

All vehicles are in this group.

GROUP_DEFAULT 

Vehicles not put in any other group are in this one.

GROUP_INVALID 

An invalid group id.

Member Function Documentation

◆ CreateGroup()

static GroupID AIGroup::CreateGroup ( AIVehicle::VehicleType  vehicle_type,
GroupID  parent_group_id 
)
static

Create a new group.

Parameters
vehicle_typeThe type of vehicle to create a group for.
parent_group_idThe parent group id to create this group under, INVALID_GROUP for top-level.
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 AIGroup::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_idThe group to delete.
Precondition
IsValidGroup(group_id).
Returns
True if and only if the group was successfully deleted.

◆ EnableAutoReplaceProtection()

static bool AIGroup::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_idThe group to change the protection for.
enableTrue if protection should be enabled.
Precondition
IsValidGroup(group_id).
Returns
True if and only if the protection was successfully changed.

◆ EnableWagonRemoval()

static bool AIGroup::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_lengthIf true, wagons will be removed if the new engine is longer.
Returns
True if and only if the value was successfully changed.

◆ GetAutoReplaceProtection()

static bool AIGroup::GetAutoReplaceProtection ( GroupID  group_id)
static

Get the autoreplace protection status.

Parameters
group_idThe group to get the protection status for.
Precondition
IsValidGroup(group_id).
Returns
The autoreplace protection status for the given group.

◆ GetCurrentUsage()

static int AIGroup::GetCurrentUsage ( GroupID  group_id)
static

Get the current vehicle usage of a group.

Parameters
group_idThe group to get the current usage of.
Precondition
IsValidGroup(group_id).
Returns
The current usage of the group.

◆ GetEngineReplacement()

static EngineID AIGroup::GetEngineReplacement ( GroupID  group_id,
EngineID  engine_id 
)
static

Get the EngineID the given EngineID is replaced with.

Parameters
group_idThe group to get the replacement from.
engine_idThe engine that is being replaced.
Precondition
IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL.
Returns
The EngineID that is replacing engine_id or an invalid EngineID in case engine_id is not begin replaced.

◆ GetName()

static string AIGroup::GetName ( GroupID  group_id)
static

Get the name of a group.

Parameters
group_idThe group to get the name of.
Precondition
IsValidGroup(group_id).
Returns
The name the group has.

◆ GetNumEngines()

static int AIGroup::GetNumEngines ( GroupID  group_id,
EngineID  engine_id 
)
static

Get the number of engines in a given group.

Parameters
group_idThe group to get the number of engines in.
engine_idThe engine id to count.
Precondition
AIEngine::IsValidEngine(engine_id).
(IsValidGroup(group_id) && AIEngine::GetVehicleType(engine_id) == GetVehicleType(group_id)) || group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
Returns
The number of engines with id engine_id in the group with id group_id.

◆ GetNumVehicles()

static int AIGroup::GetNumVehicles ( GroupID  group_id,
AIVehicle::VehicleType  vehicle_type 
)
static

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

Parameters
group_idThe group to get the number of vehicles in.
vehicle_typeThe type of vehicle of the group.
Precondition
IsValidGroup(group_id) || group_id == GROUP_ALL || group_id == GROUP_DEFAULT.
IsValidGroup(group_id) || vehicle_type == AIVehicle::VT_ROAD || vehicle_type == AIVehicle::VT_RAIL || vehicle_type == AIVehicle::VT_WATER || vehicle_type == AIVehicle::VT_AIR
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 AIGroup::GetParent ( GroupID  group_id)
static

Get parent group of a group.

Parameters
group_idThe group to get the parent of.
Precondition
IsValidGroup(group_id).
Returns
The group id of the parent group.

◆ GetPrimaryColour()

static AICompany::Colours AIGroup::GetPrimaryColour ( GroupID  group_id)
static

Get primary colour of a group.

Parameters
group_idThe group id to get the colour of.
Precondition
IsValidGroup(group_id).
Returns
The primary colour of the group.

◆ GetProfitLastYear()

static Money AIGroup::GetProfitLastYear ( GroupID  group_id)
static

Get the profit of last year of a group.

Parameters
group_idThe 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
AIEconomyTime

◆ GetProfitThisYear()

static Money AIGroup::GetProfitThisYear ( GroupID  group_id)
static

Get the current profit of a group.

Parameters
group_idThe 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
AIEconomyTime

◆ GetSecondaryColour()

static AICompany::Colours AIGroup::GetSecondaryColour ( GroupID  group_id)
static

Get secondary colour for a group.

Parameters
group_idThe group id to get the colour of.
Precondition
IsValidGroup(group_id).
Returns
The secondary colour of the group.

◆ GetVehicleType()

static AIVehicle::VehicleType AIGroup::GetVehicleType ( GroupID  group_id)
static

Get the vehicle type of a group.

Parameters
group_idThe group to get the type from.
Precondition
IsValidGroup(group_id).
Returns
The vehicletype of the given group.

◆ HasWagonRemoval()

static bool AIGroup::HasWagonRemoval ( )
static

Get the current status of wagon removal.

Returns
Whether or not wagon removal is enabled.

◆ IsValidGroup()

static bool AIGroup::IsValidGroup ( GroupID  group_id)
static

Checks whether the given group is valid.

Parameters
group_idThe 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 AIGroup::MoveVehicle ( GroupID  group_id,
VehicleID  vehicle_id 
)
static

Move a vehicle to a group.

Parameters
group_idThe group to move the vehicle to.
vehicle_idThe vehicle to move to the group.
Precondition
IsValidGroup(group_id) || group_id == GROUP_DEFAULT.
AIVehicle::IsPrimaryVehicle(vehicle_id).
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 AIGroup::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_idThe group to replace vehicles from. Use ALL_GROUP to replace vehicles from all groups that haven't set autoreplace protection.
engine_id_oldThe engine id to start replacing.
engine_id_newThe engine id to replace with.
Precondition
IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL.
AIEngine.IsBuildable(engine_id_new).
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 AIGroup::SetName ( GroupID  group_id,
Text *  name 
)
static

Set the name of a group.

Parameters
group_idThe group to set the name for.
nameThe name for the group (can be either a raw string, or a AIText object).
Precondition
IsValidGroup(group_id).
name != null && len(name) != 0
Exceptions
AIError::ERR_NAME_IS_NOT_UNIQUE
Returns
True if and only if the name was changed.

◆ SetParent()

static bool AIGroup::SetParent ( GroupID  group_id,
GroupID  parent_group_id 
)
static

Set parent group of a group.

Parameters
group_idThe group to set the parent for.
parent_group_idThe parent group to set.
Precondition
IsValidGroup(group_id).
IsValidGroup(parent_group_id).
Returns
True if and only if the parent group was changed.

◆ SetPrimaryColour()

static bool AIGroup::SetPrimaryColour ( GroupID  group_id,
AICompany::Colours  colour 
)
static

Set primary colour for a group.

Parameters
group_idThe group id to set the colour of.
colourColour to set.
Precondition
IsValidGroup(group_id).
Returns
True iff the colour was set successfully.

◆ SetSecondaryColour()

static bool AIGroup::SetSecondaryColour ( GroupID  group_id,
AICompany::Colours  colour 
)
static

Set secondary colour for a group.

Parameters
group_idThe group id to set the colour of.
colourColour to set.
Precondition
IsValidGroup(group_id).
Returns
True iff the colour was set successfully.

◆ StopAutoReplace()

static bool AIGroup::StopAutoReplace ( GroupID  group_id,
EngineID  engine_id 
)
static

Stop replacing a certain engine in the specified group.

Parameters
group_idThe group to stop replacing the engine in.
engine_idThe engine id to stop replacing with another engine.
Precondition
IsValidGroup(group_id) || group_id == GROUP_DEFAULT || group_id == GROUP_ALL.
Returns
True if and if the replacing was successfully stopped.