Skip to content
Draft
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions src/interfaces/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ define_interface!(
StatusResponse,
status_response,
[conn_id: Uuid, version: Version, description: Description]
),
(
TeleportToLastValidPos,
teleport_to_last_valid_pos,
[conn_id: Uuid]
)
);

Expand Down
109 changes: 56 additions & 53 deletions src/services/patchwork.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,59 +50,63 @@ pub fn start<
map_index: 0,
conn_id: None,
});
match &patchwork.maps[anchor.map_index].peer_connection {
Some(_) => match msg.packet {
Packet::Unknown => {}
_ => {
trace!(
"Routing packet from conn_id {:?} through anchor",
msg.conn_id
);
player_state.anchored_move_and_look(
msg.conn_id,
extract_player_position((&msg.packet).clone()),
None,
);
messenger.send_packet(anchor.conn_id.unwrap(), msg.packet.clone());
}
},
None => {
trace!("Routing packet from conn_id {:?} locally", msg.conn_id);
gameplay_router::route_packet(
msg.packet.clone(),
msg.conn_id,
player_state.clone(),
sender.clone(),
);
}
Comment on lines -53 to -77
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why delete this? Won't all non-positional packets get ignored now?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah whoops, I was just trying to avoid handling position packets that are out of bounds so I moved it to after we check the map position but you're right that this will ignore non position packets...

}
if let Some(position) = extract_map_position((&msg.packet).clone()) {
let new_map_index = patchwork_clone.position_map_index(position);
if new_map_index != anchor.map_index {
anchor.disconnect(messenger.clone());
*anchor = match &patchwork.maps[new_map_index].peer_connection {
Some(peer_connection) => Anchor::connect(
peer_connection.peer.clone(),
msg.conn_id,
new_map_index,
patchwork.maps[new_map_index].position.x,
messenger.clone(),
player_state.clone(),
)
.unwrap(),
None => {
gameplay_router::route_packet(
msg.packet.clone(),
msg.conn_id,
player_state.clone(),
sender.clone(),
);
if patchwork.maps[anchor.map_index].peer_connection.is_some() {
player_state.reintroduce(msg.conn_id);
match patchwork_clone.position_map_index(position) {
None => player_state.teleport_to_last_valid_pos(msg.conn_id),
Some(new_map_index) => {
match &patchwork.maps[anchor.map_index].peer_connection {
Some(_) => match msg.packet {
Packet::Unknown => {}
_ => {
trace!(
"Routing packet from conn_id {:?} through anchor",
msg.conn_id
);
player_state.anchored_move_and_look(
msg.conn_id,
extract_player_position((&msg.packet).clone()),
None,
);
messenger.send_packet(anchor.conn_id.unwrap(), msg.packet.clone());
}
},
None => {
trace!("Routing packet from conn_id {:?} locally", msg.conn_id);
gameplay_router::route_packet(
msg.packet.clone(),
msg.conn_id,
player_state.clone(),
sender.clone(),
);
}
Anchor {
conn_id: None,
map_index: new_map_index,
}
if new_map_index != anchor.map_index {
anchor.disconnect(messenger.clone());
*anchor = match &patchwork.maps[new_map_index].peer_connection {
Some(peer_connection) => Anchor::connect(
peer_connection.peer.clone(),
msg.conn_id,
new_map_index,
patchwork.maps[new_map_index].position.x,
messenger.clone(),
player_state.clone(),
)
.unwrap(),
None => {
gameplay_router::route_packet(
msg.packet.clone(),
msg.conn_id,
player_state.clone(),
sender.clone(),
);
if patchwork.maps[anchor.map_index].peer_connection.is_some() {
player_state.reintroduce(msg.conn_id);
}
Anchor {
conn_id: None,
map_index: new_map_index,
}
}
}
}
}
Expand Down Expand Up @@ -215,11 +219,10 @@ impl Patchwork {
.push(Map::new(next_position, self.next_entity_id_block()));
}

pub fn position_map_index(self, position: Position) -> usize {
pub fn position_map_index(self, position: Position) -> Option<usize> {
self.maps
.into_iter()
.position(|map| map.position == position)
.expect("Could not find map for position")
}

pub fn connect_map<M: Messenger + Clone>(
Expand Down
25 changes: 25 additions & 0 deletions src/services/player.rs
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,19 @@ fn handle_message<M: Messenger + Clone, B: BlockState + Clone, PA: PatchworkStat
};
messenger.send_packet(msg.conn_id, Packet::StatusResponse(status_response));
}
Operations::TeleportToLastValidPos(msg) => {
trace!(
"Teleporting to last valid position for conn_id {:?}",
msg.conn_id
);
let player = players
.get(&msg.conn_id)
.expect("Could not teleport to valid position: player not found");
messenger.send_packet(
msg.conn_id,
Packet::ClientboundPlayerPositionAndLook(player.pos_and_look_last_valid_packet())
);
}
}
}

Expand Down Expand Up @@ -247,6 +260,18 @@ impl Player {
}
}

pub fn pos_and_look_last_valid_packet(&self) -> ClientboundPlayerPositionAndLook {
ClientboundPlayerPositionAndLook {
x: self.position.x,
y: self.position.y,
z: self.position.z,
yaw: self.angle.yaw,
pitch: self.angle.pitch,
flags: 0,
teleport_id: 0
}
}

pub fn pos_and_look_packet(&self) -> ClientboundPlayerPositionAndLook {
ClientboundPlayerPositionAndLook {
x: self.position.x,
Expand Down