vehiclelist.cpp

Go to the documentation of this file.
00001 /* $Id: vehiclelist.cpp 21642 2010-12-26 09:03:19Z 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 
00021 uint32 VehicleListIdentifier::Pack()
00022 {
00023   assert(this->company < (1 <<  4));
00024   assert(this->type    < (1 <<  3));
00025   assert(this->vtype   < (1 <<  2));
00026   assert(this->index   < (1 << 20));
00027 
00028   return this->company << 28 | this->type << 23 | this->vtype << 26 | this->index;
00029 }
00030 
00036 bool VehicleListIdentifier::Unpack(uint32 data)
00037 {
00038   this->company = (CompanyID)GB(data, 28, 4);
00039   this->type    = (VehicleListType)GB(data, 23, 3);
00040   this->vtype   = (VehicleType)GB(data, 26, 2);
00041   this->index   = GB(data, 0, 20);
00042 
00043   return this->type < VLT_END;
00044 }
00045 
00050 VehicleListIdentifier::VehicleListIdentifier(uint32 data)
00051 {
00052   bool ret = this->Unpack(data);
00053   assert(ret);
00054 }
00055 
00064 void BuildDepotVehicleList(VehicleType type, TileIndex tile, VehicleList *engines, VehicleList *wagons, bool individual_wagons)
00065 {
00066   engines->Clear();
00067   if (wagons != NULL && wagons != engines) wagons->Clear();
00068 
00069   const Vehicle *v;
00070   FOR_ALL_VEHICLES(v) {
00071     /* General tests for all vehicle types */
00072     if (v->type != type) continue;
00073     if (v->tile != tile) continue;
00074 
00075     switch (type) {
00076       case VEH_TRAIN: {
00077         const Train *t = Train::From(v);
00078         if (t->IsArticulatedPart() || t->IsRearDualheaded()) continue;
00079         if (t->track != TRACK_BIT_DEPOT) continue;
00080         if (wagons != NULL && t->First()->IsFreeWagon()) {
00081           if (individual_wagons || t->IsFreeWagon()) *wagons->Append() = t;
00082           continue;
00083         }
00084         break;
00085       }
00086 
00087       default:
00088         if (!v->IsInDepot()) continue;
00089         break;
00090     }
00091 
00092     if (!v->IsPrimaryVehicle()) continue;
00093 
00094     *engines->Append() = v;
00095   }
00096 
00097   /* Ensure the lists are not wasting too much space. If the lists are fresh
00098    * (i.e. built within a command) then this will actually do nothing. */
00099   engines->Compact();
00100   if (wagons != NULL && wagons != engines) wagons->Compact();
00101 }
00102 
00109 bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli)
00110 {
00111   list->Clear();
00112 
00113   const Vehicle *v;
00114 
00115   switch (vli.type) {
00116     case VL_STATION_LIST:
00117       FOR_ALL_VEHICLES(v) {
00118         if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
00119           const Order *order;
00120 
00121           FOR_VEHICLE_ORDERS(v, order) {
00122             if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_AUTOMATIC))
00123                 && order->GetDestination() == vli.index) {
00124               *list->Append() = v;
00125               break;
00126             }
00127           }
00128         }
00129       }
00130       break;
00131 
00132     case VL_SHARED_ORDERS:
00133       /* Add all vehicles from this vehicle's shared order list */
00134       v = Vehicle::GetIfValid(vli.index);
00135       if (v == NULL || v->type != vli.vtype || !v->IsPrimaryVehicle()) return false;
00136 
00137       for (; v != NULL; v = v->NextShared()) {
00138         *list->Append() = v;
00139       }
00140       break;
00141 
00142     case VL_GROUP_LIST:
00143       if (vli.index != ALL_GROUP) {
00144         FOR_ALL_VEHICLES(v) {
00145           if (v->type == vli.vtype && v->IsPrimaryVehicle() &&
00146               v->owner == vli.company && v->group_id == vli.index) {
00147             *list->Append() = v;
00148           }
00149         }
00150         break;
00151       }
00152       /* FALL THROUGH */
00153 
00154     case VL_STANDARD:
00155       FOR_ALL_VEHICLES(v) {
00156         if (v->type == vli.vtype && v->owner == vli.company && v->IsPrimaryVehicle()) {
00157           *list->Append() = v;
00158         }
00159       }
00160       break;
00161 
00162     case VL_DEPOT_LIST:
00163       FOR_ALL_VEHICLES(v) {
00164         if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
00165           const Order *order;
00166 
00167           FOR_VEHICLE_ORDERS(v, order) {
00168             if (order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.index) {
00169               *list->Append() = v;
00170               break;
00171             }
00172           }
00173         }
00174       }
00175       break;
00176 
00177     default: return false;
00178   }
00179 
00180   list->Compact();
00181   return true;
00182 }

Generated on Thu Jan 20 22:57:43 2011 for OpenTTD by  doxygen 1.6.1