The code pasted earlier has a bug, only checks for diagonal moves. "(dx != 0 && dy != 0)" should be "(dx != 0 || dy != 0)". It's fine other than that and It's way faster than I thought it would be, in 1000ms over 1500 iterations of randomly filling a 40x40 map with walls, basic and one-way tiles, picking 5 random sources, 5 random targets and computing. Now to add LOS.
Thank you Z, helped a ton. I think I'll stick around quite a bit
ps: Hydra Slayer is a pretty cool game
Do you get to wield more than 2 weapons at some point?
EDIT:
Okay. A fun issue I had I'd like to relate here.
I had another bug with my code, not directly with the code posted earlier but with my Vector class.
To know when to stop computing I check if a set of targets is empty or not. Targets are x, y coordinates stored via a Vector class. The set is a HashSet<Vector>. But the remove operation didn't work and the algorithm computed all the map.
The reason wasn't that hard to find: my Vector class did override the "equals" method but not the "hashCode" one.
Lost no time and added a StringBuffer based one:
public int hashCode() {
StringBuffer result = new StringBuffer(x);
result.append(" "); // so 11, 1 and 1, 11 don't have same hash
result.append(y);
return result.toString().hashCode();
}
Worked as intented but the algorithm got 30% slower (a bit more than 1000 iterations per 1000 ms). Meaning that I'd have better performances WITHOUT a set of targets and by letting the algorithm compute the all map.
I then tried another hash:
public int hashCode() {
return x + y * 10000; // correct as long the width is <= 10000
}
Still worked as intented but was now 30% faster than the bugged version and twice faster than the previous working version (a bit more than 2100 iterations per 1000 ms). Felt good!
But out of curiosity I removed the target based test all together and let the algorithm compute the all map. It got only 5% slower (if not less) than the last version (a bit more than 2000 iterations per 1000 ms).
I guess it illustrates quite well what people keep telling me: don't bother with optimisations yet. I removed all target related stuff as it is as fast and easier to use.