From 7f113566aceca99dd69f085f00308d4df6c18fa2 Mon Sep 17 00:00:00 2001 From: Patrick Owen Date: Sat, 14 Oct 2023 16:42:18 -0400 Subject: [PATCH] Store chunk layout in Graph --- client/src/prediction.rs | 2 +- client/src/sim.rs | 2 +- common/benches/bench.rs | 6 +++--- common/src/cursor.rs | 2 +- common/src/graph.rs | 26 +++++++++++--------------- common/src/graph_collision.rs | 4 ++-- common/src/worldgen.rs | 2 +- server/src/sim.rs | 4 ++-- 8 files changed, 22 insertions(+), 26 deletions(-) diff --git a/client/src/prediction.rs b/client/src/prediction.rs index 06e3dc83..6f7819c8 100644 --- a/client/src/prediction.rs +++ b/client/src/prediction.rs @@ -113,7 +113,7 @@ mod tests { #[test] fn wraparound() { let mock_cfg = SimConfig::from_raw(&common::SimConfigRaw::default()); - let mut mock_graph = Graph::new(); + let mut mock_graph = Graph::new(1); common::node::populate_fresh_nodes(&mut mock_graph); let mock_character_input = CharacterInput { movement: na::Vector3::x(), diff --git a/client/src/sim.rs b/client/src/sim.rs index 1f6623ae..46925525 100644 --- a/client/src/sim.rs +++ b/client/src/sim.rs @@ -53,7 +53,7 @@ pub struct Sim { impl Sim { pub fn new(cfg: SimConfig, local_character_id: EntityId) -> Self { - let mut graph = Graph::new(); + let mut graph = Graph::new(cfg.chunk_size as usize); populate_fresh_nodes(&mut graph); Self { graph, diff --git a/common/benches/bench.rs b/common/benches/bench.rs index e97760a1..969147d2 100644 --- a/common/benches/bench.rs +++ b/common/benches/bench.rs @@ -13,7 +13,7 @@ use common::{ fn build_graph(c: &mut Criterion) { c.bench_function("build_graph 1000", |b| { b.iter(|| { - let mut graph = Graph::new(); + let mut graph = Graph::new(12); let mut n = NodeId::ROOT; for _ in 0..500 { n = graph.ensure_neighbor(n, Side::A); @@ -25,7 +25,7 @@ fn build_graph(c: &mut Criterion) { c.bench_function("nodegen 1000", |b| { b.iter(|| { - let mut graph = Graph::new(); + let mut graph = Graph::new(12); let mut n = NodeId::ROOT; for _ in 0..500 { n = graph.ensure_neighbor(n, Side::A); @@ -38,7 +38,7 @@ fn build_graph(c: &mut Criterion) { c.bench_function("worldgen", |b| { b.iter(|| { - let mut graph = Graph::new(); + let mut graph = Graph::new(12); ensure_nearby(&mut graph, &Position::origin(), 3.0); let fresh = graph.fresh().to_vec(); populate_fresh_nodes(&mut graph); diff --git a/common/src/cursor.rs b/common/src/cursor.rs index 6dbcdd18..61390752 100644 --- a/common/src/cursor.rs +++ b/common/src/cursor.rs @@ -147,7 +147,7 @@ mod tests { #[test] fn cursor_identities() { - let mut graph = Graph::new(); + let mut graph = Graph::new(1); ensure_nearby(&mut graph, &Position::origin(), 3.0); let start = Cursor::from_vertex(NodeId::ROOT, Vertex::A); let wiggle = |dir| { diff --git a/common/src/graph.rs b/common/src/graph.rs index 4625f259..8a6db2a7 100644 --- a/common/src/graph.rs +++ b/common/src/graph.rs @@ -9,7 +9,7 @@ use serde::{Deserialize, Serialize}; use crate::{ dodeca::{Side, SIDE_COUNT}, math, - node::{ChunkId, Node}, + node::{ChunkId, ChunkLayout, Node}, }; /// Graph of the right dodecahedral tiling of H^3 @@ -18,15 +18,17 @@ pub struct Graph { /// This field stores implicitly added nodes to ensure that they're initialized in the correct /// order fresh: Vec, + layout: ChunkLayout, } impl Graph { - pub fn new() -> Self { + pub fn new(dimension: usize) -> Self { let mut nodes = FxHashMap::default(); nodes.insert(NodeId::ROOT, NodeContainer::new(None, 0)); Self { nodes, fresh: vec![NodeId::ROOT], + layout: ChunkLayout::new(dimension), } } @@ -253,12 +255,6 @@ impl Graph { } } -impl Default for Graph { - fn default() -> Self { - Self::new() - } -} - #[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)] pub struct NodeId(u128); @@ -345,7 +341,7 @@ mod tests { #[test] fn parent_child_relationships() { - let mut graph = Graph::default(); + let mut graph = Graph::new(1); assert_eq!(graph.len(), 1); let a = graph.ensure_neighbor(NodeId::ROOT, Side::A); assert_eq!(graph.len(), 2); @@ -365,7 +361,7 @@ mod tests { #[test] fn children_have_common_neighbor() { - let mut graph = Graph::default(); + let mut graph = Graph::new(1); let a = graph.ensure_neighbor(NodeId::ROOT, Side::A); let b = graph.ensure_neighbor(NodeId::ROOT, Side::B); let a_neighbors = Side::iter() @@ -392,7 +388,7 @@ mod tests { #[test] fn normalize_transform() { - let mut graph = Graph::default(); + let mut graph = Graph::new(1); let a = graph.ensure_neighbor(NodeId::ROOT, Side::A); { let (node, xf) = @@ -409,9 +405,9 @@ mod tests { #[test] fn rebuild_from_tree() { - let mut a = Graph::default(); + let mut a = Graph::new(1); ensure_nearby(&mut a, &Position::origin(), 3.0); - let mut b = Graph::default(); + let mut b = Graph::new(1); for (side, parent) in a.tree() { b.insert_child(parent, side); } @@ -425,14 +421,14 @@ mod tests { #[test] fn hash_consistency() { let h1 = { - let mut g = Graph::new(); + let mut g = Graph::new(1); let n1 = g.ensure_neighbor(NodeId::ROOT, Side::A); let n2 = g.ensure_neighbor(n1, Side::B); let n3 = g.ensure_neighbor(n2, Side::C); g.ensure_neighbor(n3, Side::D) }; let h2 = { - let mut g = Graph::new(); + let mut g = Graph::new(1); let n1 = g.ensure_neighbor(NodeId::ROOT, Side::C); let n2 = g.ensure_neighbor(n1, Side::A); let n3 = g.ensure_neighbor(n2, Side::B); diff --git a/common/src/graph_collision.rs b/common/src/graph_collision.rs index ffaf8652..e3407fa9 100644 --- a/common/src/graph_collision.rs +++ b/common/src/graph_collision.rs @@ -261,7 +261,7 @@ mod tests { fn execute(self) { let dimension: usize = 12; let layout = ChunkLayout::new(dimension); - let mut graph = Graph::new(); + let mut graph = Graph::new(dimension); let graph_radius = 3.0; // Set up a graph with void chunks @@ -514,7 +514,7 @@ mod tests { fn sphere_cast_near_unloaded_chunk() { let dimension: usize = 12; let layout = ChunkLayout::new(dimension); - let mut graph = Graph::new(); + let mut graph = Graph::new(dimension); let sides = Vertex::A.canonical_sides(); diff --git a/common/src/worldgen.rs b/common/src/worldgen.rs index 4cbed49f..8cd9fc26 100644 --- a/common/src/worldgen.rs +++ b/common/src/worldgen.rs @@ -676,7 +676,7 @@ mod test { #[test] fn check_chunk_incident_max_elevations() { - let mut g = Graph::new(); + let mut g = Graph::new(1); for (i, path) in Vertex::A.dual_vertices().map(|(_, p)| p).enumerate() { let new_node = path.fold(NodeId::ROOT, |node, side| g.ensure_neighbor(node, side)); diff --git a/server/src/sim.rs b/server/src/sim.rs index 1a8b14bc..fc0dedde 100644 --- a/server/src/sim.rs +++ b/server/src/sim.rs @@ -39,16 +39,16 @@ pub struct Sim { impl Sim { pub fn new(cfg: Arc) -> Self { let mut result = Self { - cfg, rng: SmallRng::from_entropy(), step: 0, entity_ids: FxHashMap::default(), world: hecs::World::new(), - graph: Graph::new(), + graph: Graph::new(cfg.chunk_size as usize), spawns: Vec::new(), despawns: Vec::new(), graph_entities: GraphEntities::new(), dirty_nodes: FxHashSet::default(), + cfg, }; ensure_nearby(