Skip to content

Commit

Permalink
Store chunk layout in Graph
Browse files Browse the repository at this point in the history
  • Loading branch information
patowen committed Oct 25, 2023
1 parent f8f9ec8 commit 1450462
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 26 deletions.
2 changes: 1 addition & 1 deletion client/src/prediction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
2 changes: 1 addition & 1 deletion client/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions common/benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion common/src/cursor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down
26 changes: 11 additions & 15 deletions common/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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<NodeId>,
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),
}
}

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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);
Expand All @@ -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()
Expand All @@ -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) =
Expand All @@ -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);
}
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions common/src/graph_collision.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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();

Expand Down
2 changes: 1 addition & 1 deletion common/src/worldgen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down
4 changes: 2 additions & 2 deletions server/src/sim.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,16 +39,16 @@ pub struct Sim {
impl Sim {
pub fn new(cfg: Arc<SimConfig>) -> 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(
Expand Down

0 comments on commit 1450462

Please sign in to comment.