OpenTTD Source: src/vehiclelist.cpp Source File

OpenTTD Source 15.0-RC2

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 <https://www.gnu.org/licenses/old-licenses/gpl-2.0>.
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
23 WindowNumber VehicleListIdentifier::ToWindowNumber() const
24{
25 uint8_t c = this->company == OWNER_NONE ? 0xF : this->company.base();
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 void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
44{
45 engines->clear();
46 if (wagons != nullptr && wagons != engines) wagons->clear();
47
48 for (Vehicle *v : VehiclesOnTile(tile)) {
49 if (v->type != type || !v->IsInDepot()) continue;
50
51 if (type == VEH_TRAIN) {
52 const Train *t = Train::From(v);
53 if (t->IsArticulatedPart() || t->IsRearDualheaded()) continue;
54 if (wagons != nullptr && t->First()->IsFreeWagon()) {
55 if (individual_wagons || t->IsFreeWagon()) wagons->push_back(t);
56 continue;
57 }
58 }
59
60 if (v->IsPrimaryVehicle()) engines->push_back(v);
61 }
62}
63
70 bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
71{
72 list->clear();
73
74 switch (vli.type) {
75 case VL_STATION_LIST:
76 FindVehiclesWithOrder(
77 [&vli](const Vehicle *v) { return v->type == vli.vtype; },
78 [&vli](const Order *order) { return (order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_IMPLICIT)) && order->GetDestination() == vli.ToStationID(); },
79 [&list](const Vehicle *v) { list->push_back(v); }
80 );
81 break;
82
83 case VL_SHARED_ORDERS: {
84 /* Add all vehicles from this vehicle's shared order list */
85 const Vehicle *v = Vehicle::GetIfValid(vli.ToVehicleID());
86 if (v == nullptr || v->type != vli.vtype || !v->IsPrimaryVehicle()) return false;
87
88 for (; v != nullptr; v = v->NextShared()) {
89 list->push_back(v);
90 }
91 break;
92 }
93
94 case VL_GROUP_LIST:
95 if (vli.ToGroupID() != ALL_GROUP) {
96 for (const Vehicle *v : Vehicle::Iterate()) {
97 if (v->type == vli.vtype && v->IsPrimaryVehicle() &&
98 v->owner == vli.company && GroupIsInGroup(v->group_id, vli.ToGroupID())) {
99 list->push_back(v);
100 }
101 }
102 break;
103 }
104 [[fallthrough]];
105
106 case VL_STANDARD:
107 for (const Vehicle *v : Vehicle::Iterate()) {
108 if (v->type == vli.vtype && v->owner == vli.company && v->IsPrimaryVehicle()) {
109 list->push_back(v);
110 }
111 }
112 break;
113
114 case VL_DEPOT_LIST:
115 FindVehiclesWithOrder(
116 [&vli](const Vehicle *v) { return v->type == vli.vtype; },
117 [&vli](const Order *order) { return order->IsType(OT_GOTO_DEPOT) && !order->GetDepotActionType().Test(OrderDepotActionFlag::NearestDepot) && order->GetDestination() == vli.ToDestinationID(); },
118 [&list](const Vehicle *v) { list->push_back(v); }
119 );
120 break;
121
122 default: return false;
123 }
124
125 return true;
126}
VehiclesOnTile
Iterate over all vehicles on a tile.
Definition vehicle_func.h:53
OWNER_NONE
static constexpr Owner OWNER_NONE
The tile has no ownership.
Definition company_type.h:26
group.h
Base class for groups and group functions.
GroupIsInGroup
bool GroupIsInGroup(GroupID search, GroupID group)
Test if GroupID group is a descendant of (or is) GroupID search.
Definition group_cmd.cpp:899
ALL_GROUP
static constexpr GroupID ALL_GROUP
All vehicles are in this group.
Definition group_type.h:17
OrderDepotActionFlag::NearestDepot
@ NearestDepot
Send the vehicle to the nearest depot.
safeguards.h
A number of safeguards to prevent using unsafe methods.
stdafx.h
Definition of base types and functions in a cross-platform compatible way.
BaseVehicle::type
VehicleType type
Type of vehicle.
Definition vehicle_type.h:51
GroundVehicle::IsRearDualheaded
bool IsRearDualheaded() const
Tell if we are dealing with the rear end of a multiheaded engine.
Definition ground_vehicle.hpp:336
GroundVehicle::IsFreeWagon
bool IsFreeWagon() const
Check if the vehicle is a free wagon (got no engine in front of it).
Definition ground_vehicle.hpp:312
Order
Definition order_base.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:436
Pool::PoolItem<&_vehicle_pool >::GetIfValid
static Titem * GetIfValid(auto index)
Returns Titem with given index.
Definition pool_type.hpp:398
SpecializedVehicle::From
static T * From(Vehicle *v)
Converts a Vehicle to SpecializedVehicle with type checking.
Definition vehicle_base.h:1134
SpecializedVehicle::First
T * First() const
Get the first vehicle in the chain.
Definition vehicle_base.h:1031
StrongType::Typedef< uint32_t, struct TileIndexTag, StrongType::Compare, StrongType::Integer, StrongType::Compatible< int32_t >, StrongType::Compatible< int64_t > >
Train
'Train' is either a loco or a wagon.
Definition train.h:91
VehicleListIdentifier
The information about a vehicle list.
Definition vehiclelist.h:32
VehicleListIdentifier::type
VehicleListType type
The type of vehicle list.
Definition vehiclelist.h:33
VehicleListIdentifier::company
CompanyID company
The company associated with this list.
Definition vehiclelist.h:35
VehicleListIdentifier::ToWindowNumber
WindowNumber ToWindowNumber() const
Pack a VehicleListIdentifier in 32 bits so it can be used as unique WindowNumber.
Definition vehiclelist.cpp:23
VehicleListIdentifier::vtype
VehicleType vtype
The vehicle type associated with this list.
Definition vehiclelist.h:34
VehicleListIdentifier::index
uint32_t index
A vehicle list type specific index.
Definition vehiclelist.h:36
Vehicle
Vehicle data structure.
Definition vehicle_base.h:205
Vehicle::group_id
GroupID group_id
Index of group Pool array.
Definition vehicle_base.h:321
Vehicle::IsArticulatedPart
bool IsArticulatedPart() const
Check if the vehicle is an articulated part of an engine.
Definition vehicle_base.h:922
Vehicle::NextShared
Vehicle * NextShared() const
Get the next vehicle of the shared vehicle chain.
Definition vehicle_base.h:681
Vehicle::IsPrimaryVehicle
virtual bool IsPrimaryVehicle() const
Whether this is the primary vehicle in the chain.
Definition vehicle_base.h:432
Vehicle::owner
Owner owner
Which company owns the vehicle?
Definition vehicle_base.h:270
WindowNumber
Number to differentiate different windows of the same class.
Definition window_type.h:760
train.h
Base for the train class.
vehicle_func.h
Functions related to vehicles.
VehicleType
VehicleType
Available vehicle types.
Definition vehicle_type.h:22
VEH_TRAIN
@ VEH_TRAIN
Train vehicle type.
Definition vehicle_type.h:25
GenerateVehicleSortList
bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
Generate a list of vehicles based on window type.
Definition vehiclelist.cpp:70
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:43
vehiclelist.h
Functions and type for generating vehicle lists.
VehicleList
std::vector< const Vehicle * > VehicleList
A list of vehicles.
Definition vehiclelist.h:68
VL_GROUP_LIST
@ VL_GROUP_LIST
Index is the group.
Definition vehiclelist.h:27
VL_STANDARD
@ VL_STANDARD
Index is the company.
Definition vehiclelist.h:23
VL_STATION_LIST
@ VL_STATION_LIST
Index is the station.
Definition vehiclelist.h:25
VL_DEPOT_LIST
@ VL_DEPOT_LIST
Index is the destination (station for hangar of aircraft, depot for others)
Definition vehiclelist.h:26
VL_SHARED_ORDERS
@ VL_SHARED_ORDERS
Index is the first vehicle of the shared orders.
Definition vehiclelist.h:24
vehiclelist_func.h
Functions and type for generating vehicle lists.
FindVehiclesWithOrder
void FindVehiclesWithOrder(VehiclePredicate veh_pred, OrderPredicate ord_pred, VehicleFunc veh_func)
Find vehicles matching an order.
Definition vehiclelist_func.h:24