2019-06-22 21:44:27 +00:00
|
|
|
#![feature(euclidean_division, bind_by_move_pattern_guards, option_flattening)]
|
2019-05-23 20:41:01 +00:00
|
|
|
|
2019-06-11 18:39:25 +00:00
|
|
|
mod all;
|
2019-06-09 10:24:18 +00:00
|
|
|
mod block;
|
|
|
|
mod column;
|
2019-06-15 12:34:28 +00:00
|
|
|
pub mod config;
|
2019-06-18 21:22:31 +00:00
|
|
|
pub mod sim;
|
2019-06-15 12:34:28 +00:00
|
|
|
pub mod util;
|
2019-06-09 10:24:18 +00:00
|
|
|
|
|
|
|
// Reexports
|
|
|
|
pub use crate::config::CONFIG;
|
2019-05-18 08:59:58 +00:00
|
|
|
|
2019-06-15 10:36:26 +00:00
|
|
|
use crate::{
|
|
|
|
block::BlockGen,
|
2019-06-15 12:34:28 +00:00
|
|
|
column::{ColumnGen, ColumnSample},
|
2019-07-01 18:40:41 +00:00
|
|
|
util::{Sampler, SamplerMut},
|
2019-06-15 10:36:26 +00:00
|
|
|
};
|
2019-01-15 15:13:11 +00:00
|
|
|
use common::{
|
2019-05-17 17:54:56 +00:00
|
|
|
terrain::{Block, TerrainChunk, TerrainChunkMeta, TerrainChunkSize},
|
2019-08-02 14:37:26 +00:00
|
|
|
vol::{ReadVol, VolSize, Vox, WriteVol},
|
2019-01-15 15:13:11 +00:00
|
|
|
};
|
2019-08-02 14:37:26 +00:00
|
|
|
use rand::Rng;
|
2019-06-09 10:24:18 +00:00
|
|
|
use std::time::Duration;
|
2019-05-21 22:31:38 +00:00
|
|
|
use vek::*;
|
2019-01-15 15:13:11 +00:00
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum Error {
|
|
|
|
Other(String),
|
|
|
|
}
|
|
|
|
|
2019-05-18 08:59:58 +00:00
|
|
|
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 {
|
2019-05-21 22:31:38 +00:00
|
|
|
Self {
|
|
|
|
sim: sim::WorldSim::generate(seed),
|
|
|
|
}
|
2019-05-18 08:59:58 +00:00
|
|
|
}
|
|
|
|
|
2019-05-21 00:57:16 +00:00
|
|
|
pub fn sim(&self) -> &sim::WorldSim {
|
|
|
|
&self.sim
|
|
|
|
}
|
|
|
|
|
2019-07-01 18:40:41 +00:00
|
|
|
pub fn tick(&self, _dt: Duration) {
|
2019-05-18 08:59:58 +00:00
|
|
|
// TODO
|
2019-01-15 15:13:11 +00:00
|
|
|
}
|
|
|
|
|
2019-06-15 12:34:28 +00:00
|
|
|
pub fn sample_columns(
|
|
|
|
&self,
|
|
|
|
) -> impl Sampler<Index = Vec2<i32>, Sample = Option<ColumnSample>> + '_ {
|
|
|
|
ColumnGen::new(self)
|
|
|
|
}
|
|
|
|
|
2019-07-04 23:14:55 +00:00
|
|
|
pub fn sample_blocks(&self) -> BlockGen {
|
2019-06-09 10:24:18 +00:00
|
|
|
BlockGen::new(self, ColumnGen::new(self))
|
|
|
|
}
|
2019-01-23 22:21:47 +00:00
|
|
|
|
2019-08-02 14:37:26 +00:00
|
|
|
pub fn generate_chunk(&self, chunk_pos: Vec2<i32>) -> (TerrainChunk, ChunkSupplement) {
|
2019-01-23 20:01:58 +00:00
|
|
|
let air = Block::empty();
|
2019-06-29 21:09:36 +00:00
|
|
|
let stone = Block::new(2, Rgb::new(200, 220, 255));
|
2019-05-21 00:57:16 +00:00
|
|
|
let water = Block::new(5, Rgb::new(100, 150, 255));
|
2019-01-23 20:01:58 +00:00
|
|
|
|
2019-06-04 17:19:40 +00:00
|
|
|
let chunk_size2d = Vec2::from(TerrainChunkSize::SIZE);
|
2019-06-19 14:55:26 +00:00
|
|
|
let (base_z, sim_chunk) = match self
|
|
|
|
.sim
|
|
|
|
.get_interpolated(
|
|
|
|
chunk_pos.map2(chunk_size2d, |e, sz: u32| e * sz as i32 + sz as i32 / 2),
|
|
|
|
|chunk| chunk.get_base_z(),
|
|
|
|
)
|
|
|
|
.and_then(|base_z| self.sim.get(chunk_pos).map(|sim_chunk| (base_z, sim_chunk)))
|
|
|
|
{
|
2019-06-18 21:22:31 +00:00
|
|
|
Some((base_z, sim_chunk)) => (base_z as i32, sim_chunk),
|
2019-07-09 20:42:27 +00:00
|
|
|
None => {
|
2019-08-02 14:37:26 +00:00
|
|
|
return (
|
|
|
|
TerrainChunk::new(
|
|
|
|
CONFIG.sea_level as i32,
|
|
|
|
water,
|
|
|
|
air,
|
|
|
|
TerrainChunkMeta::void(),
|
|
|
|
),
|
|
|
|
ChunkSupplement::default(),
|
2019-07-09 20:42:27 +00:00
|
|
|
)
|
|
|
|
}
|
2019-05-19 14:54:37 +00:00
|
|
|
};
|
2019-05-17 21:19:32 +00:00
|
|
|
|
2019-06-25 15:59:09 +00:00
|
|
|
let meta = TerrainChunkMeta::new(sim_chunk.get_name(&self.sim), sim_chunk.get_biome());
|
2019-06-15 12:34:28 +00:00
|
|
|
let mut sampler = self.sample_blocks();
|
2019-05-24 11:08:38 +00:00
|
|
|
|
2019-08-02 14:37:26 +00:00
|
|
|
let chunk_block_pos = Vec3::from(chunk_pos) * TerrainChunkSize::SIZE.map(|e| e as i32);
|
|
|
|
|
|
|
|
let mut chunk = TerrainChunk::new(base_z, stone, air, meta);
|
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-21 22:31:38 +00:00
|
|
|
let wpos2d = Vec2::new(x, y)
|
|
|
|
+ Vec3::from(chunk_pos) * TerrainChunkSize::SIZE.map(|e| e as i32);
|
2019-05-20 02:53:04 +00:00
|
|
|
|
2019-07-08 14:51:38 +00:00
|
|
|
let z_cache = match sampler.get_z_cache(wpos2d) {
|
|
|
|
Some(z_cache) => z_cache,
|
|
|
|
None => continue,
|
|
|
|
};
|
2019-07-04 23:14:55 +00:00
|
|
|
|
2019-07-08 14:51:38 +00:00
|
|
|
let (min_z, max_z) = z_cache.get_z_limits();
|
|
|
|
|
2019-07-08 16:41:20 +00:00
|
|
|
for z in base_z..min_z as i32 {
|
2019-07-08 14:51:38 +00:00
|
|
|
let _ = chunk.set(Vec3::new(x, y, z), stone);
|
|
|
|
}
|
|
|
|
|
|
|
|
for z in min_z as i32..max_z as i32 {
|
2019-05-17 17:44:30 +00:00
|
|
|
let lpos = Vec3::new(x, y, z);
|
2019-08-02 14:37:26 +00:00
|
|
|
let wpos = chunk_block_pos + lpos;
|
2019-05-24 11:08:38 +00:00
|
|
|
|
2019-07-08 14:51:38 +00:00
|
|
|
if let Some(block) = sampler.get_with_z_cache(wpos, Some(&z_cache)) {
|
2019-06-09 10:24:18 +00:00
|
|
|
let _ = chunk.set(lpos, block);
|
|
|
|
}
|
2019-05-17 17:44:30 +00:00
|
|
|
}
|
|
|
|
}
|
2019-01-23 20:01:58 +00:00
|
|
|
}
|
|
|
|
|
2019-08-02 14:37:26 +00:00
|
|
|
let gen_entity_pos = || {
|
|
|
|
let lpos2d = Vec2::from(TerrainChunkSize::SIZE)
|
|
|
|
.map(|sz| rand::thread_rng().gen::<u32>().rem_euclid(sz));
|
|
|
|
let mut lpos = Vec3::new(lpos2d.x as i32, lpos2d.y as i32, 0);
|
|
|
|
|
|
|
|
while chunk.get(lpos).map(|vox| !vox.is_empty()).unwrap_or(false) {
|
|
|
|
lpos.z += 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
(chunk_block_pos + lpos).map(|e| e as f32) + 0.5
|
|
|
|
};
|
|
|
|
|
|
|
|
const SPAWN_RATE: f32 = 0.1;
|
2019-08-04 09:27:08 +00:00
|
|
|
const BOSS_RATE: f32 = 0.03;
|
2019-08-02 14:37:26 +00:00
|
|
|
let supplement = ChunkSupplement {
|
2019-08-02 17:48:14 +00:00
|
|
|
npcs: if rand::thread_rng().gen::<f32>() < SPAWN_RATE && sim_chunk.chaos < 0.5 {
|
2019-08-02 14:37:26 +00:00
|
|
|
vec![NpcInfo {
|
|
|
|
pos: gen_entity_pos(),
|
2019-08-02 18:56:37 +00:00
|
|
|
boss: rand::thread_rng().gen::<f32>() < BOSS_RATE,
|
2019-08-02 14:37:26 +00:00
|
|
|
}]
|
|
|
|
} else {
|
|
|
|
Vec::new()
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
(chunk, supplement)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub struct NpcInfo {
|
|
|
|
pub pos: Vec3<f32>,
|
2019-08-02 18:56:37 +00:00
|
|
|
pub boss: bool,
|
2019-08-02 14:37:26 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
pub struct ChunkSupplement {
|
|
|
|
pub npcs: Vec<NpcInfo>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for ChunkSupplement {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self { npcs: Vec::new() }
|
2019-05-23 20:41:01 +00:00
|
|
|
}
|
|
|
|
}
|