vehiclelist.cpp

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

Generated on Fri Mar 4 21:37:08 2011 for OpenTTD by  doxygen 1.6.1