mirror of https://github.com/OpenTTD/OpenTTD
Codechange: Build station and depot vehicle lists from shared order lists. (#11676)
The brings some performance advantages: * No need to iterate all vehicles and check for primary vehicle as only vehicles that can have orders are listed. * Shared orders only need to be tested once instead of for each vehicle sharing them. * Vehicle tests only need to be performed on the first shared vehicle instead of all.pull/11694/head
parent
7788b68bbe
commit
34e8c8e1c1
|
@ -510,6 +510,7 @@ add_files(
|
||||||
vehiclelist.cpp
|
vehiclelist.cpp
|
||||||
vehiclelist.h
|
vehiclelist.h
|
||||||
vehiclelist_cmd.h
|
vehiclelist_cmd.h
|
||||||
|
vehiclelist_func.h
|
||||||
viewport.cpp
|
viewport.cpp
|
||||||
viewport_cmd.h
|
viewport_cmd.h
|
||||||
viewport_func.h
|
viewport_func.h
|
||||||
|
|
|
@ -14,6 +14,7 @@
|
||||||
#include "script_station.hpp"
|
#include "script_station.hpp"
|
||||||
#include "../../depot_map.h"
|
#include "../../depot_map.h"
|
||||||
#include "../../vehicle_base.h"
|
#include "../../vehicle_base.h"
|
||||||
|
#include "../../vehiclelist_func.h"
|
||||||
#include "../../train.h"
|
#include "../../train.h"
|
||||||
#include "../../core/backup_type.hpp"
|
#include "../../core/backup_type.hpp"
|
||||||
#include <../squirrel/sqvm.h>
|
#include <../squirrel/sqvm.h>
|
||||||
|
@ -111,16 +112,12 @@ ScriptVehicleList_Station::ScriptVehicleList_Station(StationID station_id)
|
||||||
|
|
||||||
bool is_deity = ScriptCompanyMode::IsDeity();
|
bool is_deity = ScriptCompanyMode::IsDeity();
|
||||||
CompanyID owner = ScriptObject::GetCompany();
|
CompanyID owner = ScriptObject::GetCompany();
|
||||||
for (const Vehicle *v : Vehicle::Iterate()) {
|
|
||||||
if ((v->owner == owner || is_deity) && v->IsPrimaryVehicle()) {
|
FindVehiclesWithOrder(
|
||||||
for (const Order *order : v->Orders()) {
|
[is_deity, owner](const Vehicle *v) { return is_deity || v->owner == owner; },
|
||||||
if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT)) && order->GetDestination() == station_id) {
|
[station_id](const Order *order) { return (order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT)) && order->GetDestination() == station_id; },
|
||||||
this->AddItem(v->index);
|
[this](const Vehicle *v) { this->AddItem(v->index); }
|
||||||
break;
|
);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptVehicleList_Depot::ScriptVehicleList_Depot(TileIndex tile)
|
ScriptVehicleList_Depot::ScriptVehicleList_Depot(TileIndex tile)
|
||||||
|
@ -162,16 +159,12 @@ ScriptVehicleList_Depot::ScriptVehicleList_Depot(TileIndex tile)
|
||||||
|
|
||||||
bool is_deity = ScriptCompanyMode::IsDeity();
|
bool is_deity = ScriptCompanyMode::IsDeity();
|
||||||
CompanyID owner = ScriptObject::GetCompany();
|
CompanyID owner = ScriptObject::GetCompany();
|
||||||
for (const Vehicle *v : Vehicle::Iterate()) {
|
|
||||||
if ((v->owner == owner || is_deity) && v->IsPrimaryVehicle() && v->type == type) {
|
FindVehiclesWithOrder(
|
||||||
for (const Order *order : v->Orders()) {
|
[is_deity, owner, type](const Vehicle *v) { return (is_deity || v->owner == owner) && v->type == type; },
|
||||||
if (order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == dest) {
|
[dest](const Order *order) { return order->IsType(OT_GOTO_DEPOT) && order->GetDestination() == dest; },
|
||||||
this->AddItem(v->index);
|
[this](const Vehicle *v) { this->AddItem(v->index); }
|
||||||
break;
|
);
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
ScriptVehicleList_SharedOrders::ScriptVehicleList_SharedOrders(VehicleID vehicle_id)
|
ScriptVehicleList_SharedOrders::ScriptVehicleList_SharedOrders(VehicleID vehicle_id)
|
||||||
|
|
|
@ -10,6 +10,7 @@
|
||||||
#include "stdafx.h"
|
#include "stdafx.h"
|
||||||
#include "train.h"
|
#include "train.h"
|
||||||
#include "vehiclelist.h"
|
#include "vehiclelist.h"
|
||||||
|
#include "vehiclelist_func.h"
|
||||||
#include "group.h"
|
#include "group.h"
|
||||||
|
|
||||||
#include "safeguards.h"
|
#include "safeguards.h"
|
||||||
|
@ -116,17 +117,11 @@ bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli
|
||||||
|
|
||||||
switch (vli.type) {
|
switch (vli.type) {
|
||||||
case VL_STATION_LIST:
|
case VL_STATION_LIST:
|
||||||
for (const Vehicle *v : Vehicle::Iterate()) {
|
FindVehiclesWithOrder(
|
||||||
if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
|
[&vli](const Vehicle *v) { return v->type == vli.vtype; },
|
||||||
for (const Order *order : v->Orders()) {
|
[&vli](const Order *order) { return (order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_IMPLICIT)) && order->GetDestination() == vli.index; },
|
||||||
if ((order->IsType(OT_GOTO_STATION) || order->IsType(OT_GOTO_WAYPOINT) || order->IsType(OT_IMPLICIT))
|
[&list](const Vehicle *v) { list->push_back(v); }
|
||||||
&& order->GetDestination() == vli.index) {
|
);
|
||||||
list->push_back(v);
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case VL_SHARED_ORDERS: {
|
case VL_SHARED_ORDERS: {
|
||||||
|
@ -161,16 +156,11 @@ bool GenerateVehicleSortList(VehicleList *list, const VehicleListIdentifier &vli
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case VL_DEPOT_LIST:
|
case VL_DEPOT_LIST:
|
||||||
for (const Vehicle *v : Vehicle::Iterate()) {
|
FindVehiclesWithOrder(
|
||||||
if (v->type == vli.vtype && v->IsPrimaryVehicle()) {
|
[&vli](const Vehicle *v) { return v->type == vli.vtype; },
|
||||||
for (const Order *order : v->Orders()) {
|
[&vli](const Order *order) { return order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.index; },
|
||||||
if (order->IsType(OT_GOTO_DEPOT) && !(order->GetDepotActionType() & ODATFB_NEAREST_DEPOT) && order->GetDestination() == vli.index) {
|
[&list](const Vehicle *v) { list->push_back(v); }
|
||||||
list->push_back(v);
|
);
|
||||||
break;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
break;
|
break;
|
||||||
|
|
||||||
default: return false;
|
default: return false;
|
||||||
|
|
|
@ -0,0 +1,46 @@
|
||||||
|
/*
|
||||||
|
* This file is part of OpenTTD.
|
||||||
|
* 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.
|
||||||
|
* 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.
|
||||||
|
* 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/>.
|
||||||
|
*/
|
||||||
|
|
||||||
|
/** @file vehiclelist_func.h Functions and type for generating vehicle lists. */
|
||||||
|
|
||||||
|
#ifndef VEHICLELIST_FUNC_H
|
||||||
|
#define VEHICLELIST_FUNC_H
|
||||||
|
|
||||||
|
#include "order_base.h"
|
||||||
|
#include "vehicle_base.h"
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Find vehicles matching an order.
|
||||||
|
* This can be used, e.g. to find all vehicles that stop at a particular station.
|
||||||
|
* @param veh_pred Vehicle selection predicate. This is called only for the first vehicle using the order list.
|
||||||
|
* @param ord_pred Order selection predicate.
|
||||||
|
* @param veh_func Called for each vehicle that matches both vehicle and order predicates.
|
||||||
|
**/
|
||||||
|
template <class VehiclePredicate, class OrderPredicate, class VehicleFunc>
|
||||||
|
void FindVehiclesWithOrder(VehiclePredicate veh_pred, OrderPredicate ord_pred, VehicleFunc veh_func)
|
||||||
|
{
|
||||||
|
for (const OrderList *orderlist : OrderList::Iterate()) {
|
||||||
|
|
||||||
|
/* We assume all vehicles sharing an order list match the condition. */
|
||||||
|
const Vehicle *v = orderlist->GetFirstSharedVehicle();
|
||||||
|
if (!veh_pred(v)) continue;
|
||||||
|
|
||||||
|
/* Vehicle is a candidate, search for a matching order. */
|
||||||
|
for (const Order *order = orderlist->GetFirstOrder(); order != nullptr; order = order->next) {
|
||||||
|
|
||||||
|
if (!ord_pred(order)) continue;
|
||||||
|
|
||||||
|
/* An order matches, we can add all shared vehicles to the list. */
|
||||||
|
for (; v != nullptr; v = v->NextShared()) {
|
||||||
|
veh_func(v);
|
||||||
|
}
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif /* VEHICLELIST_FUNC_H */
|
Loading…
Reference in New Issue