Codechange: Add iterator for vehicles on a tile.

This commit is contained in:
frosch
2025-04-22 13:58:21 +02:00
committed by frosch
parent 917ef03e97
commit fca3103d8c
2 changed files with 81 additions and 0 deletions
+29
View File
@@ -471,6 +471,35 @@ bool HasVehicleOnPosXY(int x, int y, void *data, VehicleFromPosProc *proc)
return VehicleFromPosXY(x, y, data, proc, true) != nullptr;
}
/**
* Iterator constructor.
* Find first vehicle on tile.
*/
VehiclesOnTile::Iterator::Iterator(TileIndex tile) : tile(tile)
{
int x = GB(TileX(tile), HASH_RES, HASH_BITS);
int y = GB(TileY(tile), HASH_RES, HASH_BITS) << HASH_BITS;
this->current = _vehicle_tile_hash[(x + y) & TOTAL_HASH_MASK];
this->SkipFalseMatches();
}
/**
* Advance the internal state to the next potential vehicle.
* The vehicle may not be on the correct tile though.
*/
void VehiclesOnTile::Iterator::Increment()
{
this->current = this->current->hash_tile_next;
}
/**
* Advance the internal state until it reaches a vehicle on the correct tile or the end.
*/
void VehiclesOnTile::Iterator::SkipFalseMatches()
{
while (this->current != nullptr && this->current->tile != this->tile) this->Increment();
}
/**
* Helper function for FindVehicleOnPos/HasVehicleOnPos.
* @note Do not call this function directly!