OpenTTD
vehiclelist.cpp
Go to the documentation of this file.
1 /* $Id: vehiclelist.cpp 27893 2017-08-13 18:38:42Z frosch $ */
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 #include "stdafx.h"
13 #include "train.h"
14 #include "vehiclelist.h"
15 #include "group.h"
16 
17 #include "safeguards.h"
18 
24 {
25  byte c = this->company == OWNER_NONE ? 0xF : (byte)this->company;
26  assert(c < (1 << 4));
27  assert(this->vtype < (1 << 2));
28  assert(this->index < (1 << 20));
29  assert(this->type < VLT_END);
30  assert_compile(VLT_END <= (1 << 3));
31 
32  return c << 28 | this->type << 23 | this->vtype << 26 | this->index;
33 }
34 
41 {
42  byte c = GB(data, 28, 4);
43  this->company = c == 0xF ? OWNER_NONE : (CompanyID)c;
44  this->type = (VehicleListType)GB(data, 23, 3);
45  this->vtype = (VehicleType)GB(data, 26, 2);
46  this->index = GB(data, 0, 20);
47 
48  return this->type < VLT_END;
49 }
50 
56 {
57  VehicleListIdentifier result;
58  bool ret = result.UnpackIfValid(data);
59  assert(ret);
60  return result;
61 }
62 
71 void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
72 {
73  engines->Clear();
74  if (wagons != NULL && wagons != engines) wagons->Clear();
75 
76  const Vehicle *v;
77  FOR_ALL_VEHICLES(v) {
78  /* General tests for all vehicle types */
79  if (v->type != type) continue;
80  if (v->tile != tile) continue;
81 
82  switch (type) {
83  case VEH_TRAIN: {
84  const Train *t = Train::From(v);
85  if (t->IsArticulatedPart() || t->IsRearDualheaded()) continue;
86  if (t->track != TRACK_BIT_DEPOT) continue;
87  if (wagons != NULL && t->First()->IsFreeWagon()) {
88  if (individual_wagons || t->IsFreeWagon()) *wagons->Append() = t;
89  continue;
90  }
91  break;
92  }
93 
94  default:
95  if (!v->IsInDepot()) continue;
96  break;
97  }
98 
99  if (!v->IsPrimaryVehicle()) continue;
100 
101  *engines->Append() = v;
102  }
103 
104  /* Ensure the lists are not wasting too much space. If the lists are fresh
105  * (i.e. built within a command) then this will actually do nothing. */
106  engines->Compact();
107  if (wagons != NULL && wagons != engines) wagons->Compact();
108 }
109 
117 {
118  list->Clear();
119 
120  const Vehicle *v;
121 
122  switch (vli.type) {
123  case VL_STATION_LIST:
124  FOR_ALL_VEHICLES(v) {
125  if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
126  const Order *order;
127 
128  FOR_VEHICLE_ORDERS(v, order) {
129  if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_IMPLICIT))
130  && order->GetDestination() == vli.index) {
131  *list->Append() = v;
132  break;
133  }
134  }
135  }
136  }
137  break;
138 
139  case VL_SHARED_ORDERS:
140  /* Add all vehicles from this vehicle's shared order list */
141  v = Vehicle::GetIfValid(vli.index);
142  if (v == NULL || v->type != vli.vtype || !v->IsPrimaryVehicle()) return false;
143 
144  for (; v != NULL; v = v->NextShared()) {
145  *list->Append() = v;
146  }
147  break;
148 
149  case VL_GROUP_LIST:
150  if (vli.index != ALL_GROUP) {
151  FOR_ALL_VEHICLES(v) {
152  if (v->type == vli.vtype && v->IsPrimaryVehicle() &&
153  v->owner == vli.company && GroupIsInGroup(v->group_id, vli.index)) {
154  *list->Append() = v;
155  }
156  }
157  break;
158  }
159  FALLTHROUGH;
160 
161  case VL_STANDARD:
162  FOR_ALL_VEHICLES(v) {
163  if (v->type == vli.vtype && v->owner == vli.company && v->IsPrimaryVehicle()) {
164  *list->Append() = v;
165  }
166  }
167  break;
168 
169  case VL_DEPOT_LIST:
170  FOR_ALL_VEHICLES(v) {
171  if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
172  const Order *order;
173 
174  FOR_VEHICLE_ORDERS(v, order) {
175  if (order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.index) {
176  *list->Append() = v;
177  break;
178  }
179  }
180  }
181  }
182  break;
183 
184  default: return false;
185  }
186 
187  list->Compact();
188  return true;
189 }
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
Definition: pool_type.hpp:257
CompanyID company
The company associated with this list.
Definition: vehiclelist.h:34
The information about a vehicle list.
Definition: vehiclelist.h:31
Functions and type for generating vehicle lists.
Base for the train class.
Base class for groups and group functions.
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition: group_cmd.cpp:720
Vehicle data structure.
Definition: vehicle_base.h:212
void Clear()
Remove all items from the list.
void Compact()
Compact the list down to the smallest block size boundary.
bool UnpackIfValid(uint32 data)
Unpack a VehicleListIdentifier from a single uint32.
Definition: vehiclelist.cpp:40
T * First() const
Get the first vehicle in the chain.
void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
Generate a list of vehicles inside a depot.
Definition: vehiclelist.cpp:71
bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
Generate a list of vehicles based on window type.
static Train * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
OrderDepotActionFlags GetDepotActionType() const
What are we going to do when in the depot.
Definition: order_base.h:139
T * Append(uint to_add=1)
Append an item and return it.
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition: vehicle_base.h:434
VehicleType
Available vehicle types.
Definition: vehicle_type.h:21
uint32 index
A vehicle list type specific index.
Definition: vehiclelist.h:35
bool IsType(OrderType type) const
Check whether this order is of the given type.
Definition: order_base.h:63
Definition of base types and functions in a cross-platform compatible way.
bool IsArticulatedPart() const
Check if the vehicle is an articulated part of an engine.
Definition: vehicle_base.h:891
A number of safeguards to prevent using unsafe methods.
static VehicleListIdentifier UnPack(uint32 data)
Decode a packed vehicle list identifier into a new one.
Definition: vehiclelist.cpp:55
TileIndex tile
Current tile index.
Definition: vehicle_base.h:230
The tile has no ownership.
Definition: company_type.h:27
virtual bool IsInDepot() const
Check whether the vehicle is in the depot.
Definition: vehicle_base.h:505
bool IsRearDualheaded() const
Tell if we are dealing with the rear end of a multiheaded engine.
&#39;Train&#39; is either a loco or a wagon.
Definition: train.h:88
DestinationID GetDestination() const
Gets the destination of this order.
Definition: order_base.h:96
uint32 TileIndex
The index/ID of a Tile.
Definition: tile_type.h:80
Bitflag for a depot.
Definition: track_type.h:59
uint32 Pack() const
Pack a VehicleListIdentifier in a single uint32.
Definition: vehiclelist.cpp:23
Send the vehicle to the nearest depot.
Definition: order_type.h:111
VehicleListType type
The type of vehicle list.
Definition: vehiclelist.h:32
OwnerByte owner
Which company owns the vehicle?
Definition: vehicle_base.h:273
static uint GB(const T x, const uint8 s, const uint8 n)
Fetch n bits from x, started at bit s.
VehicleListType
Vehicle List type flags.
Definition: vehiclelist.h:21
bool IsFreeWagon() const
Check if the vehicle is a free wagon (got no engine in front of it).
#define FOR_ALL_VEHICLES(var)
Iterate over all vehicles.
Definition: vehicle_base.h:986
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Definition: vehicle_base.h:664
Owner
Enum for all companies/owners.
Definition: company_type.h:20
VehicleType vtype
The vehicle type associated with this list.
Definition: vehiclelist.h:33
VehicleTypeByte type
Type of vehicle.
Definition: vehicle_type.h:54
GroupID group_id
Index of group Pool array.
Definition: vehicle_base.h:326
Train vehicle type.
Definition: vehicle_type.h:24
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition: group_type.h:18