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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
use serde::{Deserialize, Serialize};
use super::generators::NoiseParams;
#[derive(Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InitConfig {
pub chunk_size: usize,
pub sub_chunks: usize,
pub max_height: usize,
pub max_light_level: u32,
pub min_chunk: [i32; 2],
pub max_chunk: [i32; 2],
pub gravity: [f32; 3],
pub min_bounce_impulse: f32,
pub air_drag: f32,
pub fluid_drag: f32,
pub fluid_density: f32,
}
#[derive(Clone, Default)]
pub struct WorldConfig {
pub max_clients: usize,
pub chunk_size: usize,
pub sub_chunks: usize,
pub min_chunk: [i32; 2],
pub max_chunk: [i32; 2],
pub max_height: usize,
pub max_light_level: u32,
pub max_chunks_per_tick: usize,
pub max_updates_per_tick: usize,
pub max_response_per_tick: usize,
pub max_saves_per_tick: usize,
pub water_level: usize,
pub gravity: [f32; 3],
pub min_bounce_impulse: f32,
pub air_drag: f32,
pub fluid_drag: f32,
pub fluid_density: f32,
pub collision_repulsion: f32,
pub seed: u32,
pub terrain: NoiseParams,
pub saving: bool,
pub save_dir: String,
pub save_interval: usize,
}
impl WorldConfig {
pub fn new() -> WorldConfigBuilder {
WorldConfigBuilder::new()
}
pub fn get_init_config(&self) -> InitConfig {
InitConfig {
chunk_size: self.chunk_size,
sub_chunks: self.sub_chunks,
max_height: self.max_height,
max_light_level: self.max_light_level,
min_chunk: self.min_chunk,
max_chunk: self.max_chunk,
gravity: self.gravity.to_owned(),
air_drag: self.air_drag,
fluid_density: self.fluid_density,
fluid_drag: self.fluid_drag,
min_bounce_impulse: self.min_bounce_impulse,
}
}
}
const DEFAULT_MAX_CLIENT: usize = 100;
const DEFAULT_CHUNK_SIZE: usize = 16;
const DEFAULT_SUB_CHUNKS: usize = 16;
const DEFAULT_MIN_CHUNK: [i32; 2] = [i32::MIN + 1, i32::MIN + 1];
const DEFAULT_MAX_CHUNK: [i32; 2] = [i32::MAX - 1, i32::MAX - 1];
const DEFAULT_MAX_HEIGHT: usize = 256;
const DEFAULT_MAX_LIGHT_LEVEL: u32 = 15;
const DEFAULT_MAX_CHUNKS_PER_TICK: usize = 24;
const DEFAULT_MAX_UPDATES_PER_TICK: usize = 200;
const DEFAULT_MAX_RESPONSE_PER_TICK: usize = 4;
const DEFAULT_MAX_SAVES_PER_TICK: usize = 2;
const DEFAULT_WATER_LEVEL: usize = 60;
const DEFAULT_SEED: u32 = 123123123;
const DEFAULT_GRAVITY: [f32; 3] = [0.0, -24.8, 0.0];
const DEFAULT_MIN_BOUNCE_IMPULSE: f32 = 0.5;
const DEFAULT_AIR_DRAG: f32 = 0.1;
const DEFAULT_FLUID_DRAG: f32 = 1.4;
const DEFAULT_FLUID_DENSITY: f32 = 0.8;
const DEFAULT_COLLISION_REPULSION: f32 = 0.3;
const DEFAULT_SAVING: bool = false;
const DEFAULT_SAVE_DIR: &str = "";
const DEFAULT_SAVE_INTERVAL: usize = 300;
pub struct WorldConfigBuilder {
max_clients: usize,
chunk_size: usize,
sub_chunks: usize,
min_chunk: [i32; 2],
max_chunk: [i32; 2],
max_height: usize,
max_light_level: u32,
max_chunks_per_tick: usize,
max_updates_per_tick: usize,
max_response_per_tick: usize,
max_saves_per_tick: usize,
water_level: usize,
seed: u32,
gravity: [f32; 3],
min_bounce_impulse: f32,
air_drag: f32,
fluid_drag: f32,
fluid_density: f32,
collision_repulsion: f32,
terrain: NoiseParams,
saving: bool,
save_dir: String,
save_interval: usize,
}
impl WorldConfigBuilder {
pub fn new() -> Self {
Self {
max_clients: DEFAULT_MAX_CLIENT,
chunk_size: DEFAULT_CHUNK_SIZE,
sub_chunks: DEFAULT_SUB_CHUNKS,
min_chunk: DEFAULT_MIN_CHUNK,
max_chunk: DEFAULT_MAX_CHUNK,
max_height: DEFAULT_MAX_HEIGHT,
max_light_level: DEFAULT_MAX_LIGHT_LEVEL,
max_chunks_per_tick: DEFAULT_MAX_CHUNKS_PER_TICK,
max_updates_per_tick: DEFAULT_MAX_UPDATES_PER_TICK,
max_response_per_tick: DEFAULT_MAX_RESPONSE_PER_TICK,
max_saves_per_tick: DEFAULT_MAX_SAVES_PER_TICK,
water_level: DEFAULT_WATER_LEVEL,
seed: DEFAULT_SEED,
air_drag: DEFAULT_AIR_DRAG,
fluid_drag: DEFAULT_FLUID_DRAG,
fluid_density: DEFAULT_FLUID_DENSITY,
gravity: DEFAULT_GRAVITY,
min_bounce_impulse: DEFAULT_MIN_BOUNCE_IMPULSE,
collision_repulsion: DEFAULT_COLLISION_REPULSION,
saving: DEFAULT_SAVING,
save_dir: DEFAULT_SAVE_DIR.to_owned(),
save_interval: DEFAULT_SAVE_INTERVAL,
terrain: NoiseParams::default(),
}
}
pub fn max_clients(mut self, max_clients: usize) -> Self {
self.max_clients = max_clients;
self
}
pub fn chunk_size(mut self, chunk_size: usize) -> Self {
self.chunk_size = chunk_size;
self
}
pub fn sub_chunks(mut self, sub_chunks: usize) -> Self {
self.sub_chunks = sub_chunks;
self
}
pub fn min_chunk(mut self, min_chunk: [i32; 2]) -> Self {
self.min_chunk = min_chunk;
self
}
pub fn max_chunk(mut self, max_chunk: [i32; 2]) -> Self {
self.max_chunk = max_chunk;
self
}
pub fn max_height(mut self, max_height: usize) -> Self {
self.max_height = max_height;
self
}
pub fn max_light_level(mut self, max_light_level: u32) -> Self {
assert!(max_light_level < 16, "Max light level cannot be >= 16.");
self.max_light_level = max_light_level;
self
}
pub fn max_chunks_per_tick(mut self, max_chunks_per_tick: usize) -> Self {
self.max_chunks_per_tick = max_chunks_per_tick;
self
}
pub fn max_updates_per_tick(mut self, max_updates_per_tick: usize) -> Self {
self.max_updates_per_tick = max_updates_per_tick;
self
}
pub fn max_response_per_tick(mut self, max_response_per_tick: usize) -> Self {
self.max_response_per_tick = max_response_per_tick;
self
}
pub fn max_saves_per_tick(mut self, max_saves_per_tick: usize) -> Self {
self.max_saves_per_tick = max_saves_per_tick;
self
}
pub fn water_level(mut self, water_level: usize) -> Self {
self.water_level = water_level;
self
}
pub fn seed(mut self, seed: u32) -> Self {
self.seed = seed;
self
}
pub fn terrain(mut self, terrain: &NoiseParams) -> Self {
self.terrain = terrain.to_owned();
self
}
pub fn collision_repulsion(mut self, collision_repulsion: f32) -> Self {
self.collision_repulsion = collision_repulsion;
self
}
pub fn saving(mut self, saving: bool) -> Self {
self.saving = saving;
self
}
pub fn save_dir(mut self, save_dir: &str) -> Self {
self.save_dir = save_dir.to_owned();
self
}
pub fn save_interval(mut self, save_interval: usize) -> Self {
self.save_interval = save_interval.to_owned();
self
}
pub fn build(self) -> WorldConfig {
if self.max_chunk[0] < self.min_chunk[0] || self.max_chunk[1] < self.min_chunk[1] {
panic!("Min/max chunk parameters do not make sense.");
}
if self.chunk_size % self.sub_chunks != 0 {
panic!("Chunk size should be divisible by sub-chunks.");
}
if !self.saving && !self.save_dir.is_empty() {
panic!("Save directory shouldn't be used unless `config.save` is set to true!");
}
WorldConfig {
max_clients: self.max_clients,
chunk_size: self.chunk_size,
sub_chunks: self.sub_chunks,
max_height: self.max_height,
max_light_level: self.max_light_level,
max_chunks_per_tick: self.max_chunks_per_tick,
max_updates_per_tick: self.max_updates_per_tick,
max_response_per_tick: self.max_response_per_tick,
max_saves_per_tick: self.max_saves_per_tick,
water_level: self.water_level,
seed: self.seed,
min_chunk: self.min_chunk,
max_chunk: self.max_chunk,
air_drag: self.air_drag,
fluid_drag: self.fluid_drag,
fluid_density: self.fluid_density,
gravity: self.gravity,
min_bounce_impulse: self.min_bounce_impulse,
collision_repulsion: self.collision_repulsion,
terrain: self.terrain,
saving: self.saving,
save_dir: self.save_dir,
save_interval: self.save_interval,
}
}
}