From 9597810e3dcf00b39bad7d7d33edc49dcea1e361 Mon Sep 17 00:00:00 2001 From: TelepathicWalrus Date: Sun, 30 Apr 2023 19:26:52 +0100 Subject: [PATCH] Remove is_stay function Clean code --- client/src/lib.rs | 2 +- common/net/src/synced_components.rs | 1 - common/src/comp/pet.rs | 15 +++----- common/systems/src/controller.rs | 11 +++--- server/agent/src/data.rs | 3 +- server/src/events/interaction.rs | 18 +++++++--- server/src/events/mod.rs | 2 +- server/src/sys/agent.rs | 3 +- server/src/sys/agent/behavior_tree.rs | 4 +-- server/src/sys/pets.rs | 6 ++-- voxygen/src/hud/mod.rs | 43 +++++++++-------------- voxygen/src/session/mod.rs | 50 +++++++++++++-------------- 12 files changed, 73 insertions(+), 85 deletions(-) diff --git a/client/src/lib.rs b/client/src/lib.rs index 6e49c52436..22852894b8 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -1477,7 +1477,7 @@ impl Client { pub fn unmount(&mut self) { self.send_msg(ClientGeneral::ControlEvent(ControlEvent::Unmount)); } - pub fn toggle_stay(&mut self, entity: EcsEntity){ + pub fn toggle_stay(&mut self, entity: EcsEntity) { if let Some(uid) = self.state.read_component_copied(entity) { self.send_msg(ClientGeneral::ControlEvent(ControlEvent::ToggleStay(uid))); } diff --git a/common/net/src/synced_components.rs b/common/net/src/synced_components.rs index 0661a96f23..dbcf4aa25d 100644 --- a/common/net/src/synced_components.rs +++ b/common/net/src/synced_components.rs @@ -107,7 +107,6 @@ synced_components!(reexport_comps); use crate::sync::{NetSync, SyncFrom}; - // These are synced from any entity within range. impl NetSync for PetStates { const SYNC_FROM: SyncFrom = SyncFrom::AnyEntity; diff --git a/common/src/comp/pet.rs b/common/src/comp/pet.rs index 270d08b547..7943be6b15 100644 --- a/common/src/comp/pet.rs +++ b/common/src/comp/pet.rs @@ -1,8 +1,8 @@ use crate::comp::{body::Body, phys::Mass, quadruped_medium, quadruped_small}; use crossbeam_utils::atomic::AtomicCell; +use serde::{Deserialize, Serialize}; use specs::{Component, DerefFlaggedStorage}; use std::{num::NonZeroU64, sync::Arc}; -use serde::{Deserialize, Serialize}; pub type PetId = AtomicCell>; @@ -109,24 +109,17 @@ impl Component for Pet { type Storage = specs::VecStorage; } - #[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] -pub enum StayFollow{ +pub enum StayFollow { Stay, Follow, } -#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Default)] -pub struct PetState{ +#[derive(Copy, Clone, Debug, PartialEq, Serialize, Deserialize, Default)] +pub struct PetState { pub stay: bool, } -impl PetState { - pub fn get_state(&self) -> bool{ - self.stay - } -} - impl Component for PetState { type Storage = DerefFlaggedStorage; } diff --git a/common/systems/src/controller.rs b/common/systems/src/controller.rs index 5ca0a70f07..e8e017b216 100644 --- a/common/systems/src/controller.rs +++ b/common/systems/src/controller.rs @@ -65,11 +65,12 @@ impl<'a> System<'a> for Sys { }, ControlEvent::ToggleStay(pet_uid) => { if let Some(pet_entity) = read_data - .uid_allocator - .retrieve_entity_internal(pet_uid.id()){ - server_emitter.emit(ServerEvent::ToggleStay(pet_entity)); - } - } + .id_maps + .uid_entity(pet_uid) + { + server_emitter.emit(ServerEvent::ToggleStay(pet_entity)); + } + }, ControlEvent::RemoveBuff(buff_id) => { server_emitter.emit(ServerEvent::Buff { entity, diff --git a/server/agent/src/data.rs b/server/agent/src/data.rs index f43f6a6c4c..298927f506 100644 --- a/server/agent/src/data.rs +++ b/server/agent/src/data.rs @@ -12,9 +12,10 @@ use common::{ }, slot::EquipSlot, }, + pet::PetState, ActiveAbilities, Alignment, Body, CharacterState, Combo, Energy, Health, Inventory, LightEmitter, LootOwner, Ori, PhysicsState, Poise, Pos, Presence, Scale, SkillSet, Stance, - Stats, Vel, pet::PetState, + Stats, Vel, }, consts::GRAVITY, link::Is, diff --git a/server/src/events/interaction.rs b/server/src/events/interaction.rs index 395520360f..0c7af57610 100644 --- a/server/src/events/interaction.rs +++ b/server/src/events/interaction.rs @@ -202,15 +202,23 @@ pub fn handle_unmount(server: &mut Server, rider: EcsEntity) { state.ecs().write_storage::>().remove(rider); } -pub fn handle_toggle_stay(server: &mut Server, pet: EcsEntity){ +pub fn handle_toggle_stay(server: &mut Server, pet: EcsEntity) { let state = server.state_mut(); - if state.ecs() + if state + .ecs() .read_storage::() .get(pet) - .map_or(false, |s| s.stay){ - let _ = state.ecs().write_storage::().insert(pet, PetState { stay: false }); + .map_or(false, |s| s.stay) + { + let _ = state + .ecs() + .write_storage::() + .insert(pet, PetState { stay: false }); } else { - let _ = state.ecs().write_storage::().insert(pet, PetState { stay: true }); + let _ = state + .ecs() + .write_storage::() + .insert(pet, PetState { stay: true }); } } diff --git a/server/src/events/mod.rs b/server/src/events/mod.rs index 1e3834a825..e0edde427f 100644 --- a/server/src/events/mod.rs +++ b/server/src/events/mod.rs @@ -23,7 +23,7 @@ use group_manip::handle_group; use information::handle_site_info; use interaction::{ handle_create_sprite, handle_lantern, handle_mine_block, handle_mount, handle_npc_interaction, - handle_sound, handle_unmount, handle_toggle_stay, + handle_sound, handle_toggle_stay, handle_unmount, }; use inventory_manip::handle_inventory; use invite::{handle_invite, handle_invite_response}; diff --git a/server/src/sys/agent.rs b/server/src/sys/agent.rs index d7ba718d92..1b30497877 100644 --- a/server/src/sys/agent.rs +++ b/server/src/sys/agent.rs @@ -160,8 +160,7 @@ impl<'a> System<'a> for Sys { .map_or(false, |item| { matches!(&*item.kind(), comp::item::ItemKind::Glider) }); - let is_stay = pet_state - .map_or(false, |s| s.stay); + let is_stay = pet_state.map_or(false, |s| s.stay); let is_gliding = matches!( read_data.char_states.get(entity), diff --git a/server/src/sys/agent/behavior_tree.rs b/server/src/sys/agent/behavior_tree.rs index 470c7b6987..fe235adf8f 100644 --- a/server/src/sys/agent/behavior_tree.rs +++ b/server/src/sys/agent/behavior_tree.rs @@ -408,7 +408,7 @@ fn follow_if_far_away(bdata: &mut BehaviorData) -> bool { if let Some(tgt_pos) = bdata.read_data.positions.get(target) { let dist_sqrd = bdata.agent_data.pos.0.distance_squared(tgt_pos.0); let stay = bdata.agent_data.is_stay; - if dist_sqrd > (MAX_PATROL_DIST * bdata.agent.psyche.idle_wander_factor).powi(2) && !stay{ + if dist_sqrd > (MAX_PATROL_DIST * bdata.agent.psyche.idle_wander_factor).powi(2) && !stay { bdata .agent_data .follow(bdata.agent, bdata.controller, bdata.read_data, tgt_pos); @@ -432,7 +432,7 @@ fn attack_if_owner_hurt(bdata: &mut BehaviorData) -> bool { false }; let stay = bdata.agent_data.is_stay; - if owner_recently_attacked && !stay{ + if owner_recently_attacked && !stay { bdata.agent_data.attack_target_attacker( bdata.agent, bdata.read_data, diff --git a/server/src/sys/pets.rs b/server/src/sys/pets.rs index f0663beae0..c5d8faf232 100644 --- a/server/src/sys/pets.rs +++ b/server/src/sys/pets.rs @@ -1,5 +1,5 @@ use common::{ - comp::{Alignment, Pet, PhysicsState, Pos, pet::PetState}, + comp::{pet::PetState, Alignment, Pet, PhysicsState, Pos}, terrain::TerrainGrid, uid::IdMaps, }; @@ -30,7 +30,7 @@ impl<'a> System<'a> for Sys { (entities, terrain, mut positions, alignments, pets, pet_state, physics, id_maps): Self::SystemData, ) { const LOST_PET_DISTANCE_THRESHOLD: f32 = 200.0; - + // Find pets that are too far away from their owner let lost_pets: Vec<(Entity, Pos)> = (&entities, &positions, &alignments, &pets) .join() @@ -56,7 +56,7 @@ impl<'a> System<'a> for Sys { }) .map(|(entity, owner_pos, _, _)| (entity, *owner_pos)) .collect(); - + for (pet_entity, owner_pos) in lost_pets.iter() { let stay = pet_state.get(*pet_entity).map_or(false, |f| f.stay); if let Some(mut pet_pos) = positions.get_mut(*pet_entity) && !stay{ diff --git a/voxygen/src/hud/mod.rs b/voxygen/src/hud/mod.rs index d6f379c229..be988286a2 100644 --- a/voxygen/src/hud/mod.rs +++ b/voxygen/src/hud/mod.rs @@ -2317,13 +2317,12 @@ impl Hud { poise, (alignment, is_mount, is_rider, stance), )| { - // Use interpolated position if available let pos = interpolated.map_or(pos.0, |i| i.pos); let in_group = client.group_members().contains_key(uid); let is_me = entity == me; let dist_sqr = pos.distance_squared(player_pos); - + // Determine whether to display nametag and healthbar based on whether the // entity is mounted, has been damaged, is targeted/selected, or is in your // group @@ -2433,32 +2432,22 @@ impl Hud { i18n.get_msg("hud-mount").to_string(), )); } - } - let p = entity; - - let s = is_stay.get(p) - .map(|st| st.stay); - match s { - Some(false) => { - options.push(( - GameInput::StayFollow, - i18n.get_msg("hud-stay").to_string(), - )) - }, - Some(true) => { - options.push(( - GameInput::StayFollow, - i18n.get_msg("hud-follow").to_string(), - )) - }, - None => { - options.push(( - GameInput::StayFollow, - i18n.get_msg("hud-stay").to_string(), - )) - }, - } + let pet_stay = is_stay.get(entity).map(|st| st.stay); + match pet_stay { + Some(false) => options.push(( + GameInput::StayFollow, + i18n.get_msg("hud-stay").to_string(), + )), + Some(true) => options.push(( + GameInput::StayFollow, + i18n.get_msg("hud-follow").to_string(), + )), + None => options.push(( + GameInput::StayFollow, + i18n.get_msg("hud-stay").to_string(), + )), + } options }, _ => Vec::new(), diff --git a/voxygen/src/session/mod.rs b/voxygen/src/session/mod.rs index 6112fd7eb6..b8e1608186 100644 --- a/voxygen/src/session/mod.rs +++ b/voxygen/src/session/mod.rs @@ -943,32 +943,30 @@ impl PlayState for SessionState { let player_pos = client .state() .read_storage::() - .get(client.entity()) - .copied(); - if let Some(player_pos) = player_pos { - // Find closest mountable entity - let closest_pet = ( - &client.state().ecs().entities(), - &client.state().ecs().read_storage::(), - // TODO: More cleverly filter by things that can actually be mounted - client.state().ecs().read_storage::().maybe(), - ) - .join() - .filter(|(entity, _, _)| *entity != client.entity()) - .filter(|(_, _, alignment)| matches!(alignment, Some(comp::Alignment::Owned(owner)) if Some(*owner) == client.uid())) - .map(|(entity, pos, _)| { - (entity, player_pos.0.distance_squared(pos.0)) - }) - .filter(|(_, dist_sqr)| { - *dist_sqr < MAX_MOUNT_RANGE.powi(2) - }) - .min_by_key(|(_, dist_sqr)| OrderedFloat(*dist_sqr)); - if let Some((pet_entity, _)) = closest_pet - { - client.toggle_stay(pet_entity); - } - } - + .get(client.entity()) + .copied(); + if let Some(player_pos) = player_pos { + // Find closest mountable entity + let closest_pet = ( + &client.state().ecs().entities(), + &client.state().ecs().read_storage::(), + // TODO: More cleverly filter by things that can actually be mounted + client.state().ecs().read_storage::().maybe(), + ) + .join() + .filter(|(entity, _, _)| *entity != client.entity()) + .filter(|(_, _, alignment)| matches!(alignment, Some(comp::Alignment::Owned(owner)) if Some(*owner) == client.uid())) + .map(|(entity, pos, _)| { + (entity, player_pos.0.distance_squared(pos.0)) + }) + .filter(|(_, dist_sqr)| { + *dist_sqr < MAX_MOUNT_RANGE.powi(2) + }) + .min_by_key(|(_, dist_sqr)| OrderedFloat(*dist_sqr)); + if let Some((pet_entity, _)) = closest_pet { + client.toggle_stay(pet_entity); + } + } }, GameInput::Interact => { if state {