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/gamecore/util/math/algo/floodfill/FloodFill.java

63 lines
1.5 KiB

package mightypork.gamecore.util.math.algo.floodfill;
import java.util.Collection;
import java.util.LinkedList;
import java.util.Queue;
import mightypork.gamecore.util.math.algo.Coord;
import mightypork.gamecore.util.math.algo.Step;
10 years ago
public class FloodFill {
/**
* Fill an area
10 years ago
*
* @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)
{
10 years ago
final Queue<Coord> activeNodes = new LinkedList<>();
10 years ago
final double maxDist = context.getMaxDistance();
activeNodes.add(start);
final Step[] sides = context.getSpreadSides();
boolean forceSpreadNext = context.forceSpreadStart();
boolean limitReached = false;
10 years ago
while (!activeNodes.isEmpty()) {
final Coord current = activeNodes.poll();
foundNodes.add(current);
10 years ago
if (!context.canSpreadFrom(current) && !forceSpreadNext) continue;
forceSpreadNext = false;
for (final Step spr : sides) {
10 years ago
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;
}
}