wowsunpack 0.43.0

Utility for interacting with World of Warships game assets
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
//! Render `ShipStatsProvenance` into translated, formatted attribution lines:
//! the localized stat label, the base module, and each contributing input with
//! its magnitude. Mirrors `render::render_stat_rows`.

use std::collections::HashMap;
use std::collections::HashSet;

use crate::data::ResourceLoader;
use crate::game_params::ttx::labels::TtxStat;
use crate::game_params::ttx::labels::stat_display_label;
use crate::game_params::ttx::model::StatRow;
use crate::game_params::ttx::provenance::Contribution;
use crate::game_params::ttx::provenance::InputId;
use crate::game_params::ttx::provenance::Op;
use crate::game_params::ttx::provenance::ShipStatsProvenance;
use crate::game_params::ttx::provenance::StatKey;
use crate::game_params::types::GameParamProvider;
use crate::recognized::Recognized;

/// One contributing input rendered for display.
#[derive(Clone, Debug, PartialEq)]
pub struct ContributorLine {
    /// Localized input name (e.g. "Main Battery Mod 3", "Adrenaline Rush").
    pub label: String,
    /// The applied magnitude, formatted: "x0.95" (Mul) or "+350" (Add).
    pub effect: String,
    /// The signed amount this step moved the stat, in its units, trimmed
    /// ("+1000", "-1.2"). Per-step deltas sum to `value - base_value`; for an
    /// `order_sensitive` stat a multiplicative step's delta reflects its position
    /// in the game formula.
    pub delta: String,
    /// The raw signed delta in the stat's units (`delta` unformatted). Surfaced
    /// so a caller can apply its own good/bad polarity and formatting; the model
    /// does not encode whether higher or lower is better.
    pub delta_raw: f32,
    /// The running stat value after this step applies, trimmed (the waterfall
    /// absolute; the last contributor's equals the final `value`).
    pub value_after: String,
    /// The raw running value after this step (`value_after` unformatted).
    pub value_after_raw: f32,
}

/// One stat's full attribution, rendered.
#[derive(Clone, Debug, PartialEq)]
pub struct AttributionLine {
    pub stat: TtxStat,
    pub qualifier: Option<String>,
    pub label: String,
    pub value: String,
    pub base_label: String,
    pub base_value: String,
    pub contributors: Vec<ContributorLine>,
    /// Upstream stats this value is derived from (e.g. rotation time from
    /// rotation speed). A consumer can resolve each key against the rendered
    /// set to recurse into the upstream stat's contributors.
    pub derived_from: Vec<StatKey>,
    /// True when this stat's chain interleaves multiply and add. The per-step
    /// `delta`s still sum to the total change, but a multiplicative step's delta
    /// reflects its position in the game formula (an additive input applied before
    /// a later multiply is amplified), so consumers may want to note that.
    pub order_sensitive: bool,
    /// Causes inherited from the `derived_from` upstream stats (transitively): the
    /// inputs that moved an upstream stat this value derives from (e.g. range
    /// modifiers behind a dispersion stat, concealment behind on-fire detection).
    /// Distinct from `contributors` because these do NOT sum into this stat's
    /// change and their magnitudes are in the UPSTREAM stat's units (see
    /// `via_stat`). Empty when no upstream stat is modified.
    pub inherited: Vec<InheritedContributor>,
}

/// One contributor inherited from an upstream `derived_from` stat, labeled with
/// the stat it came through. The `line`'s delta/value_after are in that upstream
/// stat's units, not this stat's.
#[derive(Clone, Debug, PartialEq)]
pub struct InheritedContributor {
    /// Localized label of the upstream stat this cause comes through (e.g.
    /// "Firing Range", "Surface Detectability").
    pub via_stat: String,
    /// The upstream stat's qualifier, if any.
    pub via_qualifier: Option<String>,
    /// The upstream stat's contributor (its input label, effect, and
    /// upstream-unit delta/value_after).
    pub line: ContributorLine,
}

/// The display label for an attribution input. Module/Upgrade names are resolved
/// through the loader/provider where possible, falling back to the raw key.
fn input_label(input: &InputId, loader: &dyn ResourceLoader, provider: &dyn GameParamProvider) -> String {
    match input {
        InputId::Module { name, .. } | InputId::Upgrade { name } => resolve_param_label(name, loader, provider),
        InputId::Skill { name } => name.as_str().to_string(),
        InputId::Consumable(c) => match c {
            Recognized::Known(k) => k.name().to_string(),
            Recognized::Unknown(raw) => raw.clone(),
        },
        InputId::Innate { skill_type } => skill_type.clone(),
    }
}

/// Resolve a param key to a localized name, falling back to the key itself.
fn resolve_param_label(key: &str, loader: &dyn ResourceLoader, provider: &dyn GameParamProvider) -> String {
    provider
        .game_param_by_name(key)
        .and_then(|p| loader.localized_name_from_param(&p))
        .unwrap_or_else(|| key.to_string())
}

/// Format a single contribution's magnitude. ASCII `x` / `+`, no unicode.
fn format_effect(c: &Contribution) -> String {
    match c.op {
        Op::Mul => format!("x{}", trim(c.operand)),
        Op::Add => format!("+{}", trim(c.operand)),
    }
}

/// Format a signed unit delta: "+1000", "-1.2" (`trim` already carries the minus).
fn format_delta(d: f32) -> String {
    let s = trim(d);
    if d >= 0.0 { format!("+{s}") } else { s }
}

/// Trim a float to at most 3 decimals without trailing zeros.
fn trim(v: f32) -> String {
    let s = format!("{v:.3}");
    let s = s.trim_end_matches('0').trim_end_matches('.');
    s.to_string()
}

/// Render provenance attributions into display lines. Each line carries the
/// localized stat label, the displayed value sourced from the model `rows`
/// (so infinite ammo shows "inf", bools show "yes"/"no", units are preserved),
/// the base module name and its value, and per-contributor effects.
///
/// `rows` must be the `ShipStats::rows()` matching the card the provenance was
/// recorded for; keys are `(stat, qualifier)` pairs. If a key is absent (should
/// not occur when Task 10 coverage holds), the numeric `a.value` is trimmed as
/// a fallback.
pub fn render_attributions(
    prov: &ShipStatsProvenance,
    rows: &[StatRow],
    loader: &dyn ResourceLoader,
    provider: &dyn GameParamProvider,
) -> Vec<AttributionLine> {
    let display: HashMap<(TtxStat, Option<String>), String> =
        rows.iter().map(|r| ((r.stat, r.qualifier.clone()), r.value.to_string())).collect();

    let mut lines: Vec<AttributionLine> = prov
        .attributions
        .iter()
        .map(|a| {
            let key = (a.stat, a.qualifier.clone());
            debug_assert!(
                display.contains_key(&key),
                "render_attributions: stat {:?} qualifier {:?} absent from rows map; provenance key-set diverged from rows()",
                a.stat,
                a.qualifier
            );
            let value = display.get(&key).cloned().unwrap_or_else(|| trim(a.value));
            let base_value = if a.steps.is_empty() {
                // No contributors: base IS the final value (ammo counts, bools,
                // derived-only stats). Reuse the StatValue display string so the
                // base column shows "inf"/"yes"/"no" rather than the sentinel float.
                value.clone()
            } else {
                trim(a.base_value)
            };
            AttributionLine {
                stat: a.stat,
                qualifier: a.qualifier.clone(),
                label: stat_display_label(a.stat, loader),
                value,
                base_label: input_label(&a.base_source, loader, provider),
                base_value,
                contributors: a
                    .steps
                    .iter()
                    .zip(a.step_deltas())
                    .zip(a.running_values())
                    .map(|((c, delta), running)| ContributorLine {
                        label: input_label(&c.input, loader, provider),
                        effect: format_effect(c),
                        delta: format_delta(delta),
                        delta_raw: delta,
                        value_after: trim(running),
                        value_after_raw: running,
                    })
                    .collect(),
                derived_from: a.derived_from.clone(),
                order_sensitive: a.order_sensitive(),
                inherited: Vec::new(),
            }
        })
        .collect();

    // Second pass: surface each line's transitive `derived_from` causes inline.
    let index: HashMap<StatKey, usize> =
        lines.iter().enumerate().map(|(i, l)| (StatKey { stat: l.stat, qualifier: l.qualifier.clone() }, i)).collect();
    let all_inherited: Vec<Vec<InheritedContributor>> = (0..lines.len())
        .map(|i| {
            let mut out = Vec::new();
            let mut visited: HashSet<StatKey> = HashSet::new();
            visited.insert(StatKey { stat: lines[i].stat, qualifier: lines[i].qualifier.clone() });
            let mut stack: Vec<StatKey> = lines[i].derived_from.clone();
            while let Some(key) = stack.pop() {
                if !visited.insert(key.clone()) {
                    continue;
                }
                if let Some(&j) = index.get(&key) {
                    for c in &lines[j].contributors {
                        out.push(InheritedContributor {
                            via_stat: lines[j].label.clone(),
                            via_qualifier: lines[j].qualifier.clone(),
                            line: c.clone(),
                        });
                    }
                    stack.extend(lines[j].derived_from.clone());
                }
            }
            out
        })
        .collect();
    for (line, inherited) in lines.iter_mut().zip(all_inherited) {
        line.inherited = inherited;
    }
    lines
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::Rc;

    const BASE_HEALTH: f32 = 19400.0;
    const HEALTH_COEFF: f32 = 1.05;
    const HEALTH_BONUS: f32 = 3500.0;
    const HEALTH_FINAL: f32 = 23870.0;
    const RENDER_EPS: f32 = 1e-3;

    use crate::game_params::ttx::model::AmmoCount;
    use crate::game_params::ttx::model::Hp;
    use crate::game_params::ttx::model::StatValue;
    use crate::game_params::ttx::module_options::ModuleSlot;
    use crate::game_params::ttx::provenance::StatAttribution;
    use crate::game_params::types::CrewSkillName;
    use crate::game_params::types::Param;

    struct EchoLoader;
    impl ResourceLoader for EchoLoader {
        fn localized_name_from_param(&self, _p: &Param) -> Option<String> {
            None
        }
        fn localized_name_from_id(&self, id: &crate::data::TranslationKey) -> Option<String> {
            Some(id.as_str().to_string())
        }
        fn game_param_by_id(&self, _id: crate::game_types::GameParamId) -> Option<Rc<Param>> {
            None
        }
        fn entity_specs(&self) -> &[crate::rpc::entitydefs::EntitySpec] {
            &[]
        }
    }

    struct EmptyProvider;
    impl GameParamProvider for EmptyProvider {
        fn game_param_by_id(&self, _id: crate::game_types::GameParamId) -> Option<Rc<Param>> {
            None
        }
        fn game_param_by_index(&self, _i: &str) -> Option<Rc<Param>> {
            None
        }
        fn game_param_by_name(&self, _n: &str) -> Option<Rc<Param>> {
            None
        }
        fn params(&self) -> &[Rc<Param>] {
            &[]
        }
    }

    #[test]
    fn renders_base_and_contributors() {
        let prov = ShipStatsProvenance {
            attributions: vec![StatAttribution {
                stat: TtxStat::Health,
                qualifier: None,
                base_value: BASE_HEALTH,
                base_source: InputId::Module { slot: ModuleSlot::Hull, name: "PAUH911".into() },
                steps: vec![
                    Contribution {
                        input: InputId::Skill { name: CrewSkillName::from("AdrenalineRush") },
                        modifier_name: "healthHullCoeff".into(),
                        op: Op::Mul,
                        operand: HEALTH_COEFF,
                    },
                    Contribution {
                        input: InputId::Upgrade { name: "PCM030".into() },
                        modifier_name: "healthPerLevel".into(),
                        op: Op::Add,
                        operand: HEALTH_BONUS,
                    },
                ],
                derived_from: Vec::new(),
                value: HEALTH_FINAL,
            }],
        };
        let rows =
            vec![StatRow { stat: TtxStat::Health, qualifier: None, value: StatValue::Hp(Hp::from(HEALTH_FINAL)) }];
        let lines = render_attributions(&prov, &rows, &EchoLoader, &EmptyProvider);
        assert_eq!(lines.len(), 1);
        let l = &lines[0];
        assert_eq!(l.base_label, "PAUH911");
        assert_eq!(l.base_value, "19400");
        assert_eq!(l.value, "23870");
        assert_eq!(l.contributors.len(), 2);
        assert_eq!(l.contributors[0].label, "AdrenalineRush");
        assert_eq!(l.contributors[0].effect, "x1.05");
        assert_eq!(l.contributors[1].effect, "+3500");
        // Mixed Mul+Add chain: order-sensitive, and each contributor carries its
        // signed unit delta. x1.05 on 19400 adds 970; +3500 adds 3500; they sum to
        // the 4470 total change (23870 - 19400).
        assert!(l.order_sensitive);
        assert_eq!(l.contributors[0].delta, "+970");
        assert_eq!(l.contributors[1].delta, "+3500");
        // Running waterfall absolutes: 19400 -> 20370 -> 23870 (= final value).
        assert_eq!(l.contributors[0].value_after, "20370");
        assert_eq!(l.contributors[1].value_after, "23870");
        // Raw numeric fields let a caller apply its own polarity/formatting.
        assert!((l.contributors[0].delta_raw - BASE_HEALTH * (HEALTH_COEFF - 1.0)).abs() < RENDER_EPS);
        assert!((l.contributors[1].delta_raw - HEALTH_BONUS).abs() < RENDER_EPS);
        assert!((l.contributors[0].value_after_raw - BASE_HEALTH * HEALTH_COEFF).abs() < RENDER_EPS);
        assert!((l.contributors[1].value_after_raw - HEALTH_FINAL).abs() < RENDER_EPS);
    }

    #[test]
    fn ammo_stat_shows_inf_not_sentinel() {
        // ShellMaxAmmo with Infinite: provenance value is -1.0 (the raw sentinel
        // stored in the attribution), but the display must come from the StatValue.
        let prov = ShipStatsProvenance {
            attributions: vec![StatAttribution {
                stat: TtxStat::ShellMaxAmmo,
                qualifier: Some("HE".into()),
                base_value: -1.0,
                base_source: InputId::Module { slot: ModuleSlot::Hull, name: "PAUH911".into() },
                steps: vec![],
                derived_from: Vec::new(),
                value: -1.0,
            }],
        };
        let rows = vec![StatRow {
            stat: TtxStat::ShellMaxAmmo,
            qualifier: Some("HE".into()),
            value: StatValue::Ammo(AmmoCount::Infinite),
        }];
        let lines = render_attributions(&prov, &rows, &EchoLoader, &EmptyProvider);
        assert_eq!(lines.len(), 1);
        let l = &lines[0];
        assert_eq!(l.value, "inf", "value should be 'inf', not '-1'");
        assert_eq!(l.base_value, "inf", "base_value should also be 'inf' when steps is empty");
    }

    #[test]
    fn bool_stat_shows_yes_not_one() {
        // TorpedoIsDamageIncreasing with true: provenance value is 1.0, but
        // display must come from the StatValue which renders as "yes".
        let prov = ShipStatsProvenance {
            attributions: vec![StatAttribution {
                stat: TtxStat::TorpedoIsDamageIncreasing,
                qualifier: Some("0".into()),
                base_value: 1.0,
                base_source: InputId::Module { slot: ModuleSlot::Hull, name: "PAUH911".into() },
                steps: vec![],
                derived_from: Vec::new(),
                value: 1.0,
            }],
        };
        let rows = vec![StatRow {
            stat: TtxStat::TorpedoIsDamageIncreasing,
            qualifier: Some("0".into()),
            value: StatValue::Bool(true),
        }];
        let lines = render_attributions(&prov, &rows, &EchoLoader, &EmptyProvider);
        assert_eq!(lines.len(), 1);
        let l = &lines[0];
        assert_eq!(l.value, "yes", "value should be 'yes', not '1'");
        assert_eq!(l.base_value, "yes", "base_value should also be 'yes' when steps is empty");
    }

    #[test]
    fn derived_stat_surfaces_inherited_upstream_contributors() {
        // Dispersion derives from range; a range modifier should appear on the
        // dispersion line as an inherited cause, labeled via the range stat, with
        // its magnitude in the upstream (range) units.
        let range_mod = InputId::Upgrade { name: "ArtilleryPlottingRoomMod".into() };
        let prov = ShipStatsProvenance {
            attributions: vec![
                StatAttribution {
                    stat: TtxStat::ArtilleryRange,
                    qualifier: None,
                    base_value: 16.0,
                    base_source: InputId::Module { slot: ModuleSlot::Hull, name: "A".into() },
                    steps: vec![Contribution {
                        input: range_mod.clone(),
                        modifier_name: "GMMaxDist".into(),
                        op: Op::Mul,
                        operand: 1.16,
                    }],
                    derived_from: Vec::new(),
                    value: 16.0 * 1.16,
                },
                StatAttribution {
                    stat: TtxStat::ArtilleryDispersion,
                    qualifier: None,
                    base_value: 100.0,
                    base_source: InputId::Module { slot: ModuleSlot::Hull, name: "A".into() },
                    steps: vec![Contribution {
                        input: InputId::Upgrade { name: "AimingSystemsMod".into() },
                        modifier_name: "GMIdealRadius".into(),
                        op: Op::Mul,
                        operand: 0.95,
                    }],
                    derived_from: vec![StatKey { stat: TtxStat::ArtilleryRange, qualifier: None }],
                    value: 95.0,
                },
            ],
        };
        let rows = vec![
            StatRow { stat: TtxStat::ArtilleryRange, qualifier: None, value: StatValue::Float(16.0 * 1.16) },
            StatRow { stat: TtxStat::ArtilleryDispersion, qualifier: None, value: StatValue::Float(95.0) },
        ];
        let lines = render_attributions(&prov, &rows, &EchoLoader, &EmptyProvider);
        let disp = lines.iter().find(|l| l.stat == TtxStat::ArtilleryDispersion).expect("dispersion line");
        let range = lines.iter().find(|l| l.stat == TtxStat::ArtilleryRange).expect("range line");
        // Its own direct contributor is the aiming mod (in dispersion units).
        assert_eq!(disp.contributors.len(), 1);
        assert_eq!(disp.contributors[0].label, "AimingSystemsMod");
        // The range mod is surfaced as an inherited cause via the range stat, with
        // the range-unit magnitude (x1.16), NOT mixed into the direct contributors.
        assert_eq!(disp.inherited.len(), 1);
        assert_eq!(disp.inherited[0].via_stat, range.label);
        assert_eq!(disp.inherited[0].line.label, "ArtilleryPlottingRoomMod");
        assert_eq!(disp.inherited[0].line.effect, "x1.16");
        // Range itself is not derived, so it has no inherited causes.
        assert!(range.inherited.is_empty());
    }
}