2020-11-11 23:59:09 +00:00
|
|
|
// We'd like to not have this file in `common`, but sadly there are
|
2020-11-12 21:31:28 +00:00
|
|
|
// things in `common` that require it (currently, `ServerEvent` and
|
|
|
|
// `Agent`). When possible, this should be moved to the `rtsim`
|
|
|
|
// module in `server`.
|
2020-11-11 23:59:09 +00:00
|
|
|
|
2023-04-02 17:37:38 +00:00
|
|
|
use crate::character::CharacterId;
|
2023-03-31 22:20:52 +00:00
|
|
|
use rand::{seq::IteratorRandom, Rng};
|
2023-03-22 09:59:34 +00:00
|
|
|
use serde::{Deserialize, Serialize};
|
2020-11-11 23:59:09 +00:00
|
|
|
use specs::Component;
|
2023-04-03 17:03:07 +00:00
|
|
|
use std::{borrow::Cow, collections::VecDeque};
|
2023-03-22 12:59:55 +00:00
|
|
|
use strum::{EnumIter, IntoEnumIterator};
|
2020-11-12 21:31:28 +00:00
|
|
|
use vek::*;
|
2020-11-11 23:59:09 +00:00
|
|
|
|
2022-08-11 10:05:01 +00:00
|
|
|
slotmap::new_key_type! { pub struct NpcId; }
|
2020-11-11 23:59:09 +00:00
|
|
|
|
2023-01-14 22:49:43 +00:00
|
|
|
slotmap::new_key_type! { pub struct VehicleId; }
|
|
|
|
|
2022-08-11 14:44:57 +00:00
|
|
|
slotmap::new_key_type! { pub struct SiteId; }
|
|
|
|
|
2022-08-15 14:00:39 +00:00
|
|
|
slotmap::new_key_type! { pub struct FactionId; }
|
|
|
|
|
2020-11-11 23:59:09 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
2022-08-11 10:05:01 +00:00
|
|
|
pub struct RtSimEntity(pub NpcId);
|
2020-11-11 23:59:09 +00:00
|
|
|
|
|
|
|
impl Component for RtSimEntity {
|
2022-08-08 04:38:20 +00:00
|
|
|
type Storage = specs::VecStorage<Self>;
|
2020-11-11 23:59:09 +00:00
|
|
|
}
|
2020-11-12 21:31:28 +00:00
|
|
|
|
2023-04-02 17:37:38 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Serialize, Deserialize, PartialEq, Eq)]
|
|
|
|
pub enum Actor {
|
|
|
|
Npc(NpcId),
|
|
|
|
Character(CharacterId),
|
2023-01-14 22:49:43 +00:00
|
|
|
}
|
|
|
|
|
2023-04-02 17:37:38 +00:00
|
|
|
impl Actor {
|
|
|
|
pub fn npc(&self) -> Option<NpcId> {
|
|
|
|
match self {
|
|
|
|
Actor::Npc(id) => Some(*id),
|
|
|
|
Actor::Character(_) => None,
|
|
|
|
}
|
|
|
|
}
|
2021-03-16 01:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-04-02 17:37:38 +00:00
|
|
|
#[derive(Copy, Clone, Debug)]
|
|
|
|
pub struct RtSimVehicle(pub VehicleId);
|
2021-03-16 01:30:35 +00:00
|
|
|
|
2023-04-02 17:37:38 +00:00
|
|
|
impl Component for RtSimVehicle {
|
|
|
|
type Storage = specs::VecStorage<Self>;
|
2021-03-16 01:30:35 +00:00
|
|
|
}
|
|
|
|
|
2023-03-22 12:59:55 +00:00
|
|
|
#[derive(EnumIter, Clone, Copy)]
|
|
|
|
pub enum PersonalityTrait {
|
|
|
|
Open,
|
|
|
|
Adventurous,
|
|
|
|
Closed,
|
|
|
|
Conscientious,
|
|
|
|
Busybody,
|
|
|
|
Unconscientious,
|
|
|
|
Extroverted,
|
|
|
|
Introverted,
|
|
|
|
Agreeable,
|
|
|
|
Sociable,
|
|
|
|
Disagreeable,
|
|
|
|
Neurotic,
|
|
|
|
Seeker,
|
|
|
|
Worried,
|
|
|
|
SadLoner,
|
|
|
|
Stable,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Serialize, Deserialize, Clone, Copy, Debug)]
|
|
|
|
pub struct Personality {
|
|
|
|
openness: u8,
|
|
|
|
conscientiousness: u8,
|
|
|
|
extraversion: u8,
|
|
|
|
agreeableness: u8,
|
|
|
|
neuroticism: u8,
|
|
|
|
}
|
|
|
|
|
|
|
|
fn distributed(min: u8, max: u8, rng: &mut impl Rng) -> u8 {
|
|
|
|
let l = max - min;
|
2023-03-31 22:20:52 +00:00
|
|
|
min + rng.gen_range(0..=l / 3)
|
|
|
|
+ rng.gen_range(0..=l / 3 + l % 3 % 2)
|
|
|
|
+ rng.gen_range(0..=l / 3 + l % 3 / 2)
|
2023-03-22 12:59:55 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Personality {
|
|
|
|
pub const HIGH_THRESHOLD: u8 = Self::MAX - Self::LOW_THRESHOLD;
|
|
|
|
pub const LITTLE_HIGH: u8 = Self::MID + (Self::MAX - Self::MIN) / 20;
|
|
|
|
pub const LITTLE_LOW: u8 = Self::MID - (Self::MAX - Self::MIN) / 20;
|
|
|
|
pub const LOW_THRESHOLD: u8 = (Self::MAX - Self::MIN) / 5 * 2 + Self::MIN;
|
|
|
|
const MAX: u8 = 255;
|
2023-03-31 22:20:52 +00:00
|
|
|
pub const MID: u8 = (Self::MAX - Self::MIN) / 2;
|
|
|
|
const MIN: u8 = 0;
|
2023-03-22 12:59:55 +00:00
|
|
|
|
2023-03-31 22:20:52 +00:00
|
|
|
fn distributed_value(rng: &mut impl Rng) -> u8 { distributed(Self::MIN, Self::MAX, rng) }
|
2023-03-22 12:59:55 +00:00
|
|
|
|
|
|
|
pub fn random(rng: &mut impl Rng) -> Self {
|
|
|
|
Self {
|
|
|
|
openness: Self::distributed_value(rng),
|
|
|
|
conscientiousness: Self::distributed_value(rng),
|
|
|
|
extraversion: Self::distributed_value(rng),
|
|
|
|
agreeableness: Self::distributed_value(rng),
|
|
|
|
neuroticism: Self::distributed_value(rng),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn random_evil(rng: &mut impl Rng) -> Self {
|
|
|
|
Self {
|
|
|
|
openness: Self::distributed_value(rng),
|
|
|
|
extraversion: Self::distributed_value(rng),
|
|
|
|
neuroticism: Self::distributed_value(rng),
|
|
|
|
agreeableness: distributed(0, Self::LOW_THRESHOLD - 1, rng),
|
|
|
|
conscientiousness: distributed(0, Self::LOW_THRESHOLD - 1, rng),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn random_good(rng: &mut impl Rng) -> Self {
|
|
|
|
Self {
|
|
|
|
openness: Self::distributed_value(rng),
|
|
|
|
extraversion: Self::distributed_value(rng),
|
|
|
|
neuroticism: Self::distributed_value(rng),
|
|
|
|
agreeableness: Self::distributed_value(rng),
|
2023-03-31 22:20:52 +00:00
|
|
|
conscientiousness: distributed(Self::LOW_THRESHOLD, Self::MAX, rng),
|
2023-03-22 12:59:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn is(&self, trait_: PersonalityTrait) -> bool {
|
|
|
|
match trait_ {
|
|
|
|
PersonalityTrait::Open => self.openness > Personality::HIGH_THRESHOLD,
|
2023-03-31 22:20:52 +00:00
|
|
|
PersonalityTrait::Adventurous => {
|
|
|
|
self.openness > Personality::HIGH_THRESHOLD && self.neuroticism < Personality::MID
|
|
|
|
},
|
2023-03-22 12:59:55 +00:00
|
|
|
PersonalityTrait::Closed => self.openness < Personality::LOW_THRESHOLD,
|
|
|
|
PersonalityTrait::Conscientious => self.conscientiousness > Personality::HIGH_THRESHOLD,
|
|
|
|
PersonalityTrait::Busybody => self.agreeableness < Personality::LOW_THRESHOLD,
|
2023-03-31 22:20:52 +00:00
|
|
|
PersonalityTrait::Unconscientious => {
|
|
|
|
self.conscientiousness < Personality::LOW_THRESHOLD
|
|
|
|
},
|
2023-03-22 12:59:55 +00:00
|
|
|
PersonalityTrait::Extroverted => self.extraversion > Personality::HIGH_THRESHOLD,
|
|
|
|
PersonalityTrait::Introverted => self.extraversion < Personality::LOW_THRESHOLD,
|
|
|
|
PersonalityTrait::Agreeable => self.agreeableness > Personality::HIGH_THRESHOLD,
|
2023-03-31 22:20:52 +00:00
|
|
|
PersonalityTrait::Sociable => {
|
|
|
|
self.agreeableness > Personality::HIGH_THRESHOLD
|
|
|
|
&& self.extraversion > Personality::MID
|
|
|
|
},
|
2023-03-22 12:59:55 +00:00
|
|
|
PersonalityTrait::Disagreeable => self.agreeableness < Personality::LOW_THRESHOLD,
|
|
|
|
PersonalityTrait::Neurotic => self.neuroticism > Personality::HIGH_THRESHOLD,
|
2023-03-31 22:20:52 +00:00
|
|
|
PersonalityTrait::Seeker => {
|
|
|
|
self.neuroticism > Personality::HIGH_THRESHOLD
|
|
|
|
&& self.openness > Personality::LITTLE_HIGH
|
|
|
|
},
|
|
|
|
PersonalityTrait::Worried => {
|
|
|
|
self.neuroticism > Personality::HIGH_THRESHOLD
|
|
|
|
&& self.agreeableness > Personality::LITTLE_HIGH
|
|
|
|
},
|
|
|
|
PersonalityTrait::SadLoner => {
|
|
|
|
self.neuroticism > Personality::HIGH_THRESHOLD
|
|
|
|
&& self.extraversion < Personality::LITTLE_LOW
|
|
|
|
},
|
2023-03-22 12:59:55 +00:00
|
|
|
PersonalityTrait::Stable => self.neuroticism < Personality::LOW_THRESHOLD,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn chat_trait(&self, rng: &mut impl Rng) -> Option<PersonalityTrait> {
|
|
|
|
PersonalityTrait::iter().filter(|t| self.is(*t)).choose(rng)
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn will_ambush(&self) -> bool {
|
2023-03-31 22:20:52 +00:00
|
|
|
self.agreeableness < Self::LOW_THRESHOLD && self.conscientiousness < Self::LOW_THRESHOLD
|
2023-03-22 12:59:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for Personality {
|
|
|
|
fn default() -> Self {
|
2023-03-31 22:20:52 +00:00
|
|
|
Self {
|
|
|
|
openness: Personality::MID,
|
|
|
|
conscientiousness: Personality::MID,
|
|
|
|
extraversion: Personality::MID,
|
|
|
|
agreeableness: Personality::MID,
|
|
|
|
neuroticism: Personality::MID,
|
|
|
|
}
|
2023-03-22 12:59:55 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-11-23 15:39:03 +00:00
|
|
|
/// This type is the map route through which the rtsim (real-time simulation)
|
|
|
|
/// aspect of the game communicates with the rest of the game. It is analagous
|
|
|
|
/// to `comp::Controller` in that it provides a consistent interface for
|
|
|
|
/// simulation NPCs to control their actions. Unlike `comp::Controller`, it is
|
|
|
|
/// very abstract and is intended for consumption by both the agent code and the
|
|
|
|
/// internal rtsim simulation code (depending on whether the entity is loaded
|
|
|
|
/// into the game as a physical entity or not). Agent code should attempt to act
|
|
|
|
/// upon its instructions where reasonable although deviations for various
|
|
|
|
/// reasons (obstacle avoidance, counter-attacking, etc.) are expected.
|
2023-04-02 22:02:06 +00:00
|
|
|
#[derive(Clone, Debug, Default)]
|
2020-11-12 21:31:28 +00:00
|
|
|
pub struct RtSimController {
|
2023-04-02 22:02:06 +00:00
|
|
|
pub activity: Option<NpcActivity>,
|
|
|
|
pub actions: VecDeque<NpcAction>,
|
2023-03-22 12:59:55 +00:00
|
|
|
pub personality: Personality,
|
2022-08-16 10:09:23 +00:00
|
|
|
pub heading_to: Option<String>,
|
2020-11-12 21:31:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RtSimController {
|
2021-03-12 18:53:06 +00:00
|
|
|
pub fn with_destination(pos: Vec3<f32>) -> Self {
|
2021-03-12 02:58:57 +00:00
|
|
|
Self {
|
2023-04-02 22:02:06 +00:00
|
|
|
activity: Some(NpcActivity::Goto(pos, 0.5)),
|
|
|
|
..Default::default()
|
2021-03-12 02:58:57 +00:00
|
|
|
}
|
|
|
|
}
|
2020-11-12 21:31:28 +00:00
|
|
|
}
|
2022-08-08 22:15:45 +00:00
|
|
|
|
2023-04-02 22:02:06 +00:00
|
|
|
#[derive(Clone, Copy, Debug)]
|
|
|
|
pub enum NpcActivity {
|
|
|
|
/// (travel_to, speed_factor)
|
|
|
|
Goto(Vec3<f32>, f32),
|
|
|
|
Gather(&'static [ChunkResource]),
|
2023-04-02 22:48:05 +00:00
|
|
|
// TODO: Generalise to other entities? What kinds of animals?
|
|
|
|
HuntAnimals,
|
2023-04-03 16:16:29 +00:00
|
|
|
Dance,
|
2023-04-02 22:02:06 +00:00
|
|
|
}
|
|
|
|
|
2023-04-03 17:03:07 +00:00
|
|
|
#[derive(Clone, Debug)]
|
2023-04-02 17:37:38 +00:00
|
|
|
pub enum NpcAction {
|
|
|
|
Greet(Actor),
|
2023-04-03 17:36:33 +00:00
|
|
|
// TODO: Use some sort of structured, language-independent value that frontends can translate
|
|
|
|
// instead
|
2023-04-03 17:03:07 +00:00
|
|
|
Say(Cow<'static, str>),
|
2023-04-02 17:37:38 +00:00
|
|
|
}
|
|
|
|
|
2022-08-08 22:15:45 +00:00
|
|
|
#[derive(Copy, Clone, Debug, Serialize, Deserialize, enum_map::Enum)]
|
|
|
|
pub enum ChunkResource {
|
2022-08-10 23:40:30 +00:00
|
|
|
#[serde(rename = "0")]
|
2022-08-08 22:15:45 +00:00
|
|
|
Grass,
|
2022-08-10 23:40:30 +00:00
|
|
|
#[serde(rename = "1")]
|
2023-01-06 01:16:15 +00:00
|
|
|
Flower,
|
2022-08-10 23:40:30 +00:00
|
|
|
#[serde(rename = "2")]
|
2023-01-06 01:16:15 +00:00
|
|
|
Fruit,
|
|
|
|
#[serde(rename = "3")]
|
|
|
|
Vegetable,
|
|
|
|
#[serde(rename = "4")]
|
|
|
|
Mushroom,
|
|
|
|
#[serde(rename = "5")]
|
|
|
|
Loot, // Chests, boxes, potions, etc.
|
|
|
|
#[serde(rename = "6")]
|
|
|
|
Plant, // Flax, cotton, wheat, corn, etc.
|
|
|
|
#[serde(rename = "7")]
|
|
|
|
Stone,
|
|
|
|
#[serde(rename = "8")]
|
|
|
|
Wood, // Twigs, logs, bamboo, etc.
|
|
|
|
#[serde(rename = "9")]
|
|
|
|
Gem, // Amethyst, diamond, etc.
|
|
|
|
#[serde(rename = "a")]
|
|
|
|
Ore, // Iron, copper, etc.
|
2022-08-08 22:15:45 +00:00
|
|
|
}
|
2022-08-14 15:08:10 +00:00
|
|
|
|
2023-01-05 15:09:32 +00:00
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
2022-08-14 15:08:10 +00:00
|
|
|
pub enum Profession {
|
|
|
|
#[serde(rename = "0")]
|
|
|
|
Farmer,
|
|
|
|
#[serde(rename = "1")]
|
|
|
|
Hunter,
|
|
|
|
#[serde(rename = "2")]
|
|
|
|
Merchant,
|
|
|
|
#[serde(rename = "3")]
|
|
|
|
Guard,
|
|
|
|
#[serde(rename = "4")]
|
|
|
|
Adventurer(u32),
|
2022-08-14 15:18:47 +00:00
|
|
|
#[serde(rename = "5")]
|
|
|
|
Blacksmith,
|
2022-08-15 10:02:38 +00:00
|
|
|
#[serde(rename = "6")]
|
|
|
|
Chef,
|
|
|
|
#[serde(rename = "7")]
|
|
|
|
Alchemist,
|
|
|
|
#[serde(rename = "8")]
|
|
|
|
Pirate,
|
|
|
|
#[serde(rename = "9")]
|
|
|
|
Cultist,
|
2023-01-05 20:25:32 +00:00
|
|
|
#[serde(rename = "10")]
|
|
|
|
Herbalist,
|
2023-01-14 22:49:43 +00:00
|
|
|
#[serde(rename = "11")]
|
|
|
|
Captain,
|
2022-08-14 15:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Profession {
|
|
|
|
pub fn to_name(&self) -> String {
|
|
|
|
match self {
|
|
|
|
Self::Farmer => "Farmer".to_string(),
|
|
|
|
Self::Hunter => "Hunter".to_string(),
|
|
|
|
Self::Merchant => "Merchant".to_string(),
|
|
|
|
Self::Guard => "Guard".to_string(),
|
|
|
|
Self::Adventurer(_) => "Adventurer".to_string(),
|
2022-08-14 15:18:47 +00:00
|
|
|
Self::Blacksmith => "Blacksmith".to_string(),
|
2022-08-15 10:02:38 +00:00
|
|
|
Self::Chef => "Chef".to_string(),
|
|
|
|
Self::Alchemist => "Alchemist".to_string(),
|
|
|
|
Self::Pirate => "Pirate".to_string(),
|
|
|
|
Self::Cultist => "Cultist".to_string(),
|
2023-01-05 20:25:32 +00:00
|
|
|
Self::Herbalist => "Herbalist".to_string(),
|
2023-01-14 22:49:43 +00:00
|
|
|
Self::Captain => "Captain".to_string(),
|
2022-08-14 15:08:10 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-08-14 15:38:31 +00:00
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize, Deserialize)]
|
|
|
|
pub struct WorldSettings {
|
|
|
|
pub start_time: f64,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for WorldSettings {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self {
|
|
|
|
start_time: 9.0 * 3600.0, // 9am
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|