A bit more cleanup to avoid duplicate testing and to separate the GOAP algorithm code from the little AI Manager thing.
This commit is contained in:
parent
b2c1b220ac
commit
3f83d3f0bb
8 changed files with 296 additions and 290 deletions
69
ai.hpp
69
ai.hpp
|
@ -6,62 +6,9 @@
|
|||
#include <optional>
|
||||
#include <nlohmann/json.hpp>
|
||||
#include "config.hpp"
|
||||
#include "goap.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);
|
||||
|
||||
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);
|
||||
|
||||
struct AIManager {
|
||||
nlohmann::json profile;
|
||||
|
||||
|
@ -70,7 +17,7 @@ namespace ai {
|
|||
std::unordered_map<std::string, std::vector<Action>> scripts;
|
||||
};
|
||||
|
||||
void init();
|
||||
void init(std::string config_path);
|
||||
|
||||
Action config_action(nlohmann::json& profile, nlohmann::json& config);
|
||||
State config_state(nlohmann::json& profile, nlohmann::json& config);
|
||||
|
@ -82,15 +29,3 @@ namespace ai {
|
|||
|
||||
std::optional<Script> plan(std::string script_name, 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);
|
||||
}
|
||||
};
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue