Quick renaming of stuff to be more generic as 'AI'. Now maybe I can get some sweet sweet investor money.
This commit is contained in:
parent
9d6dc2f5dd
commit
a079f882df
7 changed files with 95 additions and 289 deletions
2
Makefile
2
Makefile
|
@ -22,7 +22,7 @@ tracy_build:
|
|||
meson compile -j 10 -C builddir
|
||||
|
||||
test: build
|
||||
./builddir/runtests "[goap]"
|
||||
./builddir/runtests "[ai]"
|
||||
|
||||
run: build test
|
||||
powershell "cp ./builddir/zedcaster.exe ."
|
||||
|
|
|
@ -1,11 +1,11 @@
|
|||
#include "dbc.hpp"
|
||||
#include "goap.hpp"
|
||||
#include "ai.hpp"
|
||||
|
||||
namespace ailol {
|
||||
namespace ai {
|
||||
using namespace nlohmann;
|
||||
|
||||
bool is_subset(GOAPState& source, GOAPState& target) {
|
||||
GOAPState result = source & target;
|
||||
bool is_subset(State& source, State& target) {
|
||||
State result = source & target;
|
||||
return result == target;
|
||||
}
|
||||
|
||||
|
@ -55,22 +55,22 @@ namespace ailol {
|
|||
}
|
||||
}
|
||||
|
||||
bool Action::can_effect(GOAPState& state) {
|
||||
bool Action::can_effect(State& state) {
|
||||
return ((state & $positive_preconds) == $positive_preconds) &&
|
||||
((state & $negative_preconds) == ALL_ZERO);
|
||||
}
|
||||
|
||||
GOAPState Action::apply_effect(GOAPState& state) {
|
||||
State Action::apply_effect(State& state) {
|
||||
return (state | $positive_effects) & ~$negative_effects;
|
||||
}
|
||||
|
||||
int distance_to_goal(GOAPState& from, GOAPState& to) {
|
||||
int distance_to_goal(State& from, State& to) {
|
||||
auto result = from ^ to;
|
||||
return result.count();
|
||||
}
|
||||
|
||||
AStarPath reconstruct_path(std::unordered_map<Action, Action>& came_from, Action& current) {
|
||||
AStarPath total_path{current};
|
||||
Script reconstruct_path(std::unordered_map<Action, Action>& came_from, Action& current) {
|
||||
Script total_path{current};
|
||||
int count = 0;
|
||||
|
||||
while(came_from.contains(current) && count++ < 10) {
|
||||
|
@ -83,11 +83,11 @@ namespace ailol {
|
|||
return total_path;
|
||||
}
|
||||
|
||||
inline int h(GOAPState& start, GOAPState& goal) {
|
||||
inline int h(State& start, State& goal) {
|
||||
return distance_to_goal(start, goal);
|
||||
}
|
||||
|
||||
inline int d(GOAPState& start, GOAPState& goal) {
|
||||
inline int d(State& start, State& goal) {
|
||||
return distance_to_goal(start, goal);
|
||||
}
|
||||
|
||||
|
@ -106,10 +106,10 @@ namespace ailol {
|
|||
return *result;
|
||||
}
|
||||
|
||||
std::optional<AStarPath> plan_actions(std::vector<Action>& actions, GOAPState& start, GOAPState& goal) {
|
||||
std::optional<Script> plan_actions(std::vector<Action>& actions, State& start, State& goal) {
|
||||
std::unordered_map<ActionState, int> open_set;
|
||||
std::unordered_map<Action, Action> came_from;
|
||||
std::unordered_map<GOAPState, int> g_score;
|
||||
std::unordered_map<State, int> g_score;
|
||||
|
||||
ActionState start_state{FINAL_ACTION, start};
|
||||
|
||||
|
@ -120,13 +120,13 @@ namespace ailol {
|
|||
auto current = find_lowest(open_set);
|
||||
|
||||
if(is_subset(current.state, goal)) {
|
||||
return std::make_optional<AStarPath>(reconstruct_path(came_from, current.action));
|
||||
return std::make_optional<Script>(reconstruct_path(came_from, current.action));
|
||||
}
|
||||
|
||||
open_set.erase(current);
|
||||
|
||||
for(auto& neighbor_action : actions) {
|
||||
// calculate the GOAPState being current/neighbor
|
||||
// calculate the State being current/neighbor
|
||||
if(!neighbor_action.can_effect(current.state)) {
|
||||
continue;
|
||||
}
|
76
ai.hpp
Normal file
76
ai.hpp
Normal file
|
@ -0,0 +1,76 @@
|
|||
#pragma once
|
||||
#include <vector>
|
||||
#include "matrix.hpp"
|
||||
#include <bitset>
|
||||
#include <limits>
|
||||
#include <optional>
|
||||
#include <nlohmann/json.hpp>
|
||||
|
||||
namespace ai {
|
||||
constexpr const int SCORE_MAX = std::numeric_limits<int>::max();
|
||||
constexpr const size_t STATE_MAX = 32;
|
||||
|
||||
using State = std::bitset<STATE_MAX>;
|
||||
|
||||
const State ALL_ZERO;
|
||||
const State ALL_ONES = ~ALL_ZERO;
|
||||
|
||||
struct Action {
|
||||
std::string $name;
|
||||
int $cost = 0;
|
||||
|
||||
State $positive_preconds;
|
||||
State $negative_preconds;
|
||||
|
||||
State $positive_effects;
|
||||
State $negative_effects;
|
||||
|
||||
Action(std::string name, int cost) :
|
||||
$name(name), $cost(cost) { }
|
||||
|
||||
void needs(int name, bool val);
|
||||
void effect(int name, bool val);
|
||||
void load(nlohmann::json &profile, nlohmann::json& config);
|
||||
|
||||
bool can_effect(State& state);
|
||||
State apply_effect(State& state);
|
||||
|
||||
bool operator==(const Action& other) const {
|
||||
return other.$name == $name;
|
||||
}
|
||||
};
|
||||
|
||||
using Script = std::deque<Action>;
|
||||
|
||||
const Action FINAL_ACTION("END", SCORE_MAX);
|
||||
|
||||
struct ActionState {
|
||||
Action action;
|
||||
State state;
|
||||
|
||||
ActionState(Action action, State state) :
|
||||
action(action), state(state) {}
|
||||
|
||||
bool operator==(const ActionState& other) const {
|
||||
return other.action == action && other.state == state;
|
||||
}
|
||||
};
|
||||
|
||||
bool is_subset(State& source, State& target);
|
||||
|
||||
int distance_to_goal(State& from, State& to);
|
||||
|
||||
std::optional<Script> plan_actions(std::vector<Action>& actions, State& start, State& goal);
|
||||
}
|
||||
|
||||
template<> struct std::hash<ai::Action> {
|
||||
size_t operator()(const ai::Action& p) const {
|
||||
return std::hash<std::string>{}(p.$name);
|
||||
}
|
||||
};
|
||||
|
||||
template<> struct std::hash<ai::ActionState> {
|
||||
size_t operator()(const ai::ActionState& p) const {
|
||||
return std::hash<ai::Action>{}(p.action) ^ std::hash<ai::State>{}(p.state);
|
||||
}
|
||||
};
|
76
goap.hpp
76
goap.hpp
|
@ -1,76 +0,0 @@
|
|||
#pragma once
|
||||
#include <vector>
|
||||
#include "matrix.hpp"
|
||||
#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 = 32;
|
||||
|
||||
using GOAPState = std::bitset<STATE_MAX>;
|
||||
|
||||
const GOAPState ALL_ZERO;
|
||||
const GOAPState ALL_ONES = ~ALL_ZERO;
|
||||
|
||||
struct Action {
|
||||
std::string $name;
|
||||
int $cost = 0;
|
||||
|
||||
GOAPState $positive_preconds;
|
||||
GOAPState $negative_preconds;
|
||||
|
||||
GOAPState $positive_effects;
|
||||
GOAPState $negative_effects;
|
||||
|
||||
Action(std::string name, int cost) :
|
||||
$name(name), $cost(cost) { }
|
||||
|
||||
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;
|
||||
}
|
||||
};
|
||||
|
||||
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);
|
||||
}
|
||||
};
|
|
@ -93,7 +93,7 @@ sources = [
|
|||
'config.cpp',
|
||||
'dbc.cpp',
|
||||
'devices.cpp',
|
||||
'goap.cpp',
|
||||
'ai.cpp',
|
||||
'guecs.cpp',
|
||||
'gui_fsm.cpp',
|
||||
'inventory.cpp',
|
||||
|
@ -131,7 +131,7 @@ executable('runtests', sources + [
|
|||
'tests/dbc.cpp',
|
||||
'tests/dinkyecs.cpp',
|
||||
'tests/fsm.cpp',
|
||||
'tests/goap.cpp',
|
||||
'tests/ai.cpp',
|
||||
'tests/guecs.cpp',
|
||||
'tests/inventory.cpp',
|
||||
'tests/lel.cpp',
|
||||
|
|
194
tests/goap.cpp
194
tests/goap.cpp
|
@ -1,194 +0,0 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#include "dbc.hpp"
|
||||
#include "goap.hpp"
|
||||
#include <iostream>
|
||||
|
||||
using namespace dbc;
|
||||
using namespace ailol;
|
||||
using namespace nlohmann;
|
||||
|
||||
TEST_CASE("worldstate works", "[goap]") {
|
||||
enum StateNames {
|
||||
ENEMY_IN_RANGE,
|
||||
ENEMY_DEAD
|
||||
};
|
||||
|
||||
GOAPState goal;
|
||||
GOAPState start;
|
||||
std::vector<Action> actions;
|
||||
|
||||
// start off enemy not dead and not in range
|
||||
start[ENEMY_DEAD] = false;
|
||||
start[ENEMY_IN_RANGE] = false;
|
||||
|
||||
// end goal is enemy is dead
|
||||
goal[ENEMY_DEAD] = true;
|
||||
|
||||
Action move_closer("move_closer", 10);
|
||||
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);
|
||||
REQUIRE(start[ENEMY_IN_RANGE] == false);
|
||||
REQUIRE(after_move_state[ENEMY_IN_RANGE] == true);
|
||||
REQUIRE(after_move_state[ENEMY_DEAD] == false);
|
||||
// start is clean but after move is dirty
|
||||
REQUIRE(move_closer.can_effect(start));
|
||||
REQUIRE(!move_closer.can_effect(after_move_state));
|
||||
REQUIRE(distance_to_goal(start, after_move_state) == 1);
|
||||
|
||||
Action kill_it("kill_it", 10);
|
||||
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));
|
||||
|
||||
auto after_kill_state = kill_it.apply_effect(after_move_state);
|
||||
REQUIRE(!kill_it.can_effect(after_kill_state));
|
||||
REQUIRE(distance_to_goal(after_move_state, after_kill_state) == 1);
|
||||
|
||||
actions.push_back(kill_it);
|
||||
actions.push_back(move_closer);
|
||||
|
||||
REQUIRE(start != goal);
|
||||
}
|
||||
|
||||
TEST_CASE("basic feature tests", "[goap]") {
|
||||
enum StateNames {
|
||||
ENEMY_IN_RANGE,
|
||||
ENEMY_DEAD
|
||||
};
|
||||
|
||||
GOAPState goal;
|
||||
GOAPState start;
|
||||
std::vector<Action> actions;
|
||||
|
||||
// start off enemy not dead and not in range
|
||||
start[ENEMY_DEAD] = false;
|
||||
start[ENEMY_IN_RANGE] = false;
|
||||
|
||||
// end goal is enemy is dead
|
||||
goal[ENEMY_DEAD] = true;
|
||||
|
||||
Action move_closer("move_closer", 10);
|
||||
move_closer.needs(ENEMY_IN_RANGE, false);
|
||||
move_closer.effect(ENEMY_IN_RANGE, true);
|
||||
|
||||
Action kill_it("kill_it", 10);
|
||||
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);
|
||||
actions.push_back(move_closer);
|
||||
|
||||
auto result = plan_actions(actions, start, goal);
|
||||
REQUIRE(result != std::nullopt);
|
||||
|
||||
auto state = start;
|
||||
|
||||
for(auto& action : *result) {
|
||||
state = action.apply_effect(state);
|
||||
}
|
||||
|
||||
REQUIRE(state[ENEMY_DEAD]);
|
||||
}
|
||||
|
||||
TEST_CASE("wargame test from cppGOAP", "[goap]") {
|
||||
std::vector<Action> actions;
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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);
|
||||
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[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[profile["target_dead"]] = true;
|
||||
|
||||
auto result = plan_actions(actions, initial_state, goal_target_dead);
|
||||
REQUIRE(result != std::nullopt);
|
||||
|
||||
auto state = initial_state;
|
||||
|
||||
for(auto& action : *result) {
|
||||
fmt::println("ACTION: {}", action.$name);
|
||||
state = action.apply_effect(state);
|
||||
}
|
||||
|
||||
REQUIRE(state[profile["target_dead"]]);
|
||||
}
|
|
@ -4,7 +4,7 @@
|
|||
#include <fstream>
|
||||
#include "pathing.hpp"
|
||||
#include "matrix.hpp"
|
||||
#include "goap.hpp"
|
||||
#include "ai.hpp"
|
||||
|
||||
using namespace fmt;
|
||||
using namespace nlohmann;
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue