private byte GetHeight(int x, int y) { x += 30; y -= 30; //double p = 0.2 + ((findnoise2(x / 100.0, y / 100.0) + 1.0) / 2) * 0.3; double p = 0.5; double zoom = 150; double getnoise = 0; int octaves = 6; for (int a = 0; a < octaves - 1; a++) //This loops trough the octaves. { double frequency = Math.Pow(2, a); //This increases the frequency with every loop of the octave. double amplitude = Math.Pow(p, a); //This decreases the amplitude with every loop of the octave. getnoise += NoiseTools.Noise(((double)x) * frequency / zoom, ((double)y) / zoom * frequency, this.Seed) * amplitude; //This uses our perlin noise functions. It calculates all our zoom and frequency and amplitude } double maxheight = 64; int height = (int)(((getnoise + 1) / 2.0) * (maxheight - 5)) + 3;//(int)((getnoise * 128.0) + 128.0); if (height > maxheight - 1) { height = (int)maxheight - 1; } if (height < 2) { height = 2; } return((byte)height); }
public void Initialize() { mSeaLevelAlignedWithInt = (WaterLevel / MaxHeight); mBeachLevelAlignedWithInt = (WaterLevel + 1) / MaxHeight; if (Steps != null) { for (int i = 0; i < Steps.Length; i++) { if (!string.IsNullOrEmpty(Steps[i].NoiseTexturePath)) { bool repeated = false; for (int j = 0; j < i - 1; j++) { if (Steps[i].NoiseTexturePath == Steps[j].NoiseTexturePath) { Steps[i].NoiseValues = Steps[j].NoiseValues; Steps[i].NoiseTextureSize = Steps[j].NoiseTextureSize; repeated = true; break; } } if (!repeated && (Steps[i].NoiseTextureSize == 0 || Steps[i].NoiseValues == null || Steps[i].LastTextureLoaded == null || Steps[i].NoiseTexturePath != Steps[i].LastTextureLoaded)) { Steps[i].LastTextureLoaded = Steps[i].NoiseTexturePath; Steps[i].NoiseValues = NoiseTools.LoadNoiseTexture(Steps[i].NoiseTexturePath, out int NoiseTextureSize); Steps[i].NoiseTextureSize = NoiseTextureSize; } } if (Steps[i].InputIndex0 < 0 || Steps[i].InputIndex0 >= Steps.Length) { Steps[i].InputIndex0 = 0; } if (Steps[i].InputIndex1 < 0 || Steps[i].InputIndex1 >= Steps.Length) { Steps[i].InputIndex1 = 0; } } } if (!string.IsNullOrEmpty(MoisturePath) && (mNoiseMoistureTextureSize == 0 || mMoistureValues == null || mLastMoistureTextureLoaded == null || mLastMoistureTextureLoaded != MoisturePath)) { mLastMoistureTextureLoaded = MoisturePath; mMoistureValues = NoiseTools.LoadNoiseTexture(MoisturePath, out int noiseMoistureTextureSize); mNoiseMoistureTextureSize = noiseMoistureTextureSize; } if (mHeightChunkData == null) { mHeightChunkData = new HeightMapInfo[TerrainEnvironment.CHUNK_SIZE * TerrainEnvironment.CHUNK_SIZE]; } }
/// <summary> /// Returns the contents of the chunk by its coordinates. /// </summary> /// <param name="x"></param> /// <param name="y"></param> /// <param name="z"></param> /// <returns></returns> public override byte[, ,] GetChunk(int x, int y, int z) { this.waterlevel = WaterLevel; heightcache = new byte[this.ChunkSize, this.ChunkSize]; x = x * this.ChunkSize; y = y * this.ChunkSize; z = z * this.ChunkSize; byte[, ,] chunk = new byte[this.ChunkSize, this.ChunkSize, this.ChunkSize]; for (int xx = 0; xx < this.ChunkSize; xx++) { for (int yy = 0; yy < this.ChunkSize; yy++) { heightcache[xx, yy] = GetHeight(x + xx, y + yy); } } interpolatednoise = NoiseTools.InterpolateNoise3d(x, y, z, this.ChunkSize); // chance of get hay fields bool IsHay = _rnd.NextDouble() < 0.005 ? false : true; for (int xx = 0; xx < this.ChunkSize; xx++) { for (int yy = 0; yy < this.ChunkSize; yy++) { for (int zz = 0; zz < this.ChunkSize; zz++) { chunk[xx, yy, zz] = IsHay ? (byte)GetBlock(x + xx, y + yy, z + zz, heightcache[xx, yy], 0, xx, yy, zz) : (byte)GetBlock(x + xx, y + yy, z + zz, heightcache[xx, yy], 1, xx, yy, zz); } } } for (int xx = 0; xx < this.ChunkSize; xx++) { for (int yy = 0; yy < this.ChunkSize; yy++) {/* * for (int zz = 0; zz < chunksize - 1; zz++) * { * if (chunk[xx, yy, zz + 1] == TileIdGrass * && chunk[xx, yy, zz] == TileIdGrass) * { * chunk[xx, yy, zz] = (byte)TileIdDirt; * } * }*/ int v = 1; for (int zz = this.ChunkSize - 2; zz >= 0; zz--) { if (chunk[xx, yy, zz] == WorldGeneratorTools.TileIdEmpty) { v = 0; } if (chunk[xx, yy, zz] == WorldGeneratorTools.TileIdGrass) { if (v == 0) { } else if (v < 4) { chunk[xx, yy, zz] = (byte)WorldGeneratorTools.TileIdDirt; } else { chunk[xx, yy, zz] = (byte)WorldGeneratorTools.TileIdStone; } v++; } } } } if (z == 0) { for (int xx = 0; xx < this.ChunkSize; xx++) { for (int yy = 0; yy < this.ChunkSize; yy++) { chunk[xx, yy, 0] = (byte)this.ChunkSize; } } } return(chunk); }
public void GetHeightAndMoisture(float x, float z, out float altitude, out float moisture) { bool allowBeach = true; if (Steps != null && Steps.Length > 0) { float value = 0; for (int k = 0; k < Steps.Length; k++) { if (Steps[k].Enabled) { switch (Steps[k].OperationType) { case TerrainStepType.SampleHeightMapTexture: value = NoiseTools.GetNoiseValueBilinear(Steps[k].NoiseValues, Steps[k].NoiseTextureSize, x * Steps[k].Frequency, z * Steps[k].Frequency); value = value * (Steps[k].NoiseRangeMax - Steps[k].NoiseRangeMin) + Steps[k].NoiseRangeMin; break; case TerrainStepType.SampleRidgeNoiseFromTexture: value = NoiseTools.GetNoiseValueBilinear(Steps[k].NoiseValues, Steps[k].NoiseTextureSize, x * Steps[k].Frequency, z * Steps[k].Frequency, true); value = value * (Steps[k].NoiseRangeMax - Steps[k].NoiseRangeMin) + Steps[k].NoiseRangeMin; break; case TerrainStepType.Shift: value += Steps[k].Param; break; case TerrainStepType.BeachMask: { int i1 = Steps[k].InputIndex0; if (Steps[i1].Value > Steps[k].Threshold) { allowBeach = false; } } break; case TerrainStepType.AddAndMultiply: value = (value + Steps[k].Param) * Steps[k].Param2; break; case TerrainStepType.MultiplyAndAdd: value = (value * Steps[k].Param) + Steps[k].Param2; break; case TerrainStepType.Exponential: if (value < 0) { value = 0; } value = (float)System.Math.Pow(value, Steps[k].Param); break; case TerrainStepType.Constant: value = Steps[k].Param; break; case TerrainStepType.Invert: value = 1f - value; break; case TerrainStepType.Copy: { int i1 = Steps[k].InputIndex0; value = Steps[i1].Value; } break; case TerrainStepType.Random: value = WorldRandom.GetValue(x, z); break; case TerrainStepType.BlendAdditive: { int i1 = Steps[k].InputIndex0; int i2 = Steps[k].InputIndex1; value = Steps[i1].Value * Steps[k].Weight0 + Steps[i2].Value * Steps[k].Weight1; } break; case TerrainStepType.BlendMultiply: { int i1 = Steps[k].InputIndex0; int i2 = Steps[k].InputIndex1; value = Steps[i1].Value * Steps[i2].Value; } break; case TerrainStepType.Threshold: { int i1 = Steps[k].InputIndex0; if (Steps[i1].Value >= Steps[k].Threshold) { value = Steps[i1].Value + Steps[k].ThresholdShift; } else { value = Steps[k].ThresholdParam; } } break; case TerrainStepType.FlattenOrRaise: if (value >= Steps[k].Threshold) { value = (value - Steps[k].Threshold) * Steps[k].ThresholdParam + Steps[k].Threshold; } break; case TerrainStepType.Clamp: if (value < Steps[k].Min) { value = Steps[k].Min; } else if (value > Steps[k].Max) { value = Steps[k].Max; } break; case TerrainStepType.Select: { int i1 = Steps[k].InputIndex0; if (Steps[i1].Value < Steps[k].Min) { value = Steps[k].ThresholdParam; } else if (Steps[i1].Value > Steps[k].Max) { value = Steps[k].ThresholdParam; } else { value = Steps[i1].Value; } } break; case TerrainStepType.Fill: { int i1 = Steps[k].InputIndex0; if (Steps[i1].Value >= Steps[k].Min && Steps[i1].Value <= Steps[k].Max) { value = Steps[k].ThresholdParam; } } break; } } Steps[k].Value = value; } altitude = value; } else { altitude = -9999; } // Moisture moisture = NoiseTools.GetNoiseValueBilinear(mMoistureValues, mNoiseMoistureTextureSize, x * MoistureScale, z * MoistureScale); if (altitude < mBeachLevelAlignedWithInt && altitude >= mSeaLevelAlignedWithInt) { float depth = mBeachLevelAlignedWithInt - altitude; if (depth > BeachWidth || !allowBeach) { altitude = mSeaLevelAlignedWithInt - 0.0001f; } } if (altitude < mSeaLevelAlignedWithInt) { float depth = mSeaLevelAlignedWithInt - altitude; altitude = mSeaLevelAlignedWithInt - 0.0001f - depth * SeaDepthMultiplier; } }