OpenTTD

group.h
Go to the documentation of this file.
1 /* $Id: group.h 26450 2014-04-08 21:09:06Z peter1138 $ */
2
3 /*
4 * This file is part of OpenTTD.
5 * 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.
6 * 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.
7 * 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/>.
8 */
9
12 #ifndef GROUP_H
13 #define GROUP_H
14
15 #include "group_type.h"
16 #include "core/pool_type.hpp"
17 #include "company_type.h"
18 #include "vehicle_type.h"
19 #include "engine_type.h"
20
21 typedef Pool<Group, GroupID, 16, 64000> GroupPool;
22 extern GroupPool _group_pool;
23
25 struct GroupStatistics {
26 uint16 num_vehicle;
27 uint16 *num_engines;
28
29 bool autoreplace_defined;
30 bool autoreplace_finished;
31
32 uint16 num_profit_vehicle;
33 Money profit_last_year;
34
35 GroupStatistics();
36 ~GroupStatistics();
37
38 void Clear();
39
40 void ClearProfits()
41 {
42 this->num_profit_vehicle = 0;
43 this->profit_last_year = 0;
44 }
45
46 void ClearAutoreplace()
47 {
48 this->autoreplace_defined = false;
49 this->autoreplace_finished = false;
50 }
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 VehicleReachedProfitAge(const Vehicle *v);
59
60 static void UpdateProfits();
61 static void UpdateAfterLoad();
62 static void UpdateAutoreplace(CompanyID company);
63 };
64
66 struct Group : GroupPool::PoolItem<&_group_pool> {
67 char *name;
68 OwnerByte owner;
69 VehicleTypeByte vehicle_type;
70
71 bool replace_protection;
72 GroupStatistics statistics;
73
74 GroupID parent;
75
76 Group(CompanyID owner = INVALID_COMPANY);
77 ~Group();
78 };
79
80
81 static inline bool IsDefaultGroupID(GroupID index)
82 {
83 return index == DEFAULT_GROUP;
84 }
85
91 static inline bool IsAllGroupID(GroupID id_g)
92 {
93 return id_g == ALL_GROUP;
94 }
95
96 #define FOR_ALL_GROUPS_FROM(var, start) FOR_ALL_ITEMS_FROM(Group, group_index, var, start)
97 #define FOR_ALL_GROUPS(var) FOR_ALL_GROUPS_FROM(var, 0)
98
99
100 uint GetGroupNumEngines(CompanyID company, GroupID id_g, EngineID id_e);
101
102 void SetTrainGroupID(Train *v, GroupID grp);
103 void UpdateTrainGroupID(Train *v);
104 void RemoveVehicleFromGroup(const Vehicle *v);
105 void RemoveAllGroupsForCompany(const CompanyID company);
106 bool GroupIsInGroup(GroupID search, GroupID group);
107
108 extern GroupID _new_group_id;
109
110 #endif /* GROUP_H */
GroupStatistics::UpdateAfterLoad
static void UpdateAfterLoad()
Update all caches after loading a game, changing NewGRF etc.
Definition: group_cmd.cpp:103
Group::replace_protection
bool replace_protection
If set to true, the global autoreplace have no effect on the group.
Definition: group.h:71
Group::statistics
GroupStatistics statistics
NOSAVE: Statistics and caches on the vehicles in the group.
Definition: group.h:72
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:693
engine_type.h
Types related to engines.
GroupStatistics::num_engines
uint16 * num_engines
Caches the number of engines of each type the company owns.
Definition: group.h:27
GroupIsInGroup
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:720
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:212
GroupStatistics::profit_last_year
Money profit_last_year
Sum of profits for all vehicles.
Definition: group.h:33
GroupStatistics::autoreplace_finished
bool autoreplace_finished
Have all autoreplacement finished?
Definition: group.h:30
GroupStatistics::num_profit_vehicle
uint16 num_profit_vehicle
Number of vehicles considered for profit statistics;.
Definition: group.h:32
GroupStatistics::GetAllGroup
static GroupStatistics & GetAllGroup(const Vehicle *v)
Returns the GroupStatistic for the ALL_GROUPO of a vehicle type.
Definition: group_cmd.cpp:95
pool_type.hpp
Defintion of Pool, structure used to access PoolItems, and PoolItem, base structure for Vehicle...
_group_pool
GroupPool _group_pool
Pool of groups.
IsAllGroupID
static bool IsAllGroupID(GroupID id_g)
Checks if a GroupID stands for all vehicles of a company.
Definition: group.h:91
GroupStatistics
Statistics and caches on the vehicles in a group.
Definition: group.h:25
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
Group::name
char * name
Group Name.
Definition: group.h:67
SetTrainGroupID
void SetTrainGroupID(Train *v, GroupID grp)
Affect the groupID of a train to new_g.
Definition: group_cmd.cpp:644
GroupID
uint16 GroupID
Type for all group identifiers.
Definition: group_type.h:15
Group::vehicle_type
VehicleTypeByte vehicle_type
Vehicle type of the group.
Definition: group.h:69
GroupStatistics::Get
static GroupStatistics & Get(CompanyID company, GroupID id_g, VehicleType type)
Returns the GroupStatistics for a specific group.
Definition: group_cmd.cpp:65
GroupStatistics::CountVehicle
static void CountVehicle(const Vehicle *v, int delta)
Update num_vehicle when adding or removing a vehicle.
Definition: group_cmd.cpp:138
GroupStatistics::Clear
void Clear()
Clear all caches.
Definition: group_cmd.cpp:47
GroupStatistics::num_vehicle
uint16 num_vehicle
Number of vehicles.
Definition: group.h:26
UpdateTrainGroupID
void UpdateTrainGroupID(Train *v)
Recalculates the groupID of a train.
Definition: group_cmd.cpp:669
TinyEnumT< Owner >
Pool::PoolItem
Base class for all PoolItems.
Definition: pool_type.hpp:146
SimpleTinyEnumT< VehicleType, byte >
Pool
Base class for all pools.
Definition: pool_type.hpp:83
Train
&#39;Train&#39; is either a loco or a wagon.
Definition: train.h:88
GroupStatistics::UpdateProfits
static void UpdateProfits()
Recompute the profits for all groups.
Definition: group_cmd.cpp:185
DEFAULT_GROUP
static const GroupID DEFAULT_GROUP
Ungrouped vehicles are in this group.
Definition: group_type.h:19
Group::parent
GroupID parent
Parent group.
Definition: group.h:74
EngineID
uint16 EngineID
Unique identification number of an engine.
Definition: engine_type.h:22
GroupStatistics::autoreplace_defined
bool autoreplace_defined
Are any autoreplace rules set?
Definition: group.h:29
vehicle_type.h
Types related to vehicles.
GroupStatistics::UpdateAutoreplace
static void UpdateAutoreplace(CompanyID company)
Update autoreplace_defined and autoreplace_finished of all statistics of a company.
Definition: group_cmd.cpp:212
Group
Group data.
Definition: group.h:66
GroupStatistics::CountEngine
static void CountEngine(const Vehicle *v, int delta)
Update num_engines when adding/removing an engine.
Definition: group_cmd.cpp:161
company_type.h
Types related to companies.
Group::owner
OwnerByte owner
Group Owner.
Definition: group.h:68
RemoveVehicleFromGroup
void RemoveVehicleFromGroup(const Vehicle *v)
Decrease the num_vehicle variable before delete an front engine from a group.
Definition: group_cmd.cpp:630
GroupStatistics::VehicleReachedProfitAge
static void VehicleReachedProfitAge(const Vehicle *v)
Add a vehicle to the profit sum of its group.
Definition: group_cmd.cpp:171
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:20
INVALID_COMPANY
An invalid company.
Definition: company_type.h:32
group_type.h
Types of a group.
OverflowSafeInt< int64, INT64_MAX, INT64_MIN >
ALL_GROUP
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:18