Fixed a pathfinding bug!


Pathfinding in Spacestation Defense has a fallback: if something can't find a path at all, it'll just run straight toward its goal. This is supposed to only happen every possible path is blocked (which is usually a temporary condition, for example if you have multiple ships trying to get inside a hangar, the ones in the back might see no path for them at the moment, but they will once the ships in front of them get inside, so just continuing straight is a good fallback).

But I was seeing this happen when it shouldn't: a ship trying to get to a hangar would give up and just bang its face into a wall, even when there was nothing blocking the hangar.

The cause had to do with hash collisions in hashmaps. Pathfinding keeps a hashmap of all the blockers it's explored along with the position each was explored from. So there are 4 coordinates, each 32-bit floats, that need to be hashed, but Zig's hashmap API requires the hash output to be a 64-bit int. So what I'd previously done was just convert the floats to ints, add them all together and call that the hash.

It seems obvious in hindsight that this was wrong. It meant that 2 blockers at positions (72, 0) and (0, 72) would be seen as the same, causing pathfinding to sometimes ignore blockers it hadn't yet explored, which caused the failures.

The solution I found was to instead map each of the 4 coordinates to a different part of the output. They had to be shrunk for this, but that's okay, coordinates outside the range of +/- 1000 from the origin basically never happen in this game anyway. So what I did was modulus them to within the max value of a 16-bit signed int (just incase), then convert them to 64-bit signed ints, then bitshift them left by 48, 32, 16, and 0 bits.

With this change, I confirmed the bug no longer happens in my test case. I'll deploy the fix soon. Happy Spacestation defending!

Leave a comment

Log in with itch.io to leave a comment.