private void OnChunkColumnGen(IServerChunk[] chunks, int chunkX, int chunkZ) { IntMap climateMap = chunks[0].MapChunk.MapRegion.ClimateMap; ushort[] heightMap = chunks[0].MapChunk.WorldGenTerrainHeightMap; int regionChunkSize = api.WorldManager.RegionSize / chunksize; int rlX = chunkX % regionChunkSize; int rlZ = chunkZ % regionChunkSize; float facC = (float)climateMap.InnerSize / regionChunkSize; climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC)); climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC)); climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC + facC)); climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC + facC)); IntMap forestMap = chunks[0].MapChunk.MapRegion.ForestMap; float facF = (float)forestMap.InnerSize / regionChunkSize; forestUpLeft = forestMap.GetUnpaddedInt((int)(rlX * facF), (int)(rlZ * facF)); forestUpRight = forestMap.GetUnpaddedInt((int)(rlX * facF + facF), (int)(rlZ * facF)); forestBotLeft = forestMap.GetUnpaddedInt((int)(rlX * facF), (int)(rlZ * facF + facF)); forestBotRight = forestMap.GetUnpaddedInt((int)(rlX * facF + facF), (int)(rlZ * facF + facF)); IntMap shrubMap = chunks[0].MapChunk.MapRegion.ShrubMap; float facS = (float)shrubMap.InnerSize / regionChunkSize; shrubsUpLeft = shrubMap.GetUnpaddedInt((int)(rlX * facS), (int)(rlZ * facS)); shrubsUpRight = shrubMap.GetUnpaddedInt((int)(rlX * facS + facS), (int)(rlZ * facS)); shrubsBotLeft = shrubMap.GetUnpaddedInt((int)(rlX * facS), (int)(rlZ * facS + facS)); shrubsBotRight = shrubMap.GetUnpaddedInt((int)(rlX * facS + facS), (int)(rlZ * facS + facS)); Vec3d posAsVec = new Vec3d(); BlockPos pos = new BlockPos(); foreach (var val in entityTypeGroups) { EntityProperties entitytype = val.Key; float tries = entitytype.Server.SpawnConditions.Worldgen.TriesPerChunk.nextFloat(1, rnd); while (tries-- > rnd.NextDouble()) { int dx = rnd.Next(chunksize); int dz = rnd.Next(chunksize); pos.Set(chunkX * chunksize + dx, 0, chunkZ * chunksize + dz); pos.Y = entitytype.Server.SpawnConditions.Worldgen.TryOnlySurface ? heightMap[dz * chunksize + dx] + 1: rnd.Next(worldheight) ; posAsVec.Set(pos.X + 0.5, pos.Y + 0.005, pos.Z + 0.5); TrySpawnGroupAt(pos, posAsVec, entitytype, val.Value); } } }
private void OnChunkColumnGen(IServerChunk[] chunks, int chunkX, int chunkZ, ITreeAttribute chunkGenParams = null) { IntMap climateMap = chunks[0].MapChunk.MapRegion.ClimateMap; ushort[] heightMap = chunks[0].MapChunk.RainHeightMap; int regionChunkSize = api.WorldManager.RegionSize / chunksize; int rlX = chunkX % regionChunkSize; int rlZ = chunkZ % regionChunkSize; float facC = (float)climateMap.InnerSize / regionChunkSize; int climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC)); int climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC)); int climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC + facC)); int climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC + facC)); for (int x = 0; x < chunksize; x++) { for (int z = 0; z < chunksize; z++) { int posY = heightMap[z * chunksize + x]; int climate = GameMath.BiLerpRgbColor((float)x / chunksize, (float)z / chunksize, climateUpLeft, climateUpRight, climateBotLeft, climateBotRight); float temp = TerraGenConfig.GetScaledAdjustedTemperatureFloat((climate >> 16) & 0xff, posY - TerraGenConfig.seaLevel); int prevY = posY; if (PlaceSnowLayer(x, prevY, z, chunks, temp)) { heightMap[z * chunksize + x]++; } } } }
private void OnChunkColumnGen(IServerChunk[] chunks, int chunkX, int chunkZ, ITreeAttribute chunkGenParams = null) { IMapChunk mapChunk = chunks[0].MapChunk; IntMap forestMap = mapChunk.MapRegion.ForestMap; IntMap shrubMap = mapChunk.MapRegion.ShrubMap; IntMap climateMap = mapChunk.MapRegion.ClimateMap; int rlX = chunkX % regionChunkSize; int rlZ = chunkZ % regionChunkSize; float facS = (float)shrubMap.InnerSize / regionChunkSize; shrubUpLeft = shrubMap.GetUnpaddedInt((int)(rlX * facS), (int)(rlZ * facS)); shrubUpRight = shrubMap.GetUnpaddedInt((int)(rlX * facS + facS), (int)(rlZ * facS)); shrubBotLeft = shrubMap.GetUnpaddedInt((int)(rlX * facS), (int)(rlZ * facS + facS)); shrubBotRight = shrubMap.GetUnpaddedInt((int)(rlX * facS + facS), (int)(rlZ * facS + facS)); // A region has 16 chunks // Size of the forest map is RegionSize / TerraGenConfig.forestMapScale => 32*16 / 32 = 16 pixel // rlX, rlZ goes from 0..16 pixel // facF = 16/16 = 1 // Get 4 pixels for chunkx, chunkz, chunkx+1 and chunkz+1 inside the map float facF = (float)forestMap.InnerSize / regionChunkSize; forestUpLeft = forestMap.GetUnpaddedInt((int)(rlX * facF), (int)(rlZ * facF)); forestUpRight = forestMap.GetUnpaddedInt((int)(rlX * facF + facF), (int)(rlZ * facF)); forestBotLeft = forestMap.GetUnpaddedInt((int)(rlX * facF), (int)(rlZ * facF + facF)); forestBotRight = forestMap.GetUnpaddedInt((int)(rlX * facF + facF), (int)(rlZ * facF + facF)); float facC = (float)climateMap.InnerSize / regionChunkSize; climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC)); climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC)); climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC + facC)); climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC + facC)); heightmap = chunks[0].MapChunk.RainHeightMap; structuresIntersectingChunk.Clear(); api.World.BlockAccessor.WalkStructures(chunkBase.Set(chunkX * chunksize, 0, chunkZ * chunksize), chunkend.Set(chunkX * chunksize + chunksize, chunkMapSizeY * chunksize, chunkZ * chunksize + chunksize), (struc) => { if (struc.Code.StartsWith("trader")) { structuresIntersectingChunk.Add(struc); } }); if (TerraGenConfig.GenerateVegetation) { genPatches(chunkX, chunkZ, false); genShrubs(chunkX, chunkZ); genTrees(chunkX, chunkZ); genPatches(chunkX, chunkZ, true); } }
private void OnChunkColumnGen(IServerChunk[] chunks, int chunkX, int chunkZ, ITreeAttribute chunkGenParams) { IntMap beachMap = chunks[0].MapChunk.MapRegion.BeachMap; Vec3i climate = chunks[0].MapChunk.MapRegion.ClimateMap.ToClimateVec(chunkX, chunkZ, api.WorldManager.RegionSize, chunksize); BlockPos pos = new BlockPos(); ushort[] heightMap = chunks[0].MapChunk.RainHeightMap; int regionChunkSize = api.WorldManager.RegionSize / chunksize; int rdx = chunkX % regionChunkSize; int rdz = chunkZ % regionChunkSize; float beachStep = (float)beachMap.InnerSize / regionChunkSize; int beachUpLeft = beachMap.GetUnpaddedInt((int)(rdx * beachStep), (int)(rdz * beachStep)); int beachUpRight = beachMap.GetUnpaddedInt((int)(rdx * beachStep + beachStep), (int)(rdz * beachStep)); int beachBotLeft = beachMap.GetUnpaddedInt((int)(rdx * beachStep), (int)(rdz * beachStep + beachStep)); int beachBotRight = beachMap.GetUnpaddedInt((int)(rdx * beachStep + beachStep), (int)(rdz * beachStep + beachStep)); for (int x = 0; x < chunksize; x++) { for (int z = 0; z < chunksize; z++) { int y = heightMap[z * chunksize + x]; float beachRel = GameMath.BiLerp(beachUpLeft, beachUpRight, beachBotLeft, beachBotRight, (float)x / chunksize, (float)z / chunksize) / 255f; float tempRel = climate.Z / 255f; if (beachRel > 0.5 && tempRel > 0.5) { pos.X = chunkX * chunksize + x; pos.Y = y + 1; pos.Z = chunkZ * chunksize + z; var a = api.ModLoader.GetModSystem <GenBlockLayers>(); int rockID = chunks[0].MapChunk.TopRockIdMap[z * chunksize + x]; if (a.blockLayerConfig.BeachLayer.BlockIdMapping.TryGetValue(rockID, out int sand)) { int lY = y % chunksize; int i3d = (chunksize * lY + z) * chunksize + x; int chunkY = y / chunksize; double noise = sNoise.Noise(rdx + x, rdx + z); if (chunks[chunkY].Blocks[i3d] == sand && noise > 0.84) { GenPalmTree(blockAccessor, pos, sizeNoise.Noise(pos.X, pos.Y, pos.Z), fruitNoise.Noise(pos.X, pos.Y, pos.Z), frondNoise.Noise(pos.X, pos.Y, pos.Z)); } } } } } }
private void OnChunkColumnGen(IServerChunk[] chunks, int chunkX, int chunkZ) { ushort[] heightmap = chunks[0].MapChunk.RainHeightMap; IntMap climateMap = chunks[0].MapChunk.MapRegion.ClimateMap; int regionChunkSize = api.WorldManager.RegionSize / chunksize; float fac = (float)climateMap.InnerSize / regionChunkSize; int rlX = chunkX % regionChunkSize; int rlZ = chunkZ % regionChunkSize; climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * fac), (int)(rlZ * fac)); climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * fac + fac), (int)(rlZ * fac)); climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * fac), (int)(rlZ * fac + fac)); climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * fac + fac), (int)(rlZ * fac + fac)); int climateMid = GameMath.BiLerpRgbColor(0.5f, 0.5f, climateUpLeft, climateUpRight, climateBotLeft, climateBotRight); // 16-23 bits = Red = temperature // 8-15 bits = Green = rain // 0-7 bits = Blue = humidity int rain = (climateMid >> 8) & 0xff; int humidity = climateMid & 0xff; // Lake density at chunk center float lakeDensity = 4 * (rain + humidity) / 255f; float maxTries = lakeDensity * 10; int dx, dz; while (maxTries-- > 0) { if (maxTries < 1 && rand.NextDouble() > maxTries) { break; } dx = rand.Next(chunksize); dz = rand.Next(chunksize); TryPlaceLakeAt(dx, dz, chunkX, chunkZ, heightmap); //ushort blockId = blockAccessor.GetBlock(new AssetLocation("creativeblock-37")).BlockId; //blockAccessor.SetBlock(blockId, new BlockPos(chunkX * chunksize + dx, heightmap[dz * chunksize + dx] + 1, chunkZ * chunksize + dz)); } }
private void OnChunkColumnGen(IServerChunk[] chunks, int chunkX, int chunkZ) { IntMap forestMap = chunks[0].MapChunk.MapRegion.ForestMap; IntMap shrubMap = chunks[0].MapChunk.MapRegion.ShrubMap; IntMap climateMap = chunks[0].MapChunk.MapRegion.ClimateMap; int rlX = chunkX % regionChunkSize; int rlZ = chunkZ % regionChunkSize; float facS = (float)shrubMap.InnerSize / regionChunkSize; shrubUpLeft = shrubMap.GetUnpaddedInt((int)(rlX * facS), (int)(rlZ * facS)); shrubUpRight = shrubMap.GetUnpaddedInt((int)(rlX * facS + facS), (int)(rlZ * facS)); shrubBotLeft = shrubMap.GetUnpaddedInt((int)(rlX * facS), (int)(rlZ * facS + facS)); shrubBotRight = shrubMap.GetUnpaddedInt((int)(rlX * facS + facS), (int)(rlZ * facS + facS)); // A region has 16 chunks // Size of the forest map is RegionSize / TerraGenConfig.forestMapScale => 32*16 / 32 = 16 pixel // rlX, rlZ goes from 0..16 pixel // facF = 16/16 = 1 // Get 4 pixels for chunkx, chunkz, chunkx+1 and chunkz+1 inside the map float facF = (float)forestMap.InnerSize / regionChunkSize; forestUpLeft = forestMap.GetUnpaddedInt((int)(rlX * facF), (int)(rlZ * facF)); forestUpRight = forestMap.GetUnpaddedInt((int)(rlX * facF + facF), (int)(rlZ * facF)); forestBotLeft = forestMap.GetUnpaddedInt((int)(rlX * facF), (int)(rlZ * facF + facF)); forestBotRight = forestMap.GetUnpaddedInt((int)(rlX * facF + facF), (int)(rlZ * facF + facF)); float facC = (float)climateMap.InnerSize / regionChunkSize; climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC)); climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC)); climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC + facC)); climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC + facC)); heightmap = chunks[0].MapChunk.RainHeightMap; if (TerraGenConfig.GenerateVegetation) { genPatches(chunkX, chunkZ, false); genShrubs(chunkX, chunkZ); genTrees(chunkX, chunkZ); genPatches(chunkX, chunkZ, true); } }
private void OnChunkColumnGen(IServerChunk[] chunks, int chunkX, int chunkZ) { IMapChunk mapChunk = chunks[0].MapChunk; IntMap climateMap = mapChunk.MapRegion.ClimateMap; int rlX = chunkX % regionChunkSize; int rlZ = chunkZ % regionChunkSize; float facC = (float)climateMap.InnerSize / regionChunkSize; climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC)); climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC)); climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC + facC)); climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC + facC)); heightmap = chunks[0].MapChunk.RainHeightMap; genPatches(chunkX, chunkZ); }
public static Vec3i ToClimateVec(this IntMap climateMap, int chunkX, int chunkZ, int regionsize, int chunksize, float lx = 0.5f, float ly = 0.5f) { int regionChunkSize = regionsize / chunksize; float fac = (float)climateMap.InnerSize / regionChunkSize; int rlX = chunkX % regionChunkSize; int rlZ = chunkZ % regionChunkSize; int climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * fac), (int)(rlZ * fac)); int climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * fac + fac), (int)(rlZ * fac)); int climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * fac), (int)(rlZ * fac + fac)); int climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * fac + fac), (int)(rlZ * fac + fac)); int climateMid = GameMath.BiLerpRgbColor(lx, ly, climateUpLeft, climateUpRight, climateBotLeft, climateBotRight); int rain = (climateMid >> 8) & 0xff; int humidity = climateMid & 0xff; int temp = (climateMid >> 16) & 0xff; return(new Vec3i(rain, humidity, temp)); }
private void OnChunkColumnGen(IServerChunk[] chunks, int chunkX, int chunkZ, ITreeAttribute chunkGenParams = null) { if (!TerraGenConfig.GenerateStructures) { return; } IMapRegion region = chunks[0].MapChunk.MapRegion; IntMap forestMap = region.ForestMap; IntMap climateMap = region.ClimateMap; int rlX = chunkX % regionChunkSize; int rlZ = chunkZ % regionChunkSize; // A region has 16 chunks // Size of the forest map is RegionSize / TerraGenConfig.forestMapScale => 32*16 / 32 = 16 pixel // rlX, rlZ goes from 0..16 pixel // facF = 16/16 = 1 // Get 4 pixels for chunkx, chunkz, chunkx+1 and chunkz+1 inside the map float facF = (float)forestMap.InnerSize / regionChunkSize; forestUpLeft = forestMap.GetUnpaddedInt((int)(rlX * facF), (int)(rlZ * facF)); forestUpRight = forestMap.GetUnpaddedInt((int)(rlX * facF + facF), (int)(rlZ * facF)); forestBotLeft = forestMap.GetUnpaddedInt((int)(rlX * facF), (int)(rlZ * facF + facF)); forestBotRight = forestMap.GetUnpaddedInt((int)(rlX * facF + facF), (int)(rlZ * facF + facF)); float facC = (float)climateMap.InnerSize / regionChunkSize; climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC)); climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC)); climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC + facC)); climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC + facC)); heightmap = chunks[0].MapChunk.WorldGenTerrainHeightMap; DoGenStructures(region, chunkX, chunkZ, false, chunkGenParams); DoGenVillages(region, chunkX, chunkZ, false, chunkGenParams); }
private void OnChunkColumnGen(IServerChunk[] chunks, int chunkX, int chunkZ, ITreeAttribute chunkGenParams = null) { landforms = NoiseLandforms.landforms; IMapChunk mapchunk = chunks[0].MapChunk; int climateUpLeft; int climateUpRight; int climateBotLeft; int climateBotRight; IntMap climateMap = chunks[0].MapChunk.MapRegion.ClimateMap; int regionChunkSize = api.WorldManager.RegionSize / chunksize; float fac = (float)climateMap.InnerSize / regionChunkSize; int rlX = chunkX % regionChunkSize; int rlZ = chunkZ % regionChunkSize; climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * fac), (int)(rlZ * fac)); climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * fac + fac), (int)(rlZ * fac)); climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * fac), (int)(rlZ * fac + fac)); climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * fac + fac), (int)(rlZ * fac + fac)); int freezingTemp = TerraGenConfig.DescaleTemperature(-17); IntMap landformMap = mapchunk.MapRegion.LandformMap; // Amount of pixels for each chunk (probably 1, 2, or 4) in the land form map float chunkPixelSize = landformMap.InnerSize / regionChunkSize; // Adjusted lerp for the noiseWidth float chunkPixelStep = chunkPixelSize / noiseWidth; // Start coordinates for the chunk in the region map float baseX = (chunkX % regionChunkSize) * chunkPixelSize; float baseZ = (chunkZ % regionChunkSize) * chunkPixelSize; LerpedWeightedIndex2DMap landLerpMap = GetOrLoadLerpedLandformMap(chunks[0].MapChunk, chunkX / regionChunkSize, chunkZ / regionChunkSize); // Terrain octaves double[] octNoiseX0, octNoiseX1, octNoiseX2, octNoiseX3; double[] octThX0, octThX1, octThX2, octThX3; // So it seems we have some kind of off-by-one error here? // When the slope of a mountain goes up (in positive z or x direction), particularly at large word heights (512+) // then the last blocks (again in postive x/z dir) are below of where they should be? // I have no idea why, but this offset seems to greatly mitigate the issue float weirdOffset = 0.25f; chunkPixelSize += weirdOffset; GetInterpolatedOctaves(landLerpMap[baseX, baseZ], out octNoiseX0, out octThX0); GetInterpolatedOctaves(landLerpMap[baseX + chunkPixelSize, baseZ], out octNoiseX1, out octThX1); GetInterpolatedOctaves(landLerpMap[baseX, baseZ + chunkPixelSize], out octNoiseX2, out octThX2); GetInterpolatedOctaves(landLerpMap[baseX + chunkPixelSize, baseZ + chunkPixelSize], out octNoiseX3, out octThX3); double[] terrainNoise3d = GetTerrainNoise3D(octNoiseX0, octNoiseX1, octNoiseX2, octNoiseX3, octThX0, octThX1, octThX2, octThX3, chunkX * noiseWidth, 0, chunkZ * noiseWidth); // Store heightmap in the map chunk ushort[] rainheightmap = chunks[0].MapChunk.RainHeightMap; ushort[] terrainheightmap = chunks[0].MapChunk.WorldGenTerrainHeightMap; // Terrain thresholds double tnoiseY0; double tnoiseY1; double tnoiseY2; double tnoiseY3; double tnoiseGainY0; double tnoiseGainY1; double tnoiseGainY2; double tnoiseGainY3; double thNoiseX0; double thNoiseX1; double thNoiseGainX0; double thNoiseGainX1; double thNoiseGainZ0; double thNoiseZ0; float[] terrainThresholdsX0 = new float[api.WorldManager.MapSizeY]; float[] terrainThresholdsX1 = new float[api.WorldManager.MapSizeY]; float[] terrainThresholdsX2 = new float[api.WorldManager.MapSizeY]; float[] terrainThresholdsX3 = new float[api.WorldManager.MapSizeY]; for (int xN = 0; xN < noiseWidth; xN++) { for (int zN = 0; zN < noiseWidth; zN++) { // Landform thresholds LoadInterpolatedThresholds(landLerpMap[baseX + xN * chunkPixelStep, baseZ + zN * chunkPixelStep], terrainThresholdsX0); LoadInterpolatedThresholds(landLerpMap[baseX + (xN + 1) * chunkPixelStep, baseZ + zN * chunkPixelStep], terrainThresholdsX1); LoadInterpolatedThresholds(landLerpMap[baseX + xN * chunkPixelStep, baseZ + (zN + 1) * chunkPixelStep], terrainThresholdsX2); LoadInterpolatedThresholds(landLerpMap[baseX + (xN + 1) * chunkPixelStep, baseZ + (zN + 1) * chunkPixelStep], terrainThresholdsX3); for (int yN = 0; yN < noiseHeight; yN++) { // Terrain noise tnoiseY0 = terrainNoise3d[NoiseIndex3d(xN, yN, zN)]; tnoiseY1 = terrainNoise3d[NoiseIndex3d(xN, yN, zN + 1)]; tnoiseY2 = terrainNoise3d[NoiseIndex3d(xN + 1, yN, zN)]; tnoiseY3 = terrainNoise3d[NoiseIndex3d(xN + 1, yN, zN + 1)]; tnoiseGainY0 = (terrainNoise3d[NoiseIndex3d(xN, yN + 1, zN)] - tnoiseY0) * lerpDeltaVert; tnoiseGainY1 = (terrainNoise3d[NoiseIndex3d(xN, yN + 1, zN + 1)] - tnoiseY1) * lerpDeltaVert; tnoiseGainY2 = (terrainNoise3d[NoiseIndex3d(xN + 1, yN + 1, zN)] - tnoiseY2) * lerpDeltaVert; tnoiseGainY3 = (terrainNoise3d[NoiseIndex3d(xN + 1, yN + 1, zN + 1)] - tnoiseY3) * lerpDeltaVert; for (int y = 0; y < lerpVer; y++) { int posY = yN * lerpVer + y; int chunkY = posY / chunksize; int localY = posY % chunksize; // For Terrain noise double tnoiseX0 = tnoiseY0; double tnoiseX1 = tnoiseY1; double tnoiseGainX0 = (tnoiseY2 - tnoiseY0) * lerpDeltaHor; double tnoiseGainX1 = (tnoiseY3 - tnoiseY1) * lerpDeltaHor; // Landform thresholds lerp thNoiseX0 = terrainThresholdsX0[posY]; thNoiseX1 = terrainThresholdsX2[posY]; thNoiseGainX0 = (terrainThresholdsX1[posY] - thNoiseX0) * lerpDeltaHor; thNoiseGainX1 = (terrainThresholdsX3[posY] - thNoiseX1) * lerpDeltaHor; for (int x = 0; x < lerpHor; x++) { // For terrain noise double tnoiseZ0 = tnoiseX0; double tnoiseGainZ0 = (tnoiseX1 - tnoiseX0) * lerpDeltaHor; // Landform thNoiseZ0 = thNoiseX0; thNoiseGainZ0 = (thNoiseX1 - thNoiseX0) * lerpDeltaHor; for (int z = 0; z < lerpHor; z++) { int lX = xN * lerpHor + x; int lZ = zN * lerpHor + z; int mapIndex = ChunkIndex2d(lX, lZ); int chunkIndex = ChunkIndex3d(lX, localY, lZ); chunks[chunkY].Blocks[chunkIndex] = 0; if (posY == 0) { chunks[chunkY].Blocks[chunkIndex] = GlobalConfig.mantleBlockId; continue; } if (tnoiseZ0 > thNoiseZ0) { terrainheightmap[mapIndex] = rainheightmap[mapIndex] = (ushort)Math.Max(rainheightmap[mapIndex], posY); chunks[chunkY].Blocks[chunkIndex] = GlobalConfig.defaultRockId; } else { if (posY < TerraGenConfig.seaLevel) { terrainheightmap[mapIndex] = rainheightmap[mapIndex] = (ushort)Math.Max(rainheightmap[mapIndex], posY); if (posY == TerraGenConfig.seaLevel - 1) { int temp = (GameMath.BiLerpRgbColor(((float)lX) / chunksize, ((float)lZ) / chunksize, climateUpLeft, climateUpRight, climateBotLeft, climateBotRight) >> 16) & 0xff; float distort = (float)distort2dx.Noise(chunkX * chunksize + lX, chunkZ * chunksize + lZ) / 20f; float tempf = TerraGenConfig.GetScaledAdjustedTemperatureFloat(temp, 0) + distort; // dominionsmod chunks[chunkY].Blocks[chunkIndex] = (tempf < -17) ? GlobalConfig.lakeIceBlockId : saltWater; } else { // dominionsmod chunks[chunkY].Blocks[chunkIndex] = saltWater; } } else { chunks[chunkY].Blocks[chunkIndex] = 0; } } tnoiseZ0 += tnoiseGainZ0; thNoiseZ0 += thNoiseGainZ0; } tnoiseX0 += tnoiseGainX0; tnoiseX1 += tnoiseGainX1; thNoiseX0 += thNoiseGainX0; thNoiseX1 += thNoiseGainX1; } tnoiseY0 += tnoiseGainY0; tnoiseY1 += tnoiseGainY1; tnoiseY2 += tnoiseGainY2; tnoiseY3 += tnoiseGainY3; } } } } int ymax = 0; for (int i = 0; i < rainheightmap.Length; i++) { ymax = Math.Max(ymax, rainheightmap[i]); } chunks[0].MapChunk.YMax = (ushort)ymax; }
private void OnChunkColumnGeneration(IServerChunk[] chunks, int chunkX, int chunkZ, ITreeAttribute chunkGenParams = null) { IntMap forestMap = chunks[0].MapChunk.MapRegion.ForestMap; IntMap climateMap = chunks[0].MapChunk.MapRegion.ClimateMap; ushort[] heightMap = chunks[0].MapChunk.RainHeightMap; int regionChunkSize = api.WorldManager.RegionSize / chunksize; int rdx = chunkX % regionChunkSize; int rdz = chunkZ % regionChunkSize; // "Pixels per chunk" float climateStep = (float)climateMap.InnerSize / regionChunkSize; float forestStep = (float)forestMap.InnerSize / regionChunkSize; // Retrieves the map data on the chunk edges int forestUpLeft = forestMap.GetUnpaddedInt((int)(rdx * forestStep), (int)(rdz * forestStep)); int forestUpRight = forestMap.GetUnpaddedInt((int)(rdx * forestStep + forestStep), (int)(rdz * forestStep)); int forestBotLeft = forestMap.GetUnpaddedInt((int)(rdx * forestStep), (int)(rdz * forestStep + forestStep)); int forestBotRight = forestMap.GetUnpaddedInt((int)(rdx * forestStep + forestStep), (int)(rdz * forestStep + forestStep)); // increasing x -> left to right // increasing z -> top to bottom float transitionSize = blockLayerConfig.blockLayerTransitionSize; for (int x = 0; x < chunksize; x++) { for (int z = 0; z < chunksize; z++) { // Some weird randomnes stuff to hide fundamental bugs in the climate transition system :D T_T (maybe not bugs but just fundamental shortcomings of using lerp on a very low resolution map) float distx = (float)distort2dx.Noise(chunkX * chunksize + x, chunkZ * chunksize + z); float distz = (float)distort2dz.Noise(chunkX * chunksize + x, chunkZ * chunksize + z); double posRand = (double)GameMath.MurmurHash3(x + chunkX * chunksize, 1, z + chunkZ * chunksize) / int.MaxValue; double transitionRand = (posRand + 1) * transitionSize; int posY = heightMap[z * chunksize + x]; int climate = climateMap.GetUnpaddedColorLerped( rdx * climateStep + climateStep * (float)(x + distx) / chunksize, rdz * climateStep + climateStep * (float)(z + distz) / chunksize ); int tempUnscaled = (climate >> 16) & 0xff; int rnd = (int)(distx / 5); float temp = TerraGenConfig.GetScaledAdjustedTemperatureFloat(tempUnscaled, posY - TerraGenConfig.seaLevel + rnd); float rainRel = TerraGenConfig.GetRainFall((climate >> 8) & 0xff, posY + rnd) / 255f; float forestRel = GameMath.BiLerp(forestUpLeft, forestUpRight, forestBotLeft, forestBotRight, (float)x / chunksize, (float)z / chunksize) / 255f; int prevY = posY; posY = PutLayers(transitionRand, x, posY, z, chunks, rainRel, temp, tempUnscaled, heightMap); PlaceTallGrass(x, prevY, z, chunks, rainRel, temp, forestRel); // Try again to put layers if above sealevel and we found over 10 air blocks int foundAir = 0; while (posY >= TerraGenConfig.seaLevel - 1) { int chunkY = posY / chunksize; int lY = posY % chunksize; int index3d = (chunksize * lY + z) * chunksize + x; int blockId = chunks[chunkY].Blocks[index3d]; if (blockId == 0) { foundAir++; } else { if (foundAir >= 8) { temp = TerraGenConfig.GetScaledAdjustedTemperatureFloat(tempUnscaled, posY - TerraGenConfig.seaLevel); rainRel = TerraGenConfig.GetRainFall((climate >> 8) & 0xff, posY) / 255f; PutLayers(transitionRand, x, posY, z, chunks, rainRel, temp, tempUnscaled, null); break; } else { foundAir = 0; } } posY--; } } } }
private void OnChunkColumnGeneration(IServerChunk[] chunks, int chunkX, int chunkZ) { IntMap forestMap = chunks[0].MapChunk.MapRegion.ForestMap; IntMap climateMap = chunks[0].MapChunk.MapRegion.ClimateMap; ushort[] heightMap = chunks[0].MapChunk.RainHeightMap; int regionChunkSize = api.WorldManager.RegionSize / chunksize; int rlX = chunkX % regionChunkSize; int rlZ = chunkZ % regionChunkSize; // "Pixels per chunk" float facF = (float)forestMap.InnerSize / regionChunkSize; // Retrieves the map data on the chunk edges int forestUpLeft = forestMap.GetUnpaddedInt((int)(rlX * facF), (int)(rlZ * facF)); int forestUpRight = forestMap.GetUnpaddedInt((int)(rlX * facF + facF), (int)(rlZ * facF)); int forestBotLeft = forestMap.GetUnpaddedInt((int)(rlX * facF), (int)(rlZ * facF + facF)); int forestBotRight = forestMap.GetUnpaddedInt((int)(rlX * facF + facF), (int)(rlZ * facF + facF)); float facC = (float)climateMap.InnerSize / regionChunkSize; int climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC)); int climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC)); int climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC + facC)); int climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC + facC)); for (int x = 0; x < chunksize; x++) { for (int z = 0; z < chunksize; z++) { int posY = heightMap[z * chunksize + x]; int climate = GameMath.BiLerpRgbColor((float)x / chunksize, (float)z / chunksize, climateUpLeft, climateUpRight, climateBotLeft, climateBotRight); int tempUnscaled = (climate >> 16) & 0xff; float temp = TerraGenConfig.GetScaledAdjustedTemperatureFloat(tempUnscaled, posY - TerraGenConfig.seaLevel); float rainRel = TerraGenConfig.GetRainFall((climate >> 8) & 0xff, posY) / 255f; float forestRel = GameMath.BiLerp(forestUpLeft, forestUpRight, forestBotLeft, forestBotRight, (float)x / chunksize, (float)z / chunksize) / 255f; int prevY = posY; posY = PutLayers(x, posY, z, chunks, rainRel, temp, tempUnscaled, heightMap); PlaceTallGrass(x, prevY, z, chunks, rainRel, temp, forestRel); // Try again to put layers if above sealevel and we found over 10 air blocks int foundAir = 0; while (posY >= TerraGenConfig.seaLevel - 1) { int chunkY = posY / chunksize; int lY = posY % chunksize; int index3d = (chunksize * lY + z) * chunksize + x; ushort blockId = chunks[chunkY].Blocks[index3d]; if (blockId == 0) { foundAir++; } else { if (foundAir >= 8) { temp = TerraGenConfig.GetScaledAdjustedTemperatureFloat(tempUnscaled, posY - TerraGenConfig.seaLevel); rainRel = TerraGenConfig.GetRainFall((climate >> 8) & 0xff, posY) / 255f; PutLayers(x, posY, z, chunks, rainRel, temp, tempUnscaled, null); break; } else { foundAir = 0; } } posY--; } } } }
private void OnChunkColumnGen(IServerChunk[] chunks, int chunkX, int chunkZ, ITreeAttribute chunkGenParams = null) { IntMap climateMap = chunks[0].MapChunk.MapRegion.ClimateMap; int regionChunkSize = api.WorldManager.RegionSize / chunksize; float fac = (float)climateMap.InnerSize / regionChunkSize; int rlX = chunkX % regionChunkSize; int rlZ = chunkZ % regionChunkSize; int climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * fac), (int)(rlZ * fac)); int climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * fac + fac), (int)(rlZ * fac)); int climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * fac), (int)(rlZ * fac + fac)); int climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * fac + fac), (int)(rlZ * fac + fac)); int climateMid = GameMath.BiLerpRgbColor(0.5f, 0.5f, climateUpLeft, climateUpRight, climateBotLeft, climateBotRight); // 16-23 bits = Red = temperature // 8-15 bits = Green = rain // 0-7 bits = Blue = humidity int rain = (climateMid >> 8) & 0xff; int humidity = climateMid & 0xff; int temp = (climateMid >> 16) & 0xff; int quantityRivulets = (int)(80 * (rain + humidity) / 255f) * (api.WorldManager.MapSizeY / chunksize) - Math.Max(0, 100 - temp); int fx, fy, fz; while (quantityRivulets-- > 0) { int dx = 1 + rnd.Next(chunksize - 2); int y = 1 + rnd.Next(api.WorldManager.MapSizeY - 2); int dz = 1 + rnd.Next(chunksize - 2); int quantitySolid = 0; int quantityAir = 0; for (int i = 0; i < BlockFacing.ALLFACES.Length; i++) { BlockFacing facing = BlockFacing.ALLFACES[i]; fx = dx + facing.Normali.X; fy = y + facing.Normali.Y; fz = dz + facing.Normali.Z; Block block = api.World.Blocks[ chunks[fy / chunksize].Blocks[(chunksize * (fy % chunksize) + fz) * chunksize + fx] ]; bool solid = block.BlockMaterial == EnumBlockMaterial.Stone; quantitySolid += solid ? 1 : 0; quantityAir += (block.BlockMaterial == EnumBlockMaterial.Air) ? 1 : 0; if (!solid && facing == BlockFacing.UP) { quantitySolid = 0; } } if (quantitySolid != 5 || quantityAir != 1) { continue; } chunks[y / chunksize].Blocks[(chunksize * (y % chunksize) + dz) * chunksize + dx] = y < 24 ? GlobalConfig.lavaBlockId : GlobalConfig.waterBlockId; BlockPos pos = new BlockPos(chunkX * chunksize + dx, y, chunkZ * chunksize + dz); blockAccessor.ScheduleBlockUpdate(pos); } }
private void OnChunkColumnGen(IServerChunk[] chunks, int chunkX, int chunkZ) { if (!TerraGenConfig.GenerateStructures) { return; } IMapRegion region = chunks[0].MapChunk.MapRegion; IntMap forestMap = region.ForestMap; IntMap climateMap = region.ClimateMap; int rlX = chunkX % regionChunkSize; int rlZ = chunkZ % regionChunkSize; // A region has 16 chunks // Size of the forest map is RegionSize / TerraGenConfig.forestMapScale => 32*16 / 32 = 16 pixel // rlX, rlZ goes from 0..16 pixel // facF = 16/16 = 1 // Get 4 pixels for chunkx, chunkz, chunkx+1 and chunkz+1 inside the map float facF = (float)forestMap.InnerSize / regionChunkSize; forestUpLeft = forestMap.GetUnpaddedInt((int)(rlX * facF), (int)(rlZ * facF)); forestUpRight = forestMap.GetUnpaddedInt((int)(rlX * facF + facF), (int)(rlZ * facF)); forestBotLeft = forestMap.GetUnpaddedInt((int)(rlX * facF), (int)(rlZ * facF + facF)); forestBotRight = forestMap.GetUnpaddedInt((int)(rlX * facF + facF), (int)(rlZ * facF + facF)); float facC = (float)climateMap.InnerSize / regionChunkSize; climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC)); climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC)); climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * facC), (int)(rlZ * facC + facC)); climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * facC + facC), (int)(rlZ * facC + facC)); heightmap = chunks[0].MapChunk.WorldGenTerrainHeightMap; BlockPos pos = new BlockPos(); for (int i = 0; i < scfg.Structures.Length; i++) { WorldGenStructure struc = scfg.Structures[i]; float chance = struc.Chance * scfg.ChanceMultiplier; while (chance-- > rnd.NextDouble()) { int dx = rnd.Next(chunksize); int dz = rnd.Next(chunksize); int ySurface = heightmap[dz * chunksize + dx]; if (ySurface <= 0 || ySurface >= worldheight - 15) { continue; } if (struc.Placement == EnumStructurePlacement.Underground) { if (struc.Depth != null) { pos.Set(chunkX * chunksize + dx, ySurface - (int)struc.Depth.nextFloat(), chunkZ * chunksize + dz); } else { pos.Set(chunkX * chunksize + dx, 8 + rnd.Next(ySurface - 8 - 5), chunkZ * chunksize + dz); } } else { pos.Set(chunkX * chunksize + dx, ySurface, chunkZ * chunksize + dz); } struc.TryGenerate(worldgenBlockAccessor, api.World, pos, climateUpLeft, climateUpRight, climateBotLeft, climateBotRight); } } }
public void TryPlaceLakeAt(int dx, int dz, int chunkX, int chunkZ, ushort[] heightmap, int depth = 0) { searchPositionsDeltas.Clear(); lakePositions.Clear(); // Clear Array for (int i = 0; i < didCheckPosition.Length; i++) { didCheckPosition[i] = false; } lakeYPos = heightmap[dz * chunksize + dx] + 1; if (lakeYPos <= 0 || lakeYPos >= mapheight - 1) { return; } int basePosX = chunkX * chunksize; int basePosZ = chunkZ * chunksize; Vec2i tmp = new Vec2i(); searchPositionsDeltas.Enqueue(new Vec2i(dx, dz)); lakePositions.Enqueue(new Vec2i(basePosX + dx, basePosZ + dz)); didCheckPosition[(dz + mapOffset) * searchSize + dx + mapOffset] = true; while (searchPositionsDeltas.Count > 0) { Vec2i p = searchPositionsDeltas.Dequeue(); foreach (BlockFacing facing in BlockFacing.HORIZONTALS) { ndx = p.X + facing.Normali.X; ndz = p.Y + facing.Normali.Z; tmp.Set(chunkX * chunksize + ndx, chunkZ * chunksize + ndz); Block belowBlock = blockAccessor.GetBlock(tmp.X, lakeYPos - 1, tmp.Y); bool inBoundary = ndx > minBoundary && ndz > minBoundary && ndx < maxBoundary && ndz < maxBoundary; // Only continue when within our 3x3 chunk search area and having a more or less solid block below (or water) if (inBoundary && (belowBlock.Replaceable < 6000 || belowBlock.BlockId == GlobalConfig.waterBlockId)) { int arrayIndex = (ndz + mapOffset) * searchSize + ndx + mapOffset; // Already checked or did we reach a lake border? if (!didCheckPosition[arrayIndex] && blockAccessor.GetBlock(tmp.X, lakeYPos, tmp.Y).Replaceable >= 6000) { searchPositionsDeltas.Enqueue(new Vec2i(ndx, ndz)); lakePositions.Enqueue(tmp.Copy()); didCheckPosition[arrayIndex] = true; } } else { lakePositions.Clear(); searchPositionsDeltas.Clear(); return; } } } if (lakePositions.Count == 0) { return; } int curChunkX, curChunkZ; int prevChunkX = -1, prevChunkZ = -1; int regionChunkSize = api.WorldManager.RegionSize / chunksize; IMapChunk mapchunk = null; IServerChunk chunk = null; IServerChunk chunkOneBlockBelow = null; int ly = GameMath.Mod(lakeYPos, chunksize); bool extraLakeDepth = rand.NextDouble() > 0.5; bool withSeabed = extraLakeDepth || lakePositions.Count > 16; foreach (Vec2i p in lakePositions) { curChunkX = p.X / chunksize; curChunkZ = p.Y / chunksize; int lx = GameMath.Mod(p.X, chunksize); int lz = GameMath.Mod(p.Y, chunksize); // Get correct chunk and correct climate data if we don't have it already if (curChunkX != prevChunkX || curChunkZ != prevChunkZ) { chunk = ((IServerChunk)blockAccessor.GetChunk(curChunkX, lakeYPos / chunksize, curChunkZ)); chunk.Unpack(); if (ly == 0) { chunkOneBlockBelow = ((IServerChunk)blockAccessor.GetChunk(curChunkX, (lakeYPos - 1) / chunksize, curChunkZ)); chunkOneBlockBelow.Unpack(); } else { chunkOneBlockBelow = chunk; } mapchunk = chunk.MapChunk; IntMap climateMap = mapchunk.MapRegion.ClimateMap; float fac = (float)climateMap.InnerSize / regionChunkSize; int rlX = curChunkX % regionChunkSize; int rlZ = curChunkZ % regionChunkSize; climateUpLeft = climateMap.GetUnpaddedInt((int)(rlX * fac), (int)(rlZ * fac)); climateUpRight = climateMap.GetUnpaddedInt((int)(rlX * fac + fac), (int)(rlZ * fac)); climateBotLeft = climateMap.GetUnpaddedInt((int)(rlX * fac), (int)(rlZ * fac + fac)); climateBotRight = climateMap.GetUnpaddedInt((int)(rlX * fac + fac), (int)(rlZ * fac + fac)); prevChunkX = curChunkX; prevChunkZ = curChunkZ; } // Raise heightmap by 1 mapchunk.RainHeightMap[lz * chunksize + lx] = (ushort)lakeYPos; // Identify correct climate at this position int climate = GameMath.BiLerpRgbColor((float)lx / chunksize, (float)lz / chunksize, climateUpLeft, climateUpRight, climateBotLeft, climateBotRight); float temp = TerraGenConfig.GetScaledAdjustedTemperatureFloat((climate >> 16) & 0xff, lakeYPos - TerraGenConfig.seaLevel); // 1. Place water or ice block chunk.Blocks[(ly * chunksize + lz) * chunksize + lx] = temp < -5 ? GlobalConfig.lakeIceBlockId : GlobalConfig.waterBlockId; // 2. Let's make a nice muddy gravely sea bed if (!withSeabed) { continue; } // Need to check the block below first int index = ly == 0 ? ((31 * chunksize + lz) * chunksize + lx) : (((ly - 1) * chunksize + lz) * chunksize + lx) ; Block belowBlock = api.World.Blocks[chunkOneBlockBelow.Blocks[index]]; // Water below? Seabed already placed if (belowBlock.IsLiquid()) { continue; } float rainRel = TerraGenConfig.GetRainFall((climate >> 8) & 0xff, lakeYPos) / 255f; ushort rockBlockId = mapchunk.TopRockIdMap[lz * chunksize + lx]; if (rockBlockId == 0) { continue; } for (int i = 0; i < lakebedLayerConfig.BlockCodeByMin.Length; i++) { if (lakebedLayerConfig.BlockCodeByMin[i].Suitable(temp, rainRel, (float)lakeYPos / mapheight, rand)) { chunkOneBlockBelow.Blocks[index] = lakebedLayerConfig.BlockCodeByMin[i].GetBlockForMotherRock(rockBlockId); break; } } } if (lakePositions.Count > 0 && extraLakeDepth) { TryPlaceLakeAt(dx, dz, chunkX, chunkZ, heightmap, depth + 1); } }