~starkingdoms/starkingdoms

de4eacb0ed5175cd2b0c7cd0fc118385f757dd13 — ghostly_zsh 28 days ago d733c4e
feat: rewrite detachment
M crates/unified/src/server/player.rs => crates/unified/src/server/player.rs +47 -42
@@ 23,45 23,49 @@ pub fn player_management_plugin(app: &mut App) {
}

fn disconnect_part(
    entity: Entity,
    recursed_entity: Entity,
    joints: &Joints,
    q_joints: Query<&Joints>,
    q_peer: Query<(Entity, &Peer, &JointOf)>,
    (entity, joints): (Entity, &Joints),
    q_joints: &Query<(&Joint, &JointOf, &Transform, Option<&Peer>, Entity)>,
    q_only_joints: &Query<&Joints>,
    processed_peers: &mut Vec<Entity>,
    mut commands: Commands,
) {
    trace!(?entity, ?joints, ?processed_peers, "recursive disconnect");
    // recursive disconnect part
    for joint in &**joints {
        let Ok((p_e, our_peer_object, _)) = q_peer.get(*joint) else {
    //trace!(?entity, ?joints, ?processed_peers, "recursive disconnect");
    // loop the joints in the entity
    for joint in joints.iter() {
        // now we have the entity "joint"
        let Ok((_, _, _, peer, _)) = q_joints.get(joint) else {
            continue;
        };
        if processed_peers.contains(&p_e) { continue }
        let other_joint = our_peer_object.peer_joint_entity_id;

        commands.entity(*joint).remove::<Peer>();
        commands.entity(our_peer_object.physics_joint).despawn();
        processed_peers.push(p_e);
        // don't want to double process a joint
        if processed_peers.contains(&joint) { continue; }

        let Ok((p_e_2, _, other_joint_of)) = q_peer.get(other_joint) else {
            continue;
        };
        // we have the peer that holds the physics joint
        let Some(peer) = peer else { continue; };
        commands.entity(joint).remove::<Peer>();
        processed_peers.push(joint);

        commands.entity(other_joint).remove::<Peer>();
        processed_peers.push(p_e_2);
        let mut physics_joint = commands.entity(peer.physics_joint);
        // the physics joint shouldnt exist anymore
        physics_joint.despawn();

        let Ok(joints) = q_joints.get(other_joint_of.0) else {
        // delete the other side's peer that exists for some reason
        let Ok((_, other_joint_of, _, other_peer, _)) = q_joints.get(peer.peer_joint_entity_id) else {
            continue;
        };
        if other_joint != recursed_entity {
            disconnect_part(other_joint, entity, joints, q_joints, q_peer, processed_peers, commands.reborrow());
        let Some(other_peer) = other_peer else { continue; };
        commands.entity(peer.peer_joint_entity_id).remove::<Peer>();
        processed_peers.push(peer.peer_joint_entity_id);
        let Ok(other_joints) = q_only_joints.get(other_joint_of.0) else {
            continue
        };

        if other_joint_of.0 != entity {
            disconnect_part((peer.peer_joint_entity_id, joints), q_joints,
                q_only_joints, processed_peers, commands.reborrow());
        }

    }
    for ppeer in processed_peers {
        commands.entity(*ppeer).remove::<Peer>();
        commands.entity(*ppeer).remove::<PartInShip>();
    }
    // recursive disconnect part
    commands.entity(entity).remove::<PartInShip>();
}



@@ 75,8 79,9 @@ fn dragging(
            &mut LinearVelocity,
            &Joints,
            &mut AngularVelocity,
            &Part,
        ),
        (With<Part>, Without<Joint>),
        (Without<Joint>),
    >,
    snaps: Query<(&SnapOf, &SnapOfJoint)>,
    joints: Query<(&Joint, &JointOf, &Transform, Option<&Peer>, Entity)>,


@@ 154,6 159,7 @@ fn dragging(

            // great, the attachment appears to be valid
            // let's make sure this player is allowed to drag onto this part
            // getting attached to (hearty)
            let target_part = {
                let Ok(target_part) = parts.get(target_joint.1.0) else {
                    continue;


@@ 161,6 167,7 @@ fn dragging(
                target_part
            };

            // attached (housing)
            let source_part = {
                let Ok(source_part) = parts.get(source_joint.1.0) else {
                    continue;


@@ 180,14 187,12 @@ fn dragging(
            // great, we have a valid peering request

            let did_disconnect = q_joint.get(source_part.2).is_ok();
            let mut dc_queue = vec![];
            let mut processed = vec![source_joint.4];
            disconnect_part(
                source_part.2,
                Entity::PLACEHOLDER,
                source_part.4,
                q_joints,
                peer,
                &mut dc_queue,
                (source_part.2, source_part.4),
                &joints,
                &q_joints,
                &mut processed,
                commands.reborrow(),
            );



@@ 239,14 244,12 @@ fn dragging(
            );
            warn!("dragging already attached entities may cause inconsistent behavior!!");
            let source_part = parts.get(event.drag_target).unwrap();
            let mut dc_queue = vec![];
            let mut processed = vec![];
            disconnect_part(
                source_part.2,
                Entity::PLACEHOLDER,
                source_part.4,
                q_joints,
                peer,
                &mut dc_queue,
                (source_part.2, source_part.4),
                &joints,
                &q_joints,
                &mut processed,
                commands.reborrow(),
            );
            teleport_to_translation = event.drag_to;


@@ 254,8 257,10 @@ fn dragging(
        }

        let mut part = parts.get_mut(event.drag_target).unwrap();
        debug!("{:?} {:?}", part.0.translation, teleport_to_translation);
        part.0.translation.x = teleport_to_translation.x;
        part.0.translation.y = teleport_to_translation.y;
        debug!("{:?} {:?}", part.0.translation, teleport_to_translation);
        part.0.rotation = teleport_to_rotation; // client calculates this; no reason to recalculate
        if let Some(new_vel) = new_linvel {
            *part.3 = new_vel;

M crates/unified/src/server_plugins.rs => crates/unified/src/server_plugins.rs +6 -0
@@ 3,6 3,7 @@ use crate::config::planet::PlanetConfigCollection;
use crate::config::world::GlobalWorldConfig;
use aeronet_replicon::server::AeronetRepliconServerPlugin;
use aeronet_websocket::server::WebSocketServerPlugin;
use avian2d::prelude::PhysicsInterpolationPlugin;
use bevy::app::{PluginGroup, PluginGroupBuilder, ScheduleRunnerPlugin, TaskPoolPlugin};
use bevy::asset::AssetPlugin;
use bevy::diagnostic::FrameCountPlugin;


@@ 22,6 23,11 @@ pub struct ServerPluginGroup {
impl PluginGroup for ServerPluginGroup {
    fn build(self) -> PluginGroupBuilder {
        PluginGroupBuilder::start::<Self>()
            .add_group(
                PhysicsPlugins::default()
                    .with_length_unit(100.0)
                    .set(PhysicsInterpolationPlugin::interpolate_all())
            )
            .add(StatesPlugin)
            .add(TaskPoolPlugin::default())
            .add(FrameCountPlugin)

M crates/unified/src/shared_plugins.rs => crates/unified/src/shared_plugins.rs +1 -6
@@ 14,11 14,6 @@ impl PluginGroup for SharedPluginGroup {
    fn build(self) -> PluginGroupBuilder {
        PluginGroupBuilder::start::<Self>()
            //.add(RapierPhysicsPlugin::<NoUserData>::pixels_per_meter(100.0))
            .add_group(
                PhysicsPlugins::default()
                    .with_length_unit(100.0)
                    .set(PhysicsInterpolationPlugin::interpolate_all())
            )
            .add(physics_setup_plugin)
            .add(register_everything)
            .add(register_physics_components_for_replication)


@@ 69,4 64,4 @@ fn setup_physics(
    let mut params = ctx.integration_parameters;
    params.num_internal_stabilization_iterations = 16;
}
*/
\ No newline at end of file
*/