From f0d9056a1651cd787c7072195e17597cffa022ca Mon Sep 17 00:00:00 2001 From: core Date: Fri, 12 Jun 2026 23:11:25 -0400 Subject: [PATCH] netcode: fix attachment, enable all events again --- crates/unified/src/server/craft.rs | 4 ++-- crates/unified/src/server/drill.rs | 4 +++- crates/unified/src/server/player.rs | 14 ++++++++++---- crates/unified/src/server/player/thrust.rs | 19 +++++++++++++++---- crates/unified/src/shared/ecs.rs | 6 ++++-- crates/unified/src/shared/net.rs | 4 +++- 6 files changed, 37 insertions(+), 14 deletions(-) diff --git a/crates/unified/src/server/craft.rs b/crates/unified/src/server/craft.rs index a11afa4702c08f4b44637bf7e2302206977ecbea..4f09339a5abd0aa8d450affb27e02c300c5d2bf7 100644 --- a/crates/unified/src/server/craft.rs +++ b/crates/unified/src/server/craft.rs @@ -1,5 +1,5 @@ use std::collections::HashMap; - +use bevy_replicon::prelude::FromClient; use crate::{prelude::*, server::part::{SpawnPartBundle, SpawnPartRequest}}; use crate::shared::attachment::{PartInShip, Parts}; use crate::shared::ecs::{CraftPartRequest, Part, Player, SingleStorage, VariableStorage}; @@ -11,7 +11,7 @@ pub fn craft_plugin(app: &mut App) { } fn receive_crafting_request( - mut craft_part_request: MessageReader, + mut craft_part_request: MessageReader>, part_query: Query<(&Transform, &LinearVelocity, &Part, &PartInShip)>, player_query: Query<(Entity, &Transform, &LinearVelocity, &Part), With>, parts_query: Query<&Parts>, diff --git a/crates/unified/src/server/drill.rs b/crates/unified/src/server/drill.rs index cd7689cf30182f00875296e218448a30038f6fce..3791cdde9beedc801ceffaf2a8e021f2ded90a12 100644 --- a/crates/unified/src/server/drill.rs +++ b/crates/unified/src/server/drill.rs @@ -1,3 +1,4 @@ +use bevy_replicon::prelude::FromClient; use crate::server::components::PlanetSensor; use crate::prelude::*; use crate::shared::attachment::{PartInShip, Parts}; @@ -9,7 +10,7 @@ pub fn drill_plugin(app: &mut App) { } fn toggle_drill( - mut toggle_drill_reader: MessageReader, + mut toggle_drill_reader: MessageReader>, mut drills: Query<&mut Drill>, ) { for toggle_drill_event in toggle_drill_reader.read() { @@ -17,6 +18,7 @@ fn toggle_drill( // the entity is a drill let Ok(mut drill) = drills.get_mut(toggle_drill_event.drill_entity) else { return }; drill.drilling = !drill.drilling; + // TODO: does this client have permission to use this drill } } diff --git a/crates/unified/src/server/player.rs b/crates/unified/src/server/player.rs index d99519572de8cb6d50584210db46c06164cef018..5bc23b29c07f851b3107e2c3250f2cf9685f7b26 100644 --- a/crates/unified/src/server/player.rs +++ b/crates/unified/src/server/player.rs @@ -223,6 +223,8 @@ fn dragging( &Joints, &mut AngularVelocity, &Part, + &mut Position, + &mut Rotation ), Without, >, @@ -291,12 +293,12 @@ fn dragging( } // getting attached to (hearty) - let Ok((target_xform, target_in_ship, target_entity, target_linvel, _, target_angvel, _)) = parts.get(target_jt_of.0) else { + let Ok((target_xform, target_in_ship, target_entity, target_linvel, _, target_angvel, _, _, _)) = parts.get(target_jt_of.0) else { continue; }; // attached (housing) - let Ok((_, _, source_entity, _, source_joints, _, _)) = parts.get(source_jt_of.0) else { + let Ok((_, _, source_entity, _, source_joints, _, _, _, _)) = parts.get(source_jt_of.0) else { continue; }; @@ -329,6 +331,7 @@ fn dragging( }; let joint_id = commands.spawn((fixed_joint, joint_damping)).id(); + let joint_id = Entity::PLACEHOLDER; // create the peering components commands.entity(source_jt_id).insert(Peer { peer_joint_entity_id: target_jt_id, @@ -358,7 +361,7 @@ fn dragging( DragAction::Free { position, rotation } => { warn!("blindly accepting non-attachment request, someone should change this eventually"); warn!("dragging already attached entities may cause inconsistent behavior!!"); - let Ok((_, _, free_entity, _, free_joints, _, _)) = parts.get(event.drag_target) else { + let Ok((_, _, free_entity, _, free_joints, _, _, _, _)) = parts.get(event.drag_target) else { continue; }; let mut processed = vec![]; @@ -374,12 +377,15 @@ fn dragging( } } - let Ok((mut xform, _, _, mut linvel, _, mut angvel, _)) = parts.get_mut(event.drag_target) else { + let Ok((mut xform, _, _, mut linvel, _, mut angvel, _, mut position, mut rotation)) = parts.get_mut(event.drag_target) else { continue; }; xform.translation.x = teleport_to_translation.x; + position.x = teleport_to_translation.x as f64; xform.translation.y = teleport_to_translation.y; + position.y = teleport_to_translation.y as f64; xform.rotation = teleport_to_rotation; // client calculates this; no reason to recalculate + *rotation = Rotation::radians(teleport_to_rotation.to_euler(EulerRot::XYZ).2.into()); if let Some(vel) = new_linvel { *linvel = vel; } diff --git a/crates/unified/src/server/player/thrust.rs b/crates/unified/src/server/player/thrust.rs index 20724da5c122a3230f097c8ead7d05d602ab9b4c..4db269d82cd1bb90a982276b72834aa4a026bc1d 100644 --- a/crates/unified/src/server/player/thrust.rs +++ b/crates/unified/src/server/player/thrust.rs @@ -2,10 +2,12 @@ //! The thrust solver runs on the client and sends a `ThrustSolution` message; //! this file receives it and applies it to the physics simulation. +use bevy_replicon::prelude::{ClientId, FromClient}; use crate::client::components::Me; use crate::shared::ecs::{Part, Temperature}; use crate::shared::ecs::thruster::{Thruster, ThrusterOfPart}; use crate::prelude::*; +use crate::server::ConnectedNetworkEntity; use crate::shared::attachment::Parts; use crate::shared::thrust::ThrustSolution; @@ -16,13 +18,22 @@ pub fn server_thrust_plugin(app: &mut App) { } fn process_thrust_events( - mut events: MessageReader, - me_entity: Query>, + mut events: MessageReader>, + q_ls_me: Query>, + clients: Query<&ConnectedNetworkEntity>, mut commands: Commands, ) { for thrust_solution in events.read() { - let Ok(player_entity) = me_entity.single() else { return }; - commands.entity(player_entity).insert(thrust_solution.clone()); + let player_entity = match thrust_solution.client_id { + ClientId::Client(client_entity) => { + let ConnectedNetworkEntity { + game_entity: player_hearty_entity, + } = clients.get(client_entity).unwrap(); + *player_hearty_entity + }, + ClientId::Server => q_ls_me.iter().next().unwrap() + }; + commands.entity(player_entity).insert(thrust_solution.message.clone()); } } diff --git a/crates/unified/src/shared/ecs.rs b/crates/unified/src/shared/ecs.rs index 7df42fa0952d85f9bba9a837f5ccd079de80b494..fd17e9f09789877117f9fb84142e8c305ff84e40 100644 --- a/crates/unified/src/shared/ecs.rs +++ b/crates/unified/src/shared/ecs.rs @@ -72,8 +72,9 @@ pub struct PlayerStorage { #[derive(Component, Serialize, Deserialize, Debug)] pub struct CanCraft; -#[derive(Message, Debug, Clone)] +#[derive(Message, Debug, Clone, Serialize, Deserialize, MapEntities)] pub struct CraftPartRequest { + #[entities] pub crafting_part: Entity, pub crafted_part: String, pub inputs: HashMap, @@ -98,8 +99,9 @@ pub struct Drill { pub resource_multiplier: f32, pub on_planet: Option, } -#[derive(Message, Debug, Clone)] +#[derive(Message, Debug, Clone, Serialize, Deserialize, MapEntities)] pub struct ToggleDrillEvent { + #[entities] pub drill_entity: Entity, } diff --git a/crates/unified/src/shared/net.rs b/crates/unified/src/shared/net.rs index 8591a02b99861f860716c419bb344d3d051275b7..866f20dde495e139cc3f15ac0a833ae21a35432a 100644 --- a/crates/unified/src/shared/net.rs +++ b/crates/unified/src/shared/net.rs @@ -8,7 +8,7 @@ use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddr}; use url::{Host, Url}; use crate::shared::attachment::{Joint, JointOf, PartInShip, Peer, Ship, SnapOf, SnapOfJoint}; use crate::shared::config::planet::{Planet, PlanetSpring, PlanetSpringJoint}; -use crate::shared::ecs::{CanCraft, DragRequestEvent, Drill, Part, Player, PlayerStorage, SingleStorage, Temperature}; +use crate::shared::ecs::{CanCraft, CraftPartRequest, DragRequestEvent, Drill, Part, Player, PlayerStorage, SingleStorage, Temperature, ToggleDrillEvent}; use crate::shared::ecs::clock_sync::{ClientTiming, ServerTiming}; use crate::shared::ecs::thruster::{Thruster, ThrusterOfPart}; use crate::shared::thrust::ThrustSolution; @@ -23,6 +23,8 @@ pub fn register_replication(app: &mut App) { .add_mapped_client_message::(Channel::Ordered) .add_mapped_client_message::(Channel::Ordered) + .add_mapped_client_message::(Channel::Ordered) + .add_mapped_client_message::(Channel::Ordered) .replicate_once::() .replicate_once::()