GOAP is now working in a basic way, time to clean it up.
This commit is contained in:
parent
2992193447
commit
01525388ec
6 changed files with 183 additions and 179 deletions
2
Makefile
2
Makefile
|
@ -22,7 +22,7 @@ tracy_build:
|
|||
meson compile -j 10 -C builddir
|
||||
|
||||
test: build
|
||||
./builddir/runtests "[goap]"
|
||||
./builddir/runtests
|
||||
|
||||
run: build test
|
||||
powershell "cp ./builddir/zedcaster.exe ."
|
||||
|
|
113
goap.cpp
Normal file
113
goap.cpp
Normal file
|
@ -0,0 +1,113 @@
|
|||
#include "dbc.hpp"
|
||||
#include "goap.hpp"
|
||||
|
||||
namespace ailol {
|
||||
bool is_subset(GOAPState& source, GOAPState& target) {
|
||||
GOAPState result = source & target;
|
||||
return result == target;
|
||||
}
|
||||
|
||||
bool Action::can_effect(GOAPState& state) {
|
||||
for(auto [name, setting] : preconds) {
|
||||
if(state[name] != setting) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
GOAPState Action::apply_effect(GOAPState& state) {
|
||||
// RCR SUGGEST: state = (state & ~write_mask) | effect
|
||||
auto state_cp = state;
|
||||
|
||||
for(auto [name, setting] : effects) {
|
||||
state_cp[name] = setting;
|
||||
}
|
||||
|
||||
return state_cp;
|
||||
}
|
||||
|
||||
int distance_to_goal(GOAPState& from, GOAPState& to) {
|
||||
auto result = from ^ to;
|
||||
return result.count();
|
||||
}
|
||||
|
||||
AStarPath reconstruct_path(std::unordered_map<Action, Action>& came_from, Action& current) {
|
||||
AStarPath total_path{current};
|
||||
int count = 0;
|
||||
|
||||
while(came_from.contains(current) && count++ < 10) {
|
||||
current = came_from.at(current);
|
||||
if(current != FINAL_ACTION) {
|
||||
total_path.push_front(current);
|
||||
}
|
||||
}
|
||||
|
||||
return total_path;
|
||||
}
|
||||
|
||||
inline int h(GOAPState& start, GOAPState& goal) {
|
||||
return distance_to_goal(start, goal);
|
||||
}
|
||||
|
||||
inline int d(GOAPState& start, GOAPState& goal) {
|
||||
return distance_to_goal(start, goal);
|
||||
}
|
||||
|
||||
ActionState find_lowest(std::unordered_map<ActionState, int>& open_set) {
|
||||
dbc::check(!open_set.empty(), "open set can't be empty in find_lowest");
|
||||
const ActionState *result = nullptr;
|
||||
int lowest_score = SCORE_MAX;
|
||||
|
||||
for(auto& kv : open_set) {
|
||||
if(kv.second < lowest_score) {
|
||||
lowest_score = kv.second;
|
||||
result = &kv.first;
|
||||
}
|
||||
}
|
||||
|
||||
return *result;
|
||||
}
|
||||
|
||||
std::optional<AStarPath> plan_actions(std::vector<Action>& actions, GOAPState& start, GOAPState& goal) {
|
||||
std::unordered_map<ActionState, int> open_set;
|
||||
std::unordered_map<Action, Action> came_from;
|
||||
std::unordered_map<GOAPState, int> g_score;
|
||||
|
||||
ActionState start_state{FINAL_ACTION, start};
|
||||
|
||||
g_score[start] = 0;
|
||||
open_set[start_state] = g_score[start] + h(start, goal);
|
||||
|
||||
while(!open_set.empty()) {
|
||||
auto current = find_lowest(open_set);
|
||||
|
||||
if(is_subset(current.state, goal)) {
|
||||
return std::make_optional<AStarPath>(reconstruct_path(came_from, current.action));
|
||||
}
|
||||
|
||||
open_set.erase(current);
|
||||
|
||||
for(auto& neighbor_action : actions) {
|
||||
// calculate the GOAPState being current/neighbor
|
||||
if(!neighbor_action.can_effect(current.state)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto neighbor = neighbor_action.apply_effect(current.state);
|
||||
int d_score = d(current.state, neighbor);
|
||||
int tentative_g_score = g_score[current.state] + d_score;
|
||||
int neighbor_g_score = g_score.contains(neighbor) ? g_score[neighbor] : SCORE_MAX;
|
||||
if(tentative_g_score < neighbor_g_score) {
|
||||
came_from.insert_or_assign(neighbor_action, current.action);
|
||||
|
||||
g_score[neighbor] = tentative_g_score;
|
||||
// open_set gets the fScore
|
||||
ActionState neighbor_as{neighbor_action, neighbor};
|
||||
open_set[neighbor_as] = tentative_g_score + h(neighbor, goal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
}
|
65
goap.hpp
Normal file
65
goap.hpp
Normal file
|
@ -0,0 +1,65 @@
|
|||
#pragma once
|
||||
#include <vector>
|
||||
#include "matrix.hpp"
|
||||
#include <bitset>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
|
||||
namespace ailol {
|
||||
constexpr const int SCORE_MAX = std::numeric_limits<int>::max();
|
||||
constexpr const size_t STATE_MAX = 16;
|
||||
|
||||
using GOAPState = std::bitset<STATE_MAX>;
|
||||
|
||||
struct Action {
|
||||
std::string name;
|
||||
int cost = 0;
|
||||
|
||||
std::unordered_map<int, bool> preconds;
|
||||
std::unordered_map<int, bool> effects;
|
||||
|
||||
Action(std::string name, int cost) :
|
||||
name(name), cost(cost) {}
|
||||
|
||||
bool can_effect(GOAPState& state);
|
||||
GOAPState apply_effect(GOAPState& state);
|
||||
|
||||
bool operator==(const Action& other) const {
|
||||
return other.name == name;
|
||||
}
|
||||
};
|
||||
|
||||
using AStarPath = std::deque<Action>;
|
||||
|
||||
const Action FINAL_ACTION("END", SCORE_MAX);
|
||||
|
||||
struct ActionState {
|
||||
Action action;
|
||||
GOAPState state;
|
||||
|
||||
ActionState(Action action, GOAPState state) :
|
||||
action(action), state(state) {}
|
||||
|
||||
bool operator==(const ActionState& other) const {
|
||||
return other.action == action && other.state == state;
|
||||
}
|
||||
};
|
||||
|
||||
bool is_subset(GOAPState& source, GOAPState& target);
|
||||
|
||||
int distance_to_goal(GOAPState& from, GOAPState& to);
|
||||
|
||||
std::optional<AStarPath> plan_actions(std::vector<Action>& actions, GOAPState& start, GOAPState& goal);
|
||||
}
|
||||
|
||||
template<> struct std::hash<ailol::Action> {
|
||||
size_t operator()(const ailol::Action& p) const {
|
||||
return std::hash<std::string>{}(p.name);
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct std::hash<ailol::ActionState> {
|
||||
size_t operator()(const ailol::ActionState& p) const {
|
||||
return std::hash<ailol::Action>{}(p.action) ^ std::hash<ailol::GOAPState>{}(p.state);
|
||||
}
|
||||
};
|
|
@ -93,6 +93,7 @@ sources = [
|
|||
'config.cpp',
|
||||
'dbc.cpp',
|
||||
'devices.cpp',
|
||||
'goap.cpp',
|
||||
'guecs.cpp',
|
||||
'gui_fsm.cpp',
|
||||
'inventory.cpp',
|
||||
|
@ -120,14 +121,6 @@ sources = [
|
|||
'textures.cpp',
|
||||
'tilemap.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 + [
|
||||
|
|
172
tests/goap.cpp
172
tests/goap.cpp
|
@ -1,177 +1,9 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#include "dbc.hpp"
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
#include "levelmanager.hpp"
|
||||
#include "matrix.hpp"
|
||||
#include "components.hpp"
|
||||
#include <bitset>
|
||||
#include <limits>
|
||||
#include "goap.hpp"
|
||||
|
||||
using namespace dbc;
|
||||
using namespace components;
|
||||
|
||||
constexpr const int SCORE_MAX = std::numeric_limits<int>::max();
|
||||
constexpr const size_t STATE_MAX = 16;
|
||||
|
||||
using GOAPState = std::bitset<STATE_MAX>;
|
||||
|
||||
bool is_subset(GOAPState& source, GOAPState& target) {
|
||||
GOAPState result = source & target;
|
||||
return result == target;
|
||||
}
|
||||
|
||||
struct Action {
|
||||
std::string name;
|
||||
int cost = 0;
|
||||
|
||||
std::unordered_map<int, bool> preconds;
|
||||
std::unordered_map<int, bool> effects;
|
||||
|
||||
Action(std::string name, int cost) :
|
||||
name(name), cost(cost) {}
|
||||
|
||||
bool can_effect(GOAPState& state) {
|
||||
for(auto [name, setting] : preconds) {
|
||||
if(state[name] != setting) return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
GOAPState apply_effect(GOAPState& state) {
|
||||
// RCR SUGGEST: state = (state & ~write_mask) | effect
|
||||
auto state_cp = state;
|
||||
|
||||
for(auto [name, setting] : effects) {
|
||||
state_cp[name] = setting;
|
||||
}
|
||||
|
||||
return state_cp;
|
||||
}
|
||||
|
||||
bool operator==(const Action& other) const {
|
||||
return other.name == name;
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct std::hash<Action> {
|
||||
size_t operator()(const Action& p) const {
|
||||
return std::hash<std::string>{}(p.name);
|
||||
}
|
||||
};
|
||||
|
||||
const Action FINAL_ACTION("END", SCORE_MAX);
|
||||
|
||||
struct ActionState {
|
||||
Action action;
|
||||
GOAPState state;
|
||||
|
||||
ActionState(Action action, GOAPState state) :
|
||||
action(action), state(state) {}
|
||||
|
||||
bool operator==(const ActionState& other) const {
|
||||
return other.action == action && other.state == state;
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct std::hash<ActionState> {
|
||||
size_t operator()(const ActionState& p) const {
|
||||
return std::hash<Action>{}(p.action) ^ std::hash<GOAPState>{}(p.state);
|
||||
}
|
||||
};
|
||||
|
||||
using AStarPath = std::deque<Action>;
|
||||
|
||||
int distance_to_goal(GOAPState& from, GOAPState& to) {
|
||||
auto result = from ^ to;
|
||||
return result.count();
|
||||
}
|
||||
|
||||
|
||||
AStarPath reconstruct_path(std::unordered_map<Action, Action>& came_from, Action& current) {
|
||||
AStarPath total_path{current};
|
||||
int count = 0;
|
||||
|
||||
while(came_from.contains(current) && count++ < 10) {
|
||||
current = came_from.at(current);
|
||||
if(current != FINAL_ACTION) {
|
||||
total_path.push_front(current);
|
||||
}
|
||||
}
|
||||
|
||||
return total_path;
|
||||
}
|
||||
|
||||
inline int h(GOAPState& start, GOAPState& goal) {
|
||||
return distance_to_goal(start, goal);
|
||||
}
|
||||
|
||||
inline int d(GOAPState& start, GOAPState& goal) {
|
||||
return distance_to_goal(start, goal);
|
||||
}
|
||||
|
||||
inline ActionState find_lowest(std::unordered_map<ActionState, int>& open_set) {
|
||||
dbc::check(!open_set.empty(), "open set can't be empty in find_lowest");
|
||||
const ActionState *result = nullptr;
|
||||
int lowest_score = SCORE_MAX;
|
||||
|
||||
for(auto& kv : open_set) {
|
||||
if(kv.second < lowest_score) {
|
||||
lowest_score = kv.second;
|
||||
result = &kv.first;
|
||||
}
|
||||
}
|
||||
|
||||
return *result;
|
||||
}
|
||||
|
||||
|
||||
// map is the list of possible actions
|
||||
// start and goal are two world states
|
||||
std::optional<AStarPath> plan_actions(std::vector<Action>& actions, GOAPState& start, GOAPState& goal) {
|
||||
std::unordered_map<ActionState, int> open_set;
|
||||
std::unordered_map<Action, Action> came_from;
|
||||
std::unordered_map<GOAPState, int> g_score;
|
||||
|
||||
ActionState start_state{FINAL_ACTION, start};
|
||||
|
||||
g_score[start] = 0;
|
||||
open_set[start_state] = g_score[start] + h(start, goal);
|
||||
|
||||
while(!open_set.empty()) {
|
||||
auto current = find_lowest(open_set);
|
||||
|
||||
if(is_subset(current.state, goal)) {
|
||||
return std::make_optional<AStarPath>(reconstruct_path(came_from, current.action));
|
||||
}
|
||||
|
||||
open_set.erase(current);
|
||||
|
||||
for(auto& neighbor_action : actions) {
|
||||
// calculate the GOAPState being current/neighbor
|
||||
if(!neighbor_action.can_effect(current.state)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
auto neighbor = neighbor_action.apply_effect(current.state);
|
||||
int d_score = d(current.state, neighbor);
|
||||
int tentative_g_score = g_score[current.state] + d_score;
|
||||
int neighbor_g_score = g_score.contains(neighbor) ? g_score[neighbor] : SCORE_MAX;
|
||||
if(tentative_g_score < neighbor_g_score) {
|
||||
came_from.insert_or_assign(neighbor_action, current.action);
|
||||
|
||||
g_score[neighbor] = tentative_g_score;
|
||||
// open_set gets the fScore
|
||||
ActionState neighbor_as{neighbor_action, neighbor};
|
||||
open_set[neighbor_as] = tentative_g_score + h(neighbor, goal);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
using namespace ailol;
|
||||
|
||||
TEST_CASE("worldstate works", "[goap]") {
|
||||
enum StateNames {
|
||||
|
|
|
@ -4,6 +4,7 @@
|
|||
#include <fstream>
|
||||
#include "pathing.hpp"
|
||||
#include "matrix.hpp"
|
||||
#include "goap.hpp"
|
||||
|
||||
using namespace fmt;
|
||||
using namespace nlohmann;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue