Now can load action specs from JSON.

This commit is contained in:
Zed A. Shaw 2025-03-10 14:07:31 -04:00
parent 3d8a2d4342
commit 9d6dc2f5dd
4 changed files with 138 additions and 69 deletions

View file

@ -4,10 +4,11 @@
#include <bitset>
#include <limits>
#include <optional>
#include <nlohmann/json.hpp>
namespace ailol {
constexpr const int SCORE_MAX = std::numeric_limits<int>::max();
constexpr const size_t STATE_MAX = 93;
constexpr const size_t STATE_MAX = 32;
using GOAPState = std::bitset<STATE_MAX>;
@ -15,39 +16,27 @@ namespace ailol {
const GOAPState ALL_ONES = ~ALL_ZERO;
struct Action {
std::string name;
int cost = 0;
std::string $name;
int $cost = 0;
GOAPState positive_preconds;
GOAPState negative_preconds;
GOAPState $positive_preconds;
GOAPState $negative_preconds;
GOAPState positive_effects;
GOAPState negative_effects;
GOAPState $positive_effects;
GOAPState $negative_effects;
Action(std::string name, int cost) :
name(name), cost(cost) { }
$name(name), $cost(cost) { }
void set_precond(int name, bool val) {
if(val) {
positive_preconds[name] = true;
} else {
negative_preconds[name] = true;
}
}
void set_effect(int name, bool val) {
if(val) {
positive_effects[name] = true;
} else {
negative_effects[name] = true;
}
}
void needs(int name, bool val);
void effect(int name, bool val);
void load(nlohmann::json &profile, nlohmann::json& config);
bool can_effect(GOAPState& state);
GOAPState apply_effect(GOAPState& state);
bool operator==(const Action& other) const {
return other.name == name;
return other.$name == $name;
}
};
@ -76,7 +65,7 @@ namespace ailol {
template<> struct std::hash<ailol::Action> {
size_t operator()(const ailol::Action& p) const {
return std::hash<std::string>{}(p.name);
return std::hash<std::string>{}(p.$name);
}
};