From 46fa862e04bc43311ba79ef3db70abf9014b9104 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Sun, 3 Feb 2019 05:45:35 -0600 Subject: bringing to 2018 --- src/mass.rs | 168 ++++++++++++++++++++++++++++++++++-------------------------- 1 file changed, 95 insertions(+), 73 deletions(-) (limited to 'src/mass.rs') diff --git a/src/mass.rs b/src/mass.rs index f09685f..db97d49 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -1,44 +1,44 @@ extern crate rand; -use self::rand::distributions::Range; -use self::rand::distributions::Sample; - -use item::Item; -use storage::Storage; -use modules::mining::Mining; -use modules::engines::Engines; -use modules::types::ModuleType; -use modules::refinery::Refinery; -use modules::dashboard::Dashboard; -use modules::navigation::Navigation; -use modules::construction::Construction; +use self::rand::distributions::Uniform; +use self::rand::Rng; + +use crate::item::Item; +use crate::modules::construction::Construction; +use crate::modules::dashboard::Dashboard; +use crate::modules::engines::Engines; +use crate::modules::mining::Mining; +use crate::modules::navigation::Navigation; +use crate::modules::refinery::Refinery; +use crate::modules::types::ModuleType; +use crate::storage::Storage; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct Mass { - pub mass_type : MassType, - pub position : (f64, f64, f64), - pub velocity : (f64, f64, f64), + pub mass_type: MassType, + pub position: (f64, f64, f64), + pub velocity: (f64, f64, f64), } #[derive(Serialize, Deserialize, Debug, Clone)] pub enum MassType { Ship { - storage : Storage, - mining : Option, - engines : Option, - refinery : Option, - dashboard : Option, - navigation : Option, + storage: Storage, + mining: Option, + engines: Option, + refinery: Option, + dashboard: Option, + navigation: Option, construction: Option, }, Astroid { - resources : Storage, + resources: Storage, }, Item { - item : Item, + item: Item, }, Station { - module_type : ModuleType, + module_type: ModuleType, }, } @@ -46,64 +46,73 @@ impl Mass { pub fn new_astroid() -> Mass { let mut rng = rand::thread_rng(); - let mut pr = Range::new(-50.0, 50.0); - let position = (pr.sample(&mut rng), pr.sample(&mut rng), pr.sample(&mut rng)); + let p_range = Uniform::new(-50.0, 50.0); + let position = ( + rng.sample(p_range), + rng.sample(p_range), + rng.sample(p_range), + ); - let mut vr = Range::new(-0.5, 0.5); - let velocity = (vr.sample(&mut rng), vr.sample(&mut rng), vr.sample(&mut rng)); + let v_range = Uniform::new(-0.5, 0.5); + let velocity = ( + rng.sample(v_range), + rng.sample(v_range), + rng.sample(v_range), + ); - let mut rr = Range::new(0, 20); let mut resources = Vec::new(); - for _ in 0..rr.sample(&mut rng) { + for _ in 0..rng.gen_range(0, 20) { resources.push(Item::new("Mineral", 1)); } let astroid = MassType::Astroid { - resources : Storage::new(resources), + resources: Storage::new(resources), }; Mass { - mass_type : astroid, - position : position, - velocity : velocity, + mass_type: astroid, + position, + velocity, } } pub fn new_ship() -> Mass { let ship = MassType::Ship { - mining : Some(Mining::new()), - engines : Some(Engines::new()), - refinery : Some(Refinery::new()), - dashboard : Some(Dashboard::new()), - navigation : Some(Navigation::new()), + mining: Some(Mining::new()), + engines: Some(Engines::new()), + refinery: Some(Refinery::new()), + dashboard: Some(Dashboard::new()), + navigation: Some(Navigation::new()), construction: Some(Construction::new()), - storage : Storage::new(Vec::new()), + storage: Storage::new(Vec::new()), }; Mass { - mass_type : ship, - position : (0.0, 0.0, 0.0), - velocity : (0.0, 0.0, 0.0), + mass_type: ship, + position: (0.0, 0.0, 0.0), + velocity: (0.0, 0.0, 0.0), } } - pub fn new_item(item : Item, position : (f64, f64, f64), velocity : (f64, f64, f64)) -> Mass { + pub fn new_item(item: Item, position: (f64, f64, f64), velocity: (f64, f64, f64)) -> Mass { Mass { - mass_type : MassType::Item{item : item}, - position : position, - velocity : velocity, + mass_type: MassType::Item { item }, + position, + velocity, } } - pub fn new_station(module_type : ModuleType, position : (f64, f64, f64), velocity : (f64, f64, f64)) -> Mass { - let mass_type = MassType::Station { - module_type : module_type - }; + pub fn new_station( + module_type: ModuleType, + position: (f64, f64, f64), + velocity: (f64, f64, f64), + ) -> Mass { + let mass_type = MassType::Station { module_type }; Mass { - mass_type : mass_type, - position : position, - velocity : velocity, + mass_type, + position, + velocity, } } @@ -120,15 +129,20 @@ impl Mass { pub fn process(&mut self) { let mut acceleration = (0.0, 0.0, 0.0); - match self.mass_type { - MassType::Ship{ref mut navigation, ref mut engines, ref mut mining, ref mut refinery, ref mut construction, ..} => { - mining.as_mut().unwrap().process(); - refinery.as_mut().unwrap().process(); - navigation.as_mut().unwrap().process(); - construction.as_mut().unwrap().process(); - acceleration = engines.as_mut().unwrap().recv_acceleration(); - }, - _ => (), + if let MassType::Ship { + ref mut navigation, + ref mut engines, + ref mut mining, + ref mut refinery, + ref mut construction, + .. + } = self.mass_type + { + mining.as_mut().unwrap().process(); + refinery.as_mut().unwrap().process(); + navigation.as_mut().unwrap().process(); + construction.as_mut().unwrap().process(); + acceleration = engines.as_mut().unwrap().recv_acceleration(); } self.accelerate(acceleration); self.position.0 += self.velocity.0; @@ -136,7 +150,7 @@ impl Mass { self.position.2 += self.velocity.2; } - pub fn accelerate(&mut self, acceleration : (f64, f64, f64)) { + pub fn accelerate(&mut self, acceleration: (f64, f64, f64)) { self.velocity.0 += acceleration.0; self.velocity.1 += acceleration.1; self.velocity.2 += acceleration.2; @@ -144,31 +158,39 @@ impl Mass { pub fn has_minerals(&self) -> bool { match self.mass_type { - MassType::Ship{ref storage, ..} => storage.has_minerals(), - MassType::Astroid{ref resources, ..} => resources.has_minerals(), + MassType::Ship { ref storage, .. } => storage.has_minerals(), + MassType::Astroid { ref resources, .. } => resources.has_minerals(), _ => false, } } pub fn refined_count(&self) -> usize { match self.mass_type { - MassType::Ship{ref storage, ..} => storage.refined_count(), + MassType::Ship { ref storage, .. } => storage.refined_count(), _ => 0, } } - pub fn take(&mut self, name : &str) -> Option { + pub fn take(&mut self, name: &str) -> Option { match self.mass_type { - MassType::Ship{ref mut storage, ..} => storage.take(name), - MassType::Astroid{ref mut resources, ..} => resources.take(name), + MassType::Ship { + ref mut storage, .. + } => storage.take(name), + MassType::Astroid { + ref mut resources, .. + } => resources.take(name), _ => None, } } - pub fn give(&mut self, item : Item) -> bool { + pub fn give(&mut self, item: Item) -> bool { match self.mass_type { - MassType::Ship{ref mut storage, ..} => storage.give(item), - MassType::Astroid{ref mut resources, ..} => resources.give(item), + MassType::Ship { + ref mut storage, .. + } => storage.give(item), + MassType::Astroid { + ref mut resources, .. + } => resources.give(item), _ => false, } } -- cgit v1.2.3