示例#1
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);
    }