Skip to content

Set up an initial framework to allow megastructures to spawn in the world #454

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
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
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: 31 additions & 12 deletions common/src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ use crate::collision_math::Ray;
use crate::dodeca::Vertex;
use crate::graph::{Graph, NodeId};
use crate::lru_slab::SlotId;
use crate::peer_traverser::PeerTraverser;
use crate::proto::{BlockUpdate, Position, SerializedVoxelData};
use crate::voxel_math::{ChunkDirection, CoordAxis, CoordSign, Coords};
use crate::world::Material;
use crate::worldgen::NodeState;
use crate::worldgen::{NodeState, PartialNodeState};
use crate::{Chunks, margins};

/// Unique identifier for a single chunk (1/20 of a dodecahedron) in the graph
Expand All @@ -28,30 +29,47 @@ 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.descenders(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.descenders(node_id) {
self.ensure_node_state(parent);
self.ensure_partial_node_state(node_id);
let mut peers = PeerTraverser::new(node_id);
while let Some(peer) = peers.ensure_next(self) {
self.ensure_partial_node_state(peer.node());
}

let node_state = self
.parent(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 +235,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