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