Speed with TURN_COST and slight randomisation to reduce predictability
This commit is contained in:
parent
dce40ac741
commit
aac6e0ad02
12 changed files with 200 additions and 145 deletions
|
|
@ -1,6 +1,4 @@
|
|||
use super::{
|
||||
gamelog, Confusion, EntityMoved, Map, Monster, Name, ParticleBuilder, Position, RunState, Viewshed, WantsToMelee,
|
||||
};
|
||||
use super::{EntityMoved, Map, Monster, Position, TakingTurn, Viewshed, WantsToMelee};
|
||||
use rltk::Point;
|
||||
use specs::prelude::*;
|
||||
|
||||
|
|
@ -12,16 +10,13 @@ impl<'a> System<'a> for MonsterAI {
|
|||
WriteExpect<'a, Map>,
|
||||
ReadExpect<'a, Point>,
|
||||
ReadExpect<'a, Entity>,
|
||||
ReadExpect<'a, RunState>,
|
||||
Entities<'a>,
|
||||
WriteStorage<'a, Viewshed>,
|
||||
ReadStorage<'a, Monster>,
|
||||
WriteStorage<'a, Position>,
|
||||
WriteStorage<'a, WantsToMelee>,
|
||||
WriteStorage<'a, Confusion>,
|
||||
ReadStorage<'a, Name>,
|
||||
WriteExpect<'a, ParticleBuilder>,
|
||||
WriteStorage<'a, EntityMoved>,
|
||||
ReadStorage<'a, TakingTurn>,
|
||||
);
|
||||
|
||||
fn run(&mut self, data: Self::SystemData) {
|
||||
|
|
@ -29,71 +24,38 @@ impl<'a> System<'a> for MonsterAI {
|
|||
mut map,
|
||||
player_pos,
|
||||
player_entity,
|
||||
runstate,
|
||||
entities,
|
||||
mut viewshed,
|
||||
monster,
|
||||
mut position,
|
||||
mut wants_to_melee,
|
||||
mut confused,
|
||||
name,
|
||||
mut particle_builder,
|
||||
mut entity_moved,
|
||||
turns,
|
||||
) = data;
|
||||
|
||||
if *runstate != RunState::MonsterTurn {
|
||||
return;
|
||||
}
|
||||
|
||||
for (entity, mut viewshed, _monster, mut pos) in (&entities, &mut viewshed, &monster, &mut position).join() {
|
||||
let mut can_act = true;
|
||||
|
||||
// Check confusion
|
||||
let is_confused = confused.get_mut(entity);
|
||||
if let Some(i_am_confused) = is_confused {
|
||||
i_am_confused.turns -= 1;
|
||||
let entity_name = name.get(entity).unwrap();
|
||||
let mut fg = rltk::RGB::named(rltk::MAGENTA);
|
||||
let mut glyph = rltk::to_cp437('?');
|
||||
if i_am_confused.turns < 1 {
|
||||
confused.remove(entity);
|
||||
gamelog::Logger::new()
|
||||
.npc_name(&entity_name.name)
|
||||
.colour(rltk::WHITE)
|
||||
.append("snaps out of it.")
|
||||
.log();
|
||||
fg = rltk::RGB::named(rltk::MEDIUMSLATEBLUE);
|
||||
glyph = rltk::to_cp437('!');
|
||||
} else {
|
||||
gamelog::Logger::new().npc_name(&entity_name.name).colour(rltk::WHITE).append("is confused.").log();
|
||||
}
|
||||
particle_builder.request(pos.x, pos.y, fg, rltk::RGB::named(rltk::BLACK), glyph, 200.0);
|
||||
can_act = false;
|
||||
}
|
||||
|
||||
if can_act {
|
||||
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");
|
||||
}
|
||||
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");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue