예제 #1
0
 public static void tableWorker(int StartingIndex, int EndingIndex, double[][] BitMap, int[] permutationTable, Vector2[] gradients)
 {
     for (int i = StartingIndex; i < EndingIndex; i++)
     {
         BitMap[i] = new double[BitMap.GetLength(0)];
         for (int k = 0; k < BitMap.GetLength(0); k++)
         {
             double noise = Math.Max(Math.Min(Noise2d.Noise((double)i / 512, (double)k / 512, permutationTable, gradients), (double)1), (double)-1);
             BitMap[i][k] = (noise + 1) * 127.5;
         }
     }
 }
        public void Init(int X, int Y)
        {
            ChunkX = X;
            ChunkY = Y;

            var MapData = Noise2d.GenerateNoiseMap(ChunkX * ChunkManager.Instance.ChunkWidth, ChunkY * ChunkManager.Instance.ChunkHeight,
                                                   ChunkManager.Instance.ChunkWidth, ChunkManager.Instance.ChunkHeight, 8, 256);
            var BaseTileData       = new int[ChunkManager.Instance.ChunkWidth, ChunkManager.Instance.ChunkHeight];
            var DecorationTileData = new int[ChunkManager.Instance.ChunkWidth, ChunkManager.Instance.ChunkHeight];

            ChunkTileData = new TileData[ChunkManager.Instance.ChunkWidth, ChunkManager.Instance.ChunkHeight];

            for (var i = 0; i < ChunkManager.Instance.ChunkWidth; i++)
            {
                for (var j = 0; j < ChunkManager.Instance.ChunkHeight; j++)
                {
                    BaseTileData[i, j]
                        = TerrainGen.TileForHeight((ChunkX * ChunkManager.Instance.ChunkWidth) + i, (ChunkY * ChunkManager.Instance.ChunkHeight) + j,
                                                   MapData[i, j]);


                    DecorationTileData[i, j] = -1;

                    //527
                    var tree = TerrainGen.WantTree(
                        (ChunkX * ChunkManager.Instance.ChunkWidth) + i, (ChunkY * ChunkManager.Instance.ChunkHeight) + j, MapData[i, j]);


                    TileData tData = new TileData();
                    tData.ChunkTileOffsetX = i;
                    tData.ChunkTileOffsetY = j;
                    tData.TileBaseType     = BaseTileData[i, j];
                    tData.TileDetailType   = DecorationTileData[i, j];
                    tData.TileX            = (ChunkX * ChunkManager.Instance.ChunkWidth) + i;
                    tData.TileY            = (ChunkY * ChunkManager.Instance.ChunkHeight) + j;
                    tData.TileEntity       = new Entity();

                    if (tree)
                    {
                        tData.TileEntity.addComponent <TIleCollider>();

                        tData.TileEntity.addComponent(new ResourceTile("wood", 527, tData));
                    }

                    tData.UpdateWalkableState();
                    ChunkTileData[i, j] = tData;
                }
            }
        }
예제 #3
0
        /// <summary>
        /// Generates the noise map.
        /// From https://stackoverflow.com/questions/8659351/2d-perlin-noise
        /// </summary>
        /// <param name="width">The width.</param>
        /// <param name="height">The height.</param>
        /// <param name="result">The result.</param>
        /// <param name="octaves">The octaves.</param>
        public static void GenerateNoiseMap(int width, int height, int octaves, out DoubleOrSingle[] result)
        {
            var data = new DoubleOrSingle[width * height];

            // track min and max noise value. Used to normalize the result to the 0 to 1.0 range.
            var min = DoubleOrSingle.MaxValue;
            var max = DoubleOrSingle.MinValue;

            // rebuild the permutation table to get a different noise pattern.
            // Leave this out if you want to play with changing the number of octaves while
            // maintaining the same overall pattern.
            Noise2d.Reseed();

            var frequency = 0.5f;
            var amplitude = 1f;

            //var persistence = 0.25f;

            for (var octave = 0; octave < octaves; octave++)
            {
                // parallel loop - easy and fast.
                Parallel.For(0
                             , width * height
                             , (offset) =>
                {
                    var i     = offset % width;
                    var j     = offset / width;
                    var noise = Noise2d.Noise(i * frequency * 1f / width, j * frequency * 1f / height);
                    noise     = data[j * width + i] += noise * amplitude;

                    min = Math.Min(min, noise);
                    max = Math.Max(max, noise);
                }
                             );

                frequency *= 2;
                amplitude /= 2;
            }
            //Normalize
            for (int i = 0; i < data.Length; ++i)
            {
                data[i] = (data[i] - min) / (max - min);
            }
            result = data;
        }
예제 #4
0
        public void ConstructTileMap()
        {
            var noiseMap = Noise2d.GenerateNoiseMap(13, 13, 2);
            var walker   = 0;

            var sqr3 = Mathf.Sqrt(3);

            foreach (var t in _holder.TileMap)
            {
                var c   = t.Coord;
                var pos = new Vector3(sqr3 * c.Q + sqr3 * c.R / 2, 0, 1.5f * c.R) -
                          new Vector3(_holder.TileMap.Radius * 1.5f * sqr3, 0, _holder.TileMap.Radius * 1.5f);
                var tile = Instantiate(hexTilePrefab, transform);
                tile.Init(t, OnClickTile);
                var a = noiseMap[walker / 13, walker % 13];
                tile.GetComponent <SpriteRenderer>().color = new Color(a, a, a);
                tile.transform.localPosition = pos;
                walker++;
            }
        }
예제 #5
0
        public void AddObstacles()
        {
            int width  = MapConstants.mapWidth - 2;
            int height = MapConstants.mapHeight - 2;
            var data   = new float[width * height];
            var min    = float.MaxValue;
            var max    = float.MinValue;

            Noise2d.Reseed();

            var frequency = 0.1f;
            var amplitude = 1f;

            for (int i = 0; i < data.Length; i++)
            {
                var x     = i % width;
                var y     = i / width;
                var noise = Noise2d.Noise(x * frequency * 1f / width, y * frequency * 1f / height);
                noise = data[y * width + x] += noise * amplitude;

                min = Math.Min(min, noise);
                max = Math.Max(max, noise);
            }

            for (int i = 1; i < MapConstants.mapHeight - 1; i++)
            {
                for (int j = 1; j < MapConstants.mapWidth - 1; j++)
                {
                    var normalizedNoise = (data[(i - 1) * (j - 1)] - min) / (max - min);
                    var obstacle        = GetObstacleByNoise(normalizedNoise);
                    if (obstacle != null)
                    {
                        map[i, j] = obstacle;
                    }
                }
            }
        }
예제 #6
0
        public Hills(int seed, int width, int height, float frequency, float amplitude) : base(seed, width, height)
        {
            Noise2d noise = new Noise2d(seed);

            noiseMap = noise.GenerateNoiseMap(width, height, frequency, amplitude);
        }
예제 #7
0
        public FogOfWar(int seed, int width, int height, GraphicsDeviceManager graphics)
        {
            Noise = new Noise2d(seed);

            Width  = width;
            Height = height;

            Vertecies = new VertexPositionColor[width * height * 6];
            var NoiseMap = Noise.GenerateNoiseMap(width, height, 100f, 1f);

            int    i      = 0;
            Random random = new Random(123);

            for (int y = 0; y < height; y++)
            {
                for (int x = 0; x < width; x++)
                {
                    float value1 = NoiseMap[y * width + x];
                    float value2 = NoiseMap[Math.Min(y * width + x + 1, width * height - 1)];
                    float value3 = NoiseMap[Math.Min((y + 1) * width + x, width * height - 1)];
                    float value4 = NoiseMap[Math.Min((y + 1) * width + x + 1, width * height - 1)];

                    int al1 = 255; //random.NextDouble() > 0.2 ? 0 : 255;
                    int al2 = 255; //random.NextDouble() > 0.2 ? 0 : 255;


                    Color color1 = Color.FromNonPremultiplied(40, 40, (int)(value1 * 255), al1);
                    Color color2 = Color.FromNonPremultiplied(40, 40, (int)(value2 * 255), al1);
                    Color color3 = Color.FromNonPremultiplied(40, 40, (int)(value3 * 255), al1);
                    Color color4 = Color.FromNonPremultiplied(40, 40, (int)(value4 * 255), al1);

                    Color color1x = Color.FromNonPremultiplied(40, 40, (int)(value1 * 255), al2);
                    Color color2x = Color.FromNonPremultiplied(40, 40, (int)(value2 * 255), al2);
                    Color color3x = Color.FromNonPremultiplied(40, 40, (int)(value3 * 255), al2);
                    Color color4x = Color.FromNonPremultiplied(40, 40, (int)(value4 * 255), al2);

                    int cx = x * TILE_WIDTH;
                    int cy = y * TILE_DEPTH;

                    int   nx    = cx + TILE_WIDTH;
                    int   ny    = cy + TILE_DEPTH;
                    Color color = Color.Blue;//value > 10 ? value > 20 ? Color.SandyBrown: Color.Green : Color.Blue;

                    int a = 10;
                    Vertecies[i]     = new VertexPositionColor(new Vector3(cx, cy, value1 * a), color1);
                    Vertecies[i + 1] = new VertexPositionColor(new Vector3(cx, ny, value3 * a), color3);
                    Vertecies[i + 2] = new VertexPositionColor(new Vector3(nx, cy, value2 * a), color2);

                    Vertecies[i + 3] = new VertexPositionColor(Vertecies[i + 1].Position, color3x);
                    Vertecies[i + 4] = new VertexPositionColor(new Vector3(nx, ny, value4 * a), color4x);
                    Vertecies[i + 5] = new VertexPositionColor(Vertecies[i + 2].Position, color2x);

                    i += 6;
                }
            }

            //using (var reader = new BinaryReader(File.Open("Content/Shaders/FogOfWar.xnb", FileMode.Open)))
            //{
            //    effect = new Effect(graphics.GraphicsDevice, reader.ReadBytes((int)reader.BaseStream.Length));
            //}

            //effect = new BasicEffect(graphics.GraphicsDevice);
        }