1
0
Fork 0

Codechange: Pass the max-distance as parameter to VehiclesNearTileXY.

pull/14116/head
frosch 2025-04-26 15:23:14 +02:00 committed by frosch
parent 2df1233f1f
commit 3a70d1e2f7
4 changed files with 25 additions and 18 deletions

View File

@ -553,7 +553,7 @@ static bool RoadVehCheckTrainCrash(RoadVehicle *v)
if (!IsLevelCrossingTile(tile)) continue;
if (HasVehicleNearTileXY(v->x_pos, v->y_pos, [&u](const Vehicle *t) {
if (HasVehicleNearTileXY(v->x_pos, v->y_pos, 6, [&u](const Vehicle *t) {
return t->type == VEH_TRAIN && abs(t->z_pos - u->z_pos) <= 6 &&
abs(t->x_pos - u->x_pos) <= 4 && abs(t->y_pos - u->y_pos) <= 4;
})) {
@ -649,7 +649,7 @@ static RoadVehicle *RoadVehFindCloseTo(RoadVehicle *v, int x, int y, Direction d
FindClosestBlockingRoadVeh(u, &rvf);
}
} else {
for (Vehicle *u : VehiclesNearTileXY(x, y)) {
for (Vehicle *u : VehiclesNearTileXY(x, y, 6)) {
FindClosestBlockingRoadVeh(u, &rvf);
}
}

View File

@ -3228,7 +3228,7 @@ static bool CheckTrainCollision(Train *v)
num_victims += CheckTrainCollision(u, v);
}
} else {
for (Vehicle *u : VehiclesNearTileXY(v->x_pos, v->y_pos)) {
for (Vehicle *u : VehiclesNearTileXY(v->x_pos, v->y_pos, 6)) {
num_victims += CheckTrainCollision(u, v);
}
}

View File

@ -429,20 +429,27 @@ static std::array<Vehicle *, TOTAL_TILE_HASH_SIZE> _vehicle_tile_hash{};
* Iterator constructor.
* Find first vehicle near (x, y).
*/
VehiclesNearTileXY::Iterator::Iterator(int32_t x, int32_t y)
VehiclesNearTileXY::Iterator::Iterator(int32_t x, int32_t y, uint max_dist)
{
const int COLL_DIST = 6;
/* There are no negative tile coordinates */
pos_rect.left = std::max<int>(0, x - COLL_DIST);
pos_rect.right = std::max<int>(0, x + COLL_DIST);
pos_rect.top = std::max<int>(0, y - COLL_DIST);
pos_rect.bottom = std::max<int>(0, y + COLL_DIST);
pos_rect.left = std::max<int>(0, x - max_dist);
pos_rect.right = std::max<int>(0, x + max_dist);
pos_rect.top = std::max<int>(0, y - max_dist);
pos_rect.bottom = std::max<int>(0, y + max_dist);
if (2 * max_dist < TILE_HASH_MASK * TILE_SIZE) {
/* Hash area to scan */
this->hxmin = this->hx = GetTileHash1D(pos_rect.left / TILE_SIZE);
this->hxmax = GetTileHash1D(pos_rect.right / TILE_SIZE);
this->hymin = this->hy = GetTileHash1D(pos_rect.top / TILE_SIZE);
this->hymax = GetTileHash1D(pos_rect.bottom / TILE_SIZE);
} else {
/* Scan all */
this->hxmin = this->hx = 0;
this->hxmax = TILE_HASH_MASK;
this->hymin = this->hy = 0;
this->hymax = TILE_HASH_MASK;
}
this->current_veh = _vehicle_tile_hash[ComposeTileHash(this->hx, this->hy)];
this->SkipEmptyBuckets();

View File

@ -129,7 +129,7 @@ public:
using pointer = void;
using reference = void;
explicit Iterator(int32_t x, int32_t y);
explicit Iterator(int32_t x, int32_t y, uint max_dist);
bool operator==(const Iterator &rhs) const { return this->current_veh == rhs.current_veh; }
bool operator==(const std::default_sentinel_t &) const { return this->current_veh == nullptr; }
@ -160,7 +160,7 @@ public:
void SkipFalseMatches();
};
explicit VehiclesNearTileXY(int32_t x, int32_t y) : start(x, y) {}
explicit VehiclesNearTileXY(int32_t x, int32_t y, uint max_dist) : start(x, y, max_dist) {}
Iterator begin() const { return this->start; }
std::default_sentinel_t end() const { return std::default_sentinel_t(); }
private:
@ -173,9 +173,9 @@ private:
* @warning This only works for vehicles with proper Vehicle::Tile, so only ground vehicles outside wormholes.
*/
template <class UnaryPred>
bool HasVehicleNearTileXY(int32_t x, int32_t y, UnaryPred &&predicate)
bool HasVehicleNearTileXY(int32_t x, int32_t y, uint max_dist, UnaryPred &&predicate)
{
for (const auto *v : VehiclesNearTileXY(x, y)) {
for (const auto *v : VehiclesNearTileXY(x, y, max_dist)) {
if (predicate(v)) return true;
}
return false;