ai_vehiclelist.cpp

Go to the documentation of this file.
00001 /* $Id: ai_vehiclelist.cpp 17693 2009-10-04 17:16:41Z 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 "ai_vehiclelist.hpp"
00013 #include "ai_group.hpp"
00014 #include "ai_map.hpp"
00015 #include "ai_station.hpp"
00016 #include "../../company_func.h"
00017 #include "../../depot_map.h"
00018 #include "../../vehicle_base.h"
00019 
00020 AIVehicleList::AIVehicleList()
00021 {
00022   const Vehicle *v;
00023   FOR_ALL_VEHICLES(v) {
00024     if (v->owner == _current_company && v->IsPrimaryVehicle()) this->AddItem(v->index);
00025   }
00026 }
00027 
00028 AIVehicleList_Station::AIVehicleList_Station(StationID station_id)
00029 {
00030   if (!AIBaseStation::IsValidBaseStation(station_id)) return;
00031 
00032   const Vehicle *v;
00033   FOR_ALL_VEHICLES(v) {
00034     if (v->owner == _current_company && v->IsPrimaryVehicle()) {
00035       const Order *order;
00036 
00037       FOR_VEHICLE_ORDERS(v, order) {
00038         if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT)) && order->GetDestination() == station_id) {
00039           this->AddItem(v->index);
00040           break;
00041         }
00042       }
00043     }
00044   }
00045 }
00046 
00047 AIVehicleList_Depot::AIVehicleList_Depot(TileIndex tile)
00048 {
00049   if (!AIMap::IsValidTile(tile)) return;
00050 
00051   DestinationID dest;
00052   VehicleType type;
00053 
00054   switch (GetTileType(tile)) {
00055     case MP_STATION: // Aircraft
00056       if (!IsAirport(tile)) return;
00057       type = VEH_AIRCRAFT;
00058       dest = GetStationIndex(tile);
00059       break;
00060 
00061     case MP_RAILWAY:
00062       if (!IsRailDepot(tile)) return;
00063       type = VEH_TRAIN;
00064       dest = GetDepotIndex(tile);
00065       break;
00066 
00067     case MP_ROAD:
00068       if (!IsRoadDepot(tile)) return;
00069       type = VEH_ROAD;
00070       dest = GetDepotIndex(tile);
00071       break;
00072 
00073     case MP_WATER:
00074       if (!IsShipDepot(tile)) return;
00075       type = VEH_SHIP;
00076       dest = GetDepotIndex(tile);
00077       break;
00078 
00079     default: // No depot
00080       return;
00081   }
00082 
00083   const Vehicle *v;
00084   FOR_ALL_VEHICLES(v) {
00085     if (v->owner == _current_company && v->IsPrimaryVehicle() && v->type == type) {
00086       const Order *order;
00087 
00088       FOR_VEHICLE_ORDERS(v, order) {
00089         if (order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == dest) {
00090           this->AddItem(v->index);
00091           break;
00092         }
00093       }
00094     }
00095   }
00096 }
00097 
00098 AIVehicleList_SharedOrders::AIVehicleList_SharedOrders(VehicleID vehicle_id)
00099 {
00100   if (!AIVehicle::IsValidVehicle(vehicle_id)) return;
00101 
00102   for (const Vehicle *v = Vehicle::Get(vehicle_id)->FirstShared(); v != NULL; v = v->NextShared()) {
00103     this->AddItem(v->index);
00104   }
00105 }
00106 
00107 AIVehicleList_Group::AIVehicleList_Group(GroupID group_id)
00108 {
00109   if (!AIGroup::IsValidGroup((AIGroup::GroupID)group_id)) return;
00110 
00111   const Vehicle *v;
00112   FOR_ALL_VEHICLES(v) {
00113     if (v->owner == _current_company && v->IsPrimaryVehicle()) {
00114       if (v->group_id == group_id) this->AddItem(v->index);
00115     }
00116   }
00117 }
00118 
00119 AIVehicleList_DefaultGroup::AIVehicleList_DefaultGroup(AIVehicle::VehicleType vehicle_type)
00120 {
00121   if (vehicle_type < AIVehicle::VT_RAIL || vehicle_type > AIVehicle::VT_AIR) return;
00122 
00123   const Vehicle *v;
00124   FOR_ALL_VEHICLES(v) {
00125     if (v->owner == _current_company && v->IsPrimaryVehicle()) {
00126       if (v->type == vehicle_type && v->group_id == AIGroup::GROUP_DEFAULT) this->AddItem(v->index);
00127     }
00128   }
00129 }

Generated on Wed Feb 17 23:06:44 2010 for OpenTTD by  doxygen 1.6.1