Autowalker is working way better and now I have a plan for using the AI in the System.
This commit is contained in:
parent
0623170dbc
commit
ee804581a8
11 changed files with 197 additions and 127 deletions
183
autowalker.cpp
183
autowalker.cpp
|
@ -1,6 +1,6 @@
|
|||
#include "autowalker.hpp"
|
||||
#include "inventory.hpp"
|
||||
#include "ai.hpp"
|
||||
#include "ai_debug.hpp"
|
||||
|
||||
template<typename Comp>
|
||||
int number_left(gui::FSM& fsm) {
|
||||
|
@ -66,17 +66,17 @@ Pathing Autowalker::path_to_devices() {
|
|||
}
|
||||
|
||||
|
||||
void Autowalker::window_events() {
|
||||
void Autowalker::handle_window_events() {
|
||||
fsm.$window.handleEvents(
|
||||
[&](const sf::Event::KeyPressed &) {
|
||||
fsm.autowalking = false;
|
||||
close_status();
|
||||
log("Aborting autowalk. You can move now.");
|
||||
log("Aborting autowalk.");
|
||||
},
|
||||
[&](const sf::Event::MouseButtonPressed &) {
|
||||
fsm.autowalking = false;
|
||||
close_status();
|
||||
log("Aborting autowalk. You can move now.");
|
||||
log("Aborting autowalk.");
|
||||
}
|
||||
);
|
||||
}
|
||||
|
@ -98,24 +98,26 @@ Point Autowalker::get_current_position() {
|
|||
return player_position.location;
|
||||
}
|
||||
|
||||
void Autowalker::path_fail(Matrix& bad_paths, Point pos) {
|
||||
status("PATH FAIL");
|
||||
log("Autowalk failed to find a path.");
|
||||
matrix::dump("MOVE FAIL PATHS", bad_paths, pos.x, pos.y);
|
||||
send_event(gui::Event::STAIRS_DOWN);
|
||||
}
|
||||
|
||||
bool Autowalker::path_player(Pathing& paths, Point& target_out) {
|
||||
bool found = paths.random_walk(target_out, false, PATHING_TOWARD);
|
||||
|
||||
if(!found) {
|
||||
// failed to find a linear path, try diagonal
|
||||
if(!paths.random_walk(target_out, false, PATHING_TOWARD, MOVE_DIAGONAL)) {
|
||||
status("PATH FAIL");
|
||||
log("Autowalk failed to find a path.");
|
||||
matrix::dump("MOVE FAIL PATHS", paths.$paths, target_out.x, target_out.y);
|
||||
path_fail(paths.$paths, target_out);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
if(!fsm.$level.map->can_move(target_out)) {
|
||||
status("PATH FAIL");
|
||||
log("Autowalk major pathing failure. You can move now.");
|
||||
matrix::dump("BAD TARGET PATHS", paths.$paths, target_out.x, target_out.y);
|
||||
matrix::dump("BAD TARGET MAP", fsm.$level.map->walls(), target_out.x, target_out.y);
|
||||
path_fail(paths.$paths, target_out);
|
||||
return false;
|
||||
}
|
||||
|
||||
|
@ -180,8 +182,78 @@ void Autowalker::rotate_player(Point current, Point target) {
|
|||
"player isn't facing the correct direction");
|
||||
}
|
||||
|
||||
struct InventoryStats {
|
||||
int healing = 0;
|
||||
int other = 0;
|
||||
};
|
||||
|
||||
ai::State Autowalker::update_state(ai::State start) {
|
||||
int enemy_count = number_left<components::Combat>(fsm);
|
||||
int item_count = number_left<components::InventoryItem>(fsm);
|
||||
|
||||
ai::set(start, "no_more_enemies", enemy_count == 0);
|
||||
ai::set(start, "no_more_items", item_count == 0);
|
||||
ai::set(start, "enemy_found",
|
||||
fsm.in_state(gui::State::IN_COMBAT) ||
|
||||
fsm.in_state(gui::State::ATTACKING));
|
||||
ai::set(start, "health_good", player_health_good());
|
||||
ai::set(start, "in_combat",
|
||||
fsm.in_state(gui::State::IN_COMBAT) ||
|
||||
fsm.in_state(gui::State::ATTACKING));
|
||||
|
||||
auto inv = player_item_count();
|
||||
ai::set(start, "have_item", inv.other > 0 || inv.healing > 0);
|
||||
ai::set(start, "have_healing", inv.healing > 0);
|
||||
|
||||
return start;
|
||||
}
|
||||
|
||||
void Autowalker::handle_boss_fight() {
|
||||
// skip the boss fight for now
|
||||
if(fsm.in_state(gui::State::NEXT_LEVEL)) {
|
||||
// eventually we'll have AI handle this too
|
||||
send_event(gui::Event::STAIRS_DOWN);
|
||||
}
|
||||
}
|
||||
|
||||
void Autowalker::handle_player_walk(ai::State& start, ai::State& goal) {
|
||||
start = update_state(start);
|
||||
auto a_plan = ai::plan("Walker::actions", start, goal);
|
||||
dump_script("\n\n\n-----WALKER SCRIPT", start, a_plan.script);
|
||||
auto action = a_plan.script.front();
|
||||
|
||||
if(action.name == "find_enemy") {
|
||||
// this is where to test if enemy found and update state
|
||||
status("FINDING ENEMY");
|
||||
auto paths = path_to_enemies();
|
||||
process_move(paths);
|
||||
send_event(gui::Event::ATTACK);
|
||||
} else if(action.name == "kill_enemy") {
|
||||
status("KILLING ENEMY");
|
||||
process_combat();
|
||||
} else if(action.name == "use_healing") {
|
||||
status("USING HEALING");
|
||||
player_use_healing();
|
||||
} else if(action.name == "collect_items") {
|
||||
status("COLLECTING ITEMS");
|
||||
auto paths = path_to_items();
|
||||
process_move(paths);
|
||||
// path to the items and get them all
|
||||
} else if(action == ai::FINAL_ACTION) {
|
||||
close_status();
|
||||
log("Autowalk done, nothing left to do.");
|
||||
send_event(gui::Event::STAIRS_DOWN);
|
||||
} else {
|
||||
close_status();
|
||||
log("Autowalk has a bug. Unknown action.");
|
||||
fmt::println("Unknown action: {}", action.name);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
void Autowalker::autowalk() {
|
||||
window_events();
|
||||
handle_window_events();
|
||||
if(!fsm.autowalking) {
|
||||
close_status();
|
||||
return;
|
||||
|
@ -193,58 +265,11 @@ void Autowalker::autowalk() {
|
|||
auto goal = ai::load_state("Walker::final_state");
|
||||
|
||||
do {
|
||||
int enemy_count = number_left<components::Combat>(fsm);
|
||||
int item_count = number_left<components::InventoryItem>(fsm);
|
||||
handle_window_events();
|
||||
handle_boss_fight();
|
||||
handle_player_walk(start, goal);
|
||||
|
||||
window_events();
|
||||
ai::set(start, "no_more_enemies", enemy_count == 0);
|
||||
ai::set(start, "no_more_items", item_count == 0);
|
||||
ai::set(start, "enemy_found",
|
||||
fsm.in_state(gui::State::IN_COMBAT) ||
|
||||
fsm.in_state(gui::State::ATTACKING));
|
||||
ai::set(start, "health_good", player_health_good());
|
||||
ai::set(start, "in_combat",
|
||||
fsm.in_state(gui::State::IN_COMBAT) ||
|
||||
fsm.in_state(gui::State::ATTACKING));
|
||||
ai::set(start, "have_item", player_item_count() > 0);
|
||||
|
||||
auto a_plan = ai::plan("Walker::actions", start, goal);
|
||||
|
||||
// need a test for plan complete and only action is END
|
||||
for(auto action : a_plan.script) {
|
||||
if(action.name == "find_enemy") {
|
||||
// this is where to test if enemy found and update state
|
||||
status("FINDING ENEMY");
|
||||
auto paths = path_to_enemies();
|
||||
process_move(paths);
|
||||
send_event(gui::Event::ATTACK);
|
||||
} else if(action.name == "use_item") {
|
||||
status("USE ITEMS");
|
||||
} else if(action.name == "kill_enemy") {
|
||||
status("KILLING ENEMY");
|
||||
process_combat();
|
||||
} else if(action.name == "find_healing") {
|
||||
status("FINDING HEALING");
|
||||
auto paths = path_to_items();
|
||||
process_move(paths);
|
||||
// do the path to healing thing
|
||||
} else if(action.name == "collect_items") {
|
||||
status("COLLECTING ITEMS");
|
||||
auto paths = path_to_items();
|
||||
process_move(paths);
|
||||
// path to the items and get them all
|
||||
} else if(action == ai::FINAL_ACTION) {
|
||||
close_status();
|
||||
log("Autowalk done, nothing left to do.");
|
||||
fsm.autowalking = false;
|
||||
} else {
|
||||
close_status();
|
||||
log("Autowalk has a bug. Unknown action.");
|
||||
fmt::println("Unknown action: {}", action.name);
|
||||
}
|
||||
|
||||
move_attempts++;
|
||||
}
|
||||
move_attempts++;
|
||||
} while(move_attempts < 100 && fsm.autowalking);
|
||||
}
|
||||
|
||||
|
@ -254,8 +279,7 @@ void Autowalker::process_move(Pathing& paths) {
|
|||
|
||||
if(!path_player(paths, target)) {
|
||||
close_status();
|
||||
log("No paths found, aborting autowalk. You can move now.");
|
||||
fsm.autowalking = false;
|
||||
log("No paths found, aborting autowalk.");
|
||||
return;
|
||||
}
|
||||
|
||||
|
@ -277,9 +301,32 @@ bool Autowalker::player_health_good() {
|
|||
return float(combat.hp) / float(combat.max_hp) > 0.5f;
|
||||
}
|
||||
|
||||
int Autowalker::player_item_count() {
|
||||
auto inventory = fsm.$level.world->get<components::Inventory>(fsm.$level.player);
|
||||
return inventory.count();
|
||||
InventoryStats Autowalker::player_item_count() {
|
||||
auto& inventory = fsm.$level.world->get<components::Inventory>(fsm.$level.player);
|
||||
InventoryStats stats;
|
||||
|
||||
for(auto& item : inventory.items) {
|
||||
if(item.data["id"] == "POTION_HEALING_SMALL") {
|
||||
stats.healing += item.count;
|
||||
} else {
|
||||
stats.other += item.count;
|
||||
}
|
||||
}
|
||||
|
||||
return stats;
|
||||
}
|
||||
|
||||
void Autowalker::player_use_healing() {
|
||||
auto& inventory = fsm.$level.world->get<components::Inventory>(fsm.$level.player);
|
||||
// find the healing slot
|
||||
for(size_t slot = 0; slot < inventory.count(); slot++) {
|
||||
auto& item = inventory.get(slot);
|
||||
if(item.data["id"] == "POTION_HEALING_SMALL") {
|
||||
inventory.use(fsm.$level, slot);
|
||||
fsm.$status_ui.update();
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void Autowalker::start_autowalk() {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue