diff options
| author | troido <troido@protonmail.com> | 2020-03-03 16:28:19 +0100 |
|---|---|---|
| committer | troido <troido@protonmail.com> | 2020-03-03 16:28:19 +0100 |
| commit | b98ea15acbebe8815f9316b5a8ef3393fdd1c3e6 (patch) | |
| tree | 38481d682bf83706784daa31b0d54e2b53ca17d3 /src | |
| parent | 0042d08cbd71caace7d8ed0b8fd47c6d3a263e60 (diff) | |
monsters tend to stay around their home
Diffstat (limited to 'src')
| -rw-r--r-- | src/componentwrapper.rs | 1 | ||||
| -rw-r--r-- | src/main.rs | 2 | ||||
| -rw-r--r-- | src/parameter.rs | 9 | ||||
| -rw-r--r-- | src/playerid.rs | 3 | ||||
| -rw-r--r-- | src/systems/controlai.rs | 39 | ||||
| -rw-r--r-- | src/systems/spawn.rs | 4 |
6 files changed, 34 insertions, 24 deletions
diff --git a/src/componentwrapper.rs b/src/componentwrapper.rs index 9b1dd7f..919a9c2 100644 --- a/src/componentwrapper.rs +++ b/src/componentwrapper.rs @@ -143,6 +143,7 @@ components!( } }; Clan (name: String) Clan{name}; + Home (home: Pos) Home{home}; ); diff --git a/src/main.rs b/src/main.rs index 96844d3..204926f 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,7 +36,7 @@ mod sprite; mod timestamp; mod purgatory; -pub use self::{ +use self::{ pos::Pos, playerid::PlayerId, roomid::RoomId, diff --git a/src/parameter.rs b/src/parameter.rs index a1735da..9c72d69 100644 --- a/src/parameter.rs +++ b/src/parameter.rs @@ -1,7 +1,10 @@ use serde_json::{Value, json}; -use crate::Template; -use crate::components::item::ItemAction; +use crate::{ + Template, + components::item::ItemAction, + Pos +}; @@ -62,7 +65,7 @@ macro_rules! parameters { parameters!( String (String) string, v (v.as_str()?.to_string()) (json!(v)); Int (i64) int, v (v.as_i64()?) (json!(v)); -// Pos (Pos) pos, () (); + Pos (Pos) pos, v (Pos::from_json(v)?) (json!(v)); Float (f64) float, v (v.as_f64()?) (json!(v)); Template (Template) template, v (Template::from_json(v).ok()?) (v.to_json()); Action (ItemAction) action, v (ItemAction::from_json(v)?) (v.to_json()); diff --git a/src/playerid.rs b/src/playerid.rs index aeaf814..08e31d2 100644 --- a/src/playerid.rs +++ b/src/playerid.rs @@ -5,9 +5,6 @@ pub struct PlayerId { } impl PlayerId { - pub fn from_str(name: &str) -> Self { - Self {name: name.to_string()} - } pub fn to_string(&self) -> String { self.name.clone() } diff --git a/src/systems/controlai.rs b/src/systems/controlai.rs index 9d49b41..debe716 100644 --- a/src/systems/controlai.rs +++ b/src/systems/controlai.rs @@ -12,7 +12,8 @@ use specs::{ use crate::{ components::{Controller, ControlCooldown, Fighter, MonsterAI, Home, Health, Position}, - controls::{Control, Direction::{North, South, East, West}} + controls::{Control, Direction::{self, North, South, East, West}}, + Pos }; @@ -50,15 +51,7 @@ impl <'a> System<'a> for ControlAI { if closest_distance <= fighter.range { controllers.insert(entity, Controller{control: Control::AttackTarget(target)}).unwrap(); } else { - let p = position.pos; - let t = closest_position.unwrap().pos; - let mut directions = Vec::new(); - if t.x > p.x {directions.push(East);} - else if t.x < p.x {directions.push(West);} - if t.y > p.y {directions.push(South);} - else if t.y < p.y {directions.push(North);} - if !directions.is_empty() { - let direction = directions[rand::thread_rng().gen_range(0, directions.len())]; + if let Some(direction) = step_to(position.pos, closest_position.unwrap().pos){ controllers.insert(entity, Controller{control: Control::Move(direction)}).unwrap(); } } @@ -66,16 +59,30 @@ impl <'a> System<'a> for ControlAI { } } if rand::thread_rng().gen_range(0.0, 1.0) < ai.move_chance { + if let Some(home) = homes.get(entity) { + if rand::thread_rng().gen_range(0.0, 1.0) < ai.homesickness * (position.pos.distance_to(home.home) as f64) { + let direction = step_to(position.pos, home.home).unwrap(); + controllers.insert(entity, Controller{control: Control::Move(direction)}).unwrap(); + return; + } + } let direction = [North, South, East, West][rand::thread_rng().gen_range(0, 4)]; controllers.insert(entity, Controller{control: Control::Move(direction)}).unwrap(); -// home = roomData.getComponent(obj, Home) -// if home is not None and home.home.inRoom() and random.random() < (ai.homesickness * pathfinding.distanceBetween(obj, home.home)): -// direction = pathfinding.stepTo(obj, home.home) -// else: -// direction = random.choice(["north", "south", "east", "west"]) -// movable.direction = direction } } } } +fn step_to(p: Pos, t: Pos) -> Option<Direction> { + let mut directions = Vec::new(); + if t.x > p.x {directions.push(East);} + else if t.x < p.x {directions.push(West);} + if t.y > p.y {directions.push(South);} + else if t.y < p.y {directions.push(North);} + if directions.is_empty() { + None + } else { + Some(directions[rand::thread_rng().gen_range(0, directions.len())]) + } +} + diff --git a/src/systems/spawn.rs b/src/systems/spawn.rs index 838899c..27dcb1f 100644 --- a/src/systems/spawn.rs +++ b/src/systems/spawn.rs @@ -14,7 +14,8 @@ use crate::{ components::{ Position, Spawner, - Clan + Clan, + Home }, resources::{NewEntities, TimeStamp}, componentwrapper::ComponentWrapper @@ -45,6 +46,7 @@ impl <'a> System<'a> for Spawn { spawner.last_spawn = None; let mut preent = new.encyclopedia.construct(&spawner.template).expect("unable to spawn entity from spawner"); preent.push(ComponentWrapper::Clan(spawner.clan.clone())); + preent.push(ComponentWrapper::Home(Home{home: position.pos})); new.to_build.push((position.pos, preent)); } } else { |
