diff options
Diffstat (limited to 'src/server')
-rw-r--r-- | src/server/connection.rs | 1 | ||||
-rw-r--r-- | src/server/mining.rs | 8 | ||||
-rw-r--r-- | src/server/mod.rs | 1 | ||||
-rw-r--r-- | src/server/navigation.rs | 5 | ||||
-rw-r--r-- | src/server/tractorbeam.rs | 70 |
5 files changed, 79 insertions, 6 deletions
diff --git a/src/server/connection.rs b/src/server/connection.rs index 4fed2b6..662e33d 100644 --- a/src/server/connection.rs +++ b/src/server/connection.rs @@ -52,6 +52,7 @@ impl ServerConnection { ModuleType::Refinery => self.server_refinery(&mut masses), ModuleType::Dashboard => self.server_dashboard(&mut masses), ModuleType::Navigation => self.server_navigation(&mut masses), + ModuleType::Tractorbeam => self.server_tractorbeam(&mut masses), ModuleType::Construction => self.server_construction(&mut masses), } } diff --git a/src/server/mining.rs b/src/server/mining.rs index 6213e65..f6ddb41 100644 --- a/src/server/mining.rs +++ b/src/server/mining.rs @@ -5,7 +5,6 @@ use std::io::BufRead; use std::io::Write; use crate::mass::{Mass, MassType}; -use crate::math::distance; use crate::modules::mining::{Mining, MiningStatus}; use crate::modules::navigation::Navigation; use crate::server::connection::ServerConnection; @@ -52,7 +51,8 @@ impl ServerConnection { if let Some(item) = item { if !ship.give(item.clone()) { - let mass = Mass::new_item(item.clone(), ship.position, ship.velocity); + let mass = + Mass::new_item(item.clone(), ship.position.clone(), ship.velocity.clone()); masses.insert(item.name.clone(), mass); } } @@ -109,7 +109,9 @@ fn get_mining_data( let is_within_range = if has_astroid_target { match target { - Some(target) => mining.range > distance(ship.position, target.position), + Some(target) => { + mining.range > ship.position.distance_from(target.position.clone()) + } _ => false, } } else { diff --git a/src/server/mod.rs b/src/server/mod.rs index 0fec4dc..c195f3d 100644 --- a/src/server/mod.rs +++ b/src/server/mod.rs @@ -5,3 +5,4 @@ pub mod engines; pub mod mining; pub mod navigation; pub mod refinery; +pub mod tractorbeam; diff --git a/src/server/navigation.rs b/src/server/navigation.rs index eb9095d..5143667 100644 --- a/src/server/navigation.rs +++ b/src/server/navigation.rs @@ -5,7 +5,6 @@ use std::io::BufRead; use std::io::Write; use crate::mass::{Mass, MassType}; -use crate::math::distance; use crate::server::connection::ServerConnection; impl ServerConnection { @@ -18,11 +17,11 @@ impl ServerConnection { } = ship.mass_type { let navigation = navigation.as_mut().unwrap(); - navigation.verify_target(ship_clone.position, &masses); + navigation.verify_target(ship_clone.position.clone(), &masses); let mut within_range: HashMap<&String, &Mass> = masses .iter() .filter(|&(_, mass)| { - distance(ship_clone.position, mass.position) < navigation.range + ship_clone.position.distance_from(mass.position.clone()) < navigation.range }) .collect(); within_range.insert(&self.name, &ship_clone); diff --git a/src/server/tractorbeam.rs b/src/server/tractorbeam.rs new file mode 100644 index 0000000..250f5fb --- /dev/null +++ b/src/server/tractorbeam.rs @@ -0,0 +1,70 @@ +extern crate serde_json; + +use std::collections::HashMap; +use std::io::BufRead; +use std::io::Write; + +use crate::mass::{Mass, MassType}; +use crate::modules::navigation::NavigationStatus; +use crate::modules::tractorbeam::TractorbeamStatus; +use crate::server::connection::ServerConnection; + +#[derive(Serialize, Deserialize, Debug, Clone)] +pub struct TractorbeamData { + pub has_target: bool, + pub status: TractorbeamStatus, +} + +impl ServerConnection { + pub fn server_tractorbeam(&mut self, masses: &mut HashMap<String, Mass>) { + let mut ship = masses.remove(&self.name).unwrap(); + let ship_clone = ship.clone(); + + if let MassType::Ship { + ref mut tractorbeam, + ref navigation, + .. + } = ship.mass_type + { + let tractorbeam = tractorbeam.as_mut().unwrap(); + let navigation = navigation.as_ref().unwrap(); + + if self.open { + let tractorbeam_data = TractorbeamData { + has_target: navigation.status == NavigationStatus::Targeted, + status: tractorbeam.status.clone(), + }; + + let send = serde_json::to_string(&tractorbeam_data).unwrap() + "\n"; + self.open = match self.stream.write(send.as_bytes()) { + Ok(_result) => true, + Err(_error) => false, + }; + + let mut recv = String::new(); + if let Ok(result) = self.buff_r.read_line(&mut recv) { + match recv.as_bytes() { + b"o\n" => tractorbeam.toggle_pull(), + b"p\n" => tractorbeam.toggle_push(), + b"t\n" => tractorbeam.toggle_bring(5.0), + _ => { + if result == 0 { + self.open = false; + } + } + } + } + } + + if let Some(name) = navigation.target_name.clone() { + let target = masses.get_mut(&name).unwrap(); + let acceleration = tractorbeam.get_acceleration(ship_clone, target.clone()); + target.effects.give_acceleration(acceleration); + } else { + tractorbeam.off(); + } + } + + masses.insert(self.name.clone(), ship); + } +} |