2019-05-20 02:53:04 +00:00
|
|
|
use std::{
|
2019-05-20 15:01:27 +00:00
|
|
|
ops::{Add, Mul, Div, Neg},
|
2019-05-20 02:53:04 +00:00
|
|
|
f32,
|
|
|
|
};
|
2019-05-20 15:01:27 +00:00
|
|
|
use noise::{NoiseFn, SuperSimplex, OpenSimplex, Seedable};
|
2019-05-16 17:40:32 +00:00
|
|
|
use vek::*;
|
|
|
|
use common::{
|
|
|
|
terrain::TerrainChunkSize,
|
|
|
|
vol::VolSize,
|
|
|
|
};
|
2019-05-20 02:53:04 +00:00
|
|
|
|
|
|
|
pub const WORLD_SIZE: Vec2<usize> = Vec2 { x: 1024, y: 1024 };
|
2019-05-18 08:59:58 +00:00
|
|
|
|
|
|
|
pub struct WorldSim {
|
2019-05-20 02:53:04 +00:00
|
|
|
pub seed: u32,
|
2019-05-18 08:59:58 +00:00
|
|
|
chunks: Vec<SimChunk>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl WorldSim {
|
|
|
|
pub fn generate(seed: u32) -> Self {
|
|
|
|
let mut gen_ctx = GenCtx {
|
2019-05-20 15:01:27 +00:00
|
|
|
base_nz: OpenSimplex::new()
|
2019-05-20 02:53:04 +00:00
|
|
|
.set_seed(seed + 0),
|
2019-05-20 15:01:27 +00:00
|
|
|
damp_nz: SuperSimplex::new()
|
2019-05-20 02:53:04 +00:00
|
|
|
.set_seed(seed + 1),
|
2019-05-20 15:01:27 +00:00
|
|
|
chaos_nz: SuperSimplex::new()
|
|
|
|
.set_seed(seed + 2),
|
|
|
|
alt_nz: OpenSimplex::new()
|
|
|
|
.set_seed(seed + 3),
|
2019-05-18 08:59:58 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let mut chunks = Vec::new();
|
2019-05-16 17:40:32 +00:00
|
|
|
for x in 0..WORLD_SIZE.x as u32 {
|
|
|
|
for y in 0..WORLD_SIZE.y as u32 {
|
|
|
|
chunks.push(SimChunk::generate(Vec2::new(x, y), &mut gen_ctx));
|
2019-05-18 08:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Self {
|
|
|
|
seed,
|
|
|
|
chunks,
|
|
|
|
}
|
|
|
|
}
|
2019-05-16 17:40:32 +00:00
|
|
|
|
|
|
|
pub fn get(&self, chunk_pos: Vec2<u32>) -> Option<&SimChunk> {
|
|
|
|
if chunk_pos.map2(WORLD_SIZE, |e, sz| e < sz as u32).reduce_and() {
|
|
|
|
Some(&self.chunks[chunk_pos.y as usize * WORLD_SIZE.x + chunk_pos.x as usize])
|
|
|
|
} else {
|
|
|
|
None
|
|
|
|
}
|
|
|
|
}
|
2019-05-20 02:53:04 +00:00
|
|
|
|
|
|
|
pub fn get_base_z(&self, chunk_pos: Vec2<u32>) -> Option<f32> {
|
|
|
|
self
|
|
|
|
.get(chunk_pos)
|
|
|
|
.and_then(|_| (0..2)
|
|
|
|
.map(|i| (0..2)
|
|
|
|
.map(move |j| (i, j)))
|
|
|
|
.flatten()
|
|
|
|
.map(|(i, j)| self
|
|
|
|
.get(chunk_pos + Vec2::new(i, j))
|
|
|
|
.map(|c| c.get_base_z()))
|
|
|
|
.flatten()
|
|
|
|
.fold(None, |a: Option<f32>, x| a.map(|a| a.min(x)).or(Some(x))))
|
|
|
|
}
|
|
|
|
|
2019-05-20 15:01:27 +00:00
|
|
|
pub fn get_interpolated<T, F>(&self, pos: Vec2<i32>, mut f: F) -> Option<T>
|
|
|
|
where
|
|
|
|
T: Copy + Default + Add<Output=T> + Mul<f32, Output=T>,
|
|
|
|
F: FnMut(&SimChunk) -> T,
|
|
|
|
{
|
2019-05-20 02:53:04 +00:00
|
|
|
let pos = pos.map2(TerrainChunkSize::SIZE.into(), |e, sz: u32| e as f64 / sz as f64);
|
|
|
|
|
2019-05-20 15:01:27 +00:00
|
|
|
let cubic = |a: T, b: T, c: T, d: T, x: f32| -> T {
|
2019-05-20 02:53:04 +00:00
|
|
|
let x2 = x * x;
|
|
|
|
|
|
|
|
// Catmull-Rom splines
|
2019-05-20 15:01:27 +00:00
|
|
|
let co0 = a * -0.5 + b * 1.5 + c * -1.5 + d * 0.5;
|
|
|
|
let co1 = a + b * -2.5 + c * 2.0 + d * -0.5;
|
|
|
|
let co2 = a * -0.5 + c * 0.5;
|
2019-05-20 02:53:04 +00:00
|
|
|
let co3 = b;
|
|
|
|
|
|
|
|
co0 * x2 * x + co1 * x2 + co2 * x + co3
|
2019-05-20 15:01:27 +00:00
|
|
|
};
|
2019-05-20 02:53:04 +00:00
|
|
|
|
2019-05-20 15:01:27 +00:00
|
|
|
let mut y = [T::default(); 4];
|
2019-05-20 02:53:04 +00:00
|
|
|
|
|
|
|
for (y_idx, j) in (-1..3).enumerate() {
|
|
|
|
let x0 = f(self.get(pos.map2(Vec2::new(-1, j), |e, q| (e.max(0.0) as i32 + q) as u32))?);
|
|
|
|
let x1 = f(self.get(pos.map2(Vec2::new( 0, j), |e, q| (e.max(0.0) as i32 + q) as u32))?);
|
|
|
|
let x2 = f(self.get(pos.map2(Vec2::new( 1, j), |e, q| (e.max(0.0) as i32 + q) as u32))?);
|
|
|
|
let x3 = f(self.get(pos.map2(Vec2::new( 2, j), |e, q| (e.max(0.0) as i32 + q) as u32))?);
|
|
|
|
|
|
|
|
y[y_idx] = cubic(x0, x1, x2, x3, pos.x.fract() as f32);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
fn cosine_interp (a: f32, b: f32, x: f32) -> f32 {
|
|
|
|
let x2 = x;//(1.0 - (x * f32::consts::PI).cos()) / 2.0;
|
|
|
|
a * (1.0 - x2) + b * x2
|
|
|
|
}
|
|
|
|
*/
|
|
|
|
|
|
|
|
Some(cubic(y[0], y[1], y[2], y[3], pos.y.fract() as f32))
|
|
|
|
}
|
2019-05-18 08:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
struct GenCtx {
|
2019-05-20 15:01:27 +00:00
|
|
|
base_nz: OpenSimplex,
|
|
|
|
damp_nz: SuperSimplex,
|
|
|
|
chaos_nz: SuperSimplex,
|
2019-05-18 08:59:58 +00:00
|
|
|
alt_nz: OpenSimplex,
|
|
|
|
}
|
|
|
|
|
2019-05-20 15:01:27 +00:00
|
|
|
const Z_TOLERANCE: f32 = 32.0;
|
|
|
|
|
2019-05-16 17:40:32 +00:00
|
|
|
pub struct SimChunk {
|
2019-05-20 15:01:27 +00:00
|
|
|
pub damp: f32,
|
2019-05-20 02:53:04 +00:00
|
|
|
pub chaos: f32,
|
2019-05-16 17:40:32 +00:00
|
|
|
pub alt: f32,
|
2019-05-18 08:59:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
impl SimChunk {
|
2019-05-16 17:40:32 +00:00
|
|
|
fn generate(pos: Vec2<u32>, gen_ctx: &mut GenCtx) -> Self {
|
|
|
|
let wposf = (pos * Vec2::from(TerrainChunkSize::SIZE)).map(|e| e as f64);
|
|
|
|
|
2019-05-20 15:01:27 +00:00
|
|
|
let base = (gen_ctx.base_nz.get((wposf.div(6000.0)).into_array()) as f32)
|
|
|
|
.add(1.0).mul(0.5)
|
|
|
|
.mul(100.0)
|
|
|
|
.add(32.0);
|
|
|
|
|
|
|
|
let damp = (0.0
|
|
|
|
+ gen_ctx.damp_nz.get((wposf.div(2000.0)).into_array())
|
|
|
|
+ gen_ctx.damp_nz.get((wposf.div(250.0)).into_array()) * 0.25
|
|
|
|
) as f32;
|
|
|
|
|
2019-05-20 02:53:04 +00:00
|
|
|
let chaos = (gen_ctx.chaos_nz
|
2019-05-20 15:01:27 +00:00
|
|
|
.get((wposf.div(1000.0)).into_array()) as f32)
|
|
|
|
.add(1.0).mul(0.5)
|
|
|
|
.mul(damp.max(0.0))
|
2019-05-20 02:53:04 +00:00
|
|
|
.add(0.15)
|
2019-05-20 15:01:27 +00:00
|
|
|
.powf(2.0)
|
|
|
|
.min(1.0);
|
2019-05-20 02:53:04 +00:00
|
|
|
|
2019-05-18 08:59:58 +00:00
|
|
|
Self {
|
2019-05-20 15:01:27 +00:00
|
|
|
damp,
|
2019-05-20 02:53:04 +00:00
|
|
|
chaos,
|
2019-05-20 15:01:27 +00:00
|
|
|
alt: base + ((0.0
|
|
|
|
+ gen_ctx.alt_nz.get((wposf.div(650.0)).into_array()) * 0.7
|
|
|
|
+ gen_ctx.alt_nz.get((wposf.div(100.0)).into_array()) * 0.3
|
|
|
|
) as f32)
|
2019-05-20 02:53:04 +00:00
|
|
|
.add(1.0).mul(0.5)
|
|
|
|
.mul(750.0)
|
|
|
|
.mul(chaos.max(0.05)),
|
2019-05-18 08:59:58 +00:00
|
|
|
}
|
|
|
|
}
|
2019-05-20 02:53:04 +00:00
|
|
|
|
|
|
|
pub fn get_base_z(&self) -> f32 {
|
2019-05-20 15:01:27 +00:00
|
|
|
self.alt - Z_TOLERANCE
|
|
|
|
}
|
2019-05-20 02:53:04 +00:00
|
|
|
|
2019-05-20 15:01:27 +00:00
|
|
|
pub fn get_max_z(&self) -> f32 {
|
|
|
|
self.alt + Z_TOLERANCE
|
2019-05-20 02:53:04 +00:00
|
|
|
}
|
2019-05-18 08:59:58 +00:00
|
|
|
}
|