Pathing in either diagonal or simple motion works.
This commit is contained in:
parent
e92fd2b6f3
commit
fc678c6b42
3 changed files with 34 additions and 20 deletions
31
pathing.cpp
31
pathing.cpp
|
@ -76,29 +76,42 @@ void Pathing::clear_target(const Point &at) {
|
|||
|
||||
PathingResult Pathing::find_path(Point &out, int direction, bool diag)
|
||||
{
|
||||
(void)diag;
|
||||
|
||||
// get the current dijkstra number
|
||||
int cur = $paths[out.y][out.x];
|
||||
int target = cur;
|
||||
bool found = false;
|
||||
|
||||
// go through all possible directions
|
||||
for(matrix::box it{$paths, out.x, out.y, 1}; it.next();) {
|
||||
target = $paths[it.y][it.x];
|
||||
// a lambda makes it easy to capture what we have to change
|
||||
auto next_step = [&](size_t x, size_t y) -> bool {
|
||||
target = $paths[y][x];
|
||||
// don't go through walls
|
||||
if(target == WALL_PATH_LIMIT) continue;
|
||||
if(target == WALL_PATH_LIMIT) return false;
|
||||
|
||||
int weight = cur - target;
|
||||
|
||||
if(weight == direction) {
|
||||
out = {(size_t)it.x, (size_t)it.y};
|
||||
out = {x, y};
|
||||
found = true;
|
||||
break;
|
||||
return true;
|
||||
} else if(weight == 0) {
|
||||
out = {(size_t)it.x, (size_t)it.y};
|
||||
out = {x, y};
|
||||
found = true;
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
|
||||
|
||||
if(diag) {
|
||||
for(matrix::box it{$paths, out.x, out.y, 1}; it.next();) {
|
||||
bool should_stop = next_step(it.x, it.y);
|
||||
if(should_stop) break;
|
||||
}
|
||||
} else {
|
||||
for(matrix::compass it{$paths, out.x, out.y}; it.next();) {
|
||||
bool should_stop = next_step(it.x, it.y);
|
||||
if(should_stop) break;
|
||||
}
|
||||
}
|
||||
|
||||
if(target == 0) {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue