private Block GetBlockLayerBlock(int unscaledRain, int unscaledTemp, int posY, int firstBlockId, int forDepth, Block defaultBlock, IList <Block> blocks)
        {
            float temperature  = TerraGenConfig.GetScaledAdjustedTemperatureFloat(unscaledTemp, posY - TerraGenConfig.seaLevel);
            float rainRel      = TerraGenConfig.GetRainFall(unscaledRain, posY) / 255f;
            float heightRel    = ((float)posY - TerraGenConfig.seaLevel) / ((float)mapheight - TerraGenConfig.seaLevel);
            float fertilityRel = TerraGenConfig.GetFertilityFromUnscaledTemp((int)(rainRel * 255), unscaledTemp, heightRel) / 255f;

            for (int j = forDepth; j < blockLayerConfig.Blocklayers.Length; j++)
            {
                BlockLayer bl = blockLayerConfig.Blocklayers[j];

                if (
                    temperature >= bl.MinTemp && temperature <= bl.MaxTemp &&
                    rainRel >= bl.MinRain && rainRel <= bl.MaxRain &&
                    fertilityRel >= bl.MinFertility && fertilityRel <= bl.MaxFertility &&
                    (float)posY / mapheight <= bl.MaxY
                    )
                {
                    int blockId = bl.GetBlockId(0.1, temperature, rainRel, fertilityRel, firstBlockId);
                    if (blockId != 0)
                    {
                        return(blocks[blockId]);
                    }
                }
            }

            return(defaultBlock);
        }
        private void LoadBlockLayers(double posRand, float rainRel, float temperature, int unscaledTemp, int posY, int firstBlockId)
        {
            float heightRel    = ((float)posY - TerraGenConfig.seaLevel) / ((float)api.WorldManager.MapSizeY - TerraGenConfig.seaLevel);
            float fertilityRel = TerraGenConfig.GetFertilityFromUnscaledTemp((int)(rainRel * 255), unscaledTemp, heightRel) / 255f;



            float depthf = TerraGenConfig.SoilThickness(rainRel, temperature, posY - TerraGenConfig.seaLevel, 1f);
            int   depth  = (int)depthf;

            depth += (int)((depthf - depth) * rnd.NextDouble());

            BlockLayersIds.Clear();
            for (int j = 0; j < blockLayerConfig.Blocklayers.Length; j++)
            {
                BlockLayer bl = blockLayerConfig.Blocklayers[j];

                float tempDist = Math.Abs(temperature - GameMath.Clamp(temperature, bl.MinTemp, bl.MaxTemp));
                float rainDist = Math.Abs(rainRel - GameMath.Clamp(rainRel, bl.MinRain, bl.MaxRain));
                float fertDist = Math.Abs(fertilityRel - GameMath.Clamp(fertilityRel, bl.MinFertility, bl.MaxFertility));
                float yDist    = Math.Abs((float)posY / mapheight - GameMath.Min((float)posY / mapheight, bl.MaxY));


                if (tempDist + rainDist + fertDist + yDist <= posRand)
                {
                    int blockId = bl.GetBlockId(posRand, temperature, rainRel, fertilityRel, firstBlockId);
                    if (blockId != 0)
                    {
                        BlockLayersIds.Add(blockId);

                        // Would be correct, but doesn't seem to cause noticable problems
                        // so lets not add it for faster chunk gen
                        posY--;
                        temperature = TerraGenConfig.GetScaledAdjustedTemperatureFloat(unscaledTemp, posY - TerraGenConfig.seaLevel);
                        //  rainRel = TerraGenConfig.GetRainFall(unscaledRain, posY) / 255f;
                        heightRel    = ((float)posY - TerraGenConfig.seaLevel) / ((float)api.WorldManager.MapSizeY - TerraGenConfig.seaLevel);
                        fertilityRel = TerraGenConfig.GetFertilityFromUnscaledTemp((int)(rainRel * 255), unscaledTemp, heightRel) / 255f;
                    }
                }

                if (BlockLayersIds.Count >= depth)
                {
                    break;
                }
            }


            layersUnderWater = null;
            for (int j = 0; j < blockLayerConfig.LakeBedLayer.BlockCodeByMin.Length; j++)
            {
                LakeBedBlockCodeByMin lbbc = blockLayerConfig.LakeBedLayer.BlockCodeByMin[j];
                if (lbbc.Suitable(temperature, rainRel, (float)posY / api.WorldManager.MapSizeY, rnd))
                {
                    layersUnderWater = new int[] { lbbc.GetBlockForMotherRock(firstBlockId) };
                    break;
                }
            }
            if (layersUnderWater == null)
            {
                layersUnderWater = new int[0];
            }
        }