Merge branch 'kitswas/worldgen-spawn-fixes' into 'master'

Spawn fix(es)

See merge request veloren/veloren!3579
This commit is contained in:
Marcel 2022-10-05 07:07:43 +00:00
commit 6844094389

View File

@ -1,6 +1,7 @@
use crate::{column::ColumnSample, sim::SimChunk, Canvas, CONFIG}; use crate::{column::ColumnSample, sim::SimChunk, Canvas, CONFIG};
use common::terrain::{Block, BlockKind, SpriteKind}; use common::terrain::{Block, BlockKind, SpriteKind};
use noise::NoiseFn; use noise::NoiseFn;
use num::traits::Pow;
use rand::prelude::*; use rand::prelude::*;
use std::f32; use std::f32;
use vek::*; use vek::*;
@ -9,6 +10,20 @@ pub fn close(x: f32, tgt: f32, falloff: f32) -> f32 {
(1.0 - (x - tgt).abs() / falloff).max(0.0).powf(0.125) (1.0 - (x - tgt).abs() / falloff).max(0.0).powf(0.125)
} }
/// Returns a decimal value between 0 and 1.
/// The density is maximum at the middle of the highest and the lowest allowed
/// altitudes, and zero otherwise. Quadratic curve.
///
/// The formula used is:
///
/// ```latex
/// \max\left(-\frac{4\left(x-u\right)\left(x-l\right)}{\left(u-l\right)^{2}},\ 0\right)
/// ```
pub fn density_factor_by_altitude(lower_limit: f32, altitude: f32, upper_limit: f32) -> f32 {
let maximum: f32 = (upper_limit - lower_limit).pow(2) / 4.0f32;
(-((altitude - lower_limit) * (altitude - upper_limit)) / maximum).max(0.0)
}
const MUSH_FACT: f32 = 1.0e-4; // To balance things around the mushroom spawning rate const MUSH_FACT: f32 = 1.0e-4; // To balance things around the mushroom spawning rate
const GRASS_FACT: f32 = 1.0e-3; // To balance things around the grass spawning rate const GRASS_FACT: f32 = 1.0e-3; // To balance things around the grass spawning rate
const DEPTH_WATER_NORM: f32 = 15.0; // Water depth at which regular underwater sprites start spawning const DEPTH_WATER_NORM: f32 = 15.0; // Water depth at which regular underwater sprites start spawning
@ -129,17 +144,21 @@ pub fn apply_scatter_to(canvas: &mut Canvas, rng: &mut impl Rng) {
ScatterConfig { ScatterConfig {
kind: Cotton, kind: Cotton,
water_mode: Ground, water_mode: Ground,
permit: |b| matches!(b, BlockKind::Grass), permit: |b| matches!(b, BlockKind::Earth | BlockKind::Grass),
f: |_, col| { f: |_, col| {
( (
close(col.temp, CONFIG.temperate_temp, 0.7).min(close( close(col.temp, CONFIG.tropical_temp, 0.7).min(close(
col.humidity, col.humidity,
CONFIG.jungle_hum, CONFIG.jungle_hum,
0.4, 0.4,
)) * col.tree_density )) * col.tree_density
* MUSH_FACT * MUSH_FACT
* 75.0, * 200.0
Some((0.0, 256.0, 0.25)), * (!col.snow_cover) as i32 as f32 /* To prevent spawning in snow covered areas */
* density_factor_by_altitude(-500.0 , col.alt, 500.0), /* To prevent
* spawning at high
* altitudes */
Some((0.0, 128.0, 0.30)),
) )
}, },
}, },
@ -163,10 +182,16 @@ pub fn apply_scatter_to(canvas: &mut Canvas, rng: &mut impl Rng) {
permit: |b| matches!(b, BlockKind::Grass), permit: |b| matches!(b, BlockKind::Grass),
f: |_, col| { f: |_, col| {
( (
close(col.temp, 0.0, 0.7).min(close(col.humidity, CONFIG.jungle_hum, 0.4)) close(col.temp, CONFIG.temperate_temp, 0.7).min(close(
* col.tree_density col.humidity,
CONFIG.forest_hum,
0.4,
)) * col.tree_density
* MUSH_FACT * MUSH_FACT
* 600.0, * 600.0
* density_factor_by_altitude(200.0, col.alt, 1000.0), /* To control
* spawning based
* on altitude */
Some((0.0, 100.0, 0.15)), Some((0.0, 100.0, 0.15)),
) )
}, },