Пример #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);
    }
Пример #2
0
 public void MergeInto(Region r)
 {
     while (tiles.Count() > 0)
     {
         r.Assign(tiles[0]);
     }
 }
Пример #3
0
 public bool IsBorder()
 {
     if (region == null)
     {
         return(false);
     }
     if (adjacent.FindAll((t) => t.region != this.region).Count() > 0)
     {
         return(true);
     }
     return(adjacent.Count() != 4);
 }
Пример #4
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);
    }
Пример #5
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);
    }