vehiclelist.cpp

Go to the documentation of this file.
00001 /* $Id: vehiclelist.cpp 19665 2010-04-17 22:27:49Z 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 
00081 bool GenerateVehicleSortList(VehicleList *list, VehicleType type, Owner owner, uint32 index, uint16 window_type)
00082 {
00083   list->Clear();
00084 
00085   const Vehicle *v;
00086 
00087   switch (window_type) {
00088     case VLW_STATION_LIST:
00089       FOR_ALL_VEHICLES(v) {
00090         if (v->type == type && v->IsPrimaryVehicle()) {
00091           const Order *order;
00092 
00093           FOR_VEHICLE_ORDERS(v, order) {
00094             if (order->IsType(OT_GOTO_STATION) && order->GetDestination() == index) {
00095               *list->Append() = v;
00096               break;
00097             }
00098           }
00099         }
00100       }
00101       break;
00102 
00103     case VLW_SHARED_ORDERS:
00104       /* Add all vehicles from this vehicle's shared order list */
00105       v = Vehicle::GetIfValid(index);
00106       if (v == NULL || v->type != type || !v->IsPrimaryVehicle()) return false;
00107 
00108       for (; v != NULL; v = v->NextShared()) {
00109         *list->Append() = v;
00110       }
00111       break;
00112 
00113     case VLW_STANDARD:
00114       FOR_ALL_VEHICLES(v) {
00115         if (v->type == type && v->owner == owner && v->IsPrimaryVehicle()) {
00116           *list->Append() = v;
00117         }
00118       }
00119       break;
00120 
00121     case VLW_DEPOT_LIST:
00122       FOR_ALL_VEHICLES(v) {
00123         if (v->type == type && v->IsPrimaryVehicle()) {
00124           const Order *order;
00125 
00126           FOR_VEHICLE_ORDERS(v, order) {
00127             if (order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == index) {
00128               *list->Append() = v;
00129               break;
00130             }
00131           }
00132         }
00133       }
00134       break;
00135 
00136     case VLW_WAYPOINT_LIST:
00137       FOR_ALL_VEHICLES(v) {
00138         if (v->type == type && v->IsPrimaryVehicle()) {
00139           const Order *order;
00140 
00141           FOR_VEHICLE_ORDERS(v, order) {
00142             if (order->IsType(OT_GOTO_WAYPOINT) && order->GetDestination() == index) {
00143               *list->Append() = v;
00144               break;
00145             }
00146           }
00147         }
00148       }
00149       break;
00150 
00151     case VLW_GROUP_LIST:
00152       FOR_ALL_VEHICLES(v) {
00153         if (v->type == type && v->IsPrimaryVehicle() &&
00154             v->owner == owner && v->group_id == index) {
00155           *list->Append() = v;
00156         }
00157       }
00158       break;
00159 
00160     default: return false;
00161   }
00162 
00163   list->Compact();
00164   return true;
00165 }

Generated on Tue Sep 14 17:06:57 2010 for OpenTTD by  doxygen 1.6.1