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.

master
Zed A. Shaw 3 weeks ago
parent fc66d221d4
commit ff81c78d13
  1. 125
      autowalker.cpp
  2. 3
      autowalker.hpp
  3. 82
      scratchpad/a_star.cpp

@ -1,20 +1,33 @@
#include "autowalker.hpp"
#include "inventory.hpp"
#include "ai.hpp"
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_copy = walls_original;
Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)};
count_out = 0;
fsm.$level.world->query<components::Position>(
[&](const auto ent, auto& position) {
if(ent != fsm.$level.player) {
if(fsm.$level.world->has<Comp>(ent)) {
paths.set_target(position.location);
count_out = count_out + 1;
} else {
// 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;
@ -28,17 +41,18 @@ Pathing compute_paths(gui::FSM& fsm, int& count_out) {
}
Pathing Autowalker::path_to_enemies() {
return compute_paths<components::Combat>(fsm, enemy_count);
return compute_paths<components::Combat>(fsm);
}
Pathing Autowalker::path_to_items() {
return compute_paths<components::InventoryItem>(fsm, item_count);
return compute_paths<components::InventoryItem>(fsm);
}
Pathing Autowalker::path_to_devices() {
return compute_paths<components::Device>(fsm, device_count);
return compute_paths<components::Device>(fsm);
}
void Autowalker::window_events() {
fsm.$window.handleEvents(
[&](const sf::Event::KeyPressed &) {
@ -57,9 +71,11 @@ void Autowalker::process_combat() {
|| 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);
} 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);
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;
}
if(!fsm.$level.map->can_move(target_out)) {
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;
}
@ -156,34 +175,64 @@ void Autowalker::autowalk() {
window_events();
if(!fsm.autowalking) return;
process_combat();
auto paths = path_to_enemies();
int move_attempts = 0;
if(enemy_count == 0) {
dbc::log("Killed everything, now finding items.");
paths = path_to_items();
}
auto start = ai::load_state("Walker::initial_state");
auto goal = ai::load_state("Walker::final_state");
if(enemy_count == 0 && item_count == 0) {
dbc::log("No more items, find the exit.");
paths = path_to_devices();
}
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 pos = get_current_position();
matrix::dump("FINDING", paths.$paths, pos.x, pos.y);
process_move(paths);
} else if(action.$name == "kill_enemy") {
fmt::println("KILLING ENEMY");
process_combat();
} else if(action.$name == "find_healing") {
fmt::println("FINDING HEALING");
auto paths = path_to_items();
process_move(paths);
// do the path to healing thing
} else if(action.$name == "collect_items") {
fmt::println("COLLECTING ITEMS");
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;
} else {
fmt::println("Unknown action: {}", action.$name);
}
if(enemy_count == 0 &&
item_count == 0 &&
device_count == 0)
{
fsm.autowalking = false;
dbc::log("no more enemies, items, or devices.");
return;
}
move_attempts++;
}
} while(move_attempts < 100 && fsm.autowalking);
}
void Autowalker::process_move(Pathing& paths) {
Point current = get_current_position();
Point target = current;
show_map_overlay(paths.$paths, current);
if(!path_player(paths, target)) {
dbc::log("no paths found, aborting autowalk");
fsm.autowalking = false;
@ -191,28 +240,12 @@ void Autowalker::autowalk() {
}
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);
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();

@ -19,8 +19,7 @@ struct Autowalker {
bool path_player(Pathing& paths, Point &target_out);
Point get_current_position();
void rotate_player(Point current, Point target);
bool player_has_moved(Point target);
void process_move();
void process_move(Pathing& paths);
Pathing path_to_enemies();
Pathing path_to_items();
Pathing path_to_devices();

@ -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…
Cancel
Save