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/>.
10 /** @file group.h Base class for groups and group functions. */
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"
21 /** Statistics and caches on the vehicles in a group. */
22 struct GroupStatistics
{
23 uint16 num_vehicle
; ///< Number of vehicles.
24 uint16
*num_engines
; ///< Caches the number of engines of each type the company owns.
26 bool autoreplace_defined
; ///< Are any autoreplace rules set?
27 bool autoreplace_finished
; ///< Have all autoreplacement finished?
29 uint16 num_profit_vehicle
; ///< Number of vehicles considered for profit statistics;
30 Money profit_last_year
; ///< Sum of profits for all vehicles.
39 this->num_profit_vehicle
= 0;
40 this->profit_last_year
= 0;
43 void ClearAutoreplace()
45 this->autoreplace_defined
= false;
46 this->autoreplace_finished
= false;
49 static GroupStatistics
&Get(CompanyID company
, GroupID id_g
, VehicleType type
);
50 static GroupStatistics
&Get(const Vehicle
*v
);
51 static GroupStatistics
&GetAllGroup(const Vehicle
*v
);
53 static void CountVehicle(const Vehicle
*v
, int delta
);
54 static void CountEngine(const Vehicle
*v
, int delta
);
55 static void VehicleReachedProfitAge(const Vehicle
*v
);
57 static void UpdateProfits();
58 static void UpdateAfterLoad();
59 static void UpdateAutoreplace(CompanyID company
);
63 struct Group
: PooledItem
<Group
, GroupID
, 16, 64000> {
64 char *name
; ///< Group Name
65 OwnerByte owner
; ///< Group Owner
66 VehicleTypeByte vehicle_type
; ///< Vehicle type of the group
68 bool replace_protection
; ///< If set to true, the global autoreplace have no effect on the group
69 GroupStatistics statistics
; ///< NOSAVE: Statistics and caches on the vehicles in the group.
71 Group(CompanyID owner
= INVALID_COMPANY
);
76 static inline bool IsDefaultGroupID(GroupID index
)
78 return index
== DEFAULT_GROUP
;
82 * Checks if a GroupID stands for all vehicles of a company
83 * @param id_g The GroupID to check
84 * @return true is id_g is identical to ALL_GROUP
86 static inline bool IsAllGroupID(GroupID id_g
)
88 return id_g
== ALL_GROUP
;
91 #define FOR_ALL_GROUPS_FROM(var, start) FOR_ALL_ITEMS_FROM(Group, group_index, var, start)
92 #define FOR_ALL_GROUPS(var) FOR_ALL_GROUPS_FROM(var, 0)
95 uint
GetGroupNumEngines(CompanyID company
, GroupID id_g
, EngineID id_e
);
97 void SetTrainGroupID(Train
*v
, GroupID grp
);
98 void UpdateTrainGroupID(Train
*v
);
99 void RemoveVehicleFromGroup(const Vehicle
*v
);
100 void RemoveAllGroupsForCompany(const CompanyID company
);
102 extern GroupID _new_group_id
;