2019-06-22 21:44:27 +00:00
|
|
|
use crate::{
|
|
|
|
all::ForestKind,
|
|
|
|
sim::{Location, LocationInfo},
|
|
|
|
util::Sampler,
|
|
|
|
World,
|
|
|
|
CONFIG,
|
|
|
|
};
|
2019-06-09 10:24:18 +00:00
|
|
|
use common::{
|
2019-06-11 18:39:25 +00:00
|
|
|
terrain::{Block, TerrainChunkSize},
|
2019-06-15 10:36:26 +00:00
|
|
|
vol::{VolSize, Vox},
|
2019-06-09 10:24:18 +00:00
|
|
|
};
|
2019-06-15 10:36:26 +00:00
|
|
|
use noise::NoiseFn;
|
2019-06-18 21:22:31 +00:00
|
|
|
use std::{
|
2019-06-19 19:58:56 +00:00
|
|
|
f32,
|
2019-06-18 21:22:31 +00:00
|
|
|
ops::{Add, Div, Mul, Neg, Sub},
|
|
|
|
sync::Arc,
|
|
|
|
};
|
2019-06-15 10:36:26 +00:00
|
|
|
use vek::*;
|
2019-06-09 10:24:18 +00:00
|
|
|
|
|
|
|
pub struct ColumnGen<'a> {
|
|
|
|
world: &'a World,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> ColumnGen<'a> {
|
|
|
|
pub fn new(world: &'a World) -> Self {
|
2019-06-15 10:36:26 +00:00
|
|
|
Self { world }
|
2019-06-09 10:24:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<'a> Sampler for ColumnGen<'a> {
|
|
|
|
type Index = Vec2<i32>;
|
2019-06-18 21:22:31 +00:00
|
|
|
type Sample = Option<ColumnSample<'a>>;
|
2019-06-09 10:24:18 +00:00
|
|
|
|
2019-06-18 21:22:31 +00:00
|
|
|
fn get(&self, wpos: Vec2<i32>) -> Option<ColumnSample<'a>> {
|
2019-06-09 10:24:18 +00:00
|
|
|
let wposf = wpos.map(|e| e as f64);
|
2019-06-15 10:36:26 +00:00
|
|
|
let chunk_pos = wpos.map2(Vec2::from(TerrainChunkSize::SIZE), |e, sz: u32| {
|
2019-06-18 21:22:31 +00:00
|
|
|
e / sz as i32
|
2019-06-15 10:36:26 +00:00
|
|
|
});
|
2019-06-09 10:24:18 +00:00
|
|
|
|
|
|
|
let sim = self.world.sim();
|
|
|
|
|
2019-06-22 23:08:21 +00:00
|
|
|
let turb = Vec2::new(
|
2019-06-22 23:13:59 +00:00
|
|
|
sim.gen_ctx.turb_x_nz.get((wposf.div(48.0)).into_array()) as f32,
|
|
|
|
sim.gen_ctx.turb_y_nz.get((wposf.div(48.0)).into_array()) as f32,
|
2019-06-22 23:08:21 +00:00
|
|
|
) * 12.0;
|
2019-06-22 23:13:59 +00:00
|
|
|
let wposf_turb = wposf + turb.map(|e| e as f64);
|
2019-06-22 23:08:21 +00:00
|
|
|
|
2019-06-09 10:24:18 +00:00
|
|
|
let alt_base = sim.get_interpolated(wpos, |chunk| chunk.alt_base)?;
|
|
|
|
let chaos = sim.get_interpolated(wpos, |chunk| chunk.chaos)?;
|
|
|
|
let temp = sim.get_interpolated(wpos, |chunk| chunk.temp)?;
|
2019-06-19 16:18:56 +00:00
|
|
|
let dryness = sim.get_interpolated(wpos, |chunk| chunk.dryness)?;
|
2019-06-09 10:24:18 +00:00
|
|
|
let rockiness = sim.get_interpolated(wpos, |chunk| chunk.rockiness)?;
|
|
|
|
let tree_density = sim.get_interpolated(wpos, |chunk| chunk.tree_density)?;
|
|
|
|
|
2019-06-18 21:22:31 +00:00
|
|
|
let sim_chunk = sim.get(chunk_pos)?;
|
2019-06-11 18:39:25 +00:00
|
|
|
|
2019-06-19 19:58:56 +00:00
|
|
|
const RIVER_PROPORTION: f32 = 0.025;
|
|
|
|
|
|
|
|
let river = dryness
|
|
|
|
.abs()
|
|
|
|
.neg()
|
|
|
|
.add(RIVER_PROPORTION)
|
|
|
|
.div(RIVER_PROPORTION)
|
|
|
|
.max(0.0)
|
|
|
|
.mul((1.0 - (chaos - 0.15) * 20.0).max(0.0).min(1.0));
|
|
|
|
|
2019-06-21 00:53:11 +00:00
|
|
|
let cliff_hill = (sim.gen_ctx.small_nz.get((wposf.div(128.0)).into_array()) as f32)
|
2019-06-21 12:32:38 +00:00
|
|
|
.mul(16.0);
|
2019-06-21 00:53:11 +00:00
|
|
|
|
2019-06-19 19:58:56 +00:00
|
|
|
let riverless_alt = sim.get_interpolated(wpos, |chunk| chunk.alt)?
|
2019-06-18 11:33:18 +00:00
|
|
|
+ (sim.gen_ctx.small_nz.get((wposf.div(256.0)).into_array()) as f32)
|
|
|
|
.abs()
|
|
|
|
.mul(chaos.max(0.2))
|
2019-06-19 19:58:56 +00:00
|
|
|
.mul(64.0);
|
|
|
|
|
2019-06-21 00:53:11 +00:00
|
|
|
let cliffs = sim_chunk.cliffs;
|
|
|
|
|
2019-06-19 19:58:56 +00:00
|
|
|
let alt = riverless_alt
|
|
|
|
- (1.0 - river)
|
|
|
|
.mul(f32::consts::PI)
|
|
|
|
.cos()
|
|
|
|
.add(1.0)
|
|
|
|
.mul(0.5)
|
|
|
|
.mul(24.0);
|
|
|
|
|
|
|
|
let water_level = (riverless_alt - 4.0 - 5.0 * chaos).max(CONFIG.sea_level);
|
2019-06-09 10:24:18 +00:00
|
|
|
|
2019-06-15 10:36:26 +00:00
|
|
|
let rock = (sim.gen_ctx.small_nz.get(
|
|
|
|
Vec3::new(wposf.x, wposf.y, alt as f64)
|
|
|
|
.div(100.0)
|
|
|
|
.into_array(),
|
|
|
|
) as f32)
|
2019-06-09 10:24:18 +00:00
|
|
|
.mul(rockiness)
|
2019-06-10 10:50:48 +00:00
|
|
|
.sub(0.4)
|
2019-06-09 10:24:18 +00:00
|
|
|
.max(0.0)
|
2019-06-10 10:50:48 +00:00
|
|
|
.mul(8.0);
|
2019-06-09 10:24:18 +00:00
|
|
|
|
|
|
|
let wposf3d = Vec3::new(wposf.x, wposf.y, alt as f64);
|
|
|
|
|
2019-06-10 10:50:48 +00:00
|
|
|
let marble = (0.0
|
|
|
|
+ (sim.gen_ctx.hill_nz.get((wposf3d.div(48.0)).into_array()) as f32).mul(0.75)
|
2019-06-10 14:22:59 +00:00
|
|
|
+ (sim.gen_ctx.hill_nz.get((wposf3d.div(3.0)).into_array()) as f32).mul(0.25))
|
2019-06-15 10:36:26 +00:00
|
|
|
.add(1.0)
|
|
|
|
.mul(0.5);
|
2019-06-09 10:24:18 +00:00
|
|
|
|
|
|
|
// Colours
|
2019-06-18 11:33:18 +00:00
|
|
|
let cold_grass = Rgb::new(0.0, 0.3, 0.15);
|
|
|
|
let warm_grass = Rgb::new(0.2, 0.8, 0.05);
|
2019-06-09 10:24:18 +00:00
|
|
|
let cold_stone = Rgb::new(0.55, 0.7, 0.75);
|
|
|
|
let warm_stone = Rgb::new(0.65, 0.65, 0.35);
|
2019-06-10 14:22:59 +00:00
|
|
|
let beach_sand = Rgb::new(0.93, 0.84, 0.4);
|
|
|
|
let desert_sand = Rgb::new(0.98, 0.8, 0.15);
|
2019-06-11 18:39:25 +00:00
|
|
|
let snow = Rgb::broadcast(1.0);
|
2019-06-09 10:24:18 +00:00
|
|
|
|
|
|
|
let grass = Rgb::lerp(cold_grass, warm_grass, marble);
|
2019-06-10 14:22:59 +00:00
|
|
|
let sand = Rgb::lerp(beach_sand, desert_sand, marble);
|
2019-06-09 10:24:18 +00:00
|
|
|
let cliff = Rgb::lerp(cold_stone, warm_stone, marble);
|
|
|
|
|
|
|
|
let ground = Rgb::lerp(
|
2019-06-10 14:22:59 +00:00
|
|
|
Rgb::lerp(
|
|
|
|
snow,
|
|
|
|
grass,
|
2019-06-15 10:36:26 +00:00
|
|
|
temp.sub(CONFIG.snow_temp)
|
|
|
|
.sub((marble - 0.5) * 0.05)
|
|
|
|
.mul(256.0),
|
2019-06-10 14:22:59 +00:00
|
|
|
),
|
|
|
|
sand,
|
2019-06-11 18:39:25 +00:00
|
|
|
temp.sub(CONFIG.desert_temp).mul(32.0),
|
2019-06-09 10:24:18 +00:00
|
|
|
);
|
|
|
|
|
2019-06-22 21:44:27 +00:00
|
|
|
// Work out if we're on a path
|
|
|
|
let near_0 = sim_chunk.location.as_ref().map(|l| l.near[0].block_pos).unwrap_or(Vec2::zero()).map(|e| e as f32);
|
|
|
|
let near_1 = sim_chunk.location.as_ref().map(|l| l.near[1].block_pos).unwrap_or(Vec2::zero()).map(|e| e as f32);
|
|
|
|
|
|
|
|
let dist_to_path = (0.0
|
2019-06-22 23:08:21 +00:00
|
|
|
+ (near_1.y - near_0.y) * wposf_turb.x as f32
|
|
|
|
- (near_1.x - near_0.x) * wposf_turb.y as f32
|
2019-06-22 21:44:27 +00:00
|
|
|
+ near_1.x * near_0.y
|
|
|
|
- near_0.x * near_1.y)
|
|
|
|
.abs()
|
|
|
|
.div(near_0.distance(near_1));
|
|
|
|
|
2019-06-22 22:40:41 +00:00
|
|
|
let (alt, ground) = if dist_to_path < 5.0 {
|
2019-06-22 23:13:59 +00:00
|
|
|
(alt - 1.5, Lerp::lerp(
|
|
|
|
Rgb::new(0.25, 0.15, 0.05),
|
|
|
|
Rgb::new(0.4, 0.25, 0.0),
|
|
|
|
marble,
|
|
|
|
))
|
2019-06-22 21:44:27 +00:00
|
|
|
} else {
|
2019-06-22 22:40:41 +00:00
|
|
|
(alt, ground)
|
2019-06-22 21:44:27 +00:00
|
|
|
};
|
|
|
|
|
2019-06-09 10:24:18 +00:00
|
|
|
// Caves
|
|
|
|
let cave_at = |wposf: Vec2<f64>| {
|
|
|
|
(sim.gen_ctx.cave_0_nz.get(
|
|
|
|
Vec3::new(wposf.x, wposf.y, alt as f64 * 8.0)
|
|
|
|
.div(800.0)
|
|
|
|
.into_array(),
|
|
|
|
) as f32)
|
|
|
|
.powf(2.0)
|
|
|
|
.neg()
|
|
|
|
.add(1.0)
|
|
|
|
.mul((1.15 - chaos).min(1.0))
|
|
|
|
};
|
|
|
|
let cave_xy = cave_at(wposf);
|
2019-06-21 12:32:38 +00:00
|
|
|
let cave_alt = alt - 24.0
|
2019-06-09 10:24:18 +00:00
|
|
|
+ (sim
|
|
|
|
.gen_ctx
|
|
|
|
.cave_1_nz
|
|
|
|
.get(Vec2::new(wposf.x, wposf.y).div(48.0).into_array()) as f32)
|
|
|
|
* 8.0
|
|
|
|
+ (sim
|
|
|
|
.gen_ctx
|
|
|
|
.cave_1_nz
|
2019-06-21 12:32:38 +00:00
|
|
|
.get(Vec2::new(wposf.x, wposf.y).div(500.0).into_array()) as f32)
|
2019-06-09 10:24:18 +00:00
|
|
|
.add(1.0)
|
|
|
|
.mul(0.5)
|
2019-06-21 12:32:38 +00:00
|
|
|
.powf(15.0)
|
|
|
|
.mul(150.0);
|
2019-06-09 10:24:18 +00:00
|
|
|
|
|
|
|
Some(ColumnSample {
|
|
|
|
alt,
|
|
|
|
chaos,
|
2019-06-19 19:58:56 +00:00
|
|
|
water_level,
|
|
|
|
river,
|
2019-06-09 10:24:18 +00:00
|
|
|
surface_color: Rgb::lerp(
|
2019-06-10 14:22:59 +00:00
|
|
|
sand,
|
2019-06-09 10:24:18 +00:00
|
|
|
// Land
|
|
|
|
Rgb::lerp(
|
|
|
|
ground,
|
|
|
|
// Mountain
|
|
|
|
Rgb::lerp(
|
|
|
|
cliff,
|
|
|
|
snow,
|
|
|
|
(alt - CONFIG.sea_level
|
2019-06-09 21:58:09 +00:00
|
|
|
- 0.35 * CONFIG.mountain_scale
|
2019-06-09 10:24:18 +00:00
|
|
|
- alt_base
|
|
|
|
- temp * 96.0
|
|
|
|
- marble * 24.0)
|
|
|
|
/ 12.0,
|
|
|
|
),
|
2019-06-21 08:08:20 +00:00
|
|
|
(alt - CONFIG.sea_level - 0.3 * CONFIG.mountain_scale + marble * 128.0) / 100.0,
|
2019-06-09 10:24:18 +00:00
|
|
|
),
|
|
|
|
// Beach
|
2019-06-19 19:58:56 +00:00
|
|
|
((alt - CONFIG.sea_level - 2.0) / 5.0).min(1.0 - river * 2.0),
|
2019-06-09 10:24:18 +00:00
|
|
|
),
|
|
|
|
tree_density,
|
2019-06-18 21:22:31 +00:00
|
|
|
forest_kind: sim_chunk.forest_kind,
|
2019-06-09 10:24:18 +00:00
|
|
|
close_trees: sim.gen_ctx.tree_gen.get(wpos),
|
|
|
|
cave_xy,
|
|
|
|
cave_alt,
|
|
|
|
rock,
|
2019-06-21 00:53:11 +00:00
|
|
|
cliffs,
|
|
|
|
cliff_hill,
|
|
|
|
close_cliffs: sim.gen_ctx.cliff_gen.get(wpos),
|
2019-06-12 20:22:16 +00:00
|
|
|
temp,
|
2019-06-18 21:22:31 +00:00
|
|
|
location: sim_chunk.location.as_ref(),
|
2019-06-09 10:24:18 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone)]
|
2019-06-18 21:22:31 +00:00
|
|
|
pub struct ColumnSample<'a> {
|
2019-06-09 10:24:18 +00:00
|
|
|
pub alt: f32,
|
|
|
|
pub chaos: f32,
|
2019-06-19 19:58:56 +00:00
|
|
|
pub water_level: f32,
|
|
|
|
pub river: f32,
|
2019-06-09 10:24:18 +00:00
|
|
|
pub surface_color: Rgb<f32>,
|
|
|
|
pub tree_density: f32,
|
2019-06-11 18:39:25 +00:00
|
|
|
pub forest_kind: ForestKind,
|
2019-06-09 10:24:18 +00:00
|
|
|
pub close_trees: [(Vec2<i32>, u32); 9],
|
|
|
|
pub cave_xy: f32,
|
|
|
|
pub cave_alt: f32,
|
|
|
|
pub rock: f32,
|
2019-06-21 00:53:11 +00:00
|
|
|
pub cliffs: bool,
|
|
|
|
pub cliff_hill: f32,
|
|
|
|
pub close_cliffs: [(Vec2<i32>, u32); 9],
|
2019-06-12 20:22:16 +00:00
|
|
|
pub temp: f32,
|
2019-06-22 21:44:27 +00:00
|
|
|
pub location: Option<&'a LocationInfo>,
|
2019-06-09 10:24:18 +00:00
|
|
|
}
|