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
use serde::{Deserialize, Serialize};
use super::generators::NoiseParams;
#[derive(Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct InitConfig {
pub chunk_size: usize,
pub max_height: usize,
pub max_light_level: u32,
pub min_chunk: [i32; 2],
pub max_chunk: [i32; 2],
}
#[derive(Clone, Default)]
pub struct WorldConfig {
pub max_clients: usize,
pub chunk_size: usize,
pub min_chunk: [i32; 2],
pub max_chunk: [i32; 2],
pub max_height: usize,
pub max_light_level: u32,
pub max_chunk_per_tick: usize,
pub max_updates_per_tick: usize,
pub max_response_per_tick: usize,
pub preload_radius: u32,
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 seed: u32,
pub terrain: NoiseParams,
}
impl WorldConfig {
pub fn new() -> WorldConfigBuilder {
WorldConfigBuilder::new()
}
pub fn get_init_config(&self) -> InitConfig {
InitConfig {
chunk_size: self.chunk_size,
max_height: self.max_height,
max_light_level: self.max_light_level,
min_chunk: self.min_chunk,
max_chunk: self.max_chunk,
}
}
}
const DEFAULT_MAX_CLIENT: usize = 100;
const DEFAULT_CHUNK_SIZE: 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 = 500;
const DEFAULT_MAX_RESPONSE_PER_TICK: usize = 6;
const DEFAULT_WATER_LEVEL: usize = 60;
const DEFAULT_PRELOAD_RADIUS: u32 = 8;
const DEFAULT_SEED: u32 = 123123123;
const DEFAULT_GRAVITY: [f32; 3] = [0.0, -9.8, 0.0];
const DEFAULT_MIN_BOUNCE_IMPULSE: f32 = 0.1;
const DEFAULT_AIR_DRAG: f32 = 0.1;
const DEFAULT_FLUID_DRAG: f32 = 0.4;
const DEFAULT_FLUID_DENSITY: f32 = 2.0;
pub struct WorldConfigBuilder {
max_clients: usize,
chunk_size: usize,
min_chunk: [i32; 2],
max_chunk: [i32; 2],
max_height: usize,
max_light_level: u32,
max_chunk_per_tick: usize,
max_updates_per_tick: usize,
max_response_per_tick: usize,
water_level: usize,
preload_radius: u32,
seed: u32,
gravity: [f32; 3],
min_bounce_impulse: f32,
air_drag: f32,
fluid_drag: f32,
fluid_density: f32,
terrain: NoiseParams,
}
impl WorldConfigBuilder {
pub fn new() -> Self {
Self {
max_clients: DEFAULT_MAX_CLIENT,
chunk_size: DEFAULT_CHUNK_SIZE,
min_chunk: DEFAULT_MIN_CHUNK,
max_chunk: DEFAULT_MAX_CHUNK,
max_height: DEFAULT_MAX_HEIGHT,
max_light_level: DEFAULT_MAX_LIGHT_LEVEL,
max_chunk_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,
water_level: DEFAULT_WATER_LEVEL,
preload_radius: DEFAULT_PRELOAD_RADIUS,
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,
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 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_chunk_per_tick(mut self, max_chunk_per_tick: usize) -> Self {
self.max_chunk_per_tick = max_chunk_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 water_level(mut self, water_level: usize) -> Self {
self.water_level = water_level;
self
}
pub fn preload_radius(mut self, preload_radius: u32) -> Self {
self.preload_radius = preload_radius;
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 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.");
}
WorldConfig {
max_clients: self.max_clients,
chunk_size: self.chunk_size,
max_height: self.max_height,
max_light_level: self.max_light_level,
max_chunk_per_tick: self.max_chunk_per_tick,
max_updates_per_tick: self.max_updates_per_tick,
max_response_per_tick: self.max_response_per_tick,
water_level: self.water_level,
preload_radius: self.preload_radius,
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,
terrain: self.terrain,
}
}
}