Exemple #1
0
    public static TileSelection SelectContiguous(Tile start, TileSelection tiles)
    {
        var           time   = Time.realtimeSinceStartup;
        TileSelection result = new TileSelection();
        TileSelection open   = new TileSelection();

        open.Add(start);
        var i = 0;

        while (open.Count() > 0)
        {
            i++;
            if (i > tiles.Count())
            {
                throw(new Exception("Fill fail"));
            }
            var t = open[0];
            open.Remove(t);
            result.Add(t);
            foreach (var n in t.adjacent.tiles)
            {
                if (result.DoesNotContain(n) && tiles.Contains(n))
                {
                    open.Add(n);
                }
            }
        }
        _gui_manager.selected_tiles = result;
        return(result);
    }
Exemple #2
0
    public static Region ClosestRegion(TileSelection tiles, Region to_ignore = null, HashSet <Region> whitelist = null)
    {
        var open      = new List <SearchRecord>();
        var openTiles = new TileSelection();
        var closed    = new TileSelection();

        foreach (var t in tiles.tiles)
        {
            var record = new SearchRecord();
            record.t    = t;
            record.cost = 0;
            open.Add(record);
        }
        var i = 0;

        while (open.Count > 0)
        {
            i++;
            if (i > 1000)
            {
                throw(new Exception("Finding closest Region took too long"));
            }
            open.Sort((a, b) => a.cost - b.cost);
            var node = open[0];
            closed.Add(node.t);
            open.RemoveAt(0);
            openTiles.Remove(node.t);
            if (node.t.region != null && node.t.region != to_ignore)
            {
                if (whitelist == null || whitelist.Contains(node.t.region))
                {
                    return(node.t.region);
                }
            }
            foreach (var n in node.t.adjacent.tiles)
            {
                if (closed.DoesNotContain(n) && openTiles.DoesNotContain(n))
                {
                    var record = new SearchRecord();
                    record.t    = n;
                    record.cost = node.cost + 1;
                    open.Add(record);
                    openTiles.Add(record.t);
                }
            }
        }
        _gui_manager.selected_region = null;
        _gui_manager.selected_tiles  = tiles;
        throw new Exception("Could not find any other Regions after " + i + " iterations");
    }
Exemple #3
0
    public static List <TileSelection> SeperateContiguous(TileSelection tiles)
    {
        var                  time   = Time.realtimeSinceStartup;
        TileSelection        src    = new TileSelection();
        List <TileSelection> result = new List <TileSelection>();

        foreach (var t in tiles.tiles)
        {
            src.Add(t);
        }
        while (src.Count() > 0)
        {
            var region = MapGen.SelectContiguous(src[0], src);
            result.Add(region);
            foreach (var t in region.tiles)
            {
                src.Remove(t);
            }
        }
        return(result);
    }
Exemple #4
0
    public void Split(Region r, int fraction = 2)
    {
        var target             = this.tiles.Count() / (float)fraction;
        var i                  = 0;
        var original_fragments = MapGen.SeperateContiguous(this.tiles).Count();
        var open               = new TileSelection();
        var start              = tiles.Sample();

        open.Add(start);
        while (r.tiles.Count() < target && open.Count() > 0)
        {
            i++;
            var t = open.Sample();
            open.Remove(t);

            r.Assign(t);
            if (i % 10 == 1)
            {
                JoinCutOffAreas(r, original_fragments);
            }
            foreach (var n in t.adjacent.tiles)
            {
                if (n.region == this)
                {
                    if (n.land)
                    {
                        open.Add(n);
                    }
                }
            }
        }

        this.JoinCutOffAreas(r, original_fragments);
        var core = r.tiles;
        //core = MapGen.Shrink(core);
        var original_core = this.tiles;
        //original_core = MapGen.Shrink(original_core);

        var to_switch_back = new TileSelection();

        foreach (var t in r.tiles.tiles)
        {
            if (core.DoesNotContain(t))
            {
                to_switch_back.Add(t);
            }
        }
        foreach (var t in this.tiles.tiles)
        {
            if (original_core.DoesNotContain(t))
            {
                to_switch_back.Add(t);
            }
        }

        Dictionary <Tile, Region> switchMap = new Dictionary <Tile, Region>();
        var allowed_regions = new HashSet <Region>();

        allowed_regions.Add(r);
        allowed_regions.Add(this);
        foreach (var t in to_switch_back.tiles)
        {
            t.region = null;
            r.tiles.Remove(t);
            TileSelection t_as_list = new TileSelection();
            t_as_list.Add(t);
            try{
                switchMap.Add(t, MapGen.ClosestRegion(t_as_list, null, allowed_regions));
            }catch (Exception e) {
                Debug.LogError(e); // Swallow
            }
        }
        foreach (KeyValuePair <Tile, Region> entry in switchMap)
        {
            entry.Value.Assign(entry.Key);
        }
        r.JoinCutOffAreas(this, 1);
        this.JoinCutOffAreas(r, original_fragments);
    }