Got a simple retrieve to pass

This commit is contained in:
Nicholas Novak
2023-10-02 15:56:33 -07:00
parent 914290fe9e
commit e5547477fc
4 changed files with 97 additions and 38 deletions

View File

@@ -1,20 +1,21 @@
use crate::storage::world::{BlockID, BlockPos, BlockRange, ChunkData, ChunkPos};
use crate::storage_server::StorageServer;
#[derive(Clone)]
#[derive(Debug, Clone)]
struct SingleBlock {
id: BlockID,
position: BlockPos,
}
#[derive(Debug)]
struct MultipleBlocks {
id: BlockID,
range: BlockRange,
}
#[derive(Debug)]
pub struct SimpleServer {
chunks: Vec<ChunkData>,
single_blocks: Vec<SingleBlock>,
block_ranges: Vec<MultipleBlocks>,
}
@@ -22,44 +23,79 @@ impl SimpleServer {
pub fn new() -> Self {
SimpleServer {
chunks: Vec::new(),
single_blocks: Vec::new(),
block_ranges: Vec::new(),
}
}
fn chunk_at_block_mut(&mut self, block_pos: &BlockPos) -> Option<&mut ChunkData> {
// Find what chunk the block is in
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);
}
}
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
}
fn create_chunk_at(&mut self, chunk_pos: &ChunkPos) {
let new_chunk = ChunkData::new(chunk_pos);
self.chunks.push(new_chunk);
}
}
impl StorageServer for SimpleServer {
fn change_block(&mut self, target_state: BlockID, world_position: BlockPos) {
let chunk_pos = ChunkPos::from(&world_position);
fn change_block(&mut self, target_state: BlockID, world_position: &BlockPos) {
let mut chunk = self.chunk_at_block_mut(world_position);
println!("Chunk position: {:?}", chunk_pos);
for chunk in self.chunks.iter_mut() {
if chunk.pos.same_location(&chunk_pos) {
let current_section = &mut chunk.sections[world_position.y / 16];
let chunk_array_index = current_section.index_of_block(&world_position);
current_section.update_block_at_index(&target_state, chunk_array_index);
}
// 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);
}
self.single_blocks.push(SingleBlock {
id: target_state,
position: world_position,
});
let 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];
// Find the index that the block is at, and update its state
let chunk_array_index = current_section.index_of_block(&world_position);
current_section.update_block_at_index(&target_state, chunk_array_index);
}
fn change_block_range(&mut self, target_stage: BlockID, start: BlockPos, end: BlockPos) {
fn change_block_range(&mut self, target_stage: BlockID, start: &BlockPos, end: &BlockPos) {
self.block_ranges.push(MultipleBlocks {
id: target_stage,
range: BlockRange { start, end },
range: BlockRange {
start: start.clone(),
end: end.clone(),
},
})
}
fn read_block_at(&self, pos: BlockPos) -> BlockID {
for block in self.single_blocks.iter() {
if block.position == pos {
return block.id.clone();
}
fn read_block_at(&self, pos: &BlockPos) -> BlockID {
let chunk = self.chunk_at(pos);
if let Some(chunk) = chunk {
let chunk_section = chunk.section_for(pos);
return chunk_section.get_block_at_index(pos).clone();
}
for blocks in self.block_ranges.iter() {