2020-01-24 21:24:57 +00:00
|
|
|
use crate::{
|
2020-04-18 18:28:19 +00:00
|
|
|
comp::{
|
|
|
|
self,
|
|
|
|
agent::Activity,
|
|
|
|
item::{tool::ToolKind, ItemKind},
|
2020-06-04 07:11:35 +00:00
|
|
|
Agent, Alignment, CharacterState, ChatMsg, ControlAction, Controller, Loadout, MountState,
|
|
|
|
Ori, Pos, Scale, Stats,
|
2020-04-18 18:28:19 +00:00
|
|
|
},
|
2020-06-04 07:11:35 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2020-01-25 18:49:47 +00:00
|
|
|
path::Chaser,
|
2020-04-18 18:28:19 +00:00
|
|
|
state::{DeltaTime, Time},
|
2020-06-04 07:11:35 +00:00
|
|
|
sync::{Uid, UidAllocator},
|
2020-02-01 20:39:39 +00:00
|
|
|
terrain::TerrainGrid,
|
2020-03-28 01:31:22 +00:00
|
|
|
util::Dir,
|
2020-01-27 10:02:36 +00:00
|
|
|
vol::ReadVol,
|
2020-01-24 21:24:57 +00:00
|
|
|
};
|
2020-01-27 15:51:07 +00:00
|
|
|
use rand::{thread_rng, Rng};
|
2020-01-24 21:24:57 +00:00
|
|
|
use specs::{
|
|
|
|
saveload::{Marker, MarkerAllocator},
|
2020-06-04 07:11:35 +00:00
|
|
|
Entities, Join, Read, ReadExpect, ReadStorage, System, Write, WriteStorage,
|
2020-01-24 21:24:57 +00:00
|
|
|
};
|
2019-04-16 21:06:33 +00:00
|
|
|
use vek::*;
|
|
|
|
|
2019-06-09 19:33:20 +00:00
|
|
|
/// This system will allow NPCs to modify their controller
|
2019-04-16 21:06:33 +00:00
|
|
|
pub struct Sys;
|
|
|
|
impl<'a> System<'a> for Sys {
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::type_complexity)]
|
2019-04-16 21:06:33 +00:00
|
|
|
type SystemData = (
|
2020-01-24 21:24:57 +00:00
|
|
|
Read<'a, UidAllocator>,
|
2020-01-25 02:15:15 +00:00
|
|
|
Read<'a, Time>,
|
2020-04-18 20:26:43 +00:00
|
|
|
Read<'a, DeltaTime>,
|
2020-06-04 07:11:35 +00:00
|
|
|
Write<'a, EventBus<ServerEvent>>,
|
2019-05-25 21:13:38 +00:00
|
|
|
Entities<'a>,
|
2019-04-16 21:06:33 +00:00
|
|
|
ReadStorage<'a, Pos>,
|
2020-03-28 01:31:22 +00:00
|
|
|
ReadStorage<'a, Ori>,
|
2020-04-16 23:07:29 +00:00
|
|
|
ReadStorage<'a, Scale>,
|
2019-08-02 20:25:33 +00:00
|
|
|
ReadStorage<'a, Stats>,
|
2020-04-18 20:26:43 +00:00
|
|
|
ReadStorage<'a, Loadout>,
|
|
|
|
ReadStorage<'a, CharacterState>,
|
2020-06-04 07:11:35 +00:00
|
|
|
ReadStorage<'a, Uid>,
|
2019-12-11 05:28:45 +00:00
|
|
|
ReadExpect<'a, TerrainGrid>,
|
2020-01-24 21:24:57 +00:00
|
|
|
ReadStorage<'a, Alignment>,
|
2019-08-02 20:25:33 +00:00
|
|
|
WriteStorage<'a, Agent>,
|
2019-06-09 14:20:20 +00:00
|
|
|
WriteStorage<'a, Controller>,
|
2019-09-09 19:11:40 +00:00
|
|
|
ReadStorage<'a, MountState>,
|
2019-04-16 21:06:33 +00:00
|
|
|
);
|
|
|
|
|
2020-06-10 19:47:36 +00:00
|
|
|
#[allow(clippy::or_fun_call)] // TODO: Pending review in #587
|
2019-08-04 08:21:29 +00:00
|
|
|
fn run(
|
|
|
|
&mut self,
|
2019-12-11 05:28:45 +00:00
|
|
|
(
|
2020-01-24 21:24:57 +00:00
|
|
|
uid_allocator,
|
2020-01-25 02:15:15 +00:00
|
|
|
time,
|
2020-04-18 20:26:43 +00:00
|
|
|
dt,
|
2020-06-04 07:11:35 +00:00
|
|
|
event_bus,
|
2019-12-11 05:28:45 +00:00
|
|
|
entities,
|
|
|
|
positions,
|
2020-03-28 01:31:22 +00:00
|
|
|
orientations,
|
2020-04-16 23:07:29 +00:00
|
|
|
scales,
|
2019-12-11 05:28:45 +00:00
|
|
|
stats,
|
2020-04-18 20:26:43 +00:00
|
|
|
loadouts,
|
|
|
|
character_states,
|
2020-06-04 07:11:35 +00:00
|
|
|
uids,
|
2019-12-11 05:28:45 +00:00
|
|
|
terrain,
|
2020-01-24 21:24:57 +00:00
|
|
|
alignments,
|
2019-12-11 05:28:45 +00:00
|
|
|
mut agents,
|
|
|
|
mut controllers,
|
|
|
|
mount_states,
|
|
|
|
): Self::SystemData,
|
2019-08-04 08:21:29 +00:00
|
|
|
) {
|
2020-04-18 18:28:19 +00:00
|
|
|
for (
|
|
|
|
entity,
|
|
|
|
pos,
|
|
|
|
ori,
|
|
|
|
alignment,
|
|
|
|
loadout,
|
|
|
|
character_state,
|
2020-06-04 07:11:35 +00:00
|
|
|
uid,
|
2020-04-18 18:28:19 +00:00
|
|
|
agent,
|
|
|
|
controller,
|
|
|
|
mount_state,
|
|
|
|
) in (
|
2019-09-09 19:11:40 +00:00
|
|
|
&entities,
|
|
|
|
&positions,
|
2020-03-28 01:31:22 +00:00
|
|
|
&orientations,
|
2020-01-24 21:24:57 +00:00
|
|
|
alignments.maybe(),
|
2020-04-18 20:26:43 +00:00
|
|
|
&loadouts,
|
|
|
|
&character_states,
|
2020-06-04 07:11:35 +00:00
|
|
|
&uids,
|
2019-09-09 19:11:40 +00:00
|
|
|
&mut agents,
|
|
|
|
&mut controllers,
|
|
|
|
mount_states.maybe(),
|
|
|
|
)
|
|
|
|
.join()
|
2019-05-25 21:13:38 +00:00
|
|
|
{
|
2019-09-09 19:11:40 +00:00
|
|
|
// Skip mounted entities
|
|
|
|
if mount_state
|
2020-06-08 18:37:41 +00:00
|
|
|
.map(|ms| *ms != MountState::Unmounted)
|
2019-09-09 19:11:40 +00:00
|
|
|
.unwrap_or(false)
|
|
|
|
{
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
controller.reset();
|
|
|
|
|
2019-12-01 23:40:05 +00:00
|
|
|
let mut inputs = &mut controller.inputs;
|
2019-10-15 04:06:14 +00:00
|
|
|
|
2020-03-28 01:31:22 +00:00
|
|
|
// Default to looking in orientation direction
|
|
|
|
inputs.look_dir = ori.0;
|
|
|
|
|
2020-01-25 18:49:47 +00:00
|
|
|
const AVG_FOLLOW_DIST: f32 = 6.0;
|
|
|
|
const MAX_FOLLOW_DIST: f32 = 12.0;
|
|
|
|
const MAX_CHASE_DIST: f32 = 24.0;
|
2020-04-19 11:50:25 +00:00
|
|
|
const LISTEN_DIST: f32 = 16.0;
|
|
|
|
const SEARCH_DIST: f32 = 48.0;
|
2020-04-16 23:07:29 +00:00
|
|
|
const SIGHT_DIST: f32 = 128.0;
|
2020-01-25 18:49:47 +00:00
|
|
|
const MIN_ATTACK_DIST: f32 = 3.25;
|
|
|
|
|
2020-04-23 12:29:22 +00:00
|
|
|
let scale = scales.get(entity).map(|s| s.0).unwrap_or(1.0);
|
2020-04-23 14:01:37 +00:00
|
|
|
|
2020-04-23 16:00:48 +00:00
|
|
|
// This controls how picky NPCs are about their pathfinding. Giants are larger
|
|
|
|
// and so can afford to be less precise when trying to move around
|
|
|
|
// the world (especially since they would otherwise get stuck on
|
|
|
|
// obstacles that smaller entities would not).
|
2020-04-23 12:29:22 +00:00
|
|
|
let traversal_tolerance = scale;
|
2020-04-16 23:07:29 +00:00
|
|
|
|
2020-01-25 18:49:47 +00:00
|
|
|
let mut do_idle = false;
|
2020-01-26 00:06:03 +00:00
|
|
|
let mut choose_target = false;
|
2020-01-25 18:49:47 +00:00
|
|
|
|
2020-01-26 12:47:41 +00:00
|
|
|
'activity: {
|
|
|
|
match &mut agent.activity {
|
|
|
|
Activity::Idle(bearing) => {
|
|
|
|
*bearing += Vec2::new(
|
|
|
|
thread_rng().gen::<f32>() - 0.5,
|
|
|
|
thread_rng().gen::<f32>() - 0.5,
|
|
|
|
) * 0.1
|
2020-04-18 20:26:43 +00:00
|
|
|
- *bearing * 0.003
|
2020-01-26 12:47:41 +00:00
|
|
|
- if let Some(patrol_origin) = agent.patrol_origin {
|
|
|
|
Vec2::<f32>::from(pos.0 - patrol_origin) * 0.0002
|
|
|
|
} else {
|
|
|
|
Vec2::zero()
|
|
|
|
};
|
2020-04-07 20:23:04 +00:00
|
|
|
|
2020-01-27 10:02:36 +00:00
|
|
|
// Stop if we're too close to a wall
|
|
|
|
*bearing *= 0.1
|
|
|
|
+ if terrain
|
|
|
|
.ray(
|
|
|
|
pos.0 + Vec3::unit_z(),
|
|
|
|
pos.0
|
2020-02-26 22:44:30 +00:00
|
|
|
+ Vec3::from(*bearing)
|
|
|
|
.try_normalized()
|
2020-04-07 20:23:04 +00:00
|
|
|
.unwrap_or(Vec3::unit_y())
|
2020-04-19 11:50:25 +00:00
|
|
|
* 5.0
|
2020-01-27 10:02:36 +00:00
|
|
|
+ Vec3::unit_z(),
|
|
|
|
)
|
|
|
|
.until(|block| block.is_solid())
|
|
|
|
.cast()
|
|
|
|
.1
|
|
|
|
.map(|b| b.is_none())
|
|
|
|
.unwrap_or(true)
|
|
|
|
{
|
|
|
|
0.9
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
2020-01-26 12:47:41 +00:00
|
|
|
|
2020-04-07 20:23:04 +00:00
|
|
|
if bearing.magnitude_squared() > 0.5f32.powf(2.0) {
|
|
|
|
inputs.move_dir = *bearing * 0.65;
|
2020-01-26 12:47:41 +00:00
|
|
|
}
|
2020-01-25 23:39:38 +00:00
|
|
|
|
2020-04-19 11:50:25 +00:00
|
|
|
// Put away weapon
|
2020-04-18 18:28:19 +00:00
|
|
|
if thread_rng().gen::<f32>() < 0.005 {
|
2020-04-19 11:50:25 +00:00
|
|
|
controller.actions.push(ControlAction::Unwield);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sit
|
2020-04-18 18:28:19 +00:00
|
|
|
if thread_rng().gen::<f32>() < 0.0035 {
|
2020-04-19 11:50:25 +00:00
|
|
|
controller.actions.push(ControlAction::Sit);
|
|
|
|
}
|
|
|
|
|
2020-01-26 12:47:41 +00:00
|
|
|
// Sometimes try searching for new targets
|
2020-04-18 18:28:19 +00:00
|
|
|
if thread_rng().gen::<f32>() < 0.1 {
|
2020-01-26 12:47:41 +00:00
|
|
|
choose_target = true;
|
|
|
|
}
|
2020-02-26 03:59:08 +00:00
|
|
|
},
|
2020-01-26 12:47:41 +00:00
|
|
|
Activity::Follow(target, chaser) => {
|
|
|
|
if let (Some(tgt_pos), _tgt_stats) =
|
|
|
|
(positions.get(*target), stats.get(*target))
|
|
|
|
{
|
|
|
|
let dist_sqrd = pos.0.distance_squared(tgt_pos.0);
|
|
|
|
// Follow, or return to idle
|
|
|
|
if dist_sqrd > AVG_FOLLOW_DIST.powf(2.0) {
|
2020-04-18 18:28:19 +00:00
|
|
|
if let Some(bearing) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
AVG_FOLLOW_DIST,
|
|
|
|
traversal_tolerance,
|
|
|
|
) {
|
2020-01-26 12:47:41 +00:00
|
|
|
inputs.move_dir = Vec2::from(bearing)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero());
|
|
|
|
inputs.jump.set_state(bearing.z > 1.0);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
2020-02-26 03:59:08 +00:00
|
|
|
},
|
2020-01-27 15:51:07 +00:00
|
|
|
Activity::Attack {
|
|
|
|
target,
|
|
|
|
chaser,
|
|
|
|
been_close,
|
2020-04-18 20:26:43 +00:00
|
|
|
powerup,
|
2020-01-27 15:51:07 +00:00
|
|
|
..
|
|
|
|
} => {
|
2020-04-18 20:26:43 +00:00
|
|
|
enum Tactic {
|
2020-04-19 11:50:25 +00:00
|
|
|
Melee,
|
2020-04-18 20:26:43 +00:00
|
|
|
RangedPowerup,
|
2020-04-19 11:50:25 +00:00
|
|
|
Staff,
|
2020-04-18 20:26:43 +00:00
|
|
|
}
|
|
|
|
|
2020-04-18 18:28:19 +00:00
|
|
|
let tactic = match loadout.active_item.as_ref().and_then(|ic| {
|
|
|
|
if let ItemKind::Tool(tool) = &ic.item.kind {
|
|
|
|
Some(&tool.kind)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}) {
|
2020-04-18 20:26:43 +00:00
|
|
|
Some(ToolKind::Bow(_)) => Tactic::RangedPowerup,
|
2020-04-19 11:50:25 +00:00
|
|
|
Some(ToolKind::Staff(_)) => Tactic::Staff,
|
2020-04-18 20:26:43 +00:00
|
|
|
_ => Tactic::Melee,
|
|
|
|
};
|
|
|
|
|
2020-02-26 02:48:09 +00:00
|
|
|
if let (Some(tgt_pos), Some(tgt_stats), tgt_alignment) = (
|
2020-01-26 12:47:41 +00:00
|
|
|
positions.get(*target),
|
|
|
|
stats.get(*target),
|
2020-01-27 15:51:07 +00:00
|
|
|
alignments
|
|
|
|
.get(*target)
|
|
|
|
.copied()
|
|
|
|
.unwrap_or(Alignment::Owned(*target)),
|
2020-01-26 12:47:41 +00:00
|
|
|
) {
|
2020-03-28 01:31:22 +00:00
|
|
|
if let Some(dir) = Dir::from_unnormalized(tgt_pos.0 - pos.0) {
|
|
|
|
inputs.look_dir = dir;
|
|
|
|
}
|
2020-03-17 17:27:52 +00:00
|
|
|
|
2020-01-27 15:51:07 +00:00
|
|
|
// Don't attack entities we are passive towards
|
|
|
|
// TODO: This is here, it's a bit of a hack
|
|
|
|
if let Some(alignment) = alignment {
|
2020-02-26 02:48:09 +00:00
|
|
|
if (*alignment).passive_towards(tgt_alignment) || tgt_stats.is_dead
|
|
|
|
{
|
2020-01-26 12:47:41 +00:00
|
|
|
do_idle = true;
|
|
|
|
break 'activity;
|
|
|
|
}
|
2020-01-26 00:06:03 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 12:47:41 +00:00
|
|
|
let dist_sqrd = pos.0.distance_squared(tgt_pos.0);
|
2020-04-23 12:29:22 +00:00
|
|
|
if dist_sqrd < (MIN_ATTACK_DIST * scale).powf(2.0) {
|
2020-01-26 12:47:41 +00:00
|
|
|
// Close-range attack
|
2020-05-04 16:59:32 +00:00
|
|
|
/*inputs.move_dir = Vec2::from(tgt_pos.0 - pos.0)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::unit_y())
|
|
|
|
* 0.7;*/
|
2020-04-18 20:26:43 +00:00
|
|
|
|
2020-04-23 12:29:22 +00:00
|
|
|
match tactic {
|
|
|
|
Tactic::Melee | Tactic::Staff => inputs.primary.set_state(true),
|
|
|
|
Tactic::RangedPowerup => inputs.roll.set_state(true),
|
2020-04-18 20:26:43 +00:00
|
|
|
}
|
2020-01-27 15:51:07 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powf(2.0)
|
2020-04-18 18:28:19 +00:00
|
|
|
|| (dist_sqrd < SIGHT_DIST.powf(2.0)
|
|
|
|
&& (!*been_close || !matches!(tactic, Tactic::Melee)))
|
2020-01-27 15:51:07 +00:00
|
|
|
{
|
2020-04-19 11:50:25 +00:00
|
|
|
let can_see_tgt = terrain
|
|
|
|
.ray(pos.0 + Vec3::unit_z(), tgt_pos.0 + Vec3::unit_z())
|
|
|
|
.until(|block| !block.is_air())
|
|
|
|
.cast()
|
2020-04-18 18:28:19 +00:00
|
|
|
.0
|
|
|
|
.powf(2.0)
|
|
|
|
>= dist_sqrd;
|
2020-04-18 20:26:43 +00:00
|
|
|
|
2020-04-19 11:50:25 +00:00
|
|
|
if can_see_tgt {
|
|
|
|
if let Tactic::RangedPowerup = tactic {
|
|
|
|
if *powerup > 2.0 {
|
|
|
|
inputs.primary.set_state(false);
|
|
|
|
*powerup = 0.0;
|
|
|
|
} else {
|
|
|
|
inputs.primary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
}
|
|
|
|
} else if let Tactic::Staff = tactic {
|
|
|
|
if !character_state.is_wield() {
|
|
|
|
inputs.primary.set_state(true);
|
|
|
|
}
|
2020-04-18 20:26:43 +00:00
|
|
|
|
2020-04-19 11:50:25 +00:00
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
}
|
2020-04-19 15:34:23 +00:00
|
|
|
}
|
2020-04-19 11:50:25 +00:00
|
|
|
|
2020-04-19 15:34:23 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powf(2.0) {
|
|
|
|
*been_close = true;
|
2020-01-27 15:51:07 +00:00
|
|
|
}
|
|
|
|
|
2020-01-26 12:47:41 +00:00
|
|
|
// Long-range chase
|
2020-04-18 18:28:19 +00:00
|
|
|
if let Some(bearing) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
1.25,
|
|
|
|
traversal_tolerance,
|
|
|
|
) {
|
2020-01-26 12:47:41 +00:00
|
|
|
inputs.move_dir = Vec2::from(bearing)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero());
|
|
|
|
inputs.jump.set_state(bearing.z > 1.0);
|
|
|
|
}
|
2020-01-27 16:18:36 +00:00
|
|
|
|
2020-04-19 15:34:23 +00:00
|
|
|
if dist_sqrd < 16.0f32.powf(2.0)
|
|
|
|
&& matches!(tactic, Tactic::Melee)
|
|
|
|
&& thread_rng().gen::<f32>() < 0.02
|
2020-01-27 16:18:36 +00:00
|
|
|
{
|
|
|
|
inputs.roll.set_state(true);
|
|
|
|
}
|
2020-01-26 12:47:41 +00:00
|
|
|
} else {
|
|
|
|
do_idle = true;
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
2020-02-26 04:01:51 +00:00
|
|
|
},
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if do_idle {
|
2020-01-25 23:39:38 +00:00
|
|
|
agent.activity = Activity::Idle(Vec2::zero());
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
// Choose a new target to attack: only go out of our way to attack targets we
|
|
|
|
// are hostile toward!
|
2020-01-26 00:06:03 +00:00
|
|
|
if choose_target {
|
2020-01-26 12:47:41 +00:00
|
|
|
// Search for new targets (this looks expensive, but it's only run occasionally)
|
|
|
|
// TODO: Replace this with a better system that doesn't consider *all* entities
|
2020-01-27 15:51:07 +00:00
|
|
|
let closest_entity = (&entities, &positions, &stats, alignments.maybe())
|
2020-01-26 00:06:03 +00:00
|
|
|
.join()
|
|
|
|
.filter(|(e, e_pos, e_stats, e_alignment)| {
|
2020-04-19 15:34:23 +00:00
|
|
|
((e_pos.0.distance_squared(pos.0) < SEARCH_DIST.powf(2.0) &&
|
|
|
|
// Within our view
|
|
|
|
(e_pos.0 - pos.0).try_normalized().map(|v| v.dot(*inputs.look_dir) > 0.15).unwrap_or(true))
|
|
|
|
// Within listen distance
|
|
|
|
|| e_pos.0.distance_squared(pos.0) < LISTEN_DIST.powf(2.0))
|
2020-01-26 00:06:03 +00:00
|
|
|
&& *e != entity
|
|
|
|
&& !e_stats.is_dead
|
|
|
|
&& alignment
|
|
|
|
.and_then(|a| e_alignment.map(|b| a.hostile_towards(*b)))
|
|
|
|
.unwrap_or(false)
|
|
|
|
})
|
2020-04-19 11:50:25 +00:00
|
|
|
// Can we even see them?
|
|
|
|
.filter(|(_, e_pos, _, _)| terrain
|
|
|
|
.ray(pos.0 + Vec3::unit_z(), e_pos.0 + Vec3::unit_z())
|
|
|
|
.until(|block| !block.is_air())
|
|
|
|
.cast()
|
|
|
|
.0 >= e_pos.0.distance(pos.0))
|
2020-01-27 15:51:07 +00:00
|
|
|
.min_by_key(|(_, e_pos, _, _)| (e_pos.0.distance_squared(pos.0) * 100.0) as i32)
|
|
|
|
.map(|(e, _, _, _)| e);
|
2020-01-26 00:06:03 +00:00
|
|
|
|
2020-01-27 15:51:07 +00:00
|
|
|
if let Some(target) = closest_entity {
|
|
|
|
agent.activity = Activity::Attack {
|
|
|
|
target,
|
|
|
|
chaser: Chaser::default(),
|
|
|
|
time: time.0,
|
|
|
|
been_close: false,
|
2020-04-18 20:26:43 +00:00
|
|
|
powerup: 0.0,
|
2020-01-27 15:51:07 +00:00
|
|
|
};
|
2020-01-26 00:06:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-01 20:39:39 +00:00
|
|
|
// --- Activity overrides (in reverse order of priority: most important goes
|
|
|
|
// last!) ---
|
2020-01-25 18:49:47 +00:00
|
|
|
|
|
|
|
// Attack a target that's attacking us
|
2020-05-26 02:45:13 +00:00
|
|
|
if let Some(my_stats) = stats.get(entity) {
|
2020-01-25 18:49:47 +00:00
|
|
|
// Only if the attack was recent
|
2020-05-26 02:45:13 +00:00
|
|
|
if my_stats.health.last_change.0 < 5.0 {
|
2020-03-20 13:26:18 +00:00
|
|
|
if let comp::HealthSource::Attack { by }
|
|
|
|
| comp::HealthSource::Projectile { owner: Some(by) } =
|
2020-05-26 02:45:13 +00:00
|
|
|
my_stats.health.last_change.1.cause
|
2020-03-20 13:26:18 +00:00
|
|
|
{
|
2020-01-25 18:49:47 +00:00
|
|
|
if !agent.activity.is_attack() {
|
|
|
|
if let Some(attacker) = uid_allocator.retrieve_entity_internal(by.id())
|
|
|
|
{
|
2020-05-26 02:45:13 +00:00
|
|
|
if stats.get(attacker).map_or(false, |a| !a.is_dead) {
|
|
|
|
if agent.can_speak {
|
2020-06-04 07:11:35 +00:00
|
|
|
let msg = "npc.speech.villager_under_attack".to_string();
|
|
|
|
event_bus
|
|
|
|
.emit_now(ServerEvent::Chat(ChatMsg::npc(*uid, msg)));
|
2020-05-26 02:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
agent.activity = Activity::Attack {
|
|
|
|
target: attacker,
|
|
|
|
chaser: Chaser::default(),
|
|
|
|
time: time.0,
|
|
|
|
been_close: false,
|
|
|
|
powerup: 0.0,
|
|
|
|
};
|
|
|
|
}
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Follow owner if we're too far, or if they're under attack
|
2020-01-27 15:51:07 +00:00
|
|
|
if let Some(Alignment::Owned(owner)) = alignment.copied() {
|
2020-01-25 18:49:47 +00:00
|
|
|
if let Some(owner_pos) = positions.get(owner) {
|
2020-01-26 12:47:41 +00:00
|
|
|
let dist_sqrd = pos.0.distance_squared(owner_pos.0);
|
|
|
|
if dist_sqrd > MAX_FOLLOW_DIST.powf(2.0) && !agent.activity.is_follow() {
|
2020-01-25 18:49:47 +00:00
|
|
|
agent.activity = Activity::Follow(owner, Chaser::default());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Attack owner's attacker
|
|
|
|
if let Some(owner_stats) = stats.get(owner) {
|
|
|
|
if owner_stats.health.last_change.0 < 5.0 {
|
|
|
|
if let comp::HealthSource::Attack { by } =
|
|
|
|
owner_stats.health.last_change.1.cause
|
|
|
|
{
|
|
|
|
if !agent.activity.is_attack() {
|
|
|
|
if let Some(attacker) =
|
|
|
|
uid_allocator.retrieve_entity_internal(by.id())
|
|
|
|
{
|
2020-01-27 15:51:07 +00:00
|
|
|
agent.activity = Activity::Attack {
|
|
|
|
target: attacker,
|
|
|
|
chaser: Chaser::default(),
|
|
|
|
time: time.0,
|
|
|
|
been_close: false,
|
2020-04-18 20:26:43 +00:00
|
|
|
powerup: 0.0,
|
2020-01-27 15:51:07 +00:00
|
|
|
};
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-01 23:40:05 +00:00
|
|
|
debug_assert!(inputs.move_dir.map(|e| !e.is_nan()).reduce_and());
|
|
|
|
debug_assert!(inputs.look_dir.map(|e| !e.is_nan()).reduce_and());
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|