diff --git a/src/calculate/pass_4.rs b/src/calculate/pass_4.rs index 9cc5723..2866e6b 100644 --- a/src/calculate/pass_4.rs +++ b/src/calculate/pass_4.rs @@ -1,114 +1,50 @@ -use super::item::{Attribute, EffectCategory}; +use super::item::Attribute; use super::{Info, Pass, Ship}; pub struct PassFour {} -fn add_attribute(ship: &mut Ship, attribute_id: i32, base_value: f64, value: f64) { - let mut attribute = Attribute::new(base_value); - attribute.value = Some(value); - ship.hull.attributes.insert(attribute_id, attribute); -} - -fn calculate_align_time(ship: &mut Ship) -> (f64, f64) { - /* Align-time is based on agility (70) and mass (4). */ - - let base_agility = ship.hull.attributes.get(&70).unwrap().base_value; - let base_mass = ship.hull.attributes.get(&4).unwrap().base_value; - let base_align_time = -(0.25 as f64).ln() * base_agility * base_mass / 1000000.0; - - let agility = ship.hull.attributes.get(&70).unwrap().value.unwrap(); - let mass = ship.hull.attributes.get(&4).unwrap().value.unwrap(); - let align_time = -(0.25 as f64).ln() * agility * mass / 1000000.0; - - (base_align_time, align_time) -} - -fn add_scan_strength(ship: &mut Ship) -> (f64, f64) { - /* Scan Strength can be one of 4 values. */ - - let mut base_scan_strength = 0.0; - let mut scan_strength = 0.0; - for attribute_id in vec![208, 209, 210, 211].iter() { - if ship.hull.attributes.contains_key(attribute_id) { - let attribute = ship.hull.attributes.get(attribute_id).unwrap(); - - if attribute.base_value > base_scan_strength { - base_scan_strength = attribute.base_value; - } - if attribute.value.unwrap() > scan_strength { - scan_strength = attribute.value.unwrap(); - } - } +mod align_time; +mod cpu_power; +mod scan_strength; + +#[allow(non_camel_case_types)] +pub enum AttributeId { + alignTime = -1, + scanStrength = -2, + cpuUsed = -3, + powerUsed = -4, + cpuUnused = -5, + powerUnused = -6, + + mass = 4, + powerOutput = 11, + power = 30, + cpuOutput = 48, + cpu = 50, + agility = 70, + + scanRadarStrength = 208, + scanLadarStrength = 209, + scanMagnetometricStrength = 210, + scanGravimetricStrength = 211, +} + +impl Ship { + pub fn add_attribute(&mut self, attribute_id: AttributeId, base_value: f64, value: f64) { + let mut attribute = Attribute::new(base_value); + attribute.value = Some(value); + self.hull.attributes.insert(attribute_id as i32, attribute); } - - (base_scan_strength, scan_strength) -} - -fn add_cpu_used(ship: &mut Ship) -> (f64, f64) { - /* How much CPU is being used, which is adding up cpuOuput (50) from all items. */ - - let mut cpu_used = 0.0; - for item in &ship.items { - if item.attributes.contains_key(&50) && item.state != EffectCategory::Passive { - cpu_used += item.attributes.get(&50).unwrap().value.unwrap(); - } - } - - (0.0, cpu_used) -} - -fn add_pg_used(ship: &mut Ship) -> (f64, f64) { - /* How much PG is being used, which is adding up powerOutput (30) from all items. */ - - let mut pg_used = 0.0; - for item in &ship.items { - if item.attributes.contains_key(&30) && item.state != EffectCategory::Passive { - pg_used += item.attributes.get(&30).unwrap().value.unwrap(); - } - } - - (0.0, pg_used) -} - -fn add_cpu_unused(ship: &mut Ship) -> (f64, f64) { - /* How much CPU is left, which is the total CPU minus the usage. */ - - let cpu_used = ship.hull.attributes.get(&-3).unwrap().value.unwrap(); - let cpu_output = ship.hull.attributes.get(&48).unwrap().value.unwrap(); - let cpu_unused = cpu_output - cpu_used; - - (0.0, cpu_unused) -} - -fn add_pg_unused(ship: &mut Ship) -> (f64, f64) { - /* How much PG is left, which is the total PG minus the usage. */ - - let pg_used = ship.hull.attributes.get(&-4).unwrap().value.unwrap(); - let pg_output = ship.hull.attributes.get(&11).unwrap().value.unwrap(); - let pg_unused = pg_output - pg_used; - - (0.0, pg_unused) } /* Attributes don't contain all information displayed, so we calculate some fake attributes with those values. */ impl Pass for PassFour { fn pass(_info: &Info, ship: &mut Ship) { - let align_time = calculate_align_time(ship); - add_attribute(ship, -1, align_time.0, align_time.1); - - let scan_strength = add_scan_strength(ship); - add_attribute(ship, -2, scan_strength.0, scan_strength.1); - - let cpu_used = add_cpu_used(ship); - add_attribute(ship, -3, cpu_used.0, cpu_used.1); - - let pg_used = add_pg_used(ship); - add_attribute(ship, -4, pg_used.0, pg_used.1); - - let cpu_unused = add_cpu_unused(ship); - add_attribute(ship, -5, cpu_unused.0, cpu_unused.1); - - let pg_unused = add_pg_unused(ship); - add_attribute(ship, -6, pg_unused.0, pg_unused.1); + align_time::attribute_align_time(ship); + scan_strength::attribute_scan_strength(ship); + cpu_power::attribute_cpu_used(ship); + cpu_power::attribute_power_used(ship); + cpu_power::attribute_cpu_unused(ship); + cpu_power::attribute_power_unused(ship); } } diff --git a/src/calculate/pass_4/align_time.rs b/src/calculate/pass_4/align_time.rs new file mode 100644 index 0000000..2a4bc1e --- /dev/null +++ b/src/calculate/pass_4/align_time.rs @@ -0,0 +1,25 @@ +use super::super::Ship; +use super::AttributeId; + +pub fn attribute_align_time(ship: &mut Ship) { + /* Align-time is based on agility and mass. */ + + let attr_agility = AttributeId::agility as i32; + let attr_mass = AttributeId::mass as i32; + + let base_agility = ship.hull.attributes.get(&attr_agility).unwrap().base_value; + let base_mass = ship.hull.attributes.get(&attr_mass).unwrap().base_value; + let base_align_time = -(0.25 as f64).ln() * base_agility * base_mass / 1000000.0; + + let agility = ship + .hull + .attributes + .get(&attr_agility) + .unwrap() + .value + .unwrap(); + let mass = ship.hull.attributes.get(&attr_mass).unwrap().value.unwrap(); + let align_time = -(0.25 as f64).ln() * agility * mass / 1000000.0; + + ship.add_attribute(AttributeId::alignTime, base_align_time, align_time); +} diff --git a/src/calculate/pass_4/cpu_power.rs b/src/calculate/pass_4/cpu_power.rs new file mode 100644 index 0000000..83f0ea8 --- /dev/null +++ b/src/calculate/pass_4/cpu_power.rs @@ -0,0 +1,83 @@ +use super::super::item::EffectCategory; +use super::super::Ship; +use super::AttributeId; + +pub fn attribute_cpu_used(ship: &mut Ship) { + /* How much CPU is being used, which is adding up cpu from all items. */ + + let attr_cpu = AttributeId::cpu as i32; + + let mut cpu_used = 0.0; + for item in &ship.items { + if item.attributes.contains_key(&attr_cpu) && item.state != EffectCategory::Passive { + cpu_used += item.attributes.get(&attr_cpu).unwrap().value.unwrap(); + } + } + + ship.add_attribute(AttributeId::cpuUsed, 0.0, cpu_used); +} + +pub fn attribute_power_used(ship: &mut Ship) { + /* How much PG is being used, which is adding up power from all items. */ + + let attr_power = AttributeId::power as i32; + + let mut power_used = 0.0; + for item in &ship.items { + if item.attributes.contains_key(&attr_power) && item.state != EffectCategory::Passive { + power_used += item.attributes.get(&attr_power).unwrap().value.unwrap(); + } + } + + ship.add_attribute(AttributeId::powerUsed, 0.0, power_used); +} + +pub fn attribute_cpu_unused(ship: &mut Ship) { + /* How much CPU is left, which is the total CPU minus the usage. */ + + let attr_cpu_used = AttributeId::cpuUsed as i32; + let attr_cpu_output = AttributeId::cpuOutput as i32; + + let cpu_used = ship + .hull + .attributes + .get(&attr_cpu_used) + .unwrap() + .value + .unwrap(); + let cpu_output = ship + .hull + .attributes + .get(&attr_cpu_output) + .unwrap() + .value + .unwrap(); + let cpu_unused = cpu_output - cpu_used; + + ship.add_attribute(AttributeId::cpuUnused, 0.0, cpu_unused); +} + +pub fn attribute_power_unused(ship: &mut Ship) { + /* How much PG is left, which is the total PG minus the usage. */ + + let attr_power_used = AttributeId::powerUsed as i32; + let attr_power_output = AttributeId::powerOutput as i32; + + let power_used = ship + .hull + .attributes + .get(&attr_power_used) + .unwrap() + .value + .unwrap(); + let power_output = ship + .hull + .attributes + .get(&attr_power_output) + .unwrap() + .value + .unwrap(); + let power_unused = power_output - power_used; + + ship.add_attribute(AttributeId::powerUnused, 0.0, power_unused); +} diff --git a/src/calculate/pass_4/scan_strength.rs b/src/calculate/pass_4/scan_strength.rs new file mode 100644 index 0000000..9e686a1 --- /dev/null +++ b/src/calculate/pass_4/scan_strength.rs @@ -0,0 +1,30 @@ +use super::super::Ship; +use super::AttributeId; + +static SCAN_STRENGTH_ATTRIBUTES: [i32; 4] = [ + AttributeId::scanRadarStrength as i32, + AttributeId::scanLadarStrength as i32, + AttributeId::scanMagnetometricStrength as i32, + AttributeId::scanGravimetricStrength as i32, +]; + +pub fn attribute_scan_strength(ship: &mut Ship) { + /* Scan Strength can be one of 4 scan strength values (radar, ladar, magnetometric, gravimetric). */ + + let mut base_scan_strength = 0.0; + let mut scan_strength = 0.0; + for attribute_id in SCAN_STRENGTH_ATTRIBUTES.iter() { + if ship.hull.attributes.contains_key(attribute_id) { + let attribute = ship.hull.attributes.get(attribute_id).unwrap(); + + if attribute.base_value > base_scan_strength { + base_scan_strength = attribute.base_value; + } + if attribute.value.unwrap() > scan_strength { + scan_strength = attribute.value.unwrap(); + } + } + } + + ship.add_attribute(AttributeId::scanStrength, base_scan_strength, scan_strength); +}