Translations update
[openttd/fttd.git] / src / vehiclelist.cpp
blob0cf942658dcf8063c4410a7da2df7823c46be14a
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"
15 #include "group.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 assert_compile(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::Unpack(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 VehicleListIdentifier::VehicleListIdentifier(uint32 data)
55 bool ret = this->Unpack(data);
56 assert(ret);
59 /**
60 * Generate a list of vehicles inside a depot.
61 * @param type Type of vehicle
62 * @param tile The tile the depot is located on
63 * @param engines Pointer to list to add vehicles to
64 * @param wagons Pointer to list to add wagons to (can be NULL)
65 * @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.
67 void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
69 engines->Clear();
70 if (wagons != NULL && wagons != engines) wagons->Clear();
72 const Vehicle *v;
73 FOR_ALL_VEHICLES(v) {
74 /* General tests for all vehicle types */
75 if (v->type != type) continue;
76 if (v->tile != tile) continue;
78 switch (type) {
79 case VEH_TRAIN: {
80 const Train *t = Train::From(v);
81 if (t->IsArticulatedPart() || t->IsRearDualheaded()) continue;
82 if (t->trackdir != TRACKDIR_DEPOT) continue;
83 if (wagons != NULL && t->First()->IsFreeWagon()) {
84 if (individual_wagons || t->IsFreeWagon()) *wagons->Append() = t;
85 continue;
87 break;
90 default:
91 if (!v->IsInDepot()) continue;
92 break;
95 if (!v->IsPrimaryVehicle()) continue;
97 *engines->Append() = v;
100 /* Ensure the lists are not wasting too much space. If the lists are fresh
101 * (i.e. built within a command) then this will actually do nothing. */
102 engines->Compact();
103 if (wagons != NULL && wagons != engines) wagons->Compact();
107 * Generate a list of vehicles based on window type.
108 * @param list Pointer to list to add vehicles to
109 * @param vli The identifier of this vehicle list.
110 * @return false if invalid list is requested
112 bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
114 list->Clear();
116 const Vehicle *v;
118 switch (vli.type) {
119 case VL_STATION_LIST:
120 FOR_ALL_VEHICLES(v) {
121 if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
122 const Order *order;
124 FOR_VEHICLE_ORDERS(v, order) {
125 if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_IMPLICIT))
126 && order->GetDestination() == vli.index) {
127 *list->Append() = v;
128 break;
133 break;
135 case VL_SHARED_ORDERS:
136 /* Add all vehicles from this vehicle's shared order list */
137 v = Vehicle::GetIfValid(vli.index);
138 if (v == NULL || v->type != vli.vtype || !v->IsPrimaryVehicle()) return false;
140 for (; v != NULL; v = v->NextShared()) {
141 *list->Append() = v;
143 break;
145 case VL_GROUP_LIST:
146 if (vli.index != ALL_GROUP) {
147 FOR_ALL_VEHICLES(v) {
148 if (v->type == vli.vtype && v->IsPrimaryVehicle() &&
149 v->owner == vli.company && GroupIsInGroup(v->group_id, vli.index)) {
150 *list->Append() = v;
153 break;
155 /* FALL THROUGH */
157 case VL_STANDARD:
158 FOR_ALL_VEHICLES(v) {
159 if (v->type == vli.vtype && v->owner == vli.company && v->IsPrimaryVehicle()) {
160 *list->Append() = v;
163 break;
165 case VL_DEPOT_LIST:
166 FOR_ALL_VEHICLES(v) {
167 if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
168 const Order *order;
170 FOR_VEHICLE_ORDERS(v, order) {
171 if (order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.index) {
172 *list->Append() = v;
173 break;
178 break;
180 default: return false;
183 list->Compact();
184 return true;