Now can load action specs from JSON.

master
Zed A. Shaw 3 weeks ago
parent 3d8a2d4342
commit 9d6dc2f5dd
  1. 2
      Makefile
  2. 54
      goap.cpp
  3. 39
      goap.hpp
  4. 112
      tests/goap.cpp

@ -41,7 +41,7 @@ clean:
meson compile --clean -C builddir
debug_test: build
gdb --nx -x .gdbinit --ex run --args builddir/runtests.exe -e
gdb --nx -x .gdbinit --ex run --args builddir/runtests.exe -e "[goap]"
win_installer:
powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" win_installer.ifp'

@ -2,18 +2,66 @@
#include "goap.hpp"
namespace ailol {
using namespace nlohmann;
bool is_subset(GOAPState& source, GOAPState& target) {
GOAPState result = source & target;
return result == target;
}
void Action::needs(int name, bool val) {
if(val) {
$positive_preconds[name] = true;
$negative_preconds[name] = false;
} else {
$negative_preconds[name] = true;
$positive_preconds[name] = false;
}
}
void Action::effect(int name, bool val) {
if(val) {
$positive_effects[name] = true;
$negative_effects[name] = false;
} else {
$negative_effects[name] = true;
$positive_effects[name] = false;
}
}
void Action::load(nlohmann::json& profile, nlohmann::json& config) {
dbc::check(config.contains("needs"),
fmt::format("Action.load({}): no 'needs' field", $name));
dbc::check(config.contains("effects"),
fmt::format("Action.load({}): no 'effects' field", $name));
for(auto& [name_key, value] : profile.items()) {
dbc::check(value < STATE_MAX, fmt::format("Action.load({}): profile field {} has value {} greater than STATE_MAX {}", $name, (std::string)name_key, (int)value, STATE_MAX));
}
for(auto& [name_key, value] : config["needs"].items()) {
dbc::check(profile.contains(name_key), fmt::format("Action.load({}): profile does not have name {}", $name, name_key));
int name = profile[name_key].template get<int>();
needs(name, bool(value));
}
for(auto& [name_key, value] : config["effects"].items()) {
dbc::check(profile.contains(name_key), fmt::format("Action.load({}): profile does not have name {}", $name, name_key));
int name = profile[name_key].template get<int>();
effect(name, bool(value));
}
}
bool Action::can_effect(GOAPState& state) {
return ((state & positive_preconds) == positive_preconds) &&
((state & negative_preconds) == ALL_ZERO);
return ((state & $positive_preconds) == $positive_preconds) &&
((state & $negative_preconds) == ALL_ZERO);
}
GOAPState Action::apply_effect(GOAPState& state) {
return (state | positive_effects) & ~negative_effects;
return (state | $positive_effects) & ~$negative_effects;
}
int distance_to_goal(GOAPState& from, GOAPState& to) {

@ -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) { }
void set_precond(int name, bool val) {
if(val) {
positive_preconds[name] = true;
} else {
negative_preconds[name] = true;
}
}
$name(name), $cost(cost) { }
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);
}
};

@ -5,6 +5,7 @@
using namespace dbc;
using namespace ailol;
using namespace nlohmann;
TEST_CASE("worldstate works", "[goap]") {
enum StateNames {
@ -24,8 +25,8 @@ TEST_CASE("worldstate works", "[goap]") {
goal[ENEMY_DEAD] = true;
Action move_closer("move_closer", 10);
move_closer.set_precond(ENEMY_IN_RANGE, false);
move_closer.set_effect(ENEMY_IN_RANGE, true);
move_closer.needs(ENEMY_IN_RANGE, false);
move_closer.effect(ENEMY_IN_RANGE, true);
REQUIRE(move_closer.can_effect(start));
auto after_move_state = move_closer.apply_effect(start);
@ -38,9 +39,9 @@ TEST_CASE("worldstate works", "[goap]") {
REQUIRE(distance_to_goal(start, after_move_state) == 1);
Action kill_it("kill_it", 10);
kill_it.set_precond(ENEMY_IN_RANGE, true);
kill_it.set_precond(ENEMY_DEAD, false);
kill_it.set_effect(ENEMY_DEAD, true);
kill_it.needs(ENEMY_IN_RANGE, true);
kill_it.needs(ENEMY_DEAD, false);
kill_it.effect(ENEMY_DEAD, true);
REQUIRE(!kill_it.can_effect(start));
REQUIRE(kill_it.can_effect(after_move_state));
@ -73,13 +74,19 @@ TEST_CASE("basic feature tests", "[goap]") {
goal[ENEMY_DEAD] = true;
Action move_closer("move_closer", 10);
move_closer.set_precond(ENEMY_IN_RANGE, false);
move_closer.set_effect(ENEMY_IN_RANGE, true);
move_closer.needs(ENEMY_IN_RANGE, false);
move_closer.effect(ENEMY_IN_RANGE, true);
Action kill_it("kill_it", 10);
kill_it.set_precond(ENEMY_IN_RANGE, true);
kill_it.set_precond(ENEMY_DEAD, false);
kill_it.set_effect(ENEMY_DEAD, true);
kill_it.needs(ENEMY_IN_RANGE, true);
// this is duplicated on purpose to confirm that setting
// a positive then a negative properly cancels out
kill_it.needs(ENEMY_DEAD, true);
kill_it.needs(ENEMY_DEAD, false);
// same thing with effects
kill_it.effect(ENEMY_DEAD, false);
kill_it.effect(ENEMY_DEAD, true);
// order seems to matter which is wrong
actions.push_back(kill_it);
@ -91,7 +98,6 @@ TEST_CASE("basic feature tests", "[goap]") {
auto state = start;
for(auto& action : *result) {
fmt::println("ACTION: {}", action.name);
state = action.apply_effect(state);
}
@ -100,53 +106,79 @@ TEST_CASE("basic feature tests", "[goap]") {
TEST_CASE("wargame test from cppGOAP", "[goap]") {
std::vector<Action> actions;
// Constants for the various states are helpful to keep us from
// accidentally mistyping a state name.
enum WarGameStates {
target_acquired,
target_lost,
target_in_warhead_range,
target_dead
};
auto profile = R"({
"target_acquired": 0,
"target_lost": 1,
"target_in_warhead_range": 2,
"target_dead": 3
})"_json;
// Now establish all the possible actions for the action pool
// In this example we're providing the AI some different FPS actions
Action spiral("searchSpiral", 5);
spiral.set_precond(target_acquired, false);
spiral.set_precond(target_lost, true);
spiral.set_effect(target_acquired, true);
auto config = R"({
"needs": {
"target_acquired": false,
"target_lost": true
},
"effects": {
"target_acquired": true
}
})"_json;
spiral.load(profile, config);
actions.push_back(spiral);
Action serpentine("searchSerpentine", 5);
serpentine.set_precond(target_acquired, false);
serpentine.set_precond(target_lost, false);
serpentine.set_effect(target_acquired, true);
config = R"({
"needs": {
"target_acquired": false,
"target_lost": false
},
"effects": {
"target_acquired": true
}
})"_json;
serpentine.load(profile, config);
actions.push_back(serpentine);
Action intercept("interceptTarget", 5);
intercept.set_precond(target_acquired, true);
intercept.set_precond(target_dead, false);
intercept.set_effect(target_in_warhead_range, true);
config = R"({
"needs": {
"target_acquired": true,
"target_dead": false
},
"effects": {
"target_in_warhead_range": true
}
})"_json;
intercept.load(profile, config);
actions.push_back(intercept);
Action detonateNearTarget("detonateNearTarget", 5);
detonateNearTarget.set_precond(target_in_warhead_range, true);
detonateNearTarget.set_precond(target_acquired, true);
detonateNearTarget.set_precond(target_dead, false);
detonateNearTarget.set_effect(target_dead, true);
config = R"({
"needs": {
"target_in_warhead_range": true,
"target_acquired": true,
"target_dead": false
},
"effects": {
"target_dead": true
}
})"_json;
detonateNearTarget.load(profile, config);
actions.push_back(detonateNearTarget);
// Here's the initial state...
GOAPState initial_state;
initial_state[target_acquired] = false;
initial_state[target_lost] = true;
initial_state[target_in_warhead_range] = false;
initial_state[target_dead] = false;
initial_state[profile["target_acquired"]] = false;
initial_state[profile["target_lost"]] = true;
initial_state[profile["target_in_warhead_range"]] = false;
initial_state[profile["target_dead"]] = false;
// ...and the goal state
GOAPState goal_target_dead;
goal_target_dead[target_dead] = true;
goal_target_dead[profile["target_dead"]] = true;
auto result = plan_actions(actions, initial_state, goal_target_dead);
REQUIRE(result != std::nullopt);
@ -154,9 +186,9 @@ TEST_CASE("wargame test from cppGOAP", "[goap]") {
auto state = initial_state;
for(auto& action : *result) {
fmt::println("ACTION: {}", action.name);
fmt::println("ACTION: {}", action.$name);
state = action.apply_effect(state);
}
REQUIRE(state[target_dead]);
REQUIRE(state[profile["target_dead"]]);
}

Loading…
Cancel
Save