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.
This commit is contained in:
parent
c894f6e094
commit
e92fd2b6f3
10 changed files with 169 additions and 111 deletions
4
Makefile
4
Makefile
|
@ -37,7 +37,7 @@ tracy_build:
|
|||
meson compile -j 10 -C builddir
|
||||
|
||||
test: asset_build build
|
||||
./builddir/runtests -d yes
|
||||
./builddir/runtests -d yes "[pathing]"
|
||||
|
||||
run: build test
|
||||
ifeq '$(OS)' 'Windows_NT'
|
||||
|
@ -60,7 +60,7 @@ clean:
|
|||
meson compile --clean -C builddir
|
||||
|
||||
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:
|
||||
powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" scripts\win_installer.ifp'
|
||||
|
|
|
@ -54,6 +54,19 @@
|
|||
"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",
|
||||
"cost": 5,
|
||||
|
@ -119,6 +132,7 @@
|
|||
"Host::actions":
|
||||
["find_enemy",
|
||||
"kill_enemy",
|
||||
"face_enemy",
|
||||
"collect_items",
|
||||
"use_healing"],
|
||||
"Enemy::actions":
|
||||
|
|
|
@ -34,20 +34,29 @@ Pathing compute_paths() {
|
|||
|
||||
Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)};
|
||||
|
||||
level.world->query<components::Position>(
|
||||
[&](const auto ent, auto& position) {
|
||||
// first, put everything of this type as a target
|
||||
level.world->query<components::Position, Comp>(
|
||||
[&](const auto ent, auto& position, auto&) {
|
||||
if(ent != level.player) {
|
||||
if(level.world->has<Comp>(ent)) {
|
||||
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;
|
||||
}
|
||||
paths.set_target(position.location);
|
||||
}
|
||||
});
|
||||
|
||||
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);
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
|
@ -57,7 +66,7 @@ DinkyECS::Entity Autowalker::camera_aim() {
|
|||
if(level.collision->something_there(fsm.$main_ui.$rayview->aiming_at)) {
|
||||
return level.collision->get(fsm.$main_ui.$rayview->aiming_at);
|
||||
} 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) {
|
||||
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
|
||||
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);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
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);
|
||||
return false;
|
||||
}
|
||||
|
@ -143,15 +154,18 @@ bool Autowalker::path_player(Pathing& paths, Point& target_out) {
|
|||
}
|
||||
|
||||
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 = 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) {
|
||||
send_event(dir);
|
||||
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) {
|
||||
|
@ -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_items", item_count == 0);
|
||||
ai::set(start, "enemy_found",
|
||||
fsm.in_state(gui::State::IN_COMBAT) ||
|
||||
fsm.in_state(gui::State::ATTACKING));
|
||||
|
||||
// BUG: so isn't this wrong? we "find" an enemy when we are aiming at one
|
||||
ai::set(start, "enemy_found", found_enemy());
|
||||
|
||||
ai::set(start, "health_good", player_health_good());
|
||||
|
||||
ai::set(start, "in_combat",
|
||||
fsm.in_state(gui::State::IN_COMBAT) ||
|
||||
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);
|
||||
auto a_plan = ai::plan("Host::actions", start, goal);
|
||||
auto action = a_plan.script.front();
|
||||
ai::dump_script("AUTOWALK", start, a_plan.script);
|
||||
|
||||
if(action.name == "find_enemy") {
|
||||
status(L"FINDING ENEMY");
|
||||
auto paths = path_to_enemies();
|
||||
process_move(paths);
|
||||
send_event(gui::Event::ATTACK);
|
||||
face_enemy();
|
||||
} else if(action.name == "face_enemy") {
|
||||
face_enemy();
|
||||
} else if(action.name == "kill_enemy") {
|
||||
status(L"KILLING ENEMY");
|
||||
|
||||
|
@ -213,7 +232,6 @@ void Autowalker::handle_player_walk(ai::State& start, ai::State& goal) {
|
|||
close_status();
|
||||
log(L"FINAL ACTION! Autowalk done.");
|
||||
fsm.autowalking = false;
|
||||
ai::dump_script("AUTOWALK", start, a_plan.script);
|
||||
} else {
|
||||
close_status();
|
||||
dbc::log(fmt::format("Unknown action: {}", action.name));
|
||||
|
@ -295,23 +313,25 @@ void Autowalker::process_move(Pathing& paths) {
|
|||
return;
|
||||
}
|
||||
|
||||
rotate_player(target);
|
||||
if(rayview->aiming_at != target) rotate_player(target);
|
||||
|
||||
// what are we aiming at?
|
||||
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);
|
||||
}
|
||||
send_event(gui::Event::MOVE_FORWARD);
|
||||
|
||||
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) {
|
||||
fsm.event(ev);
|
||||
fsm.render();
|
||||
|
@ -345,8 +365,14 @@ bool Autowalker::face_enemy() {
|
|||
auto [found, neighbors] = level.collision->neighbors(player_at.location, true);
|
||||
|
||||
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]);
|
||||
rotate_player(enemy_pos.location);
|
||||
if(rayview->aiming_at != enemy_pos.location) rotate_player(enemy_pos.location);
|
||||
} else {
|
||||
dbc::log("No enemies nearby, moving on.");
|
||||
}
|
||||
|
|
|
@ -12,14 +12,17 @@ struct Autowalker {
|
|||
bool map_opened_once = false;
|
||||
bool weapon_crafted = false;
|
||||
gui::FSM& fsm;
|
||||
std::shared_ptr<Raycaster> rayview;
|
||||
|
||||
Autowalker(gui::FSM& fsm)
|
||||
: fsm(fsm) {}
|
||||
: fsm(fsm), rayview(fsm.$main_ui.$rayview) {}
|
||||
|
||||
void autowalk();
|
||||
void start_autowalk();
|
||||
void craft_weapon();
|
||||
void open_map();
|
||||
bool found_enemy();
|
||||
bool found_item();
|
||||
|
||||
void handle_window_events();
|
||||
void handle_boss_fight();
|
||||
|
|
|
@ -35,7 +35,6 @@ namespace gui {
|
|||
$gui.set<Sprite>(gui_id, {"armored_knight"});
|
||||
} else {
|
||||
$gui.set<Rectangle>(gui_id, {});
|
||||
dbc::log("!!!!!!!!!!!!!!!!! is this used: $gui.set<ActionData>(gui_id, {make_any<string>(name)});");
|
||||
|
||||
if(name == "ritual_ui") {
|
||||
$gui.set<Clickable>(gui_id, {
|
||||
|
|
4
map.cpp
4
map.cpp
|
@ -119,7 +119,9 @@ Point Map::center_camera(const Point &around, size_t view_x, size_t view_y) {
|
|||
* in and out.
|
||||
*/
|
||||
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() {
|
||||
|
|
|
@ -13,7 +13,11 @@ namespace matrix {
|
|||
int cell = map[it.y][it.x];
|
||||
|
||||
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) {
|
||||
print("# ");
|
||||
} else if(cell == 0) {
|
||||
|
|
77
pathing.cpp
77
pathing.cpp
|
@ -74,73 +74,40 @@ void Pathing::clear_target(const Point &at) {
|
|||
$input[at.y][at.x] = 1;
|
||||
}
|
||||
|
||||
/*
|
||||
* 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)
|
||||
PathingResult Pathing::find_path(Point &out, int direction, bool diag)
|
||||
{
|
||||
bool zero_found = false;
|
||||
|
||||
// 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");
|
||||
(void)diag;
|
||||
|
||||
// get the current dijkstra number
|
||||
int cur = $paths[out.y][out.x];
|
||||
|
||||
// pick a random start of directions
|
||||
int rand_start = Random::uniform<int>(0, dist_size);
|
||||
int target = cur;
|
||||
bool found = false;
|
||||
|
||||
// go through all possible directions
|
||||
for(size_t i = 0; i < slice_count; i++) {
|
||||
// but start at the random start, effectively randomizing
|
||||
// which valid direction to go
|
||||
// BUG: this might be wrong given the above ranom from 0-size
|
||||
Point dir = dirs[(i + rand_start) % dist_size];
|
||||
if(!shiterator::inbounds($paths, dir.x, dir.y)) continue; //skip unpathable stuff
|
||||
int weight = cur - $paths[dir.y][dir.x];
|
||||
for(matrix::box it{$paths, out.x, out.y, 1}; it.next();) {
|
||||
target = $paths[it.y][it.x];
|
||||
// don't go through walls
|
||||
if(target == WALL_PATH_LIMIT) continue;
|
||||
|
||||
int weight = cur - target;
|
||||
|
||||
if(weight == direction) {
|
||||
// no matter what we follow direct paths
|
||||
out = dir;
|
||||
return true;
|
||||
} else if(random && weight == 0) {
|
||||
// if random is selected and it's a 0 path take it
|
||||
out = dir;
|
||||
return true;
|
||||
out = {(size_t)it.x, (size_t)it.y};
|
||||
found = true;
|
||||
break;
|
||||
} else if(weight == 0) {
|
||||
// otherwise keep the last zero path for after
|
||||
out = dir;
|
||||
zero_found = true;
|
||||
out = {(size_t)it.x, (size_t)it.y};
|
||||
found = true;
|
||||
}
|
||||
}
|
||||
|
||||
// if we reach this then either zero was found and
|
||||
// zero_found is set true, or it wasn't and nothing found
|
||||
return zero_found;
|
||||
if(target == 0) {
|
||||
return PathingResult::FOUND;
|
||||
} else if(!found) {
|
||||
return PathingResult::FAIL;
|
||||
} else {
|
||||
return PathingResult::CONTINUE;
|
||||
}
|
||||
}
|
||||
|
||||
bool Pathing::INVARIANT() {
|
||||
|
|
10
pathing.hpp
10
pathing.hpp
|
@ -7,7 +7,12 @@ using matrix::Matrix;
|
|||
|
||||
constexpr const int PATHING_TOWARD=1;
|
||||
constexpr const int PATHING_AWAY=-1;
|
||||
constexpr const int DIRECTION_MAX=8;
|
||||
|
||||
enum class PathingResult {
|
||||
FAIL=0,
|
||||
FOUND=1,
|
||||
CONTINUE=2
|
||||
};
|
||||
|
||||
class Pathing {
|
||||
public:
|
||||
|
@ -29,8 +34,7 @@ public:
|
|||
Matrix &paths() { return $paths; }
|
||||
Matrix &input() { return $input; }
|
||||
int distance(Point to) { return $paths[to.y][to.x];}
|
||||
bool random_walk(Point &out, bool random, int direction,
|
||||
size_t slice_count=4, size_t dist_size=4);
|
||||
PathingResult find_path(Point &out, int direction, bool diag);
|
||||
|
||||
bool INVARIANT();
|
||||
};
|
||||
|
|
|
@ -5,17 +5,70 @@
|
|||
#include "pathing.hpp"
|
||||
#include "matrix.hpp"
|
||||
#include "ai.hpp"
|
||||
#include "game_level.hpp"
|
||||
#include <chrono>
|
||||
#include <thread>
|
||||
|
||||
using namespace fmt;
|
||||
using namespace nlohmann;
|
||||
using std::string;
|
||||
using namespace components;
|
||||
using namespace std::chrono_literals;
|
||||
|
||||
json load_test_pathing(const string &fname) {
|
||||
std::ifstream infile(fname);
|
||||
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");
|
||||
|
||||
for(auto &test : data) {
|
||||
|
@ -36,17 +89,3 @@ TEST_CASE("dijkstra algo test", "[pathing]") {
|
|||
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);
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue