From 97e0e398876fa03d4c2f1a341b26c86365bfba21 Mon Sep 17 00:00:00 2001 From: holychowders Date: Tue, 26 Apr 2022 13:22:55 -0500 Subject: [PATCH] Reduce function names and remove unnecessary component fetch from function. --- server/src/sys/agent.rs | 49 ++++++++++++++++-------------------- server/src/sys/agent/util.rs | 10 ++++---- 2 files changed, 27 insertions(+), 32 deletions(-) diff --git a/server/src/sys/agent.rs b/server/src/sys/agent.rs index 34019acb71..d2eb6e9395 100644 --- a/server/src/sys/agent.rs +++ b/server/src/sys/agent.rs @@ -13,9 +13,9 @@ use crate::{ }, data::{AgentData, AttackData, Path, ReadData, Tactic, TargetData}, util::{ - aim_projectile, are_our_owners_hostile, does_entity_see_other, get_attacker_of_entity, - get_entity_by_id, is_dead, is_dead_or_invulnerable, is_dressed_as_cultist, - is_invulnerable, is_village_guard, is_villager, stop_pursuing, + aim_projectile, are_our_owners_hostile, can_see_other, get_attacker, get_entity_by_id, + is_dead, is_dead_or_invulnerable, is_dressed_as_cultist, is_invulnerable, + is_village_guard, is_villager, stop_pursuing, }, }, }; @@ -311,7 +311,7 @@ impl<'a> System<'a> for Sys { // over the old one (i.e: because it's either close or // because they attacked us). if agent.target.map_or(true, |target| { - data.is_entity_more_dangerous_than_target( + data.is_more_dangerous_than_target( attacker, target, &read_data, ) }) { @@ -537,7 +537,7 @@ impl<'a> AgentData<'a> { } if rng.gen::() < 0.1 { - self.scan_for_new_hostile_target(agent, controller, read_data); + self.choose_target(agent, controller, read_data); } else { self.handle_sounds_heard(agent, controller, read_data, rng); } @@ -609,7 +609,7 @@ impl<'a> AgentData<'a> { self.cry_out(agent, event_emitter, read_data); agent.action_state.timer = 0.01; } else if within_flee_distance && has_opportunity_to_flee { - self.flee_from_pos(tgt_pos, agent, controller, &read_data.terrain); + self.flee(tgt_pos, agent, controller, &read_data.terrain); agent.action_state.timer += read_data.dt.0; } else { agent.action_state.timer = 0.0; @@ -637,7 +637,7 @@ impl<'a> AgentData<'a> { read_data.time.0 - selected_at > RETARGETING_THRESHOLD_SECONDS; if !in_aggro_range && is_time_to_retarget { - self.scan_for_new_hostile_target(agent, controller, read_data); + self.choose_target(agent, controller, read_data); } if aggro_on { @@ -707,8 +707,8 @@ impl<'a> AgentData<'a> { // Only emit event for agents that have a lantern equipped if lantern_equipped && rng.gen_bool(0.001) { if day_period.is_dark() && !lantern_turned_on { - // Agents with turned off lanterns turn them on randomly once it's nighttime and - // keep them on. + // Agents with turned off lanterns turn them on randomly once it's + // nighttime and keep them on. // Only emit event for agents that sill need to // turn on their lantern. controller.push_event(ControlEvent::EnableLantern) @@ -1452,9 +1452,9 @@ impl<'a> AgentData<'a> { }); } - fn flee_from_pos( + fn flee( &self, - pos: &Pos, + tgt_pos: &Pos, agent: &mut Agent, controller: &mut Controller, terrain: &TerrainGrid, @@ -1471,7 +1471,7 @@ impl<'a> AgentData<'a> { self.vel.0, // Away from the target (ironically) self.pos.0 - + (self.pos.0 - pos.0) + + (self.pos.0 - tgt_pos.0) .try_normalized() .unwrap_or_else(Vec3::unit_y) * 50.0, @@ -1545,12 +1545,7 @@ impl<'a> AgentData<'a> { } } - fn scan_for_new_hostile_target( - &self, - agent: &mut Agent, - controller: &mut Controller, - read_data: &ReadData, - ) { + fn choose_target(&self, agent: &mut Agent, controller: &mut Controller, read_data: &ReadData) { agent.action_state.timer = 0.0; let mut aggro_on = false; @@ -1566,10 +1561,10 @@ impl<'a> AgentData<'a> { }) }; let get_enemy = |entity: EcsEntity| { - if self.is_entity_an_enemy(entity, read_data) { + if self.is_enemy(entity, read_data) { Some(entity) } else if self.should_defend(entity, read_data) { - if let Some(attacker) = get_attacker_of_entity(entity, read_data) { + if let Some(attacker) = get_attacker(entity, read_data) { if !is_alignment_passive_towards_entity(attacker) { // aggro_on: attack immediately, do not warn/menace. aggro_on = true; @@ -1584,7 +1579,7 @@ impl<'a> AgentData<'a> { None } }; - let is_valid = |entity| { + let is_valid_target = |entity| { read_data.healths.get(entity).map_or(false, |health| { !health.is_dead && !is_invulnerable(entity, read_data) }) @@ -1597,13 +1592,13 @@ impl<'a> AgentData<'a> { let target = grid .in_circle_aabr(self.pos.0.xy(), agent.psyche.search_dist()) .filter(|entity| { - is_valid(*entity) - && does_entity_see_other(agent, *self.entity, *entity, controller, read_data) + is_valid_target(*entity) + && can_see_other(agent, *self.entity, *entity, controller, read_data) }) .filter_map(get_enemy) .min_by_key(|entity| { // TODO: This seems expensive. Cache this to avoid recomputing each tick - get_pos(*entity).map(|pos| (pos.0.distance_squared(self.pos.0) * 100.0) as i32) + get_pos(*entity).map(|e_pos| (e_pos.0.distance_squared(self.pos.0) * 100.0) as i32) }); if agent.target.is_none() && target.is_some() { @@ -2118,7 +2113,7 @@ impl<'a> AgentData<'a> { if !self.below_flee_health(agent) && follows_threatening_sounds { self.follow(agent, controller, &read_data.terrain, &sound_pos); } else if self.below_flee_health(agent) || !follows_threatening_sounds { - self.flee_from_pos(&sound_pos, agent, controller, &read_data.terrain); + self.flee(&sound_pos, agent, controller, &read_data.terrain); } else { self.idle(agent, controller, read_data, rng); } @@ -2336,7 +2331,7 @@ impl<'a> AgentData<'a> { self.damage.min(1.0) < agent.psyche.flee_health } - fn is_entity_more_dangerous_than_target( + fn is_more_dangerous_than_target( &self, entity: EcsEntity, target: Target, @@ -2378,7 +2373,7 @@ impl<'a> AgentData<'a> { }) } - fn is_entity_an_enemy(&self, entity: EcsEntity, read_data: &ReadData) -> bool { + fn is_enemy(&self, entity: EcsEntity, read_data: &ReadData) -> bool { let alignment = read_data.alignments.get(entity); (entity != *self.entity) diff --git a/server/src/sys/agent/util.rs b/server/src/sys/agent/util.rs index bc6a332141..24144b219c 100644 --- a/server/src/sys/agent/util.rs +++ b/server/src/sys/agent/util.rs @@ -35,7 +35,7 @@ pub fn is_invulnerable(entity: EcsEntity, read_data: &ReadData) -> bool { /// Gets alignment of owner if alignment given is `Owned`. /// Returns original alignment if not owned. -pub fn owner_alignment<'a>( +pub fn try_owner_alignment<'a>( alignment: Option<&'a Alignment>, read_data: &'a ReadData, ) -> Option<&'a Alignment> { @@ -144,8 +144,8 @@ pub fn are_our_owners_hostile( their_alignment: Option<&Alignment>, read_data: &ReadData, ) -> bool { - owner_alignment(our_alignment, read_data).map_or(false, |our_owners_alignment| { - owner_alignment(their_alignment, read_data).map_or(false, |their_owners_alignment| { + try_owner_alignment(our_alignment, read_data).map_or(false, |our_owners_alignment| { + try_owner_alignment(their_alignment, read_data).map_or(false, |their_owners_alignment| { our_owners_alignment.hostile_towards(*their_owners_alignment) }) }) @@ -198,7 +198,7 @@ pub fn is_dressed_as_cultist(entity: EcsEntity, read_data: &ReadData) -> bool { }) } -pub fn does_entity_see_other( +pub fn can_see_other( agent: &Agent, entity: EcsEntity, other: EcsEntity, @@ -242,7 +242,7 @@ pub fn does_entity_see_other( } } -pub fn get_attacker_of_entity(entity: EcsEntity, read_data: &ReadData) -> Option { +pub fn get_attacker(entity: EcsEntity, read_data: &ReadData) -> Option { read_data .healths .get(entity)