Fix #8316: Make sort industries by production and transported with a cargo filter...
[openttd-github.git] / src / vehiclelist.cpp
blob18dbef35f2e6a0debfb55aae960afe1ffcd59ee4
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 */
8 /** @file vehiclelist.cpp Lists of vehicles. */
10 #include "stdafx.h"
11 #include "train.h"
12 #include "vehiclelist.h"
13 #include "group.h"
15 #include "safeguards.h"
17 /**
18 * Pack a VehicleListIdentifier in a single uint32.
19 * @return The packed identifier.
21 uint32 VehicleListIdentifier::Pack() const
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));
30 return c << 28 | this->type << 23 | this->vtype << 26 | this->index;
33 /**
34 * Unpack a VehicleListIdentifier from a single uint32.
35 * @param data The data to unpack.
36 * @return true iff the data was valid (enough).
38 bool VehicleListIdentifier::UnpackIfValid(uint32 data)
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);
46 return this->type < VLT_END;
49 /**
50 * Decode a packed vehicle list identifier into a new one.
51 * @param data The data to unpack.
53 /* static */ VehicleListIdentifier VehicleListIdentifier::UnPack(uint32 data)
55 VehicleListIdentifier result;
56 [[maybe_unused]] bool ret = result.UnpackIfValid(data);
57 assert(ret);
58 return result;
61 /**
62 * Generate a list of vehicles inside a depot.
63 * @param type Type of vehicle
64 * @param tile The tile the depot is located on
65 * @param engines Pointer to list to add vehicles to
66 * @param wagons Pointer to list to add wagons to (can be nullptr)
67 * @param individual_wagons If true add every wagon to \a wagons which is not attached to an engine. If false only add the first wagon of every row.
69 void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
71 engines->clear();
72 if (wagons != nullptr && wagons != engines) wagons->clear();
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;
79 switch (type) {
80 case VEH_TRAIN: {
81 const Train *t = Train::From(v);
82 if (t->IsArticulatedPart() || t->IsRearDualheaded()) continue;
83 if (t->track != TRACK_BIT_DEPOT) continue;
84 if (wagons != nullptr && t->First()->IsFreeWagon()) {
85 if (individual_wagons || t->IsFreeWagon()) wagons->push_back(t);
86 continue;
88 break;
91 default:
92 if (!v->IsInDepot()) continue;
93 break;
96 if (!v->IsPrimaryVehicle()) continue;
98 engines->push_back(v);
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();
108 * Generate a list of vehicles based on window type.
109 * @param list Pointer to list to add vehicles to
110 * @param vli The identifier of this vehicle list.
111 * @return false if invalid list is requested
113 bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
115 list->clear();
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;
130 break;
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;
137 for (; v != nullptr; v = v->NextShared()) {
138 list->push_back(v);
140 break;
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);
151 break;
153 FALLTHROUGH;
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);
161 break;
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;
174 break;
176 default: return false;
179 list->shrink_to_fit();
180 return true;