diff --git a/server-cli/src/main.rs b/server-cli/src/main.rs index d9779838d1..43d3916c52 100644 --- a/server-cli/src/main.rs +++ b/server-cli/src/main.rs @@ -28,10 +28,10 @@ use structopt::StructOpt; use tracing::{info, trace}; use worldsim::{ - regionmanager::{RegionManager, meta::RegionManagerMsg}, - server::meta::{ServerMsg}, job::JobManager, region::Region, + regionmanager::{meta::RegionManagerMsg, RegionManager}, + server::meta::ServerMsg, }; lazy_static::lazy_static! { @@ -169,11 +169,12 @@ fn main() -> io::Result<()> { let mut region_manager = RegionManager::new(region_manager_tx, server_rx); let mut job_manager: Arc = Arc::new(JobManager::new()); - let mut server = worldsim::server::Server::new(server_tx,region_manager_rx,job_manager.clone()); - let mut region = Region::new((0,0),job_manager.clone()); + let mut server = + worldsim::server::Server::new(server_tx, region_manager_rx, job_manager.clone()); + let mut region = Region::new((0, 0), job_manager.clone()); - job_manager.repeat(move || region_manager.work() ); - job_manager.repeat(move || server.work() ); + job_manager.repeat(move || region_manager.work()); + job_manager.repeat(move || server.work()); // Create server let mut server = Server::new( diff --git a/worldsim/src/job/mod.rs b/worldsim/src/job/mod.rs index f4ea4cf804..bd91c05602 100644 --- a/worldsim/src/job/mod.rs +++ b/worldsim/src/job/mod.rs @@ -2,21 +2,18 @@ use std::thread; #[derive(Debug, Clone)] /*only have one JobManager per System because it bounds to all threads*/ -pub struct JobManager { +pub struct JobManager {} -} - -impl JobManager{ +impl JobManager { pub fn new() -> Self { - Self { - } + Self {} } - pub fn repeat(&self, mut f: F) where F: FnMut() -> bool, F: Send + 'static { - let worker = thread::spawn(move || { - while f() { - - } - }); + pub fn repeat(&self, mut f: F) + where + F: FnMut() -> bool, + F: Send + 'static, + { + let _worker = thread::spawn(move || while f() {}); } -} \ No newline at end of file +} diff --git a/worldsim/src/lib.rs b/worldsim/src/lib.rs index 646e1e8582..ad63ec2059 100644 --- a/worldsim/src/lib.rs +++ b/worldsim/src/lib.rs @@ -1,4 +1,4 @@ -#![feature(const_generics, associated_type_bounds, euclidean_division, duration_float, trait_alias, bind_by_move_pattern_guards, test)] +#![feature(const_generics, test)] extern crate serde_derive; #[macro_use] @@ -6,10 +6,10 @@ extern crate log; extern crate test; pub mod job; -pub mod regionmanager; -pub mod region; -pub mod server; pub mod lodstore; +pub mod region; +pub mod regionmanager; +pub mod server; #[cfg(test)] mod tests { diff --git a/worldsim/src/lodstore/area.rs b/worldsim/src/lodstore/area.rs index 009a96c06e..589cb4d5dd 100644 --- a/worldsim/src/lodstore/area.rs +++ b/worldsim/src/lodstore/area.rs @@ -1,6 +1,4 @@ -use super::lodpos::{ - LodPos, -}; +use super::lodpos::LodPos; /* A LodArea is the area between 2 LodIndex @@ -14,18 +12,18 @@ pub struct LodArea { impl LodArea { pub fn new(lower: LodPos, upper: LodPos) -> Self { - LodArea { - lower, - upper, - } + LodArea { lower, upper } } pub fn is_inside(&self, lod: LodPos) -> bool { let lower = self.lower.get(); let upper = self.upper.get(); let lod = lod.get(); - lod[0] >= lower[0] && lod[0] <= upper[0] && - lod[1] >= lower[1] && lod[1] <= upper[1] && - lod[2] >= lower[2] && lod[2] <= upper[2] + lod[0] >= lower[0] + && lod[0] <= upper[0] + && lod[1] >= lower[1] + && lod[1] <= upper[1] + && lod[2] >= lower[2] + && lod[2] <= upper[2] } -} \ No newline at end of file +} diff --git a/worldsim/src/lodstore/data.rs b/worldsim/src/lodstore/data.rs index 7a1e3cde5c..ab89339773 100644 --- a/worldsim/src/lodstore/data.rs +++ b/worldsim/src/lodstore/data.rs @@ -1,9 +1,6 @@ -use super::area::LodArea; -//use super::delta::LodDelta; -use super::lodpos::{self, multily_with_2_pow_n, relative_to_1d, two_pow_u32, AbsIndex, LodPos}; use super::index::ToOptionUsize; +use super::lodpos::{multily_with_2_pow_n, relative_to_1d, two_pow_u32, LodPos}; use fxhash::FxHashMap; -use std::collections::HashMap; use std::{u16, u32}; use vek::*; @@ -20,7 +17,7 @@ use vek::*; traits: - Layer: Every layer must implement this. KEY is the storage Type and either usize/LodPos. Layer is also defined here. - - ParentLayer: Is a Layer that contains a CHILD layer and some const functions based on their const properties + - ParentLayer: Is a Layer that contains a CHILD layer and some const properties - IndexStore: Every layer must implement this for their Layer::KEY and INDEX is often u16/u32. The index is accessed by this layer to get the corresponding child. Every Indexstore is a ParentLayer. @@ -52,19 +49,13 @@ pub trait Layer { pub trait ParentLayer: Layer { type CHILD: Layer; fn child(&self) -> &Self::CHILD; - fn CHILDS_PER_OWN_TOTAL() -> usize { - two_pow_u32(Self::LOG2_OF_CHILDS_PER_OWN_TOTAL()) as usize - } - fn LOG2_OF_CHILDS_PER_OWN_TOTAL() -> u8 { - 3 * ({ Self::LEVEL } - Self::CHILD::LEVEL) - } - fn CHILDS_PER_OWN() -> Vec3 { - Vec3 { - x: two_pow_u32(Self::LEVEL - Self::CHILD::LEVEL) as u32, - y: two_pow_u32(Self::LEVEL - Self::CHILD::LEVEL) as u32, - z: two_pow_u32(Self::LEVEL - Self::CHILD::LEVEL) as u32, - } - } + const CHILDS_PER_OWN_TOTAL: usize = two_pow_u32(Self::LOG2_OF_CHILDS_PER_OWN_TOTAL) as usize; + const LOG2_OF_CHILDS_PER_OWN_TOTAL: u8 = 3 * ({ Self::LEVEL } - Self::CHILD::LEVEL); + const CHILDS_PER_OWN: Vec3 = Vec3 { + x: two_pow_u32(Self::LEVEL - Self::CHILD::LEVEL) as u32, + y: two_pow_u32(Self::LEVEL - Self::CHILD::LEVEL) as u32, + z: two_pow_u32(Self::LEVEL - Self::CHILD::LEVEL) as u32, + }; } pub trait IndexStore: ParentLayer { @@ -86,21 +77,21 @@ pub trait Materializeable { //####################################################### -#[derive(Default, Clone, Debug)] +#[derive(Default, Clone)] pub struct VecLayer { pub detail: Vec, } -#[derive(Default, Clone, Debug)] +#[derive(Default, Clone)] pub struct HashLayer { pub detail: FxHashMap, } -#[derive(Default, Clone, Debug)] +#[derive(Default, Clone)] pub struct VecNestLayer { pub detail: Vec, pub index: Vec, pub child: C, } -#[derive(Default, Clone, Debug)] +#[derive(Default, Clone)] pub struct HashNestLayer { pub detail_index: FxHashMap, pub child: C, @@ -118,35 +109,42 @@ pub struct VecIter<'a, C: DetailStore> { layer_key: usize, } -#[rustfmt::skip] impl Layer for VecLayer { - type KEY = ( usize ); const LEVEL: u8 = { L }; + type KEY = (usize); + const LEVEL: u8 = { L }; } -#[rustfmt::skip] impl Layer for HashLayer { - type KEY = ( LodPos ); const LEVEL: u8 = { L }; + type KEY = (LodPos); + const LEVEL: u8 = { L }; } -#[rustfmt::skip] impl Layer for VecNestLayer { - type KEY = ( usize ); const LEVEL: u8 = { L }; + type KEY = (usize); + const LEVEL: u8 = { L }; } -#[rustfmt::skip] impl Layer for HashNestLayer { - type KEY = ( LodPos ); const LEVEL: u8 = { L }; + type KEY = (LodPos); + const LEVEL: u8 = { L }; } -#[rustfmt::skip] -impl ParentLayer for VecNestLayer { +impl ParentLayer + for VecNestLayer +{ type CHILD = C; - fn child(&self) -> &Self::CHILD { &self.child } + fn child(&self) -> &Self::CHILD { + &self.child + } } -#[rustfmt::skip] -impl ParentLayer for HashNestLayer { +impl ParentLayer + for HashNestLayer +{ type CHILD = C; - fn child(&self) -> &Self::CHILD { &self.child } + fn child(&self) -> &Self::CHILD { + &self.child + } } impl HashNestLayer { + #[allow(dead_code)] fn trav(&self, pos: LodPos) -> HashIter { HashIter { layer: &self, @@ -156,43 +154,44 @@ impl HashNestLayer IndexStore for VecNestLayer { type INDEX = I; - fn load(&self, key: Self::KEY) -> Self::INDEX { self.index[key] } + fn load(&self, key: Self::KEY) -> Self::INDEX { + self.index[key] + } } -#[rustfmt::skip] -impl IndexStore for HashNestLayer { +impl IndexStore + for HashNestLayer +{ type INDEX = I; fn load(&self, key: Self::KEY) -> Self::INDEX { debug_assert_eq!(key, key.align_to_level({ L })); self.detail_index[&key].1 } } - -#[rustfmt::skip] -impl DetailStore for VecNestLayer { +impl DetailStore + for VecNestLayer +{ type DETAIL = T; fn load(&self, key: Self::KEY) -> &Self::DETAIL { &self.detail[key] } } -#[rustfmt::skip] -impl DetailStore for HashNestLayer { +impl DetailStore + for HashNestLayer +{ type DETAIL = T; fn load(&self, key: LodPos) -> &Self::DETAIL { debug_assert_eq!(key, key.align_to_level({ L })); &self.detail_index[&key].0 } } -#[rustfmt::skip] impl DetailStore for VecLayer { type DETAIL = T; fn load(&self, key: usize) -> &Self::DETAIL { &self.detail[key] } } -#[rustfmt::skip] impl DetailStore for HashLayer { type DETAIL = T; fn load(&self, key: LodPos) -> &Self::DETAIL { @@ -202,13 +201,22 @@ impl DetailStore for HashLayer { } impl<'a, L: DetailStore + IndexStore> Traversable> -for HashIter<'a, L> - where - L::CHILD: DetailStore, { + for HashIter<'a, L> +where + L::CHILD: DetailStore, +{ fn get(self) -> VecIter<'a, L::CHILD> { - let child_lod = self.wanted.align_to_level(L::CHILD::LEVEL ); - let pos_offset = relative_to_1d(child_lod, self.layer_lod, L::CHILD::LEVEL, L::CHILDS_PER_OWN()); - let layer_key = ( multily_with_2_pow_n( IndexStore::load(self.layer, self.layer_lod).into_usize(), L::LOG2_OF_CHILDS_PER_OWN_TOTAL()) ) + pos_offset; + let child_lod = self.wanted.align_to_level(L::CHILD::LEVEL); + let pos_offset = relative_to_1d( + child_lod, + self.layer_lod, + L::CHILD::LEVEL, + L::CHILDS_PER_OWN, + ); + let layer_key = (multily_with_2_pow_n( + IndexStore::load(self.layer, self.layer_lod).into_usize(), + L::LOG2_OF_CHILDS_PER_OWN_TOTAL, + )) + pos_offset; VecIter { layer: self.layer.child(), wanted: self.wanted, @@ -219,13 +227,22 @@ for HashIter<'a, L> } impl<'a, L: DetailStore + IndexStore> Traversable> -for VecIter<'a, L> - where - L::CHILD: DetailStore, { + for VecIter<'a, L> +where + L::CHILD: DetailStore, +{ fn get(self) -> VecIter<'a, L::CHILD> { - let child_lod = self.wanted.align_to_level(L::CHILD::LEVEL ); - let pos_offset = relative_to_1d(child_lod, self.layer_lod, L::CHILD::LEVEL, L::CHILDS_PER_OWN()); - let layer_key = ( multily_with_2_pow_n( IndexStore::load(self.layer, self.layer_key).into_usize(), L::LOG2_OF_CHILDS_PER_OWN_TOTAL()) ) + pos_offset; + let child_lod = self.wanted.align_to_level(L::CHILD::LEVEL); + let pos_offset = relative_to_1d( + child_lod, + self.layer_lod, + L::CHILD::LEVEL, + L::CHILDS_PER_OWN, + ); + let layer_key = (multily_with_2_pow_n( + IndexStore::load(self.layer, self.layer_key).into_usize(), + L::LOG2_OF_CHILDS_PER_OWN_TOTAL, + )) + pos_offset; VecIter { layer: self.layer.child(), wanted: self.wanted, @@ -235,13 +252,13 @@ for VecIter<'a, L> } } -impl<'a, L: DetailStore> Materializeable<&'a L::DETAIL> for HashIter<'a, L> { +impl<'a, L: DetailStore> Materializeable<&'a L::DETAIL> for HashIter<'a, L> { fn mat(self) -> &'a L::DETAIL { DetailStore::load(self.layer, self.layer_lod) } } -impl<'a, L: DetailStore> Materializeable<&'a L::DETAIL> for VecIter<'a, L> { +impl<'a, L: DetailStore> Materializeable<&'a L::DETAIL> for VecIter<'a, L> { fn mat(self) -> &'a L::DETAIL { DetailStore::load(self.layer, self.layer_key) } @@ -270,20 +287,57 @@ mod tests { ExampleData { detail_index, child: VecNestLayer { - detail: vec!((),(),()), - index: vec!(0,1,u32::MAX), + detail: vec![(), (), ()], + index: vec![0, 1, u32::MAX], child: VecNestLayer { - detail: vec!(None,None,None,Some(()),Some(()),None,None,None,None,None,None,None,None,None,None,None), - index: vec!(0,u16::MAX,u16::MAX,0,u16::MAX,u16::MAX,u16::MAX,u16::MAX,u16::MAX,u16::MAX,u16::MAX,u16::MAX,u16::MAX,u16::MAX,u16::MAX,u16::MAX), + detail: vec![ + None, + None, + None, + Some(()), + Some(()), + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + None, + ], + index: vec![ + 0, + u16::MAX, + u16::MAX, + 0, + u16::MAX, + u16::MAX, + u16::MAX, + u16::MAX, + u16::MAX, + u16::MAX, + u16::MAX, + u16::MAX, + u16::MAX, + u16::MAX, + u16::MAX, + u16::MAX, + ], child: VecLayer { - detail: vec!(7,6,5,4,3,2,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0), + detail: vec![ + 7, 6, 5, 4, 3, 2, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, + ], }, }, }, } } - #[test] fn compilation() { let x = ExampleData::default(); @@ -291,7 +345,7 @@ mod tests { if false { let y = x.trav(i); let ttc = y.get().get().get(); - let tt = ttc.mat(); + let _tt = ttc.mat(); } } @@ -349,4 +403,4 @@ mod tests { } b.iter(|| x.trav(access).get().get().get().mat()); } -} \ No newline at end of file +} diff --git a/worldsim/src/lodstore/delta.rs b/worldsim/src/lodstore/delta.rs index c322a24c30..0ca22a1197 100644 --- a/worldsim/src/lodstore/delta.rs +++ b/worldsim/src/lodstore/delta.rs @@ -168,4 +168,4 @@ impl LodDelta { } } } -*/ \ No newline at end of file +*/ diff --git a/worldsim/src/lodstore/index.rs b/worldsim/src/lodstore/index.rs index 8db21650a6..e3717f3b6f 100644 --- a/worldsim/src/lodstore/index.rs +++ b/worldsim/src/lodstore/index.rs @@ -1,8 +1,3 @@ -use vek::*; -use std::ops::Sub; -use std::ops::Add; -use std::cmp; -use std::fmt; use std::{u16, u32}; pub trait ToOptionUsize: Copy { @@ -26,4 +21,4 @@ impl ToOptionUsize for u16 { fn into_usize(self) -> usize { self as usize } -} \ No newline at end of file +} diff --git a/worldsim/src/lodstore/lodpos.rs b/worldsim/src/lodstore/lodpos.rs index db77de0388..2ff4f22fc0 100644 --- a/worldsim/src/lodstore/lodpos.rs +++ b/worldsim/src/lodstore/lodpos.rs @@ -1,8 +1,8 @@ -use vek::*; -use std::ops::Sub; -use std::ops::Add; use std::cmp; use std::fmt; +use std::ops::Add; +use std::ops::Sub; +use vek::*; /* A region owns the Values from in (0, 2048) in steps of 1/32. @@ -33,9 +33,12 @@ pub struct LodPos { } /*does not work on big endian!*/ -const BIT_X_MASK: u64 = 0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0011_1111_1111_1111_1111; -const BIT_Y_MASK: u64 = 0b0000_0000_0000_0000_0000_0000_0000_1111_1111_1111_1111_1100_0000_0000_0000_0000; -const BIT_Z_MASK: u64 = 0b0000_0000_0011_1111_1111_1111_1111_0000_0000_0000_0000_0000_0000_0000_0000_0000; +const BIT_X_MASK: u64 = + 0b0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0000_0011_1111_1111_1111_1111; +const BIT_Y_MASK: u64 = + 0b0000_0000_0000_0000_0000_0000_0000_1111_1111_1111_1111_1100_0000_0000_0000_0000; +const BIT_Z_MASK: u64 = + 0b0000_0000_0011_1111_1111_1111_1111_0000_0000_0000_0000_0000_0000_0000_0000_0000; const BIT_X_MASK32: u32 = 0b0000_0000_0000_0011_1111_1111_1111_1111; const BIT_Y_OFFSET: u8 = 18; const BIT_Z_OFFSET: u8 = 36; @@ -43,16 +46,18 @@ const BIT_Z_OFFSET: u8 = 36; //TODO: Optimize! impl LodPos { pub fn new(data: Vec3) -> Self { - let mut index = LodPos {data: 0}; + let mut index = LodPos { data: 0 }; index.set(data); index } pub fn xyz(x: u32, y: u32, z: u32) -> Self { - LodPos {data: Self::encode(&x, &y, &z)} + LodPos { + data: Self::encode(&x, &y, &z), + } } - pub fn get(&self) -> Vec3 { + pub fn get(&self) -> Vec3 { Vec3::from(Self::decode(&self.data)) } @@ -62,30 +67,33 @@ impl LodPos { fn encode(x: &u32, y: &u32, z: &u32) -> u64 { let x = (x & BIT_X_MASK32) as u64; - let y = ((y & BIT_X_MASK32) as u64 ) << BIT_Y_OFFSET; - let z = ((z & BIT_X_MASK32) as u64 ) << BIT_Z_OFFSET; + let y = ((y & BIT_X_MASK32) as u64) << BIT_Y_OFFSET; + let z = ((z & BIT_X_MASK32) as u64) << BIT_Z_OFFSET; x + y + z } - fn decode(data: &u64) -> (u32, u32, u32) { + fn decode(data: &u64) -> (u32, u32, u32) { let x = (data & BIT_X_MASK) as u32; - let y = ((data & BIT_Y_MASK) >> BIT_Y_OFFSET ) as u32; - let z = ((data & BIT_Z_MASK) >> BIT_Z_OFFSET ) as u32; - (x,y,z) + let y = ((data & BIT_Y_MASK) >> BIT_Y_OFFSET) as u32; + let z = ((data & BIT_Z_MASK) >> BIT_Z_OFFSET) as u32; + (x, y, z) } pub fn align_to_level(&self, layer: u8) -> LodPos { let xyz = self.get(); let f = two_pow_u(layer) as u32; - LodPos::new(xyz.map(|i| { - (i / f) * f - })) + LodPos::new(xyz.map(|i| (i / f) * f)) } pub fn get_highest_level_that_fits(&self) -> u8 { let pos = self.get(); - cmp::min( cmp::min(cmp::min(pos[0].trailing_zeros(), - pos[1].trailing_zeros()), pos[2].trailing_zeros()), 15) as u8 + cmp::min( + cmp::min( + cmp::min(pos[0].trailing_zeros(), pos[1].trailing_zeros()), + pos[2].trailing_zeros(), + ), + 15, + ) as u8 } } @@ -105,10 +113,7 @@ pub struct AbsIndex { impl AbsIndex { pub fn new(layer: u8, index: usize) -> Self { - AbsIndex { - layer, - index, - } + AbsIndex { layer, index } } } @@ -122,7 +127,7 @@ impl Sub for LodPos { type Output = LodPos; fn sub(self, rhs: LodPos) -> Self::Output { LodPos { - data: self.data - rhs.data /*fast but has overflow issues*/ + data: self.data - rhs.data, /*fast but has overflow issues*/ } } } @@ -131,7 +136,7 @@ impl Add for LodPos { type Output = LodPos; fn add(self, rhs: LodPos) -> Self::Output { LodPos { - data: self.data + rhs.data /*fast but has overflow issues*/ + data: self.data + rhs.data, /*fast but has overflow issues*/ } } } @@ -143,26 +148,33 @@ pub const fn two_pow_u32(n: u8) -> u32 { 1 << n } -pub const fn multily_with_2_pow_n(a: usize, n: u8) -> usize { //return a * 2^n but fast +pub const fn multily_with_2_pow_n(a: usize, n: u8) -> usize { + //return a * 2^n but fast a << n } -pub fn relative_to_1d(child_lod: LodPos, parent_lod: LodPos, child_layer: u8, relative_size: Vec3) -> usize { +pub fn relative_to_1d( + child_lod: LodPos, + parent_lod: LodPos, + child_layer: u8, + relative_size: Vec3, +) -> usize { let width = two_pow_u32(child_layer) as u32; let index = (child_lod.get() - parent_lod.get()).map(|e| e / width); - (index[0] * relative_size[2] * relative_size[1] + index[1] * relative_size[2] + index[2]) as usize + (index[0] * relative_size[2] * relative_size[1] + index[1] * relative_size[2] + index[2]) + as usize } pub fn min(lhs: LodPos, rhs: LodPos) -> LodPos { let lhs = lhs.get(); let rhs = rhs.get(); - LodPos::new(lhs.map2(rhs, |a,b| cmp::min(a,b))) + LodPos::new(lhs.map2(rhs, |a, b| cmp::min(a, b))) } pub fn max(lhs: LodPos, rhs: LodPos) -> LodPos { let lhs = lhs.get(); let rhs = rhs.get(); - LodPos::new(lhs.map2(rhs, |a,b| cmp::max(a,b))) + LodPos::new(lhs.map2(rhs, |a, b| cmp::max(a, b))) } /************* @@ -171,110 +183,107 @@ pub fn max(lhs: LodPos, rhs: LodPos) -> LodPos { #[cfg(test)] mod tests { - use crate::{ - lodstore::lodpos::LodPos, - lodstore::lodpos::two_pow_u32, - }; - use vek::*; + use crate::{lodstore::lodpos::two_pow_u32, lodstore::lodpos::LodPos}; use test::Bencher; + use vek::*; #[test] fn setter_getter() { - let i = LodPos::xyz(0,0,0); - assert_eq!(i.get(), Vec3::new(0,0,0)); + let i = LodPos::xyz(0, 0, 0); + assert_eq!(i.get(), Vec3::new(0, 0, 0)); - let i = LodPos::xyz(1337,0,0); - assert_eq!(i.get(), Vec3::new(1337,0,0)); + let i = LodPos::xyz(1337, 0, 0); + assert_eq!(i.get(), Vec3::new(1337, 0, 0)); - let i = LodPos::xyz(0,1337,0); - assert_eq!(i.get(), Vec3::new(0,1337,0)); + let i = LodPos::xyz(0, 1337, 0); + assert_eq!(i.get(), Vec3::new(0, 1337, 0)); - let i = LodPos::xyz(0,0,1337); - assert_eq!(i.get(), Vec3::new(0,0,1337)); + let i = LodPos::xyz(0, 0, 1337); + assert_eq!(i.get(), Vec3::new(0, 0, 1337)); - let i = LodPos::xyz(1,1,1); - assert_eq!(i.get(), Vec3::new(1,1,1)); + let i = LodPos::xyz(1, 1, 1); + assert_eq!(i.get(), Vec3::new(1, 1, 1)); - let i = LodPos::xyz(262143,262143,262143); - assert_eq!(i.get(), Vec3::new(262143,262143,262143)); + let i = LodPos::xyz(262143, 262143, 262143); + assert_eq!(i.get(), Vec3::new(262143, 262143, 262143)); - let i = LodPos::xyz(262144,262144,262144); //overflow - assert_eq!(i.get(), Vec3::new(0,0,0)); + let i = LodPos::xyz(262144, 262144, 262144); //overflow + assert_eq!(i.get(), Vec3::new(0, 0, 0)); - let i = LodPos::xyz(42,1337,69); - assert_eq!(i.get(), Vec3::new(42,1337,69)); + let i = LodPos::xyz(42, 1337, 69); + assert_eq!(i.get(), Vec3::new(42, 1337, 69)); } #[test] fn align() { - let i = LodPos::xyz(1337,0,0).align_to_level(4); - assert_eq!(i.get(), Vec3::new(1328,0,0)); + let i = LodPos::xyz(1337, 0, 0).align_to_level(4); + assert_eq!(i.get(), Vec3::new(1328, 0, 0)); - let i = LodPos::xyz(1337,1800,0).align_to_level(5); - assert_eq!(i.get(), Vec3::new(1312,1792,0)); + let i = LodPos::xyz(1337, 1800, 0).align_to_level(5); + assert_eq!(i.get(), Vec3::new(1312, 1792, 0)); - let i = LodPos::xyz(1337,0,50).align_to_level(3); - assert_eq!(i.get(), Vec3::new(1336,0,48)); + let i = LodPos::xyz(1337, 0, 50).align_to_level(3); + assert_eq!(i.get(), Vec3::new(1336, 0, 48)); - let i = LodPos::xyz(1335,0,0).align_to_level(3); - assert_eq!(i.get(), Vec3::new(1328,0,0)); + let i = LodPos::xyz(1335, 0, 0).align_to_level(3); + assert_eq!(i.get(), Vec3::new(1328, 0, 0)); - let i = LodPos::xyz(31337,22000,25000).align_to_level(7); - assert_eq!(i.get(), Vec3::new(31232,21888,24960)); + let i = LodPos::xyz(31337, 22000, 25000).align_to_level(7); + assert_eq!(i.get(), Vec3::new(31232, 21888, 24960)); - let i = LodPos::xyz(31337,22000,25000).align_to_level(0); - assert_eq!(i.get(), Vec3::new(31337,22000,25000)); + let i = LodPos::xyz(31337, 22000, 25000).align_to_level(0); + assert_eq!(i.get(), Vec3::new(31337, 22000, 25000)); - let i = LodPos::xyz(0,0,0).align_to_level(4); - assert_eq!(i.get(), Vec3::new(0,0,0)); + let i = LodPos::xyz(0, 0, 0).align_to_level(4); + assert_eq!(i.get(), Vec3::new(0, 0, 0)); } #[test] fn get_highest_level_that_fits() { - let i = LodPos::xyz(0,0,0); + let i = LodPos::xyz(0, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 15); - let i = LodPos::xyz(1,0,0); + let i = LodPos::xyz(1, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 0); - let i = LodPos::xyz(2,0,0); + let i = LodPos::xyz(2, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 1); - let i = LodPos::xyz(3,0,0); + let i = LodPos::xyz(3, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 0); - let i = LodPos::xyz(4,0,0); + let i = LodPos::xyz(4, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 2); - let i = LodPos::xyz(5,0,0); + let i = LodPos::xyz(5, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 0); - let i = LodPos::xyz(1337,0,0); + let i = LodPos::xyz(1337, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 0); - let i = LodPos::xyz(1337,1800,0); + let i = LodPos::xyz(1337, 1800, 0); assert_eq!(i.get_highest_level_that_fits(), 0); - let i = LodPos::xyz(1338,0,50); + let i = LodPos::xyz(1338, 0, 50); assert_eq!(i.get_highest_level_that_fits(), 1); - let i = LodPos::xyz(1336,0,0); + let i = LodPos::xyz(1336, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 3); - let i = LodPos::xyz(31348,22000,25000); + let i = LodPos::xyz(31348, 22000, 25000); assert_eq!(i.get_highest_level_that_fits(), 2); - let i = LodPos::xyz(0,0,0); + let i = LodPos::xyz(0, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 15); - let i = LodPos::xyz(65536,0,0); + let i = LodPos::xyz(65536, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 15); - let i = LodPos::xyz(32768,0,0); + let i = LodPos::xyz(32768, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 15); - let i = LodPos::xyz(16384,0,0); + let i = LodPos::xyz(16384, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 14); - let i = LodPos::xyz(8192,0,0); + let i = LodPos::xyz(8192, 0, 0); assert_eq!(i.get_highest_level_that_fits(), 13); - let i = LodPos::xyz(65536,0,8192); + let i = LodPos::xyz(65536, 0, 8192); assert_eq!(i.get_highest_level_that_fits(), 13); } @@ -289,4 +298,3 @@ mod tests { b.iter(|| access.align_to_level(6)); } } - diff --git a/worldsim/src/lodstore/mod.rs b/worldsim/src/lodstore/mod.rs index 973df97e16..f6ddeedb7e 100644 --- a/worldsim/src/lodstore/mod.rs +++ b/worldsim/src/lodstore/mod.rs @@ -1,11 +1,6 @@ -pub mod lodpos; pub mod area; -pub mod index; pub mod data; pub mod delta; -pub use data::{ - HashNestLayer, - VecNestLayer, - HashLayer, - VecLayer, -}; \ No newline at end of file +pub mod index; +pub mod lodpos; +pub use data::{HashLayer, HashNestLayer, VecLayer, VecNestLayer}; diff --git a/worldsim/src/region/lod/mod.rs b/worldsim/src/region/lod/mod.rs index 71b893d511..37bbf771e2 100644 --- a/worldsim/src/region/lod/mod.rs +++ b/worldsim/src/region/lod/mod.rs @@ -1 +1 @@ -pub mod terrain; \ No newline at end of file +pub mod terrain; diff --git a/worldsim/src/region/lod/terrain.rs b/worldsim/src/region/lod/terrain.rs index 55b68c653d..78a28f1412 100644 --- a/worldsim/src/region/lod/terrain.rs +++ b/worldsim/src/region/lod/terrain.rs @@ -1,14 +1,4 @@ -use crate::lodstore::{ - HashNestLayer, - VecNestLayer, - HashLayer, - VecLayer, - lodpos::LodPos, - lodpos::AbsIndex, -}; -use vek::*; -use std::u32; -pub type LodIndex = LodPos; +use crate::lodstore::{HashNestLayer, VecLayer, VecNestLayer}; #[derive(Debug, Clone, Default)] pub struct Region { @@ -36,7 +26,6 @@ pub struct SubBlock { material: u32, } - #[rustfmt::skip] pub type TerrainLod = HashNestLayer< @@ -47,4 +36,4 @@ pub type TerrainLod = > ,Block, u16, 4 // In reality 2^(16*3) SubBlock_4 should be possible, but 2^48 subblocks would kill anything anyway, so save 75% bytes here. Limit is 65536 full blocks in SubBlocks, means (2^16) * ((2^4)^3) = 268 million > ,Chunk , u32,9 // we coult use u16 which would allow 2^31 blocks, however the world could have 2^33 blocks inside, which would mean only 25% could ever be on block level, which is prob never reacher, however ue to handling neibors and that this only would save 1/4 MB on this level, we just skip it for now > ,Region ,u16, 13 - >; \ No newline at end of file + >; diff --git a/worldsim/src/region/meta.rs b/worldsim/src/region/meta.rs index 00573b5570..27dd412383 100644 --- a/worldsim/src/region/meta.rs +++ b/worldsim/src/region/meta.rs @@ -7,8 +7,6 @@ pub struct RegionMeta { impl RegionMeta { pub fn new(id: RegionId) -> Self { - Self { - id, - } + Self { id } } -} \ No newline at end of file +} diff --git a/worldsim/src/region/mod.rs b/worldsim/src/region/mod.rs index 17a6f98251..02c5026eb7 100644 --- a/worldsim/src/region/mod.rs +++ b/worldsim/src/region/mod.rs @@ -1,14 +1,11 @@ -pub mod meta; mod lod; +pub mod meta; use std::sync::Arc; -use crate::{ - regionmanager::meta::RegionId, - job::JobManager, -}; +use crate::{job::JobManager, regionmanager::meta::RegionId}; use lod::terrain::TerrainLod; -#[derive(Debug, Clone)] +#[derive(Clone)] pub struct Region { id: RegionId, jobmanager: Arc, @@ -34,39 +31,33 @@ impl Region { } } -fn rasterize(region: &Region) -> Vec { - let mut res = Vec::new(); +fn _rasterize(_region: &Region) -> Vec { + let mut _res = Vec::new(); // iterate over all Region9 / chunk5 / Block0 / subBlock that dont have children in RECT XYZ //region.block - res + _res } - - -fn plant_trees(region: &Region) -> Vec { - let mut res = Vec::new(); +fn _plant_trees(_region: &Region) -> Vec { + let mut _res = Vec::new(); // iterate over all Region9 / chunk5 / Block0 / subBlock that dont have children in RECT XYZ // acces blocks around - res + _res } - - -fn corrosion(region: &Region) -> Vec { - let mut res = Vec::new(); +fn _corrosion(_region: &Region) -> Vec { + let mut _res = Vec::new(); // iterate over all Region9 / chunk5 / Block0 / subBlock that dont have children in RECT XYZ // access neighbours - res + _res } - - /* pub type aaa = LodLayer; @@ -118,4 +109,4 @@ mod tests { }*/ } -*/ \ No newline at end of file +*/ diff --git a/worldsim/src/regionmanager/meta.rs b/worldsim/src/regionmanager/meta.rs index 07ef5f371a..81d38d0925 100644 --- a/worldsim/src/regionmanager/meta.rs +++ b/worldsim/src/regionmanager/meta.rs @@ -7,25 +7,37 @@ pub struct Server { impl Server { pub fn new(connection_details: String) -> Self { - Self { - connection_details, - } + Self { connection_details } } } #[derive(Debug, Clone, Copy)] pub enum RegionManagerMsg { - Attached{server_id: u64, seed: u64}, - NewServerInMesh{server_id: u64, server_connection_details: ()}, - CreateRegion{region_id: RegionId}, - TakeOverRegionFrom{region_id: RegionId, server_id: u64}, + Attached { + server_id: u64, + seed: u64, + }, + NewServerInMesh { + server_id: u64, + server_connection_details: (), + }, + CreateRegion { + region_id: RegionId, + }, + TakeOverRegionFrom { + region_id: RegionId, + server_id: u64, + }, } pub type RegionIdSize = i8; -pub type RegionId = (/*x*/RegionIdSize, /*y*/RegionIdSize /*z = 0*/); +pub type RegionId = ( + /*x*/ RegionIdSize, + /*y*/ RegionIdSize, /*z = 0*/ +); -pub const RegionMIN:i8 = -64; -pub const RegionMAX:i8 = 63; +pub const REGION_MIN: i8 = -64; +pub const REGION_MAX: i8 = 63; #[derive(Debug, Clone)] pub struct Region { diff --git a/worldsim/src/regionmanager/mod.rs b/worldsim/src/regionmanager/mod.rs index ecf84e768e..aa35475dda 100644 --- a/worldsim/src/regionmanager/mod.rs +++ b/worldsim/src/regionmanager/mod.rs @@ -1,21 +1,15 @@ pub mod meta; +use crate::{ + regionmanager::meta::{Region, RegionId, RegionManagerMsg, Server, REGION_MAX, REGION_MIN}, + server::meta::ServerMsg, +}; +use std::collections::HashMap; use std::sync::{ - Arc, - mpsc, atomic::{AtomicBool, Ordering}, + mpsc, Arc, }; use std::thread::sleep; -use std::collections::HashMap; use std::time::Duration; -use crate::{ - regionmanager::meta::{ - Server, Region, RegionId, RegionManagerMsg, RegionMIN, RegionMAX, - }, - server::meta::{ - ServerMsg, - }, - job::JobManager, -}; #[derive(Debug)] pub struct RegionManager { @@ -26,17 +20,16 @@ pub struct RegionManager { regions: HashMap, } -impl RegionManager{ +impl RegionManager { pub fn new(tx: mpsc::Sender, rx: mpsc::Receiver) -> Self { - let running = Arc::new(AtomicBool::new(true)); - let mut servers = vec!(); + let servers = vec![]; let mut regions = HashMap::new(); - for x in RegionMIN..RegionMAX { - for y in RegionMIN..RegionMAX { - regions.insert((x,y), Region::new(None)); + for x in REGION_MIN..REGION_MAX { + for y in REGION_MIN..REGION_MAX { + regions.insert((x, y), Region::new(None)); } } @@ -54,19 +47,27 @@ impl RegionManager{ //It is widely important, that it causes as minimal shifting as necessary //.... fell f*** it for now - for x in RegionMIN..RegionMAX { - for y in RegionMIN..RegionMAX { + for x in REGION_MIN..REGION_MAX { + for y in REGION_MIN..REGION_MAX { if !self.servers.is_empty() { - let old = self.regions.get(&(x,y)).unwrap().server_id; + let old = self.regions.get(&(x, y)).unwrap().server_id; - self.regions.get_mut(&(x,y)).unwrap().server_id = Some(((x as usize) % self.servers.len()) as u8); + self.regions.get_mut(&(x, y)).unwrap().server_id = + Some(((x as usize) % self.servers.len()) as u8); if let Some(id) = old { - self.tx.send(RegionManagerMsg::TakeOverRegionFrom{region_id: (x,y), server_id: id as u64}); + self.tx + .send(RegionManagerMsg::TakeOverRegionFrom { + region_id: (x, y), + server_id: id as u64, + }) + .unwrap(); } else { - self.tx.send(RegionManagerMsg::CreateRegion{region_id: (x,y)}); + self.tx + .send(RegionManagerMsg::CreateRegion { region_id: (x, y) }) + .unwrap(); } } else { - self.regions.get_mut(&(x,y)).unwrap().server_id = None; + self.regions.get_mut(&(x, y)).unwrap().server_id = None; } } } @@ -81,15 +82,20 @@ impl RegionManager{ match msg { ServerMsg::Attach() => { //ERROR i cannot acceess self here ... - self.servers.push(Server::new("Hello".to_string()) ); - self.tx.send(RegionManagerMsg::Attached{server_id: self.servers.len() as u64 , seed: 1337}); + self.servers.push(Server::new("Hello".to_string())); + self.tx + .send(RegionManagerMsg::Attached { + server_id: self.servers.len() as u64, + seed: 1337, + }) + .unwrap(); error!("yay"); println!("attached"); self.rearange(); } } - }, - Err(e) => { + } + Err(_e) => { //panic!("Work error {:?}", e); } } diff --git a/worldsim/src/server/meta.rs b/worldsim/src/server/meta.rs index 39e7e7ad4e..75e314bea7 100644 --- a/worldsim/src/server/meta.rs +++ b/worldsim/src/server/meta.rs @@ -4,7 +4,10 @@ pub enum ServerMsg { } pub type RegionIdSize = i8; -pub type RegionId = (/*x*/RegionIdSize, /*y*/RegionIdSize /*z = 0*/); +pub type RegionId = ( + /*x*/ RegionIdSize, + /*y*/ RegionIdSize, /*z = 0*/ +); -pub const RegionMIN:i8 = -64; -pub const RegionMAX:i8 = 63; \ No newline at end of file +pub const REGION_MIN: i8 = -64; +pub const REGION_MAX: i8 = 63; diff --git a/worldsim/src/server/mod.rs b/worldsim/src/server/mod.rs index 85871ac2ea..f5303dc4de 100644 --- a/worldsim/src/server/mod.rs +++ b/worldsim/src/server/mod.rs @@ -1,25 +1,20 @@ pub mod meta; +use std::collections::HashMap; use std::sync::{ - mpsc, - Arc, atomic::{AtomicBool, Ordering}, + mpsc, Arc, }; use std::thread::sleep; use std::time::Duration; -use std::collections::HashMap; use crate::{ - job::JobManager, - regionmanager::meta::RegionManagerMsg, - region::Region, - server::meta::RegionId, + job::JobManager, region::Region, regionmanager::meta::RegionManagerMsg, server::meta::RegionId, server::meta::ServerMsg, }; /* one server per physical host */ -#[derive(Debug)] pub struct Server { tx: mpsc::Sender, rx: mpsc::Receiver, @@ -27,12 +22,16 @@ pub struct Server { id: Option, seed: Option, state: u64, - jobmanager: Arc, - region: HashMap, + _jobmanager: Arc, + _region: HashMap, } impl Server { - pub fn new(tx: mpsc::Sender, rx: mpsc::Receiver, jobmanager: Arc) -> Self { + pub fn new( + tx: mpsc::Sender, + rx: mpsc::Receiver, + jobmanager: Arc, + ) -> Self { let running = Arc::new(AtomicBool::new(true)); Self { @@ -42,8 +41,8 @@ impl Server { id: None, seed: None, state: 0, - jobmanager: jobmanager.clone(), - region: HashMap::new(), + _jobmanager: jobmanager.clone(), + _region: HashMap::new(), } } @@ -53,43 +52,53 @@ impl Server { ) -> bool { match self.state { 0 => { - self.tx.send(ServerMsg::Attach()); + self.tx.send(ServerMsg::Attach()).unwrap(); self.state += 1; - }, + } _ => (), } match self.rx.try_recv() { Ok(msg) => { match msg { - RegionManagerMsg::Attached{server_id, seed} => { + RegionManagerMsg::Attached { server_id, seed } => { self.id = Some(server_id); self.seed = Some(seed); - }, - RegionManagerMsg::NewServerInMesh{server_id, server_connection_details} => { - println!("new server found"); - }, - RegionManagerMsg::CreateRegion{region_id} => { + } + RegionManagerMsg::NewServerInMesh { + server_id, + server_connection_details, + } => { + println!( + "new server found {}, details: {:?}", + server_id, server_connection_details + ); + } + RegionManagerMsg::CreateRegion { region_id } => { /* let mut r = Region::new(region_id, self.jobmanager.clone()); r.block.make_at_least(Vec3::new(0,0,0), Vec3::new(65535,65535,65535), 9); self.region.insert(region_id, r); */ - println!("create region"); - }, - RegionManagerMsg::TakeOverRegionFrom{region_id, server_id} => { - println!("new server in mesh"); - }, - _ => (), + println!("create region {:?}", region_id); + } + RegionManagerMsg::TakeOverRegionFrom { + region_id, + server_id, + } => { + println!( + "new server in mesh, region: {:?}, server {}", + region_id, server_id + ); + } //_ => (), } - }, + } Err(e) => { - //panic!("Work error {:?}", e); + debug!("Work error {:?}", e); sleep(Duration::from_millis(10)); } } - self.running.load(Ordering::Relaxed) } }