# vehiclelist.cpp

Go to the documentation of this file.

    00001 /* $Id: vehiclelist.cpp 17248 2009-08-21 20:21:05Z rubidium $ */
    00002
    00003 /*
    00004  * This file is part of OpenTTD.
    00005  * 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.
    00006  * 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.
    00007  * 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/>.
    00008  */
    00009
    00012 #include "stdafx.h"
    00013 #include "vehicle_gui.h"
    00014 #include "train.h"
    00015 #include "vehiclelist.h"
    00016
    00025 void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
    00026 {
    00027   engines->Clear();
    00028   if (wagons != NULL && wagons != engines) wagons->Clear();
    00029
    00030   const Vehicle *v;
    00031   FOR_ALL_VEHICLES(v) {
    00032     /* General tests for all vehicle types */
    00033     if (v->type != type) continue;
    00034     if (v->tile != tile) continue;
    00035
    00036     switch (type) {
    00037       case VEH_TRAIN: {
    00038         const Train *t = Train::From(v);
    00039         if (t->IsArticulatedPart() || t->IsRearDualheaded()) continue;
    00040         if (t->track != TRACK_BIT_DEPOT) continue;
    00041         if (wagons != NULL && t->First()->IsFreeWagon()) {
    00042           if (individual_wagons || t->IsFreeWagon()) *wagons->Append() = t;
    00043           continue;
    00044         }
    00045         break;
    00046       }
    00047
    00048       default:
    00049         if (!v->IsInDepot()) continue;
    00050         break;
    00051     }
    00052
    00053     if (!v->IsPrimaryVehicle()) continue;
    00054
    00055     *engines->Append() = v;
    00056   }
    00057
    00058   /* Ensure the lists are not wasting too much space. If the lists are fresh
    00059    * (i.e. built within a command) then this will actually do nothing. */
    00060   engines->Compact();
    00061   if (wagons != NULL && wagons != engines) wagons->Compact();
    00062 }
    00063
    00080 void GenerateVehicleSortList(VehicleList *list, VehicleType type, Owner owner, uint32 index, uint16 window_type)
    00081 {
    00082   list->Clear();
    00083
    00084   const Vehicle *v;
    00085
    00086   switch (window_type) {
    00087     case VLW_STATION_LIST:
    00088       FOR_ALL_VEHICLES(v) {
    00089         if (v->type == type && v->IsPrimaryVehicle()) {
    00090           const Order *order;
    00091
    00092           FOR_VEHICLE_ORDERS(v, order) {
    00093             if (order->IsType(OT_GOTO_STATION) && order->GetDestination() == index) {
    00094               *list->Append() = v;
    00095               break;
    00096             }
    00097           }
    00098         }
    00099       }
    00100       break;
    00101
    00102     case VLW_SHARED_ORDERS:
    00103       /* Add all vehicles from this vehicle's shared order list */
    00104       for (v = Vehicle::Get(index); v != NULL; v = v->NextShared()) {
    00105         *list->Append() = v;
    00106       }
    00107       break;
    00108
    00109     case VLW_STANDARD:
    00110       FOR_ALL_VEHICLES(v) {
    00111         if (v->type == type && v->owner == owner && v->IsPrimaryVehicle()) {
    00112           *list->Append() = v;
    00113         }
    00114       }
    00115       break;
    00116
    00117     case VLW_DEPOT_LIST:
    00118       FOR_ALL_VEHICLES(v) {
    00119         if (v->type == type && v->IsPrimaryVehicle()) {
    00120           const Order *order;
    00121
    00122           FOR_VEHICLE_ORDERS(v, order) {
    00123             if (order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == index) {
    00124               *list->Append() = v;
    00125               break;
    00126             }
    00127           }
    00128         }
    00129       }
    00130       break;
    00131
    00132     case VLW_WAYPOINT_LIST:
    00133       FOR_ALL_VEHICLES(v) {
    00134         if (v->type == type && v->IsPrimaryVehicle()) {
    00135           const Order *order;
    00136
    00137           FOR_VEHICLE_ORDERS(v, order) {
    00138             if (order->IsType(OT_GOTO_WAYPOINT) && order->GetDestination() == index) {
    00139               *list->Append() = v;
    00140               break;
    00141             }
    00142           }
    00143         }
    00144       }
    00145       break;
    00146
    00147     case VLW_GROUP_LIST:
    00148       FOR_ALL_VEHICLES(v) {
    00149         if (v->type == type && v->IsPrimaryVehicle() &&
    00150             v->owner == owner && v->group_id == index) {
    00151           *list->Append() = v;
    00152         }
    00153       }
    00154       break;
    00155
    00156     default: NOT_REACHED();
    00157   }
    00158
    00159   list->Compact();
    00160 }