OpenTTD Source: src/group.h Source File

OpenTTD Source 14.1

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 <http://www.gnu.org/licenses/>.
6 */
7
10 #ifndef GROUP_H
11 #define GROUP_H
12
13 #include "group_type.h"
14 #include "core/pool_type.hpp"
15 #include "company_type.h"
16 #include "vehicle_type.h"
17 #include "engine_type.h"
18 #include "livery.h"
19
20 typedef Pool<Group, GroupID, 16, 64000> GroupPool;
21 extern GroupPool _group_pool;
22
24 struct GroupStatistics {
25 Money profit_last_year;
26 Money profit_last_year_min_age;
27 std::map<EngineID, uint16_t> num_engines;
28 uint16_t num_vehicle;
29 uint16_t num_vehicle_min_age;
30 bool autoreplace_defined;
31 bool autoreplace_finished;
32
33 void Clear();
34
35 void ClearProfits()
36 {
37 this->profit_last_year = 0;
38
39 this->num_vehicle_min_age = 0;
40 this->profit_last_year_min_age = 0;
41 }
42
43 void ClearAutoreplace()
44 {
45 this->autoreplace_defined = false;
46 this->autoreplace_finished = false;
47 }
48
49 uint16_t GetNumEngines(EngineID engine) const;
50
51 static GroupStatistics &Get(CompanyID company, GroupID id_g, VehicleType type);
52 static GroupStatistics &Get(const Vehicle *v);
53 static GroupStatistics &GetAllGroup(const Vehicle *v);
54
55 static void CountVehicle(const Vehicle *v, int delta);
56 static void CountEngine(const Vehicle *v, int delta);
57 static void AddProfitLastYear(const Vehicle *v);
58 static void VehicleReachedMinAge(const Vehicle *v);
59
60 static void UpdateProfits();
61 static void UpdateAfterLoad();
62 static void UpdateAutoreplace(CompanyID company);
63 };
64
65 enum GroupFlags : uint8_t {
66 GF_REPLACE_PROTECTION,
67 GF_REPLACE_WAGON_REMOVAL,
68 GF_END,
69 };
70
72 struct Group : GroupPool::PoolItem<&_group_pool> {
73 std::string name;
74 Owner owner;
75 VehicleType vehicle_type;
76
77 uint8_t flags;
78 Livery livery;
79 GroupStatistics statistics;
80
81 bool folded;
82
83 GroupID parent;
84
85 Group(CompanyID owner = INVALID_COMPANY);
86 };
87
88
89 inline bool IsDefaultGroupID(GroupID index)
90 {
91 return index == DEFAULT_GROUP;
92 }
93
99 inline bool IsAllGroupID(GroupID id_g)
100 {
101 return id_g == ALL_GROUP;
102 }
103
104
105 uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e);
106 uint GetGroupNumVehicle(CompanyID company, GroupID id_g, VehicleType type);
107 uint GetGroupNumVehicleMinAge(CompanyID company, GroupID id_g, VehicleType type);
108 Money GetGroupProfitLastYearMinAge(CompanyID company, GroupID id_g, VehicleType type);
109
110 void SetTrainGroupID(Train *v, GroupID grp);
111 void UpdateTrainGroupID(Train *v);
112 void RemoveAllGroupsForCompany(const CompanyID company);
113 bool GroupIsInGroup(GroupID search, GroupID group);
114 void UpdateCompanyGroupLiveries(const Company *c);
115
116 #endif /* GROUP_H */
SetTrainGroupID
void SetTrainGroupID(Train *v, GroupID grp)
Affect the groupID of a train to new_g.
Definition: group_cmd.cpp:743
_group_pool
GroupPool _group_pool
Pool of groups.
GroupFlags
GroupFlags
Definition: group.h:65
GroupStatistics::CountEngine
static void CountEngine(const Vehicle *v, int delta)
Update num_engines when adding/removing an engine.
Definition: group_cmd.cpp:158
Group::parent
GroupID parent
Parent group.
Definition: group.h:83
GroupStatistics::num_engines
std::map< EngineID, uint16_t > num_engines
Caches the number of engines of each type the company owns.
Definition: group.h:27
GroupStatistics::CountVehicle
static void CountVehicle(const Vehicle *v, int delta)
Update num_vehicle when adding or removing a vehicle.
Definition: group_cmd.cpp:133
GroupStatistics::VehicleReachedMinAge
static void VehicleReachedMinAge(const Vehicle *v)
Add a vehicle to the profit sum of its group.
Definition: group_cmd.cpp:180
GroupStatistics::autoreplace_finished
bool autoreplace_finished
Have all autoreplacement finished?
Definition: group.h:31
Group::livery
Livery livery
Custom colour scheme for vehicles in this group.
Definition: group.h:78
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:849
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:240
GroupStatistics::profit_last_year
Money profit_last_year
Sum of profits for all vehicles.
Definition: group.h:25
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
Group::vehicle_type
VehicleType vehicle_type
Vehicle type of the group.
Definition: group.h:75
ALL_GROUP
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:16
Group
Group data.
Definition: group.h:72
GroupStatistics
Statistics and caches on the vehicles in a group.
Definition: group.h:24
UpdateTrainGroupID
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
Definition: group_cmd.cpp:771
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:832
Group::statistics
GroupStatistics statistics
NOSAVE: Statistics and caches on the vehicles in the group.
Definition: group.h:79
Group::owner
Owner owner
Group Owner.
Definition: group.h:74
GroupStatistics::UpdateAutoreplace
static void UpdateAutoreplace(CompanyID company)
Update autoreplace_defined and autoreplace_finished of all statistics of a company.
Definition: group_cmd.cpp:221
Train
'Train' is either a loco or a wagon.
Definition: train.h:89
GroupStatistics::Get
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
Definition: group_cmd.cpp:63
DEFAULT_GROUP
static const GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition: group_type.h:17
GroupStatistics::GetAllGroup
static GroupStatistics & GetAllGroup(const Vehicle *v)
Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
Definition: group_cmd.cpp:93
GroupStatistics::profit_last_year_min_age
Money profit_last_year_min_age
Sum of profits for vehicles considered for profit statistics.
Definition: group.h:26
livery.h
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
GroupStatistics::num_vehicle
uint16_t num_vehicle
Number of vehicles.
Definition: group.h:28
GroupStatistics::autoreplace_defined
bool autoreplace_defined
Are any autoreplace rules set?
Definition: group.h:30
IsAllGroupID
bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
Definition: group.h:99
vehicle_type.h
Pool
Base class for all pools.
Definition: pool_type.hpp:80
Group::flags
uint8_t flags
Group flags.
Definition: group.h:77
engine_type.h
GroupStatistics::UpdateProfits
static void UpdateProfits()
Recompute the profits for all groups.
Definition: group_cmd.cpp:194
GroupID
uint16_t GroupID
Type for all group identifiers.
Definition: group_type.h:13
Group::name
std::string name
Group Name.
Definition: group.h:73
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:797
GroupStatistics::Clear
void Clear()
Clear all caches.
Definition: group_cmd.cpp:33
GF_REPLACE_PROTECTION
@ GF_REPLACE_PROTECTION
If set to true, the global autoreplace has no effect on the group.
Definition: group.h:66
OverflowSafeInt< int64_t >
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:815
Group::folded
bool folded
NOSAVE: Is this group folded in the group view?
Definition: group.h:81
INVALID_COMPANY
@ INVALID_COMPANY
An invalid company.
Definition: company_type.h:30
EngineID
uint16_t EngineID
Unique identification number of an engine.
Definition: engine_type.h:21
pool_type.hpp
GroupStatistics::GetNumEngines
uint16_t GetNumEngines(EngineID engine) const
Get number of vehicles of a specific engine ID.
Definition: group_cmd.cpp:49
Company
Definition: company_base.h:129
group_type.h
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:237
Livery
Information about a particular livery.
Definition: livery.h:78
company_type.h
UpdateCompanyGroupLiveries
void UpdateCompanyGroupLiveries(const Company *c)
Update group liveries for a company.
Definition: group_cmd.cpp:311
GroupStatistics::UpdateAfterLoad
static void UpdateAfterLoad()
Update all caches after loading a game, changing NewGRF, etc.
Definition: group_cmd.cpp:101
GF_REPLACE_WAGON_REMOVAL
@ GF_REPLACE_WAGON_REMOVAL
If set, autoreplace will perform wagon removal on vehicles in this group.
Definition: group.h:67
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:168
GroupStatistics::num_vehicle_min_age
uint16_t num_vehicle_min_age
Number of vehicles considered for profit statistics;.
Definition: group.h:29
GroupIsInGroup
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:872