Now have the ability to do partial solutions that will create potential paths to the goal, and a test that runs the scripts from plans in different scenarios. Also, this ai_debug thing needs some work.

This commit is contained in:
Zed A. Shaw 2025-03-11 15:33:14 -04:00
parent 3f83d3f0bb
commit fc66d221d4
11 changed files with 252 additions and 107 deletions

110
ai.cpp
View file

@ -15,12 +15,10 @@ namespace ai {
}
}
Action config_action(nlohmann::json& profile, nlohmann::json& config) {
Action config_action(AIProfile& 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"),
@ -30,72 +28,84 @@ namespace ai {
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));
result.needs(profile.at(name_key), 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));
result.effect(profile.at(name_key), bool(value));
}
return result;
}
State config_state(nlohmann::json& profile, nlohmann::json& config) {
State config_state(AIProfile& 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);
int name_id = profile.at(name_key);
result[name_id] = bool(value);
}
return result;
}
/*
* This is only used in tests so I can load different fixtures.
*/
void reset() {
initialized = false;
AIMGR.actions.clear();
AIMGR.states.clear();
AIMGR.scripts.clear();
AIMGR.profile = R"({})"_json;
}
void init(std::string config_path) {
initialized = true;
Config config(config_path);
if(!initialized) {
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);
// profile specifies what keys (bitset indexes) are allowed
// and how they map to the bitset of State
validate_profile(config["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);
}
// relies on json conversion?
AIMGR.profile = config["profile"];
// 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));
// 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);
}
AIMGR.scripts.insert_or_assign(script_name, the_script);
// 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);
}
initialized = true;
} else {
dbc::sentinel("DOUBLE INIT: AI manager should only be intialized once if not in tests.");
}
}
@ -122,7 +132,7 @@ namespace ai {
return AIMGR.scripts.at(script_name);
}
std::optional<Script> plan(std::string script_name, State start, State goal) {
ActionPlan 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);
@ -134,4 +144,16 @@ namespace ai {
name));
return AIMGR.profile.at(name);
}
void set(State& state, std::string name, bool value) {
state.set(state_id(name), value);
}
bool test(State state, std::string name) {
return state.test(state_id(name));
}
AIProfile* profile() {
return &AIMGR.profile;
}
}