Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions common/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ license = "Apache-2.0 OR Zlib"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
arrayvec = "0.7.6"
blake3 = "1.3.3"
serde = { version = "1.0.104", features = ["derive"] }
nalgebra = { workspace = true, features = ["serde-serialize"] }
Expand Down
2 changes: 1 addition & 1 deletion common/src/graph.rs
Original file line number Diff line number Diff line change
Expand Up @@ -259,7 +259,7 @@ impl std::ops::IndexMut<NodeId> for Graph {
/// A cryptographic hash function is used to ensure uniqueness, making it
/// astronomically unlikely to be able to find two different nodes in the graph
/// with the same ID.
#[derive(Debug, Copy, Clone, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[derive(Debug, Copy, Clone, Eq, PartialEq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct NodeId(u128);

impl NodeId {
Expand Down
3 changes: 1 addition & 2 deletions common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,9 @@ pub mod lru_slab;
mod margins;
pub mod math;
pub mod node;
mod plane;
pub mod peer_traverser;
pub mod proto;
mod sim_config;
pub mod terraingen;
pub mod traversal;
pub mod voxel_math;
pub mod world;
Expand Down
43 changes: 30 additions & 13 deletions common/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ use crate::lru_slab::SlotId;
use crate::proto::{BlockUpdate, Position, SerializedVoxelData};
use crate::voxel_math::{ChunkDirection, CoordAxis, CoordSign, Coords};
use crate::world::Material;
use crate::worldgen::NodeState;
use crate::{Chunks, margins};
use crate::worldgen::{NodeState, PartialNodeState};
use crate::{Chunks, margins, peer_traverser};

/// Unique identifier for a single chunk (1/20 of a dodecahedron) in the graph
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
Expand All @@ -28,30 +28,46 @@ impl ChunkId {
}

impl Graph {
/// Returns the PartialNodeState for the given node, panicking if it isn't initialized.
#[inline]
pub fn partial_node_state(&self, node_id: NodeId) -> &PartialNodeState {
self[node_id].partial_state.as_ref().unwrap()
}

/// Initializes the PartialNodeState for the given node if not already initialized,
/// initializing other nodes' NodeState and PartialNodeState as necessary
pub fn ensure_partial_node_state(&mut self, node_id: NodeId) {
if self[node_id].partial_state.is_some() {
return;
}

for (_, parent) in self.parents(node_id) {
self.ensure_node_state(parent);
}

let partial_node_state = PartialNodeState::new(self, node_id);
self[node_id].partial_state = Some(partial_node_state);
}

/// Returns the NodeState for the given node, panicking if it isn't initialized.
#[inline]
pub fn node_state(&self, node_id: NodeId) -> &NodeState {
self[node_id].state.as_ref().unwrap()
}

/// Initializes the NodeState for the given node and all its ancestors if not
/// already initialized.
/// Initializes the NodeState for the given node if not already initialized,
/// initializing other nodes' NodeState and PartialNodeState as necessary
pub fn ensure_node_state(&mut self, node_id: NodeId) {
if self[node_id].state.is_some() {
return;
}

for (_, parent) in self.parents(node_id) {
self.ensure_node_state(parent);
self.ensure_partial_node_state(node_id);
for peer in peer_traverser::ensure_peer_nodes(self, node_id) {
self.ensure_partial_node_state(peer.node());
}

let node_state = self
.primary_parent_side(node_id)
.map(|i| {
let parent_state = self.node_state(self.neighbor(node_id, i).unwrap());
parent_state.child(self, node_id, i)
})
.unwrap_or_else(NodeState::root);
let node_state = NodeState::new(self, node_id);
self[node_id].state = Some(node_state);
}

Expand Down Expand Up @@ -217,6 +233,7 @@ impl IndexMut<ChunkId> for Graph {
/// used for rendering, is stored here.
#[derive(Default)]
pub struct Node {
pub partial_state: Option<PartialNodeState>,
pub state: Option<NodeState>,
/// We can only populate chunks which lie within a cube of populated nodes, so nodes on the edge
/// of the graph always have some `Fresh` chunks.
Expand Down
Loading