wh40kdc 0.4.18

Warhammer 40K dataset for the 40kdc-data schema layer: generated types, an embedded dataset behind a linked typed API, plus ListForge + NewRecruit roster importers and exporters.
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
//! Plain-English translation of Ability-DSL `effect` trees — the Rust mirror
//! of `tools/src/translate/effect.ts` (the "ability.print()" of the dataset).
//! Output is an *approximation* generated purely from the structured data (no
//! external rules text), **ASCII-only**, and must be byte-for-byte identical
//! to the TS oracle; the `conformance/effect-translation` corpus pins both
//! ports. Any phrasing change here is a semantic corpus change (bump
//! `conformance/SPEC_VERSION`).
//!
//! Container nodes (`sequence`, `conditional`, `choice`, `dice-gated`,
//! `dice-pool-allocation`) render block-style with two-space indentation and
//! an ASCII `-> ` arrow; leaves render as single clauses. Unrecognized
//! modifier shapes degrade to a deterministic form rather than failing.

use serde_json::{Map, Value};

use super::{dekebab, describe_condition};
use crate::generated::{
    Ability, DiceGatedEffect, DiceGatedEffectComparison, DiceGatedEffectThreshold,
    DicePoolAllocationEffect, EffectNode, Scope, SingleEffect, SingleEffectType,
};

/// JS-template stringification (`String(v)` semantics; numbers print without
/// a trailing `.0`, `null`/missing prints `?`, arrays join with `, `).
pub(super) fn jval(v: &Value) -> String {
    match v {
        Value::Null => "?".to_string(),
        Value::String(s) => s.clone(),
        Value::Number(n) => {
            if let Some(i) = n.as_i64() {
                i.to_string()
            } else if let Some(u) = n.as_u64() {
                u.to_string()
            } else {
                let f = n.as_f64().unwrap_or(0.0);
                if f.fract() == 0.0 && f.is_finite() && f.abs() < 9e15 {
                    format!("{}", f as i64)
                } else {
                    format!("{f}")
                }
            }
        }
        Value::Bool(b) => b.to_string(),
        Value::Array(a) => a.iter().map(jval).collect::<Vec<_>>().join(", "),
        Value::Object(_) => "[object Object]".to_string(),
    }
}

/// `jstr(m.key)` — `?` when the key is absent or null (TS `jstr(undefined)`).
fn jv(m: &Map<String, Value>, k: &str) -> String {
    m.get(k).map(jval).unwrap_or_else(|| "?".to_string())
}

/// TS `m.key != null` (present and not null).
fn notnull(m: &Map<String, Value>, k: &str) -> bool {
    matches!(m.get(k), Some(v) if !v.is_null())
}

/// TS truthiness for `m.key ? ... : ...` sites.
fn truthy(m: &Map<String, Value>, k: &str) -> bool {
    match m.get(k) {
        None | Some(Value::Null) | Some(Value::Bool(false)) => false,
        Some(Value::Number(n)) => n.as_f64() != Some(0.0),
        Some(Value::String(s)) => !s.is_empty(),
        Some(_) => true,
    }
}

/// TS `m.a ?? m.b` over the modifier map (first present-and-not-null value).
fn first<'a>(m: &'a Map<String, Value>, keys: &[&str]) -> Option<&'a Value> {
    keys.iter().filter_map(|k| m.get(*k)).find(|v| !v.is_null())
}

/// `+3` / `-1` — sign from `operation` (`add`/`improve` positive), value via
/// JS stringification (`?` when missing).
fn signed(m: &Map<String, Value>) -> String {
    let op = m.get("operation").and_then(Value::as_str);
    let sign = if op == Some("add") || op == Some("improve") {
        "+"
    } else {
        "-"
    };
    format!("{sign}{}", jv(m, "value"))
}

fn format_comparison(
    comp: DiceGatedEffectComparison,
    threshold: &DiceGatedEffectThreshold,
) -> String {
    let th = match threshold {
        DiceGatedEffectThreshold::Integer(i) => i.to_string(),
        DiceGatedEffectThreshold::String(s) => s.to_string(),
    };
    match comp {
        DiceGatedEffectComparison::Gte => format!("{th}+"),
        DiceGatedEffectComparison::Lte => format!("{th} or less"),
        DiceGatedEffectComparison::Gt => format!("greater than {th}"),
        DiceGatedEffectComparison::Lt => format!("less than {th}"),
        DiceGatedEffectComparison::Eq => format!("exactly {th}"),
    }
}

fn dice_gated_inline(d: &DiceGatedEffect) -> String {
    let comp = format_comparison(d.comparison, &d.threshold);
    let success = d
        .on_success
        .as_deref()
        .map(describe_effect_inline)
        .unwrap_or_else(|| "nothing".to_string());
    let fail = d
        .on_fail
        .as_deref()
        .map(|f| format!(", otherwise {}", describe_effect_inline(f)))
        .unwrap_or_default();
    format!("roll {}: on {comp}, {success}{fail}", d.dice)
}

fn dice_pool_options_inline(d: &DicePoolAllocationEffect) -> String {
    d.options
        .iter()
        .map(|o| {
            format!(
                "{} ({}+): {}",
                o.name,
                o.requirement.min_value,
                describe_effect_inline(&o.effect)
            )
        })
        .collect::<Vec<_>>()
        .join(" / ")
}

/// Single-clause translation for leaf effects (and inline container forms).
/// Mirrors `describeEffectInline` in `tools/src/translate/effect.ts`.
pub fn describe_effect_inline(e: &EffectNode) -> String {
    match e {
        EffectNode::SingleEffect(s) => describe_single(s),
        EffectNode::ConditionalEffect(c) => format!(
            "if {}: {}",
            describe_condition(&c.condition),
            describe_effect_inline(&c.effect)
        ),
        EffectNode::SequenceEffect(s) => s
            .steps
            .iter()
            .map(describe_effect_inline)
            .collect::<Vec<_>>()
            .join("; "),
        EffectNode::ChoiceEffect(c) => {
            let label = c
                .choice_label
                .as_deref()
                .map(|l| format!(" ({l})"))
                .unwrap_or_default();
            format!(
                "choose one{label}: {}",
                c.options
                    .iter()
                    .map(describe_effect_inline)
                    .collect::<Vec<_>>()
                    .join(" / ")
            )
        }
        EffectNode::DiceGatedEffect(d) => dice_gated_inline(d),
        EffectNode::DicePoolAllocationEffect(d) => {
            format!(
                "roll {}{}: {}",
                d.pool.count,
                d.pool.die,
                dice_pool_options_inline(d)
            )
        }
    }
}

fn describe_single(e: &SingleEffect) -> String {
    let m = &e.modifier;
    let target = dekebab(&e.target.to_string());
    use SingleEffectType as T;

    match e.type_ {
        T::StatModifier => {
            let scope = if truthy(m, "attack_type") {
                format!(" ({})", jv(m, "attack_type"))
            } else {
                String::new()
            };
            if !notnull(m, "stat") {
                return format!("modify stats for {target}");
            }
            if m.get("operation").and_then(Value::as_str) == Some("set") {
                format!(
                    "set {} to {}{scope} for {target}",
                    jv(m, "stat"),
                    jv(m, "value")
                )
            } else {
                format!("{} {}{scope} for {target}", signed(m), jv(m, "stat"))
            }
        }
        T::RollModifier => {
            let ctx = if truthy(m, "context") {
                format!(" ({})", jv(m, "context"))
            } else {
                String::new()
            };
            if !notnull(m, "value") {
                format!(
                    "{} {} rolls{ctx} for {target}",
                    dekebab(&jv(m, "operation")),
                    jv(m, "roll")
                )
            } else {
                format!("{} to {} rolls{ctx} for {target}", signed(m), jv(m, "roll"))
            }
        }
        T::ReRoll => {
            let subset = if truthy(m, "subset") {
                format!(" ({})", dekebab(&jv(m, "subset")))
            } else {
                String::new()
            };
            let atk = if truthy(m, "attack_type") {
                format!(" ({})", jv(m, "attack_type"))
            } else {
                String::new()
            };
            format!("re-roll {} rolls{subset}{atk} for {target}", jv(m, "roll"))
        }
        T::MortalWounds => {
            let amount = first(m, &["count", "amount"]).map(jval).unwrap_or_else(|| {
                if truthy(m, "amount_table") {
                    "variable".to_string()
                } else {
                    "?".to_string()
                }
            });
            let within = first(m, &["range", "range_inches"])
                .map(|r| format!(" (within {}\")", jval(r)))
                .unwrap_or_default();
            format!("deal {amount} mortal wounds to {target}{within}")
        }
        T::FeelNoPain => format!("{target} gains Feel No Pain {}+", jv(m, "threshold")),
        T::Ward => {
            let th = first(m, &["threshold", "value"])
                .map(jval)
                .unwrap_or_else(|| "?".to_string());
            format!("{target} gains Ward {th}+")
        }
        T::InvulnerableSave => format!("{target} gains a {}+ invulnerable save", jv(m, "value")),
        T::KeywordGrant => {
            let kw = match m.get("keywords") {
                Some(Value::Array(a)) => a.iter().map(jval).collect::<Vec<_>>().join(", "),
                _ => first(m, &["keyword"])
                    .map(jval)
                    .unwrap_or_else(|| "keywords".to_string()),
            };
            if notnull(m, "weapon_name") {
                format!("{target}'s {} gains {kw}", jv(m, "weapon_name"))
            } else if notnull(m, "weapon_type") {
                format!("{target}'s {} weapons gain {kw}", jv(m, "weapon_type"))
            } else {
                format!("{target}'s weapons gain {kw}")
            }
        }
        T::AbilityGrant => {
            let grant = first(m, &["grant_type", "ability_id"])
                .map(|g| dekebab(&jval(g)))
                .unwrap_or_else(|| "an ability".to_string());
            let cap = if notnull(m, "capacity") {
                format!(" ({})", jv(m, "capacity"))
            } else {
                String::new()
            };
            format!("{target} gains {grant}{cap}")
        }
        T::MovementModifier => {
            let kind = first(m, &["move_type", "type"])
                .map(|k| dekebab(&jval(k)))
                .unwrap_or_else(|| "a movement effect".to_string());
            let inches = first(m, &["distance", "value"])
                .map(|d| format!(" {}\"", jval(d)))
                .unwrap_or_default();
            format!("{target} gains {kind}{inches}")
        }
        T::DamageReduction => {
            let amount = first(m, &["amount", "value"])
                .map(jval)
                .unwrap_or_else(|| "?".to_string());
            format!("reduce incoming damage to {target} by {amount}")
        }
        T::Resurrection => {
            let count = first(m, &["count"])
                .map(jval)
                .unwrap_or_else(|| "1".to_string());
            let wounds = first(m, &["wounds_remaining"])
                .map(jval)
                .unwrap_or_else(|| "full".to_string());
            format!("return {count} model(s) to {target} with {wounds} wounds")
        }
        T::ModelDestruction => {
            format!(
                "destroy {} non-leader model(s) from {target}",
                jv(m, "count")
            )
        }
        T::CpGain => format!("gain {} CP", jv(m, "amount")),
        T::CpRefund => format!("refund {} CP", jv(m, "amount")),
        T::ResourceGain => format!("gain {} to {}", jv(m, "amount"), jv(m, "pool_id")),
        T::ResourceSpend => format!("spend {} from {}", jv(m, "amount"), jv(m, "pool_id")),
        T::LeadershipModifier => {
            if notnull(m, "test") && !notnull(m, "operation") {
                format!("force a {} test on {target}", dekebab(&jv(m, "test")))
            } else if notnull(m, "test") {
                format!(
                    "{} {} tests for {target}",
                    dekebab(&jv(m, "operation")),
                    dekebab(&jv(m, "test"))
                )
            } else if notnull(m, "operation") {
                format!("{} Leadership for {target}", signed(m))
            } else {
                format!("modify Leadership for {target}")
            }
        }
        T::FightFirst => format!("{target} fights first"),
        T::FightLast => format!("{target} fights last"),
        T::FightOnDeath => format!("{target} fights on death"),
        T::ShootOnDeath => format!("{target} shoots on death"),
        T::DeepStrike => format!("{target} can deep strike"),
        T::FallbackAndAct => format!("{target} can fall back and act"),
        T::AttackRestriction => {
            let what = first(m, &["restriction", "restriction_type"])
                .map(|w| dekebab(&jval(w)))
                .unwrap_or_else(|| "attack restriction".to_string());
            let range = if notnull(m, "range") {
                format!(" (within {}\")", jv(m, "range"))
            } else {
                String::new()
            };
            let max = if notnull(m, "max_models") {
                format!(" (max {} models)", jv(m, "max_models"))
            } else {
                String::new()
            };
            format!("{target}: {what}{range}{max}")
        }
        T::ObjectiveControlModifier => {
            if notnull(m, "operation") {
                format!("{} OC for {target}", signed(m))
            } else {
                format!("modify OC of {target} by {}", jv(m, "value"))
            }
        }
        T::BsModifier => format!("{} BS for {target}", signed(m)),
        T::ChargeRollModifier => format!("{} to charge rolls for {target}", signed(m)),
        T::EngagementPassthrough => format!("{target} can move through engagement range"),
        T::TerrainAreaTag => format!("tag the terrain area as {}", dekebab(&jv(m, "tag"))),
        T::ObjectiveTag => format!("tag the objective as {}", dekebab(&jv(m, "tag"))),
        T::UnitTag => format!("tag {target} as {}", dekebab(&jv(m, "tag"))),
    }
}

/// Block translation of an effect tree. Containers expand over multiple lines
/// with two-space indentation; leaves delegate to [`describe_effect_inline`].
/// Mirrors `describeEffect` in `tools/src/translate/effect.ts`.
pub fn describe_effect(e: &EffectNode) -> String {
    describe_effect_at(e, 0)
}

fn describe_effect_at(e: &EffectNode, depth: usize) -> String {
    let indent = "  ".repeat(depth);
    let arrow = if depth > 0 { "-> " } else { "" };

    match e {
        EffectNode::ConditionalEffect(c) => format!(
            "{indent}If {}:\n{}",
            describe_condition(&c.condition),
            describe_effect_at(&c.effect, depth + 1)
        ),
        EffectNode::SequenceEffect(s) => s
            .steps
            .iter()
            .map(|step| describe_effect_at(step, depth))
            .collect::<Vec<_>>()
            .join("\n"),
        EffectNode::ChoiceEffect(c) => {
            let label = c
                .choice_label
                .as_deref()
                .map(|l| format!(" ({l})"))
                .unwrap_or_default();
            let options = c
                .options
                .iter()
                .enumerate()
                .map(|(i, o)| format!("{indent}  {}. {}", i + 1, describe_effect_inline(o)))
                .collect::<Vec<_>>()
                .join("\n");
            format!("{indent}{arrow}Choose one{label}:\n{options}")
        }
        EffectNode::DiceGatedEffect(d) => {
            let comp = format_comparison(d.comparison, &d.threshold);
            let success = d
                .on_success
                .as_deref()
                .map(describe_effect_inline)
                .unwrap_or_else(|| "nothing".to_string());
            let fail = d
                .on_fail
                .as_deref()
                .map(|f| format!(", otherwise {}", describe_effect_inline(f)))
                .unwrap_or_default();
            format!("{indent}{arrow}Roll {}: on {comp}, {success}{fail}", d.dice)
        }
        EffectNode::DicePoolAllocationEffect(d) => {
            let mut lines = vec![format!(
                "{indent}{arrow}Roll {}{} (max {} activations):",
                d.pool.count, d.pool.die, d.max_activations
            )];
            for opt in &d.options {
                lines.push(format!(
                    "{indent}  - {}: need {} of {}+ -> {}",
                    opt.name,
                    opt.requirement.type_,
                    opt.requirement.min_value,
                    describe_effect_inline(&opt.effect)
                ));
            }
            lines.join("\n")
        }
        EffectNode::SingleEffect(_) => {
            format!("{indent}{arrow}{}", describe_effect_inline(e))
        }
    }
}

/// `Scope: aura (6"). Duration: phase.` Mirrors `describeScope`.
pub fn describe_scope(s: &Scope) -> String {
    let range = dekebab(&s.range.to_string());
    let inches = s
        .range_inches
        .map(|r| {
            let v = if r.fract() == 0.0 && r.is_finite() && r.abs() < 9e15 {
                format!("{}", r as i64)
            } else {
                format!("{r}")
            };
            format!(" ({v}\")")
        })
        .unwrap_or_default();
    let duration = dekebab(&s.duration.to_string());
    format!("Scope: {range}{inches}. Duration: {duration}.")
}

/// Effect text plus an optional trailing scope line — the composition rule
/// shared with TS `describeAbility`.
pub fn describe_effect_with_scope(e: &EffectNode, scope: Option<&Scope>) -> String {
    let effect = describe_effect(e);
    match scope {
        Some(s) => {
            let scope_line = describe_scope(s);
            if effect.is_empty() {
                scope_line
            } else {
                format!("{effect}\n{scope_line}")
            }
        }
        None => effect,
    }
}

/// Full generated text for an ability: the effect tree plus a trailing scope
/// line. This is the `ability.print()` consumers render when the dataset
/// carries no rules prose. Mirrors `describeAbility`.
pub fn describe_ability(a: &Ability) -> String {
    describe_effect_with_scope(&a.effect, Some(&a.scope))
}