Needed to rewrite the pathing to get this to work. I actually had been using a purposefully broken pathing algorithm from when I was making random maps.

master
Zed A. Shaw 1 week ago
parent c894f6e094
commit e92fd2b6f3
  1. 4
      Makefile
  2. 14
      assets/ai.json
  3. 90
      autowalker.cpp
  4. 5
      autowalker.hpp
  5. 1
      gui/status_ui.cpp
  6. 4
      map.cpp
  7. 6
      matrix.cpp
  8. 77
      pathing.cpp
  9. 10
      pathing.hpp
  10. 69
      tests/pathing.cpp

@ -37,7 +37,7 @@ tracy_build:
meson compile -j 10 -C builddir meson compile -j 10 -C builddir
test: asset_build build test: asset_build build
./builddir/runtests -d yes ./builddir/runtests -d yes "[pathing]"
run: build test run: build test
ifeq '$(OS)' 'Windows_NT' ifeq '$(OS)' 'Windows_NT'
@ -60,7 +60,7 @@ clean:
meson compile --clean -C builddir meson compile --clean -C builddir
debug_test: build debug_test: build
gdb --nx -x .gdbinit --ex run --args builddir/runtests -e "[map]" gdb --nx -x .gdbinit --ex run --ex bt --ex q --args builddir/runtests -e "[pathing]"
win_installer: win_installer:
powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" scripts\win_installer.ifp' powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" scripts\win_installer.ifp'

@ -54,6 +54,19 @@
"enemy_dead": true "enemy_dead": true
} }
}, },
{
"name": "face_enemy",
"cost": 10,
"needs": {
"no_more_enemies": false,
"in_combat": false,
"enemy_found": true
},
"effects": {
"in_combat": true,
"enemy_dead": true
}
},
{ {
"name": "collect_items", "name": "collect_items",
"cost": 5, "cost": 5,
@ -119,6 +132,7 @@
"Host::actions": "Host::actions":
["find_enemy", ["find_enemy",
"kill_enemy", "kill_enemy",
"face_enemy",
"collect_items", "collect_items",
"use_healing"], "use_healing"],
"Enemy::actions": "Enemy::actions":

@ -34,20 +34,29 @@ Pathing compute_paths() {
Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)}; Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)};
level.world->query<components::Position>( // first, put everything of this type as a target
[&](const auto ent, auto& position) { level.world->query<components::Position, Comp>(
[&](const auto ent, auto& position, auto&) {
if(ent != level.player) { if(ent != level.player) {
if(level.world->has<Comp>(ent)) { paths.set_target(position.location);
paths.set_target(position.location); }
} 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; level.world->query<components::Collision>(
} [&](const auto ent, auto& collision) {
if(collision.has) {
auto& pos = level.world->get<components::Position>(ent);
walls_copy[pos.location.y][pos.location.x] = WALL_VALUE;
} }
}); });
paths.compute_paths(walls_copy); paths.compute_paths(walls_copy);
auto pos = GameDB::player_position().location;
matrix::dump("compute_paths walls", walls_copy, pos.x, pos.y);
matrix::dump("compute_paths input", paths.$input, pos.x, pos.y);
matrix::dump("compute_paths paths", paths.$paths, pos.x, pos.y);
return paths; return paths;
} }
@ -57,7 +66,7 @@ DinkyECS::Entity Autowalker::camera_aim() {
if(level.collision->something_there(fsm.$main_ui.$rayview->aiming_at)) { if(level.collision->something_there(fsm.$main_ui.$rayview->aiming_at)) {
return level.collision->get(fsm.$main_ui.$rayview->aiming_at); return level.collision->get(fsm.$main_ui.$rayview->aiming_at);
} else { } else {
return 0; return DinkyECS::NONE;
} }
} }
@ -124,17 +133,19 @@ void Autowalker::path_fail(const std::string& msg, Matrix& bad_paths, Point pos)
bool Autowalker::path_player(Pathing& paths, Point& target_out) { bool Autowalker::path_player(Pathing& paths, Point& target_out) {
auto &level = GameDB::current_level(); auto &level = GameDB::current_level();
bool found = paths.random_walk(target_out, false, PATHING_TOWARD, 4, 8); auto found = paths.find_path(target_out, PATHING_TOWARD, false);
if(!found) { if(found == PathingResult::FAIL) {
// failed to find a linear path, try diagonal // failed to find a linear path, try diagonal
if(!paths.random_walk(target_out, false, PATHING_TOWARD, 8, 8)) { if(paths.find_path(target_out, PATHING_TOWARD, true) == PathingResult::FAIL) {
path_fail("random_walk", paths.$paths, target_out); path_fail("random_walk", paths.$paths, target_out);
return false; return false;
} }
} }
if(!level.map->can_move(target_out)) { if(!level.map->can_move(target_out)) {
fmt::println("----- FAIL MAP IS, cell is {}", paths.$paths[target_out.y][target_out.x]);
level.map->dump(target_out.x, target_out.y);
path_fail("level_map->can_move", paths.$paths, target_out); path_fail("level_map->can_move", paths.$paths, target_out);
return false; return false;
} }
@ -143,15 +154,18 @@ bool Autowalker::path_player(Pathing& paths, Point& target_out) {
} }
void Autowalker::rotate_player(Point target) { void Autowalker::rotate_player(Point target) {
auto rayview = fsm.$main_ui.$rayview;
// auto dir = facing > target_facing ? gui::Event::ROTATE_LEFT : gui::Event::ROTATE_RIGHT; // auto dir = facing > target_facing ? gui::Event::ROTATE_LEFT : gui::Event::ROTATE_RIGHT;
auto dir = gui::Event::ROTATE_LEFT; auto dir = gui::Event::ROTATE_LEFT;
fmt::println("ROTATE TO: {},{} aim is {},{}",
target.x, target.y, rayview->aiming_at.x, rayview->aiming_at.y);
while(rayview->aiming_at != target) { while(rayview->aiming_at != target) {
send_event(dir); send_event(dir);
while(fsm.in_state(gui::State::ROTATING)) send_event(gui::Event::TICK); while(fsm.in_state(gui::State::ROTATING)) send_event(gui::Event::TICK);
} }
dbc::check(rayview->aiming_at == target, "failed to aim at target");
} }
ai::State Autowalker::update_state(ai::State start) { ai::State Autowalker::update_state(ai::State start) {
@ -160,10 +174,12 @@ ai::State Autowalker::update_state(ai::State start) {
ai::set(start, "no_more_enemies", enemy_count == 0); ai::set(start, "no_more_enemies", enemy_count == 0);
ai::set(start, "no_more_items", item_count == 0); ai::set(start, "no_more_items", item_count == 0);
ai::set(start, "enemy_found",
fsm.in_state(gui::State::IN_COMBAT) || // BUG: so isn't this wrong? we "find" an enemy when we are aiming at one
fsm.in_state(gui::State::ATTACKING)); ai::set(start, "enemy_found", found_enemy());
ai::set(start, "health_good", player_health_good()); ai::set(start, "health_good", player_health_good());
ai::set(start, "in_combat", ai::set(start, "in_combat",
fsm.in_state(gui::State::IN_COMBAT) || fsm.in_state(gui::State::IN_COMBAT) ||
fsm.in_state(gui::State::ATTACKING)); fsm.in_state(gui::State::ATTACKING));
@ -187,12 +203,15 @@ void Autowalker::handle_player_walk(ai::State& start, ai::State& goal) {
start = update_state(start); start = update_state(start);
auto a_plan = ai::plan("Host::actions", start, goal); auto a_plan = ai::plan("Host::actions", start, goal);
auto action = a_plan.script.front(); auto action = a_plan.script.front();
ai::dump_script("AUTOWALK", start, a_plan.script);
if(action.name == "find_enemy") { if(action.name == "find_enemy") {
status(L"FINDING ENEMY"); status(L"FINDING ENEMY");
auto paths = path_to_enemies(); auto paths = path_to_enemies();
process_move(paths); process_move(paths);
send_event(gui::Event::ATTACK); face_enemy();
} else if(action.name == "face_enemy") {
face_enemy();
} else if(action.name == "kill_enemy") { } else if(action.name == "kill_enemy") {
status(L"KILLING ENEMY"); status(L"KILLING ENEMY");
@ -213,7 +232,6 @@ void Autowalker::handle_player_walk(ai::State& start, ai::State& goal) {
close_status(); close_status();
log(L"FINAL ACTION! Autowalk done."); log(L"FINAL ACTION! Autowalk done.");
fsm.autowalking = false; fsm.autowalking = false;
ai::dump_script("AUTOWALK", start, a_plan.script);
} else { } else {
close_status(); close_status();
dbc::log(fmt::format("Unknown action: {}", action.name)); dbc::log(fmt::format("Unknown action: {}", action.name));
@ -295,23 +313,25 @@ void Autowalker::process_move(Pathing& paths) {
return; return;
} }
rotate_player(target); if(rayview->aiming_at != target) rotate_player(target);
// what are we aiming at? send_event(gui::Event::MOVE_FORWARD);
auto aimed_at = camera_aim();
if(aimed_at && world->has<components::InventoryItem>(aimed_at)) {
// NOTE: if we're aiming at an item then pick it up
// for now just loot it then close to get it off the map
send_event(gui::Event::LOOT_ITEM);
send_event(gui::Event::LOOT_OPEN);
} else {
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::found_enemy() {
auto world = GameDB::current_world();
auto aimed_at = camera_aim();
return aimed_at != DinkyECS::NONE && world->has<components::Combat>(aimed_at);
}
bool Autowalker::found_item() {
auto world = GameDB::current_world();
auto aimed_at = camera_aim();
return aimed_at != DinkyECS::NONE && world->has<components::InventoryItem>(aimed_at);
}
void Autowalker::send_event(gui::Event ev) { void Autowalker::send_event(gui::Event ev) {
fsm.event(ev); fsm.event(ev);
fsm.render(); fsm.render();
@ -345,8 +365,14 @@ bool Autowalker::face_enemy() {
auto [found, neighbors] = level.collision->neighbors(player_at.location, true); auto [found, neighbors] = level.collision->neighbors(player_at.location, true);
if(found) { if(found) {
fmt::println("FOUND ENEMIES:");
for(auto& ent : neighbors) {
auto enemy_pos = level.world->get<components::Position>(ent);
fmt::println("\t{}={},{}", ent, enemy_pos.location.x, enemy_pos.location.y);
}
auto enemy_pos = level.world->get<components::Position>(neighbors[0]); auto enemy_pos = level.world->get<components::Position>(neighbors[0]);
rotate_player(enemy_pos.location); if(rayview->aiming_at != enemy_pos.location) rotate_player(enemy_pos.location);
} else { } else {
dbc::log("No enemies nearby, moving on."); dbc::log("No enemies nearby, moving on.");
} }

@ -12,14 +12,17 @@ struct Autowalker {
bool map_opened_once = false; bool map_opened_once = false;
bool weapon_crafted = false; bool weapon_crafted = false;
gui::FSM& fsm; gui::FSM& fsm;
std::shared_ptr<Raycaster> rayview;
Autowalker(gui::FSM& fsm) Autowalker(gui::FSM& fsm)
: fsm(fsm) {} : fsm(fsm), rayview(fsm.$main_ui.$rayview) {}
void autowalk(); void autowalk();
void start_autowalk(); void start_autowalk();
void craft_weapon(); void craft_weapon();
void open_map(); void open_map();
bool found_enemy();
bool found_item();
void handle_window_events(); void handle_window_events();
void handle_boss_fight(); void handle_boss_fight();

@ -35,7 +35,6 @@ namespace gui {
$gui.set<Sprite>(gui_id, {"armored_knight"}); $gui.set<Sprite>(gui_id, {"armored_knight"});
} else { } else {
$gui.set<Rectangle>(gui_id, {}); $gui.set<Rectangle>(gui_id, {});
dbc::log("!!!!!!!!!!!!!!!!! is this used: $gui.set<ActionData>(gui_id, {make_any<string>(name)});");
if(name == "ritual_ui") { if(name == "ritual_ui") {
$gui.set<Clickable>(gui_id, { $gui.set<Clickable>(gui_id, {

@ -119,7 +119,9 @@ Point Map::center_camera(const Point &around, size_t view_x, size_t view_y) {
* in and out. * in and out.
*/ */
bool Map::random_walk(Point &out, bool random, int direction) { bool Map::random_walk(Point &out, bool random, int direction) {
return $paths.random_walk(out, random, direction); (void)random;
dbc::log("!!!!!!!!!!!!!!!!!!!!!!!!!!!! REWRITE THIS!");
return $paths.find_path(out, direction, true) != PathingResult::FAIL;
} }
bool Map::INVARIANT() { bool Map::INVARIANT() {

@ -13,7 +13,11 @@ namespace matrix {
int cell = map[it.y][it.x]; int cell = map[it.y][it.x];
if(int(it.x) == show_x && int(it.y) == show_y) { if(int(it.x) == show_x && int(it.y) == show_y) {
print("{:x}<", cell); if(cell == WALL_PATH_LIMIT) {
print("!<", cell);
} else {
print("{:x}<", cell);
}
} else if(cell == WALL_PATH_LIMIT) { } else if(cell == WALL_PATH_LIMIT) {
print("# "); print("# ");
} else if(cell == 0) { } else if(cell == 0) {

@ -74,73 +74,40 @@ void Pathing::clear_target(const Point &at) {
$input[at.y][at.x] = 1; $input[at.y][at.x] = 1;
} }
/* PathingResult Pathing::find_path(Point &out, int direction, bool diag)
* This is a weird discovery, but if you randomly select a starting point on
* the 8 compass, but only check 4 directions from there, it does the best
* pathing so far. It will walk around items, navigate around enemies, find
* paths through corners, etc. If you change slice_count/dist_count to just
* 4 it fails more frequently.
*
* Look in the autowalker.cpp:path_player function for an example of what
* I'm doing. I start with 4/8 and it finds paths 99% of the time, but
* if that fails I do a full 8 direction search. This weirdly finds the
* best directions to go more often.
*/
bool Pathing::random_walk(Point &out, bool random,
int direction, size_t slice_count, size_t dist_size)
{ {
bool zero_found = false; (void)diag;
// first 4 directions are n/s/e/w for most enemies
std::array<Point, DIRECTION_MAX> dirs{{
{out.x,out.y-1}, // north
{out.x+1,out.y}, // east
{out.x,out.y+1}, // south
{out.x-1,out.y}, // west
// the player and some enemies are more "agile"
{out.x+1,out.y-1}, // north east
{out.x+1,out.y+1}, // south east
{out.x-1,out.y+1}, // south west
{out.x-1,out.y-1} // north west
}};
dbc::check(slice_count <= dirs.size(), "slize_count must be <= DIRECTION_MAX");
dbc::check(dist_size <= dirs.size(), "dist_size must be <= DIRECTION_MAX");
// get the current dijkstra number // get the current dijkstra number
int cur = $paths[out.y][out.x]; int cur = $paths[out.y][out.x];
int target = cur;
// pick a random start of directions bool found = false;
int rand_start = Random::uniform<int>(0, dist_size);
// go through all possible directions // go through all possible directions
for(size_t i = 0; i < slice_count; i++) { for(matrix::box it{$paths, out.x, out.y, 1}; it.next();) {
// but start at the random start, effectively randomizing target = $paths[it.y][it.x];
// which valid direction to go // don't go through walls
// BUG: this might be wrong given the above ranom from 0-size if(target == WALL_PATH_LIMIT) continue;
Point dir = dirs[(i + rand_start) % dist_size];
if(!shiterator::inbounds($paths, dir.x, dir.y)) continue; //skip unpathable stuff int weight = cur - target;
int weight = cur - $paths[dir.y][dir.x];
if(weight == direction) { if(weight == direction) {
// no matter what we follow direct paths out = {(size_t)it.x, (size_t)it.y};
out = dir; found = true;
return true; break;
} else if(random && weight == 0) {
// if random is selected and it's a 0 path take it
out = dir;
return true;
} else if(weight == 0) { } else if(weight == 0) {
// otherwise keep the last zero path for after out = {(size_t)it.x, (size_t)it.y};
out = dir; found = true;
zero_found = true;
} }
} }
// if we reach this then either zero was found and if(target == 0) {
// zero_found is set true, or it wasn't and nothing found return PathingResult::FOUND;
return zero_found; } else if(!found) {
return PathingResult::FAIL;
} else {
return PathingResult::CONTINUE;
}
} }
bool Pathing::INVARIANT() { bool Pathing::INVARIANT() {

@ -7,7 +7,12 @@ using matrix::Matrix;
constexpr const int PATHING_TOWARD=1; constexpr const int PATHING_TOWARD=1;
constexpr const int PATHING_AWAY=-1; constexpr const int PATHING_AWAY=-1;
constexpr const int DIRECTION_MAX=8;
enum class PathingResult {
FAIL=0,
FOUND=1,
CONTINUE=2
};
class Pathing { class Pathing {
public: public:
@ -29,8 +34,7 @@ public:
Matrix &paths() { return $paths; } Matrix &paths() { return $paths; }
Matrix &input() { return $input; } Matrix &input() { return $input; }
int distance(Point to) { return $paths[to.y][to.x];} int distance(Point to) { return $paths[to.y][to.x];}
bool random_walk(Point &out, bool random, int direction, PathingResult find_path(Point &out, int direction, bool diag);
size_t slice_count=4, size_t dist_size=4);
bool INVARIANT(); bool INVARIANT();
}; };

@ -5,17 +5,70 @@
#include "pathing.hpp" #include "pathing.hpp"
#include "matrix.hpp" #include "matrix.hpp"
#include "ai.hpp" #include "ai.hpp"
#include "game_level.hpp"
#include <chrono>
#include <thread>
using namespace fmt; using namespace fmt;
using namespace nlohmann; using namespace nlohmann;
using std::string; using std::string;
using namespace components;
using namespace std::chrono_literals;
json load_test_pathing(const string &fname) { json load_test_pathing(const string &fname) {
std::ifstream infile(fname); std::ifstream infile(fname);
return json::parse(infile); return json::parse(infile);
} }
TEST_CASE("dijkstra algo test", "[pathing]") { TEST_CASE("multiple targets can path", "[pathing]") {
GameDB::init();
auto level = GameDB::create_level();
auto& walls_original = level.map->$walls;
auto walls_copy = walls_original;
Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)};
// first, put everything of this type as a target
level.world->query<Position, Combat>(
[&](const auto ent, auto& position, auto&) {
if(ent != level.player) {
paths.set_target(position.location);
}
});
level.world->query<Collision>(
[&](const auto ent, auto& collision) {
if(collision.has && ent != level.player) {
auto& pos = level.world->get<Position>(ent);
walls_copy[pos.location.y][pos.location.x] = WALL_VALUE;
}
});
paths.compute_paths(walls_copy);
auto pos = GameDB::player_position().location;
auto found = paths.find_path(pos, PATHING_TOWARD, false);
while(found == PathingResult::CONTINUE) {
fmt::println("\033[2J\033[1;1H");
matrix::dump("failed paths", paths.$paths, pos.x, pos.y);
std::this_thread::sleep_for(200ms);
found = paths.find_path(pos, PATHING_TOWARD, false);
}
fmt::println("\033[2J\033[1;1H");
matrix::dump("failed paths", paths.$paths, pos.x, pos.y);
if(found == PathingResult::FOUND) {
fmt::println("FOUND!");
} else if(found == PathingResult::FAIL) {
fmt::println("FAILED!");
std::this_thread::sleep_for(20000ms);
}
}
TEST_CASE("dijkstra algo test", "[pathing-old]") {
json data = load_test_pathing("./tests/dijkstra.json"); json data = load_test_pathing("./tests/dijkstra.json");
for(auto &test : data) { for(auto &test : data) {
@ -36,17 +89,3 @@ TEST_CASE("dijkstra algo test", "[pathing]") {
REQUIRE(pathing.$paths == expected); REQUIRE(pathing.$paths == expected);
} }
} }
TEST_CASE("random flood", "[pathing]") {
json data = load_test_pathing("./tests/dijkstra.json");
auto test = data[0];
Matrix expected = test["expected"];
Matrix walls = test["walls"];
Pathing pathing(walls[0].size(), walls.size());
pathing.$input = test["input"];
REQUIRE(pathing.INVARIANT());
pathing.compute_paths(walls);
}