OpenTTD Source: src/vehiclelist.cpp Source File

OpenTTD Source 13.1

vehiclelist.cpp
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 #include "stdafx.h"
11 #include "train.h"
12 #include "vehiclelist.h"
13 #include "group.h"
14
15 #include "safeguards.h"
16
21 uint32 VehicleListIdentifier::Pack() const
22 {
23 byte c = this->company == OWNER_NONE ? 0xF : (byte)this->company;
24 assert(c < (1 << 4));
25 assert(this->vtype < (1 << 2));
26 assert(this->index < (1 << 20));
27 assert(this->type < VLT_END);
28 static_assert(VLT_END <= (1 << 3));
29
30 return c << 28 | this->type << 23 | this->vtype << 26 | this->index;
31 }
32
38 bool VehicleListIdentifier::UnpackIfValid(uint32 data)
39 {
40 byte c = GB(data, 28, 4);
41 this->company = c == 0xF ? OWNER_NONE : (CompanyID)c;
42 this->type = (VehicleListType)GB(data, 23, 3);
43 this->vtype = (VehicleType)GB(data, 26, 2);
44 this->index = GB(data, 0, 20);
45
46 return this->type < VLT_END;
47 }
48
53 /* static */ VehicleListIdentifier VehicleListIdentifier::UnPack(uint32 data)
54 {
55 VehicleListIdentifier result;
56 [[maybe_unused]] bool ret = result.UnpackIfValid(data);
57 assert(ret);
58 return result;
59 }
60
69 void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
70 {
71 engines->clear();
72 if (wagons != nullptr && wagons != engines) wagons->clear();
73
74 for (const Vehicle *v : Vehicle::Iterate()) {
75 /* General tests for all vehicle types */
76 if (v->type != type) continue;
77 if (v->tile != tile) continue;
78
79 switch (type) {
80 case VEH_TRAIN: {
81 const Train *t = Train::From(v);
82 if (t->IsArticulatedPart() || t->IsRearDualheaded()) continue;
83 if (!t->IsInDepot()) continue;
84 if (wagons != nullptr && t->First()->IsFreeWagon()) {
85 if (individual_wagons || t->IsFreeWagon()) wagons->push_back(t);
86 continue;
87 }
88 if (!t->IsPrimaryVehicle()) continue;
89 break;
90 }
91
92 default:
93 if (!v->IsPrimaryVehicle()) continue;
94 if (!v->IsInDepot()) continue;
95 break;
96 }
97
98 engines->push_back(v);
99 }
100
101 /* Ensure the lists are not wasting too much space. If the lists are fresh
102 * (i.e. built within a command) then this will actually do nothing. */
103 engines->shrink_to_fit();
104 if (wagons != nullptr && wagons != engines) wagons->shrink_to_fit();
105 }
106
113 bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
114 {
115 list->clear();
116
117 switch (vli.type) {
118 case VL_STATION_LIST:
119 for (const Vehicle *v : Vehicle::Iterate()) {
120 if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
121 for (const Order *order : v->Orders()) {
122 if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_IMPLICIT))
123 && order->GetDestination() == vli.index) {
124 list->push_back(v);
125 break;
126 }
127 }
128 }
129 }
130 break;
131
132 case VL_SHARED_ORDERS: {
133 /* Add all vehicles from this vehicle's shared order list */
134 const Vehicle *v = Vehicle::GetIfValid(vli.index);
135 if (v == nullptr || v->type != vli.vtype || !v->IsPrimaryVehicle()) return false;
136
137 for (; v != nullptr; v = v->NextShared()) {
138 list->push_back(v);
139 }
140 break;
141 }
142
143 case VL_GROUP_LIST:
144 if (vli.index != ALL_GROUP) {
145 for (const Vehicle *v : Vehicle::Iterate()) {
146 if (v->type == vli.vtype && v->IsPrimaryVehicle() &&
147 v->owner == vli.company && GroupIsInGroup(v->group_id, vli.index)) {
148 list->push_back(v);
149 }
150 }
151 break;
152 }
153 FALLTHROUGH;
154
155 case VL_STANDARD:
156 for (const Vehicle *v : Vehicle::Iterate()) {
157 if (v->type == vli.vtype && v->owner == vli.company && v->IsPrimaryVehicle()) {
158 list->push_back(v);
159 }
160 }
161 break;
162
163 case VL_DEPOT_LIST:
164 for (const Vehicle *v : Vehicle::Iterate()) {
165 if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
166 for (const Order *order : v->Orders()) {
167 if (order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.index) {
168 list->push_back(v);
169 break;
170 }
171 }
172 }
173 }
174 break;
175
176 default: return false;
177 }
178
179 list->shrink_to_fit();
180 return true;
181 }
Train::IsInDepot
bool IsInDepot() const override
Check whether the vehicle is in the depot.
Definition: train.h:123
VehicleListType
VehicleListType
Vehicle List type flags.
Definition: vehiclelist.h:19
Train::IsPrimaryVehicle
bool IsPrimaryVehicle() const override
Whether this is the primary vehicle in the chain.
Definition: train.h:116
VehicleList
std::vector< const Vehicle * > VehicleList
A list of vehicles.
Definition: vehiclelist.h:55
VehicleListIdentifier::company
CompanyID company
The company associated with this list.
Definition: vehiclelist.h:32
GB
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
train.h
Pool::PoolItem<&_vehicle_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:348
VehicleListIdentifier
The information about a vehicle list.
Definition: vehiclelist.h:29
vehiclelist.h
VehicleListIdentifier::UnpackIfValid
bool UnpackIfValid(uint32 data)
Unpack a VehicleListIdentifier from a single uint32.
Definition: vehiclelist.cpp:38
group.h
TileIndex
The index/ID of a Tile.
Definition: tile_type.h:85
GroundVehicle::IsRearDualheaded
bool IsRearDualheaded() const
Tell if we are dealing with the rear end of a multiheaded engine.
Definition: ground_vehicle.hpp:334
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:224
Vehicle::IsPrimaryVehicle
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition: vehicle_base.h:460
Owner
Owner
Enum for all companies/owners.
Definition: company_type.h:18
Vehicle::IsArticulatedPart
bool IsArticulatedPart() const
Check if the vehicle is an articulated part of an engine.
Definition: vehicle_base.h:922
ALL_GROUP
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:16
VehicleListIdentifier::Pack
uint32 Pack() const
Pack a VehicleListIdentifier in a single uint32.
Definition: vehiclelist.cpp:21
ODATFB_NEAREST_DEPOT
@ ODATFB_NEAREST_DEPOT
Send the vehicle to the nearest depot.
Definition: order_type.h:105
BuildDepotVehicleList
void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
Generate a list of vehicles inside a depot.
Definition: vehiclelist.cpp:69
GenerateVehicleSortList
bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
Generate a list of vehicles based on window type.
Definition: vehiclelist.cpp:113
safeguards.h
Train
'Train' is either a loco or a wagon.
Definition: train.h:88
stdafx.h
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
VehicleListIdentifier::index
uint32 index
A vehicle list type specific index.
Definition: vehiclelist.h:33
Pool::PoolItem<&_vehicle_pool >::Iterate
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
Definition: pool_type.hpp:386
SpecializedVehicle< Train, Type >::From
static Train * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition: vehicle_base.h:1183
VehicleListIdentifier::vtype
VehicleType vtype
The vehicle type associated with this list.
Definition: vehiclelist.h:31
Vehicle::NextShared
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Definition: vehicle_base.h:692
OWNER_NONE
@ OWNER_NONE
The tile has no ownership.
Definition: company_type.h:25
VehicleListIdentifier::type
VehicleListType type
The type of vehicle list.
Definition: vehiclelist.h:30
VehicleListIdentifier::UnPack
static VehicleListIdentifier UnPack(uint32 data)
Decode a packed vehicle list identifier into a new one.
Definition: vehiclelist.cpp:53
GroundVehicle::IsFreeWagon
bool IsFreeWagon() const
Check if the vehicle is a free wagon (got no engine in front of it).
Definition: ground_vehicle.hpp:310
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:52
SpecializedVehicle::First
T * First() const
Get the first vehicle in the chain.
Definition: vehicle_base.h:1080
Order
Definition: order_base.h:36
GroupIsInGroup
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:850