Switched to pure iterator space search

This commit is contained in:
Joshua Barretto 2020-11-21 23:32:23 +00:00
parent 4bb0da24b6
commit 54989cf4a6

View File

@ -113,24 +113,19 @@ impl TerrainGrid {
/// Find a location suitable for spawning an entity near the given
/// position (but in the same chunk).
pub fn find_space(&self, pos: Vec3<i32>) -> Vec3<i32> {
let mut z_diff = 0;
for _ in 0..128 {
let test_pos = pos + Vec3::unit_z() * z_diff;
if self
.get(test_pos - Vec3::unit_z())
.map(|b| b.is_filled())
.unwrap_or(false)
&& (0..2)
.all(|z| self
.get(test_pos + Vec3::unit_z() * z)
.map(|b| !b.is_solid())
.unwrap_or(true))
{
return test_pos;
}
z_diff = -z_diff + if z_diff <= 0 { 1 } else { 0 };
}
pos
const SEARCH_DIST: i32 = 63;
(0..SEARCH_DIST * 2 + 1)
.map(|i| if i % 2 == 0 { i } else { -i } / 2)
.map(|z_diff| pos + Vec3::unit_z() * z_diff)
.find(|test_pos| {
self.get(test_pos - Vec3::unit_z())
.map_or(false, |b| b.is_filled())
&& (0..2).all(|z| {
self.get(test_pos + Vec3::unit_z() * z)
.map_or(true, |b| !b.is_solid())
})
})
.unwrap_or(pos)
}
}