~starkingdoms/starkingdoms

f0d9056a1651cd787c7072195e17597cffa022ca — core 4 days ago dbaf232
netcode: fix attachment, enable all events again
M crates/unified/src/server/craft.rs => crates/unified/src/server/craft.rs +2 -2
@@ 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<CraftPartRequest>,
    mut craft_part_request: MessageReader<FromClient<CraftPartRequest>>,
    part_query: Query<(&Transform, &LinearVelocity, &Part, &PartInShip)>,
    player_query: Query<(Entity, &Transform, &LinearVelocity, &Part), With<Player>>,
    parts_query: Query<&Parts>,

M crates/unified/src/server/drill.rs => crates/unified/src/server/drill.rs +3 -1
@@ 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<ToggleDrillEvent>,
    mut toggle_drill_reader: MessageReader<FromClient<ToggleDrillEvent>>,
    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
    }
}


M crates/unified/src/server/player.rs => crates/unified/src/server/player.rs +10 -4
@@ 223,6 223,8 @@ fn dragging(
            &Joints,
            &mut AngularVelocity,
            &Part,
            &mut Position,
            &mut Rotation
        ),
        Without<Joint>,
    >,


@@ 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;
        }

M crates/unified/src/server/player/thrust.rs => crates/unified/src/server/player/thrust.rs +15 -4
@@ 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<ThrustSolution>,
    me_entity: Query<Entity, With<Me>>,
    mut events: MessageReader<FromClient<ThrustSolution>>,
    q_ls_me: Query<Entity, With<Me>>,
    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());
    }
}


M crates/unified/src/shared/ecs.rs => crates/unified/src/shared/ecs.rs +4 -2
@@ 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<String, u32>,


@@ 98,8 99,9 @@ pub struct Drill {
    pub resource_multiplier: f32,
    pub on_planet: Option<String>,
}
#[derive(Message, Debug, Clone)]
#[derive(Message, Debug, Clone, Serialize, Deserialize, MapEntities)]
pub struct ToggleDrillEvent {
    #[entities]
    pub drill_entity: Entity,
}


M crates/unified/src/shared/net.rs => crates/unified/src/shared/net.rs +3 -1
@@ 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::<DragRequestEvent>(Channel::Ordered)
        .add_mapped_client_message::<ThrustSolution>(Channel::Ordered)
        .add_mapped_client_message::<ToggleDrillEvent>(Channel::Ordered)
        .add_mapped_client_message::<CraftPartRequest>(Channel::Ordered)

        .replicate_once::<Transform>()
        .replicate_once::<GlobalTransform>()