veloren/common/src/sys/agent.rs

221 lines
8.5 KiB
Rust
Raw Normal View History

use crate::comp::{
Agent, CharacterState, Controller, MountState, MovementState::Glide, Pos, Stats,
};
use crate::{hierarchical::ChunkPath, path::Path, pathfinding::WorldPath, terrain::TerrainGrid};
use rand::{seq::SliceRandom, thread_rng};
use specs::{Entities, Join, ReadExpect, ReadStorage, System, WriteStorage};
use vek::*;
2019-06-09 19:33:20 +00:00
/// This system will allow NPCs to modify their controller
pub struct Sys;
impl<'a> System<'a> for Sys {
type SystemData = (
Entities<'a>,
ReadStorage<'a, Pos>,
ReadStorage<'a, Stats>,
ReadStorage<'a, CharacterState>,
ReadExpect<'a, TerrainGrid>,
WriteStorage<'a, Agent>,
2019-06-09 14:20:20 +00:00
WriteStorage<'a, Controller>,
ReadStorage<'a, MountState>,
);
2019-08-04 08:21:29 +00:00
fn run(
&mut self,
(
entities,
positions,
stats,
character_states,
terrain,
mut agents,
mut controllers,
mount_states,
): Self::SystemData,
2019-08-04 08:21:29 +00:00
) {
for (entity, pos, agent, controller, mount_state) in (
&entities,
&positions,
&mut agents,
&mut controllers,
mount_states.maybe(),
)
.join()
{
// Skip mounted entities
if mount_state
.map(|ms| {
if let MountState::Unmounted = ms {
false
} else {
true
}
})
.unwrap_or(false)
{
continue;
}
controller.reset();
let mut inputs = &mut controller.inputs;
match agent {
Agent::Traveler { path } => {
let mut new_path: Option<WorldPath> = None;
let is_destination = |cur_pos: Vec3<i32>, dest: Vec3<i32>| {
Vec2::<i32>::from(cur_pos) == Vec2::<i32>::from(dest)
};
let found_destination = || {
const MAX_TRAVEL_DIST: f32 = 200.0;
let new_dest = Vec3::new(rand::random::<f32>(), rand::random::<f32>(), 0.0)
* MAX_TRAVEL_DIST;
2019-12-29 20:58:21 +00:00
new_path = Some(
ChunkPath::new(&*terrain, pos.0, pos.0 + new_dest)
.get_worldpath(&*terrain)
.unwrap(),
2019-12-29 20:58:21 +00:00
);
};
path.move_along_path(
&*terrain,
pos,
&mut inputs,
is_destination,
found_destination,
);
if let Some(new_path) = new_path {
*path = new_path;
}
}
Agent::Wanderer(bearing) => {
*bearing += Vec2::new(rand::random::<f32>() - 0.5, rand::random::<f32>() - 0.5)
* 0.1
- *bearing * 0.01
- pos.0 * 0.0002;
if bearing.magnitude_squared() > 0.001 {
inputs.move_dir = bearing.normalized();
}
}
Agent::Pet { target, chaser } => {
// Run towards target.
if let Some(tgt_pos) = positions.get(*target) {
if let Some(bearing) = chaser.chase(&*terrain, tgt_pos.0, pos.0) {
inputs.move_dir = Vec2::from(bearing).normalized();
inputs.jump.set_state(bearing.z > 0.9);
}
/*
const HAPPY_DIST: i32 = 4;
let plot_path = if let Some(dir) = route.traverse(&*terrain, pos.0) {
inputs.move_dir = Vec2::from(dir).normalized();
inputs.jump.set_state(dir.z > 0.9);
// Sometimes recalculate to avoid getting stuck
rand::random::<f32>() < 0.005
} else {
true
};
if plot_path {
let path: Path = WorldPath::find(&*terrain, pos.0, tgt_pos.0)
.ok()
.and_then(|wp| wp.path.map(|nodes| nodes.into_iter().rev()))
.into_iter()
.flatten()
.collect();
*route = path.into();
}
*/
} else {
inputs.move_dir = Vec2::zero();
}
}
Agent::Enemy { bearing, target } => {
2020-01-20 16:42:35 +00:00
const SIGHT_DIST: f32 = 18.0;
const MIN_ATTACK_DIST: f32 = 3.25;
let mut choose_new = false;
2019-08-02 20:35:03 +00:00
if let Some((Some(target_pos), Some(target_stats), Some(target_character))) =
2019-08-04 08:21:29 +00:00
target.map(|target| {
(
positions.get(target),
stats.get(target),
character_states.get(target),
2019-08-04 08:21:29 +00:00
)
})
{
inputs.look_dir = target_pos.0 - pos.0;
2019-08-24 19:12:54 +00:00
let dist = Vec2::<f32>::from(target_pos.0 - pos.0).magnitude();
if target_stats.is_dead {
choose_new = true;
} else if dist < 0.001 {
// Probably can only happen when entities are at a different z-level
// since at the same level repulsion would keep them apart.
// Distinct from the first if block since we may want to change the
// behavior for this case.
choose_new = true;
} else if dist < MIN_ATTACK_DIST {
2019-09-07 09:46:30 +00:00
// Fight (and slowly move closer)
inputs.move_dir =
2019-09-07 09:46:30 +00:00
Vec2::<f32>::from(target_pos.0 - pos.0).normalized() * 0.01;
2019-11-29 15:20:35 +00:00
inputs.primary.set_state(true);
} else if dist < SIGHT_DIST {
inputs.move_dir =
Vec2::<f32>::from(target_pos.0 - pos.0).normalized() * 0.96;
2019-08-03 12:31:40 +00:00
if rand::random::<f32>() < 0.02 {
2019-11-29 15:20:35 +00:00
inputs.roll.set_state(true);
2019-08-03 12:31:40 +00:00
}
2019-08-04 08:21:29 +00:00
if target_character.movement == Glide && target_pos.0.z > pos.0.z + 5.0
{
2019-11-29 15:20:35 +00:00
inputs.glide.set_state(true);
inputs.jump.set_state(true);
2019-08-04 08:21:29 +00:00
}
} else {
choose_new = true;
}
} else {
*bearing +=
Vec2::new(rand::random::<f32>() - 0.5, rand::random::<f32>() - 0.5)
* 0.1
- *bearing * 0.005;
inputs.move_dir = if bearing.magnitude_squared() > 0.001 {
bearing.normalized()
} else {
Vec2::zero()
};
choose_new = true;
}
2019-08-02 19:41:38 +00:00
if choose_new && rand::random::<f32>() < 0.1 {
let entities = (&entities, &positions, &stats)
.join()
.filter(|(e, e_pos, e_stats)| {
2019-08-04 07:58:41 +00:00
(e_pos.0 - pos.0).magnitude() < SIGHT_DIST
2019-08-02 18:22:51 +00:00
&& *e != entity
&& !e_stats.is_dead
})
.map(|(e, _, _)| e)
.collect::<Vec<_>>();
let mut rng = thread_rng();
*target = (&entities).choose(&mut rng).cloned();
}
}
}
debug_assert!(inputs.move_dir.map(|e| !e.is_nan()).reduce_and());
debug_assert!(inputs.look_dir.map(|e| !e.is_nan()).reduce_and());
}
}
}