From b4569622a0baa4ad2726d7a841f4ce726ad8f9be Mon Sep 17 00:00:00 2001 From: "Zed A. Shaw" Date: Mon, 25 Aug 2025 22:44:53 -0400 Subject: [PATCH] Cleanup the autowalker for new work. --- autowalker.cpp | 34 ++++++++++++++++++++-------------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/autowalker.cpp b/autowalker.cpp index 70fb26f..683c9a8 100644 --- a/autowalker.cpp +++ b/autowalker.cpp @@ -3,13 +3,20 @@ #include "gui/ritual_ui.hpp" #include "game_level.hpp" +struct InventoryStats { + int healing = 0; + int other = 0; +}; + template int number_left() { int count = 0; + auto world = GameDB::current_world(); + auto player = GameDB::the_player(); - GameDB::current_world()->query( + world->query( [&](const auto ent, auto&, auto&) { - if(ent != GameDB::current_level().player) { + if(ent != player) { count++; } }); @@ -20,15 +27,16 @@ int number_left() { template Pathing compute_paths() { - auto& walls_original = GameDB::current_level().map->$walls; + auto& level = GameDB::current_level(); + auto& walls_original = level.map->$walls; auto walls_copy = walls_original; Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)}; - GameDB::current_level().world->query( + level.world->query( [&](const auto ent, auto& position) { - if(ent != GameDB::current_level().player) { - if(GameDB::current_level().world->has(ent)) { + if(ent != level.player) { + if(level.world->has(ent)) { paths.set_target(position.location); } else { // this will mark that spot as a wall so we don't path there temporarily @@ -111,6 +119,7 @@ void Autowalker::path_fail(Matrix& bad_paths, Point pos) { } bool Autowalker::path_player(Pathing& paths, Point& target_out) { + auto &level = GameDB::current_level(); bool found = paths.random_walk(target_out, false, PATHING_TOWARD, 4, 8); if(!found) { @@ -121,7 +130,7 @@ bool Autowalker::path_player(Pathing& paths, Point& target_out) { } } - if(!GameDB::current_level().map->can_move(target_out)) { + if(!level.map->can_move(target_out)) { path_fail(paths.$paths, target_out); return false; } @@ -189,11 +198,6 @@ 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(); int item_count = number_left(); @@ -327,6 +331,7 @@ void Autowalker::autowalk() { } void Autowalker::process_move(Pathing& paths) { + auto world = GameDB::current_world(); Point current = get_current_position(); Point target = current; @@ -341,7 +346,7 @@ void Autowalker::process_move(Pathing& paths) { // what are we aiming at? auto aimed_at = camera_aim(); - if(aimed_at && GameDB::current_world()->has(aimed_at)) { + if(aimed_at && world->has(aimed_at)) { // NOTE: if we're aiming at an item then pick it up // for now just loot it then close to get it off the map send_event(gui::Event::LOOT_ITEM); @@ -361,7 +366,8 @@ void Autowalker::send_event(gui::Event ev) { bool Autowalker::player_health_good() { auto world = GameDB::current_world(); - auto combat = world->get(GameDB::the_player()); + auto player = GameDB::the_player(); + auto combat = world->get(player); return float(combat.hp) / float(combat.max_hp) > 0.5f; }