Skip to content

Commit 876eb92

Browse files
committed
made dungeon position correctly in the world
1 parent c1bbd0d commit 876eb92

File tree

6 files changed

+35
-27
lines changed

6 files changed

+35
-27
lines changed

src/dungeon/dungeon.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use std::cell::RefCell;
1616
use std::rc::Rc;
1717

1818
// The top leftmost corner of the dungeon
19-
pub const DUNGEON_ORIGIN: (i32, i32) = (0, 0);
19+
pub const DUNGEON_ORIGIN: (i32, i32) = (-200, -200);
2020

2121
// The positions of the doors in the world
2222
pub const DOOR_POSITIONS: [(i32, i32); 60] = [(DUNGEON_ORIGIN.0 + 31, DUNGEON_ORIGIN.1 + 15), (DUNGEON_ORIGIN.0 + 63, DUNGEON_ORIGIN.1 + 15), (DUNGEON_ORIGIN.0 + 95, DUNGEON_ORIGIN.1 + 15), (DUNGEON_ORIGIN.0 + 127, DUNGEON_ORIGIN.1 + 15), (DUNGEON_ORIGIN.0 + 159, DUNGEON_ORIGIN.1 + 15), (DUNGEON_ORIGIN.0 + 15, DUNGEON_ORIGIN.1 + 31), (DUNGEON_ORIGIN.0 + 47, DUNGEON_ORIGIN.1 + 31), (DUNGEON_ORIGIN.0 + 79, DUNGEON_ORIGIN.1 + 31), (DUNGEON_ORIGIN.0 + 111, DUNGEON_ORIGIN.1 + 31), (DUNGEON_ORIGIN.0 + 143, DUNGEON_ORIGIN.1 + 31), (DUNGEON_ORIGIN.0 + 175, DUNGEON_ORIGIN.1 + 31), (DUNGEON_ORIGIN.0 + 31, DUNGEON_ORIGIN.1 + 47), (DUNGEON_ORIGIN.0 + 63, DUNGEON_ORIGIN.1 + 47), (DUNGEON_ORIGIN.0 + 95, DUNGEON_ORIGIN.1 + 47), (DUNGEON_ORIGIN.0 + 127, DUNGEON_ORIGIN.1 + 47), (DUNGEON_ORIGIN.0 + 159, DUNGEON_ORIGIN.1 + 47), (DUNGEON_ORIGIN.0 + 15, DUNGEON_ORIGIN.1 + 63), (DUNGEON_ORIGIN.0 + 47, DUNGEON_ORIGIN.1 + 63), (DUNGEON_ORIGIN.0 + 79, DUNGEON_ORIGIN.1 + 63), (DUNGEON_ORIGIN.0 + 111, DUNGEON_ORIGIN.1 + 63), (DUNGEON_ORIGIN.0 + 143, DUNGEON_ORIGIN.1 + 63), (DUNGEON_ORIGIN.0 + 175, DUNGEON_ORIGIN.1 + 63), (DUNGEON_ORIGIN.0 + 31, DUNGEON_ORIGIN.1 + 79), (DUNGEON_ORIGIN.0 + 63, DUNGEON_ORIGIN.1 + 79), (DUNGEON_ORIGIN.0 + 95, DUNGEON_ORIGIN.1 + 79), (DUNGEON_ORIGIN.0 + 127, DUNGEON_ORIGIN.1 + 79), (DUNGEON_ORIGIN.0 + 159, DUNGEON_ORIGIN.1 + 79), (DUNGEON_ORIGIN.0 + 15, DUNGEON_ORIGIN.1 + 95), (DUNGEON_ORIGIN.0 + 47, DUNGEON_ORIGIN.1 + 95), (DUNGEON_ORIGIN.0 + 79, DUNGEON_ORIGIN.1 + 95), (DUNGEON_ORIGIN.0 + 111, DUNGEON_ORIGIN.1 + 95), (DUNGEON_ORIGIN.0 + 143, DUNGEON_ORIGIN.1 + 95), (DUNGEON_ORIGIN.0 + 175, DUNGEON_ORIGIN.1 + 95), (DUNGEON_ORIGIN.0 + 31, DUNGEON_ORIGIN.1 + 111), (DUNGEON_ORIGIN.0 + 63, DUNGEON_ORIGIN.1 + 111), (DUNGEON_ORIGIN.0 + 95, DUNGEON_ORIGIN.1 + 111), (DUNGEON_ORIGIN.0 + 127, DUNGEON_ORIGIN.1 + 111), (DUNGEON_ORIGIN.0 + 159, DUNGEON_ORIGIN.1 + 111), (DUNGEON_ORIGIN.0 + 15, DUNGEON_ORIGIN.1 + 127), (DUNGEON_ORIGIN.0 + 47, DUNGEON_ORIGIN.1 + 127), (DUNGEON_ORIGIN.0 + 79, DUNGEON_ORIGIN.1 + 127), (DUNGEON_ORIGIN.0 + 111, DUNGEON_ORIGIN.1 + 127), (DUNGEON_ORIGIN.0 + 143, DUNGEON_ORIGIN.1 + 127), (DUNGEON_ORIGIN.0 + 175, DUNGEON_ORIGIN.1 + 127), (DUNGEON_ORIGIN.0 + 31, DUNGEON_ORIGIN.1 + 143), (DUNGEON_ORIGIN.0 + 63, DUNGEON_ORIGIN.1 + 143), (DUNGEON_ORIGIN.0 + 95, DUNGEON_ORIGIN.1 + 143), (DUNGEON_ORIGIN.0 + 127, DUNGEON_ORIGIN.1 + 143), (DUNGEON_ORIGIN.0 + 159, DUNGEON_ORIGIN.1 + 143), (DUNGEON_ORIGIN.0 + 15, DUNGEON_ORIGIN.1 + 159), (DUNGEON_ORIGIN.0 + 47, DUNGEON_ORIGIN.1 + 159), (DUNGEON_ORIGIN.0 + 79, DUNGEON_ORIGIN.1 + 159), (DUNGEON_ORIGIN.0 + 111, DUNGEON_ORIGIN.1 + 159), (DUNGEON_ORIGIN.0 + 143, DUNGEON_ORIGIN.1 + 159), (DUNGEON_ORIGIN.0 + 175, DUNGEON_ORIGIN.1 + 159), (DUNGEON_ORIGIN.0 + 31, DUNGEON_ORIGIN.1 + 175), (DUNGEON_ORIGIN.0 + 63, DUNGEON_ORIGIN.1 + 175), (DUNGEON_ORIGIN.0 + 95, DUNGEON_ORIGIN.1 + 175), (DUNGEON_ORIGIN.0 + 127, DUNGEON_ORIGIN.1 + 175), (DUNGEON_ORIGIN.0 + 159, DUNGEON_ORIGIN.1 + 175)];

src/main.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,6 @@ use indoc::formatdoc;
3030
use rand::seq::IndexedRandom;
3131
use std::collections::HashMap;
3232
use std::env;
33-
use std::mem::take;
3433
use std::time::{Duration, SystemTime, UNIX_EPOCH};
3534
use tokio::sync::mpsc::unbounded_channel;
3635

@@ -214,14 +213,14 @@ async fn main() -> Result<()> {
214213
match diff {
215214
ChunkDiff::New => {
216215
if let Some(chunk) = player.world_mut().chunk_grid.get_chunk(x, z) {
217-
player.packet_buffer.write_packet(&chunk.get_chunk_data(true));
216+
player.packet_buffer.write_packet(&chunk.get_chunk_data(x, z,true));
218217
} else {
219-
let chunk_data = Chunk::new(x, z).get_chunk_data(true);
218+
let chunk_data = Chunk::new().get_chunk_data(x, z, true);
220219
player.write_packet(&chunk_data)
221220
};
222221
},
223222
ChunkDiff::Old => {
224-
let chunk_data = Chunk::new(x, z).get_chunk_data(true);
223+
let chunk_data = Chunk::new().get_chunk_data(x, z, true);
225224
player.write_packet(&chunk_data)
226225
},
227226
}

src/server/chunk/chunk.rs

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,6 @@ use crate::server::chunk::chunk_section::ChunkSection;
66
///
77
/// A chunk is composed of 16 [chunk sections][ChunkSection].
88
pub struct Chunk {
9-
pub pos_x: i32,
10-
pub pos_z: i32,
119
pub chunk_sections: [Option<ChunkSection>; 16],
1210
pub packet_buffer: PacketBuffer,
1311
}
@@ -17,10 +15,8 @@ impl Chunk {
1715
/// Creates an empty chunk at the X and Z coordinates provided.
1816
///
1917
/// The chunk is entirely empty, and block data must be added with chunk sections.
20-
pub fn new(pos_x: i32, pos_z: i32) -> Chunk {
18+
pub fn new() -> Chunk {
2119
Self {
22-
pos_x,
23-
pos_z,
2420
chunk_sections: [
2521
None, None, None, None, None, None, None, None,
2622
None, None, None, None, None, None, None, None,
@@ -49,7 +45,7 @@ impl Chunk {
4945
}
5046

5147

52-
pub fn get_chunk_data(&self, new: bool) -> ChunkData {
48+
pub fn get_chunk_data(&self, x: i32, z: i32, new: bool) -> ChunkData {
5349
let mut bitmask = 0u16;
5450

5551
for section_index in 0..16 {
@@ -93,10 +89,9 @@ impl Chunk {
9389
offset += 1;
9490
}
9591
}
96-
9792
ChunkData {
98-
chunk_x: self.pos_x,
99-
chunk_z: self.pos_z,
93+
chunk_x: x,
94+
chunk_z: z,
10095
is_new_chunk: new,
10196
bitmask,
10297
data,

src/server/chunk/chunk_grid.rs

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -10,20 +10,23 @@ use crate::server::world::VIEW_DISTANCE;
1010
pub struct ChunkGrid {
1111
pub chunks: Vec<Chunk>,
1212
pub size: usize,
13+
14+
index_offset_x: usize,
15+
index_offset_z: usize,
1316
}
1417

1518
impl ChunkGrid {
1619

17-
pub fn new(size: usize) -> ChunkGrid {
18-
let mut vec = Vec::with_capacity(size * size);
19-
for z in 0..size {
20-
for x in 0..size {
21-
vec.push(Chunk::new(x as i32, z as i32))
22-
}
20+
pub fn new(size: usize, offset_x: usize, offset_z: usize) -> ChunkGrid {
21+
let mut chunks = Vec::with_capacity(size * size);
22+
for _ in 0..size * size {
23+
chunks.push(Chunk::new());
2324
}
2425
ChunkGrid {
25-
chunks: vec,
26+
chunks,
2627
size,
28+
index_offset_x: offset_x,
29+
index_offset_z: offset_z,
2730
}
2831
}
2932

@@ -71,19 +74,30 @@ impl ChunkGrid {
7174
/// checks is block is a valid block within the chunk grid.
7275
fn is_block_valid(&self, x: i32, y: i32, z: i32) -> bool {
7376
let size = self.size as i32;
74-
x >= 0 && (x >> 4) < size && y >= 0 && y < 256 && z >= 0 && (z >> 4) < size
77+
let chunk_x = (x >> 4) + self.index_offset_x as i32;
78+
let chunk_z = (z >> 4) + self.index_offset_z as i32;
79+
y >= 0 && y < 256 && chunk_x >= 0 && chunk_x < size && chunk_z >= 0 && chunk_z < size
7580
}
7681

7782
/// returns the chunk at the x and z coordinates provided, none if no chunk is present
7883
pub fn get_chunk(&self, chunk_x: i32, chunk_z: i32) -> Option<&Chunk> {
79-
if chunk_x < 0 || chunk_z < 0 {
84+
let size = self.size as i32;
85+
let x = chunk_x + self.index_offset_x as i32;
86+
let z = chunk_z + self.index_offset_z as i32;
87+
if x < 0 || z < 0 || x >= size || z >= size {
8088
return None
8189
}
82-
self.chunks.get(chunk_z as usize * self.size + chunk_x as usize)
90+
self.chunks.get(z as usize * self.size + x as usize)
8391
}
8492

8593
fn get_chunk_mut(&mut self, chunk_x: i32, chunk_z: i32) -> Option<&mut Chunk> {
86-
self.chunks.get_mut(chunk_z as usize * self.size + chunk_x as usize)
94+
let size = self.size as i32;
95+
let x = chunk_x + self.index_offset_x as i32;
96+
let z = chunk_z + self.index_offset_z as i32;
97+
if x < 0 || z < 0 || x >= size || z >= size {
98+
return None
99+
}
100+
self.chunks.get_mut(z as usize * self.size + x as usize)
87101
}
88102
}
89103

src/server/server.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ impl Server {
8989
for x in (chunk_x - view_distance)..(chunk_x + view_distance) {
9090
for z in (chunk_z - view_distance)..(chunk_z + view_distance) {
9191
if let Some(chunk) = self.world.chunk_grid.get_chunk(x, z) {
92-
player.write_packet(&chunk.get_chunk_data(true))
92+
player.write_packet(&chunk.get_chunk_data(x, z, true))
9393
}
9494
}
9595
}

src/server/world.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ impl World {
4444
World {
4545
server: std::ptr::null_mut(),
4646

47-
chunk_grid: ChunkGrid::new(14),
47+
chunk_grid: ChunkGrid::new(16, 13, 13),
4848
interactable_blocks: HashMap::new(),
4949

5050
player_info: PlayerList::new(),

0 commit comments

Comments
 (0)