Skip to content

Commit 8f3398f

Browse files
committed
replaced usage of Block Positions with IVec3.
made BlockPosition a wrapper for IVec3 to serialize and deserialize for packets.
1 parent d0061b0 commit 8f3398f

File tree

13 files changed

+122
-216
lines changed

13 files changed

+122
-216
lines changed

src/block/rotatable.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::types::direction::Direction;
2+
use glam::IVec3;
23

34
pub trait Rotatable {
45
fn rotate(&self, direction: Direction) -> Self;
@@ -13,19 +14,18 @@ impl Rotatable for f32 {
1314
Direction::West => 270.0,
1415
Direction::Up | Direction::Down => 0.0,
1516
};
16-
// println!("yaw {self} offset {offset} result {}", (self + offset) % 360.0);
1717
(self + offset) % 360.0
18+
}
19+
}
1820

19-
// let offset = match dir {
20-
// Direction::North => 0.0,
21-
// Direction::East => 90.0,
22-
// Direction::South => 180.0,
23-
// Direction::West => -90.0,
24-
// Direction::Up | Direction::Down => 0.0,
25-
// };
26-
// let mut result = self + offset;
27-
// result = ((result + 180.0) % 360.0) - 180.0;
28-
// // println!("yaw {self} offset {offset} result {result}");
29-
// result
21+
impl Rotatable for IVec3 {
22+
fn rotate(&self, direction: Direction) -> Self {
23+
match direction {
24+
Direction::North => Self { x: self.x, y: self.y, z: self.z },
25+
Direction::East => Self { x: -self.z, y: self.y, z: self.x },
26+
Direction::South => Self { x: -self.x, y: self.y, z: -self.z },
27+
Direction::West => Self { x: self.z, y: self.y, z: -self.x },
28+
_ => Self { x: self.x, y: self.y, z: self.z },
29+
}
3030
}
3131
}

src/dungeon/door/door.rs

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
use crate::block::block_parameter::Axis;
22
use crate::block::blocks::Blocks;
3+
use crate::block::rotatable::Rotatable;
34
use crate::dungeon::door::door_entity::DoorEntityImpl;
45
use crate::dungeon::dungeon::Dungeon;
5-
use crate::types::block_position::BlockPos;
66
use crate::utils::seeded_rng::seeded_rng;
77
use crate::world::chunk::chunk_grid::ChunkGrid;
88
use crate::world::world::World;
9-
use glam::DVec3;
9+
use glam::{ivec3, DVec3, IVec3};
1010
use rand::prelude::IndexedRandom;
1111
use std::collections::HashMap;
1212

@@ -24,8 +24,8 @@ pub struct Door {
2424
pub axis: Axis,
2525
door_type: DoorType,
2626

27-
inner_start: BlockPos,
28-
inner_end: BlockPos,
27+
inner_start: IVec3,
28+
inner_end: IVec3,
2929

3030
pub is_open: bool,
3131
}
@@ -39,8 +39,8 @@ impl Door {
3939
axis,
4040
is_open: door_type == DoorType::Normal,
4141
door_type,
42-
inner_start: BlockPos::new(x - 1, 69, z - 1),
43-
inner_end: BlockPos::new(x + 1, 72, z + 1),
42+
inner_start: ivec3(x - 1, 69, z - 1),
43+
inner_end: ivec3(x + 1, 72, z + 1),
4444
}
4545
}
4646

@@ -71,21 +71,21 @@ impl Door {
7171
// Doors have a thick bedrock floor usually
7272
chunk_grid.fill_blocks(
7373
Blocks::Bedrock,
74-
BlockPos::new(self.x - dx, 67, self.z - dz),
75-
BlockPos::new(self.x + dx, 66, self.z + dz),
74+
ivec3(self.x - dx, 67, self.z - dz),
75+
ivec3(self.x + dx, 66, self.z + dz),
7676
);
7777

7878
// Might need to replace with a random palette of cobble, stone, gravel etc if we want to mimic hypixel FULLY, but this works fine.
7979
chunk_grid.fill_blocks(
8080
Blocks::Stone { variant: 0 },
81-
BlockPos::new(self.x - (dz - 2) * 2, 68, self.z - (dx - 2) * 2),
82-
BlockPos::new(self.x + (dz - 2) * 2, 68, self.z + (dx - 2) * 2),
81+
ivec3(self.x - (dz - 2) * 2, 68, self.z - (dx - 2) * 2),
82+
ivec3(self.x + (dz - 2) * 2, 68, self.z + (dx - 2) * 2),
8383
);
8484

8585
chunk_grid.fill_blocks(
8686
Blocks::Air,
87-
BlockPos::new(self.x - dx, 69, self.z - dz),
88-
BlockPos::new(self.x + dx, 73, self.z + dz),
87+
ivec3(self.x - dx, 69, self.z - dz),
88+
ivec3(self.x + dx, 73, self.z + dz),
8989
);
9090

9191
// Pretty much just to get a normal self from a wither one,
@@ -105,7 +105,7 @@ impl Door {
105105
let y = (index / (5 * 5)) as i32;
106106
let z = ((index / 5) % 5) as i32;
107107

108-
let bp = BlockPos::new(x - 2, y, z - 2).rotate(self_direction);
108+
let bp = ivec3(x - 2, y, z - 2).rotate(self_direction);
109109

110110
let mut block_to_place = block.clone();
111111
block_to_place.rotate(self_direction);
@@ -114,8 +114,8 @@ impl Door {
114114

115115
chunk_grid.fill_blocks(
116116
self.get_block(),
117-
BlockPos::new(self.x - 1, 69, self.z - 1),
118-
BlockPos::new(self.x + 1, 72, self.z + 1),
117+
ivec3(self.x - 1, 69, self.z - 1),
118+
ivec3(self.x + 1, 72, self.z + 1),
119119
);
120120
}
121121

@@ -149,8 +149,8 @@ impl Door {
149149

150150
world.chunk_grid.fill_blocks(
151151
Blocks::Barrier,
152-
BlockPos::new(self.x - 1, 69, self.z - 1),
153-
BlockPos::new(self.x + 1, 72, self.z + 1),
152+
ivec3(self.x - 1, 69, self.z - 1),
153+
ivec3(self.x + 1, 72, self.z + 1),
154154
);
155155
// door entity gets rid of blocks when it disappears
156156
world.spawn_entity(
@@ -167,7 +167,7 @@ impl Door {
167167
}
168168

169169
// inner bit of door, blocks abilities
170-
pub fn contains(&self, block_pos: &BlockPos) -> bool {
170+
pub fn contains(&self, block_pos: &IVec3) -> bool {
171171
let (x ,y , z) = (block_pos.x, block_pos.y, block_pos.z);
172172
(x >= self.inner_start.x && x <= self.inner_end.x) &&
173173
(y >= self.inner_start.y && y <= self.inner_end.y) &&

src/dungeon/door/door_entity.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::entity::entity_metadata::{EntityMetadata, EntityVariant};
55
use crate::network::binary::var_int::VarInt;
66
use crate::network::packets::packet_buffer::PacketBuffer;
77
use crate::network::protocol::play::clientbound::{DestroyEntites, EntityAttach, EntityRelativeMove, SpawnMob, SpawnObject};
8-
use crate::types::block_position::BlockPos;
8+
use glam::ivec3;
99

1010
pub struct DoorEntityImpl {
1111
pub block: Blocks,
@@ -98,8 +98,8 @@ impl EntityImpl<Dungeon> for DoorEntityImpl {
9898
world.remove_entity(entity.id);
9999
world.chunk_grid.fill_blocks(
100100
Blocks::Air,
101-
BlockPos::new(self.x, 69, self.z),
102-
BlockPos::new(self.x + 2, 72, self.z + 2),
101+
ivec3(self.x, 69, self.z),
102+
ivec3(self.x + 2, 72, self.z + 2),
103103
);
104104
}
105105
}

src/dungeon/dungeon.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,11 @@ use crate::player::attribute::{Attribute, AttributeMap, AttributeModifier};
1515
use crate::player::player::{ClientId, GameProfile, Player};
1616
use crate::player::sidebar::Sidebar;
1717
use crate::types::aabb::AABB;
18-
use crate::types::block_position::BlockPos;
1918
use crate::types::chat_component::ChatComponent;
2019
use crate::utils::hasher::deterministic_hasher::DeterministicHashMap;
2120
use crate::world::world::{World, WorldExtension};
2221
use anyhow::bail;
23-
use glam::{DVec3, IVec2};
22+
use glam::{ivec3, DVec3, IVec2};
2423
use maplit::hashmap;
2524
use std::cell::{Cell, RefCell};
2625
use std::collections::HashMap;
@@ -138,7 +137,9 @@ impl WorldExtension for Dungeon {
138137
let entrance = world.extension.entrance_room().clone();
139138
let entrance = entrance.borrow();
140139

141-
let position = entrance.get_world_block_pos(&BlockPos::new(15, 72, 18)).as_dvec3_centered();
140+
let mut position = entrance.get_world_block_position(ivec3(15, 72, 18)).as_dvec3();
141+
position.x += 0.5;
142+
position.z += 0.5;
142143

143144
let player = world.spawn_player(
144145
position,

src/dungeon/dungeon_player.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ use crate::network::protocol::play::serverbound::PlayerDiggingAction;
1010
use crate::player::packet_handling::BlockInteractResult;
1111
use crate::player::player::{Player, PlayerExtension};
1212
use crate::player::sidebar::Sidebar;
13-
use crate::types::block_position::BlockPos;
1413
use crate::types::chat_component::ChatComponent;
1514
use crate::types::direction::Direction;
1615
use crate::world::world::World;
1716
use chrono::Local;
17+
use glam::IVec3;
1818
use indoc::{formatdoc, indoc};
1919
use std::cell::{Cell, RefCell};
2020
use std::collections::HashMap;
@@ -71,7 +71,7 @@ impl PlayerExtension for DungeonPlayer {
7171
});
7272
}
7373

74-
fn dig(player: &mut Player<Self>, position: BlockPos, action: &PlayerDiggingAction) {
74+
fn dig(player: &mut Player<Self>, position: IVec3, action: &PlayerDiggingAction) {
7575
let mut restore_block = false;
7676
match action {
7777
PlayerDiggingAction::StartDestroyBlock => {
@@ -163,7 +163,7 @@ impl Player<DungeonPlayer> {
163163
None
164164
}
165165

166-
pub fn try_open_door(&mut self, world: &mut World<Dungeon>, position: &BlockPos) {
166+
pub fn try_open_door(&mut self, world: &mut World<Dungeon>, position: &IVec3) {
167167
if world.has_dungeon_started() {
168168
if let Some(room_rc) = self.get_current_room() {
169169
for neighbour in room_rc.borrow().neighbours() {

src/dungeon/room/room.rs

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
use crate::block::blocks::Blocks;
2+
use crate::block::rotatable::Rotatable;
23
use crate::dungeon::door::door::Door;
34
use crate::dungeon::dungeon::DUNGEON_ORIGIN;
45
use crate::dungeon::room::room_data::RoomData;
56
use crate::player::player::ClientId;
67
use crate::types::aabb::AABB;
7-
use crate::types::block_position::BlockPos;
88
use crate::types::direction::Direction;
99
use crate::world::chunk::chunk_grid::ChunkGrid;
10-
use glam::dvec3;
10+
use glam::{dvec3, ivec3, IVec3};
1111
use std::cell::RefCell;
1212
use std::collections::{HashMap, HashSet};
1313
use std::rc::Rc;
@@ -104,11 +104,15 @@ impl Room {
104104
self.segments.iter().flat_map(|seg| seg.neighbours.iter().map(|n| n).flatten())
105105
}
106106

107-
pub fn get_corner_pos(&self) -> BlockPos {
107+
pub fn get_corner_pos(&self) -> IVec3 {
108108
Room::get_corner_pos_from(&self.segments, &self.rotation, &self.data)
109109
}
110110

111-
pub fn get_corner_pos_from(segments: &[RoomSegment], rotation: &Direction, room_data: &RoomData) -> BlockPos {
111+
pub fn get_corner_pos_from(
112+
segments: &[RoomSegment],
113+
rotation: &Direction,
114+
room_data: &RoomData
115+
) -> IVec3 {
112116
let min_x = segments.iter().min_by(|a, b| a.x.cmp(&b.x)).unwrap().x;
113117
let min_z = segments.iter().min_by(|a, b| a.z.cmp(&b.z)).unwrap().z;
114118

@@ -117,18 +121,18 @@ impl Room {
117121
let z = min_z as i32 * 32 + DUNGEON_ORIGIN.y;
118122

119123
match rotation {
120-
Direction::North => BlockPos { x, y, z },
121-
Direction::East => BlockPos { x: x + room_data.length - 1, y, z },
122-
Direction::South => BlockPos { x: x + room_data.length - 1, y, z: z + room_data.width - 1 },
123-
Direction::West => BlockPos { x: x, y, z: z + room_data.width - 1 },
124+
Direction::North => ivec3(x, y, z),
125+
Direction::East => ivec3(x + room_data.length - 1, y, z),
126+
Direction::South => ivec3(x + room_data.length - 1, y, z + room_data.width - 1),
127+
Direction::West => ivec3(x, y, z + room_data.width - 1),
124128
_ => unreachable!(),
125129
}
126130
}
127131

128132
pub fn load_into_world(&self, chunk_grid: &mut ChunkGrid) {
129133
let corner = self.get_corner_pos();
130134

131-
for (i, block) in self.data.block_data.iter().enumerate() {
135+
for (index, block) in self.data.block_data.iter().enumerate() {
132136
if *block == Blocks::Air {
133137
continue;
134138
}
@@ -137,13 +141,13 @@ impl Room {
137141
let mut block = block.clone();
138142
block.rotate(self.rotation);
139143

140-
let ind = i as i32;
144+
let index = index as i32;
141145

142-
let x = ind % self.data.width;
143-
let z = (ind / self.data.width) % self.data.length;
144-
let y = self.data.bottom + ind / (self.data.width * self.data.length);
146+
let x = index % self.data.width;
147+
let z = (index / self.data.width) % self.data.length;
148+
let y = self.data.bottom + index / (self.data.width * self.data.length);
145149

146-
let bp = BlockPos { x, y, z }.rotate(self.rotation);
150+
let bp = ivec3(x, y, z).rotate(self.rotation);
147151

148152
chunk_grid.set_block_at(block, corner.x + bp.x, y, corner.z + bp.z);
149153
}
@@ -179,13 +183,12 @@ impl Room {
179183
*old = new;
180184
}
181185

182-
pub fn get_world_block_pos(&self, room_pos: &BlockPos) -> BlockPos {
186+
pub fn get_world_block_position(&self, room_position: IVec3) -> IVec3 {
183187
let corner = self.get_corner_pos();
184-
185-
room_pos.clone()
186-
.rotate(self.rotation)
187-
.add_x(corner.x)
188-
.add_z(corner.z)
188+
let mut position = room_position.rotate(self.rotation);
189+
position.x += corner.x;
190+
position.z += corner.z;
191+
position
189192
}
190193
}
191194

src/main.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,10 @@ use crate::network::packets::packet_buffer::PacketBuffer;
1212
use crate::network::protocol::play::serverbound::EntityInteractionType;
1313
use crate::network::run_network::run_network_thread;
1414
use crate::player::player::Player;
15-
use crate::types::block_position::BlockPos;
1615
use crate::utils::seeded_rng::{seeded_rng, SeededRng};
1716
use crate::world::world::World;
1817
use anyhow::bail;
19-
use glam::DVec3;
18+
use glam::{ivec3, DVec3};
2019
use rand::prelude::IndexedRandom;
2120
use std::collections::HashMap;
2221
use std::time::Duration;
@@ -131,7 +130,9 @@ async fn main() -> anyhow::Result<()> {
131130

132131
let entrance = world.extension.entrance_room();
133132
let entrance = entrance.borrow();
134-
let position = entrance.get_world_block_pos(&BlockPos::new(15, 69, 4)).as_dvec3_centered();
133+
let mut position = entrance.get_world_block_position(ivec3(15, 69, 4)).as_dvec3();
134+
position.x += 0.5;
135+
position.z += 0.5;
135136

136137
let yaw = 0.0.rotate(entrance.rotation);
137138
world.spawn_entity(

src/network/protocol/play/clientbound.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,13 @@ use crate::network::packets::packet_serialize::PacketSerializable;
99
use crate::player::attribute::AttributeMap;
1010
use crate::player::player::GameProfile;
1111
use crate::register_packets;
12-
use crate::types::block_position::BlockPos;
12+
use crate::types::block_position::BlockPosition;
1313
use crate::types::chat_component::ChatComponent;
1414
use crate::types::sized_string::SizedString;
1515
use blocks::packet_serializable;
1616
use bytes::BytesMut;
1717
use enumset::{EnumSet, EnumSetType};
18-
use glam::Vec3;
18+
use glam::{IVec3, Vec3};
1919
use uuid::Uuid;
2020

2121
register_packets! {
@@ -334,14 +334,14 @@ packet_serializable! {
334334

335335
packet_serializable! {
336336
pub struct BlockChange {
337-
pub block_pos: BlockPos,
337+
pub block_pos: IVec3 => &BlockPosition(self.block_pos),
338338
pub block_state: u16 => &VarInt(self.block_state as i32),
339339
}
340340
}
341341

342342
packet_serializable! {
343343
pub struct BlockAction {
344-
pub block_pos: BlockPos,
344+
pub block_pos: IVec3 => &BlockPosition(self.block_pos),
345345
pub event_id: u8,
346346
pub event_data: u8,
347347
pub block_id: u16 => &VarInt((self.block_id & 4095) as i32),

0 commit comments

Comments
 (0)