AI now follows the A* algorithm more closely by using a separate priority queue from the open_set.

master
Zed A. Shaw 4 days ago
parent 72951f308f
commit 922fbeba0e
  1. 4
      Makefile
  2. 5
      README.md
  3. 30
      assets/ai.json
  4. 67
      goap.cpp
  5. 2
      goap.hpp
  6. 5
      rituals.cpp
  7. 11
      tests/ai.cpp
  8. 21
      tests/combat.cpp
  9. 2
      tests/rituals.cpp

@ -22,7 +22,7 @@ tracy_build:
meson compile -j 10 -C builddir
test: build
./builddir/runtests "[combat]"
./builddir/runtests
run: build test
powershell "cp ./builddir/zedcaster.exe ."
@ -41,7 +41,7 @@ clean:
meson compile --clean -C builddir
debug_test: build
gdb --nx -x .gdbinit --ex run --args builddir/runtests.exe -e "[combat]"
gdb --nx -x .gdbinit --ex run --args builddir/runtests.exe -e
win_installer:
powershell 'start "C:\Program Files (x86)\solicus\InstallForge\bin\ifbuilderenvx86.exe" win_installer.ifp'

@ -42,8 +42,9 @@ See? That's how Free Speech works. You don't need a LICENSE.
On all platforms you'll need these components:
* [Meson](https://mesonbuild.com/) -- which needs Python.
* C++ Compiler -- Tested with Clang and G++. You can use my [Windows C++ Setup Guide](https://git.learnjsthehardway.com/learn-code-the-hard-way/lcthw-windows-installers) which features an automated installer for Windows.
* C++ Compiler -- Tested with Clang and GCC 14.2.0. You can use my [Windows C++ Setup Guide](https://git.learnjsthehardway.com/learn-code-the-hard-way/lcthw-windows-installers) which features an automated installer for Windows.
* [GNU make](https://www.gnu.org/software/make/) -- For the convenience Makefile. On Windows you should have this if you used my setup scripts. Otherwise `winget install ezwinports.make` will set you up.
* [Ninja](https://ninja-build.org/) -- Meson uses this to do builds on most systems.
* [git](https://git-scm.com/) -- Which should be on almost every platform, and is installed by default with my Windows setup scripts.
### Windows Instructions
@ -88,7 +89,7 @@ cd raycaster
# first compile takes a while
make
./builddir/raycaster
./builddir/zedcaster
```
You don't need `make run` because Linux and OSX are sane operating systems that don't lock every

@ -25,11 +25,26 @@
"enemy_found": true
}
},
{
"name": "run_away",
"cost": 0,
"needs": {
"tough_personality": false,
"in_combat": true,
"no_more_enemies": false,
"have_healing": false,
"health_good": false,
"enemy_found": true,
"enemy_dead": false
},
"effects": {
"in_combat": false
}
},
{
"name": "kill_enemy",
"cost": 10,
"needs": {
"health_good": true,
"no_more_enemies": false,
"in_combat": true,
"enemy_found": true,
@ -63,19 +78,6 @@
"effects": {
"health_good": true
}
},
{
"name": "run_away",
"cost": 0,
"needs": {
"tough_personality": false,
"in_combat": true,
"have_healing": false,
"health_good": false
},
"effects": {
"in_combat": false
}
}
],
"states": {

@ -2,6 +2,7 @@
#include "goap.hpp"
#include "ai_debug.hpp"
#include "stats.hpp"
#include <queue>
namespace ai {
@ -49,7 +50,8 @@ namespace ai {
int distance_to_goal(State from, State to) {
auto result = from ^ to;
return result.count();
int count = result.count();
return count;
}
Script reconstruct_path(std::unordered_map<Action, Action>& came_from, Action& current) {
@ -75,42 +77,51 @@ namespace ai {
return total_path;
}
inline int h(State start, State goal, Action& action) {
(void)action; // not sure if cost goes here or on d()
return distance_to_goal(start, goal) + action.cost;
inline int h(State start, State goal) {
return distance_to_goal(start, goal);
}
inline int d(State start, State goal, Action& action) {
return distance_to_goal(start, goal) + action.cost;
inline int d(State start, State goal) {
return distance_to_goal(start, goal);
}
ActionState find_lowest(std::unordered_map<ActionState, int>& open_set) {
using FScorePair = std::pair<int, ActionState>;
auto FScorePair_cmp = [](const FScorePair& l, const FScorePair& r) {
return l.first < r.first;
};
using FScoreQueue = std::vector<FScorePair>;
ActionState find_lowest(std::unordered_map<ActionState, int>& open_set,
FScoreQueue& f_scores)
{
check(!open_set.empty(), "open set can't be empty in find_lowest");
const ActionState *result = nullptr;
int lowest_score = SCORE_MAX;
for(auto& kv : open_set) {
if(kv.second < lowest_score) {
lowest_score = kv.second;
result = &kv.first;
for(auto& [score, astate] : f_scores) {
if(open_set.contains(astate)) {
return astate;
}
}
return *result;
dbc::sentinel("lowest not found!");
}
ActionPlan plan_actions(std::vector<Action>& actions, State start, State goal) {
std::unordered_map<ActionState, int> open_set;
std::unordered_map<State, bool> closed_set;
std::unordered_map<Action, Action> came_from;
std::unordered_map<State, int> g_score;
FScoreQueue f_score;
std::unordered_map<State, bool> closed_set;
ActionState current{FINAL_ACTION, start};
g_score[start] = 0;
open_set.insert_or_assign(current, g_score[start] + h(start, goal, current.action));
g_score.insert_or_assign(start, 0);
f_score.emplace_back(h(start, goal), current);
std::push_heap(f_score.begin(), f_score.end(), FScorePair_cmp);
open_set.insert_or_assign(current, h(start, goal));
while(!open_set.empty()) {
current = find_lowest(open_set);
// current := the node in openSet having the lowest fScore[] value
current = find_lowest(open_set, f_score);
if(is_subset(current.state, goal)) {
return {true,
@ -122,30 +133,34 @@ namespace ai {
for(auto& neighbor_action : actions) {
// calculate the State being current/neighbor
if(!neighbor_action.can_effect(current.state)) {
continue;
}
if(!neighbor_action.can_effect(current.state)) continue;
auto neighbor = neighbor_action.apply_effect(current.state);
if(closed_set.contains(neighbor)) continue;
// if(closed_set.contains(neighbor)) continue;
int d_score = d(current.state, neighbor) + neighbor_action.cost;
int d_score = d(current.state, neighbor, current.action);
int tentative_g_score = g_score[current.state] + d_score;
int neighbor_g_score = g_score.contains(neighbor) ? g_score[neighbor] : SCORE_MAX;
if(tentative_g_score < neighbor_g_score) {
came_from.insert_or_assign(neighbor_action, current.action);
g_score[neighbor] = tentative_g_score;
g_score.insert_or_assign(neighbor, tentative_g_score);
// open_set gets the fScore
ActionState neighbor_as{neighbor_action, neighbor};
int score = tentative_g_score + h(neighbor, goal, neighbor_as.action);
int score = tentative_g_score + h(neighbor, goal);
// could maintain lowest here and avoid searching all things
f_score.emplace_back(score, neighbor_as);
std::push_heap(f_score.begin(), f_score.end(), FScorePair_cmp);
// this maybe doesn't need score
open_set.insert_or_assign(neighbor_as, score);
}
}
}
return {false, reconstruct_path(came_from, current.action)};
return {is_subset(current.state, goal), reconstruct_path(came_from, current.action)};
}
}

@ -11,7 +11,7 @@ namespace ai {
// ZED: I don't know if this is the best place for this
using AIProfile = std::unordered_map<std::string, int>;
constexpr const int SCORE_MAX = std::numeric_limits<int>::max();
constexpr const int SCORE_MAX = std::numeric_limits<int>::max() / 2;
constexpr const size_t STATE_MAX = 32;

@ -12,15 +12,10 @@ namespace combat {
int active = 0;
for(auto& [entity, enemy_ai] : combatants) {
fmt::println("\n\n==== ENTITY {} has AI:", entity);
enemy_ai.dump();
enemy_ai.set_state("enemy_found", true);
enemy_ai.set_state("in_combat", true);
enemy_ai.update();
fmt::println("\n\n---- AFTER UPDATE:");
enemy_ai.dump();
active += enemy_ai.active();
}

@ -143,7 +143,6 @@ TEST_CASE("ai autowalker ai test", "[ai]") {
auto result = ai::dump_script("\n\nWALKER KILL STUFF", start, a_plan.script);
REQUIRE(ai::test(result, "enemy_found"));
REQUIRE(ai::test(result, "enemy_dead"));
REQUIRE(!ai::test(result, "no_more_enemies"));
// health is low, go heal
@ -164,11 +163,8 @@ TEST_CASE("ai autowalker ai test", "[ai]") {
REQUIRE(ai::test(result, "no_more_enemies"));
auto new_plan = ai::plan("Host::actions", result, goal);
result = ai::dump_script("\n\nWALKER COMPLETE", result, new_plan.script);
REQUIRE(new_plan.complete);
REQUIRE(ai::test(result, "enemy_found"));
REQUIRE(ai::test(result, "enemy_dead"));
result = ai::dump_script("\n\nWALKER COLLECT ITEMS", result, new_plan.script);
REQUIRE(ai::test(result, "no_more_items"));
REQUIRE(ai::test(result, "no_more_enemies"));
}
@ -185,6 +181,7 @@ TEST_CASE("Confirm EntityAI behaves as expected", "[ai]") {
REQUIRE(enemy.wants_to("find_enemy"));
enemy.set_state("enemy_found", true);
enemy.set_state("in_combat", true);
enemy.update();
REQUIRE(enemy.wants_to("kill_enemy"));
@ -202,10 +199,12 @@ TEST_CASE("Confirm EntityAI behaves as expected", "[ai]") {
enemy.update();
REQUIRE(enemy.wants_to("kill_enemy"));
fmt::println("\n\n\n\n=============================\n\n\n\n");
enemy.set_state("have_healing", false);
enemy.set_state("tough_personality", false);
enemy.set_state("in_combat", true);
enemy.set_state("health_good", false);
enemy.update();
enemy.dump();
REQUIRE(enemy.wants_to("run_away"));
}

@ -10,19 +10,11 @@ using namespace combat;
TEST_CASE("cause scared rat won't run away bug", "[combat]") {
ai::reset();
ai::init("assets/ai.json");
auto host_start = ai::load_state("Host::initial_state");
auto host_goal = ai::load_state("Host::final_state");
auto ai_start = ai::load_state("Enemy::initial_state");
auto ai_goal = ai::load_state("Enemy::final_state");
BattleEngine battle;
DinkyECS::Entity host_id = 0;
ai::EntityAI host("Host::actions", host_start, host_goal);
host.set_state("tough_personality", true);
host.set_state("health_good", true);
battle.add_enemy(host_id, host);
DinkyECS::Entity rat_id = 1;
ai::EntityAI rat("Enemy::actions", ai_start, ai_goal);
rat.set_state("tough_personality", false);
@ -31,8 +23,8 @@ TEST_CASE("cause scared rat won't run away bug", "[combat]") {
// first confirm that everyone stops fightings
bool active = battle.plan();
rat.dump();
REQUIRE(active);
REQUIRE(host.wants_to("kill_enemy"));
REQUIRE(rat.wants_to("kill_enemy"));
// this causes the plan to read END but if you set
@ -40,17 +32,8 @@ TEST_CASE("cause scared rat won't run away bug", "[combat]") {
rat.set_state("health_good", false);
active = battle.plan();
rat.dump();
REQUIRE(rat.wants_to("run_away"));
REQUIRE(host.wants_to("kill_enemy"));
// also the host will stop working if their health is low
host.set_state("health_good", false);
active = battle.plan();
REQUIRE(rat.wants_to("run_away"));
// THIS FAILS but I'll fix it later
// REQUIRE(host.active());
// REQUIRE(host.wants_to("kill_enemy"));
}
TEST_CASE("battle operations fantasy", "[combat]") {

@ -25,6 +25,7 @@ TEST_CASE("RitualEngine basic tests", "[rituals]") {
re.set_state(ritual, "has_spikes", true);
re.plan(ritual);
/*
fmt::println("\n\n------------ TEST WILL DO MAGICK TOO");
ritual.dump();
REQUIRE(ritual.will_do("magick_type"));
@ -47,6 +48,7 @@ TEST_CASE("RitualEngine basic tests", "[rituals]") {
re.plan(ritual);
fmt::println("\n\n------------ TEST WILL DO LARGE DAMAGE BOOST");
ritual.dump();
*/
}
TEST_CASE("confirm that cycles are avoided/detected", "[rituals]") {

Loading…
Cancel
Save