Esempio n. 1
0
            public static void BuildMarsh(ref MapData map, IEnumerable <ITerrainDef> terrainDefs)
            {
                var random = new GTRandom();

                var LakeBounds = map.GetBoundCells(TerrainType.LAKE);

                foreach (var bound in LakeBounds.Where(x => x.terrainType == TerrainType.PLAIN))
                {
                    if (random.Next(0, 3) < 2)
                    {
                        map.SetCell(new Cell(bound.axialCoord, terrainDefs.RandomOne()));
                    }
                }

                var riverCells = map.cells.Where(x => x.terrainType == TerrainType.PLAIN && x.HasComponent(TerrainCMPType.RIVER)).ToArray();

                foreach (var cell in riverCells)
                {
                    if (random.Next(0, 100) < 95)
                    {
                        ICell newCell = new Cell(cell.axialCoord, terrainDefs.RandomOne());
                        newCell.components.AddRange(cell.components);

                        map.SetCell(newCell);

                        foreach (var near in newCell.GetNearTerrain(TerrainType.PLAIN, 1, map))
                        {
                            if (random.Next(0, 2) < 1)
                            {
                                map.SetCell(new Cell(near.axialCoord, terrainDefs.RandomOne()));
                            }
                        }
                    }
                }
            }
Esempio n. 2
0
            public static int BuildMount(ref MapData map, double percent, IEnumerable <ITerrainDef> terrainDefs)
            {
                var random = new GTRandom();

                var cellcount = map.cells.Count();
                var total     = (int)(percent * map.cells.Count());

                if (total < 5)
                {
                    total = 5;
                }

                var seedCount = total / 5;

                seedCount = seedCount > 5 ? 5 : seedCount;

                IEnumerable <ICell> seeds = map.cells.Where(x => !x.HasComponent(TerrainCMPType.RIVER)).RandomFetch(seedCount);

                foreach (var seed in seeds)
                {
                    map.SetCell(new Cell(seed.axialCoord, terrainDefs.RandomOne()));
                }

                int curr = seeds.Count();

                while (true)
                {
                    IEnumerable <ICell> bounds = map.GetBoundCells(TerrainType.MOUNT).Where(x => !x.HasComponent(TerrainCMPType.RIVER));
                    if (bounds.Count() == 0)
                    {
                        throw new Exception("bound is null");
                    }

                    bounds = bounds.OrderBy(_ => random.Next(0, 100));

                    foreach (var bound in bounds)
                    {
                        if (random.Next(0, 10) == 0)
                        {
                            map.SetCell(new Cell(bound.axialCoord, terrainDefs.RandomOne()));
                            curr++;
                        }

                        if (curr == total)
                        {
                            return(curr);
                        }
                    }
                }
            }
Esempio n. 3
0
        public void BuildMountTest()
        {
            var random = new GTRandom();

            var map = new MapData(20);

            var terrainType = Mock.Of <ITerrainDef>(x => x.type == TerrainType.PLAIN && x.code == "plain");

            foreach (var key in map.Keys.ToArray())
            {
                var cell = new Cell(new AxialCoord(key.q, key.r), terrainType);
                if (random.Next(0, 100) < 5)
                {
                    cell.components.Add(new TerrainComponent(TerrainCMPType.RIVER));
                }

                map.SetCell(cell);
            }

            var percent = 0.1;

            MapData.Buider.BuildMount(ref map, percent, new ITerrainDef[] { Mock.Of <ITerrainDef>(x => x.type == TerrainType.MOUNT && x.code == "mount") });

            map.cells.Where(x => x.terrainType == TerrainType.MOUNT).Count().Should().Be((int)(percent * map.cells.Count()));
        }
Esempio n. 4
0
            private static void SetRiver(ref MapData map, ICell cell, int direct)
            {
                var random = new GTRandom();

                cell.components.Add(new TerrainComponent(TerrainCMPType.RIVER));

                var currMap = map;

                if (cell.axialCoord.Length() >= currMap.maxDist)
                {
                    return;
                }

                var nextCells = cell.axialCoord.GetNeighbors()
                                .Select(x => currMap.GetCell(x));

                nextCells = nextCells.Where(x => !x.HasComponent(TerrainCMPType.RIVER))
                            .Where(x => x.axialCoord.GetNeighbors()
                                   .Where(n => currMap.HasCell(n))
                                   .Select(y => currMap.GetCell(y))
                                   .Count(z => z.HasComponent(TerrainCMPType.RIVER)) < 2);
                if (nextCells.Count() == 0)
                {
                    return;
                }

                var norths = nextCells.Where(x => x.axialCoord.Sub(cell.axialCoord).Equals(AxialCoord.directions[1]) ||
                                             x.axialCoord.Sub(cell.axialCoord).Equals(AxialCoord.directions[2]) ||
                                             x.axialCoord.Sub(cell.axialCoord).Equals(AxialCoord.directions[3]));

                var souths = nextCells.Where(x => x.axialCoord.Sub(cell.axialCoord).Equals(AxialCoord.directions[0]) ||
                                             x.axialCoord.Sub(cell.axialCoord).Equals(AxialCoord.directions[4]) ||
                                             x.axialCoord.Sub(cell.axialCoord).Equals(AxialCoord.directions[5]));

                var firstCells = souths;
                var second     = norths;

                if (direct == -1)
                {
                    firstCells = norths;
                    second     = souths;
                }

                if (second.Count() > 0)
                {
                    if (random.Next(0, 100) < 10 || firstCells.Count() == 0)
                    {
                        SetRiver(ref map, second.RandomOne(), direct);
                        return;
                    }
                }

                SetRiver(ref map, firstCells.RandomOne(), direct);
            }
Esempio n. 5
0
            public static int BuildLake(ref MapData map, double percent, IEnumerable <ITerrainDef> terrainDefs)
            {
                var random = new GTRandom();

                var cellcount = map.cells.Count();
                var total     = (int)(percent * map.cells.Count());

                var seedCount = total / 5;

                seedCount = seedCount > 5 ? 5 : seedCount;

                var seeds = map.cells.Where(x => x.HasComponent(TerrainCMPType.RIVER)).RandomFetch(seedCount);

                var lakeCells = new List <ICell>();

                foreach (var seed in seeds)
                {
                    var lakeCell = new Cell(seed.axialCoord, terrainDefs.RandomOne());
                    lakeCell.components.AddRange(seed.components);
                    lakeCells.Add(lakeCell);

                    map.SetCell(lakeCell);
                }

                while (true)
                {
                    var currMap = map;

                    var nears = lakeCells.SelectMany(x => x.GetNearTerrain(TerrainType.PLAIN, 1, currMap)).Distinct().ToArray();
                    foreach (var near in nears)
                    {
                        if (random.Next(0, 3) == 0)
                        {
                            var lakeCell = new Cell(near.axialCoord, terrainDefs.RandomOne());
                            lakeCell.components.AddRange(near.components);
                            map.SetCell(lakeCell);

                            lakeCells.Add(lakeCell);
                            if (lakeCells.Count() == total)
                            {
                                return(lakeCells.Count());
                            }
                        }
                    }
                }
            }
Esempio n. 6
0
            //private static void BuildForest(ref MapData map, double v)
            //{
            //    //TODO BuildForest
            //}

            public static int BuildHill(ref MapData map, double percent, IEnumerable <ITerrainDef> terrainDefs)
            {
                var random = new GTRandom();

                var cellcount = map.cells.Count();
                int total     = (int)(percent * cellcount);

                if (total < 20)
                {
                    total = 20;
                }

                var seedCount = total / 20;

                seedCount = seedCount > 10 ? 10 : seedCount;

                var seeds = map.cells.Where(x => x.terrainType == TerrainType.PLAIN && !x.HasComponent(TerrainCMPType.RIVER)).RandomFetch(seedCount).ToArray();

                var hillCells = new List <ICell>();

                foreach (var seed in seeds)
                {
                    var newCell = new Cell(seed.axialCoord, terrainDefs.RandomOne());
                    map.SetCell(newCell);

                    hillCells.Add(newCell);
                }

                var currMap    = map;
                var mountNears = map.cells.Where(x => x.terrainType == TerrainType.MOUNT)
                                 .SelectMany(x => x.GetNearTerrain(TerrainType.PLAIN, 2, currMap))
                                 .Where(x => !x.HasComponent(TerrainCMPType.RIVER))
                                 .Distinct().ToArray();

                foreach (var mountNear in mountNears)
                {
                    var newCell = new Cell(mountNear.axialCoord, terrainDefs.RandomOne());
                    map.SetCell(newCell);

                    hillCells.Add(newCell);
                }

                while (true)
                {
                    var nears = hillCells.SelectMany(x => x.GetNearTerrain(TerrainType.PLAIN, 2, currMap)).Distinct().Where(x => !x.HasComponent(TerrainCMPType.RIVER)).ToArray();
                    foreach (var near in nears)
                    {
                        if (random.Next(0, 100) == 0)
                        {
                            var lakeCell = new Cell(near.axialCoord, terrainDefs.RandomOne());
                            lakeCell.components.AddRange(near.components);
                            map.SetCell(lakeCell);

                            hillCells.Add(lakeCell);
                            if (hillCells.Count() == total)
                            {
                                return(hillCells.Count());
                            }
                        }
                    }
                }
            }