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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
use std::sync::{RwLock, Arc, RwLockReadGuard, RwLockWriteGuard};
use std::collections::{HashMap, HashSet};
use std::collections::hash_map::Entry;
use std::fmt::{Debug, Formatter};
use hecs::{World as EcsWorld, EntityBuilder, Entity, EntityRef};
use uuid::Uuid;
use crate::entity::{GlobalEntities, EntityType};
use crate::block::{GlobalBlocks, BlockState};
use crate::biome::GlobalBiomes;
use crate::heightmap::GlobalHeightmaps;
use crate::pos::{EntityPos, BlockPos};
use crate::debug;
use super::source::{LevelSource, ChunkLoadRequest, ChunkSaveRequest, LevelSourceError, ProtoChunk};
use super::chunk::{Chunk, ChunkHeight, ChunkResult, ChunkError};
pub struct LevelEnv {
pub blocks: GlobalBlocks,
pub biomes: GlobalBiomes,
pub entities: GlobalEntities,
pub heightmaps: GlobalHeightmaps
}
impl LevelEnv {
pub fn new(
blocks: GlobalBlocks,
biomes: GlobalBiomes,
entities: GlobalEntities,
heightmaps: GlobalHeightmaps
) -> Self {
LevelEnv {
blocks,
biomes,
entities,
heightmaps
}
}
}
impl Debug for LevelEnv {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
f.debug_struct("LevelEnv")
.field("states_count", &self.blocks.states_count())
.field("blocks_count", &self.blocks.blocks_count())
.field("entity_types_count", &self.entities.entity_types_count())
.field("heightmaps_count", &self.heightmaps.heightmaps_count())
.finish()
}
}
pub struct Level {
id: String,
env: Arc<LevelEnv>,
source: Box<dyn LevelSource>,
loading_chunks: HashSet<(i32, i32)>,
height: ChunkHeight,
pub chunks: ChunkStorage,
pub entities: EntityStorage
}
impl Level {
pub fn new<S>(id: String, env: Arc<LevelEnv>, height: ChunkHeight, source: S) -> Self
where
S: LevelSource + 'static,
{
assert_ne!(env.blocks.states_count(), 0, "The given environment has no state, a level requires at least one block state.");
assert_ne!(env.biomes.biomes_count(), 0, "The given environment has no biome, a level requires at least one biome.");
Level {
id,
height,
source: Box::new(source),
loading_chunks: HashSet::new(),
chunks: ChunkStorage {
chunks: HashMap::new()
},
entities: EntityStorage {
ecs: EcsWorld::new(),
builder: EntityBuilder::new()
},
env,
}
}
pub fn get_id(&self) -> &String {
&self.id
}
pub fn get_env(&self) -> &Arc<LevelEnv> {
&self.env
}
pub fn get_height(&self) -> ChunkHeight {
self.height
}
pub fn request_chunk_load(&mut self, cx: i32, cz: i32) -> bool {
if !self.loading_chunks.contains(&(cx, cz)) {
match self.source.request_chunk_load(ChunkLoadRequest {
env: Arc::clone(&self.env),
height: self.height,
cx,
cz
}) {
Ok(_) => {
self.loading_chunks.insert((cx, cz));
true
}
Err(_) => false
}
} else {
false
}
}
pub fn load_chunks_with_callback<F>(&mut self, mut callback: F)
where
F: FnMut(i32, i32, Result<&Arc<RwLock<Chunk>>, LevelSourceError>),
{
while let Some(res) = self.source.poll_chunk() {
match res {
Ok(ProtoChunk {
inner: chunk,
mut proto_entities,
dirty
}) => {
let mut chunk = *chunk;
let (cx, cz) = chunk.get_position();
self.loading_chunks.remove(&(cx, cz));
let mut entities_handles = Vec::with_capacity(proto_entities.len());
for (entity_builder, _) in &mut proto_entities {
unsafe {
let entity = self.entities.add_entity_unchecked(entity_builder);
chunk.add_entity_unchecked(entity);
entities_handles.push(entity);
}
}
for (i, (_, passengers)) in proto_entities.into_iter().enumerate() {
if !passengers.is_empty() {
let mut base_entity = self.entities.ecs.get_mut::<BaseEntity>(entities_handles[i]).unwrap();
let mut passengers_handles = Vec::with_capacity(passengers.len());
for passenger_proto_index in passengers {
passengers_handles.push(entities_handles[passenger_proto_index]);
}
base_entity.passengers = Some(passengers_handles);
}
}
let chunk_arc = self.chunks.insert_chunk(chunk);
callback(cx, cz, Ok(chunk_arc));
if dirty {
self.request_chunk_save(cx, cz);
}
},
Err((err, chunk_info)) => {
debug!("Failed to load chunk at {}/{}: {}", chunk_info.cx, chunk_info.cz, err);
self.loading_chunks.remove(&(chunk_info.cx, chunk_info.cz));
callback(chunk_info.cx, chunk_info.cz, Err(err));
}
}
}
}
#[inline]
pub fn load_chunks(&mut self) {
self.load_chunks_with_callback(|_, _, _| {});
}
#[inline]
pub fn get_loading_chunks_count(&self) -> usize {
self.loading_chunks.len()
}
pub fn request_chunk_save(&mut self, cx: i32, cz: i32) -> bool {
if let Some(chunk) = self.chunks.get_chunk_arc(cx, cz) {
self.source.request_chunk_save(ChunkSaveRequest {
cx,
cz,
chunk
}).is_ok()
} else {
false
}
}
pub fn spawn_entity(&mut self, entity_type: &'static EntityType, pos: EntityPos) -> Option<Entity> {
if !self.env.entities.has_entity_type(entity_type) {
return None;
}
let chunk = self.chunks.get_chunk_at_block_mut(BlockPos::from(&pos));
let entity = unsafe { self.entities.spawn_entity_unchecked(entity_type, pos) };
if let Some(mut chunk) = chunk {
unsafe {
chunk.add_entity_unchecked(entity);
}
}
Some(entity)
}
}
pub struct ChunkStorage {
chunks: HashMap<(i32, i32), Arc<RwLock<Chunk>>>,
}
impl ChunkStorage {
pub fn get_chunks_count(&self) -> usize {
self.chunks.len()
}
pub fn insert_chunk(&mut self, chunk: Chunk) -> &Arc<RwLock<Chunk>> {
let pos = chunk.get_position();
let arc = Arc::new(RwLock::new(chunk));
match self.chunks.entry(pos) {
Entry::Occupied(mut o) => {
o.insert(arc);
o.into_mut()
},
Entry::Vacant(v) => {
v.insert(arc)
}
}
}
pub fn get_chunk_arc(&self, cx: i32, cz: i32) -> Option<Arc<RwLock<Chunk>>> {
self.chunks.get(&(cx, cz)).map(Arc::clone)
}
pub fn is_chunk_loaded(&self, cx: i32, cz: i32) -> bool {
self.chunks.contains_key(&(cx, cz))
}
pub fn get_chunk(&self, cx: i32, cz: i32) -> Option<RwLockReadGuard<Chunk>> {
self.chunks.get(&(cx, cz)).map(|arc| arc.read().unwrap())
}
pub fn get_chunk_mut(&self, cx: i32, cz: i32) -> Option<RwLockWriteGuard<Chunk>> {
self.chunks.get(&(cx, cz)).map(|arc| arc.write().unwrap())
}
#[inline]
pub fn get_chunk_at(&self, x: i32, z: i32) -> Option<RwLockReadGuard<Chunk>> {
self.get_chunk(x >> 4, z >> 4)
}
#[inline]
pub fn get_chunk_at_mut(&self, x: i32, z: i32) -> Option<RwLockWriteGuard<Chunk>> {
self.get_chunk_mut(x >> 4, z >> 4)
}
#[inline]
pub fn get_chunk_at_block(&self, block_pos: BlockPos) -> Option<RwLockReadGuard<Chunk>> {
self.get_chunk_at(block_pos.x, block_pos.z)
}
#[inline]
pub fn get_chunk_at_block_mut(&self, block_pos: BlockPos) -> Option<RwLockWriteGuard<Chunk>> {
self.get_chunk_at_mut(block_pos.x, block_pos.z)
}
pub fn set_block_at(&self, x: i32, y: i32, z: i32, block: &'static BlockState) -> ChunkResult<()> {
if let Some(mut chunk) = self.get_chunk_at_mut(x, z) {
chunk.set_block_at(x, y, z, block)
} else {
Err(ChunkError::ChunkUnloaded)
}
}
pub fn get_block_at(&self, x: i32, y: i32, z: i32) -> ChunkResult<&'static BlockState> {
if let Some(chunk) = self.get_chunk_at(x, z) {
chunk.get_block_at(x, y, z)
} else {
Err(ChunkError::ChunkUnloaded)
}
}
}
pub struct EntityStorage {
pub ecs: EcsWorld,
builder: EntityBuilder,
}
impl EntityStorage {
pub unsafe fn spawn_entity_unchecked(&mut self, entity_type: &'static EntityType, pos: EntityPos) -> Entity {
self.builder.add(BaseEntity::new(entity_type, Uuid::new_v4(), pos));
for &component in entity_type.codecs {
component.default(&mut self.builder);
}
self.ecs.spawn(self.builder.build())
}
pub unsafe fn add_entity_unchecked(&mut self, entity_builder: &mut EntityBuilder) -> Entity {
self.ecs.spawn(entity_builder.build())
}
pub fn remove_entity(&mut self, entity: Entity) -> bool {
self.ecs.despawn(entity).is_ok()
}
pub fn get_entity_ref(&self, entity: Entity) -> Option<EntityRef> {
self.ecs.entity(entity).ok()
}
}
pub struct BaseEntity {
pub entity_type: &'static EntityType,
pub uuid: Uuid,
pub pos: EntityPos,
passengers: Option<Vec<Entity>>
}
impl BaseEntity {
pub fn new(entity_type: &'static EntityType, uuid: Uuid, pos: EntityPos) -> Self {
Self {
entity_type,
uuid,
pos,
passengers: None
}
}
}