OpenTTD Source: src/group.h Source File

OpenTTD Source 15.3

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
10#ifndef GROUP_H
11#define GROUP_H
12
13#include "group_type.h"
14#include "core/flatset_type.hpp"
15#include "core/pool_type.hpp"
16#include "company_type.h"
17#include "vehicle_type.h"
18#include "engine_type.h"
19#include "livery.h"
20
21using GroupPool = Pool<Group, GroupID, 16>;
22extern GroupPool _group_pool;
23
25 struct GroupStatistics {
26 Money profit_last_year = 0;
27 Money profit_last_year_min_age = 0;
28 std::map<EngineID, uint16_t> num_engines{};
29 uint16_t num_vehicle = 0;
30 uint16_t num_vehicle_min_age = 0;
31 bool autoreplace_defined = false;
32 bool autoreplace_finished = false;
33
34 void Clear();
35
36 void ClearProfits()
37 {
38 this->profit_last_year = 0;
39
40 this->num_vehicle_min_age = 0;
41 this->profit_last_year_min_age = 0;
42 }
43
44 void ClearAutoreplace()
45 {
46 this->autoreplace_defined = false;
47 this->autoreplace_finished = false;
48 }
49
50 uint16_t GetNumEngines(EngineID engine) const;
51
52 static GroupStatistics &Get(CompanyID company, GroupID id_g, VehicleType type);
53 static GroupStatistics &Get(const Vehicle *v);
54 static GroupStatistics &GetAllGroup(const Vehicle *v);
55
56 static void CountVehicle(const Vehicle *v, int delta);
57 static void CountEngine(const Vehicle *v, int delta);
58 static void AddProfitLastYear(const Vehicle *v);
59 static void VehicleReachedMinAge(const Vehicle *v);
60
61 static void UpdateProfits();
62 static void UpdateAfterLoad();
63 static void UpdateAutoreplace(CompanyID company);
64};
65
66 enum class GroupFlag : uint8_t {
67 ReplaceProtection = 0,
68 ReplaceWagonRemoval = 1,
69};
70using GroupFlags = EnumBitSet<GroupFlag, uint8_t>;
71
73 struct Group : GroupPool::PoolItem<&_group_pool> {
74 std::string name{};
75 Owner owner = INVALID_OWNER;
76 VehicleType vehicle_type = VEH_INVALID;
77
78 GroupFlags flags{};
79 Livery livery{};
80 GroupStatistics statistics{};
81
82 FlatSet<GroupID> children;
83 bool folded = false;
84
85 GroupID parent = GroupID::Invalid();
86 uint16_t number = 0;
87
88 Group() {}
89 Group(CompanyID owner, VehicleType vehicle_type) : owner(owner), vehicle_type(vehicle_type) {}
90};
91
92
93inline bool IsDefaultGroupID(GroupID index)
94{
95 return index == DEFAULT_GROUP;
96}
97
103 inline bool IsAllGroupID(GroupID id_g)
104{
105 return id_g == ALL_GROUP;
106}
107
108
109void UpdateGroupChildren();
110uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e);
111uint GetGroupNumVehicle(CompanyID company, GroupID id_g, VehicleType type);
112uint GetGroupNumVehicleMinAge(CompanyID company, GroupID id_g, VehicleType type);
113 Money GetGroupProfitLastYearMinAge(CompanyID company, GroupID id_g, VehicleType type);
114
115void SetTrainGroupID(Train *v, GroupID grp);
116void UpdateTrainGroupID(Train *v);
117void RemoveAllGroupsForCompany(const CompanyID company);
118bool GroupIsInGroup(GroupID search, GroupID group);
119void UpdateCompanyGroupLiveries(const Company *c);
120
121#endif /* GROUP_H */
EnumBitSet< GroupFlag, uint8_t >
FlatSet
Flat set implementation that uses a sorted vector for storage.
Definition flatset_type.hpp:20
OverflowSafeInt< int64_t >
company_type.h
Types related to companies.
INVALID_OWNER
static constexpr Owner INVALID_OWNER
An invalid owner.
Definition company_type.h:30
engine_type.h
Types related to engines.
flatset_type.hpp
Flat set container implementation.
GetGroupProfitLastYearMinAge
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...
Definition group_cmd.cpp:882
UpdateCompanyGroupLiveries
void UpdateCompanyGroupLiveries(const Company *c)
Update group liveries for a company.
Definition group_cmd.cpp:329
UpdateGroupChildren
void UpdateGroupChildren()
Update children list for each group.
Definition group_cmd.cpp:47
UpdateTrainGroupID
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
Definition group_cmd.cpp:793
GroupFlag
GroupFlag
Definition group.h:66
GroupFlag::ReplaceProtection
@ ReplaceProtection
If set, the global autoreplace has no effect on the group.
GroupFlag::ReplaceWagonRemoval
@ ReplaceWagonRemoval
If set, autoreplace will perform wagon removal on vehicles in this group.
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.
Definition group_cmd.cpp:819
SetTrainGroupID
void SetTrainGroupID(Train *v, GroupID grp)
Affect the groupID of a train to new_g.
Definition group_cmd.cpp:765
_group_pool
GroupPool _group_pool
Pool of groups.
IsAllGroupID
bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
Definition group.h:103
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.
Definition group_cmd.cpp:840
GetGroupNumVehicleMinAge
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...
Definition group_cmd.cpp:861
GroupIsInGroup
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition group_cmd.cpp:909
group_type.h
Types of a group.
DEFAULT_GROUP
static constexpr GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition group_type.h:18
ALL_GROUP
static constexpr GroupID ALL_GROUP
All vehicles are in this group.
Definition group_type.h:17
livery.h
Functions/types related to livery colours.
pool_type.hpp
Definition of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle,...
Company
Definition company_base.h:127
GroupStatistics
Statistics and caches on the vehicles in a group.
Definition group.h:25
GroupStatistics::CountVehicle
static void CountVehicle(const Vehicle *v, int delta)
Update num_vehicle when adding or removing a vehicle.
Definition group_cmd.cpp:152
GroupStatistics::AddProfitLastYear
static void AddProfitLastYear(const Vehicle *v)
Add a vehicle's last year profit to the profit sum of its group.
Definition group_cmd.cpp:187
GroupStatistics::VehicleReachedMinAge
static void VehicleReachedMinAge(const Vehicle *v)
Add a vehicle to the profit sum of its group.
Definition group_cmd.cpp:199
GroupStatistics::GetNumEngines
uint16_t GetNumEngines(EngineID engine) const
Get number of vehicles of a specific engine ID.
Definition group_cmd.cpp:68
GroupStatistics::profit_last_year
Money profit_last_year
Sum of profits for all vehicles.
Definition group.h:26
GroupStatistics::GetAllGroup
static GroupStatistics & GetAllGroup(const Vehicle *v)
Returns the GroupStatistic for the ALL_GROUP of a vehicle type.
Definition group_cmd.cpp:112
GroupStatistics::UpdateAfterLoad
static void UpdateAfterLoad()
Update all caches after loading a game, changing NewGRF, etc.
Definition group_cmd.cpp:120
GroupStatistics::CountEngine
static void CountEngine(const Vehicle *v, int delta)
Update num_engines when adding/removing an engine.
Definition group_cmd.cpp:177
GroupStatistics::num_vehicle
uint16_t num_vehicle
Number of vehicles.
Definition group.h:29
GroupStatistics::profit_last_year_min_age
Money profit_last_year_min_age
Sum of profits for vehicles considered for profit statistics.
Definition group.h:27
GroupStatistics::UpdateAutoreplace
static void UpdateAutoreplace(CompanyID company)
Update autoreplace_defined and autoreplace_finished of all statistics of a company.
Definition group_cmd.cpp:240
GroupStatistics::UpdateProfits
static void UpdateProfits()
Recompute the profits for all groups.
Definition group_cmd.cpp:213
GroupStatistics::Clear
void Clear()
Clear all caches.
Definition group_cmd.cpp:33
GroupStatistics::autoreplace_defined
bool autoreplace_defined
Are any autoreplace rules set?
Definition group.h:31
GroupStatistics::Get
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
Definition group_cmd.cpp:82
GroupStatistics::autoreplace_finished
bool autoreplace_finished
Have all autoreplacement finished?
Definition group.h:32
GroupStatistics::num_engines
std::map< EngineID, uint16_t > num_engines
Caches the number of engines of each type the company owns.
Definition group.h:28
GroupStatistics::num_vehicle_min_age
uint16_t num_vehicle_min_age
Number of vehicles considered for profit statistics;.
Definition group.h:30
Group
Group data.
Definition group.h:73
Group::flags
GroupFlags flags
Group flags.
Definition group.h:78
Group::livery
Livery livery
Custom colour scheme for vehicles in this group.
Definition group.h:79
Group::parent
GroupID parent
Parent group.
Definition group.h:85
Group::statistics
GroupStatistics statistics
NOSAVE: Statistics and caches on the vehicles in the group.
Definition group.h:80
Group::vehicle_type
VehicleType vehicle_type
Vehicle type of the group.
Definition group.h:76
Group::folded
bool folded
NOSAVE: Is this group folded in the group view?
Definition group.h:83
Group::name
std::string name
Group Name.
Definition group.h:74
Group::number
uint16_t number
Per-company group number.
Definition group.h:86
Group::owner
Owner owner
Group Owner.
Definition group.h:75
Group::children
FlatSet< GroupID > children
NOSAVE: child groups belonging to this group.
Definition group.h:82
Livery
Information about a particular livery.
Definition livery.h:79
PoolID< uint16_t, struct EngineIDTag, 64000, 0xFFFF >
Pool::PoolItem
Base class for all PoolItems.
Definition pool_type.hpp:285
Pool
Base class for all pools.
Definition pool_type.hpp:133
Train
'Train' is either a loco or a wagon.
Definition train.h:91
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205
vehicle_type.h
Types related to vehicles.
VehicleType
VehicleType
Available vehicle types.
Definition vehicle_type.h:22
VEH_INVALID
@ VEH_INVALID
Non-existing type of vehicle.
Definition vehicle_type.h:36