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
54
goap.cpp
54
goap.cpp
|
@ -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) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue