Skip to content

Commit

Permalink
♻️ Move AttackPowerModifier to types
Browse files Browse the repository at this point in the history
  • Loading branch information
madonoharu committed Jun 21, 2022
1 parent e52b7f9 commit 96c14d6
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 105 deletions.
13 changes: 8 additions & 5 deletions crates/fleethub-core/src/analyzer/warfare_info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,15 @@ use tsify::Tsify;
use crate::{
attack::{
get_day_battle_attack_type, get_night_battle_attack_type, get_oasw_attack_type,
AswAttackContext, AswAttackType, AswTime, CustomModifiers, DayBattleAttackType,
NightAttackContext, NightBattleAttackType, NightSituation, ShellingAttackContext,
ShellingSupportAttackContext, TorpedoAttackContext, WarfareContext, WarfareShipEnvironment,
AswAttackContext, AswAttackType, AswTime, DayBattleAttackType, NightAttackContext,
NightBattleAttackType, NightSituation, ShellingAttackContext, ShellingSupportAttackContext,
TorpedoAttackContext, WarfareContext, WarfareShipEnvironment,
},
ship::Ship,
types::{AirState, BattleConfig, DayCutin, Engagement, Formation, NightCutin, OrgType, Role},
types::{
AirState, BattleConfig, CustomPowerModifiers, DayCutin, Engagement, Formation, NightCutin,
OrgType, Role,
},
};

use super::{AttackInfo, AttackInfoItem, AttackStats, DayCutinRateInfo, NightCutinRateAnalyzer};
Expand All @@ -27,7 +30,7 @@ pub struct WarfareAnalyzerShipEnvironment {
pub formation: Formation,
pub fleet_los_mod: Option<f64>,
pub night_situation: NightSituation,
pub custom_mods: CustomModifiers,
pub custom_mods: CustomPowerModifiers,
}

impl WarfareAnalyzerShipEnvironment {
Expand Down
3 changes: 1 addition & 2 deletions crates/fleethub-core/src/attack/airstrike.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
use rand::prelude::*;

use crate::{
attack::AttackPowerModifier,
member::MemberImpl,
plane::{AirstrikeType, PlaneImpl},
types::ContactRank,
types::{AttackPowerModifier, ContactRank},
};

use super::{AttackParams, AttackPowerParams, DefenseParams, HitRateParams, ProficiencyModifiers};
Expand Down
12 changes: 6 additions & 6 deletions crates/fleethub-core/src/attack/asw.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use tsify::Tsify;
use crate::{
attack::{DefenseParams, HitRateParams},
ship::Ship,
types::{gear_id, matches_gear_id, BattleConfig, Engagement, GearAttr, GearType, ShipType},
types::{
gear_id, matches_gear_id, AttackPowerModifier, BattleConfig, CustomPowerModifiers,
Engagement, GearAttr, GearType, ShipType,
},
};

use super::{
AttackParams, AttackPowerModifier, AttackPowerParams, CustomModifiers, WarfareContext,
WarfareShipEnvironment,
};
use super::{AttackParams, AttackPowerParams, WarfareContext, WarfareShipEnvironment};

const ASW_POWER_CAP: f64 = 170.0;
const ASW_ACCURACY_CONSTANT: f64 = 80.0;
Expand Down Expand Up @@ -55,7 +55,7 @@ pub struct AswAttackContext<'a> {
pub formation_accuracy_mod: f64,
pub formation_evasion_mod: f64,

pub custom_mods: &'a CustomModifiers,
pub custom_mods: &'a CustomPowerModifiers,
}

impl<'a> AswAttackContext<'a> {
Expand Down
63 changes: 4 additions & 59 deletions crates/fleethub-core/src/attack/attack_power.rs
Original file line number Diff line number Diff line change
@@ -1,52 +1,7 @@
use serde::{Deserialize, Serialize};
use tsify::Tsify;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Tsify)]
pub struct AttackPowerModifier {
pub a: f64,
pub b: f64,
}

impl Default for AttackPowerModifier {
fn default() -> Self {
Self { a: 1.0, b: 0.0 }
}
}

impl From<(f64, f64)> for AttackPowerModifier {
#[inline]
fn from((a, b): (f64, f64)) -> Self {
Self { a, b }
}
}

impl AttackPowerModifier {
pub fn new(a: f64, b: f64) -> Self {
Self { a, b }
}

pub fn set(&mut self, a: f64, b: f64) {
self.a = a;
self.b = b;
}

pub fn compose(&self, other: &Self) -> Self {
Self {
a: self.a * other.a,
b: self.b + other.b,
}
}

pub fn merge(&mut self, a: f64, b: f64) {
self.a *= a;
self.b += b;
}

#[inline]
pub fn apply(&self, v: f64) -> f64 {
self.a * v + self.b
}
}
use crate::types::{AttackPowerModifier, CustomPowerModifiers};

#[derive(Debug, Default, Clone, Serialize, Deserialize, Tsify)]
pub struct SpecialEnemyModifiers {
Expand All @@ -66,16 +21,6 @@ impl SpecialEnemyModifiers {
}
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, Tsify)]
pub struct CustomModifiers {
#[serde(default)]
pub precap_mod: AttackPowerModifier,
#[serde(default)]
pub postcap_mod: AttackPowerModifier,
#[serde(default)]
pub basic_power_mod: AttackPowerModifier,
}

#[derive(Debug, Clone, Serialize, Deserialize, Tsify)]
pub struct AttackPowerParams {
pub basic: f64,
Expand All @@ -88,7 +33,7 @@ pub struct AttackPowerParams {
pub armor_penetration: f64,
pub remaining_ammo_mod: f64,
pub special_enemy_mods: SpecialEnemyModifiers,
pub custom_mods: CustomModifiers,
pub custom_mods: CustomPowerModifiers,
}

#[derive(Debug, Default, Clone, Serialize, Deserialize, Tsify)]
Expand Down Expand Up @@ -219,7 +164,7 @@ mod test {
let precap = AttackPowerParams {
basic: 150.0,
precap_mod: AttackPowerModifier { a: 1.2, b: 0.0 },
custom_mods: CustomModifiers {
custom_mods: CustomPowerModifiers {
precap_mod: AttackPowerModifier { a: 1.3, b: 0.0 },
..Default::default()
},
Expand All @@ -233,7 +178,7 @@ mod test {
basic: 200.0,
cap: 180.0,
postcap_mod: AttackPowerModifier { a: 1.2, b: 0.0 },
custom_mods: CustomModifiers {
custom_mods: CustomPowerModifiers {
postcap_mod: AttackPowerModifier { a: 1.3, b: 0.0 },
..Default::default()
},
Expand Down
6 changes: 2 additions & 4 deletions crates/fleethub-core/src/attack/context.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
use serde::{Deserialize, Serialize};
use tsify::Tsify;

use crate::types::{AirState, Engagement, Formation, OrgType, Role};

use super::CustomModifiers;
use crate::types::{AirState, CustomPowerModifiers, Engagement, Formation, OrgType, Role};

#[derive(Debug, Clone, Default, Serialize, Deserialize, Tsify)]
#[tsify(into_wasm_abi, from_wasm_abi)]
Expand Down Expand Up @@ -32,5 +30,5 @@ pub struct WarfareContext {
pub target_env: WarfareShipEnvironment,
pub engagement: Engagement,
pub air_state: AirState,
pub custom_mods: CustomModifiers,
pub custom_mods: CustomPowerModifiers,
}
9 changes: 4 additions & 5 deletions crates/fleethub-core/src/attack/night.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,14 @@ use crate::{
attack::DefenseParams,
ship::Ship,
types::{
BattleConfig, ContactRank, GearType, NightAttackType, NightSpecialAttack, ShipType,
SpecialAttackDef,
AttackPowerModifier, BattleConfig, ContactRank, CustomPowerModifiers, GearType,
NightAttackType, NightSpecialAttack, ShipType, SpecialAttackDef,
},
};

use super::{
shelling::ProficiencyModifiers, special_enemy_mods::special_enemy_modifiers, AttackParams,
AttackPowerModifier, AttackPowerParams, CustomModifiers, HitRateParams, WarfareContext,
WarfareShipEnvironment,
AttackPowerParams, HitRateParams, WarfareContext, WarfareShipEnvironment,
};

const NIGHT_POWER_CAP: f64 = 360.0;
Expand All @@ -39,7 +38,7 @@ pub struct NightAttackContext<'a> {
pub formation_accuracy_mod: f64,
pub formation_evasion_mod: f64,

pub custom_mods: &'a CustomModifiers,
pub custom_mods: &'a CustomPowerModifiers,

pub special_attack_def: Option<SpecialAttackDef<NightSpecialAttack>>,
}
Expand Down
9 changes: 6 additions & 3 deletions crates/fleethub-core/src/attack/shelling.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,12 @@ use serde::{Deserialize, Serialize};
use tsify::Tsify;

use crate::{
attack::{AttackPowerModifier, AttackPowerParams, CustomModifiers, HitRateParams},
attack::{AttackPowerParams, HitRateParams},
ship::Ship,
types::{AirState, BattleConfig, Engagement, ShellingSpecialAttack, SpecialAttackDef},
types::{
AirState, AttackPowerModifier, BattleConfig, CustomPowerModifiers, Engagement,
ShellingSpecialAttack, SpecialAttackDef,
},
};

use super::{
Expand Down Expand Up @@ -44,7 +47,7 @@ pub struct ShellingAttackContext<'a> {

pub attacker_env: &'a WarfareShipEnvironment,
pub target_env: &'a WarfareShipEnvironment,
pub custom_mods: &'a CustomModifiers,
pub custom_mods: &'a CustomPowerModifiers,
pub engagement: Engagement,
pub air_state: AirState,

Expand Down
9 changes: 4 additions & 5 deletions crates/fleethub-core/src/attack/shelling_support.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
use crate::{
types::{BattleConfig, Engagement, FormationDef},
types::{AttackPowerModifier, BattleConfig, CustomPowerModifiers, Engagement, FormationDef},
Ship,
};

use super::{
special_enemy_mods::shelling_support_special_enemy_modifiers, AttackParams,
AttackPowerModifier, AttackPowerParams, CustomModifiers, DefenseParams, HitRateParams,
ShellingAttackType, WarfareContext,
special_enemy_mods::shelling_support_special_enemy_modifiers, AttackParams, AttackPowerParams,
DefenseParams, HitRateParams, ShellingAttackType, WarfareContext,
};

const SHELLING_SUPPORT_POWER_CAP: f64 = 170.0;
Expand All @@ -20,7 +19,7 @@ pub struct ShellingSupportAttackParams<'a> {
pub engagement: Engagement,
pub attacker_formation_def: &'a FormationDef,
pub target_formation_def: &'a FormationDef,
pub custom_mods: CustomModifiers,
pub custom_mods: CustomPowerModifiers,
pub defense_params: Option<DefenseParams>,
}

Expand Down
8 changes: 4 additions & 4 deletions crates/fleethub-core/src/attack/torpedo.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{
ship::Ship,
types::{AirState, BattleConfig, Engagement},
types::{AirState, AttackPowerModifier, BattleConfig, CustomPowerModifiers, Engagement},
};

use super::{
fleet_factor::{self, ShipPosition},
AttackParams, AttackPowerModifier, AttackPowerParams, CustomModifiers, DefenseParams,
HitRateParams, WarfareContext, WarfareShipEnvironment,
AttackParams, AttackPowerParams, DefenseParams, HitRateParams, WarfareContext,
WarfareShipEnvironment,
};

const TORPEDO_POWER_CAP: f64 = 180.0;
Expand All @@ -15,7 +15,7 @@ const TORPEDO_CRITICAL_RATE_CONSTANT: f64 = 1.5;
pub struct TorpedoAttackContext<'a> {
pub attacker_env: &'a WarfareShipEnvironment,
pub target_env: &'a WarfareShipEnvironment,
pub custom_mods: &'a CustomModifiers,
pub custom_mods: &'a CustomPowerModifiers,
pub engagement: Engagement,
pub air_state: AirState,

Expand Down
10 changes: 5 additions & 5 deletions crates/fleethub-core/src/simulator/shelling_support.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@ use tsify::Tsify;

use crate::{
attack::{
get_day_battle_attack_type, CustomModifiers, DayBattleAttackType, DefenseParams,
ShellingAttackType, ShellingSupportAttackParams,
get_day_battle_attack_type, DayBattleAttackType, DefenseParams, ShellingAttackType,
ShellingSupportAttackParams,
},
comp::Comp,
fleet::Fleet,
ship::Ship,
types::{BattleConfig, Engagement, Formation, FormationDef, Side},
types::{BattleConfig, CustomPowerModifiers, Engagement, Formation, FormationDef, Side},
};

use super::{BattleLogger, SimulatorResult};
Expand Down Expand Up @@ -88,7 +88,7 @@ pub struct ShellingSupportSimulatorParams {
attacker_formation: Formation,
target_formation: Formation,
engagement: Engagement,
custom_mods: CustomModifiers,
custom_mods: CustomPowerModifiers,
}

struct ShellingSupportBattle<'a, R>
Expand All @@ -101,7 +101,7 @@ where
engagement: Engagement,
attacker_formation_def: &'a FormationDef,
target_formation_def: &'a FormationDef,
custom_mods: CustomModifiers,
custom_mods: CustomPowerModifiers,
}

impl<'a, R> ShellingSupportBattle<'a, R>
Expand Down
2 changes: 2 additions & 0 deletions crates/fleethub-core/src/types.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
mod air_combat;
mod air_state;
mod attack_power_modifier;
mod battle_config;
mod const_id;
mod contact_rank;
Expand All @@ -25,6 +26,7 @@ mod state;

pub use air_combat::*;
pub use air_state::*;
pub use attack_power_modifier::*;
pub use battle_config::*;
#[allow(unused_imports)]
pub use const_id::*;
Expand Down
Loading

0 comments on commit 96c14d6

Please sign in to comment.