Files
voxel/Worldgen.cs

80 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace Voxel
{
public static class Worldgen
{
private static int baseHeight = 32;
private static float res1 = 2;
private static float res2 = 4;
private static float amplitude1 = 4;
private static float amplitude2 = 2;
private static float elevationMapRes = (float)1/8;
private static float elevationMapAmplitude = 32;
private static Random random = new Random();
private static FastNoiseLite noise = new FastNoiseLite(random.Next());
public static int GetHeight(int x, int z)
{
float map1 = noise.GetNoise(
x * res1,
z * res1
) * amplitude1;
float map2 = noise.GetNoise(x * res2,
z * res2
) * amplitude2;
float elevationMap = (noise.GetNoise(
x * elevationMapRes,
z * elevationMapRes
) + 0.25f) * elevationMapAmplitude;
return baseHeight + (int)(elevationMap + map1 + map2);
}
public static Blocks GetBlock(int y, int maxY)
{
if (y > maxY)
{
return Blocks.Air;
}
if (y == maxY)
{
if (y < baseHeight)
return Blocks.Sand;
else
return Blocks.Grass;
}
if (y < 4)
{
if (y == 0) return Blocks.Bedrock;
float randomValue = random.NextSingle();
if (randomValue < (1f / y))
{
return Blocks.Bedrock;
}
return Blocks.Stone;
}
if (y > maxY - 6)
{
return Blocks.Dirt;
}
return Blocks.Stone;
}
}
}