Now can load action specs from JSON.
This commit is contained in:
parent
3d8a2d4342
commit
9d6dc2f5dd
4 changed files with 138 additions and 69 deletions
2
Makefile
2
Makefile
|
@ -41,7 +41,7 @@ clean:
|
||||||
meson compile --clean -C builddir
|
meson compile --clean -C builddir
|
||||||
|
|
||||||
debug_test: build
|
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:
|
win_installer:
|
||||||
powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" win_installer.ifp'
|
powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" win_installer.ifp'
|
||||||
|
|
54
goap.cpp
54
goap.cpp
|
@ -2,18 +2,66 @@
|
||||||
#include "goap.hpp"
|
#include "goap.hpp"
|
||||||
|
|
||||||
namespace ailol {
|
namespace ailol {
|
||||||
|
using namespace nlohmann;
|
||||||
|
|
||||||
bool is_subset(GOAPState& source, GOAPState& target) {
|
bool is_subset(GOAPState& source, GOAPState& target) {
|
||||||
GOAPState result = source & target;
|
GOAPState result = source & target;
|
||||||
return result == 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) {
|
bool Action::can_effect(GOAPState& state) {
|
||||||
return ((state & positive_preconds) == positive_preconds) &&
|
return ((state & $positive_preconds) == $positive_preconds) &&
|
||||||
((state & negative_preconds) == ALL_ZERO);
|
((state & $negative_preconds) == ALL_ZERO);
|
||||||
}
|
}
|
||||||
|
|
||||||
GOAPState Action::apply_effect(GOAPState& state) {
|
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) {
|
int distance_to_goal(GOAPState& from, GOAPState& to) {
|
||||||
|
|
39
goap.hpp
39
goap.hpp
|
@ -4,10 +4,11 @@
|
||||||
#include <bitset>
|
#include <bitset>
|
||||||
#include <limits>
|
#include <limits>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
|
#include <nlohmann/json.hpp>
|
||||||
|
|
||||||
namespace ailol {
|
namespace ailol {
|
||||||
constexpr const int SCORE_MAX = std::numeric_limits<int>::max();
|
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>;
|
using GOAPState = std::bitset<STATE_MAX>;
|
||||||
|
|
||||||
|
@ -15,39 +16,27 @@ namespace ailol {
|
||||||
const GOAPState ALL_ONES = ~ALL_ZERO;
|
const GOAPState ALL_ONES = ~ALL_ZERO;
|
||||||
|
|
||||||
struct Action {
|
struct Action {
|
||||||
std::string name;
|
std::string $name;
|
||||||
int cost = 0;
|
int $cost = 0;
|
||||||
|
|
||||||
GOAPState positive_preconds;
|
GOAPState $positive_preconds;
|
||||||
GOAPState negative_preconds;
|
GOAPState $negative_preconds;
|
||||||
|
|
||||||
GOAPState positive_effects;
|
GOAPState $positive_effects;
|
||||||
GOAPState negative_effects;
|
GOAPState $negative_effects;
|
||||||
|
|
||||||
Action(std::string name, int cost) :
|
Action(std::string name, int cost) :
|
||||||
name(name), cost(cost) { }
|
$name(name), $cost(cost) { }
|
||||||
|
|
||||||
void set_precond(int name, bool val) {
|
void needs(int name, bool val);
|
||||||
if(val) {
|
void effect(int name, bool val);
|
||||||
positive_preconds[name] = true;
|
void load(nlohmann::json &profile, nlohmann::json& config);
|
||||||
} else {
|
|
||||||
negative_preconds[name] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void set_effect(int name, bool val) {
|
|
||||||
if(val) {
|
|
||||||
positive_effects[name] = true;
|
|
||||||
} else {
|
|
||||||
negative_effects[name] = true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
bool can_effect(GOAPState& state);
|
bool can_effect(GOAPState& state);
|
||||||
GOAPState apply_effect(GOAPState& state);
|
GOAPState apply_effect(GOAPState& state);
|
||||||
|
|
||||||
bool operator==(const Action& other) const {
|
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> {
|
template<> struct std::hash<ailol::Action> {
|
||||||
size_t operator()(const ailol::Action& p) const {
|
size_t operator()(const ailol::Action& p) const {
|
||||||
return std::hash<std::string>{}(p.name);
|
return std::hash<std::string>{}(p.$name);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
112
tests/goap.cpp
112
tests/goap.cpp
|
@ -5,6 +5,7 @@
|
||||||
|
|
||||||
using namespace dbc;
|
using namespace dbc;
|
||||||
using namespace ailol;
|
using namespace ailol;
|
||||||
|
using namespace nlohmann;
|
||||||
|
|
||||||
TEST_CASE("worldstate works", "[goap]") {
|
TEST_CASE("worldstate works", "[goap]") {
|
||||||
enum StateNames {
|
enum StateNames {
|
||||||
|
@ -24,8 +25,8 @@ TEST_CASE("worldstate works", "[goap]") {
|
||||||
goal[ENEMY_DEAD] = true;
|
goal[ENEMY_DEAD] = true;
|
||||||
|
|
||||||
Action move_closer("move_closer", 10);
|
Action move_closer("move_closer", 10);
|
||||||
move_closer.set_precond(ENEMY_IN_RANGE, false);
|
move_closer.needs(ENEMY_IN_RANGE, false);
|
||||||
move_closer.set_effect(ENEMY_IN_RANGE, true);
|
move_closer.effect(ENEMY_IN_RANGE, true);
|
||||||
|
|
||||||
REQUIRE(move_closer.can_effect(start));
|
REQUIRE(move_closer.can_effect(start));
|
||||||
auto after_move_state = move_closer.apply_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);
|
REQUIRE(distance_to_goal(start, after_move_state) == 1);
|
||||||
|
|
||||||
Action kill_it("kill_it", 10);
|
Action kill_it("kill_it", 10);
|
||||||
kill_it.set_precond(ENEMY_IN_RANGE, true);
|
kill_it.needs(ENEMY_IN_RANGE, true);
|
||||||
kill_it.set_precond(ENEMY_DEAD, false);
|
kill_it.needs(ENEMY_DEAD, false);
|
||||||
kill_it.set_effect(ENEMY_DEAD, true);
|
kill_it.effect(ENEMY_DEAD, true);
|
||||||
|
|
||||||
REQUIRE(!kill_it.can_effect(start));
|
REQUIRE(!kill_it.can_effect(start));
|
||||||
REQUIRE(kill_it.can_effect(after_move_state));
|
REQUIRE(kill_it.can_effect(after_move_state));
|
||||||
|
@ -73,13 +74,19 @@ TEST_CASE("basic feature tests", "[goap]") {
|
||||||
goal[ENEMY_DEAD] = true;
|
goal[ENEMY_DEAD] = true;
|
||||||
|
|
||||||
Action move_closer("move_closer", 10);
|
Action move_closer("move_closer", 10);
|
||||||
move_closer.set_precond(ENEMY_IN_RANGE, false);
|
move_closer.needs(ENEMY_IN_RANGE, false);
|
||||||
move_closer.set_effect(ENEMY_IN_RANGE, true);
|
move_closer.effect(ENEMY_IN_RANGE, true);
|
||||||
|
|
||||||
Action kill_it("kill_it", 10);
|
Action kill_it("kill_it", 10);
|
||||||
kill_it.set_precond(ENEMY_IN_RANGE, true);
|
kill_it.needs(ENEMY_IN_RANGE, true);
|
||||||
kill_it.set_precond(ENEMY_DEAD, false);
|
// this is duplicated on purpose to confirm that setting
|
||||||
kill_it.set_effect(ENEMY_DEAD, true);
|
// 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
|
// order seems to matter which is wrong
|
||||||
actions.push_back(kill_it);
|
actions.push_back(kill_it);
|
||||||
|
@ -91,7 +98,6 @@ TEST_CASE("basic feature tests", "[goap]") {
|
||||||
auto state = start;
|
auto state = start;
|
||||||
|
|
||||||
for(auto& action : *result) {
|
for(auto& action : *result) {
|
||||||
fmt::println("ACTION: {}", action.name);
|
|
||||||
state = action.apply_effect(state);
|
state = action.apply_effect(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -100,53 +106,79 @@ TEST_CASE("basic feature tests", "[goap]") {
|
||||||
|
|
||||||
TEST_CASE("wargame test from cppGOAP", "[goap]") {
|
TEST_CASE("wargame test from cppGOAP", "[goap]") {
|
||||||
std::vector<Action> actions;
|
std::vector<Action> actions;
|
||||||
|
auto profile = R"({
|
||||||
// Constants for the various states are helpful to keep us from
|
"target_acquired": 0,
|
||||||
// accidentally mistyping a state name.
|
"target_lost": 1,
|
||||||
enum WarGameStates {
|
"target_in_warhead_range": 2,
|
||||||
target_acquired,
|
"target_dead": 3
|
||||||
target_lost,
|
})"_json;
|
||||||
target_in_warhead_range,
|
|
||||||
target_dead
|
|
||||||
};
|
|
||||||
|
|
||||||
// Now establish all the possible actions for the action pool
|
// Now establish all the possible actions for the action pool
|
||||||
// In this example we're providing the AI some different FPS actions
|
// In this example we're providing the AI some different FPS actions
|
||||||
Action spiral("searchSpiral", 5);
|
Action spiral("searchSpiral", 5);
|
||||||
spiral.set_precond(target_acquired, false);
|
auto config = R"({
|
||||||
spiral.set_precond(target_lost, true);
|
"needs": {
|
||||||
spiral.set_effect(target_acquired, true);
|
"target_acquired": false,
|
||||||
|
"target_lost": true
|
||||||
|
},
|
||||||
|
"effects": {
|
||||||
|
"target_acquired": true
|
||||||
|
}
|
||||||
|
})"_json;
|
||||||
|
spiral.load(profile, config);
|
||||||
actions.push_back(spiral);
|
actions.push_back(spiral);
|
||||||
|
|
||||||
Action serpentine("searchSerpentine", 5);
|
Action serpentine("searchSerpentine", 5);
|
||||||
serpentine.set_precond(target_acquired, false);
|
config = R"({
|
||||||
serpentine.set_precond(target_lost, false);
|
"needs": {
|
||||||
serpentine.set_effect(target_acquired, true);
|
"target_acquired": false,
|
||||||
|
"target_lost": false
|
||||||
|
},
|
||||||
|
"effects": {
|
||||||
|
"target_acquired": true
|
||||||
|
}
|
||||||
|
})"_json;
|
||||||
|
serpentine.load(profile, config);
|
||||||
actions.push_back(serpentine);
|
actions.push_back(serpentine);
|
||||||
|
|
||||||
Action intercept("interceptTarget", 5);
|
Action intercept("interceptTarget", 5);
|
||||||
intercept.set_precond(target_acquired, true);
|
config = R"({
|
||||||
intercept.set_precond(target_dead, false);
|
"needs": {
|
||||||
intercept.set_effect(target_in_warhead_range, true);
|
"target_acquired": true,
|
||||||
|
"target_dead": false
|
||||||
|
},
|
||||||
|
"effects": {
|
||||||
|
"target_in_warhead_range": true
|
||||||
|
}
|
||||||
|
})"_json;
|
||||||
|
intercept.load(profile, config);
|
||||||
actions.push_back(intercept);
|
actions.push_back(intercept);
|
||||||
|
|
||||||
|
|
||||||
Action detonateNearTarget("detonateNearTarget", 5);
|
Action detonateNearTarget("detonateNearTarget", 5);
|
||||||
detonateNearTarget.set_precond(target_in_warhead_range, true);
|
config = R"({
|
||||||
detonateNearTarget.set_precond(target_acquired, true);
|
"needs": {
|
||||||
detonateNearTarget.set_precond(target_dead, false);
|
"target_in_warhead_range": true,
|
||||||
detonateNearTarget.set_effect(target_dead, true);
|
"target_acquired": true,
|
||||||
|
"target_dead": false
|
||||||
|
},
|
||||||
|
"effects": {
|
||||||
|
"target_dead": true
|
||||||
|
}
|
||||||
|
})"_json;
|
||||||
|
detonateNearTarget.load(profile, config);
|
||||||
actions.push_back(detonateNearTarget);
|
actions.push_back(detonateNearTarget);
|
||||||
|
|
||||||
// Here's the initial state...
|
// Here's the initial state...
|
||||||
GOAPState initial_state;
|
GOAPState initial_state;
|
||||||
initial_state[target_acquired] = false;
|
initial_state[profile["target_acquired"]] = false;
|
||||||
initial_state[target_lost] = true;
|
initial_state[profile["target_lost"]] = true;
|
||||||
initial_state[target_in_warhead_range] = false;
|
initial_state[profile["target_in_warhead_range"]] = false;
|
||||||
initial_state[target_dead] = false;
|
initial_state[profile["target_dead"]] = false;
|
||||||
|
|
||||||
// ...and the goal state
|
// ...and the goal state
|
||||||
GOAPState goal_target_dead;
|
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);
|
auto result = plan_actions(actions, initial_state, goal_target_dead);
|
||||||
REQUIRE(result != std::nullopt);
|
REQUIRE(result != std::nullopt);
|
||||||
|
@ -154,9 +186,9 @@ TEST_CASE("wargame test from cppGOAP", "[goap]") {
|
||||||
auto state = initial_state;
|
auto state = initial_state;
|
||||||
|
|
||||||
for(auto& action : *result) {
|
for(auto& action : *result) {
|
||||||
fmt::println("ACTION: {}", action.name);
|
fmt::println("ACTION: {}", action.$name);
|
||||||
state = action.apply_effect(state);
|
state = action.apply_effect(state);
|
||||||
}
|
}
|
||||||
|
|
||||||
REQUIRE(state[target_dead]);
|
REQUIRE(state[profile["target_dead"]]);
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue