More cleanup then starting to sort out how to make systems extensible easily.

This commit is contained in:
Zed A. Shaw 2026-03-22 13:24:03 -04:00
parent 6a0c9e8d46
commit cbff127b40
13 changed files with 106 additions and 769 deletions

View file

@ -1,420 +0,0 @@
#include "game/autowalker.hpp"
#include "ai/ai_debug.hpp"
#include "game/level.hpp"
#include "game/systems.hpp"
struct InventoryStats {
int healing = 0;
int other = 0;
};
template<typename Comp>
int number_left() {
int count = 0;
auto world = GameDB::current_world();
auto player = GameDB::the_player();
world->query<components::Position, Comp>(
[&](const auto ent, auto&, auto&) {
if(ent != player) {
count++;
}
});
return count;
}
template<typename Comp>
Pathing compute_paths() {
auto& level = GameDB::current_level();
auto walls_copy = level.map->$walls;
Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)};
System::multi_path<Comp>(level, paths, walls_copy);
return paths;
}
DinkyECS::Entity Autowalker::camera_aim() {
auto& level = GameDB::current_level();
auto player_pos = GameDB::player_position();
// what happens if there's two things at that spot
if(level.collision->something_there(player_pos.aiming_at)) {
return level.collision->get(player_pos.aiming_at);
} else {
return DinkyECS::NONE;
}
}
void Autowalker::log(std::wstring msg) {
fsm.$map_ui.log(msg);
}
void Autowalker::status(std::wstring msg) {
fsm.$main_ui.$overlay_ui.show_text("bottom", msg);
}
void Autowalker::close_status() {
fsm.$main_ui.$overlay_ui.close_text("bottom");
}
Pathing Autowalker::path_to_enemies() {
return compute_paths<components::Combat>();
}
Pathing Autowalker::path_to_items() {
return compute_paths<components::Curative>();
}
void Autowalker::handle_window_events() {
fsm.$window.handleEvents(
[&](const sf::Event::KeyPressed &) {
fsm.autowalking = false;
close_status();
log(L"Aborting autowalk.");
},
[&](const sf::Event::MouseButtonPressed &) {
fsm.autowalking = false;
close_status();
log(L"Aborting autowalk.");
}
);
}
void Autowalker::process_combat() {
while(fsm.in_state(gui::State::IN_COMBAT)
|| fsm.in_state(gui::State::ATTACKING))
{
if(fsm.in_state(gui::State::ATTACKING)) {
send_event(game::Event::TICK);
} else {
send_event(game::Event::ATTACK);
}
}
}
void Autowalker::path_fail(const std::string& msg, Matrix& bad_paths, Point pos) {
dbc::log(msg);
status(L"PATH FAIL");
matrix::dump("MOVE FAIL PATHS", bad_paths, pos.x, pos.y);
log(L"Autowalk failed to find a path.");
send_event(game::Event::BOSS_START);
}
bool Autowalker::path_player(Pathing& paths, Point& target_out) {
auto& level = GameDB::current_level();
auto found = paths.find_path(target_out, PATHING_TOWARD, false);
if(found == PathingResult::FAIL) {
// failed to find a linear path, try diagonal
if(paths.find_path(target_out, PATHING_TOWARD, true) == PathingResult::FAIL) {
path_fail("random_walk", paths.$paths, target_out);
return false;
}
}
if(!level.map->can_move(target_out)) {
path_fail("level_map->can_move", paths.$paths, target_out);
return false;
}
return true;
}
void Autowalker::rotate_player(Point target) {
auto &player = GameDB::player_position();
if(target == player.location) {
dbc::log("player stuck at a locatoin");
fsm.autowalking = false;
return;
}
auto dir = System::shortest_rotate(player.location, player.aiming_at, target);
for(int i = 0; player.aiming_at != target; i++) {
if(i > 10) {
dbc::log("HIT OVER ROTATE BUG!");
break;
}
send_event(dir);
while(fsm.in_state(gui::State::ROTATING) ||
fsm.in_state(gui::State::COMBAT_ROTATE))
{
send_event(game::Event::TICK);
}
}
fsm.autowalking = player.aiming_at == target;
}
void Autowalker::update_state(ai::EntityAI& player_ai) {
int enemy_count = number_left<components::Combat>();
int item_count = number_left<components::InventoryItem>();
player_ai.set_state("no_more_enemies", enemy_count == 0);
player_ai.set_state("no_more_items", item_count == 0);
player_ai.set_state("enemy_found", found_enemy());
player_ai.set_state("health_good", player_health_good());
player_ai.set_state("in_combat",
fsm.in_state(gui::State::IN_COMBAT) ||
fsm.in_state(gui::State::ATTACKING));
auto inv = player_item_count();
player_ai.set_state("have_item", inv.other > 0 || inv.healing > 0);
player_ai.set_state("have_healing", inv.healing > 0);
player_ai.update();
}
void Autowalker::handle_player_walk(ai::State& start, ai::State& goal) {
ai::EntityAI player_ai("Host::actions", start, goal);
update_state(player_ai);
auto level = GameDB::current_level();
if(player_ai.wants_to("find_enemy")) {
status(L"FINDING ENEMY");
auto paths = path_to_enemies();
process_move(paths, [&](auto target) -> bool {
return level.collision->occupied(target);
});
face_enemy();
} else if(player_ai.wants_to("kill_enemy")) {
status(L"KILLING ENEMY");
if(fsm.in_state(gui::State::IN_COMBAT)) {
if(face_enemy()) {
process_combat();
}
}
} else if(player_ai.wants_to("use_healing")) {
status(L"USING HEALING");
player_use_healing();
} else if(player_ai.wants_to("collect_items") || player_ai.wants_to("find_healing")) {
fmt::println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>");
status(player_ai.wants_to("collect_items") ? L"COLLECTING ITEMS" : L"FIND HEALING");
player_ai.dump();
auto paths = path_to_items();
bool found_it = process_move(paths, [&](auto target) -> bool {
if(!level.collision->something_there(target)) return false;
auto entity = level.collision->get(target);
return level.world->has<components::Curative>(entity);
});
if(found_it) pickup_item();
fmt::println("<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<");
} else if(!player_ai.active()) {
close_status();
log(L"FINAL ACTION! Autowalk done.");
fsm.autowalking = false;
} else {
close_status();
dbc::log($F("Unknown action: {}", player_ai.to_string()));
}
}
void Autowalker::open_map() {
if(!map_opened_once) {
if(!fsm.$map_open) {
send_event(game::Event::MAP_OPEN);
map_opened_once = true;
}
}
}
void Autowalker::close_map() {
if(fsm.$map_open) {
send_event(game::Event::MAP_OPEN);
}
}
void Autowalker::autowalk() {
handle_window_events();
if(!fsm.autowalking) {
close_status();
return;
}
open_map();
face_enemy();
int move_attempts = 0;
auto start = ai::load_state("Host::initial_state");
auto goal = ai::load_state("Host::final_state");
do {
handle_window_events();
handle_player_walk(start, goal);
close_map();
move_attempts++;
} while(move_attempts < 100 && fsm.autowalking);
}
bool Autowalker::process_move(Pathing& paths, std::function<bool(Point)> is_that_it) {
// target has to start at the player location then...
auto target_out = GameDB::player_position().location;
// ... target gets modified as an out parameter to find the path
if(!path_player(paths, target_out)) {
close_status();
log(L"No paths found, aborting autowalk.");
return false;
}
if(rayview->aiming_at != target_out) rotate_player(target_out);
bool found_it = is_that_it(target_out);
if(!found_it) {
send_event(game::Event::MOVE_FORWARD);
while(fsm.in_state(gui::State::MOVING)) send_event(game::Event::TICK);
}
return found_it;
}
bool Autowalker::found_enemy() {
auto& level = GameDB::current_level();
auto player = GameDB::player_position();
for(matrix::compass it{level.map->$walls, player.location.x, player.location.y}; it.next();) {
Point aim{it.x, it.y};
auto aimed_ent = level.collision->occupied_by(player.aiming_at);
if(aim != player.aiming_at || aimed_ent == DinkyECS::NONE) continue;
if(level.world->has<components::Combat>(aimed_ent)) return true;
}
return false;
}
bool Autowalker::found_item() {
auto world = GameDB::current_world();
auto aimed_at = camera_aim();
return aimed_at != DinkyECS::NONE && world->has<components::InventoryItem>(aimed_at);
}
void Autowalker::send_event(game::Event ev, std::any data) {
fsm.event(ev, data);
fsm.render();
fsm.handle_world_events();
}
bool Autowalker::player_health_good() {
auto world = GameDB::current_world();
auto player = GameDB::the_player();
auto combat = world->get<components::Combat>(player);
float health = float(combat.hp) / float(combat.max_hp);
return health > 0.5f;
}
InventoryStats Autowalker::player_item_count() {
InventoryStats stats;
auto& level = GameDB::current_level();
auto& inventory = level.world->get<inventory::Model>(level.player);
if(inventory.has("pocket_r")) {
stats.other += 1;
stats.healing += 1;
}
if(inventory.has("pocket_l")) {
stats.other += 1;
stats.healing += 1;
}
return stats;
}
void Autowalker::player_use_healing() {
auto& level = GameDB::current_level();
auto& inventory = level.world->get<inventory::Model>(level.player);
if(inventory.has("pocket_r")) {
auto gui_id = fsm.$status_ui.$gui.entity("pocket_r");
send_event(game::Event::USE_ITEM, gui_id);
}
if(inventory.has("pocket_l")) {
auto gui_id = fsm.$status_ui.$gui.entity("pocket_l");
send_event(game::Event::USE_ITEM, gui_id);
}
}
void Autowalker::start_autowalk() {
fsm.autowalking = true;
}
void Autowalker::face_target(Point target) {
if(rayview->aiming_at != target) rotate_player(target);
}
bool Autowalker::face_enemy() {
auto& level = GameDB::current_level();
auto player_at = GameDB::player_position();
auto [found, neighbors] = level.collision->neighbors(player_at.location, true);
if(found) {
auto enemy_pos = level.world->get<components::Position>(neighbors[0]);
face_target(enemy_pos.location);
} else {
dbc::log("No enemies nearby, moving on.");
}
return found;
}
void Autowalker::click_inventory(const std::string& name, guecs::Modifiers mods) {
auto& cell = fsm.$status_ui.$gui.cell_for(name);
fsm.$status_ui.mouse(cell.mid_x, cell.mid_y, mods);
fsm.handle_world_events();
}
void Autowalker::pocket_potion(GameDB::Level &level) {
auto& inventory = level.world->get<inventory::Model>(level.player);
if(inventory.has("pocket_r") && inventory.has("pocket_l")) {
player_use_healing();
}
send_event(game::Event::AIM_CLICK);
if(inventory.has("pocket_r")) {
click_inventory("pocket_l", {1 << guecs::ModBit::left});
} else {
click_inventory("pocket_r", {1 << guecs::ModBit::left});
}
}
void Autowalker::pickup_item() {
auto& level = GameDB::current_level();
auto& player_pos = GameDB::player_position();
auto collision = level.collision;
if(collision->something_there(player_pos.aiming_at)) {
auto entity = collision->get(player_pos.aiming_at);
fmt::println("AIMING AT entity {} @ {},{}",
entity, player_pos.aiming_at.x, player_pos.aiming_at.y);
if(level.world->has<components::Curative>(entity)) {
pocket_potion(level);
status(L"A POTION");
} else {
send_event(game::Event::AIM_CLICK);
status(L"I DON'T KNOW");
}
}
}

View file

@ -1,51 +0,0 @@
#pragma once
#include "ai/ai.hpp"
#include "gui/fsm.hpp"
#include <guecs/ui.hpp>
struct InventoryStats;
struct Autowalker {
int enemy_count = 0;
int item_count = 0;
int device_count = 0;
bool map_opened_once = false;
gui::FSM& fsm;
std::shared_ptr<Raycaster> rayview;
Autowalker(gui::FSM& fsm)
: fsm(fsm), rayview(fsm.$main_ui.$rayview) {}
void autowalk();
void start_autowalk();
void open_map();
void close_map();
bool found_enemy();
bool found_item();
void handle_window_events();
void handle_player_walk(ai::State& start, ai::State& goal);
void send_event(game::Event ev, std::any data={});
void process_combat();
bool process_move(Pathing& paths, std::function<bool(Point)> cb);
bool path_player(Pathing& paths, Point &target_out);
void path_fail(const std::string& msg, Matrix& bad_paths, Point pos);
void rotate_player(Point target);
void log(std::wstring msg);
void status(std::wstring msg);
void close_status();
bool player_health_good();
void player_use_healing();
InventoryStats player_item_count();
void update_state(ai::EntityAI& player_ai);
DinkyECS::Entity camera_aim();
Pathing path_to_enemies();
Pathing path_to_items();
void face_target(Point target);
bool face_enemy();
void pickup_item();
void pocket_potion(GameDB::Level &level);
void click_inventory(const std::string& name, guecs::Modifiers mods);
};

View file

@ -366,10 +366,6 @@ void System::device(World &world, Entity actor, Entity item) {
for(auto event : device.events) {
if(event == "STAIRS_DOWN") {
world.send<game::Event>(game::Event::STAIRS_DOWN, actor, device);
} else if(event == "STAIRS_UP") {
world.send<game::Event>(game::Event::STAIRS_UP, actor, device);
} else if(event == "TRAP") {
world.send<game::Event>(game::Event::TRAP, actor, device);
} else if(event == "LOOT_CONTAINER") {
world.send<game::Event>(game::Event::LOOT_CONTAINER, actor, device);
} else {
@ -388,25 +384,6 @@ void System::move_player(Position move_to) {
level.collision->move(old_pos.location, move_to.location, level.player);
}
void System::player_status() {
auto& level = GameDB::current_level();
auto& combat = level.world->get<Combat>(level.player);
float percent = float(combat.hp) / float(combat.max_hp);
if(percent > 0.8) {
sound::play("hp_status_80");
} else if(percent > 0.6) {
sound::play("hp_status_60");
} else if(percent > 0.3) {
sound::play("hp_status_30");
} else if(percent > 0.1) {
sound::play("hp_status_10");
} else {
sound::play("hp_status_00");
}
}
std::shared_ptr<sf::Shader> System::sprite_effect(Entity entity) {
auto world = GameDB::current_world();
if(auto se = world->get_if<SpriteEffect>(entity)) {
@ -497,96 +474,6 @@ bool System::inventory_occupied(Entity container_id, const std::string& name) {
return inventory.has(name);
}
void System::draw_map(Matrix& grid, EntityGrid& entity_map) {
auto& level = GameDB::current_level();
auto& world = *level.world;
Map &map = *level.map;
Matrix &fow = level.lights->$fow;
size_t view_x = matrix::width(grid) - 1;
size_t view_y = matrix::height(grid) - 1;
entity_map.clear();
auto player_pos = world.get<Position>(level.player).location;
Point cam_orig = map.center_camera(player_pos, view_x, view_y);
auto &tiles = map.tiles();
auto &tile_set = textures::get_map_tile_set();
/* I'm doing double tid->wchar_t conversion here, maybe just
* render the tids into the grid then let someone else do this. */
// first fill it with the map cells
for(shiterator::each_cell_t it{grid}; it.next();) {
size_t tile_y = size_t(it.y) + cam_orig.y;
size_t tile_x = size_t(it.x) + cam_orig.x;
if(matrix::inbounds(tiles, tile_x, tile_y) && fow[tile_y][tile_x]) {
size_t tid = tiles[tile_y][tile_x];
grid[it.y][it.x] = tile_set[tid];
} else {
grid[it.y][it.x] = L' ';
}
}
// then get the enemy/item/device tiles and fill those in
world.query<Position, Tile>([&](auto, auto &pos, auto &entity_glyph) {
// BUG: don't I have a within bounds macro somewhere?
if(pos.location.x >= cam_orig.x
&& pos.location.x <= cam_orig.x + view_x
&& pos.location.y >= cam_orig.y
&& pos.location.y <= cam_orig.y + view_y)
{
if(fow[pos.location.y][pos.location.x]) {
Point view_pos = map.map_to_camera(pos.location, cam_orig);
entity_map.insert_or_assign(view_pos, entity_glyph.display);
}
}
});
}
void System::render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture& render, int compass_dir, wchar_t player_display) {
sf::Vector2i tile_sprite_dim{MAP_TILE_DIM,MAP_TILE_DIM};
unsigned int width = matrix::width(tiles);
unsigned int height = matrix::height(tiles);
sf::Vector2u dim{width * tile_sprite_dim.x, height * tile_sprite_dim.y};
auto render_size = render.getSize();
if(render_size.x != width || render_size.y != height) {
bool worked = render.resize(dim);
dbc::check(worked, "Failed to resize map render target.");
}
render.clear({0,0,0,255});
for(matrix::each_row it{tiles}; it.next();) {
wchar_t display = tiles[it.y][it.x];
if(display == L' ') continue; // skip for now
auto& sprite = textures::get_map_sprite(display);
sprite.setPosition({float(it.x * tile_sprite_dim.x), float(it.y * tile_sprite_dim.y)});
render.draw(sprite);
}
for(auto [point, display] : entity_map) {
auto& sprite = textures::get_map_sprite(display);
if(display == player_display) {
sf::Vector2f center{float(tile_sprite_dim.x / 2), float(tile_sprite_dim.y / 2)};
float degrees = (((compass_dir * 45) + PLAYER_SPRITE_DIR_CORRECTION) % 360);
sprite.setOrigin(center);
sprite.setRotation(sf::degrees(degrees));
sprite.setPosition({float(point.x * tile_sprite_dim.x) + center.x, float(point.y * tile_sprite_dim.y) + center.y});
} else {
sprite.setPosition({float(point.x * tile_sprite_dim.x), float(point.y * tile_sprite_dim.y)});
}
render.draw(sprite);
}
render.display();
}
bool System::use_item(const string& slot_name) {
auto& level = GameDB::current_level();
auto& world = *level.world;

View file

@ -28,7 +28,6 @@ namespace System {
void combat(int attack_id);
std::shared_ptr<sf::Shader> sprite_effect(Entity entity);
void player_status();
void distribute_loot(Position target_pos);
void pickup();
@ -40,9 +39,6 @@ namespace System {
void inventory_swap(Entity container_id, const std::string& a_name, const std::string &b_name);
bool inventory_occupied(Entity container_id, const std::string& name);
void draw_map(Matrix& grid, EntityGrid& entity_map);
void render_map(Matrix& tiles, EntityGrid& entity_map, sf::RenderTexture& render, int compass_dir, wchar_t player_display);
void set_position(DinkyECS::World& world, SpatialMap& collision, Entity entity, Position pos);
bool use_item(const std::string& slot_name);