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
112
tests/goap.cpp
112
tests/goap.cpp
|
@ -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…
Add table
Add a link
Reference in a new issue