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:
Zed A. Shaw 2025-08-30 10:48:52 -04:00
parent c894f6e094
commit e92fd2b6f3
10 changed files with 169 additions and 111 deletions

View file

@ -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);
}