summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authortom barrett <spalf0@gmail.com>2019-02-14 13:27:28 -0600
committertom barrett <spalf0@gmail.com>2019-02-14 13:27:28 -0600
commitca26d538085704d3278063715a14308eeed70127 (patch)
treed3b1cf3d43d7466bafa20b03e5e7ac54750fca3b
parent35b43264c09405c987b48de78b6ca19f29dc7849 (diff)
simplified mining logic
-rw-r--r--src/modules/construction.rs2
-rw-r--r--src/modules/mining.rs2
-rw-r--r--src/server/construction.rs6
-rw-r--r--src/server/mining.rs89
4 files changed, 37 insertions, 62 deletions
diff --git a/src/modules/construction.rs b/src/modules/construction.rs
index 6c27db5..0f7f8f2 100644
--- a/src/modules/construction.rs
+++ b/src/modules/construction.rs
@@ -69,7 +69,7 @@ impl Construction {
self.status = ConstructionStatus::None;
}
- pub fn taken(&mut self) {
+ pub fn constructed(&mut self) {
self.off()
}
}
diff --git a/src/modules/mining.rs b/src/modules/mining.rs
index 120ba8c..a6cea92 100644
--- a/src/modules/mining.rs
+++ b/src/modules/mining.rs
@@ -68,7 +68,7 @@ impl Mining {
self.status = MiningStatus::None;
}
- pub fn taken(&mut self) {
+ pub fn mined(&mut self) {
self.status = MiningStatus::Mining;
}
}
diff --git a/src/server/construction.rs b/src/server/construction.rs
index 50a193a..0e24952 100644
--- a/src/server/construction.rs
+++ b/src/server/construction.rs
@@ -37,7 +37,9 @@ impl ServerConnection {
construction.give_recv(recv, &construction_data);
if construction_data.status == ConstructionStatus::Constructed {
- storage.take_items(ItemType::Iron, constants::SHIP_CONSTRUCTION_IRON_COST);
+ storage
+ .take_items(ItemType::Iron, constants::SHIP_CONSTRUCTION_IRON_COST)
+ .unwrap();
masses.insert(
"Station".to_string(),
Mass::new_station(
@@ -46,7 +48,7 @@ impl ServerConnection {
ship.velocity.clone(),
),
);
- construction.taken();
+ construction.constructed();
}
}
diff --git a/src/server/mining.rs b/src/server/mining.rs
index f5d8dd8..0bf6956 100644
--- a/src/server/mining.rs
+++ b/src/server/mining.rs
@@ -7,7 +7,6 @@ use crate::item::ItemType;
use crate::mass::{Mass, MassType};
use crate::math::Vector;
use crate::modules::mining::{Mining, MiningStatus};
-use crate::modules::navigation::Navigation;
use crate::server::connection::ServerConnection;
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -30,17 +29,18 @@ impl ServerConnection {
..
} = ship.mass_type
{
- let mining_data = get_mining_data(ship.position.clone(), mining, navigation, masses);
+ if let Some(target_name) = navigation.target_name.clone() {
+ let mut target = masses.remove(&target_name).unwrap();
- let send = serde_json::to_string(&mining_data).unwrap() + "\n";
- self.open = self.stream.write(send.as_bytes()).is_ok();
+ let mining_data = get_mining_data(ship.position.clone(), mining, target.clone());
- let recv = self.receive();
- mining.give_recv(recv, mining_data);
+ let send = serde_json::to_string(&mining_data).unwrap() + "\n";
+ self.open = self.stream.write(send.as_bytes()).is_ok();
- if mining.status == MiningStatus::Mined {
- if let Some(name) = navigation.target_name.clone() {
- let target = masses.get_mut(&name).unwrap();
+ let recv = self.receive();
+ mining.give_recv(recv, mining_data);
+
+ if mining.status == MiningStatus::Mined {
if let MassType::Astroid {
ref mut resources, ..
} = target.mass_type
@@ -59,8 +59,9 @@ impl ServerConnection {
None => mining.off(),
}
}
+ mining.mined();
}
- mining.taken();
+ masses.insert(target_name, target);
}
}
@@ -68,54 +69,26 @@ impl ServerConnection {
}
}
-fn get_mining_data(
- position: Vector,
- mining: &Mining,
- navigation: &Navigation,
- masses: &mut HashMap<String, Mass>,
-) -> MiningData {
- match navigation.target_name.clone() {
- Some(name) => {
- let target = masses.get(&name);
-
- let mut astroid_has_minerals = false;
- let has_astroid_target = match target {
- Some(target) => match target.mass_type {
- MassType::Astroid { ref resources, .. } => {
- astroid_has_minerals = resources
- .items
- .iter()
- .any(|item| item.itemtype == ItemType::CrudeMinerals);
- true
- }
- _ => false,
- },
- None => false,
- };
-
- let is_within_range = if has_astroid_target {
- match target {
- Some(target) => mining.range > position.distance_from(target.position.clone()),
- _ => false,
- }
- } else {
- false
- };
-
- MiningData {
- has_astroid_target,
- astroid_has_minerals,
- is_within_range,
- range: mining.range,
- status: mining.status.clone(),
- }
+fn get_mining_data(position: Vector, mining: &Mining, target: Mass) -> MiningData {
+ let mut astroid_has_minerals = false;
+ let mut is_within_range = false;
+ let has_astroid_target = match target.mass_type {
+ MassType::Astroid { ref resources, .. } => {
+ astroid_has_minerals = resources
+ .items
+ .iter()
+ .any(|item| item.itemtype == ItemType::CrudeMinerals);
+ is_within_range = mining.range > position.distance_from(target.position.clone());
+ true
}
- _ => MiningData {
- has_astroid_target: false,
- astroid_has_minerals: false,
- is_within_range: false,
- range: mining.range,
- status: mining.status.clone(),
- },
+ _ => false,
+ };
+
+ MiningData {
+ has_astroid_target,
+ astroid_has_minerals,
+ is_within_range,
+ range: mining.range,
+ status: mining.status.clone(),
}
}