Invalidate the right goal list when changing goals
[openttd/fttd.git] / src / vehiclelist.cpp
blobe7b96477b44f931c0f8a03c8b99d96403e8f9816
1 /* $Id$ */
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 */
10 /** @file vehiclelist.cpp Lists of vehicles. */
12 #include "stdafx.h"
13 #include "train.h"
14 #include "vehiclelist.h"
16 /**
17 * Pack a VehicleListIdentifier in a single uint32.
18 * @return The packed identifier.
20 uint32 VehicleListIdentifier::Pack()
22 byte c = this->company == OWNER_NONE ? 0xF : (byte)this->company;
23 assert(c < (1 << 4));
24 assert(this->vtype < (1 << 2));
25 assert(this->index < (1 << 20));
26 assert(this->type < VLT_END);
27 assert_compile(VLT_END <= (1 << 3));
29 return c << 28 | this->type << 23 | this->vtype << 26 | this->index;
32 /**
33 * Unpack a VehicleListIdentifier from a single uint32.
34 * @param data The data to unpack.
35 * @return true iff the data was valid (enough).
37 bool VehicleListIdentifier::Unpack(uint32 data)
39 byte c = GB(data, 28, 4);
40 this->company = c == 0xF ? OWNER_NONE : (CompanyID)c;
41 this->type = (VehicleListType)GB(data, 23, 3);
42 this->vtype = (VehicleType)GB(data, 26, 2);
43 this->index = GB(data, 0, 20);
45 return this->type < VLT_END;
48 /**
49 * Decode a packed vehicle list identifier into a new one.
50 * @param data The data to unpack.
52 VehicleListIdentifier::VehicleListIdentifier(uint32 data)
54 bool ret = this->Unpack(data);
55 assert(ret);
58 /**
59 * Generate a list of vehicles inside a depot.
60 * @param type Type of vehicle
61 * @param tile The tile the depot is located on
62 * @param engines Pointer to list to add vehicles to
63 * @param wagons Pointer to list to add wagons to (can be NULL)
64 * @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.
66 void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
68 engines->Clear();
69 if (wagons != NULL && wagons != engines) wagons->Clear();
71 const Vehicle *v;
72 FOR_ALL_VEHICLES(v) {
73 /* General tests for all vehicle types */
74 if (v->type != type) continue;
75 if (v->tile != tile) continue;
77 switch (type) {
78 case VEH_TRAIN: {
79 const Train *t = Train::From(v);
80 if (t->IsArticulatedPart() || t->IsRearDualheaded()) continue;
81 if (t->trackdir != TRACKDIR_DEPOT) continue;
82 if (wagons != NULL && t->First()->IsFreeWagon()) {
83 if (individual_wagons || t->IsFreeWagon()) *wagons->Append() = t;
84 continue;
86 break;
89 default:
90 if (!v->IsInDepot()) continue;
91 break;
94 if (!v->IsPrimaryVehicle()) continue;
96 *engines->Append() = v;
99 /* Ensure the lists are not wasting too much space. If the lists are fresh
100 * (i.e. built within a command) then this will actually do nothing. */
101 engines->Compact();
102 if (wagons != NULL && wagons != engines) wagons->Compact();
106 * Generate a list of vehicles based on window type.
107 * @param list Pointer to list to add vehicles to
108 * @param vli The identifier of this vehicle list.
109 * @return false if invalid list is requested
111 bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
113 list->Clear();
115 const Vehicle *v;
117 switch (vli.type) {
118 case VL_STATION_LIST:
119 FOR_ALL_VEHICLES(v) {
120 if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
121 const Order *order;
123 FOR_VEHICLE_ORDERS(v, order) {
124 if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_IMPLICIT))
125 && order->GetDestination() == vli.index) {
126 *list->Append() = v;
127 break;
132 break;
134 case VL_SHARED_ORDERS:
135 /* Add all vehicles from this vehicle's shared order list */
136 v = Vehicle::GetIfValid(vli.index);
137 if (v == NULL || v->type != vli.vtype || !v->IsPrimaryVehicle()) return false;
139 for (; v != NULL; v = v->NextShared()) {
140 *list->Append() = v;
142 break;
144 case VL_GROUP_LIST:
145 if (vli.index != ALL_GROUP) {
146 FOR_ALL_VEHICLES(v) {
147 if (v->type == vli.vtype && v->IsPrimaryVehicle() &&
148 v->owner == vli.company && v->group_id == vli.index) {
149 *list->Append() = v;
152 break;
154 /* FALL THROUGH */
156 case VL_STANDARD:
157 FOR_ALL_VEHICLES(v) {
158 if (v->type == vli.vtype && v->owner == vli.company && v->IsPrimaryVehicle()) {
159 *list->Append() = v;
162 break;
164 case VL_DEPOT_LIST:
165 FOR_ALL_VEHICLES(v) {
166 if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
167 const Order *order;
169 FOR_VEHICLE_ORDERS(v, order) {
170 if (order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.index) {
171 *list->Append() = v;
172 break;
177 break;
179 default: return false;
182 list->Compact();
183 return true;