thebracket / rltk

Roguelike Toolkit - Modern C++ (14) SFML-based toolkit for creating roguelikes.
MIT License
311 stars 21 forks source link

modifying is_goal function #7

Open aash29 opened 7 years ago

aash29 commented 7 years ago

I wanted to slightly change pathfinding algorithm to find a way to cells adjacent to goal. However, setting is_goal function in navigator class to

static bool is_goal(location_t &pos, location_t &goal) {
    return (std::max(abs(pos.x-goal.x),abs(pos.y-goal.y))<=1);
}

results in unexpected behavior (stopping 2 steps from goal, then teleporting to it). What is the right way to do this?

thebracket commented 7 years ago

I think the issue is that the last step of path is always the destination, so the default path-following code that applies the next path step to the entity's position will cause this jump. I've been getting around this by having the code that follows the path do a distance check and stop if the distance is 1.5 (to allow for diagonals) or less - or by searching for a path to a tile adjacent to the final destination (in cases where the final destination is impassible). A quick way to do it would be to replace if (path.steps.empty()) with if (path.steps.size() < 2 in the path-following code.

I'll try and come up with a better example later today.