summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2018-04-13 03:39:24 -0500
committertom barrett <spalf0@gmail.com>2018-04-13 03:39:24 -0500
commit4d7a7e368a3f0a6b1261e1b9180a2647eb158047 (patch)
tree50ead62fdc5e22e839920dae44a02dc25b247678
parent74c7e70096ebcb0cab10f24ce1d348198d583161 (diff)
moved over to mass struct with enum for types
-rw-r--r--Cargo.lock23
-rw-r--r--Cargo.toml2
-rw-r--r--README.md1
-rw-r--r--src/astroid.rs75
-rw-r--r--src/bin/client.rs2
-rw-r--r--src/bin/server.rs7
-rw-r--r--src/client/dashboard.rs4
-rw-r--r--src/client/navigation.rs54
-rw-r--r--src/connection.rs20
-rw-r--r--src/lib.rs5
-rw-r--r--src/mass.rs104
-rw-r--r--src/mining.rs30
-rw-r--r--src/module.rs14
-rw-r--r--src/server/dashboard.rs4
-rw-r--r--src/server/engines.rs42
-rw-r--r--src/server/mining.rs40
-rw-r--r--src/server/navigation.rs72
-rw-r--r--src/ship.rs184
-rw-r--r--src/targeting.rs10
19 files changed, 237 insertions, 456 deletions
diff --git a/Cargo.lock b/Cargo.lock
index 3ff8677..b40bfd8 100644
--- a/Cargo.lock
+++ b/Cargo.lock
@@ -4,21 +4,11 @@ version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "downcast"
-version = "0.8.0"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-
-[[package]]
name = "dtoa"
version = "0.4.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "either"
-version = "1.5.0"
-source = "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"
@@ -33,14 +23,6 @@ version = "0.3.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
[[package]]
-name = "itertools"
-version = "0.7.8"
-source = "registry+https://github.com/rust-lang/crates.io-index"
-dependencies = [
- "either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)",
-]
-
-[[package]]
name = "itoa"
version = "0.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
@@ -130,8 +112,6 @@ dependencies = [
name = "space"
version = "0.1.0"
dependencies = [
- "downcast 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)",
- "itertools 0.7.8 (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)",
@@ -193,12 +173,9 @@ 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 either 1.5.0 (registry+https://github.com/rust-lang/crates.io-index)" = "3be565ca5c557d7f59e7cfcf1844f9e3033650c929c6566f511e8005f205c1d0"
"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 itertools 0.7.8 (registry+https://github.com/rust-lang/crates.io-index)" = "f58856976b776fedd95533137617a02fb25719f40e7d9b01c7043cd65474f450"
"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"
diff --git a/Cargo.toml b/Cargo.toml
index e51dc5e..b0b82c1 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -8,6 +8,4 @@ serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
termion = "1.5.1"
-downcast = "0.8"
rand = "0.4"
-itertools = "0.7.3"
diff --git a/README.md b/README.md
index a9ebc1e..e658653 100644
--- a/README.md
+++ b/README.md
@@ -2,4 +2,3 @@ space
ideas
- remove modules enum and just use the structs
-- figure out how to serialize between server client navigation better
diff --git a/src/astroid.rs b/src/astroid.rs
deleted file mode 100644
index f95e362..0000000
--- a/src/astroid.rs
+++ /dev/null
@@ -1,75 +0,0 @@
-extern crate rand;
-extern crate serde_json;
-
-use self::rand::distributions::Range;
-use self::rand::distributions::Sample;
-
-use item::Item;
-use storage::Storage;
-use mass::{Mass, MassType};
-
-#[derive(Serialize, Deserialize, Debug, Clone)]
-pub struct Astroid {
- mass_type : MassType,
- position : (f64, f64, f64),
- velocity : (f64, f64, f64),
- resouces : Storage,
-}
-
-impl Astroid {
- pub fn new() -> Astroid {
- let mut rng = rand::thread_rng();
-
- let mut pr = Range::new(-50.0, 50.0);
- let position = (pr.sample(&mut rng), pr.sample(&mut rng), pr.sample(&mut rng));
-
- let mut vr = Range::new(-0.5, 0.5);
- let velocity = (vr.sample(&mut rng), vr.sample(&mut rng), vr.sample(&mut rng));
-
- let mut rr = Range::new(0, 20);
- let mut resouces = Vec::new();
- for _ in 0..rr.sample(&mut rng) {
- resouces.push(Item::new("Iron", 1))
- }
- Astroid {
- mass_type : MassType::Astroid,
- position : position,
- velocity : velocity,
- resouces : Storage::new(resouces),
- }
- }
-}
-
-impl Mass for Astroid {
- fn recv_mass_type(&self) -> MassType {
- self.mass_type.clone()
- }
-
- fn position(&self) -> (f64, f64, f64) {
- self.position
- }
-
- fn serialize(&self) -> String {
- serde_json::to_string(self).unwrap()
- }
-
- fn box_clone(&self) -> Box<Mass> {
- Box::new((*self).clone())
- }
-
- fn process(&mut self) {
- self.position.0 += self.velocity.0;
- self.position.1 += self.velocity.1;
- self.position.2 += self.velocity.2;
- }
-
- fn recv_velocity(&self) -> (f64, f64, f64) {
- self.velocity
- }
-
- 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/client.rs b/src/bin/client.rs
index 5460652..b690094 100644
--- a/src/bin/client.rs
+++ b/src/bin/client.rs
@@ -46,9 +46,9 @@ fn main() {
stream.write(send.as_bytes()).unwrap();
match module {
+ ModuleType::Mining => client_mining(stream, buff_r),
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),
}
}
diff --git a/src/bin/server.rs b/src/bin/server.rs
index c5954b0..68e091e 100644
--- a/src/bin/server.rs
+++ b/src/bin/server.rs
@@ -7,14 +7,13 @@ use std::collections::HashMap;
use space::mass::Mass;
use space::math::rand_name;
-use space::astroid::Astroid;
use space::connection::Connection;
-fn populate() -> HashMap<String, Box<Mass>> {
- let mut masses : HashMap<String, Box<Mass>> = HashMap::new();
+fn populate() -> HashMap<String, Mass> {
+ let mut masses : HashMap<String, Mass> = HashMap::new();
for _ in 0..10 {
- masses.insert(rand_name(), Box::new(Astroid::new()));
+ masses.insert(rand_name(), Mass::new_astroid());
}
masses
diff --git a/src/client/dashboard.rs b/src/client/dashboard.rs
index 3776223..b9f0a08 100644
--- a/src/client/dashboard.rs
+++ b/src/client/dashboard.rs
@@ -4,13 +4,13 @@ use std::io::BufRead;
use std::io::BufReader;
use std::net::TcpStream;
-use ship::Ship;
+use mass::Mass;
pub fn client_dashboard(mut buff_r : BufReader<TcpStream>) {
loop {
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
- let ship : Ship = serde_json::from_str(&recv).unwrap();
+ let ship : Mass = serde_json::from_str(&recv).unwrap();
println!("{:?}", ship);
}
}
diff --git a/src/client/navigation.rs b/src/client/navigation.rs
index e5a5069..171d050 100644
--- a/src/client/navigation.rs
+++ b/src/client/navigation.rs
@@ -1,19 +1,16 @@
extern crate termion;
-extern crate itertools;
+//extern crate itertools;
extern crate serde_json;
use std::net::TcpStream;
use std::collections::BTreeMap;
use self::termion::async_stdin;
-use self::itertools::Itertools;
use std::io::{BufRead, BufReader};
use std::io::{stdout, Read, Write};
use self::termion::raw::IntoRawMode;
-use mass::Mass;
-use ship::Ship;
use math::distance;
-use astroid::Astroid;
+use mass::{Mass, MassType};
pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : BufReader<TcpStream>){
let stdout = stdout();
@@ -23,30 +20,24 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
loop {
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
-
- let string_hashmap = recv.split(";");
- let mut masses : BTreeMap<String, Box<Mass>> = BTreeMap::new();
- for string_element in string_hashmap {
- if string_element.len() <= 1 {
- break;
- }
- let (string_name, string_mass) = string_element.split("@").next_tuple().unwrap();
- masses.insert(string_name.to_string(), build_mass(string_mass));
- }
-
+ let mut within_range : BTreeMap<String, Mass> = serde_json::from_str(&recv).unwrap();
write!(stdout, "{}{}Targets:",
termion::clear::All,
termion::cursor::Goto(1,1)).unwrap();
- let ship = masses.remove(&name).unwrap().downcast::<Ship>().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 masses.iter().enumerate() {
+ for (i, (mass_name, mass)) in within_range.iter().enumerate() {
- let target_data = match ship.recv_target() {
+ let target_data = match targeting.target.clone() {
Some(target_name) => {
if &target_name == mass_name {
- serde_json::to_string(&ship.recv_targeting_status()).unwrap()
+ serde_json::to_string(&targeting.status).unwrap()
}
else {
String::new()
@@ -59,10 +50,10 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
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()),
+ mass.position.0,
+ mass.position.1,
+ mass.position.2,
+ distance(mass.position, ship.position),
target_data
).unwrap();
}
@@ -75,9 +66,9 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
}
else {
let i = c.to_digit(10).unwrap() as usize;
- if i < masses.len() {
+ if i < within_range.len() {
let mut send = String::new();
- send.push_str(masses.iter().nth(i).unwrap().0);
+ send.push_str(within_range.iter().nth(i).unwrap().0);
send.push_str("\n");
stream.write(send.as_bytes()).unwrap();
}
@@ -88,14 +79,3 @@ pub fn client_navigation(name : String, mut stream : TcpStream, mut buff_r : Buf
stdout.flush().unwrap();
}
}
-
-fn build_mass(string_mass : &str) -> Box<Mass> {
- if string_mass.contains("Ship") {
- let mass : Ship = serde_json::from_str(&string_mass).unwrap();
- return Box::new(mass)
- }
- else {
- let mass : Astroid = serde_json::from_str(&string_mass).unwrap();
- return Box::new(mass)
- }
-}
diff --git a/src/connection.rs b/src/connection.rs
index 91da04a..52d3352 100644
--- a/src/connection.rs
+++ b/src/connection.rs
@@ -5,9 +5,8 @@ use std::io::prelude::*;
use std::net::TcpStream;
use std::collections::HashMap;
-use ship::Ship;
-use mass::Mass;
use module::ModuleType;
+use mass::{Mass, MassType};
pub struct Connection {
pub name : String,
@@ -18,25 +17,26 @@ pub struct Connection {
}
impl Connection {
- pub fn new(mut stream : TcpStream, masses : &mut HashMap<String, Box<Mass>>) -> Connection {
+ pub fn new(mut stream : TcpStream, masses : &mut HashMap<String, Mass>) -> Connection {
let mut buff_r = BufReader::new(stream.try_clone().unwrap());
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
let name = &recv[..recv.find(":").unwrap()];
- let mass = masses.entry(name.to_string()).or_insert(Box::new(Ship::new((0.0, 0.0, 0.0))));
- let ship = mass.downcast_ref::<Ship>().unwrap();
+ let ship = masses.entry(name.to_string()).or_insert(Mass::new_ship());
- let modules = ship.recv_modules();
- stream.write(modules.as_bytes()).unwrap();
+ let send = match ship.mass_type {
+ MassType::Ship{ref modules, ..} => serde_json::to_string(modules).unwrap() + "\n",
+ _ => String::new(),
+ };
+ stream.write(send.as_bytes()).unwrap();
let mut recv = String::new();
buff_r.read_line(&mut recv).unwrap();
let module_type : ModuleType = serde_json::from_str(&recv.replace("\n","")).unwrap();
stream.set_nonblocking(true).unwrap();
-
Connection {
name : String::from(name),
module_type : module_type,
@@ -46,12 +46,12 @@ impl Connection {
}
}
- pub fn process(&mut self, mut masses : &mut HashMap<String, Box<Mass>>) {
+ 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),
ModuleType::Dashboard => self.server_dashboard(&mut masses),
ModuleType::Navigation => self.server_navigation(&mut masses),
- ModuleType::Mining => self.server_mining(&mut masses),
};
}
}
diff --git a/src/lib.rs b/src/lib.rs
index d666cb8..97d506d 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,16 +1,13 @@
#[macro_use]
extern crate serde_derive;
-#[macro_use]
-extern crate downcast;
pub mod mass;
pub mod item;
-pub mod ship;
pub mod math;
pub mod module;
+pub mod mining;
pub mod client;
pub mod server;
pub mod storage;
-pub mod astroid;
pub mod targeting;
pub mod connection;
diff --git a/src/mass.rs b/src/mass.rs
index 5742f83..f13e8ce 100644
--- a/src/mass.rs
+++ b/src/mass.rs
@@ -1,27 +1,91 @@
-extern crate serde;
-
-use downcast::Any;
-
-pub trait Mass : Any {
- fn recv_mass_type(&self) -> MassType;
- fn position(&self) -> (f64, f64, f64);
- fn serialize(&self) -> String;
- fn process(&mut self);
- fn give_acceleration(&mut self, acceleration : (f64, f64, f64));
- fn recv_velocity(&self) -> (f64, f64, f64);
- fn box_clone(&self) -> Box<Mass>;
-}
+extern crate rand;
-impl Clone for Box<Mass> {
- fn clone(&self) -> Box<Mass> {
- self.box_clone()
- }
+use self::rand::distributions::Range;
+use self::rand::distributions::Sample;
+
+use storage::Storage;
+use module::ModuleType;
+use targeting::Targeting;
+use mining::Mining;
+
+#[derive(Serialize, Deserialize, Debug, Clone)]
+pub struct Mass {
+ pub mass_type : MassType,
+ pub position : (f64, f64, f64),
+ pub velocity : (f64, f64, f64),
}
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum MassType {
- Ship,
- Astroid,
+ Ship {
+ modules : Vec<ModuleType>,
+ mining : Mining,
+ targeting : Targeting,
+ storage : Storage,
+ },
+ Astroid{
+ resources : Storage,
+ },
}
-downcast!(Mass);
+impl Mass {
+ pub fn new_astroid() -> Mass {
+ let mut rng = rand::thread_rng();
+
+ let mut pr = Range::new(-50.0, 50.0);
+ let position = (pr.sample(&mut rng), pr.sample(&mut rng), pr.sample(&mut rng));
+
+ let mut vr = Range::new(-0.5, 0.5);
+ let velocity = (vr.sample(&mut rng), vr.sample(&mut rng), vr.sample(&mut rng));
+
+ let astroid = MassType::Astroid {
+ resources : Storage::new(Vec::new()),
+ };
+
+ Mass {
+ mass_type : astroid,
+ position : position,
+ velocity : velocity,
+ }
+ }
+
+ pub fn new_ship() -> Mass {
+ let mut modules = Vec::new();
+
+ modules.push(ModuleType::Navigation);
+ modules.push(ModuleType::Engines);
+ modules.push(ModuleType::Dashboard);
+ modules.push(ModuleType::Mining);
+
+ let ship = MassType::Ship {
+ modules : modules,
+ mining : Mining::new(),
+ targeting : Targeting::new(),
+ storage : Storage::new(Vec::new()),
+ };
+
+ Mass {
+ mass_type : ship,
+ position : (0.0,0.0,0.0),
+ velocity : (0.0,0.0,0.0),
+ }
+ }
+
+ pub fn process(&mut self) {
+ self.position.0 += self.velocity.0;
+ self.position.1 += self.velocity.1;
+ self.position.2 += self.velocity.2;
+ match self.mass_type {
+ MassType::Ship{ref mut targeting, ..} => {
+ targeting.process();
+ },
+ _ => (),
+ }
+ }
+
+ pub fn accelerate(&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/mining.rs b/src/mining.rs
new file mode 100644
index 0000000..ae4af49
--- /dev/null
+++ b/src/mining.rs
@@ -0,0 +1,30 @@
+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 800f4d7..937c63c 100644
--- a/src/module.rs
+++ b/src/module.rs
@@ -1,15 +1,3 @@
-use downcast::Any;
-
-pub trait Module : Any {
- fn box_clone(&self) -> Box<Module>;
-}
-
-impl Clone for Box<Module> {
- fn clone(&self) -> Box<Module> {
- self.box_clone()
- }
-}
-
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)]
pub enum ModuleType {
Mining,
@@ -17,5 +5,3 @@ pub enum ModuleType {
Dashboard,
Navigation,
}
-
-downcast!(Module);
diff --git a/src/server/dashboard.rs b/src/server/dashboard.rs
index d12ea35..82f73f9 100644
--- a/src/server/dashboard.rs
+++ b/src/server/dashboard.rs
@@ -7,9 +7,9 @@ use mass::Mass;
use connection::Connection;
impl Connection {
- pub fn server_dashboard(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool {
+ pub fn server_dashboard(&mut self, masses : &mut HashMap<String, Mass>) -> bool {
let ship = masses.get(&self.name).unwrap();
- let send = ship.serialize() + "\n";
+ let send = serde_json::to_string(&ship).unwrap();
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 ec55ee0..aa0e703 100644
--- a/src/server/engines.rs
+++ b/src/server/engines.rs
@@ -4,19 +4,21 @@ use std::io::Write;
use std::io::BufRead;
use std::collections::HashMap;
-use ship::Ship;
-use mass::Mass;
+use mass::{Mass, MassType};
use connection::Connection;
use targeting::TargetingStatus;
impl Connection {
- pub fn server_engines(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool {
+ pub fn server_engines(&mut self, masses : &mut HashMap<String, Mass>) -> bool {
let masses_clone = masses.clone();
- let mass = masses.get_mut(&self.name).unwrap();
- let ship = mass.downcast_mut::<Ship>().unwrap();
+ let ship = masses.get_mut(&self.name).unwrap();
+ let targeting = match ship.mass_type {
+ MassType::Ship{ref targeting, ..} => Some(targeting.clone()),
+ _ => None,
+ }.unwrap();
- let targeted = ship.recv_targeting_status() == TargetingStatus::Targeted;
+ let targeted = targeting.status == TargetingStatus::Targeted;
let send = serde_json::to_string(&targeted).unwrap() + "\n";
match self.stream.write(send.as_bytes()) {
@@ -34,14 +36,24 @@ impl Connection {
b"2\n" => acceleration.1 -= 0.1,
b"4\n" => acceleration.2 += 0.1,
b"6\n" => acceleration.2 -= 0.1,
- b"+\n" => ship.speedup(),
- b"-\n" => ship.slow(),
+ 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 ship.recv_target() {
+ match targeting.target {
Some(name) => {
let target = masses_clone.get(&name).unwrap();
- let d_v = target.recv_velocity();
- let m_v = ship.recv_velocity();
+ 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);
@@ -50,11 +62,11 @@ impl Connection {
}
},
b"t\n" => {
- match ship.recv_target() {
+ match targeting.target {
Some(name) => {
let target = masses_clone.get(&name).unwrap();
- let d_p = target.position();
- let m_p = ship.position();
+ 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);
@@ -71,7 +83,7 @@ impl Connection {
Err(_error) => (),
}
- ship.give_acceleration(acceleration);
+ ship.accelerate(acceleration);
true
}
diff --git a/src/server/mining.rs b/src/server/mining.rs
index 9aa054f..ff26840 100644
--- a/src/server/mining.rs
+++ b/src/server/mining.rs
@@ -4,7 +4,6 @@ use std::io::BufRead;
use std::io::Write;
use std::collections::HashMap;
-use ship::Ship;
use math::distance;
use mass::{Mass, MassType};
use connection::Connection;
@@ -18,40 +17,38 @@ struct ServerData {
}
impl Connection {
- pub fn server_mining(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool {
+ pub fn server_mining(&mut self, masses : &mut HashMap<String, Mass>) -> bool {
let masses_clone = masses.clone();
- let mass = masses.get_mut(&self.name).unwrap();
- let ship = mass.downcast_mut::<Ship>().unwrap();
+ let ship = masses.get_mut(&self.name).unwrap();
- let target = match ship.recv_target() {
- Some(name) => masses_clone.get(&name),
- None => None,
+ 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.recv_mass_type() {
- MassType::Ship => false,
- MassType::Astroid => true,
+ Some(target) => match target.mass_type {
+ MassType::Astroid{..} => true,
+ _ => false,
},
None => false,
};
let is_within_range = match has_astroid_target {
true => match target {
- Some(target) => match ship.recv_mining_range() > distance(ship.position(), target.position()) {
- true => true,
- false => false,
- },
- None => false,
+ Some(target) => mining.range > distance(ship.position, target.position),
+ _ => false,
}
- false => false,
+ _ => false,
};
let send = serde_json::to_string(&ServerData {
has_astroid_target : has_astroid_target,
is_within_range : is_within_range,
- mining_range : ship.recv_mining_range(),
- mining_status : ship.recv_mining_status(),
+ mining_range : mining.range,
+ mining_status : mining.status,
}).unwrap() + "\n";
match self.stream.write(send.as_bytes()) {
@@ -64,9 +61,9 @@ impl Connection {
Ok(result) => match recv.as_bytes() {
b"F\n" => {
if is_within_range {
- match ship.recv_mining_status() {
- true => ship.stop_mining(),
- false => ship.start_mining(),
+ match mining.status {
+ true => mining.stop(),
+ false => mining.start(),
}
}
},
@@ -78,7 +75,6 @@ impl Connection {
}
Err(_error) => (),
}
-
true
}
}
diff --git a/src/server/navigation.rs b/src/server/navigation.rs
index f75c2b7..3813732 100644
--- a/src/server/navigation.rs
+++ b/src/server/navigation.rs
@@ -4,51 +4,51 @@ use std::io::Write;
use std::io::BufRead;
use std::collections::HashMap;
-use mass::Mass;
-use ship::Ship;
+use mass::{Mass, MassType};
use math::distance;
use connection::Connection;
impl Connection {
- pub fn server_navigation(&mut self, masses : &mut HashMap<String, Box<Mass>>) -> bool {
+ pub fn server_navigation(&mut self, masses : &mut HashMap<String, Mass>) -> bool {
let masses_clone = masses.clone();
- let mass = masses.get_mut(&self.name).unwrap();
- let ship = mass.downcast_mut::<Ship>().unwrap();
-
- match ship.recv_target() {
- Some(name) => {
- let target = masses_clone.get(&name).unwrap();
- if distance(target.position(), ship.position()) > ship.recv_range() {
- ship.give_target(None);
+ let ship = masses.get_mut(&self.name).unwrap();
+ 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;
+ }
+ },
+ _ => (),
}
- }
- None => (),
- }
- let within_range : HashMap<&String, &Box<Mass>> = masses_clone.iter().filter(|&(_, mass)|
- distance(ship.position(), mass.position()) < ship.recv_range())
- .collect();
- let mut send = String::new();
- for (name, mass) in within_range {
- send.push_str(name);
- send.push_str("@");
- 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 within_range : HashMap<&String, &Mass> = masses_clone.iter().filter(|&(_, mass)|
+ distance(ship_position, mass.position) < targeting.range)
+ .collect();
- let mut recv = String::new();
- match self.buff_r.read_line(&mut recv) {
- Ok(_result) => (),
- Err(_error) => (),
- }
- if recv.len() > 0 {
- ship.give_target(Some(recv.replace("\n", "")));
+ 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", "")));
+ }
+ },
+ _ => (),
}
+
true
}
}
diff --git a/src/ship.rs b/src/ship.rs
deleted file mode 100644
index 403b4ed..0000000
--- a/src/ship.rs
+++ /dev/null
@@ -1,184 +0,0 @@
-extern crate serde_json;
-
-use std::time::SystemTime;
-//use std::collections::HashMap;
-
-use storage::Storage;
-use mass::{Mass, MassType};
-//use module::{Module, ModuleType};
-use module::ModuleType;
-use targeting::{Targeting, TargetingStatus};
-
-#[derive(Serialize, Deserialize, Debug, Clone)]
-pub struct Ship {
- mass_type : MassType,
- position : (f64, f64, f64),
- velocity : (f64, f64, f64),
- range : f64,
- modules : Vec<ModuleType>,
- mining : Mining,
- targeting : Targeting,
-// modules : HashMap<ModuleType, Box<Module>>,
- storage : Storage,
-}
-
-impl Ship {
- pub fn new(position : (f64, f64, f64)) -> Ship {
- let mut modules = Vec::new();
-
- modules.push(ModuleType::Navigation);
- modules.push(ModuleType::Engines);
- modules.push(ModuleType::Dashboard);
- modules.push(ModuleType::Mining);
-
-// let mut modules : HashMap<ModuleType, Box<Module>> = HashMap::new();
-// modules.insert(ModuleType::Navigation, Targeting::new());
-// modules.insert(ModuleType::Mining, Mining::new());
-
- Ship {
- mass_type : MassType::Ship,
- position : position,
- velocity : (0.0, 0.0, 0.0),
- range : 100.0,
- modules : modules,
- mining : Mining::new(),
- targeting : Targeting::new(),
- storage : Storage::new(Vec::new()),
- }
- }
-
- pub fn slow(&mut self) {
- if self.velocity.0 > 0.01 {
- self.velocity.0 += -1.0 * self.velocity.0 * 0.1;
- }
- else {
- self.velocity.0 = 0.0;
- }
-
- if self.velocity.1 > 0.01 {
- self.velocity.1 += -1.0 * self.velocity.1 * 0.1;
- }
- else {
- self.velocity.1 = 0.0;
- }
-
- if self.velocity.2 > 0.01 {
- self.velocity.2 += -1.0 * self.velocity.2 * 0.1;
- }
- else {
- self.velocity.2 = 0.0;
- }
- }
-
- pub fn speedup(&mut self) {
- self.velocity.0 *= 1.05;
- self.velocity.1 *= 1.05;
- self.velocity.2 *= 1.05;
- }
- pub fn start_mining(&mut self) {
- self.mining.start()
- }
-
- pub fn stop_mining(&mut self) {
- self.mining.stop()
- }
-
- pub fn recv_range(&self) -> f64 {
- self.range
- }
-
- pub fn recv_mining_range(&self) -> f64 {
- self.mining.recv_range()
- }
-
- pub fn recv_mining_status(&self) -> bool {
- self.mining.recv_status()
- }
-
- pub fn give_target(&mut self, target : Option<String>) {
- self.targeting.give_target(target);
- }
-
- pub fn recv_target(&self) -> Option<String> {
- self.targeting.recv_target()
- }
-
- pub fn recv_targeting_status(&self) -> TargetingStatus {
- self.targeting.recv_status()
- }
-
- pub fn recv_modules(&self) -> String {
- serde_json::to_string(&self.modules).unwrap() + "\n"
- }
-}
-
-impl Mass for Ship {
- fn recv_mass_type(&self) -> MassType {
- self.mass_type.clone()
- }
-
- fn process(&mut self) {
- self.position.0 += self.velocity.0;
- self.position.1 += self.velocity.1;
- self.position.2 += self.velocity.2;
- self.targeting.process()
- }
-
- fn position(&self) -> (f64, f64, f64) {
- self.position
- }
-
- fn serialize(&self) -> String {
- serde_json::to_string(self).unwrap()
- }
-
- fn box_clone(&self) -> Box<Mass> {
- Box::new((*self).clone())
- }
-
- fn recv_velocity(&self) -> (f64, f64, f64) {
- self.velocity
- }
-
- fn give_acceleration(&mut self, acceleration : (f64, f64, f64)) {
- self.velocity.0 += acceleration.0;
- self.velocity.1 += acceleration.1;
- self.velocity.2 += acceleration.2;
- }
-}
-
-
-#[derive(Serialize, Deserialize, Debug, Clone)]
-struct Mining {
- range : f64,
- 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;
- }
-
- pub fn recv_range(&self) -> f64 {
- self.range
- }
-
- pub fn recv_status(&self) -> bool {
- self.status
- }
-}
diff --git a/src/targeting.rs b/src/targeting.rs
index 49fdc54..3e566f4 100644
--- a/src/targeting.rs
+++ b/src/targeting.rs
@@ -9,10 +9,11 @@ pub enum TargetingStatus {
#[derive(Serialize, Deserialize, Debug, Clone)]
pub struct Targeting {
- target : Option<String>,
- status : TargetingStatus,
- time : u64,
- start : Option<SystemTime>,
+ pub target : Option<String>,
+ pub range : f64,
+ pub status : TargetingStatus,
+ time : u64,
+ start : Option<SystemTime>,
}
impl Targeting {
@@ -20,6 +21,7 @@ impl Targeting {
Targeting {
target : None,
status : TargetingStatus::None,
+ range : 100.0,
time : 3,
start : None,
}