OpenTTD Source 20261129-master-g72a8bdc861
group.h
Go to the documentation of this file.
1/*
2 * This file is part of OpenTTD.
3 * OpenTTD is free software; you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 2.
4 * OpenTTD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
5 * See the GNU General Public License for more details. You should have received a copy of the GNU General Public License along with OpenTTD. If not, see <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
6 */
7
9
10#ifndef GROUP_H
11#define GROUP_H
12
13#include "autoreplace_type.h"
14#include "group_type.h"
15#include "core/flatset_type.hpp"
16#include "core/pool_type.hpp"
17#include "company_type.h"
18#include "vehicle_type.h"
19#include "engine_type.h"
20#include "livery.h"
21
22using GroupPool = Pool<Group, GroupID, 16>;
23extern GroupPool _group_pool;
24
27 Money profit_last_year = 0;
29 std::map<EngineID, uint16_t> num_engines{};
30 uint16_t num_vehicle = 0;
31 uint16_t num_vehicle_min_age = 0;
32 bool autoreplace_defined = false;
33 bool autoreplace_finished = false;
34
35 void Clear();
36
37 void ClearProfits()
38 {
39 this->profit_last_year = 0;
40
41 this->num_vehicle_min_age = 0;
42 this->profit_last_year_min_age = 0;
43 }
44
45 void ClearAutoreplace()
46 {
47 this->autoreplace_defined = false;
48 this->autoreplace_finished = false;
49 }
50
51 uint16_t GetNumEngines(EngineID engine) const;
52
53 static GroupStatistics &Get(CompanyID company, GroupID id_g, VehicleType type);
54 static GroupStatistics &Get(const Vehicle *v);
55 static GroupStatistics &GetAllGroup(const Vehicle *v);
56
57 static void CountVehicle(const Vehicle *v, int delta);
58 static void CountEngine(const Vehicle *v, int delta);
59 static void AddProfitLastYear(const Vehicle *v);
60 static void VehicleReachedMinAge(const Vehicle *v);
61
62 static void UpdateProfits();
63 static void UpdateAfterLoad();
64 static void UpdateAutoreplace(CompanyID company);
65};
66
68enum class GroupFlag : uint8_t {
71};
72
75
77struct Group : GroupPool::PoolItem<&_group_pool> {
78 std::string name{};
81
85
87
89 bool folded = false;
90
91 GroupID parent = GroupID::Invalid();
92 uint16_t number = 0;
93
102};
103
104
105inline bool IsDefaultGroupID(GroupID index)
106{
107 return index == DEFAULT_GROUP;
108}
109
115inline bool IsAllGroupID(GroupID id_g)
116{
117 return id_g == ALL_GROUP;
118}
119
120
122uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e);
123uint GetGroupNumVehicle(CompanyID company, GroupID id_g, VehicleType type);
124uint GetGroupNumVehicleMinAge(CompanyID company, GroupID id_g, VehicleType type);
125Money GetGroupProfitLastYearMinAge(CompanyID company, GroupID id_g, VehicleType type);
126
127void SetTrainGroupID(Train *v, GroupID grp);
129void RemoveAllGroupsForCompany(const CompanyID company);
130bool GroupIsInGroup(GroupID search, GroupID group);
132
133#endif /* GROUP_H */
Types related to autoreplacing.
std::vector< EngineRenew > EngineRenewList
A list to group EngineRenew directives together (such as per-company).
Enum-as-bit-set wrapper.
Flat set implementation that uses a sorted vector for storage.
Types related to companies.
static constexpr Owner INVALID_OWNER
An invalid owner.
Types related to engines.
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF > EngineID
Unique identification number of an engine.
Definition engine_type.h:25
Flat set container implementation.
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...
EnumBitSet< GroupFlag, uint8_t > GroupFlags
Bitset of GroupFlag elements.
Definition group.h:74
void UpdateCompanyGroupLiveries(const Company *c)
Update group liveries for a company.
void UpdateGroupChildren()
Update children list for each group.
Definition group_cmd.cpp:48
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
GroupFlag
Configuration flags for a group.
Definition group.h:68
@ ReplaceProtection
If set, the global autoreplace has no effect on the group.
Definition group.h:69
@ ReplaceWagonRemoval
If set, autoreplace will perform wagon removal on vehicles in this group.
Definition group.h:70
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.
void SetTrainGroupID(Train *v, GroupID grp)
Affect the groupID of a train to new_g.
GroupPool _group_pool
Pool of groups.
bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
Definition group.h:115
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.
Types of a group.
static constexpr GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition group_type.h:18
static constexpr GroupID ALL_GROUP
All vehicles are in this group.
Definition group_type.h:17
Functions/types related to livery colours.
@ Vehicle
Vehicle news item. (new engine available).
Definition news_type.h:84
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
Statistics and caches on the vehicles in a group.
Definition group.h:26
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.
Definition group_cmd.cpp:69
Money profit_last_year
Sum of profits for all vehicles.
Definition group.h:27
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.
Definition group.h:30
Money profit_last_year_min_age
Sum of profits for vehicles considered for profit statistics.
Definition group.h:28
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.
void Clear()
Clear all caches.
Definition group_cmd.cpp:34
bool autoreplace_defined
Are any autoreplace rules set?
Definition group.h:32
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
Definition group_cmd.cpp:83
bool autoreplace_finished
Have all autoreplacement finished?
Definition group.h:33
std::map< EngineID, uint16_t > num_engines
Caches the number of engines of each type the company owns.
Definition group.h:29
uint16_t num_vehicle_min_age
Number of vehicles considered for profit statistics;.
Definition group.h:31
EngineRenewList engine_renew_list
Engine renewals of this group.
Definition group.h:84
GroupFlags flags
Group flags.
Definition group.h:82
Livery livery
Custom colour scheme for vehicles in this group.
Definition group.h:83
Group(GroupID index, CompanyID owner=INVALID_OWNER, VehicleType vehicle_type=VehicleType::Invalid)
Create this group.
Definition group.h:100
GroupID parent
Parent group.
Definition group.h:91
GroupStatistics statistics
NOSAVE: Statistics and caches on the vehicles in the group.
Definition group.h:86
VehicleType vehicle_type
Vehicle type of the group.
Definition group.h:80
bool folded
NOSAVE: Is this group folded in the group view?
Definition group.h:89
std::string name
Group Name.
Definition group.h:78
uint16_t number
Per-company group number.
Definition group.h:92
Owner owner
Group Owner.
Definition group.h:79
FlatSet< GroupID > children
NOSAVE: child groups belonging to this group.
Definition group.h:88
Information about a particular livery.
Definition livery.h:82
Base class for all pools.
'Train' is either a loco or a wagon.
Definition train.h:97
Types related to vehicles.
VehicleType
Available vehicle types.
@ Invalid
Non-existing type of vehicle.