Ejemplo n.º 1
0
    //Generates all the dirt in the map
    //A dirt block will only be generated at a specific position iff
    //  - Its y-coordinate is at or below the max dirt height for that x-coordinate
    //  - There is not a cave located at its position
    private void GenerateDirt()
    {
        //Variables used to calculate the average height of the top level of dirt
        topLevelSum  = 0.0;
        numTopLevels = 0;
        avgTopLevel  = 0.0;

        //The max height of every column of blocks
        maxHeights = new int[BIOME_WIDTH];
        for (int i = 0; i < BIOME_WIDTH; i++)
        {
            maxHeights[i] = -1;
        }

        //Variables used to calculate the average height of the top level of dirt
        topLevelSum  = 0.0;
        numTopLevels = 0;
        avgTopLevel  = 0.0;
        //Generates dirt
        for (int x = 0; x < BIOME_WIDTH; x++, xHeight += xHeightFreq, xCave += xCaveFreq)
        {
            //The top level that the dirt will go to
            double topLevel = PerlinNoise1D.getNoise(xHeight) * BIOME_HEIGHT * topLevelScale;
            if (topLevel > (double)BIOME_HEIGHT - 1)
            {
                topLevel = BIOME_HEIGHT - 1;
            }

            topLevelSum += topLevel;
            ++numTopLevels;
            avgTopLevel = topLevelSum / numTopLevels;
            yCave       = 0.0;
            double thresh = 1.0;
            for (int y = (int)topLevel; y >= 0; y--, yCave += yCaveFreq)
            {
                double n = ((SimplexNoise2D.octaveNoise2D(5, 0.1, 0.2, xCave, yCave) + 1.0) / 2.0 * 5.0);

                //Builds a dirt block if it is below the max height generated by the height generator and if there isnt a cave there
                if ((int)n > 1 || y > topLevel * topLevelScale * 1.2)
                {
                    if (maxHeights[x] == -1)
                    {
                        maxHeights[x] = y;
                    }
                    map[x][y].SetBT(BlockType.DIRT);
                }
                if (n >= thresh)
                {
                    backMap[x][y] = new PrimitiveBlock(x, y, BlockType.DIRT);
                }
                else
                {
                    if (rand.Next(15) == 0)
                    {
                        thresh = rand.NextDouble() + 1;
                    }
                }
            }
        }
    }
 public GlobalGenerator(uint seed)
 {
     _random     = new FastRandom(seed);
     _sysRand    = new System.Random((int)seed);
     _heightMap0 = new SimplexNoise2D(_random.NextUInt());
 }