From c13206d82de4fb4221ddf0526b87c347667b3ec5 Mon Sep 17 00:00:00 2001 From: Pfauenauge90 <44173739+Pfauenauge90@users.noreply.github.com> Date: Thu, 16 May 2019 19:40:32 +0200 Subject: [PATCH] Basic worldgen Former-commit-id: 3ca0afa95dd1d868a84a41129e889a203e8a7cd9 --- common/src/sys/control.rs | 2 +- common/src/sys/mod.rs | 6 +++--- server/src/lib.rs | 14 +++++--------- voxygen/src/hud/img_ids.rs | 4 ++-- world/src/lib.rs | 10 +++++++--- world/src/sim.rs | 32 +++++++++++++++++++++++++------- 6 files changed, 43 insertions(+), 25 deletions(-) diff --git a/common/src/sys/control.rs b/common/src/sys/control.rs index 37610a8795..0fa60fe0d0 100644 --- a/common/src/sys/control.rs +++ b/common/src/sys/control.rs @@ -73,7 +73,7 @@ impl<'a> System<'a> for Sys { * 50.0 * vel.0.map(|e| { (e.abs() * friction * (vel.0.magnitude() * 0.1 + 0.5)) - .min(e.abs()) + .min(e.abs() * dt.0 * 50.0) .copysign(e) }) * Vec3::new(1.0, 1.0, 0.0); diff --git a/common/src/sys/mod.rs b/common/src/sys/mod.rs index 18ece5816e..85427a1e59 100644 --- a/common/src/sys/mod.rs +++ b/common/src/sys/mod.rs @@ -9,12 +9,12 @@ use specs::DispatcherBuilder; // System names const AGENT_SYS: &str = "agent_sys"; const CONTROL_SYS: &str = "control_sys"; -const MOVEMENT_SYS: &str = "movement_sys"; +const PHYS_SYS: &str = "phys_sys"; const ANIM_SYS: &str = "anim_sys"; pub fn add_local_systems(dispatch_builder: &mut DispatcherBuilder) { dispatch_builder.add(agent::Sys, AGENT_SYS, &[]); - dispatch_builder.add(control::Sys, CONTROL_SYS, &[]); - dispatch_builder.add(phys::Sys, MOVEMENT_SYS, &[]); + dispatch_builder.add(phys::Sys, PHYS_SYS, &[]); + dispatch_builder.add(control::Sys, CONTROL_SYS, &["phys_sys"]); dispatch_builder.add(anim::Sys, ANIM_SYS, &[]); } diff --git a/server/src/lib.rs b/server/src/lib.rs index 411b371670..bf7c8de701 100644 --- a/server/src/lib.rs +++ b/server/src/lib.rs @@ -23,7 +23,7 @@ use specs::{ join::Join, saveload::MarkedBuilder, world::EntityBuilder as EcsEntityBuilder, Builder, Entity as EcsEntity, }; -use std::{collections::HashSet, i32, net::SocketAddr, sync::mpsc, time::Duration}; +use std::{collections::HashSet, i32, net::SocketAddr, sync::{Arc, mpsc}, time::Duration}; use threadpool::ThreadPool; use vek::*; use world::World; @@ -40,7 +40,7 @@ pub enum Event { pub struct Server { state: State, - world: World, + world: Arc, postoffice: PostOffice, clients: Clients, @@ -68,7 +68,7 @@ impl Server { let mut this = Self { state, - world: World::generate(DEFAULT_WORLD_SEED), + world: Arc::new(World::generate(DEFAULT_WORLD_SEED)), postoffice: PostOffice::bind(addrs.into())?, clients: Clients::empty(), @@ -110,11 +110,6 @@ impl Server { pub fn world(&self) -> &World { &self.world } - /// Get a mutable reference to the server's world. - #[allow(dead_code)] - pub fn world_mut(&mut self) -> &mut World { - &mut self.world - } /// Build a non-player character. #[allow(dead_code)] @@ -574,8 +569,9 @@ impl Server { pub fn generate_chunk(&mut self, key: Vec2) { if self.pending_chunks.insert(key) { let chunk_tx = self.chunk_tx.clone(); + let world = self.world.clone(); self.thread_pool.execute(move || { - let _ = chunk_tx.send((key, World::generate_chunk(key))); + let _ = chunk_tx.send((key, world.generate_chunk(key))).unwrap() }); } } diff --git a/voxygen/src/hud/img_ids.rs b/voxygen/src/hud/img_ids.rs index d514b4e1af..33a8462944 100644 --- a/voxygen/src/hud/img_ids.rs +++ b/voxygen/src/hud/img_ids.rs @@ -51,7 +51,7 @@ image_ids! { // Crosshair crosshair: "voxygen/element/misc_bg/crosshair.vox", - +//////////////////////////////////////////////////////////////////////// // Buttons @@ -121,7 +121,7 @@ image_ids! { button_hover: "voxygen/element/buttons/button_hover.vox", button_press: "voxygen/element/buttons/button_press.vox", - +////////////////////////////////////////////////////////////////////////////////////////////////////// charwindow_gradient:"voxygen/element/misc_bg/charwindow.png", diff --git a/world/src/lib.rs b/world/src/lib.rs index 8291e696e4..81c31e3f02 100644 --- a/world/src/lib.rs +++ b/world/src/lib.rs @@ -24,11 +24,11 @@ impl World { Self { sim: sim::WorldSim::generate(seed) } } - pub fn tick(&mut self, dt: Duration) { + pub fn tick(&self, dt: Duration) { // TODO } - pub fn generate_chunk(chunk_pos: Vec2) -> TerrainChunk { + pub fn generate_chunk(&self, chunk_pos: Vec2) -> TerrainChunk { // TODO: This is all test code, remove/improve this later. let air = Block::empty(); @@ -47,7 +47,11 @@ impl World { * 1000.0) }; - let offset_z = get_offset(chunk_pos * Vec2::from(TerrainChunkSize::SIZE).map(|e: u32| e as i32)); + let offset_z = self.sim.get(chunk_pos.map(|e| e as u32)) + .map(|chunk| chunk.alt as i32) + .unwrap_or(0); + + //get_offset(chunk_pos * Vec2::from(TerrainChunkSize::SIZE).map(|e: u32| e as i32)); let mut chunk = TerrainChunk::new(offset_z as i32, stone, air, TerrainChunkMeta::void()); diff --git a/world/src/sim.rs b/world/src/sim.rs index a93dfe746e..61f7999ec8 100644 --- a/world/src/sim.rs +++ b/world/src/sim.rs @@ -1,4 +1,10 @@ +use std::ops::{Mul, Div}; use noise::{NoiseFn, OpenSimplex, Seedable}; +use vek::*; +use common::{ + terrain::TerrainChunkSize, + vol::VolSize, +}; use crate::WORLD_SIZE; pub struct WorldSim { @@ -14,9 +20,9 @@ impl WorldSim { }; let mut chunks = Vec::new(); - for x in 0..WORLD_SIZE.x { - for y in 0..WORLD_SIZE.y { - chunks.push(SimChunk::generate(&mut gen_ctx)); + 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)); } } @@ -25,20 +31,32 @@ impl WorldSim { chunks, } } + + pub fn get(&self, chunk_pos: Vec2) -> 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 + } + } } struct GenCtx { alt_nz: OpenSimplex, } -struct SimChunk { - alt: f32, +pub struct SimChunk { + pub alt: f32, } impl SimChunk { - pub fn generate(gen_ctx: &mut GenCtx) -> Self { + fn generate(pos: Vec2, gen_ctx: &mut GenCtx) -> Self { + let wposf = (pos * Vec2::from(TerrainChunkSize::SIZE)).map(|e| e as f64); + Self { - alt: 0.0 + alt: gen_ctx.alt_nz + .get((wposf.div(2048.0)).into_array()) + .mul(512.0) as f32, } } }