From 74e1c462f6dfa76243917ef358195f057c236b5d Mon Sep 17 00:00:00 2001 From: tom barrett Date: Mon, 12 Mar 2018 08:15:51 -0500 Subject: -better implementation of server navigation and added range checking of targets --- src/navigation.rs | 43 +++++++++++++++++++++++++------------------ src/ship.rs | 4 ++++ 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/src/navigation.rs b/src/navigation.rs index b615eaa..67f53cb 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -79,23 +79,30 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf impl Connection { pub fn server_navigation(&mut self, masses : &mut Vec>) -> bool { - let position = masses[self.index].position(); - let range = masses[self.index].downcast_ref::().unwrap().range(); + let m = masses.to_vec(); + let ship = masses[self.index].downcast_mut::().unwrap(); - { - let within_range : Vec<&Box> = masses.iter().filter(|mass| - distance(position, mass.position()) < range) - .collect(); - let mut send = String::new(); - for mass in within_range { - send.push_str(&mass.serialize()); - send.push_str(";"); - } - send.push_str("\n"); - match self.stream.write(send.as_bytes()) { - Ok(_result) => (), - Err(_error) => return false, + match ship.recv_target() { + Some(index) => { + if distance(m[index].position(), ship.position()) > ship.range() { + ship.give_target(None); + } } + None => (), + } + + let within_range : Vec<&Box> = m.iter().filter(|mass| + distance(ship.position(), mass.position()) < ship.range()) + .collect(); + let mut send = String::new(); + for mass in within_range { + send.push_str(&mass.serialize()); + send.push_str(";"); + } + send.push_str("\n"); + match self.stream.write(send.as_bytes()) { + Ok(_result) => (), + Err(_error) => return false, } let mut string_mass = String::new(); @@ -105,9 +112,9 @@ impl Connection { } if string_mass.len() > 0 { let target = build_mass(&string_mass); - let t = masses.iter().position(|mass| - mass.name() == target.name()); - masses[self.index].downcast_mut::().unwrap().give_target(t); + let t = m.iter().position(|mass| + mass.name() == target.name()); + ship.give_target(t); } true } diff --git a/src/ship.rs b/src/ship.rs index b4b32e7..7eb3ba2 100644 --- a/src/ship.rs +++ b/src/ship.rs @@ -53,6 +53,10 @@ impl Ship { pub fn give_target(&mut self, target : Option) { self.target = target; } + + pub fn recv_target(&self) -> Option { + self.target + } } impl Mass for Ship { -- cgit v1.2.3