summaryrefslogtreecommitdiff
path: root/src/systems/controlai.rs
diff options
context:
space:
mode:
authortroido <troido@protonmail.com>2020-03-03 16:28:19 +0100
committertroido <troido@protonmail.com>2020-03-03 16:28:19 +0100
commitb98ea15acbebe8815f9316b5a8ef3393fdd1c3e6 (patch)
tree38481d682bf83706784daa31b0d54e2b53ca17d3 /src/systems/controlai.rs
parent0042d08cbd71caace7d8ed0b8fd47c6d3a263e60 (diff)
monsters tend to stay around their home
Diffstat (limited to 'src/systems/controlai.rs')
-rw-r--r--src/systems/controlai.rs39
1 files changed, 23 insertions, 16 deletions
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())])
+ }
+}
+