progress: Work on serializing and deserializing data structures, as well as disk storage

This commit is contained in:
Nicholas Novak
2023-10-28 21:39:45 -07:00
parent 63ff6e7c2c
commit 7419412be4
8 changed files with 295 additions and 100 deletions

View File

@@ -1,3 +1,4 @@
use crate::storage::disk_storage::ChunkStorageCache;
use crate::storage::world::{BlockID, BlockPos, BlockRange, ChunkData, ChunkPos};
use crate::storage_server::StorageServer;
@@ -9,66 +10,49 @@ struct MultipleBlocks {
#[derive(Debug)]
pub struct SimpleServer {
chunks: Vec<ChunkData>,
// block_ranges: Vec<MultipleBlocks>,
chunk_storage: ChunkStorageCache,
}
impl SimpleServer {
pub fn new() -> Self {
SimpleServer {
chunks: Vec::new(),
// block_ranges: Vec::new(),
chunk_storage: ChunkStorageCache::new(),
}
}
pub fn num_chunks(&self) -> usize {
self.chunks.len()
unimplemented!()
}
fn chunk_at_block_mut(&mut self, block_pos: &BlockPos) -> Option<&mut ChunkData> {
// Find what chunk the block is in
fn chunk_at(&mut self, block_pos: &BlockPos) -> Option<ChunkData> {
let chunk_pos = ChunkPos::from(block_pos);
// Find the chunk with the correct index
for chunk in self.chunks.iter_mut() {
if chunk.pos == chunk_pos {
return Some(chunk);
}
}
let chunk = self
.chunk_storage
.fetch_chunk_by_pos(&chunk_pos)
.expect("Finding chunk failed");
None
}
fn chunk_at(&self, block_pos: &BlockPos) -> Option<&ChunkData> {
let chunk_pos = ChunkPos::from(block_pos);
for chunk in self.chunks.iter() {
if chunk.pos == chunk_pos {
return Some(chunk);
}
}
None
Some(chunk)
}
fn create_chunk_at(&mut self, chunk_pos: &ChunkPos) {
let new_chunk = ChunkData::new(chunk_pos);
self.chunks.push(new_chunk);
self.chunk_storage
.fetch_chunk_by_pos(&chunk_pos)
.expect("Creatinc chunk failed");
}
}
impl StorageServer for SimpleServer {
fn change_block(&mut self, target_state: BlockID, world_position: &BlockPos) {
let mut chunk = self.chunk_at_block_mut(world_position);
let mut chunk = self.chunk_at(world_position);
// Test if there is a chunk that already exists
if chunk.is_none() {
self.create_chunk_at(&ChunkPos::from(world_position));
chunk = self.chunk_at_block_mut(world_position);
chunk = self.chunk_at(world_position);
}
let chunk = chunk.expect("Could not find chunk");
let mut chunk = chunk.expect("Could not find chunk");
// Find the section that the block is located in
let current_section = &mut chunk.sections[world_position.y % 16];
@@ -79,16 +63,9 @@ impl StorageServer for SimpleServer {
fn change_block_range(&mut self, target_stage: BlockID, start: &BlockPos, end: &BlockPos) {
unimplemented!()
// self.block_ranges.push(MultipleBlocks {
// id: target_stage,
// range: BlockRange {
// start: start.clone(),
// end: end.clone(),
// },
// })
}
fn read_block_at(&self, pos: &BlockPos) -> BlockID {
fn read_block_at(&mut self, pos: &BlockPos) -> BlockID {
let chunk = self.chunk_at(pos);
if let Some(chunk) = chunk {
@@ -97,12 +74,6 @@ impl StorageServer for SimpleServer {
return chunk_section.get_block_at_index(pos).clone();
}
// for blocks in self.block_ranges.iter() {
// if blocks.range.within_range(&pos) {
// return blocks.id.clone();
// }
// }
BlockID::Empty
}
}