GOAP is now working in a basic way, time to clean it up.

This commit is contained in:
Zed A. Shaw 2025-03-10 11:06:26 -04:00
parent 2992193447
commit 01525388ec
6 changed files with 183 additions and 179 deletions

65
goap.hpp Normal file
View 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);
}
};