Switch to non-recursive fill algorithm - handle huge regions better
This commit is contained in:
		@@ -1,5 +1,6 @@
 | 
			
		||||
package org.dynmap.factions;
 | 
			
		||||
 | 
			
		||||
import java.util.ArrayDeque;
 | 
			
		||||
import java.util.ArrayList;
 | 
			
		||||
import java.util.Collection;
 | 
			
		||||
import java.util.HashMap;
 | 
			
		||||
@@ -181,14 +182,26 @@ public class DynmapFactionsPlugin extends JavaPlugin {
 | 
			
		||||
     */
 | 
			
		||||
    private int floodFillTarget(TileFlags src, TileFlags dest, int x, int y) {
 | 
			
		||||
        int cnt = 0;
 | 
			
		||||
        if(src.getFlag(x, y)) { /* Set in src */
 | 
			
		||||
            src.setFlag(x, y, false);   /* Clear source */
 | 
			
		||||
            dest.setFlag(x, y, true);   /* Set in destination */
 | 
			
		||||
            cnt++;
 | 
			
		||||
            cnt += floodFillTarget(src, dest, x+1, y); /* Fill adjacent blocks */
 | 
			
		||||
            cnt += floodFillTarget(src, dest, x-1, y);
 | 
			
		||||
            cnt += floodFillTarget(src, dest, x, y+1);
 | 
			
		||||
            cnt += floodFillTarget(src, dest, x, y-1);
 | 
			
		||||
        ArrayDeque<int[]> stack = new ArrayDeque<int[]>();
 | 
			
		||||
        stack.push(new int[] { x, y });
 | 
			
		||||
        
 | 
			
		||||
        while(stack.isEmpty() == false) {
 | 
			
		||||
            int[] nxt = stack.pop();
 | 
			
		||||
            x = nxt[0];
 | 
			
		||||
            y = nxt[1];
 | 
			
		||||
            if(src.getFlag(x, y)) { /* Set in src */
 | 
			
		||||
                src.setFlag(x, y, false);   /* Clear source */
 | 
			
		||||
                dest.setFlag(x, y, true);   /* Set in destination */
 | 
			
		||||
                cnt++;
 | 
			
		||||
                if(src.getFlag(x+1, y))
 | 
			
		||||
                    stack.push(new int[] { x+1, y });
 | 
			
		||||
                if(src.getFlag(x-1, y))
 | 
			
		||||
                    stack.push(new int[] { x-1, y });
 | 
			
		||||
                if(src.getFlag(x, y+1))
 | 
			
		||||
                    stack.push(new int[] { x, y+1 });
 | 
			
		||||
                if(src.getFlag(x, y-1))
 | 
			
		||||
                    stack.push(new int[] { x, y-1 });
 | 
			
		||||
            }
 | 
			
		||||
        }
 | 
			
		||||
        return cnt;
 | 
			
		||||
    }
 | 
			
		||||
 
 | 
			
		||||
@@ -12,7 +12,7 @@ import java.util.HashMap;
 | 
			
		||||
 */
 | 
			
		||||
public class TileFlags {
 | 
			
		||||
	private HashMap<Long, long[]> chunkmap = new HashMap<Long, long[]>(); 
 | 
			
		||||
	private long last_key = Long.MIN_VALUE;
 | 
			
		||||
	private long last_key = Long.MAX_VALUE;
 | 
			
		||||
	private long[] last_row;
 | 
			
		||||
	
 | 
			
		||||
	public TileFlags() {
 | 
			
		||||
@@ -62,6 +62,6 @@ public class TileFlags {
 | 
			
		||||
	public void clear() {
 | 
			
		||||
		chunkmap.clear();
 | 
			
		||||
		last_row = null;
 | 
			
		||||
		last_key = Long.MIN_VALUE;
 | 
			
		||||
		last_key = Long.MAX_VALUE;
 | 
			
		||||
	}
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user