Pass positions and bodies into entities_have_line_of_sight() instead of fetching within.

This commit is contained in:
holychowders 2022-04-26 18:34:29 -05:00
parent 706ec91e69
commit 5d78294141
3 changed files with 150 additions and 107 deletions

View File

@ -1974,12 +1974,18 @@ impl<'a> AgentData<'a> {
tgt_data,
read_data,
),
Tactic::Turret => self.handle_turret_attack(agent, controller, &attack_data, read_data),
Tactic::FixedTurret => {
self.handle_fixed_turret_attack(agent, controller, &attack_data, read_data)
Tactic::Turret => {
self.handle_turret_attack(agent, controller, &attack_data, tgt_data, read_data)
},
Tactic::FixedTurret => self.handle_fixed_turret_attack(
agent,
controller,
&attack_data,
tgt_data,
read_data,
),
Tactic::RotatingTurret => {
self.handle_rotating_turret_attack(agent, controller, read_data)
self.handle_rotating_turret_attack(agent, controller, tgt_data, read_data)
},
Tactic::Mindflayer => self.handle_mindflayer_attack(
agent,

View File

@ -131,9 +131,7 @@ impl<'a> AgentData<'a> {
read_data: &ReadData,
) {
let line_of_sight_with_target = || {
agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
entities_have_line_of_sight(self.pos, self.body, tgt_data.pos, tgt_data.body, read_data)
};
let elevation = self.pos.0.z - tgt_data.pos.0.z;
@ -248,9 +246,13 @@ impl<'a> AgentData<'a> {
if attack_data.dist_sqrd < 32.0f32.powi(2)
&& has_leap()
&& has_energy(50.0)
&& agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
&& entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
)
{
use_leap(controller);
}
@ -311,9 +313,13 @@ impl<'a> AgentData<'a> {
if attack_data.dist_sqrd < 32.0f32.powi(2)
&& has_leap()
&& has_energy(50.0)
&& agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
&& entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
)
{
use_leap(controller);
}
@ -359,9 +365,13 @@ impl<'a> AgentData<'a> {
read_data,
Path::Separate,
None,
) && agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
}) {
) && entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
) {
if agent.action_state.timer > 4.0 && attack_data.angle < 45.0 {
controller.push_basic_input(InputKind::Secondary);
agent.action_state.timer = 0.0;
@ -400,10 +410,8 @@ impl<'a> AgentData<'a> {
const OPTIMAL_TARGET_VELOCITY: f32 = 5.0;
const DESIRED_ENERGY_LEVEL: f32 = 50.0;
let line_of_sight_with_target = |agent: &Agent| {
agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
let line_of_sight_with_target = || {
entities_have_line_of_sight(self.pos, self.body, tgt_data.pos, tgt_data.body, read_data)
};
// Logic to use abilities
@ -430,7 +438,7 @@ impl<'a> AgentData<'a> {
// If in repeater ranged, have enough energy, and aren't in recovery, try to
// keep firing
if attack_data.dist_sqrd > attack_data.min_attack_dist.powi(2)
&& line_of_sight_with_target(agent)
&& line_of_sight_with_target()
{
// Only keep firing if not in melee range or if can see target
controller.push_basic_input(InputKind::Secondary);
@ -464,8 +472,7 @@ impl<'a> AgentData<'a> {
controller.push_basic_input(InputKind::Primary);
}
}
} else if attack_data.dist_sqrd < MAX_PATH_DIST.powi(2) && line_of_sight_with_target(agent)
{
} else if attack_data.dist_sqrd < MAX_PATH_DIST.powi(2) && line_of_sight_with_target() {
// If not really far, and can see target, attempt to shoot bow
if self.energy.current() < DESIRED_ENERGY_LEVEL {
// If low on energy, use primary to attempt to regen energy
@ -504,7 +511,7 @@ impl<'a> AgentData<'a> {
..self.traversal_config
},
) {
if line_of_sight_with_target(agent) && attack_data.angle < 45.0 {
if line_of_sight_with_target() && attack_data.angle < 45.0 {
controller.inputs.move_dir = bearing
.xy()
.rotated_z(rng.gen_range(0.5..1.57))
@ -636,9 +643,13 @@ impl<'a> AgentData<'a> {
..self.traversal_config
},
) {
if agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
}) && attack_data.angle < 45.0
if entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
) && attack_data.angle < 45.0
{
controller.inputs.move_dir = bearing
.xy()
@ -687,15 +698,13 @@ impl<'a> AgentData<'a> {
const DESIRED_ENERGY_LEVEL: f32 = 50.0;
const DESIRED_COMBO_LEVEL: u32 = 8;
let line_of_sight_with_target = |agent: &Agent| {
agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
let line_of_sight_with_target = || {
entities_have_line_of_sight(self.pos, self.body, tgt_data.pos, tgt_data.body, read_data)
};
// Logic to use abilities
if attack_data.dist_sqrd > attack_data.min_attack_dist.powi(2)
&& line_of_sight_with_target(agent)
&& line_of_sight_with_target()
{
// If far enough away, and can see target, check which skill is appropriate to
// use
@ -773,7 +782,7 @@ impl<'a> AgentData<'a> {
..self.traversal_config
},
) {
if line_of_sight_with_target(agent) && attack_data.angle < 45.0 {
if line_of_sight_with_target() && attack_data.angle < 45.0 {
controller.inputs.move_dir = bearing
.xy()
.rotated_z(rng.gen_range(0.5..1.57))
@ -832,9 +841,13 @@ impl<'a> AgentData<'a> {
read_data,
Path::Separate,
None,
) && agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
}) && attack_data.angle < 90.0
) && entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
) && attack_data.angle < 90.0
{
if agent.action_state.timer > 5.0 {
controller.push_basic_input(InputKind::Secondary);
@ -972,9 +985,13 @@ impl<'a> AgentData<'a> {
},
) {
if attack_data.angle < 15.0
&& agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
&& entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
)
{
if agent.action_state.timer > 5.0 {
agent.action_state.timer = 0.0;
@ -1167,9 +1184,13 @@ impl<'a> AgentData<'a> {
Path::Separate,
None,
) && attack_data.angle < 15.0
&& agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
&& entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
)
{
controller.push_basic_input(InputKind::Primary);
}
@ -1321,11 +1342,11 @@ impl<'a> AgentData<'a> {
agent: &mut Agent,
controller: &mut Controller,
attack_data: &AttackData,
tgt_data: &TargetData,
read_data: &ReadData,
) {
if agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
}) && attack_data.angle < 15.0
if entities_have_line_of_sight(self.pos, self.body, tgt_data.pos, tgt_data.body, read_data)
&& attack_data.angle < 15.0
{
controller.push_basic_input(InputKind::Primary);
} else {
@ -1338,12 +1359,12 @@ impl<'a> AgentData<'a> {
agent: &mut Agent,
controller: &mut Controller,
attack_data: &AttackData,
tgt_data: &TargetData,
read_data: &ReadData,
) {
controller.inputs.look_dir = self.ori.look_dir();
if agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
}) && attack_data.angle < 15.0
if entities_have_line_of_sight(self.pos, self.body, tgt_data.pos, tgt_data.body, read_data)
&& attack_data.angle < 15.0
{
controller.push_basic_input(InputKind::Primary);
} else {
@ -1355,6 +1376,7 @@ impl<'a> AgentData<'a> {
&self,
agent: &mut Agent,
controller: &mut Controller,
tgt_data: &TargetData,
read_data: &ReadData,
) {
controller.inputs.look_dir = Dir::new(
@ -1364,9 +1386,8 @@ impl<'a> AgentData<'a> {
.try_normalized()
.unwrap_or_default(),
);
if agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
}) {
if entities_have_line_of_sight(self.pos, self.body, tgt_data.pos, tgt_data.body, read_data)
{
controller.push_basic_input(InputKind::Primary);
} else {
agent.target = None;
@ -1405,9 +1426,13 @@ impl<'a> AgentData<'a> {
agent.action_state.counter -= MINION_SUMMON_THRESHOLD;
}
} else if attack_data.dist_sqrd < MINDFLAYER_ATTACK_DIST.powi(2) {
if agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
}) {
if entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
) {
// If close to target, use either primary or secondary ability
if matches!(self.char_state, CharacterState::BasicBeam(c) if c.timer < Duration::from_secs(10) && !matches!(c.stage_section, StageSection::Recover))
{
@ -1497,9 +1522,13 @@ impl<'a> AgentData<'a> {
let small_chance = rng.gen_bool(0.05);
if small_chance
&& agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
&& entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
)
&& attack_data.angle < 15.0
{
// Fireball
@ -1620,9 +1649,13 @@ impl<'a> AgentData<'a> {
controller.push_cancel_input(InputKind::Fly);
if attack_data.dist_sqrd > 30.0_f32.powi(2) {
if rng.gen_bool(0.05)
&& agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
&& entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
)
&& attack_data.angle < 15.0
{
controller.push_basic_input(InputKind::Primary);
@ -1834,9 +1867,13 @@ impl<'a> AgentData<'a> {
},
) {
if attack_data.angle < 15.0
&& agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
&& entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
)
{
if agent.action_state.timer > 5.0 {
agent.action_state.timer = 0.0;
@ -2046,10 +2083,8 @@ impl<'a> AgentData<'a> {
.map(|t| t.target)
.and_then(|e| read_data.velocities.get(e))
.map_or(0.0, |v| v.0.cross(self.ori.look_vec()).magnitude_squared());
let line_of_sight_with_target = |agent: &Agent| {
agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
let line_of_sight_with_target = || {
entities_have_line_of_sight(self.pos, self.body, tgt_data.pos, tgt_data.body, read_data)
};
if attack_data.dist_sqrd < golem_melee_range.powi(2) {
@ -2068,7 +2103,7 @@ impl<'a> AgentData<'a> {
} else if attack_data.dist_sqrd < GOLEM_LASER_RANGE.powi(2) {
if matches!(self.char_state, CharacterState::BasicBeam(c) if c.timer < Duration::from_secs(5))
|| target_speed_cross_sqd < GOLEM_TARGET_SPEED.powi(2)
&& line_of_sight_with_target(agent)
&& line_of_sight_with_target()
&& attack_data.angle < 45.0
{
// If target in range threshold and haven't been lasering for more than 5
@ -2080,9 +2115,7 @@ impl<'a> AgentData<'a> {
controller.push_basic_input(InputKind::Ability(0));
}
} else if attack_data.dist_sqrd < GOLEM_LONG_RANGE.powi(2) {
if target_speed_cross_sqd < GOLEM_TARGET_SPEED.powi(2)
&& line_of_sight_with_target(agent)
{
if target_speed_cross_sqd < GOLEM_TARGET_SPEED.powi(2) && line_of_sight_with_target() {
// If target is far-ish and moving slow-ish, rocket them
controller.push_basic_input(InputKind::Ability(1));
} else if health_fraction < 0.7 {
@ -2114,10 +2147,8 @@ impl<'a> AgentData<'a> {
const BUBBLE_RANGE: f32 = 20.0;
const MINION_SUMMON_THRESHOLD: f32 = 0.20;
let health_fraction = self.health.map_or(0.5, |h| h.fraction());
let line_of_sight_with_target = |agent: &Agent| {
agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
let line_of_sight_with_target = || {
entities_have_line_of_sight(self.pos, self.body, tgt_data.pos, tgt_data.body, read_data)
};
// Sets counter at start of combat, using `condition` to keep track of whether
@ -2149,12 +2180,12 @@ impl<'a> AgentData<'a> {
} else if attack_data.in_min_range() && attack_data.angle < 60.0 {
// Pincer them if they're in range and angle
controller.push_basic_input(InputKind::Primary);
} else if attack_data.angle < 30.0 && line_of_sight_with_target(agent) {
} else if attack_data.angle < 30.0 && line_of_sight_with_target() {
// Start bubbling them if not close enough to do something else and in angle and
// can see target
controller.push_basic_input(InputKind::Ability(0));
}
} else if attack_data.angle < 90.0 && line_of_sight_with_target(agent) {
} else if attack_data.angle < 90.0 && line_of_sight_with_target() {
// Start scuttling if not close enough to do something else and in angle and can
// see target
controller.push_basic_input(InputKind::Secondary);
@ -2237,10 +2268,8 @@ impl<'a> AgentData<'a> {
const FIRE_BREATH_RANGE: f32 = 20.0;
const MAX_PUMPKIN_RANGE: f32 = 50.0;
let health_fraction = self.health.map_or(0.5, |h| h.fraction());
let line_of_sight_with_target = |agent: &Agent| {
agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
let line_of_sight_with_target = || {
entities_have_line_of_sight(self.pos, self.body, tgt_data.pos, tgt_data.body, read_data)
};
if health_fraction < VINE_CREATION_THRESHOLD && !agent.action_state.condition {
@ -2253,7 +2282,7 @@ impl<'a> AgentData<'a> {
}
} else if attack_data.dist_sqrd < FIRE_BREATH_RANGE.powi(2) {
if matches!(self.char_state, CharacterState::BasicBeam(c) if c.timer < Duration::from_secs(5))
&& line_of_sight_with_target(agent)
&& line_of_sight_with_target()
{
// Keep breathing fire if close enough, can see target, and have not been
// breathing for more than 5 seconds
@ -2261,13 +2290,11 @@ impl<'a> AgentData<'a> {
} else if attack_data.in_min_range() && attack_data.angle < 60.0 {
// Scythe them if they're in range and angle
controller.push_basic_input(InputKind::Primary);
} else if attack_data.angle < 30.0 && line_of_sight_with_target(agent) {
} else if attack_data.angle < 30.0 && line_of_sight_with_target() {
// Start breathing fire at them if close enough, in angle, and can see target
controller.push_basic_input(InputKind::Secondary);
}
} else if attack_data.dist_sqrd < MAX_PUMPKIN_RANGE.powi(2)
&& line_of_sight_with_target(agent)
{
} else if attack_data.dist_sqrd < MAX_PUMPKIN_RANGE.powi(2) && line_of_sight_with_target() {
// Throw a pumpkin at them if close enough and can see them
controller.push_basic_input(InputKind::Ability(1));
}
@ -2361,9 +2388,13 @@ impl<'a> AgentData<'a> {
if attack_data.in_min_range() {
controller.push_basic_input(InputKind::Primary);
} else if attack_data.dist_sqrd < MAX_PATH_DIST.powi(2)
&& agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
})
&& entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
)
{
// If in pathing range and can see target, move towards them
self.path_toward_target(
@ -2495,9 +2526,13 @@ impl<'a> AgentData<'a> {
if attack_data.in_min_range() {
// If in range, shockwave
controller.push_basic_input(InputKind::Ability(0));
} else if agent.target.map_or(false, |target| {
entities_have_line_of_sight(*self.entity, target.target, read_data)
}) {
} else if entities_have_line_of_sight(
self.pos,
self.body,
tgt_data.pos,
tgt_data.body,
read_data,
) {
// Else if in sight, barrage
controller.push_basic_input(InputKind::Secondary);
}

View File

@ -3,7 +3,7 @@ use common::{
combat::compute_stealth_coefficient,
comp::{
agent::Psyche, buff::BuffKind, inventory::item::ItemTag, item::ItemDesc, Agent, Alignment,
CharacterState, Controller, Pos,
Body, CharacterState, Controller, Pos,
},
consts::GRAVITY,
terrain::Block,
@ -152,24 +152,21 @@ pub fn are_our_owners_hostile(
}
pub fn entities_have_line_of_sight(
entity: EcsEntity,
other: EcsEntity,
pos: &Pos,
body: Option<&Body>,
other_pos: &Pos,
other_body: Option<&Body>,
read_data: &ReadData,
) -> bool {
let eye_pos = |ent: EcsEntity| {
let eye_offset = read_data.bodies.get(ent).map_or(0.0, |b| b.eye_height());
let get_eye_pos = |pos: &Pos, body: Option<&Body>| {
let eye_offset = body.map_or(0.0, |b| b.eye_height());
read_data
.positions
.get(entity)
.map(|pos| Pos(Vec3::new(pos.0.x, pos.0.y, pos.0.z + eye_offset)))
Pos(pos.0.with_z(pos.0.z + eye_offset))
};
let eye_pos = get_eye_pos(pos, body);
let other_eye_pos = get_eye_pos(other_pos, other_body);
if let (Some(eye_pos), Some(other_eye_pos)) = (eye_pos(entity), eye_pos(other)) {
positions_have_line_of_sight(&eye_pos, &other_eye_pos, read_data)
} else {
false
}
positions_have_line_of_sight(&eye_pos, &other_eye_pos, read_data)
}
pub fn positions_have_line_of_sight(pos_a: &Pos, pos_b: &Pos, read_data: &ReadData) -> bool {
@ -236,7 +233,12 @@ pub fn can_see_other(
.try_normalized()
.map_or(false, |v| v.dot(*controller.inputs.look_dir) > 0.15);
within_sight_dist && within_fov && entities_have_line_of_sight(entity, other, read_data)
let body = read_data.bodies.get(entity);
let other_body = read_data.bodies.get(other);
within_sight_dist
&& within_fov
&& entities_have_line_of_sight(pos, body, other_pos, other_body, read_data)
} else {
false
}