|
|
|
#include "autowalker.hpp"
|
|
|
|
|
|
|
|
Pathing Autowalker::compute_paths() {
|
|
|
|
Pathing paths{fsm.$level.map->width(), fsm.$level.map->height()};
|
|
|
|
enemy_count = 0;
|
|
|
|
|
|
|
|
fsm.$level.world->query<components::Position, components::Combat>(
|
|
|
|
[&](const auto ent, auto& position, auto&) {
|
|
|
|
if(ent != fsm.$level.player) {
|
|
|
|
paths.set_target(position.location);
|
|
|
|
enemy_count++;
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
fmt::println("PATHING to {} count enemies", enemy_count);
|
|
|
|
|
|
|
|
// BUG: using walls() will cause a map full of walls?
|
|
|
|
dbc::check(matrix::width(fsm.$level.map->$walls) == paths.$width, "WTF the maps's walls width changed?");
|
|
|
|
dbc::check(matrix::height(fsm.$level.map->$walls) == paths.$height, "WTF the maps's walls height changed?");
|
|
|
|
|
|
|
|
paths.compute_paths(fsm.$level.map->$walls);
|
|
|
|
|
|
|
|
return paths;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Autowalker::window_events() {
|
|
|
|
fsm.$window.handleEvents(
|
|
|
|
[&](const sf::Event::KeyPressed &) {
|
|
|
|
fsm.autowalking = false;
|
|
|
|
fmt::println("ABORT AUTOWALK");
|
|
|
|
},
|
|
|
|
[&](const sf::Event::MouseButtonPressed &) {
|
|
|
|
fsm.autowalking = false;
|
|
|
|
fmt::println("ABORT AUTOWALK");
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
void Autowalker::process_combat() {
|
|
|
|
while(fsm.in_state(gui::State::IN_COMBAT)
|
|
|
|
|| fsm.in_state(gui::State::ATTACKING))
|
|
|
|
{
|
|
|
|
if(fsm.in_state(gui::State::ATTACKING)) {
|
|
|
|
send_event(gui::Event::TICK);
|
|
|
|
} else {
|
|
|
|
send_event(gui::Event::ATTACK);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Point Autowalker::get_current_position() {
|
|
|
|
auto& player_position = fsm.$level.world->get<components::Position>(fsm.$level.player);
|
|
|
|
return player_position.location;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Autowalker::path_player(Pathing& paths, Point& target_out) {
|
|
|
|
bool found = paths.random_walk(target_out, false, PATHING_TOWARD);
|
|
|
|
|
|
|
|
if(!found) {
|
|
|
|
dbc::log("no neighbor found, aborting autowalk");
|
|
|
|
fsm.autowalking = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!fsm.$level.map->can_move(target_out)) {
|
|
|
|
dbc::log("neighbors is telling me to go to a bad spot.");
|
|
|
|
fsm.autowalking = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Autowalker::rotate_player(Point current, Point target) {
|
|
|
|
int delta_x = int(target.x) - int(current.x);
|
|
|
|
int delta_y = int(target.y) - int(current.y);
|
|
|
|
|
|
|
|
int facing = fsm.$main_ui.$compass_dir;
|
|
|
|
int target_facing = 0;
|
|
|
|
|
|
|
|
if(delta_x == -1 && delta_y == 0) {
|
|
|
|
// west
|
|
|
|
fmt::println("WEST: {}, {}", target.x, target.y);
|
|
|
|
target_facing = 4;
|
|
|
|
} else if(delta_x == 1 && delta_y == 0) {
|
|
|
|
// east
|
|
|
|
fmt::println("EAST: {}, {}", target.x, target.y);
|
|
|
|
target_facing = 0;
|
|
|
|
} else if(delta_x == 0 && delta_y == 1) {
|
|
|
|
fmt::println("SOUTH: {}, {}", target.x, target.y);
|
|
|
|
// south
|
|
|
|
target_facing = 2;
|
|
|
|
} else if(delta_x == 0 && delta_y == -1) {
|
|
|
|
fmt::println("NORTH: {}, {}", target.x, target.y);
|
|
|
|
// north
|
|
|
|
target_facing = 6;
|
|
|
|
} else {
|
|
|
|
dbc::sentinel(
|
|
|
|
fmt::format("got more than 4 direction result: "
|
|
|
|
"current={},{} "
|
|
|
|
"target={},{} "
|
|
|
|
"delta={},{} ",
|
|
|
|
current.x, current.y,
|
|
|
|
target.x, target.y,
|
|
|
|
delta_x, delta_y));
|
|
|
|
}
|
|
|
|
|
|
|
|
auto dir = facing < target_facing ? gui::Event::ROTATE_LEFT : gui::Event::ROTATE_RIGHT;
|
|
|
|
|
|
|
|
while(facing != target_facing) {
|
|
|
|
send_event(dir);
|
|
|
|
facing = fsm.$main_ui.$compass_dir;
|
|
|
|
}
|
|
|
|
|
|
|
|
dbc::check(fsm.$main_ui.$compass_dir == target_facing,
|
|
|
|
"player isn't facing the correct direction");
|
|
|
|
}
|
|
|
|
|
|
|
|
void Autowalker::autowalk() {
|
|
|
|
window_events();
|
|
|
|
if(!fsm.autowalking) return;
|
|
|
|
|
|
|
|
process_combat();
|
|
|
|
auto paths = compute_paths();
|
|
|
|
|
|
|
|
Point current = get_current_position();
|
|
|
|
Point target = current;
|
|
|
|
|
|
|
|
matrix::dump("AUTO PATHS", paths.$paths, current.x, current.y);
|
|
|
|
|
|
|
|
if(!path_player(paths, target)) {
|
|
|
|
dbc::log("no paths found, aborting autowalk");
|
|
|
|
fsm.autowalking = false;
|
|
|
|
return;
|
|
|
|
} else if(enemy_count == 0) {
|
|
|
|
dbc::log("Nobody left to kill. You win.");
|
|
|
|
fsm.autowalking = false;
|
|
|
|
return;
|
|
|
|
} else {
|
|
|
|
dbc::log("Hunting down more enemies.");
|
|
|
|
}
|
|
|
|
|
|
|
|
rotate_player(current, target);
|
|
|
|
|
|
|
|
int move_attempts = 0;
|
|
|
|
do {
|
|
|
|
process_combat();
|
|
|
|
process_move();
|
|
|
|
// BUG: sometimes in idle but there's an enemy near but combat hasn't started
|
|
|
|
// for now just toss out an ATTACK and it'll be ignored or cause combat
|
|
|
|
send_event(gui::Event::ATTACK);
|
|
|
|
move_attempts++;
|
|
|
|
} while(move_attempts < 100 && !player_has_moved(target));
|
|
|
|
}
|
|
|
|
|
|
|
|
void Autowalker::process_move() {
|
|
|
|
send_event(gui::Event::MOVE_FORWARD);
|
|
|
|
while(fsm.in_state(gui::State::MOVING)) send_event(gui::Event::TICK);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Autowalker::player_has_moved(Point target) {
|
|
|
|
Point current = get_current_position();
|
|
|
|
return current.x == target.x && current.y == target.y;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Autowalker::send_event(gui::Event ev) {
|
|
|
|
fsm.event(ev);
|
|
|
|
fsm.render();
|
|
|
|
fsm.handle_world_events();
|
|
|
|
}
|
|
|
|
|
|
|
|
void Autowalker::start_autowalk() {
|
|
|
|
fsm.autowalking = true;
|
|
|
|
}
|