2020-08-13 19:22:47 +00:00
|
|
|
use crate::{
|
2020-09-16 03:17:56 +00:00
|
|
|
comp::{humanoid, quadruped_low, quadruped_medium, quadruped_small, Body},
|
2020-08-13 19:22:47 +00:00
|
|
|
path::Chaser,
|
2020-11-12 21:31:28 +00:00
|
|
|
rtsim::RtSimController,
|
2020-11-23 15:39:03 +00:00
|
|
|
sync::Uid,
|
2020-08-13 19:22:47 +00:00
|
|
|
};
|
2020-04-26 17:03:19 +00:00
|
|
|
use specs::{Component, Entity as EcsEntity};
|
2020-07-06 05:56:02 +00:00
|
|
|
use specs_idvs::IdvStorage;
|
2019-07-29 19:54:58 +00:00
|
|
|
use vek::*;
|
2020-04-26 17:03:19 +00:00
|
|
|
#[derive(Copy, Clone, Debug, PartialEq)]
|
2020-01-24 21:24:57 +00:00
|
|
|
pub enum Alignment {
|
2020-05-26 02:45:13 +00:00
|
|
|
/// Wild animals and gentle giants
|
2020-01-24 21:24:57 +00:00
|
|
|
Wild,
|
2020-05-26 02:45:13 +00:00
|
|
|
/// Dungeon cultists and bandits
|
2020-01-24 21:24:57 +00:00
|
|
|
Enemy,
|
2020-05-26 02:45:13 +00:00
|
|
|
/// Friendly folk in villages
|
2020-01-24 21:24:57 +00:00
|
|
|
Npc,
|
2020-05-26 02:45:13 +00:00
|
|
|
/// Farm animals and pets of villagers
|
|
|
|
Tame,
|
|
|
|
/// Pets you've tamed with a collar
|
2020-07-07 00:01:39 +00:00
|
|
|
Owned(Uid),
|
2020-08-13 19:22:47 +00:00
|
|
|
/// Passive objects like training dummies
|
|
|
|
Passive,
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
|
|
|
|
2020-01-24 21:24:57 +00:00
|
|
|
impl Alignment {
|
2020-01-27 15:51:07 +00:00
|
|
|
// Always attacks
|
2020-01-24 21:24:57 +00:00
|
|
|
pub fn hostile_towards(self, other: Alignment) -> bool {
|
|
|
|
match (self, other) {
|
2020-01-27 15:51:07 +00:00
|
|
|
(Alignment::Enemy, Alignment::Enemy) => false,
|
2020-08-13 19:22:47 +00:00
|
|
|
(Alignment::Enemy, Alignment::Wild) => false,
|
|
|
|
(Alignment::Wild, Alignment::Enemy) => false,
|
|
|
|
(Alignment::Wild, Alignment::Wild) => false,
|
2020-08-14 21:55:03 +00:00
|
|
|
(Alignment::Npc, Alignment::Wild) => false,
|
|
|
|
(Alignment::Npc, Alignment::Enemy) => true,
|
2020-01-27 15:51:07 +00:00
|
|
|
(_, Alignment::Enemy) => true,
|
2020-08-13 19:22:47 +00:00
|
|
|
(Alignment::Enemy, _) => true,
|
2020-01-27 15:51:07 +00:00
|
|
|
_ => false,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Never attacks
|
|
|
|
pub fn passive_towards(self, other: Alignment) -> bool {
|
|
|
|
match (self, other) {
|
2020-04-19 11:50:25 +00:00
|
|
|
(Alignment::Enemy, Alignment::Enemy) => true,
|
2020-01-27 15:51:07 +00:00
|
|
|
(Alignment::Owned(a), Alignment::Owned(b)) if a == b => true,
|
2020-05-26 02:45:13 +00:00
|
|
|
(Alignment::Npc, Alignment::Npc) => true,
|
|
|
|
(Alignment::Npc, Alignment::Tame) => true,
|
2020-08-13 19:22:47 +00:00
|
|
|
(Alignment::Enemy, Alignment::Wild) => true,
|
|
|
|
(Alignment::Wild, Alignment::Enemy) => true,
|
2020-05-26 02:45:13 +00:00
|
|
|
(Alignment::Tame, Alignment::Npc) => true,
|
|
|
|
(Alignment::Tame, Alignment::Tame) => true,
|
2020-08-13 19:22:47 +00:00
|
|
|
(_, Alignment::Passive) => true,
|
2020-01-27 15:51:07 +00:00
|
|
|
_ => false,
|
2019-08-02 18:56:37 +00:00
|
|
|
}
|
|
|
|
}
|
2020-07-06 20:18:30 +00:00
|
|
|
|
|
|
|
// TODO: Remove this hack
|
|
|
|
pub fn is_friendly_to_players(&self) -> bool {
|
2020-08-17 09:08:11 +00:00
|
|
|
matches!(self, Alignment::Npc | Alignment::Tame | Alignment::Owned(_))
|
2020-07-06 20:18:30 +00:00
|
|
|
}
|
2020-01-24 21:24:57 +00:00
|
|
|
}
|
2020-01-22 03:12:17 +00:00
|
|
|
|
2020-01-24 21:24:57 +00:00
|
|
|
impl Component for Alignment {
|
2020-04-26 17:03:19 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2020-01-24 21:24:57 +00:00
|
|
|
}
|
|
|
|
|
2020-07-29 19:58:14 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct Psyche {
|
2020-08-13 19:22:47 +00:00
|
|
|
pub aggro: f32, // 0.0 = always flees, 1.0 = always attacks, 0.5 = flee at 50% health
|
2020-07-29 19:58:14 +00:00
|
|
|
}
|
2020-11-12 21:31:28 +00:00
|
|
|
|
2020-07-29 19:58:14 +00:00
|
|
|
impl<'a> From<&'a Body> for Psyche {
|
|
|
|
fn from(body: &'a Body) -> Self {
|
|
|
|
Self {
|
|
|
|
aggro: match body {
|
2020-08-13 19:22:47 +00:00
|
|
|
Body::Humanoid(humanoid) => match humanoid.species {
|
|
|
|
humanoid::Species::Danari => 0.9,
|
2020-11-22 08:09:43 +00:00
|
|
|
humanoid::Species::Dwarf => 0.8,
|
|
|
|
humanoid::Species::Elf => 0.7,
|
|
|
|
humanoid::Species::Human => 0.6,
|
|
|
|
humanoid::Species::Orc => 0.9,
|
|
|
|
humanoid::Species::Undead => 0.9,
|
2020-08-13 19:22:47 +00:00
|
|
|
},
|
|
|
|
Body::QuadrupedSmall(quadruped_small) => match quadruped_small.species {
|
2020-08-14 01:22:25 +00:00
|
|
|
quadruped_small::Species::Pig => 0.5,
|
2020-11-22 08:09:43 +00:00
|
|
|
quadruped_small::Species::Fox => 0.3,
|
2020-08-14 01:22:25 +00:00
|
|
|
quadruped_small::Species::Sheep => 0.5,
|
2020-11-22 08:09:43 +00:00
|
|
|
quadruped_small::Species::Boar => 0.8,
|
2020-08-13 19:22:47 +00:00
|
|
|
quadruped_small::Species::Jackalope => 0.4,
|
2020-11-22 08:09:43 +00:00
|
|
|
quadruped_small::Species::Skunk => 0.6,
|
2020-08-13 19:22:47 +00:00
|
|
|
quadruped_small::Species::Cat => 0.2,
|
2020-11-22 08:09:43 +00:00
|
|
|
quadruped_small::Species::Batfox => 0.6,
|
2020-08-13 19:22:47 +00:00
|
|
|
quadruped_small::Species::Raccoon => 0.4,
|
2020-11-22 08:09:43 +00:00
|
|
|
quadruped_small::Species::Quokka => 0.4,
|
2020-08-13 19:22:47 +00:00
|
|
|
quadruped_small::Species::Dodarock => 0.9,
|
|
|
|
quadruped_small::Species::Holladon => 1.0,
|
|
|
|
quadruped_small::Species::Hyena => 0.4,
|
|
|
|
quadruped_small::Species::Rabbit => 0.1,
|
|
|
|
quadruped_small::Species::Truffler => 0.8,
|
2020-11-22 08:09:43 +00:00
|
|
|
quadruped_small::Species::Frog => 0.4,
|
2020-11-17 00:13:59 +00:00
|
|
|
quadruped_small::Species::Hare => 0.2,
|
2020-11-22 08:09:43 +00:00
|
|
|
_ => 0.0,
|
2020-08-13 19:22:47 +00:00
|
|
|
},
|
|
|
|
Body::QuadrupedMedium(quadruped_medium) => match quadruped_medium.species {
|
2020-11-22 08:09:43 +00:00
|
|
|
quadruped_medium::Species::Tuskram => 0.7,
|
2020-08-13 19:22:47 +00:00
|
|
|
quadruped_medium::Species::Frostfang => 0.9,
|
2020-11-22 08:09:43 +00:00
|
|
|
quadruped_medium::Species::Mouflon => 0.7,
|
2020-08-13 19:22:47 +00:00
|
|
|
quadruped_medium::Species::Catoblepas => 0.8,
|
2020-09-20 15:58:54 +00:00
|
|
|
quadruped_medium::Species::Deer => 0.6,
|
|
|
|
quadruped_medium::Species::Hirdrasil => 0.7,
|
2020-11-17 21:54:12 +00:00
|
|
|
quadruped_medium::Species::Donkey => 0.7,
|
|
|
|
quadruped_medium::Species::Camel => 0.7,
|
|
|
|
quadruped_medium::Species::Zebra => 0.7,
|
|
|
|
quadruped_medium::Species::Antelope => 0.6,
|
2020-11-22 08:09:43 +00:00
|
|
|
quadruped_medium::Species::Horse => 0.7,
|
|
|
|
_ => 0.5,
|
2020-08-13 19:22:47 +00:00
|
|
|
},
|
|
|
|
Body::QuadrupedLow(quadruped_low) => match quadruped_low.species {
|
2020-11-22 08:09:43 +00:00
|
|
|
quadruped_low::Species::Salamander => 0.7,
|
|
|
|
quadruped_low::Species::Monitor => 0.7,
|
2020-08-13 19:22:47 +00:00
|
|
|
quadruped_low::Species::Asp => 0.9,
|
2020-11-22 08:09:43 +00:00
|
|
|
quadruped_low::Species::Pangolin => 0.4,
|
|
|
|
_ => 0.6,
|
2020-08-13 19:22:47 +00:00
|
|
|
},
|
2020-11-22 08:09:43 +00:00
|
|
|
Body::BirdMedium(_) => 0.5,
|
2020-08-13 19:22:47 +00:00
|
|
|
Body::BirdSmall(_) => 0.4,
|
2020-07-29 19:58:14 +00:00
|
|
|
Body::FishMedium(_) => 0.15,
|
|
|
|
Body::FishSmall(_) => 0.0,
|
|
|
|
Body::BipedLarge(_) => 1.0,
|
2020-08-13 19:22:47 +00:00
|
|
|
Body::Object(_) => 1.0,
|
2020-07-29 19:58:14 +00:00
|
|
|
Body::Golem(_) => 1.0,
|
2020-09-18 02:58:02 +00:00
|
|
|
Body::Theropod(_) => 1.0,
|
2020-07-29 19:58:14 +00:00
|
|
|
Body::Dragon(_) => 1.0,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-24 21:24:57 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
|
|
|
pub struct Agent {
|
2020-11-12 21:31:28 +00:00
|
|
|
pub rtsim_controller: RtSimController,
|
2020-01-24 21:24:57 +00:00
|
|
|
pub patrol_origin: Option<Vec3<f32>>,
|
2020-01-25 18:49:47 +00:00
|
|
|
pub activity: Activity,
|
2020-05-26 02:45:13 +00:00
|
|
|
/// Does the agent talk when e.g. hit by the player
|
|
|
|
// TODO move speech patterns into a Behavior component
|
|
|
|
pub can_speak: bool,
|
2020-07-29 19:58:14 +00:00
|
|
|
pub psyche: Psyche,
|
2020-01-24 21:24:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Agent {
|
2020-01-25 02:15:15 +00:00
|
|
|
pub fn with_patrol_origin(mut self, origin: Vec3<f32>) -> Self {
|
|
|
|
self.patrol_origin = Some(origin);
|
|
|
|
self
|
|
|
|
}
|
2020-05-26 02:45:13 +00:00
|
|
|
|
2020-11-12 21:31:28 +00:00
|
|
|
pub fn new(patrol_origin: Option<Vec3<f32>>, can_speak: bool, body: &Body) -> Self {
|
2020-05-26 02:45:13 +00:00
|
|
|
Agent {
|
|
|
|
patrol_origin,
|
|
|
|
can_speak,
|
2020-07-29 19:58:14 +00:00
|
|
|
psyche: Psyche::from(body),
|
2020-05-26 02:45:13 +00:00
|
|
|
..Default::default()
|
|
|
|
}
|
|
|
|
}
|
2019-08-02 18:56:37 +00:00
|
|
|
}
|
|
|
|
|
2019-04-16 21:06:33 +00:00
|
|
|
impl Component for Agent {
|
2020-07-06 05:56:02 +00:00
|
|
|
type Storage = IdvStorage<Self>;
|
2019-04-16 21:06:33 +00:00
|
|
|
}
|
2020-01-25 18:49:47 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug)]
|
|
|
|
pub enum Activity {
|
2020-11-12 21:31:28 +00:00
|
|
|
Idle {
|
|
|
|
bearing: Vec2<f32>,
|
|
|
|
chaser: Chaser,
|
|
|
|
},
|
2020-07-04 00:17:51 +00:00
|
|
|
Follow {
|
|
|
|
target: EcsEntity,
|
|
|
|
chaser: Chaser,
|
|
|
|
},
|
2020-01-27 15:51:07 +00:00
|
|
|
Attack {
|
|
|
|
target: EcsEntity,
|
|
|
|
chaser: Chaser,
|
|
|
|
time: f64,
|
|
|
|
been_close: bool,
|
2020-04-18 20:26:43 +00:00
|
|
|
powerup: f32,
|
2020-01-27 15:51:07 +00:00
|
|
|
},
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Activity {
|
2020-08-17 09:08:11 +00:00
|
|
|
pub fn is_follow(&self) -> bool { matches!(self, Activity::Follow { .. }) }
|
2020-01-25 18:49:47 +00:00
|
|
|
|
2020-08-17 09:08:11 +00:00
|
|
|
pub fn is_attack(&self) -> bool { matches!(self, Activity::Attack { .. }) }
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Activity {
|
2020-11-12 21:31:28 +00:00
|
|
|
fn default() -> Self {
|
|
|
|
Activity::Idle {
|
|
|
|
bearing: Vec2::zero(),
|
|
|
|
chaser: Chaser::default(),
|
|
|
|
}
|
|
|
|
}
|
2020-01-25 18:49:47 +00:00
|
|
|
}
|