The autowalker now uses the GOAP AI system to walk the map and do its thing. The code needs a big cleanup, so I might just do a full rewrite based on what I know now.
This commit is contained in:
parent
fc66d221d4
commit
ff81c78d13
3 changed files with 162 additions and 48 deletions
125
autowalker.cpp
125
autowalker.cpp
|
@ -1,20 +1,33 @@
|
||||||
#include "autowalker.hpp"
|
#include "autowalker.hpp"
|
||||||
#include "inventory.hpp"
|
#include "inventory.hpp"
|
||||||
|
#include "ai.hpp"
|
||||||
|
|
||||||
template<typename Comp>
|
template<typename Comp>
|
||||||
Pathing compute_paths(gui::FSM& fsm, int& count_out) {
|
int number_left(gui::FSM& fsm) {
|
||||||
|
int count = 0;
|
||||||
|
|
||||||
|
fsm.$level.world->query<components::Position, Comp>(
|
||||||
|
[&](const auto ent, auto&, auto&) {
|
||||||
|
if(ent != fsm.$level.player) {
|
||||||
|
count++;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
|
return count;
|
||||||
|
}
|
||||||
|
|
||||||
|
template<typename Comp>
|
||||||
|
Pathing compute_paths(gui::FSM& fsm) {
|
||||||
auto& walls_original = fsm.$level.map->$walls;
|
auto& walls_original = fsm.$level.map->$walls;
|
||||||
auto walls_copy = walls_original;
|
auto walls_copy = walls_original;
|
||||||
|
|
||||||
Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)};
|
Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)};
|
||||||
count_out = 0;
|
|
||||||
|
|
||||||
fsm.$level.world->query<components::Position>(
|
fsm.$level.world->query<components::Position>(
|
||||||
[&](const auto ent, auto& position) {
|
[&](const auto ent, auto& position) {
|
||||||
if(ent != fsm.$level.player) {
|
if(ent != fsm.$level.player) {
|
||||||
if(fsm.$level.world->has<Comp>(ent)) {
|
if(fsm.$level.world->has<Comp>(ent)) {
|
||||||
paths.set_target(position.location);
|
paths.set_target(position.location);
|
||||||
count_out = count_out + 1;
|
|
||||||
} else {
|
} else {
|
||||||
// this will mark that spot as a wall so we don't path there temporarily
|
// this will mark that spot as a wall so we don't path there temporarily
|
||||||
walls_copy[position.location.y][position.location.x] = WALL_PATH_LIMIT;
|
walls_copy[position.location.y][position.location.x] = WALL_PATH_LIMIT;
|
||||||
|
@ -28,17 +41,18 @@ Pathing compute_paths(gui::FSM& fsm, int& count_out) {
|
||||||
}
|
}
|
||||||
|
|
||||||
Pathing Autowalker::path_to_enemies() {
|
Pathing Autowalker::path_to_enemies() {
|
||||||
return compute_paths<components::Combat>(fsm, enemy_count);
|
return compute_paths<components::Combat>(fsm);
|
||||||
}
|
}
|
||||||
|
|
||||||
Pathing Autowalker::path_to_items() {
|
Pathing Autowalker::path_to_items() {
|
||||||
return compute_paths<components::InventoryItem>(fsm, item_count);
|
return compute_paths<components::InventoryItem>(fsm);
|
||||||
}
|
}
|
||||||
|
|
||||||
Pathing Autowalker::path_to_devices() {
|
Pathing Autowalker::path_to_devices() {
|
||||||
return compute_paths<components::Device>(fsm, device_count);
|
return compute_paths<components::Device>(fsm);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
void Autowalker::window_events() {
|
void Autowalker::window_events() {
|
||||||
fsm.$window.handleEvents(
|
fsm.$window.handleEvents(
|
||||||
[&](const sf::Event::KeyPressed &) {
|
[&](const sf::Event::KeyPressed &) {
|
||||||
|
@ -57,9 +71,11 @@ void Autowalker::process_combat() {
|
||||||
|| fsm.in_state(gui::State::ATTACKING))
|
|| fsm.in_state(gui::State::ATTACKING))
|
||||||
{
|
{
|
||||||
if(fsm.in_state(gui::State::ATTACKING)) {
|
if(fsm.in_state(gui::State::ATTACKING)) {
|
||||||
|
fmt::println("In attacking state, sending a TICK");
|
||||||
send_event(gui::Event::TICK);
|
send_event(gui::Event::TICK);
|
||||||
} else {
|
} else {
|
||||||
send_event(gui::Event::ATTACK);
|
fmt::println("Not in ATTACK, sending an ATTACK to continue combat.");
|
||||||
|
send_event(gui::Event::ATTACK);;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -73,12 +89,15 @@ bool Autowalker::path_player(Pathing& paths, Point& target_out) {
|
||||||
bool found = paths.random_walk(target_out, false, PATHING_TOWARD);
|
bool found = paths.random_walk(target_out, false, PATHING_TOWARD);
|
||||||
|
|
||||||
if(!found) {
|
if(!found) {
|
||||||
dbc::log("no neighbor found, aborting autowalk");
|
dbc::log("no neighbor found in any direction, aborting autowalk");
|
||||||
|
matrix::dump("NO TOWARD", paths.$paths, target_out.x, target_out.y);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if(!fsm.$level.map->can_move(target_out)) {
|
if(!fsm.$level.map->can_move(target_out)) {
|
||||||
dbc::log("neighbors is telling me to go to a bad spot.");
|
dbc::log("neighbors is telling me to go to a bad spot.");
|
||||||
|
matrix::dump("BAD TARGET PATHS", paths.$paths, target_out.x, target_out.y);
|
||||||
|
matrix::dump("BAD TARGET MAP", fsm.$level.map->walls(), target_out.x, target_out.y);
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -156,34 +175,64 @@ void Autowalker::autowalk() {
|
||||||
window_events();
|
window_events();
|
||||||
if(!fsm.autowalking) return;
|
if(!fsm.autowalking) return;
|
||||||
|
|
||||||
process_combat();
|
int move_attempts = 0;
|
||||||
|
|
||||||
|
auto start = ai::load_state("Walker::initial_state");
|
||||||
|
auto goal = ai::load_state("Walker::final_state");
|
||||||
|
|
||||||
|
do {
|
||||||
|
int enemy_count = number_left<components::Combat>(fsm);
|
||||||
|
int item_count = number_left<components::InventoryItem>(fsm);
|
||||||
|
|
||||||
|
fmt::println("ENEMY COUNT: {}, ITEM COUNT: {}", enemy_count, item_count);
|
||||||
|
|
||||||
|
window_events();
|
||||||
|
ai::set(start, "no_more_enemies", enemy_count == 0);
|
||||||
|
ai::set(start, "no_more_items", item_count == 0);
|
||||||
|
ai::set(start, "enemy_found",
|
||||||
|
fsm.in_state(gui::State::IN_COMBAT) ||
|
||||||
|
fsm.in_state(gui::State::ATTACKING));
|
||||||
|
|
||||||
|
auto a_plan = ai::plan("Walker::actions", start, goal);
|
||||||
|
|
||||||
|
// need a test for plan complete and only action is END
|
||||||
|
for(auto action : a_plan.script) {
|
||||||
|
if(action.$name == "find_enemy") {
|
||||||
|
// this is where to test if enemy found and update state
|
||||||
|
fmt::println("FINDING AN ENEMY");
|
||||||
auto paths = path_to_enemies();
|
auto paths = path_to_enemies();
|
||||||
|
auto pos = get_current_position();
|
||||||
if(enemy_count == 0) {
|
matrix::dump("FINDING", paths.$paths, pos.x, pos.y);
|
||||||
dbc::log("Killed everything, now finding items.");
|
process_move(paths);
|
||||||
paths = path_to_items();
|
} else if(action.$name == "kill_enemy") {
|
||||||
}
|
fmt::println("KILLING ENEMY");
|
||||||
|
process_combat();
|
||||||
if(enemy_count == 0 && item_count == 0) {
|
} else if(action.$name == "find_healing") {
|
||||||
dbc::log("No more items, find the exit.");
|
fmt::println("FINDING HEALING");
|
||||||
paths = path_to_devices();
|
auto paths = path_to_items();
|
||||||
}
|
process_move(paths);
|
||||||
|
// do the path to healing thing
|
||||||
if(enemy_count == 0 &&
|
} else if(action.$name == "collect_items") {
|
||||||
item_count == 0 &&
|
fmt::println("COLLECTING ITEMS");
|
||||||
device_count == 0)
|
auto paths = path_to_items();
|
||||||
{
|
process_move(paths);
|
||||||
|
// path to the items and get them all
|
||||||
|
} else if(action.$name == "END") {
|
||||||
|
fmt::println("END STATE, complete? {}", a_plan.complete);
|
||||||
fsm.autowalking = false;
|
fsm.autowalking = false;
|
||||||
dbc::log("no more enemies, items, or devices.");
|
} else {
|
||||||
return;
|
fmt::println("Unknown action: {}", action.$name);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
move_attempts++;
|
||||||
|
}
|
||||||
|
} while(move_attempts < 100 && fsm.autowalking);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Autowalker::process_move(Pathing& paths) {
|
||||||
Point current = get_current_position();
|
Point current = get_current_position();
|
||||||
Point target = current;
|
Point target = current;
|
||||||
|
|
||||||
show_map_overlay(paths.$paths, current);
|
|
||||||
|
|
||||||
|
|
||||||
if(!path_player(paths, target)) {
|
if(!path_player(paths, target)) {
|
||||||
dbc::log("no paths found, aborting autowalk");
|
dbc::log("no paths found, aborting autowalk");
|
||||||
fsm.autowalking = false;
|
fsm.autowalking = false;
|
||||||
|
@ -191,28 +240,12 @@ void Autowalker::autowalk() {
|
||||||
}
|
}
|
||||||
|
|
||||||
rotate_player(current, target);
|
rotate_player(current, target);
|
||||||
|
while(fsm.in_state(gui::State::ROTATING)) send_event(gui::Event::TICK);
|
||||||
|
|
||||||
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);
|
send_event(gui::Event::MOVE_FORWARD);
|
||||||
while(fsm.in_state(gui::State::MOVING)) send_event(gui::Event::TICK);
|
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) {
|
void Autowalker::send_event(gui::Event ev) {
|
||||||
fsm.event(ev);
|
fsm.event(ev);
|
||||||
fsm.render();
|
fsm.render();
|
||||||
|
|
|
@ -19,8 +19,7 @@ struct Autowalker {
|
||||||
bool path_player(Pathing& paths, Point &target_out);
|
bool path_player(Pathing& paths, Point &target_out);
|
||||||
Point get_current_position();
|
Point get_current_position();
|
||||||
void rotate_player(Point current, Point target);
|
void rotate_player(Point current, Point target);
|
||||||
bool player_has_moved(Point target);
|
void process_move(Pathing& paths);
|
||||||
void process_move();
|
|
||||||
Pathing path_to_enemies();
|
Pathing path_to_enemies();
|
||||||
Pathing path_to_items();
|
Pathing path_to_items();
|
||||||
Pathing path_to_devices();
|
Pathing path_to_devices();
|
||||||
|
|
82
scratchpad/a_star.cpp
Normal file
82
scratchpad/a_star.cpp
Normal file
|
@ -0,0 +1,82 @@
|
||||||
|
|
||||||
|
constexpr const float SCORE_MAX = std::numeric_limits<float>::max()
|
||||||
|
|
||||||
|
using AStarPath = std::deque<Point>;
|
||||||
|
|
||||||
|
void update_map(Matrix& map, std::deque<Point>& total_path) {
|
||||||
|
for(auto &point : total_path) {
|
||||||
|
map[point.y][point.x] = 10;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
AStarPath reconstruct_path(std::unordered_map<Point, Point>& came_from, Point current) {
|
||||||
|
std::deque<Point> total_path{current};
|
||||||
|
|
||||||
|
while(came_from.contains(current)) {
|
||||||
|
current = came_from[current];
|
||||||
|
total_path.push_front(current);
|
||||||
|
}
|
||||||
|
|
||||||
|
return total_path;
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float h(Point from, Point to) {
|
||||||
|
return std::hypot(float(from.x) - float(to.x),
|
||||||
|
float(from.y) - float(to.y));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline float d(Point current, Point neighbor) {
|
||||||
|
return std::hypot(float(current.x) - float(neighbor.x),
|
||||||
|
float(current.y) - float(neighbor.y));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline Point find_lowest(std::unordered_map<Point, float>& open_set) {
|
||||||
|
dbc::check(!open_set.empty(), "open set can't be empty in find_lowest");
|
||||||
|
Point result;
|
||||||
|
float lowest_score = SCORE_MAX;
|
||||||
|
|
||||||
|
for(auto [point, score] : open_set) {
|
||||||
|
if(score < lowest_score) {
|
||||||
|
lowest_score = score;
|
||||||
|
result = point;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
std::optional<AStarPath> path_to_player(Matrix& map, Point start, Point goal) {
|
||||||
|
std::unordered_map<Point, float> open_set;
|
||||||
|
std::unordered_map<Point, Point> came_from;
|
||||||
|
std::unordered_map<Point, float> g_score;
|
||||||
|
g_score[start] = 0;
|
||||||
|
|
||||||
|
open_set[start] = g_score[start] + h(start, goal);
|
||||||
|
|
||||||
|
while(!open_set.empty()) {
|
||||||
|
auto current = find_lowest(open_set);
|
||||||
|
|
||||||
|
if(current == goal) {
|
||||||
|
return std::make_optional<AStarPath>(reconstruct_path(came_from, current));
|
||||||
|
}
|
||||||
|
|
||||||
|
open_set.erase(current);
|
||||||
|
|
||||||
|
for(matrix::compass it{map, current.x, current.y}; it.next();) {
|
||||||
|
Point neighbor{it.x, it.y};
|
||||||
|
|
||||||
|
float d_score = d(current, neighbor) + map[it.y][it.x] * SCORE_MAX;
|
||||||
|
float tentative_g_score = g_score[current] + d_score;
|
||||||
|
float neighbor_g_score = g_score.contains(neighbor) ? g_score[neighbor] : SCORE_MAX;
|
||||||
|
if(tentative_g_score < neighbor_g_score) {
|
||||||
|
came_from[neighbor] = current;
|
||||||
|
g_score[neighbor] = tentative_g_score;
|
||||||
|
// open_set gets the fScore
|
||||||
|
open_set[neighbor] = tentative_g_score + h(neighbor, goal);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return std::nullopt;
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue