From 248577bdefba527f9a1c72f339055f01ad93d4e1 Mon Sep 17 00:00:00 2001 From: Joshua Barretto Date: Wed, 23 Jan 2019 20:01:58 +0000 Subject: [PATCH] Added test terrain loading and meshing --- client/Cargo.toml | 1 + client/src/lib.rs | 36 ++++++- common/src/state.rs | 62 ++++++++++-- common/src/terrain/block.rs | 20 ++++ common/src/volumes/vol_map.rs | 15 +++ voxygen/src/anim/character/run.rs | 7 +- voxygen/src/main.rs | 2 + voxygen/src/mesh/mod.rs | 8 +- voxygen/src/mesh/segment.rs | 17 ++-- voxygen/src/mesh/terrain.rs | 147 +++++++++++++++++++++++++++ voxygen/src/scene/camera.rs | 2 +- voxygen/src/scene/mod.rs | 30 +++--- voxygen/src/scene/terrain.rs | 162 +++++++++++++++++++++++++++--- voxygen/src/session.rs | 23 ++++- world/Cargo.toml | 1 + world/src/lib.rs | 35 ++++++- 16 files changed, 508 insertions(+), 60 deletions(-) create mode 100644 voxygen/src/mesh/terrain.rs diff --git a/client/Cargo.toml b/client/Cargo.toml index 04e8d4ae16..ac82be29e1 100644 --- a/client/Cargo.toml +++ b/client/Cargo.toml @@ -10,3 +10,4 @@ world = { package = "veloren-world", path = "../world" } specs = "0.14" vek = "0.9" +threadpool = "1.7" diff --git a/client/src/lib.rs b/client/src/lib.rs index eafda2d0b1..5a90e14ff8 100644 --- a/client/src/lib.rs +++ b/client/src/lib.rs @@ -4,6 +4,7 @@ use std::time::Duration; // Library use specs::Entity as EcsEntity; use vek::*; +use threadpool; // Project use common::{ @@ -23,8 +24,10 @@ pub struct Input { } pub struct Client { - state: State, + thread_pool: threadpool::ThreadPool, + tick: u64, + state: State, player: Option, // Testing @@ -36,8 +39,12 @@ impl Client { /// Create a new `Client`. pub fn new() -> Self { Self { - state: State::new(), + thread_pool: threadpool::Builder::new() + .thread_name("veloren-worker".into()) + .build(), + tick: 0, + state: State::new(), player: None, // Testing @@ -46,18 +53,34 @@ impl Client { } } - /// TODO: Get rid of this + /// Get a reference to the client's worker thread pool. This pool should be used for any + /// computationally expensive operations that run outside of the main thread (i.e: threads that + /// block on I/O operations are exempt). + pub fn thread_pool(&self) -> &threadpool::ThreadPool { &self.thread_pool } + + // TODO: Get rid of this pub fn with_test_state(mut self) -> Self { self.chunk = Some(self.world.generate_chunk(Vec3::zero())); self } + // TODO: Get rid of this + pub fn load_chunk(&mut self, pos: Vec3) { + self.state.terrain_mut().insert(pos, self.world.generate_chunk(pos)); + self.state.changes_mut().new_chunks.push(pos); + } + /// Get a reference to the client's game state. pub fn state(&self) -> &State { &self.state } /// Get a mutable reference to the client's game state. pub fn state_mut(&mut self) -> &mut State { &mut self.state } + /// Get the current tick number. + pub fn get_tick(&self) -> u64 { + self.tick + } + /// Execute a single client tick, handle input and update the game state by the given duration pub fn tick(&mut self, input: Input, dt: Duration) -> Result<(), Error> { // This tick function is the centre of the Veloren universe. Most client-side things are @@ -76,6 +99,13 @@ impl Client { self.state.tick(dt); // Finish the tick, pass control back to the frontend (step 6) + self.tick += 1; Ok(()) } + + /// Clean up the client after a tick + pub fn cleanup(&mut self) { + // Cleanup the local state + self.state.cleanup(); + } } diff --git a/common/src/state.rs b/common/src/state.rs index 71a7e741c2..a4d974ee57 100644 --- a/common/src/state.rs +++ b/common/src/state.rs @@ -1,9 +1,10 @@ // Standard use std::time::Duration; -// External +// Library use specs::World as EcsWorld; -use shred::Fetch; +use shred::{Fetch, FetchMut}; +use vek::*; // Crate use crate::{ @@ -19,12 +20,35 @@ const DAY_CYCLE_FACTOR: f64 = 24.0 * 60.0; struct TimeOfDay(f64); /// A resource to store the tick (i.e: physics) time -struct Tick(f64); +struct Time(f64); + +pub struct Changes { + pub new_chunks: Vec>, + pub changed_chunks: Vec>, + pub removed_chunks: Vec>, +} + +impl Changes { + pub fn default() -> Self { + Self { + new_chunks: vec![], + changed_chunks: vec![], + removed_chunks: vec![], + } + } + + pub fn cleanup(&mut self) { + self.new_chunks.clear(); + self.changed_chunks.clear(); + self.removed_chunks.clear(); + } +} /// A type used to represent game state stored on both the client and the server. This includes /// things like entity components, terrain data, and global state like weather, time of day, etc. pub struct State { ecs_world: EcsWorld, + changes: Changes, } impl State { @@ -34,7 +58,7 @@ impl State { // Register resources used by the ECS ecs_world.add_resource(TimeOfDay(0.0)); - ecs_world.add_resource(Tick(0.0)); + ecs_world.add_resource(Time(0.0)); ecs_world.add_resource(TerrainMap::new()); // Register common components with the state @@ -42,9 +66,20 @@ impl State { Self { ecs_world, + changes: Changes::default(), } } + /// Get a reference to the internal ECS world + pub fn ecs_world(&self) -> &EcsWorld { &self.ecs_world } + + /// Get a reference to the `Changes` structure of the state. This contains + /// information about state that has changed since the last game tick. + pub fn changes(&self) -> &Changes { &self.changes } + + // TODO: Get rid of this since it shouldn't be needed + pub fn changes_mut(&mut self) -> &mut Changes { &mut self.changes } + /// Get the current in-game time of day. /// /// Note that this should not be used for physics, animations or other such localised timings. @@ -52,11 +87,11 @@ impl State { self.ecs_world.read_resource::().0 } - /// Get the current in-game tick time. + /// Get the current in-game time. /// /// Note that this does not correspond to the time of day. - pub fn get_tick(&self) -> f64 { - self.ecs_world.read_resource::().0 + pub fn get_time(&self) -> f64 { + self.ecs_world.read_resource::