A simple A* pathing function that works on maps, but I'll be changing it to do the GOAP pathing.
This commit is contained in:
parent
73b0600039
commit
a34becdaeb
7 changed files with 133 additions and 10 deletions
2
Makefile
2
Makefile
|
@ -22,7 +22,7 @@ tracy_build:
|
||||||
meson compile -j 10 -C builddir
|
meson compile -j 10 -C builddir
|
||||||
|
|
||||||
test: build
|
test: build
|
||||||
./builddir/runtests
|
./builddir/runtests "[goap]"
|
||||||
|
|
||||||
run: build test
|
run: build test
|
||||||
powershell "cp ./builddir/zedcaster.exe ."
|
powershell "cp ./builddir/zedcaster.exe ."
|
||||||
|
|
|
@ -134,6 +134,11 @@ Libraries Needed:
|
||||||
* libxi-dev
|
* libxi-dev
|
||||||
* libfreetype-dev
|
* libfreetype-dev
|
||||||
|
|
||||||
|
It uses c++ so you may need to install a libg++ or libc++ for your system. Usually this is all you
|
||||||
|
need:
|
||||||
|
|
||||||
|
apt install build-essential
|
||||||
|
|
||||||
## OSX Build Notes
|
## OSX Build Notes
|
||||||
|
|
||||||
* Quite a bad experience. Need to install Python, cmake, meson, and ninja all which are in homebrew but if you don't use homebrew then this is a problem.
|
* Quite a bad experience. Need to install Python, cmake, meson, and ninja all which are in homebrew but if you don't use homebrew then this is a problem.
|
||||||
|
|
13
lel.cpp
13
lel.cpp
|
@ -43,17 +43,13 @@ namespace lel {
|
||||||
|
|
||||||
for(auto& row : grid) {
|
for(auto& row : grid) {
|
||||||
size_t columns = row.size();
|
size_t columns = row.size();
|
||||||
|
|
||||||
for(auto& name : row) {
|
|
||||||
if(name == "_") continue;
|
|
||||||
auto& cell = cells.at(name);
|
|
||||||
|
|
||||||
int cell_width = grid_w / columns;
|
int cell_width = grid_w / columns;
|
||||||
dbc::check(cell_width > 0, "invalid cell width calc");
|
dbc::check(cell_width > 0, "invalid cell width calc");
|
||||||
dbc::check(cell_height > 0, "invalid cell height calc");
|
dbc::check(cell_height > 0, "invalid cell height calc");
|
||||||
|
|
||||||
cell.x = grid_x + (cell.col * cell_width);
|
for(auto& name : row) {
|
||||||
cell.y = grid_y + (cell.row * cell_height);
|
if(name == "_") continue;
|
||||||
|
auto& cell = cells.at(name);
|
||||||
|
|
||||||
// ZED: getting a bit hairy but this should work
|
// ZED: getting a bit hairy but this should work
|
||||||
if(cell.percent) {
|
if(cell.percent) {
|
||||||
|
@ -73,6 +69,9 @@ namespace lel {
|
||||||
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
|
dbc::check(cell.h > 0, fmt::format("invalid height cell {}", name));
|
||||||
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
|
dbc::check(cell.w > 0, fmt::format("invalid width cell {}", name));
|
||||||
|
|
||||||
|
cell.x = grid_x + (cell.col * cell_width);
|
||||||
|
cell.y = grid_y + (cell.row * cell_height);
|
||||||
|
|
||||||
// keep the midpoint since it is used a lot
|
// keep the midpoint since it is used a lot
|
||||||
cell.mid_x = std::midpoint(cell.x, cell.x + cell.w);
|
cell.mid_x = std::midpoint(cell.x, cell.x + cell.w);
|
||||||
cell.mid_y = std::midpoint(cell.y, cell.y + cell.h);
|
cell.mid_y = std::midpoint(cell.y, cell.y + cell.h);
|
||||||
|
|
2
main.cpp
2
main.cpp
|
@ -8,7 +8,7 @@ int main(int argc, char* argv[]) {
|
||||||
try {
|
try {
|
||||||
textures::init();
|
textures::init();
|
||||||
sound::init();
|
sound::init();
|
||||||
sound::mute(false);
|
sound::mute(true);
|
||||||
gui::FSM main;
|
gui::FSM main;
|
||||||
main.event(gui::Event::STARTED);
|
main.event(gui::Event::STARTED);
|
||||||
Autowalker walker(main);
|
Autowalker walker(main);
|
||||||
|
|
|
@ -20,7 +20,7 @@ namespace matrix {
|
||||||
} else if(cell == WALL_PATH_LIMIT) {
|
} else if(cell == WALL_PATH_LIMIT) {
|
||||||
print("# ");
|
print("# ");
|
||||||
} else if(cell > 15) {
|
} else if(cell > 15) {
|
||||||
print("* ");
|
print("[{:x}]", cell);
|
||||||
} else {
|
} else {
|
||||||
print("{:x} ", cell);
|
print("{:x} ", cell);
|
||||||
}
|
}
|
||||||
|
|
|
@ -120,6 +120,14 @@ sources = [
|
||||||
'textures.cpp',
|
'textures.cpp',
|
||||||
'tilemap.cpp',
|
'tilemap.cpp',
|
||||||
'worldbuilder.cpp',
|
'worldbuilder.cpp',
|
||||||
|
'goap2/Action.cpp',
|
||||||
|
'goap2/Action.h',
|
||||||
|
'goap2/Node.cpp',
|
||||||
|
'goap2/Node.h',
|
||||||
|
'goap2/Planner.cpp',
|
||||||
|
'goap2/Planner.h',
|
||||||
|
'goap2/WorldState.cpp',
|
||||||
|
'goap2/WorldState.h',
|
||||||
]
|
]
|
||||||
|
|
||||||
executable('runtests', sources + [
|
executable('runtests', sources + [
|
||||||
|
@ -130,6 +138,7 @@ executable('runtests', sources + [
|
||||||
'tests/dbc.cpp',
|
'tests/dbc.cpp',
|
||||||
'tests/dinkyecs.cpp',
|
'tests/dinkyecs.cpp',
|
||||||
'tests/fsm.cpp',
|
'tests/fsm.cpp',
|
||||||
|
'tests/goap.cpp',
|
||||||
'tests/guecs.cpp',
|
'tests/guecs.cpp',
|
||||||
'tests/inventory.cpp',
|
'tests/inventory.cpp',
|
||||||
'tests/lel.cpp',
|
'tests/lel.cpp',
|
||||||
|
|
110
tests/goap.cpp
Normal file
110
tests/goap.cpp
Normal file
|
@ -0,0 +1,110 @@
|
||||||
|
#include <catch2/catch_test_macros.hpp>
|
||||||
|
#include "dbc.hpp"
|
||||||
|
#include <iostream>
|
||||||
|
#include <vector>
|
||||||
|
#include "levelmanager.hpp"
|
||||||
|
#include "matrix.hpp"
|
||||||
|
#include "components.hpp"
|
||||||
|
#include <bitset>
|
||||||
|
|
||||||
|
using namespace dbc;
|
||||||
|
using namespace components;
|
||||||
|
|
||||||
|
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 h(Point from, Point to) {
|
||||||
|
return std::hypot(float(from.x) - float(to.x),
|
||||||
|
float(from.y) - float(to.y));
|
||||||
|
}
|
||||||
|
|
||||||
|
inline 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 = 10000;
|
||||||
|
|
||||||
|
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] * 1000;
|
||||||
|
float tentative_g_score = g_score[current] + d_score;
|
||||||
|
float neighbor_g_score = g_score.contains(neighbor) ? g_score[neighbor] : 10000.0f;
|
||||||
|
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;
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_CASE("basic feature tests", "[goap]") {
|
||||||
|
for(int i = 0; i < 10; i++) {
|
||||||
|
LevelManager levels;
|
||||||
|
GameLevel level = levels.current();
|
||||||
|
auto &map = *level.map;
|
||||||
|
|
||||||
|
auto& player_at = level.world->get<Position>(level.player);
|
||||||
|
// matrix::dump("A* PLAYER", map.walls(), player_at.location.x, player_at.location.y);
|
||||||
|
|
||||||
|
level.world->query<Position, Combat>([&](const auto ent, auto& enemy_at, auto&) {
|
||||||
|
if(ent != level.player) {
|
||||||
|
auto result = path_to_player(map.walls(), enemy_at.location, player_at.location);
|
||||||
|
REQUIRE(result != std::nullopt);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Add a link
Reference in a new issue