RogueLife/src/pathfinding.h

55 lines
2.3 KiB
C
Raw Normal View History

2021-06-15 17:27:30 +02:00
//---
// pathfinding: Methods to find short enough paths to move around the map
//---
#pragma once
#include "map.h"
#include "geometry.h"
/* Returns a BFS from start to end, with grid-based points. */
ipoint_t *bfs(map_t const *map, ipoint_t start, ipoint_t end, int *count);
/* raycast_clear(): Check collisions with the map for a straight ray
This function checks if the straight-line path from [start] to [end] is
clear of collisions with walls. It checks all tile transitions to ensure
that no solid tile is crossed. This function checks movement only for a ray
of no width, which is suitable for objects tht do not collide with walls.
TODO: Use tile hitboxes rather than simply checking if the tile is solid.
When hitting diagonal corners perfectly, this functions moves up/down before
moving left/right and ignores the other possibility.
Returns [true] if the path is clear, [false] otherwise. */
bool raycast_clear(map_t const *map, fpoint_t start, fpoint_t end);
/* raycast_clear_hitbox(): Check collisions with the map for a hitbox
This function is similar to raycast_clear(), but it accounts for the width
of the hitbox being moved. The hitbox should be centered around (0,0) so
that the path brings that center from [start] to [end].
This function simply checks raycast_clear() for two separate rays at the
opposite corners of the hitbox, perpendicular to the direction of the
movement. In addition, collisions at the start and end point are tested.
TODO: Currently, raycast_clear_hitbox() does not work if the hitbox if
larger than the obstacles to avoid (as it casts rays only at the
corners of the hitbox, not in the middle).
Returns [true] if the path is clear, [false] otherwise. */
bool raycast_clear_hitbox(map_t const *map, fpoint_t start, fpoint_t end,
frect_t hitbox);
/* Returns a full continuous path from start to end. */
fpoint_t *pathfind(map_t const *map, fpoint_t start, fpoint_t end, int *count,
ipoint_t **p_grid, int *p_grid_length, frect_t hitbox);
/* Returns the first point to treach to move from start to end. Note that the
direction can be reevaluated at each frame, giving a smooth path. This is
faster than pathfind(). Returns [start] if pathfinding fails. */
fpoint_t pathfind_one(map_t const *map, fpoint_t start, fpoint_t end,
frect_t hitbox);