2019-05-18 08:59:58 +00:00
|
|
|
mod sim;
|
|
|
|
|
|
|
|
use std::time::Duration;
|
2019-05-01 11:28:26 +00:00
|
|
|
use noise::{NoiseFn, Perlin, Seedable};
|
2019-04-29 20:37:19 +00:00
|
|
|
use vek::*;
|
2019-01-15 15:13:11 +00:00
|
|
|
use common::{
|
2019-05-17 17:54:56 +00:00
|
|
|
terrain::{Block, TerrainChunk, TerrainChunkMeta, TerrainChunkSize},
|
|
|
|
vol::{SizedVol, VolSize, Vox, WriteVol},
|
2019-01-15 15:13:11 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Other(String),
|
|
|
|
}
|
|
|
|
|
2019-05-18 08:59:58 +00:00
|
|
|
pub const WORLD_SIZE: Vec2<usize> = Vec2 { x: 1024, y: 1024 };
|
|
|
|
|
|
|
|
pub struct World {
|
|
|
|
sim: sim::WorldSim,
|
|
|
|
}
|
2019-01-15 15:13:11 +00:00
|
|
|
|
|
|
|
impl World {
|
2019-05-18 08:59:58 +00:00
|
|
|
pub fn generate(seed: u32) -> Self {
|
|
|
|
Self { sim: sim::WorldSim::generate(seed) }
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn tick(&mut self, dt: Duration) {
|
|
|
|
// TODO
|
2019-01-15 15:13:11 +00:00
|
|
|
}
|
|
|
|
|
2019-05-17 17:44:30 +00:00
|
|
|
pub fn generate_chunk(chunk_pos: Vec2<i32>) -> TerrainChunk {
|
2019-05-17 09:22:32 +00:00
|
|
|
// TODO: This is all test code, remove/improve this later.
|
2019-01-23 22:21:47 +00:00
|
|
|
|
2019-01-23 20:01:58 +00:00
|
|
|
let air = Block::empty();
|
2019-04-29 20:37:19 +00:00
|
|
|
let stone = Block::new(1, Rgb::new(200, 220, 255));
|
2019-04-25 21:43:32 +00:00
|
|
|
let grass = Block::new(2, Rgb::new(75, 150, 0));
|
2019-04-25 14:16:10 +00:00
|
|
|
let dirt = Block::new(3, Rgb::new(128, 90, 0));
|
|
|
|
let sand = Block::new(4, Rgb::new(180, 150, 50));
|
2019-01-23 20:01:58 +00:00
|
|
|
|
2019-05-17 21:19:32 +00:00
|
|
|
let offset_nz = Perlin::new().set_seed(3);
|
2019-05-09 17:58:16 +00:00
|
|
|
let perlin_nz = Perlin::new().set_seed(1);
|
|
|
|
let temp_nz = Perlin::new().set_seed(2);
|
|
|
|
let chaos_nz = Perlin::new().set_seed(3);
|
2019-01-23 20:01:58 +00:00
|
|
|
|
2019-05-17 21:19:32 +00:00
|
|
|
let get_offset = |pos: Vec2<i32>| {
|
|
|
|
((offset_nz.get(pos.map(|e| e as f64 / 4000.0).into_array()) + 1.0)
|
2019-05-17 23:50:14 +00:00
|
|
|
* 1000.0)
|
2019-05-17 21:19:32 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
let offset_z = get_offset(chunk_pos * Vec2::from(TerrainChunkSize::SIZE).map(|e: u32| e as i32));
|
|
|
|
|
2019-05-17 23:50:14 +00:00
|
|
|
let mut chunk = TerrainChunk::new(offset_z as i32, stone, air, TerrainChunkMeta::void());
|
2019-05-17 21:19:32 +00:00
|
|
|
|
2019-05-17 17:44:30 +00:00
|
|
|
for x in 0..TerrainChunkSize::SIZE.x as i32 {
|
|
|
|
for y in 0..TerrainChunkSize::SIZE.y as i32 {
|
2019-05-17 23:50:14 +00:00
|
|
|
for z in offset_z as i32..offset_z as i32 + 256 {
|
2019-05-17 17:44:30 +00:00
|
|
|
let lpos = Vec3::new(x, y, z);
|
2019-05-17 21:19:32 +00:00
|
|
|
let wpos = lpos
|
|
|
|
+ Vec3::from(chunk_pos) * TerrainChunkSize::SIZE.map(|e| e as i32);
|
2019-05-17 17:44:30 +00:00
|
|
|
let wposf = wpos.map(|e| e as f64);
|
2019-01-23 20:01:58 +00:00
|
|
|
|
2019-05-17 17:44:30 +00:00
|
|
|
let chaos_freq = 1.0 / 100.0;
|
|
|
|
let freq = 1.0 / 128.0;
|
|
|
|
let ampl = 75.0;
|
|
|
|
let small_freq = 1.0 / 32.0;
|
|
|
|
let small_ampl = 6.0;
|
2019-05-17 21:19:32 +00:00
|
|
|
let offs = 128.0;
|
2019-05-06 12:11:03 +00:00
|
|
|
|
2019-05-17 17:44:30 +00:00
|
|
|
let chaos = chaos_nz
|
|
|
|
.get(Vec2::from(wposf * chaos_freq).into_array())
|
|
|
|
.max(0.0)
|
|
|
|
+ 0.5;
|
2019-05-06 12:11:03 +00:00
|
|
|
|
2019-05-17 17:54:56 +00:00
|
|
|
let height =
|
|
|
|
perlin_nz.get(Vec2::from(wposf * freq).into_array()) * ampl * chaos
|
|
|
|
+ perlin_nz.get((wposf * small_freq).into_array())
|
|
|
|
* small_ampl
|
|
|
|
* 3.0
|
|
|
|
* chaos.powf(2.0)
|
2019-05-17 21:19:32 +00:00
|
|
|
+ offs
|
|
|
|
+ get_offset(Vec2::from(wpos)) as f64;
|
2019-05-17 17:54:56 +00:00
|
|
|
let temp =
|
|
|
|
(temp_nz.get(Vec2::from(wposf * (1.0 / 64.0)).into_array()) + 1.0) * 0.5;
|
2019-01-23 20:01:58 +00:00
|
|
|
|
2019-05-17 17:54:56 +00:00
|
|
|
let _ = chunk.set(
|
|
|
|
lpos,
|
|
|
|
if wposf.z < height - 4.0 {
|
|
|
|
stone
|
|
|
|
} else if wposf.z < height - 2.0 {
|
|
|
|
dirt
|
|
|
|
} else if wposf.z < height {
|
|
|
|
Block::new(2, Rgb::new(10 + (150.0 * temp) as u8, 150, 0))
|
|
|
|
} else {
|
|
|
|
air
|
|
|
|
},
|
|
|
|
);
|
2019-05-17 17:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-23 20:01:58 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
chunk
|
2019-01-15 15:13:11 +00:00
|
|
|
}
|
|
|
|
}
|