Exploring raycasters and possibly make a little "doom like" game based on it.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 
raycaster/autowalker.cpp

276 lines
7.8 KiB

#include "autowalker.hpp"
#include "inventory.hpp"
#include "ai.hpp"
template<typename Comp>
int number_left(gui::FSM& fsm) {
int count = 0;
fsm.$level.world->query<components::Position, Comp>(
[&](const auto ent, auto&, auto&) {
if(ent != fsm.$level.player) {
count++;
}
});
return count;
}
template<typename Comp>
Pathing compute_paths(gui::FSM& fsm) {
auto& walls_original = fsm.$level.map->$walls;
auto walls_copy = walls_original;
Pathing paths{matrix::width(walls_copy), matrix::height(walls_copy)};
fsm.$level.world->query<components::Position>(
[&](const auto ent, auto& position) {
if(ent != fsm.$level.player) {
if(fsm.$level.world->has<Comp>(ent)) {
paths.set_target(position.location);
} else {
// this will mark that spot as a wall so we don't path there temporarily
walls_copy[position.location.y][position.location.x] = WALL_PATH_LIMIT;
}
}
});
paths.compute_paths(walls_copy);
return paths;
}
Pathing Autowalker::path_to_enemies() {
return compute_paths<components::Combat>(fsm);
}
Pathing Autowalker::path_to_items() {
return compute_paths<components::InventoryItem>(fsm);
}
Pathing Autowalker::path_to_devices() {
return compute_paths<components::Device>(fsm);
}
void Autowalker::window_events() {
fsm.$window.handleEvents(
[&](const sf::Event::KeyPressed &) {
fsm.autowalking = false;
fmt::println("ABORT AUTOWALK");
},
[&](const sf::Event::MouseButtonPressed &) {
fsm.autowalking = false;
fmt::println("ABORT 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)) {
fmt::println("In attacking state, sending a TICK");
send_event(gui::Event::TICK);
} else {
fmt::println("Not in ATTACK, sending an ATTACK to continue combat.");
send_event(gui::Event::ATTACK);;
}
}
}
Point Autowalker::get_current_position() {
auto& player_position = fsm.$level.world->get<components::Position>(fsm.$level.player);
return player_position.location;
}
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)) {
dbc::log("couldn't find a diagonal direction");
matrix::dump("MOVE FAIL PATHS", paths.$paths, target_out.x, target_out.y);
return false;
}
}
if(!fsm.$level.map->can_move(target_out)) {
dbc::log("neighbors is telling me to go to a bad spot.");
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);
return false;
}
return true;
}
void Autowalker::rotate_player(Point current, Point target) {
int delta_x = int(target.x) - int(current.x);
int delta_y = int(target.y) - int(current.y);
int facing = fsm.$main_ui.$compass_dir;
int target_facing = 0;
/* This is a massive pile of garbage. Need a way
* to determine player facing direction without
* hacking into the compass, and also do accurate
* turns.
*/
if(delta_x == -1 && delta_y == 0) {
// west
target_facing = 4;
} else if(delta_x == 1 && delta_y == 0) {
// east
target_facing = 0;
} else if(delta_x == 0 && delta_y == 1) {
// south
target_facing = 2;
} else if(delta_x == 0 && delta_y == -1) {
// north
target_facing = 6;
} else if(delta_x == 1 && delta_y == -1) {
// north east
target_facing = 5;
} else if(delta_x == 1 && delta_y == 1) {
// south east
target_facing = 1;
} else if(delta_x == -1 && delta_y == 1) {
// south west
target_facing = 3;
} else if(delta_x == -1 && delta_y == -1) {
// north west
target_facing = 5;
} else {
dbc::sentinel(
fmt::format("got more than 8 direction result: "
"current={},{} "
"target={},{} "
"delta={},{} ",
current.x, current.y,
target.x, target.y,
delta_x, delta_y));
}
auto dir = facing > target_facing ? gui::Event::ROTATE_LEFT : gui::Event::ROTATE_RIGHT;
while(facing != target_facing) {
send_event(dir);
facing = fsm.$main_ui.$compass_dir;
}
dbc::check(fsm.$main_ui.$compass_dir == target_facing,
"player isn't facing the correct direction");
}
void Autowalker::show_map_overlay(matrix::Matrix& map, Point current) {
auto debug = fsm.$level.world->get_the<components::Debug>();
if(!debug.FPS) {
fsm.$main_ui.$overlay_ui.close_text("top_right");
return;
}
std::string map_overlay;
for(matrix::box it{map, current.x, current.y, 6}; it.next();) {
if(it.x == it.left) map_overlay += "\n";
int cell = map[it.y][it.x];
if(it.x == current.x && it.y == current.y) {
map_overlay += fmt::format("{:x}<", cell);
} else if(cell == WALL_PATH_LIMIT) {
map_overlay += fmt::format("# ");
} else if(cell > 15) {
map_overlay += fmt::format("* ");
} else {
map_overlay += fmt::format("{:x} ", cell);
}
}
fsm.$main_ui.$overlay_ui.show_text("top_right", map_overlay);
}
void Autowalker::autowalk() {
window_events();
if(!fsm.autowalking) return;
int move_attempts = 0;
auto start = ai::load_state("Walker::initial_state");
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);
fmt::println("ENEMY COUNT: {}, ITEM COUNT: {}", enemy_count, item_count);
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));
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
fmt::println("FINDING AN ENEMY");
auto paths = path_to_enemies();
process_move(paths);
send_event(gui::Event::ATTACK);
} else if(action.name == "kill_enemy") {
fmt::println("KILLING ENEMY");
process_combat();
} else if(action.name == "find_healing") {
fmt::println("FINDING HEALING");
auto paths = path_to_items();
process_move(paths);
// do the path to healing thing
} else if(action.name == "collect_items") {
fmt::println("COLLECTING ITEMS");
auto paths = path_to_items();
process_move(paths);
// path to the items and get them all
} else if(action == ai::FINAL_ACTION) {
fmt::println("END STATE, complete? {}", a_plan.complete);
fsm.autowalking = false;
} else {
fmt::println("Unknown action: {}", action.name);
}
move_attempts++;
}
} while(move_attempts < 100 && fsm.autowalking);
}
void Autowalker::process_move(Pathing& paths) {
Point current = get_current_position();
Point target = current;
if(!path_player(paths, target)) {
dbc::log("no paths found, aborting autowalk");
fsm.autowalking = false;
return;
}
rotate_player(current, target);
while(fsm.in_state(gui::State::ROTATING)) send_event(gui::Event::TICK);
send_event(gui::Event::MOVE_FORWARD);
while(fsm.in_state(gui::State::MOVING)) send_event(gui::Event::TICK);
}
void Autowalker::send_event(gui::Event ev) {
fsm.event(ev);
fsm.render();
fsm.handle_world_events();
}
void Autowalker::start_autowalk() {
fsm.autowalking = true;
}