diff --git a/common/src/terrain/chonk.rs b/common/src/terrain/chonk.rs index cac8e3f67b..adea72ed2f 100644 --- a/common/src/terrain/chonk.rs +++ b/common/src/terrain/chonk.rs @@ -1,19 +1,10 @@ -use vek::*; -use serde_derive::{Deserialize, Serialize}; +use super::{block::Block, TerrainChunkMeta, TerrainChunkSize}; use crate::{ - vol::{ - BaseVol, - ReadVol, - WriteVol, - VolSize, - }, + vol::{BaseVol, ReadVol, VolSize, WriteVol}, volumes::chunk::{Chunk, ChunkErr}, }; -use super::{ - block::Block, - TerrainChunkSize, - TerrainChunkMeta, -}; +use serde_derive::{Deserialize, Serialize}; +use vek::*; #[derive(Debug)] pub enum ChonkError { @@ -51,14 +42,15 @@ impl BaseVol for Chonk { type Err = ChonkError; } - impl ReadVol for Chonk { #[inline(always)] fn get(&self, pos: Vec3) -> Result<&Block, ChonkError> { if pos.z < self.z_offset { // Below the terrain Ok(&self.below) - } else if pos.z >= self.z_offset + TerrainChunkSize::SIZE.z as i32 * self.sub_chunks.len() as i32 { + } else if pos.z + >= self.z_offset + TerrainChunkSize::SIZE.z as i32 * self.sub_chunks.len() as i32 + { // Above the terrain Ok(&self.above) } else { @@ -66,17 +58,16 @@ impl ReadVol for Chonk { let sub_chunk_idx = self.sub_chunk_idx(pos.z); - match &self.sub_chunks[sub_chunk_idx] { // Can't fail + match &self.sub_chunks[sub_chunk_idx] { + // Can't fail SubChunk::Homogeneous(block) => Ok(block), SubChunk::Heterogeneous(chunk) => { - let rpos = pos - Vec3::unit_z() * ( - self.z_offset + - sub_chunk_idx as i32 * TerrainChunkSize::SIZE.z as i32 - ); - chunk - .get(rpos) - .map_err(|err| ChonkError::ChunkError(err)) - }, + let rpos = pos + - Vec3::unit_z() + * (self.z_offset + + sub_chunk_idx as i32 * TerrainChunkSize::SIZE.z as i32); + chunk.get(rpos).map_err(|err| ChonkError::ChunkError(err)) + } } } } @@ -94,30 +85,26 @@ impl WriteVol for Chonk { self.sub_chunks.push(SubChunk::Homogeneous(self.above)); } - let rpos = pos - Vec3::unit_z() * ( - self.z_offset + - sub_chunk_idx as i32 * TerrainChunkSize::SIZE.z as i32 - ); + let rpos = pos + - Vec3::unit_z() + * (self.z_offset + sub_chunk_idx as i32 * TerrainChunkSize::SIZE.z as i32); - match &mut self.sub_chunks[sub_chunk_idx] { // Can't fail + match &mut self.sub_chunks[sub_chunk_idx] { + // Can't fail SubChunk::Homogeneous(cblock) if *cblock == block => Ok(()), SubChunk::Homogeneous(cblock) => { let mut new_chunk = Chunk::filled(*cblock, ()); - match new_chunk - .set(rpos, block) - .map_err(|err| { - println!("Error!! {:?}", rpos); - ChonkError::ChunkError(err) - }) - { + match new_chunk.set(rpos, block).map_err(|err| { + println!("Error!! {:?}", rpos); + ChonkError::ChunkError(err) + }) { Ok(()) => { self.sub_chunks[sub_chunk_idx] = SubChunk::Heterogeneous(new_chunk); Ok(()) - }, + } Err(err) => Err(err), } - - }, + } SubChunk::Heterogeneous(chunk) => chunk .set(rpos, block) .map_err(|err| ChonkError::ChunkError(err)), diff --git a/common/src/volumes/mod.rs b/common/src/volumes/mod.rs index 9190b62193..56a27bcf2e 100644 --- a/common/src/volumes/mod.rs +++ b/common/src/volumes/mod.rs @@ -1,4 +1,4 @@ pub mod chunk; pub mod dyna; -pub mod vol_map_3d; pub mod vol_map_2d; +pub mod vol_map_3d; diff --git a/common/src/volumes/vol_map_2d.rs b/common/src/volumes/vol_map_2d.rs index 25e4fbc686..79db65cee4 100644 --- a/common/src/volumes/vol_map_2d.rs +++ b/common/src/volumes/vol_map_2d.rs @@ -1,9 +1,3 @@ -use std::{ - collections::HashMap, - sync::Arc, - marker::PhantomData, -}; -use vek::*; use crate::{ terrain::TerrainChunkMeta, vol::{BaseVol, ReadVol, SampleVol, SizedVol, VolSize, Vox, WriteVol}, @@ -12,6 +6,8 @@ use crate::{ dyna::{Dyna, DynaErr}, }, }; +use std::{collections::HashMap, marker::PhantomData, sync::Arc}; +use vek::*; #[derive(Debug)] pub enum VolMap2dErr { @@ -134,11 +130,7 @@ impl VolMap2d { S::SIZE.into() } - pub fn insert( - &mut self, - key: Vec2, - chunk: Arc, - ) -> Option> { + pub fn insert(&mut self, key: Vec2, chunk: Arc) -> Option> { self.chunks.insert(key, chunk) } diff --git a/common/src/volumes/vol_map_3d.rs b/common/src/volumes/vol_map_3d.rs index 024bcc5a30..3d8f2b0ba0 100644 --- a/common/src/volumes/vol_map_3d.rs +++ b/common/src/volumes/vol_map_3d.rs @@ -1,9 +1,3 @@ -use std::{ - collections::HashMap, - sync::Arc, - marker::PhantomData, -}; -use vek::*; use crate::{ terrain::TerrainChunkMeta, vol::{BaseVol, ReadVol, SampleVol, SizedVol, VolSize, Vox, WriteVol}, @@ -12,6 +6,8 @@ use crate::{ dyna::{Dyna, DynaErr}, }, }; +use std::{collections::HashMap, marker::PhantomData, sync::Arc}; +use vek::*; #[derive(Debug)] pub enum VolMap3dErr { @@ -134,11 +130,7 @@ impl VolMap3d { S::SIZE } - pub fn insert( - &mut self, - key: Vec3, - chunk: Arc, - ) -> Option> { + pub fn insert(&mut self, key: Vec3, chunk: Arc) -> Option> { self.chunks.insert(key, chunk) } diff --git a/voxygen/src/mesh/terrain.rs b/voxygen/src/mesh/terrain.rs index 270c13eda2..f250f86926 100644 --- a/voxygen/src/mesh/terrain.rs +++ b/voxygen/src/mesh/terrain.rs @@ -44,7 +44,7 @@ impl Meshable for Dyna { } } -impl + ReadVol, S: VolSize + Clone> Meshable for VolMap2d { +impl + ReadVol, S: VolSize + Clone> Meshable for VolMap2d { type Pipeline = TerrainPipeline; type Supplement = Aabb; @@ -108,7 +108,7 @@ impl + ReadVol, S: VolSize + Clone> Meshable for VolMap2d< } } -impl + ReadVol, S: VolSize + Clone> Meshable for VolMap3d { +impl + ReadVol, S: VolSize + Clone> Meshable for VolMap3d { type Pipeline = TerrainPipeline; type Supplement = Aabb; diff --git a/voxygen/src/scene/terrain.rs b/voxygen/src/scene/terrain.rs index 3c085400c5..4155f36829 100644 --- a/voxygen/src/scene/terrain.rs +++ b/voxygen/src/scene/terrain.rs @@ -1,11 +1,11 @@ -use std::{collections::HashMap, sync::mpsc, time::Duration}; -use vek::*; -use client::Client; -use common::{terrain::TerrainMap, vol::SampleVol, volumes::vol_map_2d::VolMap2dErr}; use crate::{ mesh::Meshable, render::{Consts, Globals, Mesh, Model, Renderer, TerrainLocals, TerrainPipeline}, }; +use client::Client; +use common::{terrain::TerrainMap, vol::SampleVol, volumes::vol_map_2d::VolMap2dErr}; +use std::{collections::HashMap, sync::mpsc, time::Duration}; +use vek::*; struct TerrainChunk { // GPU data @@ -174,12 +174,12 @@ impl Terrain { .expect("Failed to upload chunk mesh to the GPU"), locals: renderer .create_consts(&[TerrainLocals { - model_offs: Vec3::from(response - .pos - .map2(TerrainMap::chunk_size(), |e, sz| { + model_offs: Vec3::from( + response.pos.map2(TerrainMap::chunk_size(), |e, sz| { e as f32 * sz as f32 - })) - .into_array(), + }), + ) + .into_array(), }]) .expect("Failed to upload chunk locals to the GPU"), }, diff --git a/world/src/lib.rs b/world/src/lib.rs index c716f981ab..0a19f3a546 100644 --- a/world/src/lib.rs +++ b/world/src/lib.rs @@ -4,8 +4,8 @@ use vek::*; // Project use common::{ - terrain::{Block, TerrainChunk, TerrainChunkSize, TerrainChunkMeta}, - vol::{SizedVol, Vox, WriteVol, VolSize}, + terrain::{Block, TerrainChunk, TerrainChunkMeta, TerrainChunkSize}, + vol::{SizedVol, VolSize, Vox, WriteVol}, }; #[derive(Debug)] @@ -39,7 +39,8 @@ impl World { for y in 0..TerrainChunkSize::SIZE.y as i32 { for z in 0..256 { let lpos = Vec3::new(x, y, z); - let wpos = lpos + Vec3::from(chunk_pos) * TerrainChunkSize::SIZE.map(|e| e as i32); + let wpos = + lpos + Vec3::from(chunk_pos) * TerrainChunkSize::SIZE.map(|e| e as i32); let wposf = wpos.map(|e| e as f64); let chaos_freq = 1.0 / 100.0; @@ -54,27 +55,28 @@ impl World { .max(0.0) + 0.5; - 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) - + offs; - let temp = (temp_nz.get(Vec2::from(wposf * (1.0 / 64.0)).into_array()) + 1.0) * 0.5; + 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) + + offs; + let temp = + (temp_nz.get(Vec2::from(wposf * (1.0 / 64.0)).into_array()) + 1.0) * 0.5; - 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 - }, - ); + 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 + }, + ); } } }