Rogue: Savage Rats, a retro-themed dungeon crawler
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
rogue-savage-rats/src/mightypork/rogue/world/pathfinding/FloodFill.java

62 lines
1.4 KiB

package mightypork.rogue.world.pathfinding;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Queue;
import mightypork.rogue.world.Coord;
public class FloodFill {
/**
* Fill an area
*
* @param start start point
* @param context filling context
* @param foundNodes collection to put filled coords in
* @return true if fill was successful; false if max range was reached.
*/
public static final boolean fill(Coord start, FillContext context, Collection<Coord> foundNodes)
{
final Queue<Coord> activeNodes = new LinkedList<>();
final double maxDist = context.getMaxDistance();
activeNodes.add(start);
final Coord[] sides = context.getSpreadSides();
boolean forceSpreadNext = context.forceSpreadStart();
boolean limitReached = false;
while (!activeNodes.isEmpty()) {
final Coord current = activeNodes.poll();
foundNodes.add(current);
if (!context.canSpreadFrom(current) && !forceSpreadNext) continue;
forceSpreadNext = false;
for (final Coord spr : sides) {
final Coord next = current.add(spr);
if (activeNodes.contains(next) || foundNodes.contains(next)) continue;
if (next.dist(start) > maxDist) {
limitReached = true;
continue;
}
if (context.canEnter(next)) {
activeNodes.add(next);
} else {
foundNodes.add(next);
}
}
}
return !limitReached;
}
}