From 070485e093dc540a5db9650d438cffe3d18d3883 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Tue, 12 Feb 2019 13:20:06 -0600 Subject: added constants file, made so items are referred to by an enum, figured better ways than ship_clone --- src/server/construction.rs | 39 +++++++++++++++---------------- src/server/engines.rs | 8 +++++-- src/server/mining.rs | 57 +++++++++++++++++++++++++++------------------- src/server/navigation.rs | 2 +- src/server/refinery.rs | 29 ++++++++++++----------- src/server/tractorbeam.rs | 4 ++-- 6 files changed, 75 insertions(+), 64 deletions(-) (limited to 'src/server') diff --git a/src/server/construction.rs b/src/server/construction.rs index 70038ae..9c700ae 100644 --- a/src/server/construction.rs +++ b/src/server/construction.rs @@ -4,58 +4,52 @@ use std::collections::HashMap; use std::io::BufRead; use std::io::Write; +use crate::constants; +use crate::item::ItemType; use crate::mass::{Mass, MassType}; use crate::modules::construction::Construction; use crate::modules::construction::ConstructionStatus; use crate::modules::types::ModuleType; use crate::server::connection::ServerConnection; +use crate::storage::Storage; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct ConstructionData { pub status: ConstructionStatus, - pub has_refined: bool, + pub has_enough: bool, } impl ServerConnection { pub fn server_construction(&mut self, masses: &mut HashMap) { let mut ship = masses.remove(&self.name).unwrap(); - let ship_clone = ship.clone(); - let mut construct = false; if let MassType::Ship { ref mut construction, + ref mut storage, .. } = ship.mass_type { let construction = construction.as_mut().unwrap(); - let construction_data = get_construction_data(ship_clone.clone(), construction); + let construction_data = get_construction_data(storage, construction); if self.open && self.txrx_construction(&construction_data) { construction.toggle(); } if construction_data.status == ConstructionStatus::Constructed { - construction.take(); + storage.take_items(ItemType::Iron, constants::SHIP_CONSTRUCTION_IRON_COST); masses.insert( "Station".to_string(), Mass::new_station( ModuleType::Refinery, - ship_clone.position, - ship_clone.velocity, + ship.position.clone(), + ship.velocity.clone(), ), ); - construct = true; + construction.taken(); } } - if construct { - ship.take("Refined Mineral"); - ship.take("Refined Mineral"); - ship.take("Refined Mineral"); - ship.take("Refined Mineral"); - ship.take("Refined Mineral"); - } - masses.insert(self.name.clone(), ship); } @@ -69,7 +63,7 @@ impl ServerConnection { if let Ok(result) = self.buff_r.read_line(&mut recv) { match recv.as_bytes() { b"c\n" => { - if construction_data.has_refined { + if construction_data.has_enough { return true; } } @@ -85,11 +79,14 @@ impl ServerConnection { } } -fn get_construction_data(ship: Mass, construction: &Construction) -> ConstructionData { - let has_refined = ship.refined_count() >= 5; - +fn get_construction_data(storage: &Storage, construction: &Construction) -> ConstructionData { ConstructionData { status: construction.status.clone(), - has_refined, + has_enough: storage + .items + .iter() + .filter(|item| item.itemtype == ItemType::Iron) + .count() + >= constants::SHIP_CONSTRUCTION_IRON_COST, } } diff --git a/src/server/engines.rs b/src/server/engines.rs index 559ecd9..f3e68f0 100644 --- a/src/server/engines.rs +++ b/src/server/engines.rs @@ -12,7 +12,6 @@ impl ServerConnection { pub fn server_engines(&mut self, masses: &mut HashMap) { if self.open { let mut ship = masses.remove(&self.name).unwrap(); - let ship_clone = ship.clone(); if let MassType::Ship { ref mut engines, @@ -36,7 +35,12 @@ impl ServerConnection { let mut recv = String::new(); if let Ok(result) = self.buff_r.read_line(&mut recv) { - engines.give_client_data(&ship_clone, target, recv); + engines.give_client_data( + ship.position.clone(), + ship.velocity.clone(), + target, + recv, + ); if result == 0 { self.open = false; } diff --git a/src/server/mining.rs b/src/server/mining.rs index f6ddb41..4c1d3f6 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -4,7 +4,9 @@ use std::collections::HashMap; use std::io::BufRead; use std::io::Write; +use crate::item::ItemType; use crate::mass::{Mass, MassType}; +use crate::math::Vector; use crate::modules::mining::{Mining, MiningStatus}; use crate::modules::navigation::Navigation; use crate::server::connection::ServerConnection; @@ -21,18 +23,17 @@ pub struct MiningData { impl ServerConnection { pub fn server_mining(&mut self, masses: &mut HashMap) { let mut ship = masses.remove(&self.name).unwrap(); - let ship_clone = ship.clone(); - let mut item = None; if let MassType::Ship { ref mut mining, + ref mut storage, ref navigation, .. } = ship.mass_type { let mining = mining.as_mut().unwrap(); let navigation = navigation.as_ref().unwrap(); - let mining_data = get_mining_data(ship_clone, mining, navigation, masses); + let mining_data = get_mining_data(ship.position.clone(), mining, navigation, masses); if self.open && self.txrx_mining(&mining_data) { mining.toggle(); @@ -41,19 +42,28 @@ impl ServerConnection { if !mining_data.is_within_range { mining.off(); } else if mining.status == MiningStatus::Mined { - mining.take(); if let Some(name) = navigation.target_name.clone() { let target = masses.get_mut(&name).unwrap(); - item = target.take("Mineral"); + if let MassType::Astroid { + ref mut resources, .. + } = target.mass_type + { + match resources.take_item(ItemType::CrudeMinerals) { + Some(item) => { + if !storage.give_item(item.clone()) { + let mass = Mass::new_item( + item.clone(), + ship.position.clone(), + ship.velocity.clone(), + ); + masses.insert(item.name.clone(), mass); + } + } + None => mining.off(), + } + } } - } - } - - if let Some(item) = item { - if !ship.give(item.clone()) { - let mass = - Mass::new_item(item.clone(), ship.position.clone(), ship.velocity.clone()); - masses.insert(item.name.clone(), mass); + mining.taken(); } } @@ -86,7 +96,7 @@ impl ServerConnection { } fn get_mining_data( - ship: Mass, + position: Vector, mining: &Mining, navigation: &Navigation, masses: &mut HashMap, @@ -97,21 +107,22 @@ fn get_mining_data( let mut astroid_has_minerals = false; let has_astroid_target = match target { - Some(target) => { - astroid_has_minerals = target.has_minerals(); - match target.mass_type { - MassType::Astroid { .. } => true, - _ => false, + Some(target) => match target.mass_type { + MassType::Astroid { ref resources, .. } => { + astroid_has_minerals = resources + .items + .iter() + .any(|item| item.itemtype == ItemType::CrudeMinerals); + true } - } + _ => false, + }, None => false, }; let is_within_range = if has_astroid_target { match target { - Some(target) => { - mining.range > ship.position.distance_from(target.position.clone()) - } + Some(target) => mining.range > position.distance_from(target.position.clone()), _ => false, } } else { diff --git a/src/server/navigation.rs b/src/server/navigation.rs index 5143667..8fc7731 100644 --- a/src/server/navigation.rs +++ b/src/server/navigation.rs @@ -17,7 +17,7 @@ impl ServerConnection { } = ship.mass_type { let navigation = navigation.as_mut().unwrap(); - navigation.verify_target(ship_clone.position.clone(), &masses); + navigation.verify_target(ship.position.clone(), &masses); let mut within_range: HashMap<&String, &Mass> = masses .iter() .filter(|&(_, mass)| { diff --git a/src/server/refinery.rs b/src/server/refinery.rs index d4eb26b..7cec23e 100644 --- a/src/server/refinery.rs +++ b/src/server/refinery.rs @@ -4,31 +4,34 @@ use std::collections::HashMap; use std::io::BufRead; use std::io::Write; -use crate::item::Item; +use crate::item::{Item, ItemType}; use crate::mass::{Mass, MassType}; use crate::modules::refinery::RefineryStatus; use crate::server::connection::ServerConnection; #[derive(Serialize, Deserialize, Debug, Clone)] pub struct RefineryData { - pub has_minerals: bool, + pub has_crude_minerals: bool, pub status: RefineryStatus, } impl ServerConnection { pub fn server_refinery(&mut self, masses: &mut HashMap) { let mut ship = masses.remove(&self.name).unwrap(); - let ship_clone = ship.clone(); - let mut refine = false; if let MassType::Ship { - ref mut refinery, .. + ref mut refinery, + ref mut storage, + .. } = ship.mass_type { let refinery = refinery.as_mut().unwrap(); let refinery_data = RefineryData { - has_minerals: ship_clone.has_minerals(), + has_crude_minerals: storage + .items + .iter() + .any(|item| item.itemtype == ItemType::CrudeMinerals), status: refinery.status.clone(), }; @@ -36,21 +39,17 @@ impl ServerConnection { refinery.toggle(); } - if !refinery_data.has_minerals { + if !refinery_data.has_crude_minerals { refinery.off(); } if refinery.status == RefineryStatus::Refined { - refinery.take(); - refine = true; + storage.take_item(ItemType::CrudeMinerals); + storage.give_item(Item::new(ItemType::Iron)); + refinery.taken(); } } - if refine { - ship.take("Mineral"); - ship.give(Item::new("Refined Mineral", 1)); - } - masses.insert(self.name.clone(), ship); } @@ -64,7 +63,7 @@ impl ServerConnection { if let Ok(result) = self.buff_r.read_line(&mut recv) { match recv.as_bytes() { b"R\n" => { - if refinery_data.has_minerals { + if refinery_data.has_crude_minerals { return true; } } diff --git a/src/server/tractorbeam.rs b/src/server/tractorbeam.rs index 250f5fb..fc76b7c 100644 --- a/src/server/tractorbeam.rs +++ b/src/server/tractorbeam.rs @@ -18,7 +18,6 @@ pub struct TractorbeamData { impl ServerConnection { pub fn server_tractorbeam(&mut self, masses: &mut HashMap) { let mut ship = masses.remove(&self.name).unwrap(); - let ship_clone = ship.clone(); if let MassType::Ship { ref mut tractorbeam, @@ -58,7 +57,8 @@ impl ServerConnection { if let Some(name) = navigation.target_name.clone() { let target = masses.get_mut(&name).unwrap(); - let acceleration = tractorbeam.get_acceleration(ship_clone, target.clone()); + let acceleration = + tractorbeam.get_acceleration(ship.position.clone(), target.clone()); target.effects.give_acceleration(acceleration); } else { tractorbeam.off(); -- cgit v1.2.3