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.
59 lines
1.3 KiB
59 lines
1.3 KiB
package mightypork.rogue.world.pathfinding;
|
|
|
|
|
|
import java.util.*;
|
|
|
|
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)
|
|
{
|
|
Queue<Coord> activeNodes = new LinkedList<>();
|
|
|
|
double maxDist = context.getMaxDistance();
|
|
|
|
activeNodes.add(start);
|
|
|
|
Coord[] sides = context.getSpreadSides();
|
|
boolean forceSpreadNext = context.forceSpreadStart();
|
|
|
|
boolean limitReached = false;
|
|
|
|
while (!activeNodes.isEmpty()) {
|
|
Coord current = activeNodes.poll();
|
|
foundNodes.add(current);
|
|
|
|
if(!context.canSpreadFrom(current) && !forceSpreadNext) continue;
|
|
|
|
forceSpreadNext = false;
|
|
|
|
|
|
for (Coord spr : sides) {
|
|
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;
|
|
}
|
|
}
|
|
|