wowsunpack 0.38.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
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
//! Effective (modifier-applied) consumable stats.
//!
//! Transcribes the client's consumable modifier pipeline so consumers get the
//! FINAL consumable stats with an equipped [`ModifierBundle`] applied, the
//! consumable analog of the TTX weapon factories (e.g. `Artillery.reload_time`).
//!
//! The reduction follows `m8074268a/consumables/ConsumableUtils.py`
//! `updateConsumableParams` (lines 113-206) and the `getConsumable*` helpers in
//! `Modifiers/ModifiersApply.py`. Each coefficient cites its deob source. All
//! coefficient names resolve through [`ModifierBundle::coef`] (multiplicative,
//! identity 1.0 when absent) or [`ModifierBundle::apply`] (operator chosen by the
//! modifier's own classification); add-vs-multiply is never hardcoded. The one
//! legitimate default is the absent-modifier identity (a missing coefficient
//! leaves the base value unchanged). A base field that is itself absent maps to an
//! `Option::None` output rather than a fabricated value.

use std::collections::BTreeMap;

use crate::game_params::ttx::constants::BW_TO_BALLISTIC;
use crate::game_params::ttx::constants::KM_TO_M;
use crate::game_params::ttx::model::AmmoCount;
use crate::game_params::ttx::model::Seconds;
use crate::game_params::ttx::modifiers::ModifierBundle;
use crate::game_params::types::AbilityCategory;
use crate::game_params::types::Km;
use crate::game_params::types::Meters;

/// Consumable group strings, `ConsumableConstants.py` `ConsumableGroup`.
const GROUP_SHIP: &str = "ship";
const GROUP_SQUADRON: &str = "squadron";

/// `lifeCycleType` enum values, `ConsumableConstants.py` `ConsumableLifeCycleType`.
const LIFECYCLE_COUNT_BASED: f32 = 0.0;
const LIFECYCLE_TIME_BASED: f32 = 1.0;

/// `ConsumableNames.REGEN_CREW`, the Repair Party consumable type string.
const TYPE_REGEN_CREW: &str = "regenCrew";

/// `ConsumableNames.SMOKE_GENERATOR` (`ConsumableConstants.py:162`).
const TYPE_SMOKE_GENERATOR: &str = "smokeGenerator";
/// `ConsumableNames.PLANE_SMOKE_GENERATOR` (`ConsumableConstants.py:205`).
const TYPE_PLANE_SMOKE_GENERATOR: &str = "planeSmokeGenerator";
/// `ConsumableNames.FIGHTER` (`ConsumableConstants.py:164`).
const TYPE_FIGHTER: &str = "fighter";
/// `ConsumableNames.REGENERATE_HEALTH` (`ConsumableConstants.py:188`).
const TYPE_REGENERATE_HEALTH: &str = "regenerateHealth";

/// `CallFightersConsumables` (`ConsumableConstants.py:226`), the set whose members
/// take the `callFighters*` per-type effect modifiers (ConsumableUtils.py:149-152).
const CALL_FIGHTERS_CONSUMABLES: &[&str] = &["callFighters", "planeTacticalFighters"];

/// `ConsumablesWithReloadCoefficients` (`ConsumableConstants.py`). Membership gates
/// the per-type `<typeName>ReloadCoeff` multiplier in `getConsumableReloadTime`
/// (ModifiersApply.py:200-201, 209-210).
const CONSUMABLES_WITH_RELOAD_COEFFICIENTS: &[&str] = &[
    "artilleryBoosters",
    "torpedoReloader",
    "crashCrew",
    "airDefenseDisp",
    "scout",
    "fighter",
    "sonar",
    "rls",
    "smokeGenerator",
    "speedBoosters",
    "regenCrew",
    "healForsage",
    "regenerateHealth",
    "planeSmokeGenerator",
    "hydrophone",
    "submarineLocator",
    "activeManeuvering",
];

/// `ConsumablesWithWorkTimeCoefficients` (`ConsumableConstants.py`). Gates the
/// per-type `<typeName>WorkTimeCoeff` multiplier in `getConsumableWorkTime`
/// (ModifiersApply.py:225-226, 234-235).
const CONSUMABLES_WITH_WORK_TIME_COEFFICIENTS: &[&str] = &[
    "speedBoosters",
    "smokeGenerator",
    "scout",
    "crashCrew",
    "regenCrew",
    "airDefenseDisp",
    "sonar",
    "rls",
    "fighter",
    "regenerateHealth",
    "callFighters",
    "planeSmokeGenerator",
    "subsEnergyFreeze",
    "fastRudders",
    "submarineLocator",
    "planeTacticalFighters",
    "activeManeuvering",
];

/// `ConsumablesWithCapacityCoefficients` (`ConsumableConstants.py`). Gates the
/// per-type `<typeName>CapacityCoeff` multiplier in `getConsumableCapacity`
/// (ModifiersApply.py:249-250, 258-259).
const CONSUMABLES_WITH_CAPACITY_COEFFICIENTS: &[&str] = &[
    "crashCrew",
    "regenCrew",
    "smokeGenerator",
    "planeSmokeGenerator",
    "speedBoosters",
    "sonar",
    "rls",
    "artilleryBoosters",
    "regenerateHealth",
    "subsEnergyFreeze",
    "scout",
    "fighter",
    "callFighters",
];

/// `AdditionalConsumablesCount` (`ConsumableConstants.py`). Gates the per-type
/// `<typeName>AdditionalConsumables` additive count in `getAdditionalConsumablesCount`
/// (ModifiersApply.py:277-278).
const ADDITIONAL_CONSUMABLES_COUNT: &[&str] = &[
    "crashCrew",
    "regenCrew",
    "regenerateHealth",
    "callFighters",
    "scout",
    "torpedoReloader",
    "smokeGenerator",
    "planeTacticalFighters",
    "activeManeuvering",
];

/// Final, modifier-applied consumable stats. Each field is the base value after the
/// equipped [`ModifierBundle`] has been folded in. Fields that are not applicable to
/// a given consumable (or whose base value is absent) are `None`.
#[derive(Clone, Debug, PartialEq)]
pub struct EffectiveConsumable {
    /// Cooldown between charges, `reloadTime * getConsumableReloadTime`
    /// (ConsumableUtils.py:115).
    pub reload_time: Seconds,
    /// Active duration. `Some` for COUNT_BASED consumables (`workTime`,
    /// ConsumableUtils.py:120); `None` for TIME_BASED consumables, which are governed
    /// by `max_capacity`/`min_work_time` instead.
    pub work_time: Option<Seconds>,
    /// Activation delay, `preparationTime * getConsumableReloadTime`
    /// (ConsumableUtils.py:116).
    pub preparation_time: Seconds,
    /// Resource pool. COUNT_BASED uses `numConsumables` plus the additional-count
    /// bonuses (ConsumableUtils.py:122-128); `-1` means an unlimited pool, modeled as
    /// [`AmmoCount::Infinite`] and never made finite by modifiers.
    pub charges: AmmoCount,
    /// TIME_BASED capacity pool, `maxCapacity * getConsumableCapacity`
    /// (ConsumableUtils.py:166-167). `None` for COUNT_BASED consumables.
    pub max_capacity: Option<f32>,
    /// Detection radius in meters (radar / hydro / sublocator). Read from the base
    /// category (`distShip` / `hydrophoneWaveRadius`); no consumable detection-radius
    /// modifier exists in `updateConsumableParams`, so this is the base value.
    pub detection_radius: Option<Meters>,
    /// Repair Party heal rate as a fraction of max HP per second,
    /// `regenerationHPSpeed * regenerationHPSpeed` modifier (ConsumableUtils.py:140).
    /// `None` for non-heal consumables.
    pub regeneration_hp_speed: Option<f32>,
    /// Smoke screen radius, `logic.radius *= smokeScreenRadiusCoefficient`
    /// (ConsumableUtils.py:134). KILOMETER measure with `BW_TO_BALLISTIC / KM_TO_M`
    /// scale (MODIFIER_SETTINGS `radius`). `Some` only for smoke generators with a base
    /// `radius`.
    pub smoke_radius: Option<Km>,
    /// Smoke screen lifetime, `logic.lifeTime *= smokeGeneratorLifeTime` (ship) /
    /// `planeSmokeGeneratorLifeTime` (plane) (ConsumableUtils.py:133/137). SECOND
    /// measure, multiplier 1.0 (MODIFIER_SETTINGS `lifeTime`). `Some` only for smoke
    /// generators with a base `lifeTime`.
    pub smoke_lifetime: Option<Seconds>,
    /// Fighter count, `logic.fightersNum += extraFighterCount` (ConsumableUtils.py:147,
    /// additive). NONE measure (a raw count). `Some` only for the `fighter` type with a
    /// base `fightersNum`.
    pub fighters_count: Option<f32>,
    /// Call-fighters patrol radius, `logic.radius *= callFightersRadiusCoeff`
    /// (ConsumableUtils.py:150). KILOMETER measure with `BW_TO_BALLISTIC / KM_TO_M`
    /// scale. `Some` only for `CallFightersConsumables` types with a base `radius`.
    pub call_fighters_radius: Option<Km>,
    /// Call-fighters attack delay, `logic.timeDelayAttack *= callFightersTimeDelayAttack`
    /// (ConsumableUtils.py:151). Seconds, multiplier 1.0 (no MODIFIER_SETTINGS display
    /// override). `Some` only for `CallFightersConsumables` types with a base value.
    pub call_fighters_time_delay: Option<Seconds>,
    /// Call-fighters appearance delay, `logic.timeFromHeaven *= callFightersAppearDelay`
    /// (ConsumableUtils.py:152). Seconds, multiplier 1.0. `Some` only for
    /// `CallFightersConsumables` types with a base value.
    pub call_fighters_time_from_heaven: Option<Seconds>,
    /// Plane heal rate, `logic.regenerationRate *= planeRegenerationRate`
    /// (ConsumableUtils.py:144). Raw rate (PERCENT measure, multiplier 1.0). `Some` only
    /// for the `regenerateHealth` type with a base `regenerationRate`.
    pub plane_regeneration_rate: Option<f32>,
}

/// Multiply `base` by `bundle.coef(name)` only when the consumable `type_name` is in
/// `gate`; otherwise return `base` unchanged. Mirrors the client's
/// `if type in <Set>: coeff *= getattr(modifier, type + '<Suffix>')`.
fn gated_type_coef(bundle: &ModifierBundle, base: f32, type_name: &str, gate: &[&str], suffix: &str) -> f32 {
    if gate.contains(&type_name) { base * bundle.coef(&format!("{type_name}{suffix}")) } else { base }
}

/// `getConsumableReloadTime` (ModifiersApply.py:190-214), `slotID=None`.
///
/// `coeff = allConsumableReloadTime`; SHIP `*= ConsumableReloadTime`, SQUADRON
/// `*= planeConsumableReloadTime`; then `*= <typeName>ReloadCoeff` when the type is
/// in `ConsumablesWithReloadCoefficients`.
fn consumable_reload_coeff(bundle: &ModifierBundle, type_name: &str, group: &str) -> f32 {
    // ModifiersApply.py:191
    let mut coeff = bundle.coef("allConsumableReloadTime");
    match group {
        GROUP_SHIP => coeff *= bundle.coef("ConsumableReloadTime"), // :197
        GROUP_SQUADRON => coeff *= bundle.coef("planeConsumableReloadTime"), // :206
        _ => {}
    }
    // :200-201 / :209-210
    gated_type_coef(bundle, coeff, type_name, CONSUMABLES_WITH_RELOAD_COEFFICIENTS, "ReloadCoeff")
}

/// `getConsumableWorkTime` (ModifiersApply.py:217-239), `slotID=None`.
///
/// SHIP `*= ConsumablesWorkTime`, SQUADRON `*= planeConsumablesWorkTime`; then
/// `*= <typeName>WorkTimeCoeff` when the type is in
/// `ConsumablesWithWorkTimeCoefficients`.
fn consumable_work_time_coeff(bundle: &ModifierBundle, type_name: &str, group: &str) -> f32 {
    let mut coeff = 1.0;
    match group {
        GROUP_SHIP => coeff *= bundle.coef("ConsumablesWorkTime"), // :222
        GROUP_SQUADRON => coeff *= bundle.coef("planeConsumablesWorkTime"), // :231
        _ => {}
    }
    // :225-226 / :234-235
    gated_type_coef(bundle, coeff, type_name, CONSUMABLES_WITH_WORK_TIME_COEFFICIENTS, "WorkTimeCoeff")
}

/// `getConsumableCapacity` (ModifiersApply.py:242-263).
///
/// `coeff = consumableCapacityCoeff`; SHIP `*= shipConsumableCapacityCoeff`,
/// SQUADRON `*= squadronConsumableCapacityCoeff`; then `*= <typeName>CapacityCoeff`
/// when the type is in `ConsumablesWithCapacityCoefficients`.
fn consumable_capacity_coeff(bundle: &ModifierBundle, type_name: &str, group: &str) -> f32 {
    let mut coeff = bundle.coef("consumableCapacityCoeff"); // :243
    match group {
        GROUP_SHIP => coeff *= bundle.coef("shipConsumableCapacityCoeff"), // :246
        GROUP_SQUADRON => coeff *= bundle.coef("squadronConsumableCapacityCoeff"), // :255
        _ => {}
    }
    // :249-250 / :258-259
    gated_type_coef(bundle, coeff, type_name, CONSUMABLES_WITH_CAPACITY_COEFFICIENTS, "CapacityCoeff")
}

/// `getAdditionalConsumablesCount` (ModifiersApply.py:274-280): the additive
/// per-type `<typeName>AdditionalConsumables` bonus (0 when the type is not in
/// `AdditionalConsumablesCount`).
fn additional_consumables_count(bundle: &ModifierBundle, type_name: &str) -> f32 {
    if ADDITIONAL_CONSUMABLES_COUNT.contains(&type_name) {
        // The bonus name is additive (base 0.0); read it via apply onto 0.0.
        bundle.apply(0.0, &format!("{type_name}AdditionalConsumables"))
    } else {
        0.0
    }
}

/// `getAdditionalConsumablesCountForGroup` (ModifiersApply.py:283-290): the additive
/// group-wide bonus, `additionalConsumables` (SHIP) or `planeAdditionalConsumables`
/// (SQUADRON).
fn additional_consumables_for_group(bundle: &ModifierBundle, group: &str) -> f32 {
    match group {
        GROUP_SHIP => bundle.apply(0.0, "additionalConsumables"), // :285
        GROUP_SQUADRON => bundle.apply(0.0, "planeAdditionalConsumables"), // :288
        _ => 0.0,
    }
}

/// Read a numeric field from the category's merged effect-field map.
fn field(fields: &BTreeMap<String, f32>, name: &str) -> Option<f32> {
    fields.get(name).copied()
}

/// Convert a BigWorld ballistic `radius` into kilometers, MODIFIER_SETTINGS `radius`
/// uses `Measures.KILOMETER` with scale `BW_TO_BALLISTIC / KM_TO_M`
/// (`mbf4783af/ModifierSettings.py`). The multiplicative effect modifier commutes with
/// this scale, so the conversion order is irrelevant.
fn km_from_ballistic(radius: f32) -> Km {
    Km::from(radius * (BW_TO_BALLISTIC / KM_TO_M))
}

/// Compute the final, modifier-applied stats for `category` under `modifiers`.
///
/// This is the consumable analog of the TTX weapon factories: it returns FINAL
/// values with the equipped bundle folded in, so callers never need to know which
/// modifier multiplies and which adds. An empty bundle yields the base values
/// unchanged.
pub fn effective_consumable(category: &AbilityCategory, modifiers: &ModifierBundle) -> EffectiveConsumable {
    let type_name = category.consumable_type_raw();
    let group = category.group();
    let fields = category.effect_fields();

    // reloadTime / preparationTime both scale by getConsumableReloadTime
    // (ConsumableUtils.py:115-116).
    let reload_coeff = consumable_reload_coeff(modifiers, type_name, group);
    let reload_time = Seconds::from(category.reload_time() * reload_coeff);
    let preparation_time = Seconds::from(category.preparation_time() * reload_coeff);

    let lifecycle = field(fields, "lifeCycleType").unwrap_or(LIFECYCLE_COUNT_BASED);

    // workTime is multiplied only for COUNT_BASED consumables (ConsumableUtils.py:120);
    // TIME_BASED consumables have no port workTime stat (capacity/minWorkTime govern).
    let work_time = if lifecycle == LIFECYCLE_COUNT_BASED {
        Some(Seconds::from(category.work_time() * consumable_work_time_coeff(modifiers, type_name, group)))
    } else {
        None
    };

    // charges: COUNT_BASED applies numConsumables + additional counts
    // (ConsumableUtils.py:122-128). A base of -1 is an unlimited pool and stays
    // Infinite regardless of modifiers.
    let base_count = category.num_consumables();
    let charges = if base_count < 0 {
        AmmoCount::Infinite
    } else if lifecycle == LIFECYCLE_COUNT_BASED {
        let added =
            additional_consumables_count(modifiers, type_name) + additional_consumables_for_group(modifiers, group);
        // ConsumableUtils.py:128 `max(0, numConsumables + added)`
        let total = (base_count as f32 + added).max(0.0);
        AmmoCount::Finite(total.round() as u32)
    } else {
        AmmoCount::Finite(base_count as u32)
    };

    // maxCapacity for TIME_BASED consumables (ConsumableUtils.py:166-167). Absent for
    // COUNT_BASED.
    let max_capacity = if lifecycle == LIFECYCLE_TIME_BASED {
        field(fields, "maxCapacity")
            .filter(|&c| c >= 0.0)
            .map(|c| c * consumable_capacity_coeff(modifiers, type_name, group))
    } else {
        None
    };

    // regenerationHPSpeed: REGEN_CREW only, `*= regenerationHPSpeed` modifier
    // (ConsumableUtils.py:140). The base is read off the typed accessor.
    let regeneration_hp_speed = if type_name == TYPE_REGEN_CREW {
        category.regeneration_hp_speed().map(|v| v * modifiers.coef("regenerationHPSpeed"))
    } else {
        None
    };

    // detection_radius has no modifier in updateConsumableParams; the base value is
    // final.
    let detection_radius = category.detection_radius();

    // Per-type effect fields (ConsumableUtils.py:132-152, identical in both the
    // COUNT_BASED and TIME_BASED branches). Each reads its base off effect_fields() and
    // applies the per-type modifier via bundle.apply (operator picked by the modifier's
    // own classification); an absent base field yields None.
    let mut smoke_radius = None;
    let mut smoke_lifetime = None;
    let mut fighters_count = None;
    let mut call_fighters_radius = None;
    let mut call_fighters_time_delay = None;
    let mut call_fighters_time_from_heaven = None;
    let mut plane_regeneration_rate = None;

    if type_name == TYPE_SMOKE_GENERATOR {
        // :133 lifeTime *= smokeGeneratorLifeTime; :134 radius *= smokeScreenRadiusCoefficient
        smoke_lifetime = field(fields, "lifeTime").map(|v| Seconds::from(modifiers.apply(v, "smokeGeneratorLifeTime")));
        smoke_radius =
            field(fields, "radius").map(|v| km_from_ballistic(modifiers.apply(v, "smokeScreenRadiusCoefficient")));
    } else if type_name == TYPE_PLANE_SMOKE_GENERATOR {
        // :137 lifeTime *= planeSmokeGeneratorLifeTime (plane smoke has no radius modifier)
        smoke_lifetime =
            field(fields, "lifeTime").map(|v| Seconds::from(modifiers.apply(v, "planeSmokeGeneratorLifeTime")));
    } else if type_name == TYPE_REGENERATE_HEALTH {
        // :144 regenerationRate *= planeRegenerationRate
        plane_regeneration_rate =
            field(fields, "regenerationRate").map(|v| modifiers.apply(v, "planeRegenerationRate"));
    } else if type_name == TYPE_FIGHTER {
        // :147 fightersNum += extraFighterCount (additive, base 0.0)
        fighters_count = field(fields, "fightersNum").map(|v| modifiers.apply(v, "extraFighterCount"));
    } else if CALL_FIGHTERS_CONSUMABLES.contains(&type_name) {
        // :150 radius *= callFightersRadiusCoeff
        call_fighters_radius =
            field(fields, "radius").map(|v| km_from_ballistic(modifiers.apply(v, "callFightersRadiusCoeff")));
        // :151 timeDelayAttack *= callFightersTimeDelayAttack
        call_fighters_time_delay =
            field(fields, "timeDelayAttack").map(|v| Seconds::from(modifiers.apply(v, "callFightersTimeDelayAttack")));
        // :152 timeFromHeaven *= callFightersAppearDelay
        call_fighters_time_from_heaven =
            field(fields, "timeFromHeaven").map(|v| Seconds::from(modifiers.apply(v, "callFightersAppearDelay")));
    }

    EffectiveConsumable {
        reload_time,
        work_time,
        preparation_time,
        charges,
        max_capacity,
        detection_radius,
        regeneration_hp_speed,
        smoke_radius,
        smoke_lifetime,
        fighters_count,
        call_fighters_radius,
        call_fighters_time_delay,
        call_fighters_time_from_heaven,
        plane_regeneration_rate,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::game_params::types::AbilityCategory;
    use crate::game_params::types::CrewSkillModifier;
    use crate::game_params::types::Species;

    /// The version at which the toolkit's `MODIFIER_SETTINGS` table takes effect.
    const VERSION: crate::data::Version = crate::data::Version::base(15, 0, 0);

    /// Build a Damage Control Party (`crashCrew`) category from the real GameParams
    /// values (`PCY001_CrashCrew`, queried from G:\wows_dump\GameParams.json):
    /// consumableType "crashCrew", group "ship", reloadTime 120, preparationTime 0,
    /// numConsumables -1, workTime 15, COUNT_BASED.
    fn crash_crew() -> AbilityCategory {
        let mut fields = BTreeMap::new();
        fields.insert("lifeCycleType".to_string(), LIFECYCLE_COUNT_BASED);
        fields.insert("reloadTime".to_string(), 120.0);
        fields.insert("workTime".to_string(), 15.0);
        AbilityCategory::builder()
            .consumable_type("crashCrew".to_string())
            .group("ship".to_string())
            .icon_id(String::new())
            .num_consumables(-1)
            .preparation_time(0.0)
            .reload_time(120.0)
            .work_time(15.0)
            .effect_fields(fields)
            .build()
    }

    /// A finite-charge ship consumable (`sonar`, COUNT_BASED) for charge tests:
    /// numConsumables 3, reloadTime 90, workTime 100.
    fn finite_sonar() -> AbilityCategory {
        let mut fields = BTreeMap::new();
        fields.insert("lifeCycleType".to_string(), LIFECYCLE_COUNT_BASED);
        AbilityCategory::builder()
            .consumable_type("sonar".to_string())
            .group("ship".to_string())
            .icon_id(String::new())
            .num_consumables(3)
            .preparation_time(0.0)
            .reload_time(90.0)
            .work_time(100.0)
            .effect_fields(fields)
            .build()
    }

    /// A Smoke Generator (`smokeGenerator`, COUNT_BASED) with the real
    /// `PCY007_SmokeGenerator` logic values (jaq-verified on GameParams.json): logic
    /// `radius` 15.0, `lifeTime` 77.0.
    fn smoke_generator() -> AbilityCategory {
        let mut fields = BTreeMap::new();
        fields.insert("lifeCycleType".to_string(), LIFECYCLE_COUNT_BASED);
        fields.insert("radius".to_string(), 15.0);
        fields.insert("lifeTime".to_string(), 77.0);
        AbilityCategory::builder()
            .consumable_type("smokeGenerator".to_string())
            .group("ship".to_string())
            .icon_id(String::new())
            .num_consumables(2)
            .preparation_time(0.0)
            .reload_time(240.0)
            .work_time(20.0)
            .effect_fields(fields)
            .build()
    }

    /// A Call Fighters (`callFighters`, SQUADRON) with the real logic values: `radius`
    /// 116.667, `timeDelayAttack` 5.0, `timeFromHeaven` 3.0.
    fn call_fighters() -> AbilityCategory {
        let mut fields = BTreeMap::new();
        fields.insert("lifeCycleType".to_string(), LIFECYCLE_COUNT_BASED);
        fields.insert("radius".to_string(), 116.667);
        fields.insert("timeDelayAttack".to_string(), 5.0);
        fields.insert("timeFromHeaven".to_string(), 3.0);
        AbilityCategory::builder()
            .consumable_type("callFighters".to_string())
            .group("squadron".to_string())
            .icon_id(String::new())
            .num_consumables(-1)
            .preparation_time(0.0)
            .reload_time(60.0)
            .work_time(60.0)
            .effect_fields(fields)
            .build()
    }

    /// A Fighter (`fighter`, COUNT_BASED) with the real `fightersNum` 1.
    fn fighter() -> AbilityCategory {
        let mut fields = BTreeMap::new();
        fields.insert("lifeCycleType".to_string(), LIFECYCLE_COUNT_BASED);
        fields.insert("fightersNum".to_string(), 1.0);
        AbilityCategory::builder()
            .consumable_type("fighter".to_string())
            .group("ship".to_string())
            .icon_id(String::new())
            .num_consumables(3)
            .preparation_time(0.0)
            .reload_time(90.0)
            .work_time(60.0)
            .effect_fields(fields)
            .build()
    }

    /// A plane Regenerate Health (`regenerateHealth`, SQUADRON) with the real
    /// `regenerationRate` 0.1.
    fn regenerate_health() -> AbilityCategory {
        let mut fields = BTreeMap::new();
        fields.insert("lifeCycleType".to_string(), LIFECYCLE_COUNT_BASED);
        fields.insert("regenerationRate".to_string(), 0.1);
        AbilityCategory::builder()
            .consumable_type("regenerateHealth".to_string())
            .group("squadron".to_string())
            .icon_id(String::new())
            .num_consumables(-1)
            .preparation_time(0.0)
            .reload_time(60.0)
            .work_time(60.0)
            .effect_fields(fields)
            .build()
    }

    fn modifier(name: &str, value: f32) -> CrewSkillModifier {
        CrewSkillModifier::builder()
            .name(name.to_string())
            .aircraft_carrier(value)
            .auxiliary(value)
            .battleship(value)
            .cruiser(value)
            .destroyer(value)
            .submarine(value)
            .excluded_consumables(Vec::new())
            .build()
    }

    /// An empty bundle leaves every base value unchanged.
    #[test]
    fn empty_bundle_yields_base_values() {
        let cat = crash_crew();
        let eff = effective_consumable(&cat, &ModifierBundle::empty(Species::Battleship));
        assert_eq!(eff.reload_time, Seconds::from(120.0));
        assert_eq!(eff.preparation_time, Seconds::from(0.0));
        assert_eq!(eff.work_time, Some(Seconds::from(15.0)));
        assert_eq!(eff.charges, AmmoCount::Infinite);
        assert_eq!(eff.max_capacity, None);
    }

    /// An equipped `ConsumableReloadTime` coefficient (real modifier name, base 1.0,
    /// multiplicative) scales reload by its value: 120 * 0.9 = 108.
    #[test]
    fn reload_modifier_scales_reload_time() {
        let cat = crash_crew();
        let mods = [modifier("ConsumableReloadTime", 0.9)];
        let bundle =
            ModifierBundle::from_modifiers(&mods, Species::Battleship, VERSION).expect("test modifiers are all known");
        let eff = effective_consumable(&cat, &bundle);
        assert!((eff.reload_time.value() - 108.0).abs() < 1e-3, "got {}", eff.reload_time.value());
    }

    /// The `allConsumableReloadTime` and `ConsumableReloadTime` coefficients compound:
    /// 120 * 0.9 * 0.95 = 102.6.
    #[test]
    fn reload_coefficients_compound() {
        let cat = crash_crew();
        let mods = [modifier("ConsumableReloadTime", 0.9), modifier("allConsumableReloadTime", 0.95)];
        let bundle =
            ModifierBundle::from_modifiers(&mods, Species::Battleship, VERSION).expect("test modifiers are all known");
        let eff = effective_consumable(&cat, &bundle);
        assert!((eff.reload_time.value() - 102.6).abs() < 1e-3, "got {}", eff.reload_time.value());
    }

    /// A `numConsumables = -1` base stays Infinite even with an additional-count
    /// modifier equipped (modifiers never make an unlimited pool finite).
    #[test]
    fn infinite_charges_stay_infinite() {
        let cat = crash_crew();
        let mods = [modifier("crashCrewAdditionalConsumables", 1.0)];
        let bundle =
            ModifierBundle::from_modifiers(&mods, Species::Battleship, VERSION).expect("test modifiers are all known");
        let eff = effective_consumable(&cat, &bundle);
        assert_eq!(eff.charges, AmmoCount::Infinite);
    }

    /// A finite base count gains the additive per-type and group bonuses: 3 + 1
    /// (sonar has no per-type AdditionalConsumables, so only the group-wide
    /// `additionalConsumables` applies) = 4.
    #[test]
    fn finite_charges_add_group_bonus() {
        let cat = finite_sonar();
        let mods = [modifier("additionalConsumables", 1.0)];
        let bundle =
            ModifierBundle::from_modifiers(&mods, Species::Battleship, VERSION).expect("test modifiers are all known");
        let eff = effective_consumable(&cat, &bundle);
        assert_eq!(eff.charges, AmmoCount::Finite(4));
    }

    /// A finite base count with no modifiers is unchanged.
    #[test]
    fn finite_charges_base_unchanged() {
        let cat = finite_sonar();
        let eff = effective_consumable(&cat, &ModifierBundle::empty(Species::Cruiser));
        assert_eq!(eff.charges, AmmoCount::Finite(3));
    }

    /// The per-type `sonarWorkTimeCoeff` gate fires for `sonar`: workTime 100 * 0.8
    /// (ConsumablesWorkTime is absent -> 1.0, sonarWorkTimeCoeff 0.8) ... here only
    /// the per-type coeff is equipped, so 100 * 0.8 = 80.
    #[test]
    fn per_type_work_time_coeff_applies() {
        let cat = finite_sonar();
        let mods = [modifier("sonarWorkTimeCoeff", 0.8)];
        let bundle =
            ModifierBundle::from_modifiers(&mods, Species::Battleship, VERSION).expect("test modifiers are all known");
        let eff = effective_consumable(&cat, &bundle);
        assert_eq!(eff.work_time, Some(Seconds::from(80.0)));
    }

    /// Smoke generator base: radius 15 -> 0.45 km (15 * 30/1000), lifeTime 77 -> 77 s,
    /// empty bundle leaves both at the converted base. Other per-type fields are None.
    #[test]
    fn smoke_effects_base() {
        let cat = smoke_generator();
        let eff = effective_consumable(&cat, &ModifierBundle::empty(Species::Cruiser));
        assert_eq!(eff.smoke_radius, Some(Km::from(0.45)));
        assert_eq!(eff.smoke_lifetime, Some(Seconds::from(77.0)));
        assert_eq!(eff.call_fighters_radius, None);
        assert_eq!(eff.fighters_count, None);
        assert_eq!(eff.plane_regeneration_rate, None);
    }

    /// `smokeScreenRadiusCoefficient` 1.2 (real modifier, base 1.0, multiplicative)
    /// scales radius: 15 * 1.2 * 30/1000 = 0.54 km.
    #[test]
    fn smoke_radius_modifier_applies() {
        let cat = smoke_generator();
        let mods = [modifier("smokeScreenRadiusCoefficient", 1.2)];
        let bundle =
            ModifierBundle::from_modifiers(&mods, Species::Cruiser, VERSION).expect("test modifiers are all known");
        let eff = effective_consumable(&cat, &bundle);
        assert!((eff.smoke_radius.unwrap().value() - 0.54).abs() < 1e-4, "got {}", eff.smoke_radius.unwrap().value());
    }

    /// Call fighters base: radius 116.667 -> 3.5 km (116.667 * 30/1000), timeDelayAttack
    /// 5 s, timeFromHeaven 3 s, empty bundle. Smoke/fighter/regen fields are None.
    #[test]
    fn call_fighters_effects_base() {
        let cat = call_fighters();
        let eff = effective_consumable(&cat, &ModifierBundle::empty(Species::AirCarrier));
        assert!(
            (eff.call_fighters_radius.unwrap().value() - 3.5).abs() < 1e-3,
            "got {}",
            eff.call_fighters_radius.unwrap().value()
        );
        assert_eq!(eff.call_fighters_time_delay, Some(Seconds::from(5.0)));
        assert_eq!(eff.call_fighters_time_from_heaven, Some(Seconds::from(3.0)));
        assert_eq!(eff.smoke_radius, None);
        assert_eq!(eff.fighters_count, None);
    }

    /// `callFightersTimeDelayAttack` 0.8 (real modifier, base 1.0, multiplicative)
    /// scales timeDelayAttack: 5 * 0.8 = 4 s.
    #[test]
    fn call_fighters_time_modifier_applies() {
        let cat = call_fighters();
        let mods = [modifier("callFightersTimeDelayAttack", 0.8)];
        let bundle =
            ModifierBundle::from_modifiers(&mods, Species::AirCarrier, VERSION).expect("test modifiers are all known");
        let eff = effective_consumable(&cat, &bundle);
        assert_eq!(eff.call_fighters_time_delay, Some(Seconds::from(4.0)));
    }

    /// `extraFighterCount` 1 (real modifier, base 0.0, ADDITIVE) adds to fightersNum:
    /// 1 + 1 = 2.
    #[test]
    fn fighter_count_additive_modifier() {
        let cat = fighter();
        let mods = [modifier("extraFighterCount", 1.0)];
        let bundle =
            ModifierBundle::from_modifiers(&mods, Species::Cruiser, VERSION).expect("test modifiers are all known");
        let eff = effective_consumable(&cat, &bundle);
        assert_eq!(eff.fighters_count, Some(2.0));
        assert_eq!(eff.smoke_radius, None);
    }

    /// Fighter base with no modifiers leaves fightersNum at 1.
    #[test]
    fn fighter_count_base() {
        let cat = fighter();
        let eff = effective_consumable(&cat, &ModifierBundle::empty(Species::Cruiser));
        assert_eq!(eff.fighters_count, Some(1.0));
    }

    /// `planeRegenerationRate` 1.5 (real modifier, base 1.0, multiplicative) scales
    /// regenerationRate: 0.1 * 1.5 = 0.15.
    #[test]
    fn plane_regen_rate_modifier_applies() {
        let cat = regenerate_health();
        let mods = [modifier("planeRegenerationRate", 1.5)];
        let bundle =
            ModifierBundle::from_modifiers(&mods, Species::AirCarrier, VERSION).expect("test modifiers are all known");
        let eff = effective_consumable(&cat, &bundle);
        assert!(
            (eff.plane_regeneration_rate.unwrap() - 0.15).abs() < 1e-6,
            "got {}",
            eff.plane_regeneration_rate.unwrap()
        );
    }

    /// A non-matching consumable (`crashCrew`) has all the new per-type effect fields
    /// None (no base fields, no matching type gate).
    #[test]
    fn non_matching_consumable_has_no_effect_fields() {
        let cat = crash_crew();
        let eff = effective_consumable(&cat, &ModifierBundle::empty(Species::Battleship));
        assert_eq!(eff.smoke_radius, None);
        assert_eq!(eff.smoke_lifetime, None);
        assert_eq!(eff.fighters_count, None);
        assert_eq!(eff.call_fighters_radius, None);
        assert_eq!(eff.call_fighters_time_delay, None);
        assert_eq!(eff.call_fighters_time_from_heaven, None);
        assert_eq!(eff.plane_regeneration_rate, None);
    }
}