delvewright-dsl 0.39.0

Staged JSON DSL types and schemas for Delvewright adventure-map campaigns — the format the delvec compiler reads.
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
//! DSL v0.4 (spec-0008 + addendum, spec-0009) negative tests for the v0.4-only
//! semantic checks (`dsl::validate`, run from each object's module): mannequin skins, wave-mob
//! `effects`, `set-block` block ids, environment triggers, and the
//! despawned-npc `talk-to` guard. Each test bumps only the stage(s) that carry
//! the v0.4 construct under test to `dsl_version 0.4.0` (avoiding an incidental
//! diagnostic on an unrelated stage) and asserts the
//! expected code fires.
//!
//! Built on the hello-world fixture campaign (`crates/dsl/fixtures/valid/
//! hello-world`), matching the `v03.rs`/`v05.rs` pattern (literal JSON +
//! `check_campaign`, `.any(|d| d.code == "DWxxxx")`).

mod common;

use delvewright_dsl::{RawCampaign, check_campaign};
use std::sync::LazyLock;

fn campaign_with(npcs: &str, quests: &str, dialogue: &str) -> RawCampaign {
    RawCampaign {
        world: common::read_valid("world.json"),
        npcs: npcs.to_string(),
        classes: common::read_valid("classes.json"),
        quest_plan: common::read_valid("quest-plan.json"),
        quests: quests.to_string(),
        dialogue: dialogue.to_string(),
        world_edits: None,
        geometry_brief: None,
        layout_graph: None,
        site_plan: None,
        detail_plan: None,
        design: None,
    }
}

fn valid_npcs_v04() -> String {
    common::read_valid("npcs.json").replacen("\"0.2.0\"", "\"0.4.0\"", 1)
}

fn valid_dialogue_v04() -> String {
    common::read_valid("dialogue.json").replacen("\"0.2.0\"", "\"0.4.0\"", 1)
}

/// The hello-world quests document, at v0.4.0, with no v0.4 constructs — used as
/// the base that individual tests inject one bad construct into.
static QUESTS_BASE: LazyLock<String> = LazyLock::new(|| {
    common::at_dsl_version(
        r#"{
  "dsl_version": "%dsl_version%",
  "campaign_id": "hello-world",
  "stage": "quests",
  "content": {
    "quests": [
      {
        "id": "quest/open-the-door",
        "trigger": { "type": "campaign-start" },
        "objectives": [
          { "type": "talk-to", "id": "obj/talk", "npc": "npc/keeper" },
          { "type": "reach-anchor", "id": "obj/exit", "anchor": "anchor/exit", "radius": 2, "after": ["obj/talk"] }
        ],
        "on_objective_complete": {
          "obj/talk": [ { "type": "open-gate", "anchor": "anchor/door" } ]
        },
        "on_complete": [ { "type": "campaign-complete" } ]
      }
    ]
  }
}"#,
    )
});

#[test]
fn v04_base_campaign_validates_clean() {
    let diags = check_campaign(&campaign_with(
        &valid_npcs_v04(),
        QUESTS_BASE.as_str(),
        &valid_dialogue_v04(),
    ));
    assert!(
        diags.is_empty(),
        "expected zero diagnostics for the v0.4 base campaign, got: {diags:#?}"
    );
}

// ---------------------------------------------------------------------------
// DW0190 — mannequin skin malformed / duplicated
// ---------------------------------------------------------------------------

#[test]
fn malformed_skin_texture_id_is_dw0190() {
    let npcs = valid_npcs_v04().replacen(
        "\"base_entity\": \"minecraft:villager\",",
        "\"base_entity\": \"minecraft:villager\", \"skin\": { \"texture_id\": \"Not Kebab!\", \
         \"model\": \"wide\" },",
        1,
    );
    let diags = check_campaign(&campaign_with(
        &npcs,
        QUESTS_BASE.as_str(),
        &valid_dialogue_v04(),
    ));
    assert!(
        diags.iter().any(|d| d.code == "DW0190"),
        "malformed texture_id must be DW0190: {diags:#?}"
    );
}

/// spec-0097 §4.3: a `texture_id` names a file, and two bodies may wear it —
/// a twin that hides her hat stands beside her sister in the same face.
#[test]
fn two_mannequins_may_wear_one_texture() {
    let npcs = common::patch_doc(&valid_npcs_v04(), |d| {
        let npcs = d["content"]["npcs"].as_array_mut().unwrap();
        npcs[0]["skin"] = serde_json::json!({ "texture_id": "keeper", "model": "wide" });
        let mut twin = npcs[0].clone();
        twin["id"] = serde_json::json!("npc/keeper-twin");
        twin["skin"] = serde_json::json!({
            "texture_id": "keeper", "model": "wide", "hidden_layers": ["hat"]
        });
        npcs.push(twin);
    });
    let diags = check_campaign(&campaign_with(
        &npcs,
        QUESTS_BASE.as_str(),
        &valid_dialogue_v04(),
    ));
    assert!(
        !diags.iter().any(|d| d.code == "DW0190"),
        "one file, two bodies is not a malformed id: {diags:#?}"
    );
}

// ---------------------------------------------------------------------------
// DW0980 — a mannequin layer hidden twice (spec-0097 §5)
// ---------------------------------------------------------------------------

fn npcs_hiding(layers: &str) -> String {
    valid_npcs_v04().replacen(
        "\"base_entity\": \"minecraft:villager\",",
        &format!(
            "\"base_entity\": \"minecraft:villager\", \"skin\": {{ \"texture_id\": \"keeper\", \
             \"model\": \"wide\", \"hidden_layers\": {layers} }},"
        ),
        1,
    )
}

#[test]
fn a_layer_hidden_twice_is_dw0980_and_each_layer_once_is_clean() {
    let twice = check_campaign(&campaign_with(
        &npcs_hiding(r#"["hat", "jacket", "hat"]"#),
        QUESTS_BASE.as_str(),
        &valid_dialogue_v04(),
    ));
    let hit: Vec<_> = twice.iter().filter(|d| d.code == "DW0980").collect();
    assert_eq!(hit.len(), 1, "one repeat, one refusal: {twice:#?}");
    assert!(
        hit[0].path.ends_with("/skin/hidden_layers/2"),
        "{:?}",
        hit[0].path
    );
    let once = check_campaign(&campaign_with(
        &npcs_hiding(r#"["hat", "jacket", "cape"]"#),
        QUESTS_BASE.as_str(),
        &valid_dialogue_v04(),
    ));
    assert!(
        !once.iter().any(|d| d.code == "DW0980"),
        "distinct layers are clean: {once:#?}"
    );
    let unknown = check_campaign(&campaign_with(
        &npcs_hiding(r#"["helmet"]"#),
        QUESTS_BASE.as_str(),
        &valid_dialogue_v04(),
    ));
    assert!(
        unknown.iter().any(|d| d.code == "DW0100"),
        "a layer the client does not name is a schema error: {unknown:#?}"
    );
}

// ---------------------------------------------------------------------------
// DW0191 — talk-to with only flag-gated completing options (deadlock risk)
// ---------------------------------------------------------------------------

#[test]
fn all_completing_options_flag_gated_is_dw0191() {
    // Gate the only completing option on a flag nothing ever sets: every
    // completing option for `obj/talk` becomes requires_flags-gated.
    let dialogue = common::patch_doc(&valid_dialogue_v04(), |d| {
        let opts = d["content"]["dialogues"][0]["nodes"][0]["options"]
            .as_array_mut()
            .expect("the greeting node has options");
        let completing = opts
            .iter_mut()
            .find(|o| o["effects"][0]["type"] == "complete-objective")
            .expect("the greeting node still holds the only completing option");
        completing["requires_flags"] = serde_json::json!(["flag/never-set"]);
    });
    let diags = check_campaign(&campaign_with(
        &valid_npcs_v04(),
        QUESTS_BASE.as_str(),
        &dialogue,
    ));
    assert!(
        diags.iter().any(|d| d.code == "DW0191"),
        "an all-flag-gated talk-to must be DW0191: {diags:#?}"
    );
}

// ---------------------------------------------------------------------------
// DW0192 — wave-mob `effects[].effect` not a known status-effect id
// ---------------------------------------------------------------------------

static QUESTS_BAD_WAVE_EFFECT: LazyLock<String> = LazyLock::new(|| {
    common::at_dsl_version(
        r#"{
  "dsl_version": "%dsl_version%",
  "campaign_id": "hello-world",
  "stage": "quests",
  "content": {
    "quests": [
      {
        "id": "quest/open-the-door",
        "trigger": { "type": "campaign-start" },
        "objectives": [
          { "type": "talk-to", "id": "obj/talk", "npc": "npc/keeper" },
          { "type": "reach-anchor", "id": "obj/exit", "anchor": "anchor/exit", "radius": 2, "after": ["obj/talk"] }
        ],
        "on_objective_complete": {
          "obj/talk": [ { "type": "open-gate", "anchor": "anchor/door" } ]
        },
        "on_complete": [ { "type": "campaign-complete" } ]
      }
    ],
    "waves": [
      {
        "id": "wave/ambush",
        "anchor": "anchor/keeper-stand",
        "mobs": [
          { "entity": "minecraft:zombie", "count": 1, "effects": [ { "effect": "minecraft:not_a_real_effect", "amplifier": 0 } ] }
        ]
      }
    ]
  }
}"#,
    )
});

#[test]
fn unknown_wave_mob_effect_is_dw0192() {
    let diags = check_campaign(&campaign_with(
        &valid_npcs_v04(),
        QUESTS_BAD_WAVE_EFFECT.as_str(),
        &valid_dialogue_v04(),
    ));
    assert!(
        diags.iter().any(|d| d.code == "DW0192"),
        "unknown wave-mob effect id must be DW0192: {diags:#?}"
    );
}

// ---------------------------------------------------------------------------
// DW0193 — `set-block` / `interact.prop` block id not a known 1.21.11 block
// ---------------------------------------------------------------------------

#[test]
fn unknown_set_block_id_is_dw0193() {
    let quests = QUESTS_BASE.replacen(
        "{ \"type\": \"open-gate\", \"anchor\": \"anchor/door\" }",
        "{ \"type\": \"open-gate\", \"anchor\": \"anchor/door\" }, \
         { \"type\": \"set-block\", \"anchor\": \"anchor/exit\", \"block\": \"minecraft:not_a_real_block\" }",
        1,
    );
    let diags = check_campaign(&campaign_with(
        &valid_npcs_v04(),
        &quests,
        &valid_dialogue_v04(),
    ));
    assert!(
        diags.iter().any(|d| d.code == "DW0193"),
        "unknown set-block block id must be DW0193: {diags:#?}"
    );
}

// ---------------------------------------------------------------------------
// DW0194 — environment-trigger id malformed/duplicated, or `approach` range 0
// ---------------------------------------------------------------------------

static QUESTS_BAD_TRIGGER: LazyLock<String> = LazyLock::new(|| {
    common::at_dsl_version(
        r#"{
  "dsl_version": "%dsl_version%",
  "campaign_id": "hello-world",
  "stage": "quests",
  "content": {
    "quests": [
      {
        "id": "quest/open-the-door",
        "trigger": { "type": "campaign-start" },
        "objectives": [
          { "type": "talk-to", "id": "obj/talk", "npc": "npc/keeper" },
          { "type": "reach-anchor", "id": "obj/exit", "anchor": "anchor/exit", "radius": 2, "after": ["obj/talk"] }
        ],
        "on_objective_complete": {
          "obj/talk": [ { "type": "open-gate", "anchor": "anchor/door" } ]
        },
        "on_complete": [ { "type": "campaign-complete" } ]
      }
    ],
    "triggers": [
      {
        "id": "not-a-valid-trigger-id",
        "at": "anchor/exit",
        "on": { "on": "strike" },
        "effects": []
      }
    ]
  }
}"#,
    )
});

#[test]
fn malformed_trigger_id_is_dw0194() {
    let diags = check_campaign(&campaign_with(
        &valid_npcs_v04(),
        QUESTS_BAD_TRIGGER.as_str(),
        &valid_dialogue_v04(),
    ));
    assert!(
        diags.iter().any(|d| d.code == "DW0194"),
        "malformed trigger id must be DW0194: {diags:#?}"
    );
}

// ---------------------------------------------------------------------------
// DW0350 — a `use` trigger anchored where an NPC stands (round-6 island QA:
// two interaction hitboxes in one cell race for the same right-click, and the
// losing entity is silently dead — dialogue soft-lock class)
// ---------------------------------------------------------------------------

/// The trigger body decides the verdict: `use` on the keeper's own anchor is the
/// collision; `strike` there is the sanctioned form (the NPC's hitbox carries
/// the trigger's tag — no second entity, nothing to race).
fn quests_trigger_on_keeper_stand(on: &str) -> String {
    QUESTS_BAD_TRIGGER.replace(
        r#""id": "not-a-valid-trigger-id",
        "at": "anchor/exit",
        "on": { "on": "strike" },"#,
        &format!(
            r#""id": "trigger/nudge",
        "at": "anchor/keeper-stand",
        "on": {{ "on": "{on}" }},"#
        ),
    )
}

#[test]
fn use_trigger_on_an_npc_anchor_is_dw0350() {
    let quests = quests_trigger_on_keeper_stand("use");
    assert!(
        quests.contains("anchor/keeper-stand"),
        "fixture patch applied"
    );
    let diags = check_campaign(&campaign_with(
        &valid_npcs_v04(),
        &quests,
        &valid_dialogue_v04(),
    ));
    assert!(
        diags.iter().any(|d| d.code == "DW0350"),
        "a `use` trigger on an NPC's anchor must be DW0350: {diags:#?}"
    );
}

#[test]
fn strike_trigger_on_an_npc_anchor_is_not_dw0350() {
    let quests = quests_trigger_on_keeper_stand("strike");
    let diags = check_campaign(&campaign_with(
        &valid_npcs_v04(),
        &quests,
        &valid_dialogue_v04(),
    ));
    assert!(
        !diags.iter().any(|d| d.code == "DW0350"),
        "`strike` on an NPC's anchor is the sanctioned shared-hitbox form: {diags:#?}"
    );
}

// ---------------------------------------------------------------------------
// DW0195 — a `talk-to` targets an NPC despawned by a prerequisite quest
// ---------------------------------------------------------------------------

/// Two quests: `quest/open-the-door` despawns `npc/keeper` when its `obj/talk`
/// completes; `quest/second` (triggered by the first's completion, and the
/// declared finale) has its own `talk-to` on the now-despawned `npc/keeper`.
static QUEST_PLAN_TWO_QUESTS: LazyLock<String> = LazyLock::new(|| {
    common::at_dsl_version(
        r#"{
  "dsl_version": "%dsl_version%",
  "campaign_id": "hello-world",
  "stage": "quest-plan",
  "content": {
    "quests": [
      {
        "id": "quest/open-the-door",
        "goal": "Get the Keeper to open the door and leave the keep.",
        "area": "area/keep",
        "npcs": ["npc/keeper"],
        "depends_on": [],
        "mandatory": true,
        "act": 1
      },
      {
        "id": "quest/second",
        "goal": "Speak to the Keeper once more.",
        "area": "area/keep",
        "npcs": ["npc/keeper"],
        "depends_on": ["quest/open-the-door"],
        "mandatory": true,
        "act": 1
      }
    ],
    "finale": "quest/second"
  }
}"#,
    )
});

static QUESTS_DESPAWNED_REF: LazyLock<String> = LazyLock::new(|| {
    common::at_dsl_version(
        r#"{
  "dsl_version": "%dsl_version%",
  "campaign_id": "hello-world",
  "stage": "quests",
  "content": {
    "quests": [
      {
        "id": "quest/open-the-door",
        "trigger": { "type": "campaign-start" },
        "objectives": [
          { "type": "talk-to", "id": "obj/talk", "npc": "npc/keeper" }
        ],
        "on_objective_complete": {
          "obj/talk": [
            { "type": "open-gate", "anchor": "anchor/door" },
            { "type": "despawn-npc", "npc": "npc/keeper" }
          ]
        },
        "on_complete": []
      },
      {
        "id": "quest/second",
        "trigger": { "type": "quest-complete", "quest": "quest/open-the-door" },
        "objectives": [
          { "type": "talk-to", "id": "obj/talk2", "npc": "npc/keeper" }
        ],
        "on_objective_complete": {},
        "on_complete": [ { "type": "campaign-complete" } ]
      }
    ]
  }
}"#,
    )
});

/// The base dialogue tree, plus a third root option completing `obj/talk2` (so
/// `quest/second`'s objective has a reachable completing option too — DW0195 is
/// the deep despawn-ordering guard, not a dialogue-coverage gap).
static DIALOGUE_TWO_OBJECTIVES: LazyLock<String> = LazyLock::new(|| {
    common::at_dsl_version(
        r#"{
  "dsl_version": "%dsl_version%",
  "campaign_id": "hello-world",
  "stage": "dialogue",
  "content": {
    "dialogues": [
      {
        "npc": "npc/keeper",
        "root": "dlg/greeting",
        "nodes": [
          {
            "id": "dlg/greeting",
            "text": "Halt, traveler. This keep is mine to guard, and the door stays shut.",
            "options": [
              { "label": "Who are you?", "next": "dlg/lore" },
              { "label": "Open the door, please.", "effects": [ { "type": "complete-objective", "objective": "obj/talk" } ] },
              { "label": "Anything else?", "effects": [ { "type": "complete-objective", "objective": "obj/talk2" } ] }
            ]
          },
          {
            "id": "dlg/lore",
            "text": "I am the Keeper. I have watched this gate since the moor swallowed the old road.",
            "options": [ { "label": "Back.", "next": "dlg/greeting" } ]
          }
        ]
      }
    ]
  }
}"#,
    )
});

#[test]
fn talk_to_targets_despawned_npc_is_dw0195() {
    let raw = RawCampaign {
        world: common::read_valid("world.json"),
        npcs: valid_npcs_v04(),
        classes: common::read_valid("classes.json"),
        quest_plan: QUEST_PLAN_TWO_QUESTS.to_string(),
        quests: QUESTS_DESPAWNED_REF.to_string(),
        dialogue: DIALOGUE_TWO_OBJECTIVES.to_string(),
        world_edits: None,
        geometry_brief: None,
        layout_graph: None,
        site_plan: None,
        detail_plan: None,
        design: None,
    };
    let diags = check_campaign(&raw);
    assert!(
        diags.iter().any(|d| d.code == "DW0195"),
        "a talk-to on an npc despawned by a prerequisite quest must be DW0195: {diags:#?}"
    );
}