23#include "table/strings.h"
35 this->num_vehicle = 0;
36 this->profit_last_year = 0;
37 this->num_vehicle_min_age = 0;
38 this->profit_last_year_min_age = 0;
41 this->num_engines.clear();
50 if (g->parent == GroupID::Invalid())
continue;
52 if (pg ==
nullptr || pg->
owner != g->owner || pg->
vehicle_type != g->vehicle_type) {
55 Debug(misc, 2,
"Group {} has invalid parent {}", g->index, g->parent);
56 g->parent = GroupID::Invalid();
71 if (found != std::end(this->
num_engines))
return found->second;
86 assert(g->
owner == company);
91 if (IsDefaultGroupID(id_g))
return Company::Get(company)->group_default[type];
125 c->group_all[type].Clear();
126 c->group_default[type].Clear();
132 g->statistics.Clear();
136 if (!v->IsEngineCountable())
continue;
154 assert(delta == 1 || delta == -1);
179 assert(delta == 1 || delta == -1);
218 c->group_all[type].ClearProfits();
219 c->group_default[type].ClearProfits();
225 g->statistics.ClearProfits();
229 if (v->IsPrimaryVehicle()) {
251 if (g->owner != company)
continue;
252 g->statistics.ClearAutoreplace();
275 if (old_g != new_g) {
287 if (g->
parent == GroupID::Invalid()) {
289 return &c->livery[LS_DEFAULT];
307 if (v->group_id == g->
index && (!v->IsGroundVehicle() || v->IsFrontEngine())) {
308 for (
Vehicle *u = v; u !=
nullptr; u = u->
Next()) {
309 u->colourmap = PAL_NONE;
310 u->InvalidateNewGRFCache();
403 for (
const GroupID &childgroup : children) {
411 if (g->
owner < MAX_COMPANIES) {
422 if (g->
parent != GroupID::Invalid()) {
456 bool reset = text.empty();
488 g->
parent = (pg ==
nullptr) ? GroupID::Invalid() : pg->
index;
493 if (!g->
livery.
in_use.
All({Livery::Flag::Primary, Livery::Flag::Secondary})) {
495 const Livery *livery = GetParentLivery(g);
529 default: NOT_REACHED();
539 for (
Vehicle *u = v; u !=
nullptr; u = u->
Next()) {
540 u->colourmap = PAL_NONE;
541 u->InvalidateNewGRFCache();
542 u->UpdateViewport(
true);
567 if (veh_id == VehicleID::Invalid() && vli.Valid()) {
571 if (v ==
nullptr)
return {
CMD_ERROR, GroupID::Invalid() };
576 for (
const Vehicle *v : list) {
587 auto [ret, new_group_id] =
CmdCreateGroup(flags, vtype, GroupID::Invalid());
588 if (ret.Failed())
return { ret, new_group_id };
590 new_g = new_group_id;
594 for (
const Vehicle *vc : list) {
634 if (v->type == type && v->IsPrimaryVehicle()) {
635 if (v->group_id != id_g)
continue;
666 if (v->IsPrimaryVehicle()) {
667 if (v->group_id != group_id)
continue;
693 if (colour >= COLOUR_END && colour != INVALID_COLOUR)
return CMD_ERROR;
698 if (colour == INVALID_COLOUR) colour = GetParentLivery(g)->
colour1;
702 if (colour == INVALID_COLOUR) colour = GetParentLivery(g)->
colour2;
726 if (!children)
return;
771 for (
Vehicle *u = v; u !=
nullptr; u = u->
Next()) {
775 u->colourmap = PAL_NONE;
776 u->InvalidateNewGRFCache();
777 u->UpdateViewport(
true);
798 for (
Vehicle *u = v; u !=
nullptr; u = u->
Next()) {
802 u->colourmap = PAL_NONE;
803 u->InvalidateNewGRFCache();
895void RemoveAllGroupsForCompany(
const CompanyID company)
898 if (company == g->
owner)
delete g;
914 if (search == group)
return true;
916 }
while (search != GroupID::Invalid());
Base class for autoreplaces/autorenews.
Functions related to autoreplacing.
CommandCost RemoveEngineReplacementForCompany(Company *c, EngineID engine, GroupID group, DoCommandFlags flags)
Remove an engine replacement for the company.
constexpr bool All(const Timpl &other) const
Test if all of the values are set.
constexpr bool Test(Tvalue_type value) const
Test if the value-th bit is set.
constexpr Timpl & Reset()
Reset all bits.
constexpr Timpl & Set()
Set all bits.
Common return value for all commands.
VehicleType type
Vehicle type, ie VEH_ROAD, VEH_TRAIN, etc.
Flat set implementation that uses a sorted vector for storage.
UnitID UseID(UnitID index)
Use a unit number.
void ReleaseID(UnitID index)
Release a unit number.
UnitID NextID() const
Find first unused unit number.
Functions related to commands.
static const CommandCost CMD_ERROR
Define a default return value for a failed command.
@ Execute
execute the given command
@ SetGroupFlag
set/clear a flag for a group
@ AddVehicleToGroup
add a vehicle to a group
CompanyID _current_company
Company currently doing an action.
Functions related to companies.
#define Debug(category, level, format_string,...)
Output a line of debugging information.
GroupFlag
Configuration flags for a group.
@ ReplaceProtection
If set, the global autoreplace has no effect on the group.
@ ReplaceWagonRemoval
If set, autoreplace will perform wagon removal on vehicles in this group.
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.
GroupPool _group_pool
Pool of groups.
bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
Money GetGroupProfitLastYearMinAge(CompanyID company, GroupID id_g, VehicleType type)
Get last year's profit of vehicles above minimum age for the group with GroupID id_g and its sub-grou...
CommandCost CmdAddSharedVehicleGroup(DoCommandFlags flags, GroupID id_g, VehicleType type)
Add all shared vehicles of all vehicles from a group.
CommandCost CmdSetGroupFlag(DoCommandFlags flags, GroupID group_id, GroupFlag flag, bool value, bool recursive)
(Un)set group flag from a group
std::tuple< CommandCost, GroupID > CmdCreateGroup(DoCommandFlags flags, VehicleType vt, GroupID parent_group)
Create a new vehicle group.
void UpdateCompanyGroupLiveries(const Company *c)
Update group liveries for a company.
CommandCost CmdAlterGroup(DoCommandFlags flags, AlterGroupMode mode, GroupID group_id, GroupID parent_id, const std::string &text)
Alter a group.
void SetTrainGroupID(Train *v, GroupID new_g)
Affect the groupID of a train to new_g.
void UpdateGroupChildren()
Update children list for each group.
CommandCost CmdSetGroupLivery(DoCommandFlags flags, GroupID group_id, bool primary, Colours colour)
Set the livery for a vehicle group.
CommandCost CmdRemoveAllVehiclesGroup(DoCommandFlags flags, GroupID group_id)
Remove all vehicles from a group.
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
CommandCost CmdDeleteGroup(DoCommandFlags flags, GroupID group_id)
Add all vehicles in the given group to the default group and then deletes the group.
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.
static void UpdateNumEngineGroup(const Vehicle *v, GroupID old_g, GroupID new_g)
Update the num engines of a groupID.
std::tuple< CommandCost, GroupID > CmdAddVehicleGroup(DoCommandFlags flags, GroupID group_id, VehicleID veh_id, bool add_shared, const VehicleListIdentifier &vli)
Add a vehicle to a group.
static void PropagateChildLivery(const Group *g, bool reset_cache)
Propagate a livery change to a group's children, and optionally update cached vehicle colourmaps.
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.
uint GetGroupNumVehicleMinAge(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...
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Command definitions related to engine groups.
AlterGroupMode
Action for CmdAlterGroup.
@ Rename
Change group name.
@ SetParent
Change group parent.
void MarkWholeScreenDirty()
This function mark the whole screen as dirty.
static constexpr GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
static constexpr GroupID ALL_GROUP
All vehicles are in this group.
static constexpr GroupID NEW_GROUP
Sentinel for a to-be-created group.
static const uint MAX_LENGTH_GROUP_NAME_CHARS
The maximum length of a group name in characters including '\0'.
Functions related to order backups.
Some methods of Pool are placed here in order to reduce compilation time and binary size.
#define INSTANTIATE_POOL_METHODS(name)
Force instantiation of pool methods so we don't get linker errors.
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
size_t Utf8StringLength(std::string_view str)
Get the length of an UTF-8 encoded string in number of characters and thus not the number of bytes th...
Functions related to low-level strings.
VehicleType type
Type of vehicle.
CompanySettings settings
settings specific for each company
EngineRenewList engine_renew_list
Engine renewals of this company.
bool renew_keep_length
sell some wagons if after autoreplace the train is longer than before
std::array< GroupStatistics, VEH_COMPANY_END > group_default
NOSAVE: Statistics for the DEFAULT_GROUP group.
std::array< GroupStatistics, VEH_COMPANY_END > group_all
NOSAVE: Statistics for the ALL_GROUP group.
Struct to store engine replacements.
bool IsFreeWagon() const
Check if the vehicle is a free wagon (got no engine in front of it).
Statistics and caches on the vehicles in a group.
static void CountVehicle(const Vehicle *v, int delta)
Update num_vehicle when adding or removing a vehicle.
static void AddProfitLastYear(const Vehicle *v)
Add a vehicle's last year profit to the profit sum of its group.
static void VehicleReachedMinAge(const Vehicle *v)
Add a vehicle to the profit sum of its group.
uint16_t GetNumEngines(EngineID engine) const
Get number of vehicles of a specific engine ID.
Money profit_last_year
Sum of profits for all vehicles.
static GroupStatistics & GetAllGroup(const Vehicle *v)
Returns the GroupStatistic for the ALL_GROUP of a vehicle type.
static void UpdateAfterLoad()
Update all caches after loading a game, changing NewGRF, etc.
static void CountEngine(const Vehicle *v, int delta)
Update num_engines when adding/removing an engine.
uint16_t num_vehicle
Number of vehicles.
Money profit_last_year_min_age
Sum of profits for vehicles considered for profit statistics.
static void UpdateAutoreplace(CompanyID company)
Update autoreplace_defined and autoreplace_finished of all statistics of a company.
static void UpdateProfits()
Recompute the profits for all groups.
bool autoreplace_defined
Are any autoreplace rules set?
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
bool autoreplace_finished
Have all autoreplacement finished?
std::map< EngineID, uint16_t > num_engines
Caches the number of engines of each type the company owns.
uint16_t num_vehicle_min_age
Number of vehicles considered for profit statistics;.
GroupFlags flags
Group flags.
Livery livery
Custom colour scheme for vehicles in this group.
GroupID parent
Parent group.
GroupStatistics statistics
NOSAVE: Statistics and caches on the vehicles in the group.
VehicleType vehicle_type
Vehicle type of the group.
std::string name
Group Name.
uint16_t number
Per-company group number.
FlatSet< GroupID > children
NOSAVE: child groups belonging to this group.
Information about a particular livery.
Flags in_use
Livery flags.
Colours colour2
Second colour, for vehicles with 2CC support.
Colours colour1
First colour, for all vehicles.
@ Primary
Primary colour is set.
@ Secondary
Secondary colour is set.
static void ClearGroup(GroupID group)
Clear the group of all backups having this group ID.
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static T * Create(Targs &&... args)
Creates a new T-object in the associated pool.
static Titem * Get(auto index)
Returns Titem with given index.
static bool CanAllocateItem(size_t n=1)
Helper functions so we can use PoolItem::Function() instead of _poolitem_pool.Function()
static bool IsValidID(auto index)
Tests whether given index can be used to get valid (non-nullptr) Titem.
const Tindex index
Index of this pool item.
static Titem * GetIfValid(auto index)
Returns Titem with given index.
Base class for all pools.
T * Next() const
Get next vehicle in the chain.
static T * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
'Train' is either a loco or a wagon.
The information about a vehicle list.
WindowNumber ToWindowNumber() const
Pack a VehicleListIdentifier in 32 bits so it can be used as unique WindowNumber.
EngineID engine_type
The type of engine used for this vehicle.
TimerGameEconomy::Date economy_age
Age in economy days.
GroupID group_id
Index of group Pool array.
bool IsEngineCountable() const
Check if a vehicle is counted in num_engines in each company struct.
Vehicle * Next() const
Get the next vehicle of this vehicle.
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Money GetDisplayProfitLastYear() const
Gets the profit vehicle had last year.
bool IsFrontEngine() const
Check if the vehicle is a front engine.
Vehicle * FirstShared() const
Get the first vehicle of this vehicle chain.
TileIndex tile
Current tile index.
Owner owner
Which company owns the vehicle?
@ Clear
A tile without any structures, i.e. grass, rocks, farm fields etc.
Base for the train class.
Functions related to vehicles.
static const TimerGameEconomy::Date VEHICLE_PROFIT_MIN_AGE
Only vehicles older than this have a meaningful profit.
bool IsCompanyBuildableVehicleType(VehicleType type)
Is the given vehicle type buildable by a company?
WindowClass GetWindowClassForVehicleType(VehicleType vt)
Get WindowClass for vehicle list of given vehicle type.
VehicleType
Available vehicle types.
@ VEH_ROAD
Road vehicle type.
@ VEH_AIRCRAFT
Aircraft vehicle type.
@ VEH_SHIP
Ship vehicle type.
@ VEH_TRAIN
Train vehicle type.
@ VEH_COMPANY_END
Last company-ownable type.
bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
Generate a list of vehicles based on window type.
Functions and type for generating vehicle lists.
std::vector< const Vehicle * > VehicleList
A list of vehicles.
@ VL_GROUP_LIST
Index is the group.
void CloseWindowById(WindowClass cls, WindowNumber number, bool force, int data)
Close a window by its class and window number (if it is open).
void InvalidateWindowData(WindowClass cls, WindowNumber number, int data, bool gui_scope)
Mark window data of the window of a given class and specific window number as invalid (in need of re-...
void SetWindowDirty(WindowClass cls, WindowNumber number)
Mark window as dirty (in need of repainting)
void InvalidateWindowClassesData(WindowClass cls, int data, bool gui_scope)
Mark window data of all windows of a given class as invalid (in need of re-computing) Note that by de...
@ WC_COMPANY_COLOUR
Company colour selection; Window numbers:
@ WC_VEHICLE_DEPOT
Depot view; Window numbers:
@ WC_REPLACE_VEHICLE
Replace vehicle window; Window numbers:
@ WC_VEHICLE_DETAILS
Vehicle details; Window numbers:
@ WC_VEHICLE_VIEW
Vehicle view; Window numbers: