diff options
author | tom barrett <spalf0@gmail.com> | 2019-02-21 11:20:16 -0600 |
---|---|---|
committer | tom barrett <spalf0@gmail.com> | 2019-02-21 11:20:16 -0600 |
commit | ce9adc4cc1b4b30322dc30ee85c42ee1451ffbf9 (patch) | |
tree | e383b1aca16db9945cf6d224f6170f66b5d9dedb | |
parent | eb920a2c85e4ce4bbf755968a42218c0eb74987f (diff) |
redid engines, finished simple construction tests, setup recv using less parameters
-rw-r--r-- | src/bin/server.rs | 2 | ||||
-rw-r--r-- | src/mass.rs | 33 | ||||
-rw-r--r-- | src/modules/engines.rs | 70 | ||||
-rw-r--r-- | tests/tests.rs | 75 |
4 files changed, 86 insertions, 94 deletions
diff --git a/src/bin/server.rs b/src/bin/server.rs index 62da029..e8fa735 100644 --- a/src/bin/server.rs +++ b/src/bin/server.rs @@ -50,7 +50,7 @@ fn main() { connection.open = connection.stream.write(send.as_bytes()).is_ok(); let recv = connection.receive(); - ship.give_received_data(connection.module_type.clone(), recv, &masses); + ship.give_received_data(connection.module_type.clone(), recv); masses.insert(connection.name.clone(), ship); } diff --git a/src/mass.rs b/src/mass.rs index 7cc7d70..3d90573 100644 --- a/src/mass.rs +++ b/src/mass.rs @@ -186,6 +186,12 @@ impl Mass { masses.insert(target_name.to_string(), target); } + let target = match &navigation.target_name { + Some(target_name) => masses.get(target_name), + None => None, + }; + + engines.process(self.position.clone(), self.velocity.clone(), target); refinery.process(storage); navigation.process(self.position.clone(), masses); construction.process( @@ -194,7 +200,6 @@ impl Mass { masses, storage, ); - engines.process(self.velocity.clone()); self.effects.give_acceleration(engines.take_acceleration()); } @@ -236,12 +241,7 @@ impl Mass { } } - pub fn give_received_data( - &mut self, - module_type: ModuleType, - recv: String, - masses: &HashMap<String, Mass>, - ) { + pub fn give_received_data(&mut self, module_type: ModuleType, recv: String) { if let MassType::Ship { ref mut navigation, ref mut engines, @@ -252,18 +252,9 @@ impl Mass { .. } = self.mass_type { - let target = match &navigation.target_name { - Some(target_name) => masses.get(target_name), - None => None, - }; match module_type { ModuleType::Navigation => navigation.give_received_data(recv), - ModuleType::Engines => engines.give_received_data( - recv, - self.position.clone(), - self.velocity.clone(), - target, - ), + ModuleType::Engines => engines.give_received_data(recv, self.velocity.clone()), ModuleType::Mining => mining.give_received_data(recv), ModuleType::Construction => construction.give_received_data(recv), ModuleType::Refinery => refinery.give_received_data(recv), @@ -299,12 +290,8 @@ impl Mass { pub fn item_count(&self, item_type: ItemType) -> usize { match &self.mass_type { - MassType::Ship { - storage, .. - } => storage.item_count(item_type), - MassType::Astroid { - resources, .. - } => resources.item_count(item_type), + MassType::Ship { storage, .. } => storage.item_count(item_type), + MassType::Astroid { resources, .. } => resources.item_count(item_type), _ => 0, } } diff --git a/src/modules/engines.rs b/src/modules/engines.rs index 313346a..ebf4e8b 100644 --- a/src/modules/engines.rs +++ b/src/modules/engines.rs @@ -5,6 +5,7 @@ use crate::modules::navigation; #[derive(Serialize, Deserialize, Debug, Clone, Default)] pub struct Engines { + pub status: Status, acceleration: Vector, target_velocity: Option<Vector>, pub fuel: f64, @@ -13,28 +14,44 @@ pub struct Engines { impl Engines { pub fn new() -> Engines { Engines { + status: Status::None, acceleration: Vector::default(), target_velocity: None, fuel: constants::SHIP_ENGINES_FUEL_START, } } - pub fn process(&mut self, velocity: Vector) { - if let Some(target_velocity) = self.target_velocity.clone() { - self.acceleration += target_velocity - velocity; - if self.acceleration == Vector::default() { - self.target_velocity = None + pub fn process(&mut self, position: Vector, velocity: Vector, target: Option<&Mass>) { + if self.target_velocity.is_none() && self.status != Status::None { + if self.status == Status::Stopping { + self.target_velocity = Some(Vector::default()); + } + if let Some(target) = target { + match self.status { + Status::TowardsTarget => { + self.acceleration = (target.position.clone() - position).unitize() + * constants::SHIP_ENGINES_ACCELERATION; + self.status = Status::None; + } + Status::FollowingTarget => self.target_velocity = Some(target.velocity.clone()), + _ => (), + } + } else { + self.status = Status::None; } } + match self.target_velocity.clone() { + Some(target_velocity) => { + self.acceleration += target_velocity - velocity; + if self.acceleration == Vector::default() { + self.target_velocity = None; + } + } + None => (), + } } - pub fn give_received_data( - &mut self, - recv: String, - position: Vector, - velocity: Vector, - target: Option<&Mass>, - ) { + pub fn give_received_data(&mut self, recv: String, velocity: Vector) { let mut acceleration = Vector::default(); match recv.as_str() { "5" => acceleration.x += constants::SHIP_ENGINES_ACCELERATION, @@ -45,18 +62,9 @@ impl Engines { "6" => acceleration.z -= constants::SHIP_ENGINES_ACCELERATION, "+" => acceleration = velocity.unitize() * constants::SHIP_ENGINES_ACCELERATION, "-" => acceleration = velocity.unitize() * -1.0 * constants::SHIP_ENGINES_ACCELERATION, - "s" => self.target_velocity = Some(Vector::default()), - "c" => { - if let Some(target) = target { - self.target_velocity = Some(target.velocity.clone()); - } - } - "t" => { - if let Some(target) = target { - acceleration = (target.position.clone() - position).unitize() - * constants::SHIP_ENGINES_ACCELERATION; - } - } + "s" => self.status = Status::Stopping, + "c" => self.status = Status::FollowingTarget, + "t" => self.status = Status::TowardsTarget, _ => (), } self.acceleration = acceleration; @@ -91,3 +99,17 @@ pub struct ClientData { pub has_target: bool, pub fuel: f64, } + +#[derive(Serialize, Deserialize, Debug, Clone, PartialEq)] +pub enum Status { + None, + Stopping, + FollowingTarget, + TowardsTarget, +} + +impl Default for Status { + fn default() -> Self { + Status::None + } +} diff --git a/tests/tests.rs b/tests/tests.rs index 53c314a..141cc41 100644 --- a/tests/tests.rs +++ b/tests/tests.rs @@ -8,7 +8,7 @@ mod test { use space::constants; use space::item::{Item, ItemType}; - use space::mass::{Mass, MassType}; + use space::mass::Mass; use space::math::Vector; use space::modules::construction; use space::modules::mining; @@ -28,7 +28,7 @@ mod test { } fn setup_ship_target(ship: &mut Mass, masses: &mut HashMap<String, Mass>) { - ship.give_received_data(ModuleType::Navigation, String::from("astroid"), &masses); + ship.give_received_data(ModuleType::Navigation, String::from("astroid")); ship.process(masses); sleep(Duration::from_secs(constants::SHIP_NAVIGATION_TIME + 1)); ship.process(masses); @@ -38,7 +38,7 @@ mod test { fn test_navigation_range() { let (mut ship, mut masses) = setup(); - ship.give_received_data(ModuleType::Navigation, String::from("astroid"), &masses); + ship.give_received_data(ModuleType::Navigation, String::from("astroid")); ship.process(&mut masses); let data = ship.get_client_data(ModuleType::Navigation, &masses); let navigation_data: navigation::ClientData = serde_json::from_str(&data).unwrap(); @@ -74,7 +74,7 @@ mod test { let (mut ship, mut masses) = setup(); setup_ship_target(&mut ship, &mut masses); - ship.give_received_data(ModuleType::Mining, String::from("F"), &masses); + ship.give_received_data(ModuleType::Mining, String::from("F")); ship.process(&mut masses); let data = ship.get_client_data(ModuleType::Mining, &masses); let mining_data: mining::ClientData = serde_json::from_str(&data).unwrap(); @@ -93,7 +93,7 @@ mod test { let (mut ship, mut masses) = setup(); setup_ship_target(&mut ship, &mut masses); - ship.give_received_data(ModuleType::Mining, String::from("F"), &masses); + ship.give_received_data(ModuleType::Mining, String::from("F")); ship.process(&mut masses); sleep(Duration::from_secs(constants::SHIP_MINING_TIME + 1)); ship.process(&mut masses); @@ -113,7 +113,7 @@ mod test { ship.give_item(Item::new(ItemType::CrudeMinerals)); } - ship.give_received_data(ModuleType::Mining, String::from("F"), &masses); + ship.give_received_data(ModuleType::Mining, String::from("F")); ship.process(&mut masses); sleep(Duration::from_secs(constants::SHIP_MINING_TIME + 1)); assert!(masses.len() == 1); @@ -126,7 +126,7 @@ mod test { let (mut ship, mut masses) = setup(); setup_ship_target(&mut ship, &mut masses); - ship.give_received_data(ModuleType::Refinery, String::from("R"), &masses); + ship.give_received_data(ModuleType::Refinery, String::from("R")); ship.process(&mut masses); let data = ship.get_client_data(ModuleType::Refinery, &masses); let mining_data: refinery::ClientData = serde_json::from_str(&data).unwrap(); @@ -134,7 +134,7 @@ mod test { ship.give_item(Item::new(ItemType::CrudeMinerals)); - ship.give_received_data(ModuleType::Refinery, String::from("R"), &masses); + ship.give_received_data(ModuleType::Refinery, String::from("R")); ship.process(&mut masses); let data = ship.get_client_data(ModuleType::Refinery, &masses); let mining_data: refinery::ClientData = serde_json::from_str(&data).unwrap(); @@ -150,44 +150,27 @@ mod test { fn test_construction() { let (mut ship, mut masses) = setup(); - if let MassType::Ship { - ref mut storage, - ref mut construction, - .. - } = ship.mass_type - { - construction.give_received_data(String::from("c")); - construction.process( - ship.velocity.clone(), - ship.position.clone(), - &mut masses, - storage, - ); - assert!(construction.status == construction::Status::None); - - for _ in 0..5 { - storage.give_item(Item::new(ItemType::Iron)); - } - - construction.give_received_data(String::from("c")); - construction.process( - ship.velocity.clone(), - ship.position.clone(), - &mut masses, - storage, - ); - assert!(construction.status == construction::Status::Constructing); - assert!(masses.len() == 1); - - sleep(Duration::from_secs(constants::SHIP_CONSTRUCTION_TIME + 1)); - construction.process( - ship.velocity.clone(), - ship.position.clone(), - &mut masses, - storage, - ); - assert!(masses.len() == 2); - assert!(storage.items.len() == 0); + ship.give_received_data(ModuleType::Construction, String::from("c")); + ship.process(&mut masses); + let data = ship.get_client_data(ModuleType::Construction, &masses); + let construction_data: construction::ClientData = serde_json::from_str(&data).unwrap(); + assert!(construction_data.status == construction::Status::None); + + for _ in 0..5 { + ship.give_item(Item::new(ItemType::Iron)); } + + ship.give_received_data(ModuleType::Construction, String::from("c")); + ship.process(&mut masses); + + let data = ship.get_client_data(ModuleType::Construction, &masses); + let construction_data: construction::ClientData = serde_json::from_str(&data).unwrap(); + assert!(construction_data.status == construction::Status::Constructing); + assert!(masses.len() == 1); + + sleep(Duration::from_secs(constants::SHIP_CONSTRUCTION_TIME + 1)); + ship.process(&mut masses); + assert!(masses.len() == 2); + assert!(ship.item_count(ItemType::Iron) == 0); } } |