Skip to content

Commit c86fd2c

Browse files
committed
Refactored dungeon room storage to not use rc<refcell<T>>
1 parent 2cb9bef commit c86fd2c

File tree

9 files changed

+329
-125
lines changed

9 files changed

+329
-125
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,4 +24,4 @@ opt-level = 3
2424

2525
[profile.release]
2626
codegen-units = 1
27-
lto = "thin"
27+
lto = "thin"

src/dungeon/crushers.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use crate::server::utils::direction::Direction;
55
use crate::server::world::World;
66
use serde_json::Value;
77

8+
#[derive(Debug)]
89
pub struct Crusher {
910
pub block_pos: BlockPos,
1011
pub direction: Direction,

src/dungeon/dungeon.rs

Lines changed: 268 additions & 81 deletions
Large diffs are not rendered by default.

src/dungeon/map.rs

Lines changed: 30 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
use crate::dungeon::door::DoorType;
2+
use crate::dungeon::dungeon::Dungeon;
23
use crate::dungeon::room::room::Room;
34
use crate::dungeon::room::room_data::{RoomData, RoomShape, RoomType::*};
45
use crate::server::block::block_parameter::Axis;
@@ -121,60 +122,63 @@ impl DungeonMap {
121122
}
122123
}
123124
}
124-
125-
pub fn draw_room(&mut self, room: &Room) {
126-
125+
126+
pub fn draw_room(dungeon: &mut Dungeon, room: usize) {
127+
let room = &dungeon.rooms[room];
128+
let map = &mut dungeon.map;
127129
let color = get_room_color(&room.room_data);
128-
130+
129131
for segment in room.segments.iter() {
130132
let x = segment.x * 20;
131133
let y = segment.z * 20;
132-
133-
self.fill_px(x, y, 16, 16, color);
134-
134+
135+
map.fill_px(x, y, 16, 16, color);
135136
if room.segments.iter().find(|seg| seg.x == segment.x + 1 && seg.z == segment.z).is_some() {
136-
self.fill_px(x + 16, y, 4, 16, color);
137+
map.fill_px(x + 16, y, 4, 16, color);
137138
}
138139
if room.segments.iter().find(|seg| seg.x == segment.x && seg.z == segment.z + 1).is_some() {
139-
self.fill_px(x, y + 16, 16, 4, color);
140+
map.fill_px(x, y + 16, 16, 4, color);
140141
}
141-
142+
142143
for (index, neighbour) in segment.neighbours.iter().enumerate() {
143144
if neighbour.is_none() {
144145
continue;
145146
}
146-
let (neighbour, door) = {
147+
148+
let (neighbour_room, door) = {
147149
let neighbour = neighbour.as_ref().unwrap();
148-
(neighbour.room.borrow(), neighbour.door.borrow())
150+
let room = dungeon.rooms.get(neighbour.room_index).unwrap();
151+
let door = dungeon.doors.get(neighbour.door_index).unwrap();
152+
(room, door)
149153
};
150-
154+
151155
let mut x = segment.x * 20 + 6;
152156
let mut y = segment.z * 20 + 6;
153-
157+
154158
match index {
155159
0 => y -= 10,
156160
1 => x += 10,
157161
2 => y += 10,
158162
3 => x -= 10,
159163
_ => unreachable!()
160164
}
161-
165+
162166
let (width, height) = match door.direction {
163167
Axis::X => (4, 5),
164168
Axis::Z => (5, 4),
165169
_ => unreachable!()
166170
};
167-
168-
if neighbour.entered {
169-
let color = get_door_color(room, &*neighbour);
170-
self.fill_px(x, y, width, height, color);
171+
172+
if neighbour_room.entered {
173+
let color = get_door_color(room, &*neighbour_room);
174+
map.fill_px(x, y, width, height, color);
171175
} else {
172176
let color = match door.door_type {
173177
DoorType::WITHER => BLACK,
174178
DoorType::BLOOD => RED,
175179
_ => GRAY,
176180
};
177-
self.fill_px(x, y, width, height, color);
181+
map.fill_px(x, y, width, height, color);
178182

179183
let mut x = segment.x * 20;
180184
let mut y = segment.z * 20;
@@ -187,28 +191,28 @@ impl DungeonMap {
187191
_ => unreachable!()
188192
}
189193

190-
self.fill_px(x ,y, 16, 16, GRAY);
194+
map.fill_px(x ,y, 16, 16, GRAY);
191195

192196
for (qx, qy) in QUESTION_MARK_POSITIONS {
193-
self.set_px(x + qx + 5, y + qy + 5, BLACK);
197+
map.set_px(x + qx + 5, y + qy + 5, BLACK);
194198
}
195199
}
196-
}
200+
}
197201
}
198-
202+
199203
// fill in hole
200204
if room.room_data.shape == RoomShape::TwoByTwo {
201205
let x = room.segments[0].x * 20 + 16;
202206
let y = room.segments[0].z * 20 + 16;
203-
self.fill_px(x, y, 4, 4, color)
207+
map.fill_px(x, y, 4, 4, color)
204208
}
205209

206210
{
207211
let x = room.segments[0].x * 20 + 4;
208212
let y = room.segments[0].z * 20 + 4;
209213

210214
for (cx, cy) in CHECKMARK_POSITIONS {
211-
self.set_px(x + cx, y + cy, GREEN)
215+
map.set_px(x + cx, y + cy, GREEN)
212216
}
213217
}
214218
}

src/dungeon/room/room.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,21 +7,25 @@ use crate::server::block::blocks::Blocks;
77
use crate::server::block::rotatable::Rotatable;
88
use crate::server::utils::direction::Direction;
99
use crate::server::world::World;
10-
use std::cell::RefCell;
1110
use std::collections::HashSet;
12-
use std::rc::Rc;
1311

12+
#[derive(Debug)]
1413
pub struct RoomSegment {
1514
pub x: usize,
1615
pub z: usize,
1716
pub neighbours: [Option<RoomNeighbour>; 4]
1817
}
1918

19+
#[derive(Debug)]
2020
pub struct RoomNeighbour {
21-
pub door: Rc<RefCell<Door>>,
22-
pub room: Rc<RefCell<Room>>,
21+
// pub door: Rc<RefCell<Door>>,
22+
// pub room: Rc<RefCell<Room>>,
23+
24+
pub door_index: usize,
25+
pub room_index: usize,
2326
}
2427

28+
#[derive(Debug)]
2529
pub struct Room {
2630
pub segments: Vec<RoomSegment>,
2731
pub room_data: RoomData,
@@ -37,7 +41,7 @@ impl Room {
3741

3842
pub fn new(
3943
mut segments: Vec<RoomSegment>,
40-
dungeon_doors: &Vec<Door>,
44+
dungeon_doors: &[Door],
4145
room_data: RoomData
4246
) -> Room {
4347
// Sort room segments by z and then x
@@ -87,7 +91,7 @@ impl Room {
8791
Room::get_corner_pos_from(&self.segments, &self.rotation, &self.room_data)
8892
}
8993

90-
pub fn get_corner_pos_from(segments: &Vec<RoomSegment>, rotation: &Direction, room_data: &RoomData) -> BlockPos {
94+
pub fn get_corner_pos_from(segments: &[RoomSegment], rotation: &Direction, room_data: &RoomData) -> BlockPos {
9195
let min_x = segments.iter().min_by(|a, b| a.x.cmp(&b.x)).unwrap().x;
9296
let min_z = segments.iter().min_by(|a, b| a.z.cmp(&b.z)).unwrap().z;
9397

@@ -108,7 +112,7 @@ impl Room {
108112
self.tick_amount += 1;
109113
}
110114

111-
pub fn get_1x1_shape_and_type(segments: &Vec<RoomSegment>, dungeon_doors: &Vec<Door>) -> (RoomShape, Direction) {
115+
pub fn get_1x1_shape_and_type(segments: &[RoomSegment], dungeon_doors: &[Door]) -> (RoomShape, Direction) {
112116
let center_x = segments[0].x as i32 * 32 + 15 + DUNGEON_ORIGIN.0;
113117
let center_z = segments[0].z as i32 * 32 + 15 + DUNGEON_ORIGIN.1;
114118

@@ -158,7 +162,7 @@ impl Room {
158162
}
159163
}
160164

161-
pub fn get_rotation_from_segments(segments: &Vec<RoomSegment>, dungeon_doors: &Vec<Door>) -> Direction {
165+
pub fn get_rotation_from_segments(segments: &Vec<RoomSegment>, dungeon_doors: &[Door]) -> Direction {
162166
let unique_x = segments.iter()
163167
.map(|segment| segment.x)
164168
.collect::<HashSet<usize>>();

src/dungeon/room/room_data.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ impl RoomShape {
4242
}
4343
}
4444

45-
pub fn from_segments(segments: &Vec<RoomSegment>, dungeon_doors: &Vec<Door>) -> RoomShape {
45+
pub fn from_segments(segments: &[RoomSegment], dungeon_doors: &[Door]) -> RoomShape {
4646

4747
let unique_x = segments.iter()
4848
.map(|segment| segment.x)

src/main.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ async fn main() -> Result<()> {
130130
println!("Rng Seed: {}", rng_seed);
131131
SeededRng::set_seed(rng_seed);
132132

133-
let dungeon = Dungeon::from_string(dungeon_str, &room_data_storage)?;
133+
let dungeon = Dungeon::from_str(dungeon_str, &room_data_storage)?;
134134
let mut server = Server::initialize_with_dungeon(network_tx, dungeon);
135135
server.world.server = &mut server;
136136
server.dungeon.server = &mut server;
@@ -145,7 +145,6 @@ async fn main() -> Result<()> {
145145
let dungeon = &server.dungeon;
146146

147147
for room in &dungeon.rooms {
148-
let room = room.borrow();
149148
// println!("Room: {:?} type={:?} rotation={:?} shape={:?} corner={:?}", room.segments, room.room_data.room_type, room.rotation, room.room_data.shape, room.get_corner_pos());
150149
room.load_into_world(&mut server.world);
151150

@@ -192,7 +191,7 @@ async fn main() -> Result<()> {
192191
}
193192

194193
for door in &dungeon.doors {
195-
door.borrow().load_into_world(&mut server.world, &door_type_blocks);
194+
door.load_into_world(&mut server.world, &door_type_blocks);
196195
}
197196

198197
// let zombie_spawn_pos = DVec3 {
@@ -356,8 +355,8 @@ async fn main() -> Result<()> {
356355
format!("{} {}{}", SKYBLOCK_MONTHS[month], day_of_month, suffix)
357356
};
358357

359-
let room_id = if let Some(room) = player.server_mut().dungeon.get_player_room(player) {
360-
&room.borrow().room_data.id
358+
let room_id = if let Some(room) = server.dungeon.get_player_room(player) {
359+
&server.dungeon.rooms[room].room_data.id
361360
} else {
362361
""
363362
};

src/server/block/block_interact_action.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ impl BlockInteractAction {
3939

4040
if let DungeonState::Started { .. } = dungeon.state {
4141
// todo check if player has a key
42-
let door = &dungeon.doors[*id].borrow();
42+
let door = &dungeon.doors[*id];
4343
door.open_door(world);
4444
}
4545
}

src/server/server.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
use crate::dungeon::dungeon::Dungeon;
22
use crate::net::internal_packets::{MainThreadMessage, NetworkThreadMessage};
33
use crate::net::packets::packet::ProcessPacket;
4-
use crate::net::protocol::play::clientbound::{AddEffect, EntityProperties, JoinGame, PlayerAbilities, PlayerListHeaderFooter, PositionLook};
4+
use crate::net::packets::packet_serialize::PacketSerializable;
5+
use crate::net::protocol::play::clientbound::{AddEffect, CustomPayload, EntityProperties, JoinGame, PlayerAbilities, PlayerListHeaderFooter, PositionLook};
56
use crate::net::var_int::VarInt;
67
use crate::server::items::Item;
78
use crate::server::player::attribute::{Attribute, AttributeMap, AttributeModifier};
@@ -103,7 +104,7 @@ impl Server {
103104

104105
player.sidebar.write_init_packets(&mut player.packet_buffer);
105106

106-
// player.send_packet(self.world.player_info.new_packet())?;
107+
// player.write_packet(&self.world.player_info.new_packet());
107108

108109
player.write_packet(&PlayerListHeaderFooter {
109110
header: header(),
@@ -172,6 +173,14 @@ impl Server {
172173
walk_speed: playerspeed,
173174
});
174175

176+
// let mut buf = Vec::new();
177+
// "hypixel".write(&mut buf);
178+
179+
// player.write_packet(&CustomPayload {
180+
// channel: "MC|Brand".into(),
181+
// data: &buf,
182+
// });
183+
175184
player.flush_packets();
176185

177186
self.world.players.insert(client_id, player);

0 commit comments

Comments
 (0)