Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: don't depend on hard-coded attribute IDs
Browse files Browse the repository at this point in the history
TrueBrain committed Jul 21, 2024
1 parent 579c7f5 commit 51eaea2
Showing 5 changed files with 47 additions and 48 deletions.
20 changes: 3 additions & 17 deletions src/calculate/pass_4.rs
Original file line number Diff line number Diff line change
@@ -5,22 +5,8 @@ pub struct PassFour {}

mod capacitor;

#[allow(non_camel_case_types)]
pub enum AttributeId {
capacitorPeakDelta = -1,
capacitorDepletesIn = -2,

capacitorNeed = 6,

speed = 51,
rechargeRate = 55,
duration = 73,

capacitorCapacity = 482,
}

impl Item {
pub fn add_attribute(&mut self, attribute_id: AttributeId, base_value: f64, value: f64) {
pub fn add_attribute(&mut self, attribute_id: i32, base_value: f64, value: f64) {
let mut attribute = Attribute::new(base_value);
attribute.value = Some(value);
self.attributes.insert(attribute_id as i32, attribute);
@@ -29,7 +15,7 @@ impl Item {

/* Attributes don't contain all information displayed, so we calculate some fake attributes with those values. */
impl Pass for PassFour {
fn pass(_info: &impl Info, ship: &mut Ship) {
capacitor::attribute_capacitor_depletes_in(ship);
fn pass(info: &impl Info, ship: &mut Ship) {
capacitor::attribute_capacitor_depletes_in(info, ship);
}
}
57 changes: 26 additions & 31 deletions src/calculate/pass_4/capacitor.rs
Original file line number Diff line number Diff line change
@@ -1,46 +1,46 @@
use crate::info::Info;

use super::super::Ship;
use super::AttributeId;

struct Module {
capacitor_need: f64,
duration: f64,
time_next: f64,
}

pub fn attribute_capacitor_depletes_in(ship: &mut Ship) {
pub fn attribute_capacitor_depletes_in(info: &impl Info, ship: &mut Ship) {
/* Amount of seconds it takes for the capacitor to deplete; or negative if it is stable. */

let attr_capacitor_peak_delta_id = info.attribute_name_to_id("capacitorPeakDelta");
let attr_capacitor_capacity_id = info.attribute_name_to_id("capacitorCapacity");
let attr_recharge_rate_id = info.attribute_name_to_id("rechargeRate");
let attr_capacitor_need_id = info.attribute_name_to_id("capacitorNeed");
let attr_cycle_time_id = info.attribute_name_to_id("cycleTime");
let attr_capacitor_depletes_in_id = info.attribute_name_to_id("capacitorDepletesIn");

if !ship
.hull
.attributes
.contains_key(&(AttributeId::capacitorDepletesIn as i32))
.contains_key(&attr_capacitor_peak_delta_id)
{
return;
}

let mut depletes_in = -1.0;
let mut depletes_in = -1000.0;

let attr_capacitor_peak_delta = ship
.hull
.attributes
.get(&(AttributeId::capacitorPeakDelta as i32))
.get(&attr_capacitor_peak_delta_id)
.unwrap();

if attr_capacitor_peak_delta.value.unwrap() < 0.0 {
let attr_capacitor_capacity = ship
.hull
.attributes
.get(&(AttributeId::capacitorCapacity as i32))
.unwrap();
let attr_recharge_rate = ship
.hull
.attributes
.get(&(AttributeId::rechargeRate as i32))
.get(&attr_capacitor_capacity_id)
.unwrap();

let attr_capacitor_need = AttributeId::capacitorNeed as i32;
let attr_duration = AttributeId::duration as i32;
let attr_rate_of_fire = AttributeId::speed as i32;
let attr_recharge_rate = ship.hull.attributes.get(&attr_recharge_rate_id).unwrap();

/* Find all modules consuming capacitor. */
let mut modules = Vec::new();
@@ -49,27 +49,22 @@ pub fn attribute_capacitor_depletes_in(ship: &mut Ship) {
continue;
}

if !item.attributes.contains_key(&attr_capacitor_need) {
if !item.attributes.contains_key(&attr_capacitor_need_id)
|| !item.attributes.contains_key(&attr_cycle_time_id)
{
continue;
}

/* Depending on the module, the duration is based either on "duration" or on "speed" (read: rate-of-fire). */
let duration = if item.attributes.contains_key(&attr_duration) {
item.attributes.get(&attr_duration).unwrap().value.unwrap()
} else if item.attributes.contains_key(&attr_rate_of_fire) {
item.attributes
.get(&attr_rate_of_fire)
.unwrap()
.value
.unwrap()
} else {
/* Neither speed nor duration; so no cap use. */
continue;
};
let duration = item
.attributes
.get(&attr_cycle_time_id)
.unwrap()
.value
.unwrap();

let capacitor_need = item
.attributes
.get(&attr_capacitor_need)
.get(&attr_capacitor_need_id)
.unwrap()
.value
.unwrap();
@@ -116,5 +111,5 @@ pub fn attribute_capacitor_depletes_in(ship: &mut Ship) {
}

ship.hull
.add_attribute(AttributeId::capacitorDepletesIn, 0.0, depletes_in / 1000.0);
.add_attribute(attr_capacitor_depletes_in_id, 0.0, depletes_in / 1000.0);
}
1 change: 1 addition & 0 deletions src/info.rs
Original file line number Diff line number Diff line change
@@ -11,4 +11,5 @@ pub trait Info {
fn get_dogma_effects(&self, type_id: i32) -> Vec<data_types::TypeDogmaEffect>;
fn get_dogma_effect(&self, effect_id: i32) -> data_types::DogmaEffect;
fn get_type(&self, type_id: i32) -> data_types::Type;
fn attribute_name_to_id(&self, name: &str) -> i32;
}
9 changes: 9 additions & 0 deletions src/rust/info.rs
Original file line number Diff line number Diff line change
@@ -138,6 +138,15 @@ impl Info for InfoMain {
}
}

fn attribute_name_to_id(&self, name: &str) -> i32 {
for (attribute_id, attribute) in &self.dogma_attributes {
if attribute.name == name {
return *attribute_id;
}
}
0
}

fn skills(&self) -> &BTreeMap<i32, i32> {
&self.skills
}
8 changes: 8 additions & 0 deletions src/wasm/mod.rs
Original file line number Diff line number Diff line change
@@ -22,6 +22,9 @@ extern "C" {

#[wasm_bindgen(js_namespace = window)]
fn get_type(type_id: i32) -> JsValue;

#[wasm_bindgen(js_namespace = window)]
fn attribute_name_to_id(name: &str) -> JsValue;
}

pub struct InfoWasm {
@@ -55,6 +58,11 @@ impl Info for InfoWasm {
serde_wasm_bindgen::from_value(js).unwrap()
}

fn attribute_name_to_id(&self, name: &str) -> i32 {
let js = attribute_name_to_id(name);
serde_wasm_bindgen::from_value(js).unwrap()
}

fn skills(&self) -> &BTreeMap<i32, i32> {
&self.skills
}

0 comments on commit 51eaea2

Please sign in to comment.