From 5e9a35a7c9c5162e3e9ef638ca361b92151285ec Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Sat, 13 Mar 2021 00:26:50 +0000 Subject: [PATCH] Sync controllers with clients --- common/net/src/msg/ecs_packet.rs | 5 ++++ common/src/comp/controller.rs | 4 +-- common/sys/src/phys.rs | 2 +- server/src/sys/agent.rs | 45 +++++++++++++++++++------------- server/src/sys/sentinel.rs | 15 +++++++++-- 5 files changed, 48 insertions(+), 23 deletions(-) diff --git a/common/net/src/msg/ecs_packet.rs b/common/net/src/msg/ecs_packet.rs index 1ee77e127d..c539992aa1 100644 --- a/common/net/src/msg/ecs_packet.rs +++ b/common/net/src/msg/ecs_packet.rs @@ -32,6 +32,7 @@ sum_type! { Gravity(comp::Gravity), Sticky(comp::Sticky), CharacterState(comp::CharacterState), + Controller(comp::Controller), Pos(comp::Pos), Vel(comp::Vel), Ori(comp::Ori), @@ -66,6 +67,7 @@ sum_type! { Gravity(PhantomData), Sticky(PhantomData), CharacterState(PhantomData), + Controller(PhantomData), Pos(PhantomData), Vel(PhantomData), Ori(PhantomData), @@ -100,6 +102,7 @@ impl sync::CompPacket for EcsCompPacket { EcsCompPacket::Gravity(comp) => sync::handle_insert(comp, entity, world), EcsCompPacket::Sticky(comp) => sync::handle_insert(comp, entity, world), EcsCompPacket::CharacterState(comp) => sync::handle_insert(comp, entity, world), + EcsCompPacket::Controller(comp) => sync::handle_insert(comp, entity, world), EcsCompPacket::Pos(comp) => sync::handle_insert(comp, entity, world), EcsCompPacket::Vel(comp) => sync::handle_insert(comp, entity, world), EcsCompPacket::Ori(comp) => sync::handle_insert(comp, entity, world), @@ -132,6 +135,7 @@ impl sync::CompPacket for EcsCompPacket { EcsCompPacket::Gravity(comp) => sync::handle_modify(comp, entity, world), EcsCompPacket::Sticky(comp) => sync::handle_modify(comp, entity, world), EcsCompPacket::CharacterState(comp) => sync::handle_modify(comp, entity, world), + EcsCompPacket::Controller(comp) => sync::handle_modify(comp, entity, world), EcsCompPacket::Pos(comp) => sync::handle_modify(comp, entity, world), EcsCompPacket::Vel(comp) => sync::handle_modify(comp, entity, world), EcsCompPacket::Ori(comp) => sync::handle_modify(comp, entity, world), @@ -168,6 +172,7 @@ impl sync::CompPacket for EcsCompPacket { EcsCompPhantom::CharacterState(_) => { sync::handle_remove::(entity, world) }, + EcsCompPhantom::Controller(_) => sync::handle_remove::(entity, world), EcsCompPhantom::Pos(_) => sync::handle_remove::(entity, world), EcsCompPhantom::Vel(_) => sync::handle_remove::(entity, world), EcsCompPhantom::Ori(_) => sync::handle_remove::(entity, world), diff --git a/common/src/comp/controller.rs b/common/src/comp/controller.rs index 000c13de57..05b82e3a3f 100644 --- a/common/src/comp/controller.rs +++ b/common/src/comp/controller.rs @@ -9,7 +9,7 @@ use crate::{ util::Dir, }; use serde::{Deserialize, Serialize}; -use specs::{Component, DerefFlaggedStorage}; +use specs::{Component, DerefFlaggedStorage, FlaggedStorage}; use specs_idvs::IdvStorage; use std::time::Duration; use vek::*; @@ -314,7 +314,7 @@ impl Controller { } impl Component for Controller { - type Storage = IdvStorage; + type Storage = FlaggedStorage>; } #[derive(Clone, Debug, PartialEq, Serialize, Deserialize)] diff --git a/common/sys/src/phys.rs b/common/sys/src/phys.rs index 62063f3e41..f169e91230 100644 --- a/common/sys/src/phys.rs +++ b/common/sys/src/phys.rs @@ -1004,7 +1004,7 @@ fn cylinder_voxel_collision<'a, T: BaseVol + ReadVol>( z_range.clone(), ) && vel.0.z < 0.5 && vel.0.z > -1.5 - && was_on_ground + && (was_on_ground || vel.0.z <= 0.05) { let snap_height = terrain .get(Vec3::new(pos.0.x, pos.0.y, pos.0.z - 0.1).map(|e| e.floor() as i32)) diff --git a/server/src/sys/agent.rs b/server/src/sys/agent.rs index 924d61ff26..c8067797cb 100644 --- a/server/src/sys/agent.rs +++ b/server/src/sys/agent.rs @@ -118,7 +118,7 @@ impl<'a> System<'a> for Sys { (read_data, event_bus, mut agents, mut controllers): Self::SystemData, ) { job.cpu_stats.measure(ParMode::Rayon); - ( + let controllers_to_insert = ( &read_data.entities, (&read_data.energies, &read_data.healths), &read_data.positions, @@ -130,7 +130,7 @@ impl<'a> System<'a> for Sys { &read_data.physics_states, &read_data.uids, &mut agents, - &mut controllers, + &controllers, read_data.light_emitter.maybe(), read_data.groups.maybe(), read_data.mount_states.maybe(), @@ -142,7 +142,7 @@ impl<'a> System<'a> for Sys { .map(|ms| *ms == MountState::Unmounted) .unwrap_or(true) }) - .for_each( + .map( |( entity, (energy, health), @@ -160,6 +160,8 @@ impl<'a> System<'a> for Sys { groups, _, )| { + let mut controller = (*controller).clone(); + //// Hack, replace with better system when groups are more sophisticated //// Override alignment if in a group unless entity is owned already let alignment = if !matches!( @@ -266,7 +268,7 @@ impl<'a> System<'a> for Sys { if hostile { data.hostile_tree( agent, - controller, + &mut controller, &read_data, &mut event_emitter, ); @@ -279,7 +281,7 @@ impl<'a> System<'a> for Sys { agent.bearing = Vec2::zero(); data.follow( agent, - controller, + &mut controller, &read_data.terrain, tgt_pos, ); @@ -311,12 +313,12 @@ impl<'a> System<'a> for Sys { hostile: false, }); data.idle( - agent, controller, &read_data, + agent, &mut controller, &read_data, ); } else { data.attack( agent, - controller, + &mut controller, &read_data.terrain, tgt_pos, read_data.bodies.get(attacker), @@ -331,7 +333,7 @@ impl<'a> System<'a> for Sys { } else if dist_sqrd > MAX_FOLLOW_DIST.powi(2) { data.follow( agent, - controller, + &mut controller, &read_data.terrain, tgt_pos, ); @@ -340,7 +342,7 @@ impl<'a> System<'a> for Sys { } else { data.idle_tree( agent, - controller, + &mut controller, &read_data, &mut event_emitter, ); @@ -349,18 +351,18 @@ impl<'a> System<'a> for Sys { } else { data.idle_tree( agent, - controller, + &mut controller, &read_data, &mut event_emitter, ); } } else { agent.target = None; - data.idle_tree(agent, controller, &read_data, &mut event_emitter); + data.idle_tree(agent, &mut controller, &read_data, &mut event_emitter); } } else { agent.target = None; - data.idle_tree(agent, controller, &read_data, &mut event_emitter); + data.idle_tree(agent, &mut controller, &read_data, &mut event_emitter); } } else { // Target an entity that's attacking us if the attack was recent @@ -381,7 +383,7 @@ impl<'a> System<'a> for Sys { agent.target = None; data.idle_tree( agent, - controller, + &mut controller, &read_data, &mut event_emitter, ); @@ -392,7 +394,7 @@ impl<'a> System<'a> for Sys { }); data.attack( agent, - controller, + &mut controller, &read_data.terrain, tgt_pos, read_data.bodies.get(attacker), @@ -403,7 +405,7 @@ impl<'a> System<'a> for Sys { agent.target = None; data.idle_tree( agent, - controller, + &mut controller, &read_data, &mut event_emitter, ); @@ -411,17 +413,24 @@ impl<'a> System<'a> for Sys { } } else { agent.target = None; - data.idle_tree(agent, controller, &read_data, &mut event_emitter); + data.idle_tree(agent, &mut controller, &read_data, &mut event_emitter); } } else { - data.idle_tree(agent, controller, &read_data, &mut event_emitter); + data.idle_tree(agent, &mut controller, &read_data, &mut event_emitter); } } debug_assert!(controller.inputs.move_dir.map(|e| !e.is_nan()).reduce_and()); debug_assert!(controller.inputs.look_dir.map(|e| !e.is_nan()).reduce_and()); + + (entity, controller) }, - ); + ) + .collect::>(); + + for (entity, controller) in controllers_to_insert { + controllers.insert(entity, controller); + } } } diff --git a/server/src/sys/sentinel.rs b/server/src/sys/sentinel.rs index 778892724c..13ff1b375c 100644 --- a/server/src/sys/sentinel.rs +++ b/server/src/sys/sentinel.rs @@ -2,7 +2,7 @@ use common::{ comp::{ Auras, BeamSegment, Body, Buffs, CanBuild, CharacterState, Collider, Combo, Energy, Gravity, Group, Health, Inventory, Item, LightEmitter, Mass, MountState, Mounting, Ori, - Player, Poise, Pos, Scale, Shockwave, Stats, Sticky, Vel, + Player, Poise, Pos, Scale, Shockwave, Stats, Sticky, Vel, Controller, }, uid::Uid, }; @@ -63,6 +63,7 @@ pub struct TrackedComps<'a> { pub character_state: ReadStorage<'a, CharacterState>, pub shockwave: ReadStorage<'a, Shockwave>, pub beam_segment: ReadStorage<'a, BeamSegment>, + pub controller: ReadStorage<'a, Controller>, } impl<'a> TrackedComps<'a> { pub fn create_entity_package( @@ -166,6 +167,10 @@ impl<'a> TrackedComps<'a> { .get(entity) .cloned() .map(|c| comps.push(c.into())); + self.controller + .get(entity) + .cloned() + .map(|c| comps.push(c.into())); // Add untracked comps pos.map(|c| comps.push(c.into())); vel.map(|c| comps.push(c.into())); @@ -201,6 +206,7 @@ pub struct ReadTrackers<'a> { pub character_state: ReadExpect<'a, UpdateTracker>, pub shockwave: ReadExpect<'a, UpdateTracker>, pub beam_segment: ReadExpect<'a, UpdateTracker>, + pub controller: ReadExpect<'a, UpdateTracker>, } impl<'a> ReadTrackers<'a> { pub fn create_sync_packages( @@ -245,7 +251,8 @@ impl<'a> ReadTrackers<'a> { filter, ) .with_component(&comps.uid, &*self.shockwave, &comps.shockwave, filter) - .with_component(&comps.uid, &*self.beam_segment, &comps.beam_segment, filter); + .with_component(&comps.uid, &*self.beam_segment, &comps.beam_segment, filter) + .with_component(&comps.uid, &*self.controller, &comps.controller, filter); (entity_sync_package, comp_sync_package) } @@ -278,6 +285,7 @@ pub struct WriteTrackers<'a> { character_state: WriteExpect<'a, UpdateTracker>, shockwave: WriteExpect<'a, UpdateTracker>, beam: WriteExpect<'a, UpdateTracker>, + controller: WriteExpect<'a, UpdateTracker>, } fn record_changes(comps: &TrackedComps, trackers: &mut WriteTrackers) { @@ -309,6 +317,7 @@ fn record_changes(comps: &TrackedComps, trackers: &mut WriteTrackers) { .record_changes(&comps.character_state); trackers.shockwave.record_changes(&comps.shockwave); trackers.beam.record_changes(&comps.beam_segment); + trackers.controller.record_changes(&comps.controller); // Debug how many updates are being sent /* macro_rules! log_counts { @@ -347,6 +356,7 @@ fn record_changes(comps: &TrackedComps, trackers: &mut WriteTrackers) { log_counts!(character_state, "Character States"); log_counts!(shockwave, "Shockwaves"); log_counts!(beam, "Beams"); + log_counts!(controller, "Controllers"); */ } @@ -376,6 +386,7 @@ pub fn register_trackers(world: &mut World) { world.register_tracker::(); world.register_tracker::(); world.register_tracker::(); + world.register_tracker::(); } /// Deleted entities grouped by region