Codechange: Replace FindVehicleOnPos with 'for' loop.

This commit is contained in:
frosch
2025-04-22 15:54:43 +02:00
committed by frosch
parent 06c399b79e
commit 2bee313642
12 changed files with 144 additions and 288 deletions

View File

@@ -10,6 +10,7 @@
#include "stdafx.h"
#include "roadveh.h"
#include "core/pool_func.hpp"
#include "core/container_func.hpp"
#include "roadstop_base.h"
#include "station_base.h"
#include "vehicle_func.h"
@@ -309,39 +310,6 @@ void RoadStop::Entry::Enter(const RoadVehicle *rv)
GetDriveThroughStopAxis(next) == GetDriveThroughStopAxis(rs);
}
typedef std::list<const RoadVehicle *> RVList; ///< A list of road vehicles
/** Helper for finding RVs in a road stop. */
struct RoadStopEntryRebuilderHelper {
RVList vehicles; ///< The list of vehicles to possibly add to.
DiagDirection dir; ///< The direction the vehicle has to face to be added.
};
/**
* Add road vehicles to the station's list if needed.
* @param v the found vehicle
* @param data the extra data used to make our decision
* @return always nullptr
*/
Vehicle *FindVehiclesInRoadStop(Vehicle *v, void *data)
{
RoadStopEntryRebuilderHelper *rserh = (RoadStopEntryRebuilderHelper*)data;
/* Not a RV or not in the right direction or crashed :( */
if (v->type != VEH_ROAD || DirToDiagDir(v->direction) != rserh->dir || !v->IsPrimaryVehicle() || v->vehstatus.Test(VehState::Crashed)) return nullptr;
RoadVehicle *rv = RoadVehicle::From(v);
/* Don't add ones not in a road stop */
if (rv->state < RVSB_IN_ROAD_STOP) return nullptr;
/* Do not add duplicates! */
for (const auto &it : rserh->vehicles) {
if (rv == it) return nullptr;
}
rserh->vehicles.push_back(rv);
return nullptr;
}
/**
* Get the DiagDirection for entering the drive through stop from the given 'side' (east or west) on the given axis.
* @param east Enter from the east when true or from the west when false.
@@ -369,18 +337,27 @@ void RoadStop::Entry::Rebuild(const RoadStop *rs, int side)
Axis axis = GetDriveThroughStopAxis(rs->xy);
if (side == -1) side = (rs->east == this);
RoadStopEntryRebuilderHelper rserh;
rserh.dir = GetEntryDirection(side, axis);
auto entry_dir = GetEntryDirection(side, axis);
std::vector<const RoadVehicle *> vehicles;
this->length = 0;
TileIndexDiff offset = TileOffsByAxis(axis);
for (TileIndex tile = rs->xy; IsDriveThroughRoadStopContinuation(rs->xy, tile); tile += offset) {
this->length += TILE_SIZE;
FindVehicleOnPos(tile, &rserh, FindVehiclesInRoadStop);
for (const Vehicle *v : VehiclesOnTile(tile)) {
/* Not a RV or not in the right direction or crashed :( */
if (v->type != VEH_ROAD || DirToDiagDir(v->direction) != entry_dir || !v->IsPrimaryVehicle() || v->vehstatus.Test(VehState::Crashed)) continue;
const RoadVehicle *rv = RoadVehicle::From(v);
/* Don't add ones not in a road stop */
if (rv->state < RVSB_IN_ROAD_STOP) continue;
include(vehicles, rv);
}
}
this->occupied = 0;
for (const auto &it : rserh.vehicles) {
for (const auto &it : vehicles) {
this->occupied += it->gcache.cached_total_length;
}
}