diff options
| author | metamuffin <metamuffin@disroot.org> | 2024-09-25 00:31:56 +0200 | 
|---|---|---|
| committer | metamuffin <metamuffin@disroot.org> | 2024-09-25 00:31:56 +0200 | 
| commit | c764975c56f191db577aa65169c8b71fa2b2a5c6 (patch) | |
| tree | 2159613c7467841dcc64f4151206b6484ed2dcee /server | |
| parent | e4aae7680926d5023a22776d0c332d22c735b034 (diff) | |
| download | hurrycurry-c764975c56f191db577aa65169c8b71fa2b2a5c6.tar hurrycurry-c764975c56f191db577aa65169c8b71fa2b2a5c6.tar.bz2 hurrycurry-c764975c56f191db577aa65169c8b71fa2b2a5c6.tar.zst | |
begin actually implementing spatial index
Diffstat (limited to 'server')
| -rw-r--r-- | server/client-lib/src/spatial_index.rs | 16 | 
1 files changed, 14 insertions, 2 deletions
| diff --git a/server/client-lib/src/spatial_index.rs b/server/client-lib/src/spatial_index.rs index d4bd1776..3f0d5ad0 100644 --- a/server/client-lib/src/spatial_index.rs +++ b/server/client-lib/src/spatial_index.rs @@ -15,20 +15,30 @@      along with this program.  If not, see <https://www.gnu.org/licenses/>.  */ -use hurrycurry_protocol::glam::Vec2; +use hurrycurry_protocol::glam::{IVec2, Vec2};  use std::{collections::HashMap, hash::Hash};  // TODO stub implementation. please implement  pub struct SpatialIndex<T> {      entries: HashMap<T, Vec2>, +    bins: HashMap<IVec2, Vec<T>>,  }  impl<T: Eq + Hash + Copy> SpatialIndex<T> {      pub fn update_entry(&mut self, id: T, position: Vec2) {          self.entries.insert(id, position); +        let e = self.bins.entry(position.as_ivec2()).or_default(); +        if !e.contains(&id) { +            e.push(id); +        }      }      pub fn remove_entry(&mut self, id: T) { -        self.entries.remove(&id); +        if let Some(pos) = self.entries.remove(&id) { +            self.bins +                .entry(pos.as_ivec2()) +                .or_default() +                .retain(|e| *e != id); +        }      }      pub fn all(&self, mut cb: impl FnMut(T, Vec2)) {          for (&e, &pos) in &self.entries { @@ -36,6 +46,7 @@ impl<T: Eq + Hash + Copy> SpatialIndex<T> {          }      }      pub fn query(&self, position: Vec2, radius: f32, mut cb: impl FnMut(T, Vec2)) { +        // TODO query bins          self.all(|pl, p| {              if p.distance(position) < radius {                  cb(pl, p) @@ -47,6 +58,7 @@ impl<T> Default for SpatialIndex<T> {      fn default() -> Self {          Self {              entries: Default::default(), +            bins: Default::default(),          }      }  } | 
