faction table, applying factions to mobs, and querying reactions
This commit is contained in:
parent
0375c31acf
commit
76d835021b
11 changed files with 108 additions and 13 deletions
63
src/ai/bystander_ai_system.rs
Normal file
63
src/ai/bystander_ai_system.rs
Normal file
|
|
@ -0,0 +1,63 @@
|
|||
use crate::{Bystander, EntityMoved, Map, Position, TakingTurn, Viewshed};
|
||||
use specs::prelude::*;
|
||||
|
||||
pub struct BystanderAI {}
|
||||
|
||||
impl<'a> System<'a> for BystanderAI {
|
||||
#[allow(clippy::type_complexity)]
|
||||
type SystemData = (
|
||||
WriteExpect<'a, Map>,
|
||||
Entities<'a>,
|
||||
WriteStorage<'a, Viewshed>,
|
||||
ReadStorage<'a, Bystander>,
|
||||
WriteStorage<'a, Position>,
|
||||
WriteStorage<'a, EntityMoved>,
|
||||
WriteExpect<'a, rltk::RandomNumberGenerator>,
|
||||
ReadStorage<'a, TakingTurn>,
|
||||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
let (mut map, entities, mut viewshed, bystander, mut position, mut entity_moved, mut rng, turns) = data;
|
||||
|
||||
for (entity, mut viewshed, _bystander, mut pos, _turn) in
|
||||
(&entities, &mut viewshed, &bystander, &mut position, &turns).join()
|
||||
{
|
||||
if try_move_randomly(&mut pos, &mut rng, &mut map, &mut viewshed) {
|
||||
entity_moved.insert(entity, EntityMoved {}).expect("Unable to insert marker");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn try_move_randomly(
|
||||
pos: &mut Position,
|
||||
rng: &mut rltk::RandomNumberGenerator,
|
||||
map: &mut Map,
|
||||
viewshed: &mut Viewshed,
|
||||
) -> bool {
|
||||
// Try to move randomly
|
||||
let mut x = pos.x;
|
||||
let mut y = pos.y;
|
||||
let move_roll = rng.roll_dice(1, 8);
|
||||
match move_roll {
|
||||
1 => x -= 1,
|
||||
2 => x += 1,
|
||||
3 => y -= 1,
|
||||
4 => y += 1,
|
||||
_ => {}
|
||||
}
|
||||
|
||||
if x > 0 && x < map.width - 1 && y > 0 && y < map.height - 1 {
|
||||
let dest_idx = map.xy_idx(x, y);
|
||||
if !map.blocked[dest_idx] {
|
||||
let idx = map.xy_idx(pos.x, pos.y);
|
||||
map.blocked[idx] = false;
|
||||
pos.x = x;
|
||||
pos.y = y;
|
||||
map.blocked[dest_idx] = true;
|
||||
viewshed.dirty = true;
|
||||
return true;
|
||||
}
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
|
@ -8,3 +8,7 @@ mod regen_system;
|
|||
pub use regen_system::RegenSystem;
|
||||
mod encumbrance_system;
|
||||
pub use encumbrance_system::{EncumbranceSystem, CARRY_CAPACITY_PER_STRENGTH};
|
||||
mod bystander_ai_system;
|
||||
pub use bystander_ai_system::BystanderAI;
|
||||
mod monster_ai_system;
|
||||
pub use monster_ai_system::MonsterAI;
|
||||
|
|
|
|||
70
src/ai/monster_ai_system.rs
Normal file
70
src/ai/monster_ai_system.rs
Normal file
|
|
@ -0,0 +1,70 @@
|
|||
use super::bystander_ai_system::try_move_randomly;
|
||||
use crate::{EntityMoved, Map, Monster, Position, TakingTurn, Viewshed, WantsToMelee};
|
||||
use rltk::Point;
|
||||
use specs::prelude::*;
|
||||
|
||||
pub struct MonsterAI {}
|
||||
|
||||
impl<'a> System<'a> for MonsterAI {
|
||||
#[allow(clippy::type_complexity)]
|
||||
type SystemData = (
|
||||
WriteExpect<'a, Map>,
|
||||
WriteExpect<'a, rltk::RandomNumberGenerator>,
|
||||
ReadExpect<'a, Point>,
|
||||
ReadExpect<'a, Entity>,
|
||||
Entities<'a>,
|
||||
WriteStorage<'a, Viewshed>,
|
||||
ReadStorage<'a, Monster>,
|
||||
WriteStorage<'a, Position>,
|
||||
WriteStorage<'a, WantsToMelee>,
|
||||
WriteStorage<'a, EntityMoved>,
|
||||
ReadStorage<'a, TakingTurn>,
|
||||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
let (
|
||||
mut map,
|
||||
mut rng,
|
||||
player_pos,
|
||||
player_entity,
|
||||
entities,
|
||||
mut viewshed,
|
||||
monster,
|
||||
mut position,
|
||||
mut wants_to_melee,
|
||||
mut entity_moved,
|
||||
turns,
|
||||
) = data;
|
||||
|
||||
for (entity, mut viewshed, _monster, mut pos, _turn) in
|
||||
(&entities, &mut viewshed, &monster, &mut position, &turns).join()
|
||||
{
|
||||
let distance = rltk::DistanceAlg::Pythagoras.distance2d(Point::new(pos.x, pos.y), *player_pos);
|
||||
if distance < 1.5 {
|
||||
wants_to_melee
|
||||
.insert(entity, WantsToMelee { target: *player_entity })
|
||||
.expect("Unable to insert attack.");
|
||||
} else if viewshed.visible_tiles.contains(&*player_pos) {
|
||||
// If the player is visible, but the path is obstructed, this will currently search
|
||||
// the entire map (i.e. Will do a huge ASTAR to find an alternate route), and the
|
||||
// mob will follow that path until it leaves vision, then lose sight of the player
|
||||
// and stop.
|
||||
let path = rltk::a_star_search(map.xy_idx(pos.x, pos.y), map.xy_idx(player_pos.x, player_pos.y), &*map);
|
||||
if path.success && path.steps.len() > 1 {
|
||||
let mut idx = map.xy_idx(pos.x, pos.y);
|
||||
map.blocked[idx] = false;
|
||||
pos.x = (path.steps[1] as i32) % map.width;
|
||||
pos.y = (path.steps[1] as i32) / map.width;
|
||||
idx = map.xy_idx(pos.x, pos.y);
|
||||
map.blocked[idx] = true;
|
||||
viewshed.dirty = true;
|
||||
entity_moved.insert(entity, EntityMoved {}).expect("Unable to insert marker");
|
||||
}
|
||||
} else {
|
||||
if try_move_randomly(&mut pos, &mut rng, &mut map, &mut viewshed) {
|
||||
entity_moved.insert(entity, EntityMoved {}).expect("Unable to insert marker");
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue