Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add Entity Saving/Reading #638

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pumpkin-protocol/src/client/play/chunk_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use pumpkin_data::packet::clientbound::PLAY_LEVEL_CHUNK_WITH_LIGHT;
use pumpkin_macros::packet;
use pumpkin_world::{
DIRECT_PALETTE_BITS,
chunk::{ChunkData, SUBCHUNKS_COUNT},
storage::{ChunkData, SUBCHUNKS_COUNT},
};

#[packet(PLAY_LEVEL_CHUNK_WITH_LIGHT)]
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-world/src/block/state.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::chunk::format::PaletteEntry;
use crate::storage::format::PaletteEntry;

use super::registry::{get_block, get_state_by_state_id};

Expand Down
167 changes: 0 additions & 167 deletions pumpkin-world/src/chunk/format/mod.rs

This file was deleted.

2 changes: 1 addition & 1 deletion pumpkin-world/src/generation/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use pumpkin_util::math::vector2::Vector2;
use pumpkin_util::math::vector3::Vector3;

use crate::block::state::ChunkBlockState;
use crate::chunk::{ChunkBlocks, ChunkData};
use crate::coordinates::{BlockCoordinates, ChunkRelativeBlockCoordinates, XZBlockCoordinates};
use crate::generation::Seed;
use crate::storage::{ChunkBlocks, ChunkData};

pub trait GeneratorInit {
fn new(seed: Seed) -> Self;
Expand Down
3 changes: 2 additions & 1 deletion pumpkin-world/src/generation/generic_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ use pumpkin_util::math::vector2::Vector2;

use crate::{
WORLD_LOWEST_Y,
chunk::{ChunkBlocks, ChunkData},
coordinates::{ChunkRelativeBlockCoordinates, ChunkRelativeXZBlockCoordinates},
storage::{ChunkBlocks, ChunkData},
};

use super::{
Expand Down Expand Up @@ -76,6 +76,7 @@ impl<B: BiomeGenerator, T: PerlinTerrainGenerator> WorldGenerator for GenericGen
blocks,
heightmap: Default::default(),
position: at,
entities: vec![], // TODO: chunks do have inital entities
// We just generated this chunk! Mark it as dirty
dirty: true,
}
Expand Down
3 changes: 2 additions & 1 deletion pumpkin-world/src/generation/implementation/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ use pumpkin_util::math::{vector2::Vector2, vector3::Vector3};

use crate::{
WORLD_LOWEST_Y, WORLD_MAX_Y,
chunk::{ChunkBlocks, ChunkData},
coordinates::ChunkRelativeBlockCoordinates,
generation::{
GlobalRandomConfig, Seed, WorldGenerator, generator::GeneratorInit,
noise_router::proto_noise_router::GlobalProtoNoiseRouter, proto_chunk::ProtoChunk,
},
noise_router::NOISE_ROUTER_ASTS,
storage::{ChunkBlocks, ChunkData},
};

pub struct TestGenerator {
Expand Down Expand Up @@ -57,6 +57,7 @@ impl WorldGenerator for TestGenerator {
blocks,
heightmap: Default::default(),
position: at,
entities: vec![], // TODO: chunks do have inital entities
// This chunk was just created! We want to say its been changed
dirty: true,
}
Expand Down
47 changes: 27 additions & 20 deletions pumpkin-world/src/level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,16 @@ use tokio::{
};

use crate::{
chunk::{
ChunkData, ChunkParsingError, ChunkReadingError,
format::{anvil::AnvilChunkFile, linear::LinearFile},
io::{ChunkIO, LoadedData, chunk_file_manager::ChunkFileManager},
},
generation::{Seed, WorldGenerator, get_world_gen},
lock::{LevelLocker, anvil::AnvilLevelLocker},
storage::{
ChunkData, ChunkParsingError, ChunkReadingError,
format::{
anvil::{AnvilChunkFile, chunk::AnvilChunkFormat},
linear::LinearFile,
},
io::{ChunkIO, LoadedData, file_manager::FileManager},
},
world_info::{
LevelData, WorldInfoError, WorldInfoReader, WorldInfoWriter,
anvil::{AnvilLevelInfo, LEVEL_DAT_BACKUP_FILE_NAME, LEVEL_DAT_FILE_NAME},
Expand Down Expand Up @@ -49,7 +52,7 @@ pub struct Level {
loaded_chunks: Arc<DashMap<Vector2<i32>, SyncChunk>>,
chunk_watchers: Arc<DashMap<Vector2<i32>, usize>>,

chunk_saver: Arc<dyn ChunkIO<Data = SyncChunk>>,
chunk_io: Arc<dyn ChunkIO<Data = SyncChunk>>,
world_gen: Arc<dyn WorldGenerator>,
// Gets unlocked when dropped
// TODO: Make this a trait
Expand All @@ -59,6 +62,7 @@ pub struct Level {
#[derive(Clone)]
pub struct LevelFolder {
pub root_folder: PathBuf,
pub entities_folder: PathBuf,
pub region_folder: PathBuf,
}

Expand All @@ -67,10 +71,15 @@ impl Level {
// If we are using an already existing world we want to read the seed from the level.dat, If not we want to check if there is a seed in the config, if not lets create a random one
let region_folder = root_folder.join("region");
if !region_folder.exists() {
std::fs::create_dir_all(&region_folder).expect("Failed to create Region folder");
std::fs::create_dir_all(&region_folder).expect("Failed to create region folder");
}
let entities_folder = root_folder.join("entities");
if !entities_folder.exists() {
std::fs::create_dir_all(&region_folder).expect("Failed to create entities folder");
}
let level_folder = LevelFolder {
root_folder,
entities_folder,
region_folder,
};

Expand Down Expand Up @@ -110,18 +119,18 @@ impl Level {
let seed = Seed(level_info.world_gen_settings.seed as u64);
let world_gen = get_world_gen(seed).into();

let chunk_saver: Arc<dyn ChunkIO<Data = SyncChunk>> = match advanced_config().chunk.format {
let chunk_io: Arc<dyn ChunkIO<Data = SyncChunk>> = match advanced_config().chunk.format {
//ChunkFormat::Anvil => (Arc::new(AnvilChunkFormat), Arc::new(AnvilChunkFormat)),
ChunkFormat::Linear => Arc::new(ChunkFileManager::<LinearFile>::default()),
ChunkFormat::Anvil => Arc::new(ChunkFileManager::<AnvilChunkFile>::default()),
ChunkFormat::Linear => Arc::new(FileManager::<LinearFile>::default()),
ChunkFormat::Anvil => Arc::new(FileManager::<AnvilChunkFormat>::default()),
};

Self {
seed,
world_gen,
world_info_writer: Arc::new(AnvilLevelInfo),
level_folder,
chunk_saver,
chunk_io,
spawn_chunks: Arc::new(DashMap::new()),
loaded_chunks: Arc::new(DashMap::new()),
chunk_watchers: Arc::new(DashMap::new()),
Expand All @@ -134,7 +143,7 @@ impl Level {
log::info!("Saving level...");

// wait for chunks currently saving in other threads
self.chunk_saver.block_and_await_ongoing_tasks().await;
self.chunk_io.block_and_await_ongoing_tasks().await;

// save all chunks currently in memory
let chunks_to_write = self
Expand All @@ -145,7 +154,7 @@ impl Level {
self.loaded_chunks.clear();

// TODO: I think the chunk_saver should be at the server level
self.chunk_saver.clear_watched_chunks().await;
self.chunk_io.clear_watched_chunks().await;
self.write_chunks(chunks_to_write).await;

// then lets save the world info
Expand All @@ -166,7 +175,7 @@ impl Level {
}

pub async fn clean_up_log(&self) {
self.chunk_saver.clean_up_log().await;
self.chunk_io.clean_up_log().await;
}

pub fn list_cached(&self) {
Expand Down Expand Up @@ -197,9 +206,7 @@ impl Level {
}
}

self.chunk_saver
.watch_chunks(&self.level_folder, chunks)
.await;
self.chunk_io.watch_chunks(&self.level_folder, chunks).await;
}

#[inline]
Expand Down Expand Up @@ -233,7 +240,7 @@ impl Level {
}
}

self.chunk_saver
self.chunk_io
.unwatch_chunks(&self.level_folder, chunks)
.await;
chunks_to_clean
Expand Down Expand Up @@ -320,7 +327,7 @@ impl Level {
return;
}

let chunk_saver = self.chunk_saver.clone();
let chunk_saver = self.chunk_io.clone();
let level_folder = self.level_folder.clone();

trace!("Sending chunks to ChunkIO {:}", chunks_to_write.len());
Expand Down Expand Up @@ -465,7 +472,7 @@ impl Level {
set.spawn(handle_load);
set.spawn(handle_generate);

self.chunk_saver
self.chunk_io
.fetch_chunks(&self.level_folder, &remaining_chunks, load_bridge_send)
.await;
let _ = set.join_all().await;
Expand Down
2 changes: 1 addition & 1 deletion pumpkin-world/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use pumpkin_util::math::vector2::Vector2;

pub mod biome;
pub mod block;
pub mod chunk;
pub mod coordinates;
pub mod cylindrical_chunk_iterator;
pub mod dimension;
Expand All @@ -11,6 +10,7 @@ pub mod item;
pub mod level;
mod lock;
mod noise_router;
pub mod storage;
pub mod world_info;
pub const WORLD_HEIGHT: usize = 384;
pub const WORLD_LOWEST_Y: i16 = -64;
Expand Down
Loading
Loading