1 /*
2  * This file is part of OpenTTD.
3  * 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.
4  * 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.
5  * 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/>.
6  */
7 
8 /** @file script_vehiclelist.cpp Implementation of ScriptVehicleList and friends. */
9 
10 #include "../../stdafx.h"
11 #include "script_vehiclelist.hpp"
12 #include "script_group.hpp"
13 #include "script_map.hpp"
14 #include "script_station.hpp"
15 #include "../../depot_map.h"
16 #include "../../vehicle_base.h"
17 #include "../../train.h"
18 
19 #include "../../safeguards.h"
20 
ScriptVehicleList()21 ScriptVehicleList::ScriptVehicleList()
22 {
23 	for (const Vehicle *v : Vehicle::Iterate()) {
24 		if ((v->owner == ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY) && (v->IsPrimaryVehicle() || (v->type == VEH_TRAIN && ::Train::From(v)->IsFreeWagon()))) this->AddItem(v->index);
25 	}
26 }
27 
ScriptVehicleList_Station(StationID station_id)28 ScriptVehicleList_Station::ScriptVehicleList_Station(StationID station_id)
29 {
30 	if (!ScriptBaseStation::IsValidBaseStation(station_id)) return;
31 
32 	for (const Vehicle *v : Vehicle::Iterate()) {
33 		if ((v->owner == ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY) && v->IsPrimaryVehicle()) {
34 			for (const Order *order : v->Orders()) {
35 				if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT)) && order->GetDestination() == station_id) {
36 					this->AddItem(v->index);
37 					break;
38 				}
39 			}
40 		}
41 	}
42 }
43 
ScriptVehicleList_Depot(TileIndex tile)44 ScriptVehicleList_Depot::ScriptVehicleList_Depot(TileIndex tile)
45 {
46 	if (!ScriptMap::IsValidTile(tile)) return;
47 
48 	DestinationID dest;
49 	VehicleType type;
50 
51 	switch (GetTileType(tile)) {
52 		case MP_STATION: // Aircraft
53 			if (!IsAirport(tile)) return;
54 			type = VEH_AIRCRAFT;
55 			dest = GetStationIndex(tile);
56 			break;
57 
58 		case MP_RAILWAY:
59 			if (!IsRailDepot(tile)) return;
60 			type = VEH_TRAIN;
61 			dest = GetDepotIndex(tile);
62 			break;
63 
64 		case MP_ROAD:
65 			if (!IsRoadDepot(tile)) return;
66 			type = VEH_ROAD;
67 			dest = GetDepotIndex(tile);
68 			break;
69 
70 		case MP_WATER:
71 			if (!IsShipDepot(tile)) return;
72 			type = VEH_SHIP;
73 			dest = GetDepotIndex(tile);
74 			break;
75 
76 		default: // No depot
77 			return;
78 	}
79 
80 	for (const Vehicle *v : Vehicle::Iterate()) {
81 		if ((v->owner == ScriptObject::GetCompany() || ScriptObject::GetCompany() == OWNER_DEITY) && v->IsPrimaryVehicle() && v->type == type) {
82 			for (const Order *order : v->Orders()) {
83 				if (order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == dest) {
84 					this->AddItem(v->index);
85 					break;
86 				}
87 			}
88 		}
89 	}
90 }
91 
ScriptVehicleList_SharedOrders(VehicleID vehicle_id)92 ScriptVehicleList_SharedOrders::ScriptVehicleList_SharedOrders(VehicleID vehicle_id)
93 {
94 	if (!ScriptVehicle::IsValidVehicle(vehicle_id)) return;
95 
96 	for (const Vehicle *v = Vehicle::Get(vehicle_id)->FirstShared(); v != nullptr; v = v->NextShared()) {
97 		this->AddItem(v->index);
98 	}
99 }
100 
ScriptVehicleList_Group(GroupID group_id)101 ScriptVehicleList_Group::ScriptVehicleList_Group(GroupID group_id)
102 {
103 	if (!ScriptGroup::IsValidGroup((ScriptGroup::GroupID)group_id)) return;
104 
105 	for (const Vehicle *v : Vehicle::Iterate()) {
106 		if (v->owner == ScriptObject::GetCompany() && v->IsPrimaryVehicle()) {
107 			if (v->group_id == group_id) this->AddItem(v->index);
108 		}
109 	}
110 }
111 
ScriptVehicleList_DefaultGroup(ScriptVehicle::VehicleType vehicle_type)112 ScriptVehicleList_DefaultGroup::ScriptVehicleList_DefaultGroup(ScriptVehicle::VehicleType vehicle_type)
113 {
114 	if (vehicle_type < ScriptVehicle::VT_RAIL || vehicle_type > ScriptVehicle::VT_AIR) return;
115 
116 	for (const Vehicle *v : Vehicle::Iterate()) {
117 		if (v->owner == ScriptObject::GetCompany() && v->IsPrimaryVehicle()) {
118 			if (v->type == (::VehicleType)vehicle_type && v->group_id == ScriptGroup::GROUP_DEFAULT) this->AddItem(v->index);
119 		}
120 	}
121 }
122