2023-06-04 02:23:42 +00:00
|
|
|
use crate::comp::{Pos, Presence, Vel};
|
2021-03-08 22:40:02 +00:00
|
|
|
use common_base::span;
|
2019-10-06 17:35:47 +00:00
|
|
|
use hashbrown::{hash_map::DefaultHashBuilder, HashSet};
|
2019-10-04 00:41:42 +00:00
|
|
|
use indexmap::IndexMap;
|
2023-09-15 20:30:54 +00:00
|
|
|
use specs::{hibitset::BitSetLike, BitSet, Entities, Join, LendJoin, ReadStorage};
|
2019-10-04 00:41:42 +00:00
|
|
|
use vek::*;
|
|
|
|
|
2019-10-06 17:35:47 +00:00
|
|
|
pub enum Event {
|
|
|
|
// Contains the key of the region the entity moved to
|
2023-06-02 23:54:08 +00:00
|
|
|
// TODO: We only actually use the info from this when the entity moves to another region and
|
|
|
|
// isn't deleted, but we still generate and process these events in the case where the entity
|
|
|
|
// was deleted.
|
2019-10-06 17:35:47 +00:00
|
|
|
Left(u32, Option<Vec2<i32>>),
|
|
|
|
// Contains the key of the region the entity came from
|
|
|
|
Entered(u32, Option<Vec2<i32>>),
|
|
|
|
}
|
|
|
|
|
2019-10-04 00:41:42 +00:00
|
|
|
/// Region consisting of a bitset of entities within it
|
2021-12-10 10:36:46 +00:00
|
|
|
#[derive(Default)]
|
2019-10-06 17:35:47 +00:00
|
|
|
pub struct Region {
|
2019-10-04 00:41:42 +00:00
|
|
|
// Use specs bitset for simplicity (and joinability)
|
|
|
|
bitset: BitSet,
|
|
|
|
// Indices of neighboring regions
|
|
|
|
neighbors: [Option<usize>; 8],
|
2019-10-06 17:35:47 +00:00
|
|
|
// TODO consider SmallVec for these
|
2020-08-25 12:21:25 +00:00
|
|
|
// Entities that left or entered this region
|
2019-10-06 17:35:47 +00:00
|
|
|
events: Vec<Event>,
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
2019-10-06 17:35:47 +00:00
|
|
|
impl Region {
|
|
|
|
/// Checks if the region contains no entities and no events
|
2020-02-01 20:39:39 +00:00
|
|
|
fn removable(&self) -> bool { self.bitset.is_empty() && self.events.is_empty() }
|
|
|
|
|
2019-10-06 17:35:47 +00:00
|
|
|
fn add(&mut self, id: u32, from: Option<Vec2<i32>>) {
|
|
|
|
self.bitset.add(id);
|
|
|
|
self.events.push(Event::Entered(id, from));
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-10-06 17:35:47 +00:00
|
|
|
fn remove(&mut self, id: u32, to: Option<Vec2<i32>>) {
|
|
|
|
self.bitset.remove(id);
|
|
|
|
self.events.push(Event::Left(id, to));
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
|
|
|
pub fn events(&self) -> &[Event] { &self.events }
|
|
|
|
|
|
|
|
pub fn entities(&self) -> &BitSet { &self.bitset }
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
/// How far can an entity roam outside its region before it is switched over to
|
|
|
|
/// the neighboring one In units of blocks (i.e. world pos)
|
2019-10-04 00:41:42 +00:00
|
|
|
/// Used to prevent rapid switching of entities between regions
|
2019-10-06 17:35:47 +00:00
|
|
|
pub const TETHER_LENGTH: u32 = 16;
|
2019-10-07 01:00:47 +00:00
|
|
|
/// Bitshift between region and world pos, i.e. log2(REGION_SIZE)
|
2019-10-06 17:35:47 +00:00
|
|
|
const REGION_LOG2: u8 = 9;
|
2019-10-07 01:00:47 +00:00
|
|
|
/// Region Size in blocks
|
|
|
|
pub const REGION_SIZE: u32 = 1 << REGION_LOG2;
|
2019-10-04 00:41:42 +00:00
|
|
|
/// Offsets to iterate though neighbors
|
|
|
|
/// Counter-clockwise order
|
|
|
|
const NEIGHBOR_OFFSETS: [Vec2<i32>; 8] = [
|
|
|
|
Vec2::new(0, 1),
|
|
|
|
Vec2::new(-1, 1),
|
|
|
|
Vec2::new(-1, 0),
|
|
|
|
Vec2::new(-1, -1),
|
|
|
|
Vec2::new(0, -1),
|
|
|
|
Vec2::new(1, -1),
|
|
|
|
Vec2::new(1, 0),
|
|
|
|
Vec2::new(1, 1),
|
|
|
|
];
|
|
|
|
|
2021-12-10 10:36:46 +00:00
|
|
|
#[derive(Default)]
|
2019-10-04 00:41:42 +00:00
|
|
|
// TODO generic region size (16x16 for now)
|
|
|
|
// TODO compare to sweep and prune approach
|
2023-06-04 02:23:42 +00:00
|
|
|
/// A region system that tracks where entities are.
|
|
|
|
///
|
|
|
|
/// Note, this structure is primarily intended for tracking which entities need
|
|
|
|
/// to be synchronized to which clients (and as part of that what entities are
|
|
|
|
/// already synchronized). If an entity is marked to not be synchronized to
|
|
|
|
/// other clients it may not appear here.
|
2019-10-06 17:35:47 +00:00
|
|
|
pub struct RegionMap {
|
2019-10-04 00:41:42 +00:00
|
|
|
// Tree?
|
|
|
|
// Sorted Vec? (binary search lookup)
|
2020-02-01 20:39:39 +00:00
|
|
|
// Sort into multiple vecs (say 32) using lower bits of morton code, then binary search via
|
|
|
|
// upper bits? <-- sounds very promising to me (might not be super good though?)
|
2019-10-06 17:35:47 +00:00
|
|
|
regions: IndexMap<Vec2<i32>, Region, DefaultHashBuilder>,
|
2019-10-04 00:41:42 +00:00
|
|
|
// If an entity isn't here it needs to be added to a region
|
|
|
|
tracked_entities: BitSet,
|
|
|
|
// Re-useable vecs
|
|
|
|
// (src, entity, pos)
|
|
|
|
entities_to_move: Vec<(usize, u32, Vec3<i32>)>,
|
|
|
|
// (region, entity)
|
|
|
|
entities_to_remove: Vec<(usize, u32)>,
|
2019-10-06 17:35:47 +00:00
|
|
|
// Track the current tick, used to enable not checking everything every tick
|
2021-12-10 10:36:46 +00:00
|
|
|
// rate is dependent on the rate the caller calls region_manager.tick()
|
2019-10-06 17:35:47 +00:00
|
|
|
tick: u64,
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
2019-10-06 17:35:47 +00:00
|
|
|
impl RegionMap {
|
2021-12-10 10:36:46 +00:00
|
|
|
pub fn new() -> Self { Self::default() }
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-10-06 17:35:47 +00:00
|
|
|
// TODO maintain within a system?
|
|
|
|
// TODO special case large entities
|
2023-06-04 02:23:42 +00:00
|
|
|
pub fn tick(
|
|
|
|
&mut self,
|
|
|
|
pos: ReadStorage<Pos>,
|
|
|
|
vel: ReadStorage<Vel>,
|
|
|
|
presence: ReadStorage<Presence>,
|
|
|
|
entities: Entities,
|
|
|
|
) {
|
2020-09-07 04:59:16 +00:00
|
|
|
span!(_guard, "tick", "Region::tick");
|
2019-10-06 17:35:47 +00:00
|
|
|
self.tick += 1;
|
2019-11-29 06:04:37 +00:00
|
|
|
// Clear events within each region
|
2021-04-09 07:34:58 +00:00
|
|
|
self.regions.values_mut().for_each(|region| {
|
|
|
|
region.events.clear();
|
|
|
|
});
|
2019-11-29 06:04:37 +00:00
|
|
|
|
2020-08-25 12:21:25 +00:00
|
|
|
// Add any untracked entities
|
2023-06-04 02:23:42 +00:00
|
|
|
for (pos, id) in (&pos, &entities, presence.maybe(), !&self.tracked_entities)
|
2019-10-04 00:41:42 +00:00
|
|
|
.join()
|
2023-08-12 00:09:10 +00:00
|
|
|
.filter(|(_, _, presence, _)| presence.map_or(true, |p| p.kind.sync_me()))
|
2023-06-04 02:23:42 +00:00
|
|
|
.map(|(pos, e, _, _)| (pos, e.id()))
|
2019-10-04 00:41:42 +00:00
|
|
|
.collect::<Vec<_>>()
|
|
|
|
{
|
|
|
|
// Add entity
|
2019-10-06 17:35:47 +00:00
|
|
|
self.tracked_entities.add(id);
|
|
|
|
self.add_entity(id, pos.0.map(|e| e as i32), None);
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 17:35:47 +00:00
|
|
|
let mut regions_to_remove = Vec::new();
|
2019-10-04 00:41:42 +00:00
|
|
|
|
2021-04-09 07:34:58 +00:00
|
|
|
let RegionMap {
|
|
|
|
entities_to_move,
|
|
|
|
entities_to_remove,
|
|
|
|
regions,
|
|
|
|
..
|
|
|
|
} = self;
|
|
|
|
regions
|
|
|
|
.iter()
|
|
|
|
.enumerate()
|
|
|
|
.for_each(|(i, (¤t_region, region_data))| {
|
2023-06-04 02:23:42 +00:00
|
|
|
for (maybe_pos, _maybe_vel, maybe_presence, id) in (
|
|
|
|
pos.maybe(),
|
|
|
|
vel.maybe(),
|
|
|
|
presence.maybe(),
|
|
|
|
®ion_data.bitset,
|
|
|
|
)
|
|
|
|
.join()
|
2021-04-09 07:34:58 +00:00
|
|
|
{
|
2023-08-12 00:09:10 +00:00
|
|
|
let should_sync = maybe_presence.map_or(true, |p| p.kind.sync_me());
|
2021-04-09 07:34:58 +00:00
|
|
|
match maybe_pos {
|
|
|
|
// Switch regions for entities which need switching
|
|
|
|
// TODO don't check every tick (use velocity) (and use id to stagger)
|
|
|
|
// Starting parameters at v = 0 check every 100 ticks
|
|
|
|
// tether_length^2 / vel^2 (with a max of every tick)
|
2023-06-04 02:23:42 +00:00
|
|
|
Some(pos) if should_sync => {
|
2021-04-09 07:34:58 +00:00
|
|
|
let pos = pos.0.map(|e| e as i32);
|
|
|
|
let key = Self::pos_key(pos);
|
|
|
|
// Consider switching
|
|
|
|
// Calculate distance outside border
|
|
|
|
if key != current_region
|
|
|
|
&& (Vec2::<i32>::from(pos) - Self::key_pos(current_region))
|
2022-04-25 11:12:02 +00:00
|
|
|
.map(|e| e.unsigned_abs())
|
2021-04-09 07:34:58 +00:00
|
|
|
.reduce_max()
|
|
|
|
> TETHER_LENGTH
|
|
|
|
{
|
|
|
|
// Switch
|
|
|
|
entities_to_move.push((i, id, pos));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
// Remove any non-existant entities (or just ones that lost their position
|
|
|
|
// component) TODO: distribute this between ticks
|
2023-06-04 02:23:42 +00:00
|
|
|
None | Some(_) => {
|
2021-04-09 07:34:58 +00:00
|
|
|
// TODO: shouldn't there be a way to extract the bitset of entities with
|
2023-06-02 23:54:08 +00:00
|
|
|
// positions directly from specs? Yes, with `.mask()` on the component
|
|
|
|
// storage.
|
2021-04-09 07:34:58 +00:00
|
|
|
entities_to_remove.push((i, id));
|
|
|
|
},
|
|
|
|
}
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
|
|
|
|
2021-04-09 07:34:58 +00:00
|
|
|
// Remove region if it is empty
|
|
|
|
// TODO: distribute this between ticks
|
|
|
|
if region_data.removable() {
|
|
|
|
regions_to_remove.push(current_region);
|
|
|
|
}
|
|
|
|
});
|
2019-10-04 00:41:42 +00:00
|
|
|
|
|
|
|
// Mutate
|
2020-02-01 20:39:39 +00:00
|
|
|
// Note entity moving is outside the whole loop so that the same entity is not
|
|
|
|
// checked twice (this may be fine though...)
|
2019-10-06 17:35:47 +00:00
|
|
|
while let Some((i, id, pos)) = self.entities_to_move.pop() {
|
|
|
|
let (prev_key, region) = self.regions.get_index_mut(i).map(|(k, v)| (*k, v)).unwrap();
|
|
|
|
region.remove(id, Some(Self::pos_key(pos)));
|
|
|
|
|
|
|
|
self.add_entity(id, pos, Some(prev_key));
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
2019-10-06 17:35:47 +00:00
|
|
|
for (i, id) in self.entities_to_remove.drain(..) {
|
2019-10-04 00:41:42 +00:00
|
|
|
self.regions
|
|
|
|
.get_index_mut(i)
|
|
|
|
.map(|(_, v)| v)
|
|
|
|
.unwrap()
|
2019-10-06 17:35:47 +00:00
|
|
|
.remove(id, None);
|
|
|
|
self.tracked_entities.remove(id);
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
2019-10-07 01:00:47 +00:00
|
|
|
for key in regions_to_remove.into_iter() {
|
2019-10-06 19:38:54 +00:00
|
|
|
// Check that the region is still removable
|
2019-10-07 01:00:47 +00:00
|
|
|
if self.regions.get(&key).unwrap().removable() {
|
2020-02-01 20:39:39 +00:00
|
|
|
// Note we have to use key's here since the index can change when others are
|
|
|
|
// removed
|
2019-10-07 01:00:47 +00:00
|
|
|
self.remove(key);
|
2019-10-06 19:38:54 +00:00
|
|
|
}
|
|
|
|
}
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2023-06-06 06:25:45 +00:00
|
|
|
/// Must be called immediately after succesfully deleting an entity from the
|
|
|
|
/// ecs (i.e. when deleting the entity did not generate a WrongGeneration
|
|
|
|
/// error).
|
|
|
|
pub fn entity_deleted(&mut self, entity: specs::Entity) {
|
|
|
|
self.tracked_entities.remove(entity.id());
|
|
|
|
}
|
|
|
|
|
2019-10-06 17:35:47 +00:00
|
|
|
fn add_entity(&mut self, id: u32, pos: Vec3<i32>, from: Option<Vec2<i32>>) {
|
2019-10-04 00:41:42 +00:00
|
|
|
let key = Self::pos_key(pos);
|
|
|
|
if let Some(region) = self.regions.get_mut(&key) {
|
2019-10-06 17:35:47 +00:00
|
|
|
region.add(id, from);
|
2019-10-04 00:41:42 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-10-06 17:35:47 +00:00
|
|
|
let index = self.insert(key);
|
|
|
|
self.regions
|
|
|
|
.get_index_mut(index)
|
|
|
|
.map(|(_, v)| v)
|
|
|
|
.unwrap()
|
|
|
|
.add(id, None);
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
|
|
|
fn pos_key<P: Into<Vec2<i32>>>(pos: P) -> Vec2<i32> { pos.into().map(|e| e >> REGION_LOG2) }
|
|
|
|
|
|
|
|
pub fn key_pos(key: Vec2<i32>) -> Vec2<i32> { key.map(|e| e << REGION_LOG2) }
|
|
|
|
|
|
|
|
/// Finds the region where a given entity is located using a given position
|
|
|
|
/// to speed up the search
|
2019-11-29 06:04:37 +00:00
|
|
|
pub fn find_region(&self, entity: specs::Entity, pos: Vec3<f32>) -> Option<Vec2<i32>> {
|
|
|
|
let id = entity.id();
|
|
|
|
// Compute key for most likely region
|
|
|
|
let key = Self::pos_key(pos.map(|e| e as i32));
|
|
|
|
// Get region
|
|
|
|
if let Some(region) = self.regions.get(&key) {
|
|
|
|
if region.entities().contains(id) {
|
|
|
|
return Some(key);
|
|
|
|
} else {
|
|
|
|
// Check neighbors
|
2021-03-12 11:10:42 +00:00
|
|
|
for idx in region.neighbors.iter().flatten() {
|
|
|
|
let (key, region) = self.regions.get_index(*idx).unwrap();
|
|
|
|
if region.entities().contains(id) {
|
|
|
|
return Some(*key);
|
2019-11-29 06:04:37 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2023-06-06 06:25:45 +00:00
|
|
|
} else if !self.tracked_entities.contains(id) {
|
|
|
|
return None;
|
2019-11-29 06:04:37 +00:00
|
|
|
} else {
|
|
|
|
// Check neighbors
|
2020-06-30 14:56:49 +00:00
|
|
|
for o in &NEIGHBOR_OFFSETS {
|
|
|
|
let key = key + o;
|
2019-11-29 06:04:37 +00:00
|
|
|
if let Some(region) = self.regions.get(&key) {
|
|
|
|
if region.entities().contains(id) {
|
|
|
|
return Some(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Scan though all regions
|
|
|
|
for (key, region) in self.iter() {
|
|
|
|
if region.entities().contains(id) {
|
|
|
|
return Some(key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
None
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2023-06-06 06:25:45 +00:00
|
|
|
/// Checks if this entity is located in the `RegionMap`.
|
|
|
|
pub fn in_region_map(&self, entity: specs::Entity) -> bool {
|
|
|
|
self.tracked_entities.contains(entity.id())
|
2023-06-04 18:21:42 +00:00
|
|
|
}
|
|
|
|
|
2019-10-06 19:38:54 +00:00
|
|
|
fn key_index(&self, key: Vec2<i32>) -> Option<usize> {
|
|
|
|
self.regions.get_full(&key).map(|(i, _, _)| i)
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-10-04 00:41:42 +00:00
|
|
|
/// Adds a new region
|
2019-10-06 17:35:47 +00:00
|
|
|
/// Returns the index of the region in the index map
|
|
|
|
fn insert(&mut self, key: Vec2<i32>) -> usize {
|
2021-12-10 10:36:46 +00:00
|
|
|
let (index, old_region) = self.regions.insert_full(key, Region::default());
|
2019-10-04 00:41:42 +00:00
|
|
|
if old_region.is_some() {
|
|
|
|
panic!("Inserted a region that already exists!!!(this should never need to occur");
|
|
|
|
}
|
|
|
|
// Add neighbors and add to neighbors
|
|
|
|
let mut neighbors = [None; 8];
|
|
|
|
for i in 0..8 {
|
|
|
|
if let Some((idx, _, region)) = self.regions.get_full_mut(&(key + NEIGHBOR_OFFSETS[i]))
|
|
|
|
{
|
|
|
|
// Add neighbor to the new region
|
|
|
|
neighbors[i] = Some(idx);
|
|
|
|
// Add new region to neighbor
|
|
|
|
region.neighbors[(i + 4) % 8] = Some(index);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
self.regions
|
|
|
|
.get_index_mut(index)
|
|
|
|
.map(|(_, v)| v)
|
|
|
|
.unwrap()
|
|
|
|
.neighbors = neighbors;
|
2019-10-06 17:35:47 +00:00
|
|
|
|
|
|
|
index
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-10-04 00:41:42 +00:00
|
|
|
/// Remove a region using its key
|
2019-10-06 19:38:54 +00:00
|
|
|
fn remove(&mut self, key: Vec2<i32>) {
|
|
|
|
if let Some(index) = self.key_index(key) {
|
|
|
|
self.remove_index(index);
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2019-10-04 00:41:42 +00:00
|
|
|
/// Add a region using its key
|
|
|
|
fn remove_index(&mut self, index: usize) {
|
2020-02-01 20:39:39 +00:00
|
|
|
// Remap neighbor indices for neighbors of the region that will be moved from
|
|
|
|
// the end of the index map
|
2019-10-06 17:35:47 +00:00
|
|
|
if index != self.regions.len() - 1 {
|
|
|
|
let moved_neighbors = self
|
|
|
|
.regions
|
|
|
|
.get_index(self.regions.len() - 1)
|
|
|
|
.map(|(_, v)| v)
|
|
|
|
.unwrap()
|
|
|
|
.neighbors;
|
2021-12-03 09:38:30 +00:00
|
|
|
for (i, possible_idx) in moved_neighbors.iter().enumerate() {
|
|
|
|
if let Some(idx) = possible_idx {
|
2019-10-06 17:35:47 +00:00
|
|
|
self.regions
|
2021-12-03 09:38:30 +00:00
|
|
|
.get_index_mut(*idx)
|
2019-10-06 17:35:47 +00:00
|
|
|
.map(|(_, v)| v)
|
|
|
|
.unwrap()
|
|
|
|
.neighbors[(i + 4) % 8] = Some(index);
|
|
|
|
}
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if let Some(region) = self
|
|
|
|
.regions
|
|
|
|
.swap_remove_index(index)
|
|
|
|
.map(|(_, region)| region)
|
|
|
|
{
|
|
|
|
if !region.bitset.is_empty() {
|
|
|
|
panic!("Removed region containing entities");
|
|
|
|
}
|
|
|
|
// Remove from neighbors
|
|
|
|
for i in 0..8 {
|
|
|
|
if let Some(idx) = region.neighbors[i] {
|
|
|
|
self.regions
|
|
|
|
.get_index_mut(idx)
|
|
|
|
.map(|(_, v)| v)
|
|
|
|
.unwrap()
|
|
|
|
.neighbors[(i + 4) % 8] = None;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-02-01 20:39:39 +00:00
|
|
|
|
2021-12-25 23:06:55 +00:00
|
|
|
/// Returns a region given a key
|
2020-02-01 20:39:39 +00:00
|
|
|
pub fn get(&self, key: Vec2<i32>) -> Option<&Region> { self.regions.get(&key) }
|
|
|
|
|
2021-12-25 23:06:55 +00:00
|
|
|
/// Returns an iterator of (Position, Region)
|
2019-10-06 17:35:47 +00:00
|
|
|
pub fn iter(&self) -> impl Iterator<Item = (Vec2<i32>, &Region)> {
|
|
|
|
self.regions.iter().map(|(key, r)| (*key, r))
|
|
|
|
}
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
|
|
|
|
2021-12-25 23:06:55 +00:00
|
|
|
/// Note vd is in blocks in this case
|
2019-10-06 17:35:47 +00:00
|
|
|
pub fn region_in_vd(key: Vec2<i32>, pos: Vec3<f32>, vd: f32) -> bool {
|
|
|
|
let vd_extended = vd + TETHER_LENGTH as f32 * 2.0f32.sqrt();
|
2019-10-04 00:41:42 +00:00
|
|
|
|
2019-10-06 17:35:47 +00:00
|
|
|
let min_region_pos = RegionMap::key_pos(key).map(|e| e as f32);
|
2020-02-01 20:39:39 +00:00
|
|
|
// Should be diff to closest point on the square (which can be in the middle of
|
|
|
|
// an edge)
|
2019-10-06 17:35:47 +00:00
|
|
|
let diff = (min_region_pos - Vec2::from(pos)).map(|e| {
|
|
|
|
if e < 0.0 {
|
|
|
|
(e + REGION_SIZE as f32).min(0.0)
|
|
|
|
} else {
|
|
|
|
e
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
2019-10-06 17:35:47 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
diff.magnitude_squared() < vd_extended.powi(2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Note vd is in blocks in this case
|
|
|
|
pub fn regions_in_vd(pos: Vec3<f32>, vd: f32) -> HashSet<Vec2<i32>> {
|
|
|
|
let mut set = HashSet::new();
|
|
|
|
|
|
|
|
let pos_xy = Vec2::<f32>::from(pos);
|
|
|
|
let vd_extended = vd + TETHER_LENGTH as f32 * 2.0f32.sqrt();
|
|
|
|
|
|
|
|
let max = RegionMap::pos_key(pos_xy.map(|e| (e + vd_extended) as i32));
|
|
|
|
let min = RegionMap::pos_key(pos_xy.map(|e| (e - vd_extended) as i32));
|
2019-10-04 00:41:42 +00:00
|
|
|
|
2020-01-12 14:45:20 +00:00
|
|
|
for x in min.x..max.x + 1 {
|
|
|
|
for y in min.y..max.y + 1 {
|
2019-10-06 17:35:47 +00:00
|
|
|
let key = Vec2::new(x, y);
|
2019-10-04 00:41:42 +00:00
|
|
|
|
2019-10-06 17:35:47 +00:00
|
|
|
if region_in_vd(key, pos, vd) {
|
|
|
|
set.insert(key);
|
|
|
|
}
|
|
|
|
}
|
2019-10-04 00:41:42 +00:00
|
|
|
}
|
2019-10-06 17:35:47 +00:00
|
|
|
|
|
|
|
set
|
|
|
|
}
|
2019-10-04 00:41:42 +00:00
|
|
|
// Iterator designed for use in collision systems
|
|
|
|
// Iterates through all regions yielding them along with half of their neighbors
|
2019-10-06 17:35:47 +00:00
|
|
|
// ..................
|
2019-10-04 00:41:42 +00:00
|
|
|
|
|
|
|
/*fn interleave_i32_with_zeros(mut x: i32) -> i64 {
|
|
|
|
x = (x ^ (x << 16)) & 0x0000ffff0000ffff;
|
|
|
|
x = (x ^ (x << 8)) & 0x00ff00ff00ff00ff;
|
|
|
|
x = (x ^ (x << 4)) & 0x0f0f0f0f0f0f0f0f;
|
|
|
|
x = (x ^ (x << 2)) & 0x3333333333333333;
|
|
|
|
x = (x ^ (x << 1)) & 0x5555555555555555;
|
|
|
|
x
|
|
|
|
}
|
|
|
|
|
|
|
|
fn morton_code(pos: Vec2<i32>) -> i64 {
|
|
|
|
interleave_i32_with_zeros(pos.x) | (interleave_i32_with_zeros(pos.y) << 1)
|
|
|
|
}*/
|