@@ -8,10 +8,11 @@ use crate::collision_math::Ray;
88use crate :: dodeca:: Vertex ;
99use crate :: graph:: { Graph , NodeId } ;
1010use crate :: lru_slab:: SlotId ;
11+ use crate :: peer_traverser:: PeerTraverser ;
1112use crate :: proto:: { BlockUpdate , Position , SerializedVoxelData } ;
1213use crate :: voxel_math:: { ChunkDirection , CoordAxis , CoordSign , Coords } ;
1314use crate :: world:: Material ;
14- use crate :: worldgen:: NodeState ;
15+ use crate :: worldgen:: { NodeState , PartialNodeState } ;
1516use crate :: { Chunks , margins} ;
1617
1718/// Unique identifier for a single chunk (1/20 of a dodecahedron) in the graph
@@ -28,21 +29,44 @@ impl ChunkId {
2829}
2930
3031impl Graph {
32+ /// Returns the PartialNodeState for the given node, panicking if it isn't initialized.
33+ #[ inline]
34+ pub fn partial_node_state ( & self , node_id : NodeId ) -> & PartialNodeState {
35+ self [ node_id] . partial_state . as_ref ( ) . unwrap ( )
36+ }
37+
38+ /// Initializes the PartialNodeState for the given node if not already initialized,
39+ /// initializing other nodes' NodeState and PartialNodeState as necessary
40+ pub fn ensure_partial_node_state ( & mut self , node_id : NodeId ) {
41+ if self [ node_id] . partial_state . is_some ( ) {
42+ return ;
43+ }
44+
45+ for ( _, parent) in self . descenders ( node_id) {
46+ self . ensure_node_state ( parent) ;
47+ }
48+
49+ let partial_node_state = PartialNodeState :: new ( self , node_id) ;
50+ self [ node_id] . partial_state = Some ( partial_node_state) ;
51+ }
52+
3153 /// Returns the NodeState for the given node, panicking if it isn't initialized.
3254 #[ inline]
3355 pub fn node_state ( & self , node_id : NodeId ) -> & NodeState {
3456 self [ node_id] . state . as_ref ( ) . unwrap ( )
3557 }
3658
37- /// Initializes the NodeState for the given node and all its ancestors if not
38- /// already initialized.
59+ /// Initializes the NodeState for the given node if not already initialized,
60+ /// initializing other nodes' NodeState and PartialNodeState as necessary
3961 pub fn ensure_node_state ( & mut self , node_id : NodeId ) {
4062 if self [ node_id] . state . is_some ( ) {
4163 return ;
4264 }
4365
44- for ( _, parent) in self . descenders ( node_id) {
45- self . ensure_node_state ( parent) ;
66+ self . ensure_partial_node_state ( node_id) ;
67+ let mut peers = PeerTraverser :: new ( node_id) ;
68+ while let Some ( peer) = peers. ensure_next ( self ) {
69+ self . ensure_partial_node_state ( peer. node ( ) ) ;
4670 }
4771
4872 let node_state = NodeState :: new ( self , node_id) ;
@@ -211,6 +235,7 @@ impl IndexMut<ChunkId> for Graph {
211235/// used for rendering, is stored here.
212236#[ derive( Default ) ]
213237pub struct Node {
238+ pub partial_state : Option < PartialNodeState > ,
214239 pub state : Option < NodeState > ,
215240 /// We can only populate chunks which lie within a cube of populated nodes, so nodes on the edge
216241 /// of the graph always have some `Fresh` chunks.
0 commit comments