1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
use log::info;
use nanoid::nanoid;
use specs::{ReadExpect, System, WriteExpect};
use crate::{
Chunk, ChunkParams, ChunkUtils, Chunks, Pipeline, Registry, SeededNoise, SeededTerrain, Vec2,
WorldConfig,
};
pub struct ChunkPipeliningSystem;
impl<'a> System<'a> for ChunkPipeliningSystem {
type SystemData = (
ReadExpect<'a, Registry>,
ReadExpect<'a, WorldConfig>,
ReadExpect<'a, SeededNoise>,
ReadExpect<'a, SeededTerrain>,
WriteExpect<'a, Pipeline>,
WriteExpect<'a, Chunks>,
);
fn run(&mut self, data: Self::SystemData) {
let (registry, config, noise, terrain, mut pipeline, mut chunks) = data;
let max_per_tick = config.max_chunks_per_tick;
let chunk_size = config.chunk_size;
if let Ok((list, new_changes)) = pipeline.results() {
new_changes.into_iter().for_each(|(voxel, id)| {
let coords = ChunkUtils::map_voxel_to_chunk(voxel.0, voxel.1, voxel.2, chunk_size);
let mut already = pipeline.leftovers.remove(&coords).unwrap_or_else(|| vec![]);
already.push((voxel, id));
pipeline.leftovers.insert(coords, already);
});
list.into_iter().for_each(|mut chunk| {
pipeline.advance(&mut chunk);
if chunk.stage.is_none() {
chunk.calculate_max_height(®istry);
if !chunks.to_remesh.contains(&chunk.coords) {
chunks.to_remesh.push_back(chunk.coords.to_owned());
}
}
chunks.renew(chunk);
});
}
let mut processes = vec![];
let mut processed = 0;
while processed < max_per_tick {
if pipeline.is_empty() {
break;
}
let (Vec2(cx, cz), index) = pipeline.pop().unwrap();
let stage = pipeline.get_stage(index);
let margin = stage.neighbors(&config);
let r = (margin as f32 / chunk_size as f32).ceil() as i32;
let coords = Vec2(cx, cz);
let chunk = chunks.raw(&coords);
if chunk.is_none() {
if let Some(chunk) = chunks.try_load(&coords) {
chunks.add(chunk);
pipeline.remove(&coords);
if !chunks.to_remesh.contains(&coords) {
chunks.to_remesh.push_back(coords);
}
continue;
}
let new_chunk = Chunk::new(
&nanoid!(),
cx,
cz,
&ChunkParams {
max_height: config.max_height,
sub_chunks: config.sub_chunks,
size: config.chunk_size,
},
);
pipeline.postpone(&new_chunk.coords, 0);
chunks.add(new_chunk);
continue;
}
let chunk = chunk.unwrap().clone();
if chunk.stage.is_none() {
continue;
}
if chunk.stage.unwrap() > index {
continue;
}
let mut ready = true;
for x in -r..=r {
for z in -r..=r {
if (x == 0 && z == 0) || (x * x + z * z > r * r) {
continue;
}
let coords = Vec2(cx + x, cz + z);
if !chunks.is_within_world(&coords) {
continue;
}
if let Some(neighbor) = chunks.raw(&coords) {
if neighbor.stage.is_none()
|| neighbor.stage.unwrap() >= chunk.stage.unwrap()
{
continue;
}
}
ready = false;
break;
}
if !ready {
break;
}
}
if !ready {
pipeline.postpone(&chunk.coords, index);
continue;
}
if let Some(data) = stage.needs_space() {
let mut space = chunks.make_space(&chunk.coords, margin);
if data.needs_voxels {
space = space.needs_voxels();
}
if data.needs_lights {
space = space.needs_lights();
}
if data.needs_height_maps {
space = space.needs_height_maps();
}
let space = space.build();
processes.push((chunk, Some(space), index));
} else {
processes.push((chunk, None, index));
}
processed += 1;
}
if !processes.is_empty() {
pipeline.process(processes, ®istry, &config, &noise, &terrain);
}
}
}