parent
7f7b58733d
commit
f3aab596aa
@ -1,51 +1,59 @@ |
|||||||
package mightypork.rogue.world.pathfinding; |
package mightypork.rogue.world.pathfinding; |
||||||
|
|
||||||
|
|
||||||
import java.util.Collection; |
import java.util.*; |
||||||
import java.util.HashSet; |
|
||||||
import java.util.Set; |
|
||||||
import java.util.Stack; |
|
||||||
|
|
||||||
import mightypork.rogue.world.Coord; |
import mightypork.rogue.world.Coord; |
||||||
|
|
||||||
|
|
||||||
public class FloodFill { |
public class FloodFill { |
||||||
|
|
||||||
public static final Collection<Coord> fill(Coord start, FillContext context) |
/** |
||||||
|
* 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) |
||||||
{ |
{ |
||||||
Set<Coord> filled = new HashSet<>(); |
Queue<Coord> activeNodes = new LinkedList<>(); |
||||||
Stack<Coord> active = new Stack<>(); |
|
||||||
|
|
||||||
double maxDist = context.getMaxDistance(); |
double maxDist = context.getMaxDistance(); |
||||||
|
|
||||||
active.push(start); |
activeNodes.add(start); |
||||||
|
|
||||||
Coord[] sides = context.getSpreadSides(); |
Coord[] sides = context.getSpreadSides(); |
||||||
boolean first = true; |
boolean forceSpreadNext = context.forceSpreadStart(); |
||||||
|
|
||||||
while (!active.isEmpty()) { |
boolean limitReached = false; |
||||||
Coord current = active.pop(); |
|
||||||
filled.add(current); |
|
||||||
|
|
||||||
if(!context.canSpreadFrom(current) && !first) continue; |
while (!activeNodes.isEmpty()) { |
||||||
|
Coord current = activeNodes.poll(); |
||||||
|
foundNodes.add(current); |
||||||
|
|
||||||
first = false; |
if(!context.canSpreadFrom(current) && !forceSpreadNext) continue; |
||||||
|
|
||||||
|
forceSpreadNext = false; |
||||||
|
|
||||||
|
|
||||||
for (Coord spr : sides) { |
for (Coord spr : sides) { |
||||||
Coord next = current.add(spr); |
Coord next = current.add(spr); |
||||||
if(active.contains(next) || filled.contains(next)) continue; |
if(activeNodes.contains(next) || foundNodes.contains(next)) continue; |
||||||
|
|
||||||
if (next.dist(start) > maxDist) continue; |
if (next.dist(start) > maxDist) { |
||||||
|
limitReached = true; |
||||||
|
continue; |
||||||
|
} |
||||||
|
|
||||||
if (context.canEnter(next)) { |
if (context.canEnter(next)) { |
||||||
active.push(next); |
activeNodes.add(next); |
||||||
} else { |
} else { |
||||||
filled.add(next); |
foundNodes.add(next); |
||||||
} |
} |
||||||
} |
} |
||||||
} |
} |
||||||
|
|
||||||
return filled; |
return !limitReached; |
||||||
} |
} |
||||||
} |
} |
||||||
|
Loading…
Reference in new issue