Now have a cycle and repeated action mitigation technique in the AI algorithm called 'delete shit you've seen'.
This commit is contained in:
parent
6fa7b0a418
commit
862d8b4d81
3 changed files with 44 additions and 21 deletions
4
Makefile
4
Makefile
|
@ -22,7 +22,7 @@ tracy_build:
|
||||||
meson compile -j 10 -C builddir
|
meson compile -j 10 -C builddir
|
||||||
|
|
||||||
test: build
|
test: build
|
||||||
./builddir/runtests
|
./builddir/runtests "[rituals]"
|
||||||
|
|
||||||
run: build test
|
run: build test
|
||||||
powershell "cp ./builddir/zedcaster.exe ."
|
powershell "cp ./builddir/zedcaster.exe ."
|
||||||
|
@ -41,7 +41,7 @@ clean:
|
||||||
meson compile --clean -C builddir
|
meson compile --clean -C builddir
|
||||||
|
|
||||||
debug_test: build
|
debug_test: build
|
||||||
gdb --nx -x .gdbinit --ex run --args builddir/runtests.exe -e
|
gdb --nx -x .gdbinit --ex run --args builddir/runtests.exe -e "[rituals]"
|
||||||
|
|
||||||
win_installer:
|
win_installer:
|
||||||
powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" win_installer.ifp'
|
powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" win_installer.ifp'
|
||||||
|
|
54
goap.cpp
54
goap.cpp
|
@ -54,24 +54,50 @@ namespace ai {
|
||||||
return count;
|
return count;
|
||||||
}
|
}
|
||||||
|
|
||||||
Script reconstruct_path(std::unordered_map<Action, Action>& came_from, Action& current) {
|
inline void dump_came_from(std::string msg, std::unordered_map<Action, Action>& came_from, Action& current) {
|
||||||
Script total_path{current};
|
fmt::println("{}: {}", msg, current.name);
|
||||||
bool final_found = false;
|
|
||||||
|
for(auto& [from, to] : came_from) {
|
||||||
|
fmt::println("from={}; to={}", from.name, to.name);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
inline void path_invariant(std::unordered_map<Action, Action>& came_from, Action& current) {
|
||||||
|
#if defined(NDEBUG)
|
||||||
|
(void)came_from; // disable errors about unused
|
||||||
|
(void)current;
|
||||||
|
#else
|
||||||
|
bool final_found = current == FINAL_ACTION;
|
||||||
|
|
||||||
for(size_t i = 0; i <= came_from.size() && came_from.contains(current); i++) {
|
for(size_t i = 0; i <= came_from.size() && came_from.contains(current); i++) {
|
||||||
current = came_from.at(current);
|
current = came_from.at(current);
|
||||||
if(current != FINAL_ACTION) {
|
final_found = 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) {
|
||||||
if(!final_found && total_path[0] != FINAL_ACTION) {
|
dump_came_from("CYCLE DETECTED!", came_from, current);
|
||||||
auto error = fmt::format("!!!!! You may have a cycle in your json. No FINAL found. Here's the path: ");
|
dbc::sentinel("AI CYCLE FOUND!");
|
||||||
for(auto& action : total_path) error += fmt::format("{} ", action.name);
|
}
|
||||||
dbc::sentinel(error);
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
Script reconstruct_path(std::unordered_map<Action, Action>& came_from, Action& current) {
|
||||||
|
Script total_path{current};
|
||||||
|
|
||||||
|
path_invariant(came_from, current);
|
||||||
|
|
||||||
|
for(size_t i = 0; i <= came_from.size() && came_from.contains(current); i++) {
|
||||||
|
auto next = came_from.at(current);
|
||||||
|
|
||||||
|
if(next != FINAL_ACTION) {
|
||||||
|
// remove the previous node to avoid cycles and repeated actions
|
||||||
|
total_path.push_front(next);
|
||||||
|
came_from.erase(current);
|
||||||
|
current = next;
|
||||||
|
} else {
|
||||||
|
// found the terminator, done
|
||||||
|
break;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return total_path;
|
return total_path;
|
||||||
|
@ -147,11 +173,9 @@ namespace ai {
|
||||||
came_from.insert_or_assign(neighbor_action, current.action);
|
came_from.insert_or_assign(neighbor_action, current.action);
|
||||||
|
|
||||||
g_score.insert_or_assign(neighbor, tentative_g_score);
|
g_score.insert_or_assign(neighbor, tentative_g_score);
|
||||||
// open_set gets the fScore
|
|
||||||
ActionState neighbor_as{neighbor_action, neighbor};
|
ActionState neighbor_as{neighbor_action, neighbor};
|
||||||
|
|
||||||
int score = tentative_g_score + h(neighbor, goal);
|
int score = tentative_g_score + h(neighbor, goal);
|
||||||
// could maintain lowest here and avoid searching all things
|
|
||||||
f_score.emplace_back(score, neighbor_as);
|
f_score.emplace_back(score, neighbor_as);
|
||||||
std::push_heap(f_score.begin(), f_score.end(), FScorePair_cmp);
|
std::push_heap(f_score.begin(), f_score.end(), FScorePair_cmp);
|
||||||
|
|
||||||
|
|
|
@ -25,13 +25,12 @@ TEST_CASE("RitualEngine basic tests", "[rituals]") {
|
||||||
re.set_state(ritual, "has_spikes", true);
|
re.set_state(ritual, "has_spikes", true);
|
||||||
re.plan(ritual);
|
re.plan(ritual);
|
||||||
|
|
||||||
/*
|
|
||||||
fmt::println("\n\n------------ TEST WILL DO MAGICK TOO");
|
fmt::println("\n\n------------ TEST WILL DO MAGICK TOO");
|
||||||
ritual.dump();
|
ritual.dump();
|
||||||
REQUIRE(ritual.will_do("magick_type"));
|
REQUIRE(ritual.will_do("pierce_type"));
|
||||||
|
|
||||||
ritual.pop();
|
ritual.pop();
|
||||||
REQUIRE(ritual.will_do("pierce_type"));
|
REQUIRE(ritual.will_do("magick_type"));
|
||||||
|
|
||||||
re.reset(ritual);
|
re.reset(ritual);
|
||||||
re.set_state(ritual, "has_magick", true);
|
re.set_state(ritual, "has_magick", true);
|
||||||
|
@ -48,7 +47,6 @@ TEST_CASE("RitualEngine basic tests", "[rituals]") {
|
||||||
re.plan(ritual);
|
re.plan(ritual);
|
||||||
fmt::println("\n\n------------ TEST WILL DO LARGE DAMAGE BOOST");
|
fmt::println("\n\n------------ TEST WILL DO LARGE DAMAGE BOOST");
|
||||||
ritual.dump();
|
ritual.dump();
|
||||||
*/
|
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_CASE("confirm that cycles are avoided/detected", "[rituals]") {
|
TEST_CASE("confirm that cycles are avoided/detected", "[rituals]") {
|
||||||
|
@ -58,6 +56,7 @@ TEST_CASE("confirm that cycles are avoided/detected", "[rituals]") {
|
||||||
re.set_state(ritual, "has_magick", true);
|
re.set_state(ritual, "has_magick", true);
|
||||||
re.set_state(ritual, "cursed_item", true);
|
re.set_state(ritual, "cursed_item", true);
|
||||||
re.set_state(ritual, "shiny_bauble", true);
|
re.set_state(ritual, "shiny_bauble", true);
|
||||||
|
re.plan(ritual);
|
||||||
|
|
||||||
fmt::println("\n\n------------ CYCLES AVOIDED");
|
fmt::println("\n\n------------ CYCLES AVOIDED");
|
||||||
ritual.dump();
|
ritual.dump();
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue