private static Bitmap RenderColorBmp(TerrainTile[,] tiles)
 {
     int w = tiles.GetLength(0);
     int h = tiles.GetLength(1);
     Bitmap bmp = new Bitmap(w, h);
     BitmapBuffer buff = new BitmapBuffer(bmp);
     buff.Lock();
     for (int y = 0; y < w; y++)
         for (int x = 0; x < h; x++)
             buff[x, y] = TileTypes.color[tiles[x, y].TileId];
     buff.Unlock();
     return bmp;
 }
 public override void Apply(TerrainTile[,] tiles)
 {
     for (int row = 0; row < tiles.GetLength(0); row++) {
         for (int col = 0; col < tiles.GetLength(1); col++) {
             var tile = tiles[row, col];
             if (!tile.WasTerrainApplied) {
                 var neighbor = pickNeighbor(tiles, row, col);
                 var mat = neighbor.renderer.sharedMaterial;
                 var cost = neighbor.MoveCost;
                 tile.SetTerrain(cost, mat);
             }
         }
       }
 }
 public override void Apply(TerrainTile[,] tiles)
 {
     _tiles = tiles;
     _numRows = tiles.GetLength(0);
     _numCols = tiles.GetLength(1);
     int minRow = Mathf.Max(0, Row - Range);
     int maxRow = Mathf.Min(_numRows, Row + Range);
     int minCol = Mathf.Max(0, Col - Range);
     int maxCol = Mathf.Min(_numCols, Col + Range);
     int nSplotches = (int)(Density * Range * Range);
     for (int i = 0; i < nSplotches; i++) {
         int row = Random.Range(minRow, maxRow);
         int col = Random.Range(minCol, maxCol);
         PlaceBuilding(row, col, BuildingSize, WallPrefab, Direction.North);
     }
 }
 public override void Apply(TerrainTile[,] tiles)
 {
     _tiles = tiles;
     _numRows = tiles.GetLength(0);
     _numCols = tiles.GetLength(1);
     int minRow = Mathf.Max(0, Row - Range);
     int maxRow = Mathf.Min(_numRows, Row + Range);
     int minCol = Mathf.Max(0, Col - Range);
     int maxCol = Mathf.Min(_numRows, Col + Range);
     int nSplotches = (int)(Density * Range * Range);
     for (int i = 0; i < nSplotches; i++) {
         int row = Random.Range(minRow, maxRow);
         int col = Random.Range(minCol, maxCol);
         ApplyMaterialSplotch(row, col, 5);
     }
 }
        public static byte[] Export(TerrainTile[,] tiles)
        {
            List<TerrainTile> dict = new List<TerrainTile>();

            int w = tiles.GetLength(0);
            int h = tiles.GetLength(1);
            byte[] dat = new byte[w * h * 3];
            int idx = 0;
            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    TerrainTile tile = tiles[x, y];
                    short i = (short)dict.IndexOf(tile);
                    if (i == -1)
                    {
                        i = (short)dict.Count;
                        dict.Add(tile);
                    }
                    dat[idx] = (byte)(i & 0xff);
                    dat[idx + 1] = (byte)(i >> 8);
                    dat[idx + 2] = tile.Elevation;
                    idx += 3;
                }

            MemoryStream ms = new MemoryStream();
            using (BinaryWriter wtr = new BinaryWriter(ms))
            {
                wtr.Write((ushort)dict.Count);
                foreach (TerrainTile i in dict)
                {
                    wtr.Write(i.TileId);
                    wtr.Write(i.TileObj ?? "");
                    wtr.Write(i.Name ?? "");
                    wtr.Write((byte)i.Terrain);
                    wtr.Write((byte)i.Region);
                    //wtr.Write((byte)i.Elevation);
                }
                wtr.Write(w);
                wtr.Write(h);
                wtr.Write(dat);
            }
            byte[] buff = ZlibStream.CompressBuffer(ms.ToArray());
            byte[] ret = new byte[buff.Length + 1];
            Buffer.BlockCopy(buff, 0, ret, 1, buff.Length);
            ret[0] = 2;
            return ret;
        }
        public static byte[] Export(TerrainTile[,] tiles)
        {
            List<TerrainTile> dict = new List<TerrainTile>();

            int w = tiles.GetLength(0);
            int h = tiles.GetLength(1);
            byte[] dat = new byte[w * h * 2];
            int idx = 0;
            for (int y = 0; y < h; y++)
                for (int x = 0; x < w; x++)
                {
                    TerrainTile tile = tiles[x, y];
                    short i = (short)dict.IndexOf(tile);
                    if (i == -1)
                    {
                        i = (short)dict.Count;
                        dict.Add(tile);
                    }
                    dat[idx] = (byte)(i & 0xff);
                    dat[idx + 1] = (byte)(i >> 8);
                    idx += 2;
                }

            MemoryStream ms = new MemoryStream();
            using (BinaryWriter wtr = new BinaryWriter(ms))
            {
                wtr.Write((short)dict.Count);
                foreach (var i in dict)
                {
                    wtr.Write(i.TileId);
                    wtr.Write(i.TileObj ?? "");
                    wtr.Write(i.Name ?? "");
                    wtr.Write((byte)i.Terrain);
                    wtr.Write((byte)i.Region);
                }
                wtr.Write(w);
                wtr.Write(h);
                wtr.Write(dat);
            }
            return ZlibStream.CompressBuffer(ms.ToArray());
        }
Exemple #7
0
        void AddNoiseAndBiome(TerrainTile[,] buff, Dictionary<MapPolygon, double> moist)
        {
            int w = buff.GetLength(0);
            int h = buff.GetLength(1);
            var elevationNoise = new Noise(rand.Next());
            var moistureNoise = new Noise(rand.Next());
            //var elevationNoise = PerlinNoise.GetPerlinNoise(rand.Next(), 256, 256, 2);
            //var moistureNoise = PerlinNoise.GetPerlinNoise(rand.Next(), 256, 256, 2);
            for (int y = 0; y < w; y++)
                for (int x = 0; x < h; x++)
                {
                    var tile = buff[x, y];
                    if (tile.PolygonId != -1)
                    {
                        var poly = map.Polygons[tile.PolygonId];
                        poly = poly.Neighbour[poly.Id % poly.Neighbour.Length];

                        tile.Elevation = Math.Min(1, (float)(poly.DistanceToCoast + poly.DistanceToCoast *
                            elevationNoise.GetNoise(x * 128.0 / w, y * 128.0 / h, 0.3) * 0.01f) * 2);
                        if (tile.Elevation > 1) tile.Elevation = 1;
                        else if (tile.Elevation < 0) tile.Elevation = 0;

                        tile.Moisture = (float)(moist[poly] + moist[poly] *
                            moistureNoise.GetNoise(x * 128.0 / w, y * 128.0 / h, 0.3) * 0.01f);
                        if (tile.Moisture > 1) tile.Moisture = 1;
                        else if (tile.Moisture < 0) tile.Moisture = 0;
                    }

                    tile.Biome = GetBiome(tile);
                    var biomeGround = GetBiomeGround(tile.Biome);
                    if (biomeGround != 0)
                        tile.TileId = biomeGround;

                    buff[x, y] = tile;
                }
        }
Exemple #8
0
        private void ComputeSpawnTerrains(TerrainTile[,] buff)
        {
            var w = buff.GetLength(0);
            var h = buff.GetLength(1);
            for (var y = 0; y < w; y++)
                for (var x = 0; x < h; x++)
                {
                    var tile = buff[x, y];
                    tile.Terrain = GetBiomeTerrain(tile);

                    buff[x, y] = tile;
                }
        }
 static Bitmap RenderColorBmp(TerrainTile[,] tiles)
 {
     int w = tiles.GetLength(0);
     int h = tiles.GetLength(1);
     Bitmap bmp = new Bitmap(w, h);
     BitmapBuffer buff = new BitmapBuffer(bmp);
     buff.Lock();
     for (int y = 0; y < w; y++)
         for (int x = 0; x < h; x++)
         {
             buff[x, y] = GetColor(tiles[x, y]);
         }
     buff.Unlock();
     return bmp;
 }
        private TerrainTile[,] GetTestTerrain()
        {
            #region TempTerrain
            // temp manual terrain
            var terrainTiles = new TerrainTile[MapSize, MapSize];
            // Fill with grass
            for (int i = 0; i < terrainTiles.GetLength(0); i++)
            {
                for (int j = 0; j < terrainTiles.GetLength(1); j++)
                {
                    terrainTiles[i, j] = new TerrainTile(TerrainTile.TerrainType.Grass);
                }
            }
            // top left castle
            tempBuildCastle(terrainTiles, 2, 2);
            // top right castle
            tempBuildCastle(terrainTiles, 16, 2);
            // center castle
            tempBuildCastle(terrainTiles, 10, 7);
            // bottom center castle
            tempBuildCastle(terrainTiles, 9, 15);
            // bottom right castle
            tempBuildCastle(terrainTiles, 15, 18);

            // top right forest
            for (int i = 0; i < 4; i++)
            {
                terrainTiles[16 + i, 0] = new TerrainTile(TerrainTile.TerrainType.Forest);
            }
            terrainTiles[19, 1] = new TerrainTile(TerrainTile.TerrainType.Forest);
            terrainTiles[19, 2] = new TerrainTile(TerrainTile.TerrainType.Forest);

            // bottom left forest
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    terrainTiles[4 + i, 17 + j] = new TerrainTile(TerrainTile.TerrainType.Forest);
                }
            }
            terrainTiles[6, 18] = new TerrainTile(TerrainTile.TerrainType.Forest);

            // center mountain
            terrainTiles[9, 4] = new TerrainTile(TerrainTile.TerrainType.Mountain);
            terrainTiles[9, 5] = new TerrainTile(TerrainTile.TerrainType.Mountain);
            for (int i = 0; i < 2; i++)
            {
                for (int j = 0; j < 2; j++)
                {
                    terrainTiles[7 + i, 5 + j] = new TerrainTile(TerrainTile.TerrainType.Mountain);
                }
            }
            terrainTiles[6, 7] = new TerrainTile(TerrainTile.TerrainType.Mountain);
            terrainTiles[7, 7] = new TerrainTile(TerrainTile.TerrainType.Mountain);

            // center road
            for (int i = 0; i < 7; i++)
            {
                terrainTiles[9, 8 + i] = new TerrainTile(TerrainTile.TerrainType.Road);
            }

            // Center bridge, overwrites road
            terrainTiles[9, 12] = new TerrainTile(TerrainTile.TerrainType.Bridge);

            // bottom road
            for (int i = 0; i < 2; i++)
            {
                terrainTiles[11 + i, 16] = new TerrainTile(TerrainTile.TerrainType.Road);
            }
            for (int i = 0; i < 4; i++)
            {
                terrainTiles[12 + i, 17] = new TerrainTile(TerrainTile.TerrainType.Road);
            }

            // water
            tempWaterVertLine(terrainTiles, 4, 15, 19);
            tempWaterVertLine(terrainTiles, 6, 14, 18);
            tempWaterVertLine(terrainTiles, 6, 13, 17);
            tempWaterVertLine(terrainTiles, 8, 12, 16);
            tempWaterVertLine(terrainTiles, 8, 12, 15);
            tempWaterVertLine(terrainTiles, 11, 12, 14);
            tempWaterHoriLine(terrainTiles, 10, 13, 12);

            for (int i = 0; i < 6; i++)
            {
                for (int j = 0; j < 3; j++)
                {
                    terrainTiles[8 + i, 0 + j] = new TerrainTile(TerrainTile.TerrainType.Water);
                }
            }

            tempWaterVertLine(terrainTiles, 0, 4, 7);
            tempWaterVertLine(terrainTiles, 0, 6, 6);
            tempWaterVertLine(terrainTiles, 0, 7, 5);
            tempWaterHoriLine(terrainTiles, 0, 5, 0);

            tempWaterVertLine(terrainTiles, 5, 13, 4);
            tempWaterVertLine(terrainTiles, 5, 14, 3);
            tempWaterVertLine(terrainTiles, 5, 19, 2);
            tempWaterVertLine(terrainTiles, 5, 19, 1);
            tempWaterVertLine(terrainTiles, 0, 19, 0);

            tempWaterVertLine(terrainTiles, 11, 12, 5);
            tempWaterHoriLine(terrainTiles, 6, 8, 12);
            terrainTiles[2, 5] = new TerrainTile(TerrainTile.TerrainType.Bridge);
            #endregion

            return(terrainTiles);
        }
Exemple #11
0
        //https://code.google.com/p/imagelibrary/source/browse/trunk/Filters/GaussianBlurFilter.cs
        //Blur the elevation

        static void BlurElevation(TerrainTile[,] tiles, double radius)
        {
            int w = tiles.GetLength(0);
            int h = tiles.GetLength(1);

            int shift, source;
            int blurDiam = (int)Math.Pow(radius, 2);
            int gaussWidth = (blurDiam * 2) + 1;

            double[] kernel = CreateKernel(gaussWidth, blurDiam);

            // Calculate the sum of the Gaussian kernel      
            double gaussSum = 0;
            for (int n = 0; n < gaussWidth; n++)
            {
                gaussSum += kernel[n];
            }

            // Scale the Gaussian kernel
            for (int n = 0; n < gaussWidth; n++)
            {
                kernel[n] = kernel[n] / gaussSum;
            }
            //premul = kernel[k] / gaussSum;


            // Create an X & Y pass buffer  
            float[,] gaussPassX = new float[w, h];

            // Do Horizontal Pass  
            for (int y = 0; y < h; y++)
            {
                for (int x = 0; x < w; x++)
                {
                    // Iterate through kernel  
                    for (int k = 0; k < gaussWidth; k++)
                    {
                        // Get pixel-shift (pixel dist between dest and source)  
                        shift = k - blurDiam;

                        // Basic edge clamp  
                        source = x + shift;
                        if (source <= 0 || source >= w) { source = x; }

                        // Combine source and destination pixels with Gaussian Weight  
                        gaussPassX[x, y] = (float)(gaussPassX[x, y] + tiles[source, y].Elevation * kernel[k]);
                    }
                }
            }

            // Do Vertical Pass  
            for (int x = 0; x < w; x++)
            {
                for (int y = 0; y < h; y++)
                {
                    tiles[x, y].Elevation = 0;
                    // Iterate through kernel  
                    for (int k = 0; k < gaussWidth; k++)
                    {
                        // Get pixel-shift (pixel dist between dest and source)   
                        shift = k - blurDiam;

                        // Basic edge clamp  
                        source = y + shift;
                        if (source <= 0 || source >= h) { source = y; }

                        // Combine source and destination pixels with Gaussian Weight  
                        tiles[x, y].Elevation = (float)(tiles[x, y].Elevation + (gaussPassX[x, source]) * kernel[k]);
                    }
                }
            }
        }
Exemple #12
0
        void ComputeSpawnTerrains(TerrainTile[,] buff)
        {
            int w = buff.GetLength(0);
            int h = buff.GetLength(1);
            for (int y = 0; y < w; y++)
                for (int x = 0; x < h; x++)
                {
                    var tile = buff[x, y];
                    tile.Terrain = GetBiomeTerrain(tile);

                    buff[x, y] = tile;
                }
        }
Exemple #13
0
        void Randomize(TerrainTile[,] buff)
        {
            int w = buff.GetLength(0);
            int h = buff.GetLength(1);
            TerrainTile[,] tmp = (TerrainTile[,])buff.Clone();
            for (int y = 10; y < h - 10; y++)
                for (int x = 10; x < w - 10; x++)
                {
                    var tile = buff[x, y];

                    if (tile.TileId == TileTypes.Water && tile.Elevation >= elevationThreshold[3])
                        tile.TileId = TileTypes.SnowRock;
                    else if (tile.TileId != TileTypes.Water && tile.TileId != TileTypes.Road &&
                             tile.TileId != TileTypes.Beach && tile.TileId != TileTypes.MovingWater &&
                             tile.TileId != TileTypes.DeepWater)
                    {
                        var id = tmp[x + rand.Next(-3, 4), y + rand.Next(-3, 4)].TileId;
                        while (id == TileTypes.Water || id == TileTypes.Road ||
                               id == TileTypes.Beach || id == TileTypes.MovingWater ||
                               id == TileTypes.DeepWater)
                            id = tmp[x + rand.Next(-3, 4), y + rand.Next(-3, 4)].TileId;
                        tile.TileId = id;
                    }

                    //if (tile.TileId == TileTypes.Beach)
                    //    tile.Region = TileRegion.Spawn;

                    string biome = tile.Biome;
                    if (tile.TileId == TileTypes.Beach) biome = "beach";
                    else if (tile.TileId == TileTypes.MovingWater) biome = "coast";

                    var biomeObj = Decoration.GetDecor(biome, rand);
                    if (biomeObj != null)
                    {
                        tile.TileObj = biomeObj;
                        var size = Decoration.GetSize(biomeObj, rand);
                        if (size != null)
                            tile.Name = "size:" + size;
                    }

                    float elevation = 0;
                    int c = 0;
                    for (int dy = -1; dy <= 1; dy++)
                        for (int dx = -1; dx <= 1; dx++)
                        {
                            if (x + dx < 0 || x + dx >= w || y + dy < 0 || y + dy >= h) continue;
                            elevation += tmp[x + dx, y + dy].Elevation;
                            c++;
                        }
                    tile.Elevation = elevation / c;

                    buff[x, y] = tile;
                }
        }
Exemple #14
0
 static Bitmap RenderMoistBmp(TerrainTile[,] tiles)
 {
     int w = tiles.GetLength(0);
     int h = tiles.GetLength(1);
     Bitmap bmp = new Bitmap(w, h);
     BitmapBuffer buff = new BitmapBuffer(bmp);
     buff.Lock();
     for (int y = 0; y < w; y++)
         for (int x = 0; x < h; x++)
         {
             uint color = 0x00ffffff;
             color |= (uint)(tiles[x, y].Moisture * 255) << 24;
             buff[x, y] = color;
         }
     buff.Unlock();
     return bmp;
 }
Exemple #15
0
        void Randomize(TerrainTile[,] buff)
        {
            int w = buff.GetLength(0);
            int h = buff.GetLength(1);
            TerrainTile[,] tmp = (TerrainTile[,])buff.Clone();
            for (int y = 10; y < h - 10; y++)
                for (int x = 10; x < w - 10; x++)
                {
                    var tile = buff[x, y];

                    if (tile.TileId == TileTypes.Water && tile.Elevation >= elevationThreshold[3])
                        tile.TileId = TileTypes.SnowRock;
                    else if (tile.TileId != TileTypes.Water && tile.TileId != TileTypes.Road &&
                             tile.TileId != TileTypes.Beach && tile.TileId != TileTypes.MovingWater &&
                             tile.TileId != TileTypes.DeepWater)
                    {
                        var id = buff[x + rand.Next(-1, 2), y + rand.Next(-1, 2)].TileId;
                        while (id == TileTypes.Water || id == TileTypes.Road ||
                               id == TileTypes.Beach || id == TileTypes.MovingWater ||
                               id == TileTypes.DeepWater)
                            id = buff[x + rand.Next(-5, 5), y + rand.Next(-5, 5)].TileId;
                        tile.TileId = id;
                    }

                    //if (tile.TileId == TileTypes.Beach)
                    //    tile.Region = TileRegion.Spawn;

                    string biome = tile.Biome;
                    if (tile.TileId == TileTypes.Beach) biome = "beach";
                    else if (tile.TileId == TileTypes.MovingWater) biome = "coast";

                    var biomeObj = Decoration.GetDecor(biome, rand);
                    if (biomeObj != null)
                    {
                        tile.TileObj = biomeObj;
                        var size = Decoration.GetSize(biomeObj, rand);
                        if (size != null)
                            tile.Name = "size:" + size;
                    }

                    buff[x, y] = tile;
                }
        }
Exemple #16
0
 private static Bitmap RenderEvalBmp(TerrainTile[,] tiles)
 {
     var w = tiles.GetLength(0);
     var h = tiles.GetLength(1);
     var bmp = new Bitmap(w, h);
     var buff = new BitmapBuffer(bmp);
     buff.Lock();
     for (var y = 0; y < w; y++)
         for (var x = 0; x < h; x++)
         {
             uint color = 0x00ffffff;
             color |= (uint) ((byte) (tiles[x, y].Elevation*255) << 24);
             buff[x, y] = color;
         }
     buff.Unlock();
     return bmp;
 }
 private static Bitmap RenderColorBmp(TerrainTile[,] tiles)
 {
     var w = tiles.GetLength(0);
     var h = tiles.GetLength(1);
     var bmp = new Bitmap(w, h);
     var buff = new BitmapBuffer(bmp);
     buff.Lock();
     for (var y = 0; y < w; y++)
         for (var x = 0; x < h; x++)
         {
             buff[x, y] = GetColor(tiles[x, y]);
         }
     buff.Unlock();
     return bmp;
 }