OpenTTD Source  14.0-beta1
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 "vehiclelist_func.h"
14 #include "group.h"
15 
16 #include "safeguards.h"
17 
23 {
24  byte c = this->company == OWNER_NONE ? 0xF : (byte)this->company;
25  assert(c < (1 << 4));
26  assert(this->vtype < (1 << 2));
27  assert(this->index < (1 << 20));
28  assert(this->type < VLT_END);
29  static_assert(VLT_END <= (1 << 3));
30 
31  return c << 28 | this->type << 23 | this->vtype << 26 | this->index;
32 }
33 
40 {
41  byte c = GB(data, 28, 4);
42  this->company = c == 0xF ? OWNER_NONE : (CompanyID)c;
43  this->type = (VehicleListType)GB(data, 23, 3);
44  this->vtype = (VehicleType)GB(data, 26, 2);
45  this->index = GB(data, 0, 20);
46 
47  return this->type < VLT_END;
48 }
49 
55 {
56  VehicleListIdentifier result;
57  [[maybe_unused]] bool ret = result.UnpackIfValid(data);
58  assert(ret);
59  return result;
60 }
61 
70 void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
71 {
72  engines->clear();
73  if (wagons != nullptr && wagons != engines) wagons->clear();
74 
75  for (const Vehicle *v : Vehicle::Iterate()) {
76  /* General tests for all vehicle types */
77  if (v->type != type) continue;
78  if (v->tile != tile) continue;
79 
80  switch (type) {
81  case VEH_TRAIN: {
82  const Train *t = Train::From(v);
83  if (t->IsArticulatedPart() || t->IsRearDualheaded()) continue;
84  if (!t->IsInDepot()) continue;
85  if (wagons != nullptr && t->First()->IsFreeWagon()) {
86  if (individual_wagons || t->IsFreeWagon()) wagons->push_back(t);
87  continue;
88  }
89  if (!t->IsPrimaryVehicle()) continue;
90  break;
91  }
92 
93  default:
94  if (!v->IsPrimaryVehicle()) continue;
95  if (!v->IsInDepot()) continue;
96  break;
97  }
98 
99  engines->push_back(v);
100  }
101 
102  /* Ensure the lists are not wasting too much space. If the lists are fresh
103  * (i.e. built within a command) then this will actually do nothing. */
104  engines->shrink_to_fit();
105  if (wagons != nullptr && wagons != engines) wagons->shrink_to_fit();
106 }
107 
115 {
116  list->clear();
117 
118  switch (vli.type) {
119  case VL_STATION_LIST:
121  [&vli](const Vehicle *v) { return v->type == vli.vtype; },
122  [&vli](const Order *order) { return (order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_IMPLICIT)) && order->GetDestination() == vli.index; },
123  [&list](const Vehicle *v) { list->push_back(v); }
124  );
125  break;
126 
127  case VL_SHARED_ORDERS: {
128  /* Add all vehicles from this vehicle's shared order list */
129  const Vehicle *v = Vehicle::GetIfValid(vli.index);
130  if (v == nullptr || v->type != vli.vtype || !v->IsPrimaryVehicle()) return false;
131 
132  for (; v != nullptr; v = v->NextShared()) {
133  list->push_back(v);
134  }
135  break;
136  }
137 
138  case VL_GROUP_LIST:
139  if (vli.index != ALL_GROUP) {
140  for (const Vehicle *v : Vehicle::Iterate()) {
141  if (v->type == vli.vtype && v->IsPrimaryVehicle() &&
142  v->owner == vli.company && GroupIsInGroup(v->group_id, vli.index)) {
143  list->push_back(v);
144  }
145  }
146  break;
147  }
148  [[fallthrough]];
149 
150  case VL_STANDARD:
151  for (const Vehicle *v : Vehicle::Iterate()) {
152  if (v->type == vli.vtype && v->owner == vli.company && v->IsPrimaryVehicle()) {
153  list->push_back(v);
154  }
155  }
156  break;
157 
158  case VL_DEPOT_LIST:
160  [&vli](const Vehicle *v) { return v->type == vli.vtype; },
161  [&vli](const Order *order) { return order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.index; },
162  [&list](const Vehicle *v) { list->push_back(v); }
163  );
164  break;
165 
166  default: return false;
167  }
168 
169  list->shrink_to_fit();
170  return true;
171 }
Train::IsInDepot
bool IsInDepot() const override
Check whether the vehicle is in the depot.
Definition: train.h:124
VehicleListType
VehicleListType
Vehicle List type flags.
Definition: vehiclelist.h:18
Train::IsPrimaryVehicle
bool IsPrimaryVehicle() const override
Whether this is the primary vehicle in the chain.
Definition: train.h:117
VehicleList
std::vector< const Vehicle * > VehicleList
A list of vehicles.
Definition: vehiclelist.h:54
VehicleListIdentifier::company
CompanyID company
The company associated with this list.
Definition: vehiclelist.h:31
train.h
Pool::PoolItem<&_vehicle_pool >::GetIfValid
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:346
VehicleListIdentifier
The information about a vehicle list.
Definition: vehiclelist.h:28
vehiclelist.h
GB
constexpr static debug_inline uint GB(const T x, const uint8_t s, const uint8_t n)
Fetch n bits from x, started at bit s.
Definition: bitmath_func.hpp:32
Vehicle::group_id
GroupID group_id
Index of group Pool array.
Definition: vehicle_base.h:357
group.h
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
GroundVehicle::IsRearDualheaded
bool IsRearDualheaded() const
Tell if we are dealing with the rear end of a multiheaded engine.
Definition: ground_vehicle.hpp:333
Vehicle
Vehicle data structure.
Definition: vehicle_base.h:240
Vehicle::IsPrimaryVehicle
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition: vehicle_base.h:472
Vehicle::owner
Owner owner
Which company owns the vehicle?
Definition: vehicle_base.h:304
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:940
ALL_GROUP
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:16
VehicleListIdentifier::index
uint32_t index
A vehicle list type specific index.
Definition: vehiclelist.h:32
ODATFB_NEAREST_DEPOT
@ ODATFB_NEAREST_DEPOT
Send the vehicle to the nearest depot.
Definition: order_type.h:105
VehicleListIdentifier::Pack
uint32_t Pack() const
Pack a VehicleListIdentifier in a single uint32.
Definition: vehiclelist.cpp:22
FindVehiclesWithOrder
void FindVehiclesWithOrder(VehiclePredicate veh_pred, OrderPredicate ord_pred, VehicleFunc veh_func)
Find vehicles matching an order.
Definition: vehiclelist_func.h:24
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:70
GenerateVehicleSortList
bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
Generate a list of vehicles based on window type.
Definition: vehiclelist.cpp:114
safeguards.h
Train
'Train' is either a loco or a wagon.
Definition: train.h:89
stdafx.h
VehicleType
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
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:384
SpecializedVehicle< Train, Type >::From
static Train * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition: vehicle_base.h:1201
VehicleListIdentifier::vtype
VehicleType vtype
The vehicle type associated with this list.
Definition: vehiclelist.h:30
Vehicle::NextShared
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Definition: vehicle_base.h:709
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:29
GroundVehicle::IsFreeWagon
bool IsFreeWagon() const
Check if the vehicle is a free wagon (got no engine in front of it).
Definition: ground_vehicle.hpp:309
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition: vehicle_type.h:24
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition: vehicle_type.h:51
SpecializedVehicle::First
T * First() const
Get the first vehicle in the chain.
Definition: vehicle_base.h:1098
VehicleListIdentifier::UnPack
static VehicleListIdentifier UnPack(uint32_t data)
Decode a packed vehicle list identifier into a new one.
Definition: vehiclelist.cpp:54
Order
Definition: order_base.h:36
VehicleListIdentifier::UnpackIfValid
bool UnpackIfValid(uint32_t data)
Unpack a VehicleListIdentifier from a single uint32.
Definition: vehiclelist.cpp:39
vehiclelist_func.h
GroupIsInGroup
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:872