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};


/// A structure that contains the static environment of a World, this can be used for multiple
/// `Level`s through an `Arc<LevelEnv>`.
pub struct LevelEnv {
    /// Global blocks palette.
    pub blocks: GlobalBlocks,
    /// Global biomes palette.
    pub biomes: GlobalBiomes,
    /// Global entity types palette.
    pub entities: GlobalEntities,
    /// Global heightmap types palette.
    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()
    }
}


/// Main storage for a level, part of a World. This structure is intentionally not `Sync + Send`,
/// however each chunk is stored in a `RwLock` in order to make them shared across threads if
/// you want.
pub struct Level {
    /// The unique ID of this level (among all levels of the world).
    id: String,
    /// The global environment used by this level, this environment should not be mutated afterward.
    /// It contains the global blocks and biomes palettes, it also contains
    env: Arc<LevelEnv>,
    /// The level loader used to load uncached chunks either from a generator or from an anvil file
    /// system loader.
    source: Box<dyn LevelSource>,
    /// A set of chunk positions that has been requested to the level source.
    loading_chunks: HashSet<(i32, i32)>,
    /// The configured height of this level.
    height: ChunkHeight,
    /// Chunk storage.
    pub chunks: ChunkStorage,
    /// Entities storage.
    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,
        }

    }

    /// Return the unique ID (unique in the owning world).
    pub fn get_id(&self) -> &String {
        &self.id
    }

    /// Return the level environment used by this level.
    pub fn get_env(&self) -> &Arc<LevelEnv> {
        &self.env
    }

    /// Return the minimum and maximum chunks position allowed in this world.
    /// The limits can -128 to 127, it is more than enough.
    pub fn get_height(&self) -> ChunkHeight {
        self.height
    }

    // CHUNKS LOADING (FROM SOURCE) //

    /// Request internal level source to load the given chunk.
    pub fn request_chunk_load(&mut self, cx: i32, cz: i32) -> bool {
        if !self.loading_chunks.contains(&(cx, cz)) {
            // debug!("Request chunk load at {}/{}", 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
        }
    }

    /// Poll loaded chunks from internal level source, all successfully loaded chunks
    /// are added to the underlying `LevelStorage`. The callback is called for each
    /// loaded chunks or loading error.
    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();
                    // debug!("Loaded chunk at {}/{}", cx, cz);

                    self.loading_chunks.remove(&(cx, cz));

                    // This list retains all entity handles with the same order as proto chunk
                    // entities data.
                    let mut entities_handles = Vec::with_capacity(proto_entities.len());

                    // Then we only add entities without their passengers, but store their handles.
                    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);
                        }
                    }

                    // Now that we have created all our entities, we can set passengers.
                    for (i, (_, passengers)) in proto_entities.into_iter().enumerate() {
                        if !passengers.is_empty() {

                            // Here we don't check is passengers is empty because ProtoChunk only
                            // set 'Some' if there are passengers.

                            // SAFETY: Unwrap is safe because the entity was just created with `BaseEntity` component.
                            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)) => {
                    // IDE shows an error for 'Display' not being implemented, but we use the
                    // crate 'thiserror' to implement it through a custom derive.
                    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));
                }
            }
        }
    }

    /// Poll loaded chunks from internal level source, all successfully loaded chunks
    /// are added to the underlying `LevelStorage`.
    #[inline]
    pub fn load_chunks(&mut self) {
        self.load_chunks_with_callback(|_, _, _| {});
    }

    /// Returns the number of chunks being loaded or queued for loading (use `load_chunks` or
    /// `load_chunks_with_callback` to load actually them.
    #[inline]
    pub fn get_loading_chunks_count(&self) -> usize {
        self.loading_chunks.len()
    }

    // CHUNK SAVING (TO SOURCE) //

    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
        }
    }

    // ENTITIES //

    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 {
    /// Storing all cached chunks that were loaded from source.
    chunks: HashMap<(i32, i32), Arc<RwLock<Chunk>>>,
}

impl ChunkStorage {

    // CHUNKS //

    pub fn get_chunks_count(&self) -> usize {
        self.chunks.len()
    }

    /// Insert a chunk at a specific position.
    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)
    }

    /// Return true if a chunk is loaded at a specific position.
    pub fn is_chunk_loaded(&self, cx: i32, cz: i32) -> bool {
        self.chunks.contains_key(&(cx, cz))
    }

    /// Get a chunk reference at specific coordinates.
    pub fn get_chunk(&self, cx: i32, cz: i32) -> Option<RwLockReadGuard<Chunk>> {
        self.chunks.get(&(cx, cz)).map(|arc| arc.read().unwrap())
    }

    /// Get a mutable chunk reference at specific coordinates.
    pub fn get_chunk_mut(&self, cx: i32, cz: i32) -> Option<RwLockWriteGuard<Chunk>> {
        self.chunks.get(&(cx, cz)).map(|arc| arc.write().unwrap())
    }

    /// Get a chunk reference at specific blocks coordinates.
    #[inline]
    pub fn get_chunk_at(&self, x: i32, z: i32) -> Option<RwLockReadGuard<Chunk>> {
        self.get_chunk(x >> 4, z >> 4)
    }

    /// Get a mutable chunk reference at specific blocks coordinates.
    #[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)
    }

    // BLOCKS //

    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 {
    /// The ECS storing all entities in the level.
    pub ecs: EcsWorld,
    /// Internal entity builder kept
    builder: EntityBuilder,
}

impl EntityStorage {

    /// Spawn an entity in the level owning this storage, you must give its type and position,
    /// its handle is returned. If the given entity type is not supported by the level's
    /// environment, `None` is returned.
    ///
    /// # Safety:
    /// This method is made for internal use because the entity type must be supported checked
    /// to be supported by level's environment. The returned entity handle must also be added
    /// in the the associated chunk if existing.
    ///
    /// # See:
    /// Use `Level::spawn_entity` instead of this method if you want to avoid safety issues.
    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())

    }

    /// Add a raw entity from a builder, this method is unsafe because the caller must ensure
    /// that the builder contains a `BaseEntity` component with an entity
    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()
    }

}

/// Base entity component, present in all entities of a level, must not be removed.
pub struct BaseEntity {
    pub entity_type: &'static EntityType,
    pub uuid: Uuid,
    pub pos: EntityPos,
    /// An optional list of entities that are on top of this one.
    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
        }
    }

}