From b5896e2d7597e42818a47710da22098d178bf8f6 Mon Sep 17 00:00:00 2001 From: tom barrett Date: Tue, 27 Feb 2018 06:59:58 -0600 Subject: -now use velocity and position vector, astroids are now randomly populated --- Cargo.lock | 56 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/astroid.rs | 47 ++++++++++++++++++++++++++++++++++------------ src/bin/server.rs | 8 +++++++- src/connection.rs | 18 +++++++++--------- src/mass.rs | 6 +++--- src/navigation.rs | 13 ++++++++----- src/ship.rs | 40 ++++++++++++++++++++++++--------------- 8 files changed, 144 insertions(+), 45 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 099afcc..ab9d1b9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,3 +1,8 @@ +[[package]] +name = "bitflags" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "downcast" version = "0.8.0" @@ -16,6 +21,20 @@ dependencies = [ "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "fuchsia-zircon" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)", + "fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "fuchsia-zircon-sys" +version = "0.3.3" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "itoa" version = "0.3.4" @@ -44,6 +63,16 @@ name = "quote" version = "0.3.15" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "rand" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)", +] + [[package]] name = "redox_syscall" version = "0.1.37" @@ -98,6 +127,7 @@ version = "0.1.0" dependencies = [ "downcast 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "erased-serde 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)", + "rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_derive 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.9 (registry+https://github.com/rust-lang/crates.io-index)", @@ -137,15 +167,38 @@ name = "unicode-xid" version = "0.0.4" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "winapi" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", + "winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "winapi-i686-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [metadata] +"checksum bitflags 1.0.1 (registry+https://github.com/rust-lang/crates.io-index)" = "b3c30d3802dfb7281680d6285f2ccdaa8c2d8fee41f93805dba5c4cf50dc23cf" "checksum downcast 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)" = "242756f88358d71447a37780b71b1cb180a276bd214765ee2d81dfe7c90e59bb" "checksum dtoa 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "09c3753c3db574d215cba4ea76018483895d7bff25a31b49ba45db21c48e50ab" "checksum erased-serde 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "c564e32677839f1c551664c478e079c9b128a1a2d223180bffb2ddfabeded0be" +"checksum fuchsia-zircon 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "2e9763c69ebaae630ba35f74888db465e49e259ba1bc0eda7d06f4a067615d82" +"checksum fuchsia-zircon-sys 0.3.3 (registry+https://github.com/rust-lang/crates.io-index)" = "3dcaa9ae7725d12cdb85b3ad99a434db70b468c09ded17e012d86b5c1010f7a7" "checksum itoa 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8324a32baf01e2ae060e9de58ed0bc2320c9a2833491ee36cd3b4c414de4db8c" "checksum libc 0.2.36 (registry+https://github.com/rust-lang/crates.io-index)" = "1e5d97d6708edaa407429faa671b942dc0f2727222fb6b6539bf1db936e4b121" "checksum num-traits 0.1.43 (registry+https://github.com/rust-lang/crates.io-index)" = "92e5113e9fd4cc14ded8e499429f396a20f98c772a47cc8622a736e1ec843c31" "checksum num-traits 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "e7de20f146db9d920c45ee8ed8f71681fd9ade71909b48c3acbd766aa504cf10" "checksum quote 0.3.15 (registry+https://github.com/rust-lang/crates.io-index)" = "7a6e920b65c65f10b2ae65c831a81a073a89edd28c7cce89475bff467ab4167a" +"checksum rand 0.4.2 (registry+https://github.com/rust-lang/crates.io-index)" = "eba5f8cb59cc50ed56be8880a5c7b496bfd9bd26394e176bc67884094145c2c5" "checksum redox_syscall 0.1.37 (registry+https://github.com/rust-lang/crates.io-index)" = "0d92eecebad22b767915e4d529f89f28ee96dbbf5a4810d2b844373f136417fd" "checksum redox_termios 0.1.1 (registry+https://github.com/rust-lang/crates.io-index)" = "7e891cfe48e9100a70a3b6eb652fef28920c117d366339687bd5576160db0f76" "checksum serde 1.0.27 (registry+https://github.com/rust-lang/crates.io-index)" = "db99f3919e20faa51bb2996057f5031d8685019b5a06139b1ce761da671b8526" @@ -156,3 +209,6 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum synom 0.11.3 (registry+https://github.com/rust-lang/crates.io-index)" = "a393066ed9010ebaed60b9eafa373d4b1baac186dd7e008555b0f702b51945b6" "checksum termion 1.5.1 (registry+https://github.com/rust-lang/crates.io-index)" = "689a3bdfaab439fd92bc87df5c4c78417d3cbe537487274e9b0b2dce76e92096" "checksum unicode-xid 0.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8c1f860d7d29cf02cb2f3f359fd35991af3d30bac52c57d265a3c461074cb4dc" +"checksum winapi 0.3.4 (registry+https://github.com/rust-lang/crates.io-index)" = "04e3bd221fcbe8a271359c04f21a76db7d0c6028862d1bb5512d85e1e2eb5bb3" +"checksum winapi-i686-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" +"checksum winapi-x86_64-pc-windows-gnu 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" diff --git a/Cargo.toml b/Cargo.toml index 9eb8bce..69c08e8 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,3 +10,4 @@ erased-serde = "0.3" serde_json = "1.0" termion = "1.5.1" downcast = "0.8" +rand = "0.4" diff --git a/src/astroid.rs b/src/astroid.rs index 6aa1807..fb14751 100644 --- a/src/astroid.rs +++ b/src/astroid.rs @@ -1,35 +1,58 @@ use mass::{Mass, Type}; +extern crate rand; +use self::rand::distributions::Range; +use astroid::rand::distributions::Sample; extern crate serde_json; +use astroid::rand::Rng; #[derive(Serialize, Deserialize, Debug)] pub struct Astroid { name : String, t : Type, - location : (f64, f64, f64), + position : (f64, f64, f64), + velocity : (f64, f64, f64), } -impl Mass for Astroid { - fn new(name : &str, location : (f64, f64, f64)) -> Astroid { +impl Astroid { + pub fn new() -> Astroid { + let name : String = rand::thread_rng() + .gen_ascii_chars() + .take(8) + .collect(); + let mut r = Range::new(-50.0, 50.0); + let mut rng = rand::thread_rng(); + let position = (r.sample(&mut rng), r.sample(&mut rng), r.sample(&mut rng)); Astroid { - name : String::from(name), - t : Type::Astroid, - location : location, + name : name, + t : Type::Astroid, + position : position, + velocity : (0.0, 0.0, 0.0), } } +} +impl Mass for Astroid { fn name(&self) -> &String { &self.name } - fn location(&self) -> (f64, f64, f64) { - self.location - } - - fn set_location(&mut self, location : (f64, f64, f64)) { - self.location = location; + fn position(&self) -> (f64, f64, f64) { + self.position } fn serialize(&self) ->String { serde_json::to_string(self).unwrap() } + + fn process(&mut self) { + self.position.0 += self.velocity.0; + self.position.1 += self.velocity.1; + self.position.2 += self.velocity.2; + } + + fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) { + self.velocity.0 += acceleration.0; + self.velocity.1 += acceleration.1; + self.velocity.2 += acceleration.2; + } } diff --git a/src/bin/server.rs b/src/bin/server.rs index c327257..36840c1 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -11,7 +11,9 @@ use space::connection::Connection; fn populate() -> Vec> { let mut masses : Vec> = Vec::new(); - masses.push(Box::new(Astroid::new("cZfAJ", (10.0, -5.0, 4.0)))); + masses.push(Box::new(Astroid::new())); + masses.push(Box::new(Astroid::new())); + masses.push(Box::new(Astroid::new())); masses } @@ -32,6 +34,10 @@ fn main() { } connections.retain(|connection| connection.open); + for mass in masses.iter_mut() { + mass.process(); + } + sleep(Duration::from_millis(100)); } } diff --git a/src/connection.rs b/src/connection.rs index db258e4..bcd4a30 100644 --- a/src/connection.rs +++ b/src/connection.rs @@ -64,16 +64,16 @@ impl Connection { } } Module::Engines => { - let mut location = masses[self.index].location(); + let mut acceleration = (0.0, 0.0, 0.0); let mut data = String::new(); match self.buff_r.read_line(&mut data) { Ok(result) => match data.as_bytes() { - b"5\n" => location.0 += 1.0, - b"0\n" => location.0 -= 1.0, - b"8\n" => location.1 += 1.0, - b"2\n" => location.1 -= 1.0, - b"4\n" => location.2 += 1.0, - b"6\n" => location.2 -= 1.0, + b"5\n" => acceleration.0 += 1.0, + b"0\n" => acceleration.0 -= 1.0, + b"8\n" => acceleration.1 += 1.0, + b"2\n" => acceleration.1 -= 1.0, + b"4\n" => acceleration.2 += 1.0, + b"6\n" => acceleration.2 -= 1.0, _ => { if result == 0 { self.open = false; @@ -82,13 +82,13 @@ impl Connection { }, Err(_error) => (), } - masses[self.index].set_location(location); + masses[self.index].give_acceleration(acceleration); } Module::Navigation => { let ship = &masses[self.index].downcast_ref::().unwrap(); let within_range : Vec<&Box> = masses.iter().filter(|mass| - distance(ship.location(), mass.location()) < ship.range()).collect(); + distance(ship.position(), mass.position()) < ship.range()).collect(); let mut send = String::new(); for mass in within_range { diff --git a/src/mass.rs b/src/mass.rs index 71723cd..d91b292 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -1,11 +1,11 @@ use downcast::Any; pub trait Mass : Any { - fn new(name : &str, location : (f64, f64, f64)) -> Self where Self: Sized; fn name(&self) -> &String; - fn location(&self) -> (f64, f64, f64); - fn set_location(&mut self, location : (f64, f64, f64)); + fn position(&self) -> (f64, f64, f64); fn serialize(&self) -> String; + fn process(&mut self); + fn give_acceleration(&mut self, acceleration : (f64, f64, f64)); } #[derive(Serialize, Deserialize, Debug)] diff --git a/src/navigation.rs b/src/navigation.rs index cc2221a..4683c0c 100644 --- a/src/navigation.rs +++ b/src/navigation.rs @@ -16,7 +16,7 @@ use ship::Ship; use math::distance; use astroid::Astroid; -pub fn Navigation(name : String, mut stream :TcpStream, mut buff_r : BufReader){ +pub fn Navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader){ let stdout = stdout(); let mut stdout = stdout.lock().into_raw_mode().unwrap(); let mut stdin = async_stdin().bytes(); @@ -55,14 +55,16 @@ pub fn Navigation(name : String, mut stream :TcpStream, mut buff_r : BufReader f64 { - self.r - } -} - -impl Mass for Ship { - fn new(name : &str, location : (f64, f64, f64)) -> Ship { + pub fn new(name : &str, position : (f64, f64, f64)) -> Ship { Ship { name : String::from(name), - location : location, + position : position, + velocity : (0.0, 0.0, 0.0), t : Type::Ship, r : 100.0, } } - fn name(&self) -> &String { - &self.name + pub fn range(&self) -> f64 { + self.r } +} - fn location(&self) -> (f64, f64, f64) { - self.location +impl Mass for Ship { + fn name(&self) -> &String { + &self.name } - fn set_location(&mut self, location : (f64, f64, f64)) { - self.location = location; + fn position(&self) -> (f64, f64, f64) { + self.position } fn serialize(&self) -> String { serde_json::to_string(self).unwrap() } + + fn process(&mut self) { + self.position.0 += self.velocity.0; + self.position.1 += self.velocity.1; + self.position.2 += self.velocity.2; + } + + fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) { + self.velocity.0 += acceleration.0; + self.velocity.1 += acceleration.1; + self.velocity.2 += acceleration.2; + } } -- cgit v1.2.3