|
|
|
@ -1,8 +1,10 @@ |
|
|
|
|
#include "dbc.hpp" |
|
|
|
|
#include "goap.hpp" |
|
|
|
|
#include "ai_debug.hpp" |
|
|
|
|
#include "stats.hpp" |
|
|
|
|
|
|
|
|
|
namespace ai { |
|
|
|
|
|
|
|
|
|
using namespace nlohmann; |
|
|
|
|
using namespace dbc; |
|
|
|
|
|
|
|
|
@ -36,7 +38,6 @@ namespace ai { |
|
|
|
|
$negative_preconds[name] = false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
bool Action::can_effect(State& state) { |
|
|
|
|
return ((state & $positive_preconds) == $positive_preconds) && |
|
|
|
|
((state & $negative_preconds) == ALL_ZERO); |
|
|
|
@ -53,14 +54,24 @@ namespace ai { |
|
|
|
|
|
|
|
|
|
Script reconstruct_path(std::unordered_map<Action, Action>& came_from, Action& current) { |
|
|
|
|
Script total_path{current}; |
|
|
|
|
bool final_found = false; |
|
|
|
|
|
|
|
|
|
while(came_from.contains(current)) { |
|
|
|
|
for(size_t i = 0; i <= came_from.size() && came_from.contains(current); i++) { |
|
|
|
|
current = came_from.at(current); |
|
|
|
|
if(current != FINAL_ACTION) { |
|
|
|
|
total_path.push_front(current); |
|
|
|
|
} else { |
|
|
|
|
final_found = true; |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// this here temporarily while I figure out cycle detects/prevention
|
|
|
|
|
if(!final_found && total_path[0] != FINAL_ACTION) { |
|
|
|
|
auto error = fmt::format("!!!!! You may have a cycle in your json. No FINAL found. Here's the path: "); |
|
|
|
|
for(auto& action : total_path) error += fmt::format("{} ", action.name); |
|
|
|
|
dbc::sentinel(error); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
return total_path; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
@ -96,7 +107,7 @@ namespace ai { |
|
|
|
|
ActionState current{FINAL_ACTION, start}; |
|
|
|
|
|
|
|
|
|
g_score[start] = 0; |
|
|
|
|
open_set[current] = g_score[start] + h(start, goal, current.action); |
|
|
|
|
open_set.insert_or_assign(current, g_score[start] + h(start, goal, current.action)); |
|
|
|
|
|
|
|
|
|
while(!open_set.empty()) { |
|
|
|
|
current = find_lowest(open_set); |
|
|
|
@ -127,7 +138,10 @@ namespace ai { |
|
|
|
|
g_score[neighbor] = tentative_g_score; |
|
|
|
|
// open_set gets the fScore
|
|
|
|
|
ActionState neighbor_as{neighbor_action, neighbor}; |
|
|
|
|
open_set[neighbor_as] = tentative_g_score + h(neighbor, goal, neighbor_as.action); |
|
|
|
|
|
|
|
|
|
int score = tentative_g_score + h(neighbor, goal, neighbor_as.action); |
|
|
|
|
// could maintain lowest here and avoid searching all things
|
|
|
|
|
open_set.insert_or_assign(neighbor_as, score); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|