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/>.
8 /** @file script_vehiclelist.cpp Implementation of ScriptVehicleList and friends. */
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 "../../vehiclelist_func.h"
18 #include "../../train.h"
20 #include "../../safeguards.h"
22 ScriptVehicleList::ScriptVehicleList(HSQUIRRELVM vm
)
24 EnforceDeityOrCompanyModeValid_Void();
26 bool is_deity
= ScriptCompanyMode::IsDeity();
27 CompanyID owner
= ScriptObject::GetCompany();
29 ScriptList::FillList
<Vehicle
>(vm
, this,
30 [is_deity
, owner
](const Vehicle
*v
) {
31 return (is_deity
|| v
->owner
== owner
) && (v
->IsPrimaryVehicle() || (v
->type
== VEH_TRAIN
&& ::Train::From(v
)->IsFreeWagon()));
36 ScriptVehicleList_Station::ScriptVehicleList_Station(StationID station_id
)
38 EnforceDeityOrCompanyModeValid_Void();
39 if (!ScriptBaseStation::IsValidBaseStation(station_id
)) return;
41 bool is_deity
= ScriptCompanyMode::IsDeity();
42 CompanyID owner
= ScriptObject::GetCompany();
44 FindVehiclesWithOrder(
45 [is_deity
, owner
](const Vehicle
*v
) { return is_deity
|| v
->owner
== owner
; },
46 [station_id
](const Order
*order
) { return (order
->IsType(OT_GOTO_STATION
) || order
->IsType(OT_GOTO_WAYPOINT
)) && order
->GetDestination() == station_id
; },
47 [this](const Vehicle
*v
) { this->AddItem(v
->index
); }
51 ScriptVehicleList_Depot::ScriptVehicleList_Depot(TileIndex tile
)
53 EnforceDeityOrCompanyModeValid_Void();
54 if (!ScriptMap::IsValidTile(tile
)) return;
59 switch (GetTileType(tile
)) {
60 case MP_STATION
: // Aircraft
61 if (!IsAirport(tile
)) return;
63 dest
= GetStationIndex(tile
);
67 if (!IsRailDepot(tile
)) return;
69 dest
= GetDepotIndex(tile
);
73 if (!IsRoadDepot(tile
)) return;
75 dest
= GetDepotIndex(tile
);
79 if (!IsShipDepot(tile
)) return;
81 dest
= GetDepotIndex(tile
);
88 bool is_deity
= ScriptCompanyMode::IsDeity();
89 CompanyID owner
= ScriptObject::GetCompany();
91 FindVehiclesWithOrder(
92 [is_deity
, owner
, type
](const Vehicle
*v
) { return (is_deity
|| v
->owner
== owner
) && v
->type
== type
; },
93 [dest
](const Order
*order
) { return order
->IsType(OT_GOTO_DEPOT
) && order
->GetDestination() == dest
; },
94 [this](const Vehicle
*v
) { this->AddItem(v
->index
); }
98 ScriptVehicleList_SharedOrders::ScriptVehicleList_SharedOrders(VehicleID vehicle_id
)
100 if (!ScriptVehicle::IsPrimaryVehicle(vehicle_id
)) return;
102 for (const Vehicle
*v
= Vehicle::Get(vehicle_id
)->FirstShared(); v
!= nullptr; v
= v
->NextShared()) {
103 this->AddItem(v
->index
);
107 ScriptVehicleList_Group::ScriptVehicleList_Group(GroupID group_id
)
109 EnforceCompanyModeValid_Void();
110 if (!ScriptGroup::IsValidGroup((ScriptGroup::GroupID
)group_id
)) return;
112 CompanyID owner
= ScriptObject::GetCompany();
114 ScriptList::FillList
<Vehicle
>(this,
115 [owner
](const Vehicle
*v
) { return v
->owner
== owner
&& v
->IsPrimaryVehicle(); },
116 [group_id
](const Vehicle
*v
) { return v
->group_id
== group_id
; }
120 ScriptVehicleList_DefaultGroup::ScriptVehicleList_DefaultGroup(ScriptVehicle::VehicleType vehicle_type
)
122 EnforceCompanyModeValid_Void();
123 if (vehicle_type
< ScriptVehicle::VT_RAIL
|| vehicle_type
> ScriptVehicle::VT_AIR
) return;
125 CompanyID owner
= ScriptObject::GetCompany();
127 ScriptList::FillList
<Vehicle
>(this,
128 [owner
](const Vehicle
*v
) { return v
->owner
== owner
&& v
->IsPrimaryVehicle(); },
129 [vehicle_type
](const Vehicle
*v
) { return v
->type
== (::VehicleType
)vehicle_type
&& v
->group_id
== ScriptGroup::GROUP_DEFAULT
; }