OpenTTD Source 20250205-master-gfd85ab1e2c
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 "vehicle_func.h"
13#include "vehiclelist.h"
14#include "vehiclelist_func.h"
15#include "group.h"
16
17#include "safeguards.h"
18
24{
25 uint8_t c = this->company == OWNER_NONE ? 0xF : (uint8_t)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 static_assert(VLT_END <= (1 << 3));
31
32 return c << 28 | this->type << 23 | this->vtype << 26 | this->index;
33}
34
43
51{
52 auto bdvld = static_cast<BuildDepotVehicleListData *>(data);
53 if (v->type != bdvld->type || !v->IsInDepot()) return nullptr;
54
55 if (bdvld->type == VEH_TRAIN) {
56 const Train *t = Train::From(v);
57 if (t->IsArticulatedPart() || t->IsRearDualheaded()) return nullptr;
58 if (bdvld->wagons != nullptr && t->First()->IsFreeWagon()) {
59 if (bdvld->individual_wagons || t->IsFreeWagon()) bdvld->wagons->push_back(t);
60 return nullptr;
61 }
62 }
63
64 if (v->IsPrimaryVehicle()) bdvld->engines->push_back(v);
65 return nullptr;
66};
67
76void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
77{
78 engines->clear();
79 if (wagons != nullptr && wagons != engines) wagons->clear();
80
81 BuildDepotVehicleListData bdvld{engines, wagons, type, individual_wagons};
83}
84
92{
93 list->clear();
94
95 switch (vli.type) {
96 case VL_STATION_LIST:
98 [&vli](const Vehicle *v) { return v->type == vli.vtype; },
99 [&vli](const Order *order) { return (order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_IMPLICIT)) && order->GetDestination() == vli.index; },
100 [&list](const Vehicle *v) { list->push_back(v); }
101 );
102 break;
103
104 case VL_SHARED_ORDERS: {
105 /* Add all vehicles from this vehicle's shared order list */
106 const Vehicle *v = Vehicle::GetIfValid(vli.index);
107 if (v == nullptr || v->type != vli.vtype || !v->IsPrimaryVehicle()) return false;
108
109 for (; v != nullptr; v = v->NextShared()) {
110 list->push_back(v);
111 }
112 break;
113 }
114
115 case VL_GROUP_LIST:
116 if (vli.index != ALL_GROUP) {
117 for (const Vehicle *v : Vehicle::Iterate()) {
118 if (v->type == vli.vtype && v->IsPrimaryVehicle() &&
119 v->owner == vli.company && GroupIsInGroup(v->group_id, vli.index)) {
120 list->push_back(v);
121 }
122 }
123 break;
124 }
125 [[fallthrough]];
126
127 case VL_STANDARD:
128 for (const Vehicle *v : Vehicle::Iterate()) {
129 if (v->type == vli.vtype && v->owner == vli.company && v->IsPrimaryVehicle()) {
130 list->push_back(v);
131 }
132 }
133 break;
134
135 case VL_DEPOT_LIST:
137 [&vli](const Vehicle *v) { return v->type == vli.vtype; },
138 [&vli](const Order *order) { return order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.index; },
139 [&list](const Vehicle *v) { list->push_back(v); }
140 );
141 break;
142
143 default: return false;
144 }
145
146 return true;
147}
@ OWNER_NONE
The tile has no ownership.
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.
static const GroupID ALL_GROUP
All vehicles are in this group.
Definition group_type.h:16
@ ODATFB_NEAREST_DEPOT
Send the vehicle to the nearest depot.
Definition order_type.h:105
A number of safeguards to prevent using unsafe methods.
Definition of base types and functions in a cross-platform compatible way.
VehicleType type
Type of vehicle.
Data for building a depot vehicle list.
VehicleList * engines
Pointer to list to add vehicles to.
VehicleType type
Type of vehicle.
bool individual_wagons
If true add every wagon to wagons which is not attached to an engine. If false only add the first wag...
VehicleList * wagons
Pointer to list to add wagons to (can be nullptr).
bool IsRearDualheaded() const
Tell if we are dealing with the rear end of a multiheaded engine.
bool IsFreeWagon() const
Check if the vehicle is a free wagon (got no engine in front of it).
static Pool::IterateWrapper< Titem > Iterate(size_t from=0)
Returns an iterable ensemble of all valid Titem.
static Titem * GetIfValid(size_t index)
Returns Titem with given index.
static T * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
T * First() const
Get the first vehicle in the chain.
'Train' is either a loco or a wagon.
Definition train.h:89
The information about a vehicle list.
Definition vehiclelist.h:29
VehicleListType type
The type of vehicle list.
Definition vehiclelist.h:30
CompanyID company
The company associated with this list.
Definition vehiclelist.h:32
WindowNumber ToWindowNumber() const
Pack a VehicleListIdentifier in 32 bits so it can be used as unique WindowNumber.
VehicleType vtype
The vehicle type associated with this list.
Definition vehiclelist.h:31
uint32_t index
A vehicle list type specific index.
Definition vehiclelist.h:33
Vehicle data structure.
GroupID group_id
Index of group Pool array.
bool IsArticulatedPart() const
Check if the vehicle is an articulated part of an engine.
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
virtual bool IsInDepot() const
Check whether the vehicle is in the depot.
Owner owner
Which company owns the vehicle?
Number to differentiate different windows of the same class.
Base for the train class.
void FindVehicleOnPos(TileIndex tile, void *data, VehicleFromPosProc *proc)
Find a vehicle from a specific location.
Definition vehicle.cpp:505
Functions related to vehicles.
VehicleType
Available vehicle types.
@ VEH_TRAIN
Train vehicle type.
bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
Generate a list of vehicles based on window type.
void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
Generate a list of vehicles inside a depot.
static Vehicle * BuildDepotVehicleListProc(Vehicle *v, void *data)
Add vehicles to a depot vehicle list.
Functions and type for generating vehicle lists.
std::vector< const Vehicle * > VehicleList
A list of vehicles.
Definition vehiclelist.h:53
Functions and type for generating vehicle lists.
void FindVehiclesWithOrder(VehiclePredicate veh_pred, OrderPredicate ord_pred, VehicleFunc veh_func)
Find vehicles matching an order.