2020-01-24 21:24:57 +00:00
|
|
|
use crate::{
|
2020-04-18 18:28:19 +00:00
|
|
|
comp::{
|
|
|
|
self,
|
|
|
|
agent::Activity,
|
2020-04-26 17:03:19 +00:00
|
|
|
group,
|
2020-08-07 01:59:28 +00:00
|
|
|
group::Invite,
|
2020-11-06 17:39:49 +00:00
|
|
|
item::{
|
|
|
|
tool::{ToolKind, UniqueKind},
|
|
|
|
ItemKind,
|
|
|
|
},
|
2020-11-19 08:04:36 +00:00
|
|
|
Agent, Alignment, Body, CharacterState, ControlAction, ControlEvent, Controller, Energy,
|
|
|
|
GroupManip, Health, LightEmitter, Loadout, MountState, Ori, PhysicsState, Pos, Scale,
|
|
|
|
UnresolvedChatMsg, Vel,
|
2020-04-18 18:28:19 +00:00
|
|
|
},
|
2020-06-04 07:11:35 +00:00
|
|
|
event::{EventBus, ServerEvent},
|
2020-09-14 12:56:05 +00:00
|
|
|
metrics::SysMetrics,
|
2020-07-13 22:23:44 +00:00
|
|
|
path::{Chaser, TraversalConfig},
|
2020-08-26 08:11:31 +00:00
|
|
|
span,
|
2020-10-07 02:23:20 +00:00
|
|
|
state::{DeltaTime, Time, TimeOfDay},
|
2020-06-04 07:11:35 +00:00
|
|
|
sync::{Uid, UidAllocator},
|
2020-09-26 13:55:01 +00:00
|
|
|
terrain::{Block, TerrainGrid},
|
2020-10-07 02:23:20 +00:00
|
|
|
time::DayPeriod,
|
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
|
|
|
};
|
2020-11-11 03:18:45 +00:00
|
|
|
use std::f32::consts::PI;
|
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-10-08 22:55:30 +00:00
|
|
|
(
|
|
|
|
Read<'a, UidAllocator>,
|
|
|
|
Read<'a, Time>,
|
|
|
|
Read<'a, DeltaTime>,
|
|
|
|
Read<'a, group::GroupManager>,
|
|
|
|
),
|
2020-09-14 12:56:05 +00:00
|
|
|
ReadExpect<'a, SysMetrics>,
|
2020-06-04 07:11:35 +00:00
|
|
|
Write<'a, EventBus<ServerEvent>>,
|
2019-05-25 21:13:38 +00:00
|
|
|
Entities<'a>,
|
2020-10-07 20:52:29 +00:00
|
|
|
ReadStorage<'a, Energy>,
|
2019-04-16 21:06:33 +00:00
|
|
|
ReadStorage<'a, Pos>,
|
2020-07-03 19:30:21 +00:00
|
|
|
ReadStorage<'a, Vel>,
|
2020-03-28 01:31:22 +00:00
|
|
|
ReadStorage<'a, Ori>,
|
2020-04-16 23:07:29 +00:00
|
|
|
ReadStorage<'a, Scale>,
|
2020-10-31 22:34:08 +00:00
|
|
|
ReadStorage<'a, Health>,
|
2020-04-18 20:26:43 +00:00
|
|
|
ReadStorage<'a, Loadout>,
|
2020-07-09 23:43:11 +00:00
|
|
|
ReadStorage<'a, PhysicsState>,
|
2020-06-04 07:11:35 +00:00
|
|
|
ReadStorage<'a, Uid>,
|
2020-04-26 17:03:19 +00:00
|
|
|
ReadStorage<'a, group::Group>,
|
2019-12-11 05:28:45 +00:00
|
|
|
ReadExpect<'a, TerrainGrid>,
|
2020-01-24 21:24:57 +00:00
|
|
|
ReadStorage<'a, Alignment>,
|
2020-07-10 14:00:20 +00:00
|
|
|
ReadStorage<'a, Body>,
|
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>,
|
2020-08-07 01:59:28 +00:00
|
|
|
ReadStorage<'a, Invite>,
|
2020-10-07 02:23:20 +00:00
|
|
|
Read<'a, TimeOfDay>,
|
|
|
|
ReadStorage<'a, LightEmitter>,
|
2020-11-03 04:09:38 +00:00
|
|
|
ReadStorage<'a, CharacterState>,
|
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-10-08 22:55:30 +00:00
|
|
|
(uid_allocator, time, dt, group_manager),
|
2020-09-14 12:56:05 +00:00
|
|
|
sys_metrics,
|
2020-06-04 07:11:35 +00:00
|
|
|
event_bus,
|
2019-12-11 05:28:45 +00:00
|
|
|
entities,
|
2020-10-07 20:52:29 +00:00
|
|
|
energies,
|
2019-12-11 05:28:45 +00:00
|
|
|
positions,
|
2020-07-03 19:30:21 +00:00
|
|
|
velocities,
|
2020-03-28 01:31:22 +00:00
|
|
|
orientations,
|
2020-04-16 23:07:29 +00:00
|
|
|
scales,
|
2020-10-31 22:34:08 +00:00
|
|
|
healths,
|
2020-04-18 20:26:43 +00:00
|
|
|
loadouts,
|
2020-07-09 23:43:11 +00:00
|
|
|
physics_states,
|
2020-06-04 07:11:35 +00:00
|
|
|
uids,
|
2020-04-26 17:03:19 +00:00
|
|
|
groups,
|
2019-12-11 05:28:45 +00:00
|
|
|
terrain,
|
2020-01-24 21:24:57 +00:00
|
|
|
alignments,
|
2020-07-10 14:00:20 +00:00
|
|
|
bodies,
|
2019-12-11 05:28:45 +00:00
|
|
|
mut agents,
|
|
|
|
mut controllers,
|
|
|
|
mount_states,
|
2020-08-07 01:59:28 +00:00
|
|
|
invites,
|
2020-10-07 02:23:20 +00:00
|
|
|
time_of_day,
|
|
|
|
light_emitter,
|
2020-11-03 04:09:38 +00:00
|
|
|
char_states,
|
2019-12-11 05:28:45 +00:00
|
|
|
): Self::SystemData,
|
2019-08-04 08:21:29 +00:00
|
|
|
) {
|
2020-09-14 12:56:05 +00:00
|
|
|
let start_time = std::time::Instant::now();
|
2020-09-07 04:59:16 +00:00
|
|
|
span!(_guard, "run", "agent::Sys::run");
|
2020-04-18 18:28:19 +00:00
|
|
|
for (
|
|
|
|
entity,
|
2020-10-07 20:52:29 +00:00
|
|
|
energy,
|
2020-04-18 18:28:19 +00:00
|
|
|
pos,
|
2020-07-03 19:30:21 +00:00
|
|
|
vel,
|
2020-04-18 18:28:19 +00:00
|
|
|
ori,
|
|
|
|
alignment,
|
|
|
|
loadout,
|
2020-07-09 23:43:11 +00:00
|
|
|
physics_state,
|
2020-07-10 14:00:20 +00:00
|
|
|
body,
|
2020-06-04 07:11:35 +00:00
|
|
|
uid,
|
2020-04-18 18:28:19 +00:00
|
|
|
agent,
|
|
|
|
controller,
|
|
|
|
mount_state,
|
2020-04-26 17:03:19 +00:00
|
|
|
group,
|
2020-10-07 02:23:20 +00:00
|
|
|
light_emitter,
|
2020-04-18 18:28:19 +00:00
|
|
|
) in (
|
2019-09-09 19:11:40 +00:00
|
|
|
&entities,
|
2020-10-07 20:52:29 +00:00
|
|
|
&energies,
|
2019-09-09 19:11:40 +00:00
|
|
|
&positions,
|
2020-07-03 19:30:21 +00:00
|
|
|
&velocities,
|
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,
|
2020-07-09 23:43:11 +00:00
|
|
|
&physics_states,
|
2020-07-10 14:00:20 +00:00
|
|
|
bodies.maybe(),
|
2020-06-04 07:11:35 +00:00
|
|
|
&uids,
|
2019-09-09 19:11:40 +00:00
|
|
|
&mut agents,
|
|
|
|
&mut controllers,
|
|
|
|
mount_states.maybe(),
|
2020-04-26 17:03:19 +00:00
|
|
|
groups.maybe(),
|
2020-10-07 02:23:20 +00:00
|
|
|
light_emitter.maybe(),
|
2019-09-09 19:11:40 +00:00
|
|
|
)
|
|
|
|
.join()
|
2019-05-25 21:13:38 +00:00
|
|
|
{
|
2020-04-26 17:03:19 +00:00
|
|
|
// Hack, replace with better system when groups are more sophisticated
|
2020-08-03 21:54:33 +00:00
|
|
|
// Override alignment if in a group unless entity is owned already
|
|
|
|
let alignment = if !matches!(alignment, Some(Alignment::Owned(_))) {
|
|
|
|
group
|
|
|
|
.and_then(|g| group_manager.group_info(*g))
|
|
|
|
.and_then(|info| uids.get(info.leader))
|
|
|
|
.copied()
|
|
|
|
.map(Alignment::Owned)
|
|
|
|
.or(alignment.copied())
|
|
|
|
} else {
|
|
|
|
alignment.copied()
|
|
|
|
};
|
2020-04-26 17:03:19 +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();
|
2020-10-07 02:23:20 +00:00
|
|
|
let mut event_emitter = event_bus.emitter();
|
|
|
|
// Light lanterns at night
|
|
|
|
// TODO Add a method to turn on NPC lanterns underground
|
|
|
|
let lantern_equipped = loadout.lantern.as_ref().map_or(false, |item| {
|
|
|
|
matches!(item.kind(), comp::item::ItemKind::Lantern(_))
|
|
|
|
});
|
|
|
|
let lantern_turned_on = light_emitter.is_some();
|
|
|
|
let day_period = DayPeriod::from(time_of_day.0);
|
|
|
|
// Only emit event for agents that have a lantern equipped
|
|
|
|
if lantern_equipped {
|
|
|
|
let mut rng = thread_rng();
|
|
|
|
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
|
|
|
|
// Only emit event for agents that sill need to
|
|
|
|
// turn on their lantern
|
|
|
|
if let 0 = rng.gen_range(0, 1000) {
|
|
|
|
controller.events.push(ControlEvent::EnableLantern)
|
|
|
|
}
|
|
|
|
} else if lantern_turned_on && day_period.is_light() {
|
|
|
|
// agents with turned on lanterns turn them off randomly once it's daytime and
|
|
|
|
// keep them off
|
|
|
|
if let 0 = rng.gen_range(0, 2000) {
|
|
|
|
controller.events.push(ControlEvent::DisableLantern)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
2019-09-09 19:11:40 +00:00
|
|
|
|
2019-12-01 23:40:05 +00:00
|
|
|
let mut inputs = &mut controller.inputs;
|
2019-10-15 04:06:14 +00:00
|
|
|
|
2020-08-25 12:21:25 +00:00
|
|
|
// Default to looking in orientation direction (can be overridden below)
|
2020-03-28 01:31:22 +00:00
|
|
|
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;
|
2020-08-20 23:55:18 +00:00
|
|
|
const MAX_CHASE_DIST: f32 = 18.0;
|
2020-04-19 11:50:25 +00:00
|
|
|
const LISTEN_DIST: f32 = 16.0;
|
|
|
|
const SEARCH_DIST: f32 = 48.0;
|
2020-08-20 23:55:18 +00:00
|
|
|
const SIGHT_DIST: f32 = 80.0;
|
|
|
|
const MIN_ATTACK_DIST: f32 = 2.0;
|
|
|
|
const MAX_FLEE_DIST: f32 = 20.0;
|
2020-11-03 04:09:38 +00:00
|
|
|
const SNEAK_COEFFICIENT: f32 = 0.25;
|
2020-01-25 18:49:47 +00:00
|
|
|
|
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-07-30 19:26:49 +00:00
|
|
|
let node_tolerance = scale * 1.5;
|
2020-07-10 14:00:20 +00:00
|
|
|
let slow_factor = body.map(|b| b.base_accel() / 250.0).unwrap_or(0.0).min(1.0);
|
2020-11-14 17:00:32 +00:00
|
|
|
let traversal_config = TraversalConfig {
|
|
|
|
node_tolerance,
|
|
|
|
slow_factor,
|
|
|
|
on_ground: physics_state.on_ground,
|
|
|
|
in_liquid: physics_state.in_liquid.is_some(),
|
|
|
|
min_tgt_dist: 1.0,
|
|
|
|
can_climb: body.map(|b| b.can_climb()).unwrap_or(false),
|
|
|
|
};
|
|
|
|
|
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 {
|
2020-11-12 21:31:28 +00:00
|
|
|
Activity::Idle { bearing, chaser } => {
|
|
|
|
if let Some(travel_to) = agent.rtsim_controller.travel_to {
|
2020-11-23 15:39:03 +00:00
|
|
|
if let Some((bearing, speed)) =
|
|
|
|
chaser.chase(&*terrain, pos.0, vel.0, travel_to, TraversalConfig {
|
2020-11-12 21:31:28 +00:00
|
|
|
min_tgt_dist: 1.25,
|
2020-11-14 17:00:32 +00:00
|
|
|
..traversal_config
|
2020-11-23 15:39:03 +00:00
|
|
|
})
|
|
|
|
{
|
|
|
|
inputs.move_dir =
|
|
|
|
bearing.xy().try_normalized().unwrap_or(Vec2::zero())
|
|
|
|
* speed.min(agent.rtsim_controller.speed_factor);
|
2020-11-12 21:31:28 +00:00
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
2020-11-14 23:45:27 +00:00
|
|
|
inputs.climb = Some(comp::Climb::Up);
|
2020-11-23 15:39:03 +00:00
|
|
|
//.filter(|_| bearing.z > 0.1 || physics_state.in_liquid.is_some());
|
2020-11-14 23:45:27 +00:00
|
|
|
inputs.move_z = bearing.z + 0.05;
|
2020-11-12 21:31:28 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
*bearing += Vec2::new(
|
|
|
|
thread_rng().gen::<f32>() - 0.5,
|
|
|
|
thread_rng().gen::<f32>() - 0.5,
|
|
|
|
) * 0.1
|
|
|
|
- *bearing * 0.003
|
|
|
|
- agent.patrol_origin.map_or(Vec2::zero(), |patrol_origin| {
|
|
|
|
(pos.0 - patrol_origin).xy() * 0.0002
|
|
|
|
});
|
|
|
|
|
|
|
|
// Stop if we're too close to a wall
|
|
|
|
*bearing *= 0.1
|
|
|
|
+ if terrain
|
|
|
|
.ray(
|
|
|
|
pos.0 + Vec3::unit_z(),
|
|
|
|
pos.0
|
|
|
|
+ Vec3::from(*bearing)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec3::unit_y())
|
|
|
|
* 5.0
|
|
|
|
+ Vec3::unit_z(),
|
|
|
|
)
|
|
|
|
.until(Block::is_solid)
|
|
|
|
.cast()
|
|
|
|
.1
|
|
|
|
.map_or(true, |b| b.is_none())
|
|
|
|
{
|
|
|
|
0.9
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
2020-01-26 12:47:41 +00:00
|
|
|
|
2020-11-25 00:28:24 +00:00
|
|
|
if bearing.magnitude_squared() > 0.5f32.powi(2) {
|
2020-11-12 21:31:28 +00:00
|
|
|
inputs.move_dir = *bearing * 0.65;
|
|
|
|
}
|
|
|
|
|
2020-11-23 19:27:18 +00:00
|
|
|
// Put away weapon
|
|
|
|
if thread_rng().gen::<f32>() < 0.005 {
|
|
|
|
controller.actions.push(ControlAction::Unwield);
|
|
|
|
}
|
|
|
|
|
2020-11-12 21:31:28 +00:00
|
|
|
// Sit
|
|
|
|
if thread_rng().gen::<f32>() < 0.0035 {
|
|
|
|
controller.actions.push(ControlAction::Sit);
|
|
|
|
}
|
2020-01-26 12:47:41 +00:00
|
|
|
}
|
2020-01-25 23:39:38 +00:00
|
|
|
|
2020-11-14 17:00:32 +00:00
|
|
|
controller.actions.push(ControlAction::Unwield);
|
2020-04-19 11:50:25 +00:00
|
|
|
|
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-07-04 19:22:55 +00:00
|
|
|
Activity::Follow { target, chaser } => {
|
2020-10-31 22:34:08 +00:00
|
|
|
if let (Some(tgt_pos), _tgt_health) =
|
|
|
|
(positions.get(*target), healths.get(*target))
|
2020-01-26 12:47:41 +00:00
|
|
|
{
|
2020-07-06 19:51:23 +00:00
|
|
|
let dist = pos.0.distance(tgt_pos.0);
|
2020-01-26 12:47:41 +00:00
|
|
|
// Follow, or return to idle
|
2020-07-06 19:51:23 +00:00
|
|
|
if dist > AVG_FOLLOW_DIST {
|
2020-07-04 19:22:55 +00:00
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
2020-04-18 18:28:19 +00:00
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
2020-07-04 19:22:55 +00:00
|
|
|
vel.0,
|
2020-04-18 18:28:19 +00:00
|
|
|
tgt_pos.0,
|
2020-07-13 22:23:44 +00:00
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: AVG_FOLLOW_DIST,
|
2020-11-14 17:00:32 +00:00
|
|
|
..traversal_config
|
2020-07-13 22:23:44 +00:00
|
|
|
},
|
2020-04-18 18:28:19 +00:00
|
|
|
) {
|
2020-07-04 19:22:55 +00:00
|
|
|
inputs.move_dir =
|
|
|
|
bearing.xy().try_normalized().unwrap_or(Vec2::zero())
|
2020-07-06 19:51:23 +00:00
|
|
|
* speed.min(0.2 + (dist - AVG_FOLLOW_DIST) / 8.0);
|
2020-07-04 00:17:51 +00:00
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
2020-11-03 22:46:07 +00:00
|
|
|
inputs.move_z = bearing.z;
|
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 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-10-08 02:12:44 +00:00
|
|
|
#[derive(Eq, PartialEq)]
|
2020-04-18 20:26:43 +00:00
|
|
|
enum Tactic {
|
2020-04-19 11:50:25 +00:00
|
|
|
Melee,
|
2020-10-19 03:48:28 +00:00
|
|
|
Axe,
|
2020-10-07 20:52:29 +00:00
|
|
|
Hammer,
|
|
|
|
Sword,
|
2020-11-11 04:36:40 +00:00
|
|
|
Bow,
|
2020-04-19 11:50:25 +00:00
|
|
|
Staff,
|
2020-08-22 23:35:58 +00:00
|
|
|
StoneGolemBoss,
|
2020-11-24 23:23:29 +00:00
|
|
|
CircleCharge { radius: u32, circle_time: u32 },
|
2020-11-16 04:35:22 +00:00
|
|
|
QuadLowRanged,
|
|
|
|
TailSlap,
|
|
|
|
QuadLowQuick,
|
|
|
|
QuadLowBasic,
|
|
|
|
QuadMedJump,
|
2020-11-23 19:27:18 +00:00
|
|
|
QuadMedBasic,
|
2020-11-22 08:09:43 +00:00
|
|
|
Lavadrake,
|
|
|
|
Theropod,
|
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| {
|
2020-09-17 23:02:14 +00:00
|
|
|
if let ItemKind::Tool(tool) = &ic.item.kind() {
|
2020-04-18 18:28:19 +00:00
|
|
|
Some(&tool.kind)
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}) {
|
2020-11-11 04:36:40 +00:00
|
|
|
Some(ToolKind::Bow) => Tactic::Bow,
|
2020-11-06 17:39:49 +00:00
|
|
|
Some(ToolKind::Staff) => Tactic::Staff,
|
|
|
|
Some(ToolKind::Hammer) => Tactic::Hammer,
|
|
|
|
Some(ToolKind::Sword) => Tactic::Sword,
|
|
|
|
Some(ToolKind::Axe) => Tactic::Axe,
|
|
|
|
Some(ToolKind::Unique(UniqueKind::StoneGolemFist)) => {
|
|
|
|
Tactic::StoneGolemBoss
|
2020-08-22 23:35:58 +00:00
|
|
|
},
|
2020-11-23 19:27:18 +00:00
|
|
|
Some(ToolKind::Unique(UniqueKind::QuadMedQuick)) => {
|
|
|
|
Tactic::CircleCharge {
|
|
|
|
radius: 3,
|
|
|
|
circle_time: 2,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Some(ToolKind::Unique(UniqueKind::QuadMedCharge)) => {
|
|
|
|
Tactic::CircleCharge {
|
|
|
|
radius: 15,
|
|
|
|
circle_time: 1,
|
|
|
|
}
|
|
|
|
},
|
2020-11-19 08:04:36 +00:00
|
|
|
|
2020-11-16 04:35:22 +00:00
|
|
|
Some(ToolKind::Unique(UniqueKind::QuadMedJump)) => Tactic::QuadMedJump,
|
2020-11-17 07:44:30 +00:00
|
|
|
Some(ToolKind::Unique(UniqueKind::QuadMedBasic)) => {
|
2020-11-23 19:27:18 +00:00
|
|
|
Tactic::QuadMedBasic
|
2020-11-17 07:44:30 +00:00
|
|
|
},
|
2020-11-16 04:35:22 +00:00
|
|
|
Some(ToolKind::Unique(UniqueKind::QuadLowRanged)) => {
|
|
|
|
Tactic::QuadLowRanged
|
|
|
|
},
|
|
|
|
Some(ToolKind::Unique(UniqueKind::QuadLowTail)) => Tactic::TailSlap,
|
|
|
|
Some(ToolKind::Unique(UniqueKind::QuadLowQuick)) => {
|
|
|
|
Tactic::QuadLowQuick
|
|
|
|
},
|
|
|
|
Some(ToolKind::Unique(UniqueKind::QuadLowBasic)) => {
|
|
|
|
Tactic::QuadLowBasic
|
|
|
|
},
|
2020-11-22 08:09:43 +00:00
|
|
|
Some(ToolKind::Unique(UniqueKind::QuadLowBreathe)) => Tactic::Lavadrake,
|
|
|
|
Some(ToolKind::Unique(UniqueKind::TheropodBasic)) => Tactic::Theropod,
|
|
|
|
Some(ToolKind::Unique(UniqueKind::TheropodBird)) => Tactic::Theropod,
|
2020-04-18 20:26:43 +00:00
|
|
|
_ => Tactic::Melee,
|
|
|
|
};
|
|
|
|
|
2020-10-31 22:34:08 +00:00
|
|
|
if let (Some(tgt_pos), Some(tgt_health), tgt_alignment) = (
|
2020-01-26 12:47:41 +00:00
|
|
|
positions.get(*target),
|
2020-10-31 22:34:08 +00:00
|
|
|
healths.get(*target),
|
2020-07-07 00:11:37 +00:00
|
|
|
alignments.get(*target).copied().unwrap_or(
|
|
|
|
uids.get(*target)
|
2020-07-07 00:01:39 +00:00
|
|
|
.copied()
|
|
|
|
.map(Alignment::Owned)
|
2020-07-07 00:11:37 +00:00
|
|
|
.unwrap_or(Alignment::Wild),
|
|
|
|
),
|
2020-01-26 12:47:41 +00:00
|
|
|
) {
|
2020-11-16 04:35:22 +00:00
|
|
|
// Wield the weapon as running towards the target
|
2020-11-11 04:36:40 +00:00
|
|
|
controller.actions.push(ControlAction::Wield);
|
|
|
|
|
2020-11-15 01:17:41 +00:00
|
|
|
let eye_offset = body.map_or(0.0, |b| b.eye_height());
|
2020-11-14 22:05:35 +00:00
|
|
|
|
2020-11-24 23:23:29 +00:00
|
|
|
let tgt_eye_offset = bodies.get(*target).map_or(0.0, |b| b.eye_height()) +
|
|
|
|
// Special case for jumping attacks to jump at the body
|
|
|
|
// of the target and not the ground around the target
|
|
|
|
// For the ranged it is to shoot at the feet and not
|
|
|
|
// the head to get splash damage
|
|
|
|
if tactic == Tactic::QuadMedJump {
|
|
|
|
1.0
|
|
|
|
} else if matches!(tactic, Tactic::QuadLowRanged) {
|
|
|
|
-1.0
|
|
|
|
} else {
|
|
|
|
0.0
|
|
|
|
};
|
2020-11-20 01:42:22 +00:00
|
|
|
|
2020-11-23 19:27:18 +00:00
|
|
|
// Hacky distance offset for ranged weapons
|
2020-11-11 04:36:40 +00:00
|
|
|
let distance_offset = match tactic {
|
|
|
|
Tactic::Bow => 0.0004 * pos.0.distance_squared(tgt_pos.0),
|
|
|
|
Tactic::Staff => 0.0015 * pos.0.distance_squared(tgt_pos.0),
|
2020-11-23 19:27:18 +00:00
|
|
|
Tactic::QuadLowRanged => 0.03 * pos.0.distance_squared(tgt_pos.0),
|
2020-11-11 04:36:40 +00:00
|
|
|
_ => 0.0,
|
|
|
|
};
|
|
|
|
|
2020-11-16 04:35:22 +00:00
|
|
|
// Apply the distance and eye offsets to make the
|
|
|
|
// look_dir the vector from projectile launch to
|
|
|
|
// target point
|
2020-11-11 04:36:40 +00:00
|
|
|
if let Some(dir) = Dir::from_unnormalized(
|
|
|
|
Vec3::new(
|
|
|
|
tgt_pos.0.x,
|
|
|
|
tgt_pos.0.y,
|
|
|
|
tgt_pos.0.z + tgt_eye_offset + distance_offset,
|
|
|
|
) - Vec3::new(pos.0.x, pos.0.y, pos.0.z + eye_offset),
|
|
|
|
) {
|
2020-03-28 01:31:22 +00:00
|
|
|
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-10-31 22:34:08 +00:00
|
|
|
if alignment.passive_towards(tgt_alignment) || tgt_health.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-07-29 19:58:14 +00:00
|
|
|
|
2020-10-31 22:34:08 +00:00
|
|
|
let damage = healths
|
2020-07-29 19:58:14 +00:00
|
|
|
.get(entity)
|
2020-10-31 22:34:08 +00:00
|
|
|
.map(|h| h.current() as f32 / h.maximum() as f32)
|
2020-07-29 19:58:14 +00:00
|
|
|
.unwrap_or(0.5);
|
|
|
|
|
|
|
|
// Flee
|
2020-07-30 19:26:49 +00:00
|
|
|
let flees = alignment
|
|
|
|
.map(|a| !matches!(a, Alignment::Enemy | Alignment::Owned(_)))
|
|
|
|
.unwrap_or(true);
|
|
|
|
if 1.0 - agent.psyche.aggro > damage && flees {
|
2020-11-22 08:09:43 +00:00
|
|
|
if let Some(body) = body {
|
2020-11-24 23:23:29 +00:00
|
|
|
if body.can_strafe() {
|
2020-11-22 08:09:43 +00:00
|
|
|
controller.actions.push(ControlAction::Unwield);
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_FLEE_DIST.powi(2) {
|
2020-07-29 23:14:06 +00:00
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
// Away from the target (ironically)
|
2020-08-12 14:10:12 +00:00
|
|
|
pos.0
|
|
|
|
+ (pos.0 - tgt_pos.0)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or_else(Vec3::unit_y)
|
2020-11-22 08:09:43 +00:00
|
|
|
* 50.0,
|
2020-07-29 23:14:06 +00:00
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
2020-11-14 17:00:32 +00:00
|
|
|
..traversal_config
|
2020-07-29 23:14:06 +00:00
|
|
|
},
|
|
|
|
) {
|
2020-10-17 01:27:57 +00:00
|
|
|
inputs.move_dir =
|
|
|
|
bearing.xy().try_normalized().unwrap_or(Vec2::zero())
|
2020-11-23 19:27:18 +00:00
|
|
|
* speed;
|
2020-07-29 23:14:06 +00:00
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
2020-11-03 22:46:07 +00:00
|
|
|
inputs.move_z = bearing.z;
|
2020-07-29 23:14:06 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
2020-07-29 19:58:14 +00:00
|
|
|
}
|
2020-11-23 19:27:18 +00:00
|
|
|
} else {
|
|
|
|
// Match on tactic. Each tactic has different controls
|
|
|
|
// depending on the distance from the agent to the target
|
2020-04-23 12:29:22 +00:00
|
|
|
match tactic {
|
2020-11-23 19:27:18 +00:00
|
|
|
Tactic::Melee => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-22 08:09:43 +00:00
|
|
|
inputs.primary.set_state(true);
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = Vec2::zero();
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
2020-11-11 03:18:45 +00:00
|
|
|
|
2020-11-24 23:23:29 +00:00
|
|
|
if dist_sqrd < 16.0f32.powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
&& thread_rng().gen::<f32>() < 0.02
|
|
|
|
{
|
|
|
|
inputs.roll.set_state(true);
|
|
|
|
}
|
2020-11-13 06:13:37 +00:00
|
|
|
} else {
|
2020-11-23 19:27:18 +00:00
|
|
|
do_idle = true;
|
2020-11-13 06:13:37 +00:00
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::Axe => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = Vec2::zero();
|
|
|
|
if *powerup > 6.0 {
|
2020-11-11 04:36:40 +00:00
|
|
|
inputs.secondary.set_state(false);
|
|
|
|
*powerup = 0.0;
|
2020-11-23 19:27:18 +00:00
|
|
|
} else if *powerup > 4.0 && energy.current() > 10 {
|
2020-11-11 04:36:40 +00:00
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
2020-11-23 19:27:18 +00:00
|
|
|
} else if energy.current() > 800
|
|
|
|
&& thread_rng().gen_bool(0.5)
|
2020-11-11 04:36:40 +00:00
|
|
|
{
|
|
|
|
inputs.ability3.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else {
|
|
|
|
inputs.primary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < 16.0f32.powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
&& thread_rng().gen::<f32>() < 0.02
|
|
|
|
{
|
|
|
|
inputs.roll.set_state(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::Hammer => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = Vec2::zero();
|
2020-11-11 04:36:40 +00:00
|
|
|
if *powerup > 4.0 {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.secondary.set_state(false);
|
2020-11-11 04:36:40 +00:00
|
|
|
*powerup = 0.0;
|
2020-11-23 19:27:18 +00:00
|
|
|
} else if *powerup > 2.0 {
|
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else if energy.current() > 700 {
|
|
|
|
inputs.ability3.set_state(true);
|
|
|
|
*powerup += dt.0;
|
2020-11-11 04:36:40 +00:00
|
|
|
} else {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.primary.set_state(true);
|
2020-11-11 04:36:40 +00:00
|
|
|
*powerup += dt.0;
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
if can_see_tgt(&*terrain, pos, tgt_pos, dist_sqrd) {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
if *powerup > 5.0 {
|
|
|
|
inputs.ability3.set_state(true);
|
|
|
|
*powerup = 0.0;
|
|
|
|
} else {
|
|
|
|
*powerup += dt.0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < 16.0f32.powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
&& thread_rng().gen::<f32>() < 0.02
|
|
|
|
{
|
|
|
|
inputs.roll.set_state(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::Sword => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = Vec2::zero();
|
|
|
|
if *powerup < 2.0 && energy.current() > 600 {
|
2020-11-11 04:36:40 +00:00
|
|
|
inputs.ability3.set_state(true);
|
2020-11-23 19:27:18 +00:00
|
|
|
*powerup += dt.0;
|
|
|
|
} else if *powerup > 2.0 {
|
2020-11-11 04:36:40 +00:00
|
|
|
*powerup = 0.0;
|
|
|
|
} else {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.primary.set_state(true);
|
2020-11-11 04:36:40 +00:00
|
|
|
*powerup += dt.0;
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
if can_see_tgt(&*terrain, pos, tgt_pos, dist_sqrd) {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
if *powerup > 4.0 {
|
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
*powerup = 0.0;
|
|
|
|
} else {
|
|
|
|
*powerup += dt.0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < 16.0f32.powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
&& thread_rng().gen::<f32>() < 0.02
|
|
|
|
{
|
|
|
|
inputs.roll.set_state(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::Bow => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (2.0 * MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.roll.set_state(true);
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
if can_see_tgt(&*terrain, pos, tgt_pos, dist_sqrd) {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.rotated_z(
|
|
|
|
thread_rng().gen_range(0.5, 1.57),
|
|
|
|
)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
if *powerup > 4.0 {
|
|
|
|
inputs.secondary.set_state(false);
|
|
|
|
*powerup = 0.0;
|
|
|
|
} else if *powerup > 2.0
|
|
|
|
&& energy.current() > 300
|
|
|
|
{
|
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else if energy.current() > 400
|
|
|
|
&& thread_rng().gen_bool(0.8)
|
|
|
|
{
|
|
|
|
inputs.secondary.set_state(false);
|
|
|
|
inputs.ability3.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else {
|
|
|
|
inputs.secondary.set_state(false);
|
|
|
|
inputs.primary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < 16.0f32.powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
&& thread_rng().gen::<f32>() < 0.02
|
|
|
|
{
|
|
|
|
inputs.roll.set_state(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::Staff => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.roll.set_state(true);
|
|
|
|
} else if dist_sqrd
|
2020-11-25 00:28:24 +00:00
|
|
|
< (5.0 * MIN_ATTACK_DIST * scale).powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
|
|
|
if *powerup < 1.5 {
|
|
|
|
inputs.move_dir = (tgt_pos.0 - pos.0)
|
|
|
|
.xy()
|
|
|
|
.rotated_z(0.47 * PI)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::unit_y());
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else if *powerup < 3.0 {
|
|
|
|
inputs.move_dir = (tgt_pos.0 - pos.0)
|
|
|
|
.xy()
|
|
|
|
.rotated_z(-0.47 * PI)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::unit_y());
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else {
|
2020-11-11 04:36:40 +00:00
|
|
|
*powerup = 0.0;
|
2020-11-23 19:27:18 +00:00
|
|
|
}
|
|
|
|
if energy.current() > 800
|
|
|
|
&& thread_rng().gen::<f32>() > 0.8
|
|
|
|
{
|
|
|
|
inputs.ability3.set_state(true);
|
|
|
|
} else if energy.current() > 10 {
|
|
|
|
inputs.secondary.set_state(true);
|
2020-11-11 04:36:40 +00:00
|
|
|
} else {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.primary.set_state(true);
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
if can_see_tgt(&*terrain, pos, tgt_pos, dist_sqrd) {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.rotated_z(
|
|
|
|
thread_rng().gen_range(-1.57, -0.5),
|
|
|
|
)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
inputs.primary.set_state(true);
|
|
|
|
} else {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < 16.0f32.powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
&& thread_rng().gen::<f32>() < 0.02
|
|
|
|
{
|
|
|
|
inputs.roll.set_state(true);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::StoneGolemBoss => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = Vec2::zero();
|
|
|
|
inputs.primary.set_state(true);
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
if can_see_tgt(&*terrain, pos, tgt_pos, dist_sqrd) {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
if *powerup > 5.0 {
|
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
*powerup = 0.0;
|
|
|
|
} else {
|
|
|
|
*powerup += dt.0;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::CircleCharge {
|
|
|
|
radius,
|
|
|
|
circle_time,
|
|
|
|
} => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (MIN_ATTACK_DIST * scale).powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
&& thread_rng().gen_bool(0.5)
|
|
|
|
{
|
|
|
|
inputs.move_dir = Vec2::zero();
|
|
|
|
inputs.primary.set_state(true);
|
|
|
|
} else if dist_sqrd
|
2020-11-25 00:28:24 +00:00
|
|
|
< (radius as f32 * MIN_ATTACK_DIST * scale).powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
|
|
|
inputs.move_dir = (pos.0 - tgt_pos.0)
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::unit_y());
|
|
|
|
} else if dist_sqrd
|
|
|
|
< ((radius as f32 + 1.0) * MIN_ATTACK_DIST * scale)
|
2020-11-25 00:28:24 +00:00
|
|
|
.powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
&& dist_sqrd
|
2020-11-25 00:28:24 +00:00
|
|
|
> (radius as f32 * MIN_ATTACK_DIST * scale).powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
|
|
|
if *powerup < circle_time as f32 {
|
|
|
|
inputs.move_dir = (tgt_pos.0 - pos.0)
|
|
|
|
.xy()
|
|
|
|
.rotated_z(0.47 * PI)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::unit_y());
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else if *powerup < circle_time as f32 + 0.5 {
|
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else if *powerup < 2.0 * circle_time as f32 + 0.5 {
|
|
|
|
inputs.move_dir = (tgt_pos.0 - pos.0)
|
|
|
|
.xy()
|
|
|
|
.rotated_z(-0.47 * PI)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::unit_y());
|
2020-11-11 04:36:40 +00:00
|
|
|
*powerup += dt.0;
|
2020-11-23 19:27:18 +00:00
|
|
|
} else if *powerup < 2.0 * circle_time as f32 + 1.0 {
|
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else {
|
|
|
|
*powerup = 0.0;
|
2020-11-11 04:36:40 +00:00
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::QuadLowRanged => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (5.0 * MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = (tgt_pos.0 - pos.0)
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::unit_y());
|
2020-11-22 08:09:43 +00:00
|
|
|
inputs.primary.set_state(true);
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
if can_see_tgt(&*terrain, pos, tgt_pos, dist_sqrd) {
|
|
|
|
if *powerup > 5.0 {
|
|
|
|
*powerup = 0.0;
|
|
|
|
} else if *powerup > 2.5 {
|
|
|
|
inputs.move_dir = (tgt_pos.0 - pos.0)
|
|
|
|
.xy()
|
|
|
|
.rotated_z(1.75 * PI)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else {
|
|
|
|
inputs.move_dir = (tgt_pos.0 - pos.0)
|
|
|
|
.xy()
|
|
|
|
.rotated_z(0.25 * PI)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
*powerup += dt.0;
|
|
|
|
}
|
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::TailSlap => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (1.5 * MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-22 08:09:43 +00:00
|
|
|
if *powerup > 4.0 {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.primary.set_state(false);
|
2020-11-22 08:09:43 +00:00
|
|
|
*powerup = 0.0;
|
2020-11-23 19:27:18 +00:00
|
|
|
} else if *powerup > 1.0 {
|
|
|
|
inputs.primary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
2020-11-22 08:09:43 +00:00
|
|
|
} else {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.secondary.set_state(true);
|
2020-11-22 08:09:43 +00:00
|
|
|
*powerup += dt.0;
|
|
|
|
}
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = (tgt_pos.0 - pos.0)
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::unit_y())
|
|
|
|
* 0.1;
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
2020-07-13 22:23:44 +00:00
|
|
|
},
|
2020-11-23 19:27:18 +00:00
|
|
|
Tactic::QuadLowQuick => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (1.5 * MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = Vec2::zero();
|
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
} else if dist_sqrd
|
2020-11-25 00:28:24 +00:00
|
|
|
< (3.0 * MIN_ATTACK_DIST * scale).powi(2)
|
|
|
|
&& dist_sqrd > (2.0 * MIN_ATTACK_DIST * scale).powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
|
|
|
inputs.primary.set_state(true);
|
|
|
|
inputs.move_dir = (tgt_pos.0 - pos.0)
|
|
|
|
.xy()
|
|
|
|
.rotated_z(-0.47 * PI)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::unit_y());
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
2020-10-17 01:27:57 +00:00
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::QuadLowBasic => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (1.5 * MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = Vec2::zero();
|
|
|
|
if *powerup > 5.0 {
|
|
|
|
*powerup = 0.0;
|
|
|
|
} else if *powerup > 2.0 {
|
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else {
|
|
|
|
inputs.primary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
2020-10-17 01:27:57 +00:00
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::QuadMedJump => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (1.5 * MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = Vec2::zero();
|
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
} else if dist_sqrd
|
2020-11-25 00:28:24 +00:00
|
|
|
< (5.0 * MIN_ATTACK_DIST * scale).powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
|
|
|
inputs.ability3.set_state(true);
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
if can_see_tgt(&*terrain, pos, tgt_pos, dist_sqrd) {
|
|
|
|
inputs.primary.set_state(true);
|
2020-11-13 06:13:37 +00:00
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
} else {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
2020-11-13 06:13:37 +00:00
|
|
|
}
|
2020-11-23 19:27:18 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::QuadMedBasic => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = Vec2::zero();
|
|
|
|
if *powerup < 2.0 {
|
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else if *powerup < 3.0 {
|
|
|
|
inputs.primary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else {
|
|
|
|
*powerup = 0.0;
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
2020-10-17 01:27:57 +00:00
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
2020-10-17 01:27:57 +00:00
|
|
|
}
|
2020-11-23 19:27:18 +00:00
|
|
|
},
|
|
|
|
Tactic::Lavadrake => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (2.5 * MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = Vec2::zero();
|
|
|
|
inputs.secondary.set_state(true);
|
|
|
|
} else if dist_sqrd
|
2020-11-25 00:28:24 +00:00
|
|
|
< (7.0 * MIN_ATTACK_DIST * scale).powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
|
|
|
if *powerup < 2.0 {
|
|
|
|
inputs.move_dir = (tgt_pos.0 - pos.0)
|
|
|
|
.xy()
|
|
|
|
.rotated_z(0.47 * PI)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::unit_y());
|
|
|
|
inputs.primary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else if *powerup < 4.0 {
|
|
|
|
inputs.move_dir = (tgt_pos.0 - pos.0)
|
|
|
|
.xy()
|
|
|
|
.rotated_z(-0.47 * PI)
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::unit_y());
|
|
|
|
inputs.primary.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else if *powerup < 6.0 {
|
|
|
|
inputs.ability3.set_state(true);
|
|
|
|
*powerup += dt.0;
|
|
|
|
} else {
|
|
|
|
*powerup = 0.0;
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
Tactic::Theropod => {
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < (2.0 * MIN_ATTACK_DIST * scale).powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
inputs.move_dir = Vec2::zero();
|
|
|
|
inputs.primary.set_state(true);
|
2020-11-25 00:28:24 +00:00
|
|
|
} else if dist_sqrd < MAX_CHASE_DIST.powi(2)
|
|
|
|
|| (dist_sqrd < SIGHT_DIST.powi(2) && !*been_close)
|
2020-11-23 19:27:18 +00:00
|
|
|
{
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd < MAX_CHASE_DIST.powi(2) {
|
2020-11-23 19:27:18 +00:00
|
|
|
*been_close = true;
|
|
|
|
}
|
|
|
|
if let Some((bearing, speed)) = chaser.chase(
|
|
|
|
&*terrain,
|
|
|
|
pos.0,
|
|
|
|
vel.0,
|
|
|
|
tgt_pos.0,
|
|
|
|
TraversalConfig {
|
|
|
|
min_tgt_dist: 1.25,
|
|
|
|
..traversal_config
|
|
|
|
},
|
|
|
|
) {
|
|
|
|
inputs.move_dir = bearing
|
|
|
|
.xy()
|
|
|
|
.try_normalized()
|
|
|
|
.unwrap_or(Vec2::zero())
|
|
|
|
* speed;
|
|
|
|
inputs.jump.set_state(bearing.z > 1.5);
|
|
|
|
inputs.move_z = bearing.z;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
do_idle = true;
|
|
|
|
}
|
|
|
|
},
|
2020-01-27 16:18:36 +00:00
|
|
|
}
|
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-11-12 21:31:28 +00:00
|
|
|
agent.activity = Activity::Idle {
|
|
|
|
bearing: Vec2::zero(),
|
|
|
|
chaser: Chaser::default(),
|
|
|
|
};
|
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-10-31 22:34:08 +00:00
|
|
|
let closest_entity = (&entities, &positions, &healths, alignments.maybe(), char_states.maybe())
|
2020-01-26 00:06:03 +00:00
|
|
|
.join()
|
2020-10-31 22:34:08 +00:00
|
|
|
.filter(|(e, e_pos, e_health, e_alignment, char_state)| {
|
2020-11-03 04:09:38 +00:00
|
|
|
let mut search_dist = SEARCH_DIST;
|
|
|
|
let mut listen_dist = LISTEN_DIST;
|
|
|
|
if char_state.map_or(false, |c_s| c_s.is_stealthy()) {
|
|
|
|
// TODO: make sneak more effective based on a stat like e_stats.fitness
|
|
|
|
search_dist *= SNEAK_COEFFICIENT;
|
|
|
|
listen_dist *= SNEAK_COEFFICIENT;
|
|
|
|
}
|
2020-11-25 00:28:24 +00:00
|
|
|
((e_pos.0.distance_squared(pos.0) < search_dist.powi(2) &&
|
2020-04-19 15:34:23 +00:00
|
|
|
// 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
|
2020-11-25 00:28:24 +00:00
|
|
|
|| e_pos.0.distance_squared(pos.0) < listen_dist.powi(2))
|
2020-01-26 00:06:03 +00:00
|
|
|
&& *e != entity
|
2020-10-31 22:34:08 +00:00
|
|
|
&& !e_health.is_dead
|
2020-01-26 00:06:03 +00:00
|
|
|
&& 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?
|
2020-11-03 04:09:38 +00:00
|
|
|
.filter(|(_, e_pos, _, _, _)| terrain
|
2020-04-19 11:50:25 +00:00
|
|
|
.ray(pos.0 + Vec3::unit_z(), e_pos.0 + Vec3::unit_z())
|
2020-09-26 13:55:01 +00:00
|
|
|
.until(Block::is_opaque)
|
2020-04-19 11:50:25 +00:00
|
|
|
.cast()
|
|
|
|
.0 >= e_pos.0.distance(pos.0))
|
2020-11-03 04:09:38 +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-10-31 22:34:08 +00:00
|
|
|
if let Some(my_health) = healths.get(entity) {
|
2020-01-25 18:49:47 +00:00
|
|
|
// Only if the attack was recent
|
2020-10-31 22:34:08 +00:00
|
|
|
if my_health.last_change.0 < 3.0 {
|
2020-11-05 01:21:42 +00:00
|
|
|
if let comp::HealthSource::Damage { by: Some(by), .. } =
|
2020-10-31 22:34:08 +00:00
|
|
|
my_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-10-31 22:34:08 +00:00
|
|
|
if healths.get(attacker).map_or(false, |a| !a.is_dead) {
|
2020-07-30 19:26:49 +00:00
|
|
|
match agent.activity {
|
|
|
|
Activity::Attack { target, .. } if target == attacker => {},
|
|
|
|
_ => {
|
|
|
|
if agent.can_speak {
|
2020-08-12 14:10:12 +00:00
|
|
|
let msg =
|
|
|
|
"npc.speech.villager_under_attack".to_string();
|
2020-10-07 02:23:20 +00:00
|
|
|
event_emitter.emit(ServerEvent::Chat(
|
2020-08-12 14:10:12 +00:00
|
|
|
UnresolvedChatMsg::npc(*uid, msg),
|
|
|
|
));
|
2020-07-30 19:26:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
agent.activity = Activity::Attack {
|
|
|
|
target: attacker,
|
|
|
|
chaser: Chaser::default(),
|
|
|
|
time: time.0,
|
|
|
|
been_close: false,
|
|
|
|
powerup: 0.0,
|
|
|
|
};
|
|
|
|
},
|
2020-05-26 02:45:13 +00:00
|
|
|
}
|
|
|
|
}
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Follow owner if we're too far, or if they're under attack
|
2020-04-26 17:03:19 +00:00
|
|
|
if let Some(Alignment::Owned(owner)) = alignment {
|
2020-07-07 00:01:39 +00:00
|
|
|
(|| {
|
|
|
|
let owner = uid_allocator.retrieve_entity_internal(owner.id())?;
|
|
|
|
|
|
|
|
let owner_pos = positions.get(owner)?;
|
2020-01-26 12:47:41 +00:00
|
|
|
let dist_sqrd = pos.0.distance_squared(owner_pos.0);
|
2020-11-25 00:28:24 +00:00
|
|
|
if dist_sqrd > MAX_FOLLOW_DIST.powi(2) && !agent.activity.is_follow() {
|
2020-07-04 00:17:51 +00:00
|
|
|
agent.activity = Activity::Follow {
|
|
|
|
target: owner,
|
|
|
|
chaser: Chaser::default(),
|
|
|
|
};
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Attack owner's attacker
|
2020-10-31 22:34:08 +00:00
|
|
|
let owner_health = healths.get(owner)?;
|
|
|
|
if owner_health.last_change.0 < 5.0 && owner_health.last_change.1.amount < 0 {
|
2020-11-05 01:21:42 +00:00
|
|
|
if let comp::HealthSource::Damage { by: Some(by), .. } =
|
|
|
|
owner_health.last_change.1.cause
|
2020-07-07 00:01:39 +00:00
|
|
|
{
|
|
|
|
if !agent.activity.is_attack() {
|
|
|
|
let attacker = uid_allocator.retrieve_entity_internal(by.id())?;
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-07 00:01:39 +00:00
|
|
|
|
|
|
|
Some(())
|
|
|
|
})();
|
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
|
|
|
}
|
2020-08-07 01:59:28 +00:00
|
|
|
|
2020-08-25 12:21:25 +00:00
|
|
|
// Process group invites
|
2020-09-03 22:46:32 +00:00
|
|
|
for (_invite, /*alignment,*/ agent, controller) in
|
|
|
|
(&invites, /*&alignments,*/ &mut agents, &mut controllers).join()
|
2020-08-07 01:59:28 +00:00
|
|
|
{
|
2020-10-19 03:00:35 +00:00
|
|
|
let accept = false; // set back to "matches!(alignment, Alignment::Npc)" when we got better NPC recruitment mechanics
|
2020-08-07 01:59:28 +00:00
|
|
|
if accept {
|
|
|
|
// Clear agent comp
|
|
|
|
*agent = Agent::default();
|
|
|
|
controller
|
|
|
|
.events
|
|
|
|
.push(ControlEvent::GroupManip(GroupManip::Accept));
|
|
|
|
} else {
|
|
|
|
controller
|
|
|
|
.events
|
|
|
|
.push(ControlEvent::GroupManip(GroupManip::Decline));
|
|
|
|
}
|
|
|
|
}
|
2020-09-14 12:56:05 +00:00
|
|
|
sys_metrics.agent_ns.store(
|
|
|
|
start_time.elapsed().as_nanos() as i64,
|
|
|
|
std::sync::atomic::Ordering::Relaxed,
|
|
|
|
);
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-23 19:27:18 +00:00
|
|
|
|
|
|
|
fn can_see_tgt(terrain: &TerrainGrid, pos: &Pos, tgt_pos: &Pos, dist_sqrd: f32) -> bool {
|
|
|
|
terrain
|
|
|
|
.ray(pos.0 + Vec3::unit_z(), tgt_pos.0 + Vec3::unit_z())
|
|
|
|
.until(Block::is_opaque)
|
|
|
|
.cast()
|
|
|
|
.0
|
2020-11-25 00:28:24 +00:00
|
|
|
.powi(2)
|
2020-11-23 19:27:18 +00:00
|
|
|
>= dist_sqrd
|
|
|
|
}
|