summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-04-16 02:28:33 -0500
committertom barrett <spalf0@gmail.com>2018-04-16 02:28:33 -0500
commit2940809bf50c2874487ba55efb17ba03c52dd9c2 (patch)
tree2a69facf7922c38256e5886484ad45a566cda970 /src
parent4d7a7e368a3f0a6b1261e1b9180a2647eb158047 (diff)
-removed trait objects and went to structs with enumerations completely
Diffstat (limited to 'src')
-rw-r--r--src/bin/client.rs17
-rw-r--r--src/client/navigation.rs96
-rw-r--r--src/connection.rs16
-rw-r--r--src/lib.rs2
-rw-r--r--src/mass.rs27
-rw-r--r--src/mining.rs30
-rw-r--r--src/module.rs90
-rw-r--r--src/server/dashboard.rs2
-rw-r--r--src/server/engines.rs137
-rw-r--r--src/server/mining.rs119
-rw-r--r--src/server/navigation.rs65
-rw-r--r--src/targeting.rs62
12 files changed, 351 insertions, 312 deletions
diff --git a/src/bin/client.rs b/src/bin/client.rs
index b690094..1766a59 100644
--- a/src/bin/client.rs
+++ b/src/bin/client.rs
@@ -2,11 +2,12 @@ extern crate space;
extern crate serde_json;
use std::io;
-use std::io::prelude::*;
use std::io::BufReader;
+use std::io::prelude::*;
use std::net::TcpStream;
+use std::collections::BTreeMap;
-use space::module::ModuleType;
+use space::module::{Module, ModuleType};
use space::client::mining::client_mining;
use space::client::engines::client_engines;
use space::client::dashboard::client_dashboard;
@@ -31,24 +32,24 @@ fn main() {
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
- let modules : Vec<ModuleType> = serde_json::from_str(&recv.replace("\n","")).unwrap();
+ let modules : BTreeMap<String, Module> = serde_json::from_str(&recv.replace("\n","")).unwrap();
println!("Choose your module:");
- for (i, module) in modules.iter().enumerate() {
+ for (i, module) in modules.keys().enumerate() {
println!("{}) {:?}", i, module);
}
let mut choice = String::new();
io::stdin().read_line(&mut choice).expect("Failed");
- let module = modules[choice.replace("\n", "").parse::<usize>().unwrap()].clone();
+ let module = modules.values().nth(choice.replace("\n", "").parse::<usize>().unwrap()).unwrap();
let send = serde_json::to_string(&module).unwrap() + "\n";
stream.write(send.as_bytes()).unwrap();
- match module {
- ModuleType::Mining => client_mining(stream, buff_r),
+ match module.module_type {
ModuleType::Dashboard => client_dashboard(buff_r),
ModuleType::Engines => client_engines(stream, buff_r),
- ModuleType::Navigation => client_navigation(name, stream, buff_r),
+ ModuleType::Mining{..} => client_mining(stream, buff_r),
+ ModuleType::Navigation{..} => client_navigation(name, stream, buff_r),
}
}
diff --git a/src/client/navigation.rs b/src/client/navigation.rs
index 171d050..59b6987 100644
--- a/src/client/navigation.rs
+++ b/src/client/navigation.rs
@@ -1,5 +1,4 @@
extern crate termion;
-//extern crate itertools;
extern crate serde_json;
use std::net::TcpStream;
@@ -11,6 +10,7 @@ use self::termion::raw::IntoRawMode;
use math::distance;
use mass::{Mass, MassType};
+use module::ModuleType;
pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader<TcpStream>){
let stdout = stdout();
@@ -27,55 +27,61 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
termion::cursor::Goto(1,1)).unwrap();
let ship = within_range.remove(&name).unwrap();
- let targeting = match ship.mass_type {
- MassType::Ship{ref targeting, ..} => Some(targeting.clone()),
- _ => None,
- }.unwrap();
- for (i, (mass_name, mass)) in within_range.iter().enumerate() {
+ match ship.mass_type {
+ MassType::Ship{ref modules, ..} => {
+ match modules.get("Navigation").unwrap().module_type {
+ ModuleType::Navigation{ref status, ref target_name, ..} => {
+ for (i, (mass_name, mass)) in within_range.iter().enumerate() {
+ let target_data = match target_name.clone() {
+ Some(target_name) => {
+ if &target_name == mass_name {
+ serde_json::to_string(status).unwrap()
+ }
+ else {
+ String::new()
+ }
+ }
+ None => String::new(),
+ };
- let target_data = match targeting.target.clone() {
- Some(target_name) => {
- if &target_name == mass_name {
- serde_json::to_string(&targeting.status).unwrap()
- }
- else {
- String::new()
- }
- }
- None => String::new(),
- };
-
- write!(stdout, "{}{}) {} ({:.2}, {:.2}, {:.2}) Distance : {:.2} {}",
- termion::cursor::Goto(1, 2 + i as u16),
- i,
- mass_name,
- mass.position.0,
- mass.position.1,
- mass.position.2,
- distance(mass.position, ship.position),
- target_data
- ).unwrap();
- }
+ write!(stdout, "{}{}) {} ({:.2}, {:.2}, {:.2}) Distance : {:.2} {}",
+ termion::cursor::Goto(1, 2 + i as u16),
+ i,
+ mass_name,
+ mass.position.0,
+ mass.position.1,
+ mass.position.2,
+ distance(mass.position, ship.position),
+ target_data
+ ).unwrap();
+ }
- match stdin.next() {
- Some(c) => {
- let c = c.unwrap() as char;
- if c == 'q' {
- break;
+ match stdin.next() {
+ Some(c) => {
+ let c = c.unwrap() as char;
+ if c == 'q' {
+ break;
+ }
+ else {
+ let i = c.to_digit(10).unwrap() as usize;
+ if i < within_range.len() {
+ let mut send = String::new();
+ send.push_str(within_range.iter().nth(i).unwrap().0);
+ send.push_str("\n");
+ stream.write(send.as_bytes()).unwrap();
+ }
+ }
+ }
+ None => ()
+ }
+ },
+ _ => (),
}
- else {
- let i = c.to_digit(10).unwrap() as usize;
- if i < within_range.len() {
- let mut send = String::new();
- send.push_str(within_range.iter().nth(i).unwrap().0);
- send.push_str("\n");
- stream.write(send.as_bytes()).unwrap();
- }
- }
- }
- None => ()
+ },
+ _ => (),
}
+
stdout.flush().unwrap();
}
}
diff --git a/src/connection.rs b/src/connection.rs
index 52d3352..f4637e4 100644
--- a/src/connection.rs
+++ b/src/connection.rs
@@ -5,12 +5,12 @@ use std::io::prelude::*;
use std::net::TcpStream;
use std::collections::HashMap;
-use module::ModuleType;
+use module::{Module, ModuleType};
use mass::{Mass, MassType};
pub struct Connection {
pub name : String,
- pub module_type : ModuleType,
+ pub module : Module,
pub stream : TcpStream,
pub buff_r : BufReader<TcpStream>,
pub open : bool,
@@ -34,12 +34,12 @@ impl Connection {
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
- let module_type : ModuleType = serde_json::from_str(&recv.replace("\n","")).unwrap();
+ let module : Module = serde_json::from_str(&recv.replace("\n","")).unwrap();
stream.set_nonblocking(true).unwrap();
Connection {
name : String::from(name),
- module_type : module_type,
+ module : module,
stream : stream,
buff_r : buff_r,
open : true,
@@ -47,11 +47,11 @@ impl Connection {
}
pub fn process(&mut self, mut masses : &mut HashMap<String, Mass>) {
- self.open = match self.module_type {
- ModuleType::Mining => self.server_mining(&mut masses),
- ModuleType::Engines => self.server_engines(&mut masses),
+ self.open = match self.module.module_type {
ModuleType::Dashboard => self.server_dashboard(&mut masses),
- ModuleType::Navigation => self.server_navigation(&mut masses),
+ ModuleType::Engines => self.server_engines(&mut masses),
+ ModuleType::Mining{..} => self.server_mining(&mut masses),
+ ModuleType::Navigation{..} => self.server_navigation(&mut masses),
};
}
}
diff --git a/src/lib.rs b/src/lib.rs
index 97d506d..9b8050c 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -5,9 +5,7 @@ pub mod mass;
pub mod item;
pub mod math;
pub mod module;
-pub mod mining;
pub mod client;
pub mod server;
pub mod storage;
-pub mod targeting;
pub mod connection;
diff --git a/src/mass.rs b/src/mass.rs
index f13e8ce..762c9ab 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -1,12 +1,11 @@
extern crate rand;
+use std::collections::HashMap;
use self::rand::distributions::Range;
use self::rand::distributions::Sample;
use storage::Storage;
-use module::ModuleType;
-use targeting::Targeting;
-use mining::Mining;
+use module::{Module, ModuleType};
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Mass {
@@ -18,9 +17,7 @@ pub struct Mass {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum MassType {
Ship {
- modules : Vec<ModuleType>,
- mining : Mining,
- targeting : Targeting,
+ modules : HashMap<String, Module>,
storage : Storage,
},
Astroid{
@@ -50,17 +47,15 @@ impl Mass {
}
pub fn new_ship() -> Mass {
- let mut modules = Vec::new();
+ let mut modules = HashMap::new();
- modules.push(ModuleType::Navigation);
- modules.push(ModuleType::Engines);
- modules.push(ModuleType::Dashboard);
- modules.push(ModuleType::Mining);
+ modules.insert("Mining".to_string(), Module::new_mining());
+ modules.insert("Engines".to_string(), Module::new_engines());
+ modules.insert("Dashboard".to_string(), Module::new_dashboard());
+ modules.insert("Navigation".to_string(), Module::new_navigation());
let ship = MassType::Ship {
modules : modules,
- mining : Mining::new(),
- targeting : Targeting::new(),
storage : Storage::new(Vec::new()),
};
@@ -76,8 +71,10 @@ impl Mass {
self.position.1 += self.velocity.1;
self.position.2 += self.velocity.2;
match self.mass_type {
- MassType::Ship{ref mut targeting, ..} => {
- targeting.process();
+ MassType::Ship{ref mut modules, ..} => {
+ for module in modules.values_mut() {
+ module.process();
+ }
},
_ => (),
}
diff --git a/src/mining.rs b/src/mining.rs
deleted file mode 100644
index ae4af49..0000000
--- a/src/mining.rs
+++ /dev/null
@@ -1,30 +0,0 @@
-extern crate serde_json;
-
-use std::time::SystemTime;
-
-#[derive(Serialize, Deserialize, Debug, Clone)]
-pub struct Mining {
- pub range : f64,
- pub status : bool,
- time : u64,
- start : Option<SystemTime>,
-}
-
-impl Mining {
- pub fn new() -> Mining {
- Mining {
- range : 10.0,
- status : false,
- time : 1,
- start : None,
- }
- }
-
- pub fn start(&mut self) {
- self.status = true;
- }
-
- pub fn stop(&mut self) {
- self.status = false;
- }
-}
diff --git a/src/module.rs b/src/module.rs
index 937c63c..0318aad 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -1,7 +1,91 @@
-#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
+use std::time::SystemTime;
+
+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
+pub enum NavigationStatus {
+ None,
+ Targeting,
+ Targeted,
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone)]
+pub struct Module {
+ pub module_type : ModuleType,
+}
+
+#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
pub enum ModuleType {
- Mining,
+ Mining {
+ range : f64,
+ status : bool,
+ time : u64,
+ start : Option<SystemTime>,
+ },
+ Navigation {
+ range : f64,
+ status : NavigationStatus,
+ time : u64,
+ start : Option<SystemTime>,
+ target_name : Option<String>,
+ },
Engines,
Dashboard,
- Navigation,
+}
+
+impl Module {
+ pub fn new_mining() -> Module {
+ let mining = ModuleType::Mining {
+ range : 10.0,
+ status : false,
+ time : 1,
+ start : None,
+ };
+
+ Module {
+ module_type : mining,
+ }
+ }
+
+ pub fn new_navigation() -> Module {
+ let navigation = ModuleType::Navigation {
+ target_name : None,
+ range : 100.0,
+ status : NavigationStatus::None,
+ time : 3,
+ start : None,
+ };
+
+ Module {
+ module_type : navigation,
+ }
+ }
+
+ pub fn new_dashboard() -> Module {
+ Module {
+ module_type : ModuleType::Dashboard,
+ }
+ }
+
+ pub fn new_engines() -> Module {
+ Module {
+ module_type : ModuleType::Engines,
+ }
+ }
+
+ pub fn process(&mut self) {
+ match self.module_type {
+ ModuleType::Navigation{ref mut status, ref mut start, ref time, ..} => {
+ match start.clone() {
+ Some(timer) => {
+ if timer.elapsed().unwrap().as_secs() > *time {
+ *status = NavigationStatus::Targeted;
+ *start = None;
+ }
+ }
+ _ => (),
+ }
+ },
+ ModuleType::Mining{..} => (),
+ _ => (),
+ }
+ }
}
diff --git a/src/server/dashboard.rs b/src/server/dashboard.rs
index 82f73f9..81ac522 100644
--- a/src/server/dashboard.rs
+++ b/src/server/dashboard.rs
@@ -9,7 +9,7 @@ use connection::Connection;
impl Connection {
pub fn server_dashboard(&mut self, masses : &mut HashMap<String, Mass>) -> bool {
let ship = masses.get(&self.name).unwrap();
- let send = serde_json::to_string(&ship).unwrap();
+ let send = serde_json::to_string(&ship).unwrap() + "\n";
match self.stream.write(send.as_bytes()) {
Ok(_result) => true,
Err(_error) => false,
diff --git a/src/server/engines.rs b/src/server/engines.rs
index aa0e703..85346f6 100644
--- a/src/server/engines.rs
+++ b/src/server/engines.rs
@@ -5,82 +5,89 @@ use std::io::BufRead;
use std::collections::HashMap;
use mass::{Mass, MassType};
+use module::{ModuleType};
use connection::Connection;
-use targeting::TargetingStatus;
+use module::NavigationStatus;
impl Connection {
pub fn server_engines(&mut self, masses : &mut HashMap<String, Mass>) -> bool {
let masses_clone = masses.clone();
let ship = masses.get_mut(&self.name).unwrap();
- let targeting = match ship.mass_type {
- MassType::Ship{ref targeting, ..} => Some(targeting.clone()),
- _ => None,
- }.unwrap();
+ let mut acceleration = (0.0, 0.0, 0.0);
- let targeted = targeting.status == TargetingStatus::Targeted;
- let send = serde_json::to_string(&targeted).unwrap() + "\n";
+ match ship.mass_type {
+ MassType::Ship{ref modules, ..} => {
+ match modules.get("Navigation").unwrap().module_type {
+ ModuleType::Navigation{ref status, ref target_name, ..} => {
+ let targeted = status == &NavigationStatus::Targeted;
- match self.stream.write(send.as_bytes()) {
- Ok(_result) => (),
- Err(_error) => return false,
- }
+ let send = serde_json::to_string(&targeted).unwrap() + "\n";
+ match self.stream.write(send.as_bytes()) {
+ Ok(_result) => (),
+ Err(_error) => return false,
+ }
- let mut acceleration = (0.0, 0.0, 0.0);
- let mut recv = String::new();
- match self.buff_r.read_line(&mut recv) {
- Ok(result) => match recv.as_bytes() {
- b"5\n" => acceleration.0 += 0.1,
- b"0\n" => acceleration.0 -= 0.1,
- b"8\n" => acceleration.1 += 0.1,
- b"2\n" => acceleration.1 -= 0.1,
- b"4\n" => acceleration.2 += 0.1,
- b"6\n" => acceleration.2 -= 0.1,
- b"+\n" => {
- let m_v = ship.velocity;
- acceleration = (m_v.0 * 0.05,
- m_v.1 * 0.05,
- m_v.2 * 0.05);
- },
- b"-\n" => {
- let m_v = ship.velocity;
- acceleration = (-1.0 * m_v.0 * 0.05,
- -1.0 * m_v.1 * 0.05,
- -1.0 * m_v.2 * 0.05);
- },
- b"c\n" => {
- match targeting.target {
- Some(name) => {
- let target = masses_clone.get(&name).unwrap();
- let d_v = target.velocity;
- let m_v = ship.velocity;
- acceleration = (d_v.0 - m_v.0,
- d_v.1 - m_v.1,
- d_v.2 - m_v.2);
- },
- None => (),
- }
- },
- b"t\n" => {
- match targeting.target {
- Some(name) => {
- let target = masses_clone.get(&name).unwrap();
- let d_p = target.position;
- let m_p = ship.position;
- acceleration = ((d_p.0 - m_p.0) * 0.01,
- (d_p.1 - m_p.1) * 0.01,
- (d_p.2 - m_p.2) * 0.01);
- },
- None => (),
- }
- },
- _ => {
- if result == 0 {
- return false
- }
- },
+ let mut recv = String::new();
+ match self.buff_r.read_line(&mut recv) {
+ Ok(result) => match recv.as_bytes() {
+ b"5\n" => acceleration.0 += 0.1,
+ b"0\n" => acceleration.0 -= 0.1,
+ b"8\n" => acceleration.1 += 0.1,
+ b"2\n" => acceleration.1 -= 0.1,
+ b"4\n" => acceleration.2 += 0.1,
+ b"6\n" => acceleration.2 -= 0.1,
+ b"+\n" => {
+ let m_v = ship.velocity;
+ acceleration = (m_v.0 * 0.05,
+ m_v.1 * 0.05,
+ m_v.2 * 0.05);
+ },
+ b"-\n" => {
+ let m_v = ship.velocity;
+ acceleration = (-1.0 * m_v.0 * 0.05,
+ -1.0 * m_v.1 * 0.05,
+ -1.0 * m_v.2 * 0.05);
+ },
+ b"c\n" => {
+ match target_name {
+ &Some(ref name) => {
+ let target = masses_clone.get(name).unwrap();
+ let d_v = target.velocity;
+ let m_v = ship.velocity;
+ acceleration = (d_v.0 - m_v.0,
+ d_v.1 - m_v.1,
+ d_v.2 - m_v.2);
+ },
+ &None => (),
+ }
+ },
+ b"t\n" => {
+ match target_name {
+ &Some(ref name) => {
+ let target = masses_clone.get(name).unwrap();
+ let d_p = target.position;
+ let m_p = ship.position;
+ acceleration = ((d_p.0 - m_p.0) * 0.01,
+ (d_p.1 - m_p.1) * 0.01,
+ (d_p.2 - m_p.2) * 0.01);
+ },
+ &None => (),
+ }
+ },
+ _ => {
+ if result == 0 {
+ return false
+ }
+ },
+ },
+ Err(_error) => (),
+ }
+ },
+ _ => (),
+ }
},
- Err(_error) => (),
+ _ => (),
}
ship.accelerate(acceleration);
diff --git a/src/server/mining.rs b/src/server/mining.rs
index ff26840..98803c5 100644
--- a/src/server/mining.rs
+++ b/src/server/mining.rs
@@ -6,6 +6,7 @@ use std::collections::HashMap;
use math::distance;
use mass::{Mass, MassType};
+use module::ModuleType;
use connection::Connection;
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -21,60 +22,90 @@ impl Connection {
let masses_clone = masses.clone();
let ship = masses.get_mut(&self.name).unwrap();
- let (mining, targeting) = match ship.mass_type {
- MassType::Ship{ref targeting, ref mut mining, ..} => (Some(mining), Some(targeting.clone())),
- _ => (None, None),
- };
- let mining = mining.unwrap();
- let target = masses_clone.get(&targeting.unwrap().target.unwrap());
- let has_astroid_target = match target {
- Some(target) => match target.mass_type {
- MassType::Astroid{..} => true,
- _ => false,
- },
- None => false,
- };
+ match ship.mass_type {
+ MassType::Ship{ref modules, ..} => {
+ let mut mining_range = 0.0;
+ let mut mining_status = false;
- let is_within_range = match has_astroid_target {
- true => match target {
- Some(target) => mining.range > distance(ship.position, target.position),
- _ => false,
- }
- _ => false,
- };
+ match modules.get("Mining").unwrap().module_type {
+ ModuleType::Mining{ref range, ref status, ..} => {
+ mining_range = range.clone();
+ mining_status = status.clone();
+ }
+ _ => (),
+ }
- let send = serde_json::to_string(&ServerData {
- has_astroid_target : has_astroid_target,
- is_within_range : is_within_range,
- mining_range : mining.range,
- mining_status : mining.status,
- }).unwrap() + "\n";
+ match modules.get("Navigation").unwrap().module_type {
+ ModuleType::Navigation{ref target_name, ..} => {
+ let mut has_astroid_target = false;
+ let mut is_within_range = false;
+ match target_name.clone() {
+ Some(name) => {
+ let target = masses_clone.get(&name);
+ has_astroid_target = match target {
+ Some(target) => match target.mass_type {
+ MassType::Astroid{..} => true,
+ _ => false,
+ },
+ None => false,
+ };
+ is_within_range = match has_astroid_target {
+ true => match target {
+ Some(target) => mining_range > distance(ship.position, target.position),
+ _ => false,
+ }
+ _ => false,
+ };
+ }
+ _ => (),
+ }
- match self.stream.write(send.as_bytes()) {
- Ok(_result) => (),
- Err(_error) => return false,
- }
+ let send = serde_json::to_string(&ServerData {
+ has_astroid_target : has_astroid_target,
+ is_within_range : is_within_range,
+ mining_range : mining_range,
+ mining_status : mining_status,
+ }).unwrap() + "\n";
- let mut recv = String::new();
- match self.buff_r.read_line(&mut recv) {
- Ok(result) => match recv.as_bytes() {
- b"F\n" => {
- if is_within_range {
- match mining.status {
- true => mining.stop(),
- false => mining.start(),
+ match self.stream.write(send.as_bytes()) {
+ Ok(_result) => (),
+ Err(_error) => return false,
}
}
- },
- _ => {
- if result == 0 {
- return false
+ _ => (),
+ }
+
+ match modules.get("Mining").unwrap().module_type {
+ ModuleType::Mining{ref range, ref status, ..} => {
+ let mut recv = String::new();
+ match self.buff_r.read_line(&mut recv) {
+ Ok(result) => match recv.as_bytes() {
+ b"F\n" => {
+ /*
+ if is_within_range {
+ match mining.status {
+ true => mining.stop(),
+ false => mining.start(),
+ }
+ }
+ */
+ },
+ _ => {
+ if result == 0 {
+ return false
+ }
+ },
+ }
+ Err(_error) => (),
+ }
}
- },
+ _ => (),
+ }
}
- Err(_error) => (),
+ _ => (),
}
+
true
}
}
diff --git a/src/server/navigation.rs b/src/server/navigation.rs
index 3813732..58d88e7 100644
--- a/src/server/navigation.rs
+++ b/src/server/navigation.rs
@@ -2,10 +2,12 @@ extern crate serde_json;
use std::io::Write;
use std::io::BufRead;
+use std::time::SystemTime;
use std::collections::HashMap;
-use mass::{Mass, MassType};
use math::distance;
+use module::{ModuleType, NavigationStatus};
+use mass::{Mass, MassType};
use connection::Connection;
impl Connection {
@@ -15,40 +17,45 @@ impl Connection {
let ship_position = ship.position;
match ship.mass_type {
- MassType::Ship{ref mut targeting, ..} => {
- let target_name = targeting.clone().target;
- match target_name {
- Some(target_name) => {
- let target = masses_clone.get(&target_name).unwrap();
- if distance(target.position, ship.position) > targeting.range {
- targeting.target = None;
+ MassType::Ship{ref mut modules, ..} => {
+ match modules.get_mut("Navigation").unwrap().module_type {
+ ModuleType::Navigation{ref mut target_name, ref mut start, ref mut status, ref range, ..} => {
+ match target_name.clone() {
+ Some(name) => {
+ let target = masses_clone.get(&name).unwrap();
+ if distance(target.position, ship.position) > *range {
+ *target_name = None;
+ }
+ },
+ _ => (),
}
- },
- _ => (),
- }
- let within_range : HashMap<&String, &Mass> = masses_clone.iter().filter(|&(_, mass)|
- distance(ship_position, mass.position) < targeting.range)
- .collect();
+ let within_range : HashMap<&String, &Mass> = masses_clone.iter().filter(|&(_, mass)|
+ distance(ship_position, mass.position) < *range)
+ .collect();
- let send = serde_json::to_string(&within_range).unwrap() + "\n";
- match self.stream.write(send.as_bytes()) {
- Ok(_result) => (),
- Err(_error) => return false,
- }
+ let send = serde_json::to_string(&within_range).unwrap() + "\n";
+ match self.stream.write(send.as_bytes()) {
+ Ok(_result) => (),
+ Err(_error) => return false,
+ }
- let mut recv = String::new();
- match self.buff_r.read_line(&mut recv) {
- Ok(_result) => (),
- Err(_error) => (),
- }
- if !recv.is_empty() {
- targeting.give_target(Some(recv.replace("\n", "")));
+ let mut recv = String::new();
+ match self.buff_r.read_line(&mut recv) {
+ Ok(_result) => (),
+ Err(_error) => (),
+ }
+ if !recv.is_empty() {
+ *target_name = Some(recv.replace("\n", ""));
+ *start = Some(SystemTime::now());
+ *status = NavigationStatus::Targeting;
+ }
+ },
+ _ => (),
}
- },
- _ => (),
+ }
+ _ => (),
}
-
true
}
}
diff --git a/src/targeting.rs b/src/targeting.rs
deleted file mode 100644
index 3e566f4..0000000
--- a/src/targeting.rs
+++ /dev/null
@@ -1,62 +0,0 @@
-use std::time::SystemTime;
-
-#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)]
-pub enum TargetingStatus {
- None,
- Targeting,
- Targeted,
-}
-
-#[derive(Serialize, Deserialize, Debug, Clone)]
-pub struct Targeting {
- pub target : Option<String>,
- pub range : f64,
- pub status : TargetingStatus,
- time : u64,
- start : Option<SystemTime>,
-}
-
-impl Targeting {
- pub fn new() -> Targeting {
- Targeting {
- target : None,
- status : TargetingStatus::None,
- range : 100.0,
- time : 3,
- start : None,
- }
- }
-
- pub fn process(&mut self) {
- match self.start {
- Some(time) => {
- if time.elapsed().unwrap().as_secs() > self.time {
- self.status = TargetingStatus::Targeted;
- self.start = None;
- }
- }
- None => (),
- }
- }
- pub fn give_target(&mut self, target : Option<String>) {
- self.target = target;
- match self.target {
- Some(_) => {
- self.status = TargetingStatus::Targeting;
- self.start = Some(SystemTime::now());
- },
- None => {
- self.status = TargetingStatus::None;
- self.start = None;
- }
- }
- }
-
- pub fn recv_target(&self) -> Option<String> {
- self.target.clone()
- }
-
- pub fn recv_status(&self) -> TargetingStatus {
- self.status.clone()
- }
-}