Skip to content

Commit c7b1bfe

Browse files
committed
Merge NodeState's two constructors to simplify its usage
1 parent ac863a4 commit c7b1bfe

File tree

2 files changed

+39
-38
lines changed

2 files changed

+39
-38
lines changed

common/src/node.rs

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,13 +45,7 @@ impl Graph {
4545
self.ensure_node_state(parent);
4646
}
4747

48-
let node_state = self
49-
.parent(node_id)
50-
.map(|i| {
51-
let parent_state = self.node_state(self.neighbor(node_id, i).unwrap());
52-
parent_state.child(self, node_id, i)
53-
})
54-
.unwrap_or_else(NodeState::root);
48+
let node_state = NodeState::new(self, node_id);
5549
self[node_id].state = Some(node_state);
5650
}
5751

common/src/worldgen.rs

Lines changed: 38 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -71,53 +71,53 @@ pub struct NodeState {
7171
enviro: EnviroFactors,
7272
}
7373
impl NodeState {
74-
pub fn root() -> Self {
75-
Self {
76-
kind: NodeStateKind::ROOT,
77-
surface: Plane::from(Side::A),
78-
road_state: NodeStateRoad::ROOT,
79-
enviro: EnviroFactors {
74+
pub fn new(graph: &Graph, node: NodeId) -> Self {
75+
let mut parents = graph
76+
.descenders(node)
77+
.map(|(s, n)| ParentInfo {
78+
node_id: n,
79+
side: s,
80+
node_state: graph.node_state(n),
81+
})
82+
.fuse();
83+
let parents = [parents.next(), parents.next(), parents.next()];
84+
85+
let enviro = match (parents[0], parents[1]) {
86+
(None, None) => EnviroFactors {
8087
max_elevation: 0.0,
8188
temperature: 0.0,
8289
rainfall: 0.0,
8390
blockiness: 0.0,
8491
},
85-
}
86-
}
87-
88-
pub fn child(&self, graph: &Graph, node: NodeId, side: Side) -> Self {
89-
let mut d = graph
90-
.descenders(node)
91-
.map(|(s, n)| (s, graph.node_state(n)));
92-
let enviro = match (d.next(), d.next()) {
93-
(Some(_), None) => {
94-
let parent_side = graph.parent(node).unwrap();
95-
let parent_node = graph.neighbor(node, parent_side).unwrap();
96-
let parent_state = graph.node_state(parent_node);
92+
(Some(parent), None) => {
9793
let spice = graph.hash_of(node) as u64;
98-
EnviroFactors::varied_from(parent_state.enviro, spice)
94+
EnviroFactors::varied_from(parent.node_state.enviro, spice)
9995
}
100-
(Some((a_side, a_state)), Some((b_side, b_state))) => {
101-
let ab_node = graph
102-
.neighbor(graph.neighbor(node, a_side).unwrap(), b_side)
103-
.unwrap();
104-
let ab_state = graph.node_state(ab_node);
105-
EnviroFactors::continue_from(a_state.enviro, b_state.enviro, ab_state.enviro)
96+
(Some(parent_a), Some(parent_b)) => {
97+
let ab_node = graph.neighbor(parent_a.node_id, parent_b.side).unwrap();
98+
let ab_state = &graph.node_state(ab_node);
99+
EnviroFactors::continue_from(
100+
parent_a.node_state.enviro,
101+
parent_b.node_state.enviro,
102+
ab_state.enviro,
103+
)
106104
}
107105
_ => unreachable!(),
108106
};
109107

110-
let child_kind = self.kind.child(side);
111-
let child_road = self.road_state.child(side);
108+
let kind = parents[0].map_or(NodeStateKind::ROOT, |p| p.node_state.kind.child(p.side));
109+
let road_state = parents[0].map_or(NodeStateRoad::ROOT, |p| {
110+
p.node_state.road_state.child(p.side)
111+
});
112112

113113
Self {
114-
kind: child_kind,
115-
surface: match child_kind {
114+
kind,
115+
surface: match kind {
116116
Land => Plane::from(Side::A),
117117
Sky => -Plane::from(Side::A),
118-
_ => side * self.surface,
118+
_ => parents[0].map(|p| p.side * p.node_state.surface).unwrap(),
119119
},
120-
road_state: child_road,
120+
road_state,
121121
enviro,
122122
}
123123
}
@@ -127,6 +127,13 @@ impl NodeState {
127127
}
128128
}
129129

130+
#[derive(Clone, Copy)]
131+
struct ParentInfo<'a> {
132+
node_id: NodeId,
133+
side: Side,
134+
node_state: &'a NodeState,
135+
}
136+
130137
struct VoxelCoords {
131138
counter: u32,
132139
dimension: u8,

0 commit comments

Comments
 (0)