~starkingdoms/starkingdoms

dbaf2324f5e58d8d9b4f2b10c2606294d655d3f2 — core 4 days ago 1dfcf5e
netcode: entity map important things

# Conflicts:
#	crates/unified/assets/config/world.wc.toml
M crates/unified/src/server/player.rs => crates/unified/src/server/player.rs +15 -3
@@ 10,7 10,9 @@ use crate::server::system_sets::PlayerInputSet;
use crate::prelude::*;
use crate::shared::world_config::WorldConfigResource;
use std::f64::consts::PI;
use bevy_replicon::prelude::{ClientId, FromClient};
use crate::client::components::Me;
use crate::server::ConnectedNetworkEntity;

pub fn player_management_plugin(app: &mut App) {
    app.add_systems(


@@ 209,8 211,9 @@ fn propagate_disconnect(
}

fn dragging(
    mut events: MessageReader<DragRequestEvent>,
    me: Query<Entity, With<Me>>,
    mut events: MessageReader<FromClient<DragRequestEvent>>,
    clients: Query<&ConnectedNetworkEntity>,
    q_ls_me: Query<Entity, With<Me>>,
    mut parts: Query<
        (
            &mut Transform,


@@ 232,11 235,20 @@ fn dragging(
    let Some(world_config) = &world_config.config else {
        return;
    };
    let Ok(player_hearty_entity) = me.single() else { return };

    for event in events.read() {
        debug!(?event, "got drag request event");

        let player_hearty_entity = match event.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()
        };

        let teleport_to_translation;
        let teleport_to_rotation;
        let mut new_linvel = None;

M crates/unified/src/shared/ecs.rs => crates/unified/src/shared/ecs.rs +12 -4
@@ 8,6 8,7 @@ use crate::prelude::*;
use avian2d::prelude::*;
use std::collections::HashMap;
use std::sync::LazyLock;
use bevy::ecs::entity::MapEntities;
use bevy_replicon::prelude::Replicated;

#[derive(States, Debug, Clone, PartialEq, Eq, Hash)]


@@ 42,16 43,23 @@ pub struct Player {
    pub client: Entity,
}

#[derive(Message, Debug, Clone)]
#[derive(Message, Debug, Clone, Serialize, Deserialize, MapEntities)]
pub struct DragRequestEvent {
    #[entities]
    pub drag_target: Entity,
    #[entities]
    pub action: DragAction,
}

#[derive(Debug, Clone)]
#[derive(Debug, Clone, Serialize, Deserialize, MapEntities)]
pub enum DragAction {
    Attach { snap_target: Entity, peer_snap: Entity },
    Free   { position: Vec2, rotation: Quat },
    Attach {
        #[entities]
        snap_target: Entity,
        #[entities]
        peer_snap: Entity
    },
    Free { position: Vec2, rotation: Quat },
}

#[derive(Component, Serialize, Deserialize, Debug)]

M crates/unified/src/shared/net.rs => crates/unified/src/shared/net.rs +5 -1
@@ 8,9 8,10 @@ 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, Drill, Part, Player, PlayerStorage, SingleStorage, Temperature};
use crate::shared::ecs::{CanCraft, DragRequestEvent, Drill, Part, Player, PlayerStorage, SingleStorage, Temperature};
use crate::shared::ecs::clock_sync::{ClientTiming, ServerTiming};
use crate::shared::ecs::thruster::{Thruster, ThrusterOfPart};
use crate::shared::thrust::ThrustSolution;

pub const STARKINGDOMS_PROTOCOL_MAGIC: u64 = 0x5a5a_e37e_4aaa;



@@ 20,6 21,9 @@ pub fn register_replication(app: &mut App) {
        .add_client_message::<ClientTiming>(Channel::Ordered)
        .add_server_message::<ServerTiming>(Channel::Ordered)

        .add_mapped_client_message::<DragRequestEvent>(Channel::Ordered)
        .add_mapped_client_message::<ThrustSolution>(Channel::Ordered)

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


M crates/unified/src/shared/thrust.rs => crates/unified/src/shared/thrust.rs +4 -1
@@ 1,11 1,14 @@
use std::collections::BTreeSet;
use bevy::ecs::entity::MapEntities;
use bevy::prelude::{Component, Entity, Message, Resource};
use serde::{Deserialize, Serialize};

/// A thrust solution, found by the thrust solver on the client.
/// `thrusters_on` is the set of thrusters that should be on.
/// Any thrusters not in this set should be off.
#[derive(Eq, PartialEq, Debug, Clone, Resource, Component, Message)]
#[derive(Eq, PartialEq, Debug, Clone, Resource, Component, Message, Serialize, Deserialize, MapEntities)]
pub struct ThrustSolution {
    #[entities]
    pub thrusters_on: BTreeSet<Entity>,
    pub converged: bool
}

M crates/xtask/src/util.rs => crates/xtask/src/util.rs +36 -0
@@ 18,6 18,13 @@ pub fn cargo(cmd: String) {
    println!("{} cargo {}", "[cargo]".bold().cyan(), cmd);
    let mut output = std::process::Command::new(env!("CARGO"));

    for (key, _) in std::env::vars_os() {
        let Some(key) = key.to_str() else { continue };
        if SANITIZED_ENV_VARS.matches(key) {
            output.env_remove(key);
        }
    }

    for command in cmd.split(" ") {
        output.arg(command);
    }


@@ 29,6 36,35 @@ pub fn cargo(cmd: String) {
    }
}

#[derive(Debug)]
struct SanitizedEnvVars {
    // At the moment we only ban some prefixes, but we may also want to ban env
    // vars by exact name in the future.
    prefixes: &'static [&'static str],
}

impl SanitizedEnvVars {
    const fn new() -> Self {
        // Remove many of the environment variables set in
        // https://doc.rust-lang.org/cargo/reference/environment-variables.html#environment-variables-cargo-sets-for-build-scripts.
        // This is done to avoid recompilation with crates like ring between
        // `cargo clippy` and `cargo xtask clippy`. (This is really a bug in
        // both ring's build script and in Cargo.)
        //
        // The current list is informed by looking at ring's build script, so
        // it's not guaranteed to be exhaustive and it may need to grow over
        // time.
        let prefixes = &["CARGO_PKG_", "CARGO_MANIFEST_", "CARGO_CFG_"];
        Self { prefixes }
    }

    fn matches(&self, key: &str) -> bool {
        self.prefixes.iter().any(|prefix| key.starts_with(prefix))
    }
}

static SANITIZED_ENV_VARS: SanitizedEnvVars = SanitizedEnvVars::new();

pub fn wasmopt(cmd: String) {
    println!("{} wasm-opt {}", "[wasm-opt]".bold().cyan(), cmd);
    let mut output = std::process::Command::new("wasm-opt");