You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
137 lines
4.5 KiB
137 lines
4.5 KiB
#include "dbc.hpp"
|
|
#include "ai.hpp"
|
|
|
|
namespace ai {
|
|
using namespace nlohmann;
|
|
using namespace dbc;
|
|
|
|
static AIManager AIMGR;
|
|
static bool initialized = false;
|
|
|
|
inline void validate_profile(nlohmann::json& profile) {
|
|
for(auto& [name_key, value] : profile.items()) {
|
|
check(value < STATE_MAX,
|
|
fmt::format("profile field {} has value {} greater than STATE_MAX {}", (std::string)name_key, (int)value, STATE_MAX));
|
|
}
|
|
}
|
|
|
|
Action config_action(nlohmann::json& profile, nlohmann::json& config) {
|
|
check(config.contains("name"), "config_action: action config missing name");
|
|
check(config.contains("cost"), "config_action: action config missing cost");
|
|
|
|
validate_profile(profile);
|
|
|
|
Action result(config["name"], config["cost"]);
|
|
|
|
check(config.contains("needs"),
|
|
fmt::format("config_action: no 'needs' field", result.$name));
|
|
check(config.contains("effects"),
|
|
fmt::format("config_action: no 'effects' field", result.$name));
|
|
|
|
for(auto& [name_key, value] : config["needs"].items()) {
|
|
check(profile.contains(name_key), fmt::format("config_action: profile does not have name {}", result.$name, name_key));
|
|
int name = profile[name_key].template get<int>();
|
|
|
|
result.needs(name, bool(value));
|
|
}
|
|
|
|
for(auto& [name_key, value] : config["effects"].items()) {
|
|
check(profile.contains(name_key), fmt::format("config_action: profile does not have name {}", result.$name, name_key));
|
|
|
|
int name = profile[name_key].template get<int>();
|
|
|
|
result.effect(name, bool(value));
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
State config_state(nlohmann::json& profile, nlohmann::json& config) {
|
|
State result;
|
|
validate_profile(profile);
|
|
|
|
for(auto& [name_key, value] : config.items()) {
|
|
check(profile.contains(name_key), fmt::format("config_state: profile does not have name {}", name_key));
|
|
|
|
int name = profile[name_key].template get<int>();
|
|
result[name] = bool(value);
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
void init(std::string config_path) {
|
|
initialized = true;
|
|
Config config(config_path);
|
|
|
|
// profile specifies what keys (bitset indexes) are allowed
|
|
// and how they map to the bitset of State
|
|
AIMGR.profile = config["profile"];
|
|
validate_profile(AIMGR.profile);
|
|
|
|
// load all actions
|
|
auto& actions = config["actions"];
|
|
for(auto& action_vars : actions) {
|
|
auto the_action = config_action(AIMGR.profile, action_vars);
|
|
AIMGR.actions.insert_or_assign(the_action.$name, the_action);
|
|
}
|
|
|
|
// load all states
|
|
auto& states = config["states"];
|
|
for(auto& [name, state_vars] : states.items()) {
|
|
auto the_state = config_state(AIMGR.profile, state_vars);
|
|
AIMGR.states.insert_or_assign(name, the_state);
|
|
}
|
|
|
|
auto& scripts = config["scripts"];
|
|
for(auto& [script_name, action_names] : scripts.items()) {
|
|
std::vector<Action> the_script;
|
|
for(auto name : action_names) {
|
|
|
|
check(AIMGR.actions.contains(name),
|
|
fmt::format("ai::init(): script {} uses action {} that doesn't exist",
|
|
(std::string)script_name, (std::string)name));
|
|
|
|
the_script.push_back(AIMGR.actions.at(name));
|
|
}
|
|
|
|
AIMGR.scripts.insert_or_assign(script_name, the_script);
|
|
}
|
|
}
|
|
|
|
State load_state(std::string state_name) {
|
|
check(initialized, "you forgot to initialize the AI first.");
|
|
check(AIMGR.states.contains(state_name), fmt::format(
|
|
"ai::load_state({}): state does not exist in config",
|
|
state_name));
|
|
|
|
return AIMGR.states.at(state_name);
|
|
}
|
|
|
|
Action load_action(std::string action_name) {
|
|
check(initialized, "you forgot to initialize the AI first.");
|
|
check(AIMGR.states.contains(action_name), fmt::format(
|
|
"ai::load_action({}): action does not exist in config",
|
|
action_name));
|
|
return AIMGR.actions.at(action_name);
|
|
}
|
|
|
|
std::vector<Action> load_script(std::string script_name) {
|
|
check(AIMGR.scripts.contains(script_name), fmt::format(
|
|
"ai::load_script(): no script named {} configured", script_name));
|
|
return AIMGR.scripts.at(script_name);
|
|
}
|
|
|
|
std::optional<Script> plan(std::string script_name, State start, State goal) {
|
|
check(initialized, "you forgot to initialize the AI first.");
|
|
auto script = load_script(script_name);
|
|
return plan_actions(script, start, goal);
|
|
}
|
|
|
|
int state_id(std::string name) {
|
|
check(AIMGR.profile.contains(name), fmt::format(
|
|
"ai::state_id({}): id is not configured in profile",
|
|
name));
|
|
return AIMGR.profile.at(name);
|
|
}
|
|
}
|
|
|