rs-runec 0.7.0

Compiler, linter, and language server for RuneScript, ClientScript, and GlobalScript, with ClientScript decompilation
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
//! Single source of truth for RuneScript trigger metadata.
//!
//! Every trigger keyword (`[opnpc1,…]`, `[if_button,…]`, `[login,_]`, …)
//! has the same shape of metadata: a byte discriminant for lookup-key
//! dispatch, an optional preferred subject type for entity resolution,
//! whether its subject should be validated to resolve, and whether it's a
//! button trigger that participates in pointer-grant logic.
//!
//! Before this module existed, that metadata was scattered across three
//! separate matches in `compiler.rs` (the byte table, the
//! `is_byte_keyed_trigger` whitelist, and the `preferred_type` selector)
//! plus a 13-arm match in `pointer_checker.rs`. Adding a trigger meant
//! editing four places, and one of them was the bug shape that silently
//! produced unreachable scripts.

use crate::types::Type;
use std::collections::HashMap;
use std::path::Path;
use std::sync::OnceLock;

#[derive(Debug, Clone, Copy)]
pub struct TriggerInfo {
    pub name: &'static str,
    pub byte: u8,
    /// Preferred entity type for `lookup_entity_id_typed`. `None` falls
    /// through to the flat entity-id lookup.
    pub subject_type: Option<Type>,
    /// True if a `[trigger,name]` script with a non-`_`, non-`_category`,
    /// non-coord subject should resolve to a real entity. Drives the
    /// "subject did not resolve" warning. False for triggers that:
    ///   - have no entity subject (login, logout)
    ///   - take coord-shaped subjects (zone, zoneexit, mapzone, mapzoneexit)
    ///   - intentionally don't validate (ai_queue, ai_timer — preserved
    ///     from the original `is_byte_keyed_trigger` whitelist; widening
    ///     this would change emitted warnings)
    pub validates_subject: bool,
    /// True for if_button/if_button1-10/if_buttond and inv_button1-5/inv_buttond.
    /// Used by pointer_checker to grant p_active_player on non-overlay
    /// interfaces, mirroring ServerPointerChecker.setsPointerTrigger().
    pub is_button: bool,
}

const TRIGGERS: &[TriggerInfo] = &[
    // ── NPC triggers ─────────────────────────────────────────────────
    npc("apnpc1", 3),
    npc("apnpc2", 4),
    npc("apnpc3", 5),
    npc("apnpc4", 6),
    npc("apnpc5", 7),
    npc("apnpcu", 8),
    npc("apnpct", 9),
    npc("opnpc1", 10),
    npc("opnpc2", 11),
    npc("opnpc3", 12),
    npc("opnpc4", 13),
    npc("opnpc5", 14),
    npc("opnpcu", 15),
    npc("opnpct", 16),
    npc("ai_apnpc1", 17),
    npc("ai_apnpc2", 18),
    npc("ai_apnpc3", 19),
    npc("ai_apnpc4", 20),
    npc("ai_apnpc5", 21),
    npc("ai_opnpc1", 24),
    npc("ai_opnpc2", 25),
    npc("ai_opnpc3", 26),
    npc("ai_opnpc4", 27),
    npc("ai_opnpc5", 28),
    // ── Ground obj triggers ──────────────────────────────────────────
    obj("apobj1", 31),
    obj("apobj2", 32),
    obj("apobj3", 33),
    obj("apobj4", 34),
    obj("apobj5", 35),
    obj("apobju", 36),
    obj("apobjt", 37),
    obj("opobj1", 38),
    obj("opobj2", 39),
    obj("opobj3", 40),
    obj("opobj4", 41),
    obj("opobj5", 42),
    obj("opobju", 43),
    obj("opobjt", 44),
    // An npc's AI triggers name the npc (or `_category`) whatever its target:
    // the engine keys them by the acting npc's type, and RuneScriptTS gives them
    // SubjectMode.Type(NPC).
    npc("ai_apobj1", 45),
    npc("ai_apobj2", 46),
    npc("ai_apobj3", 47),
    npc("ai_apobj4", 48),
    npc("ai_apobj5", 49),
    npc("ai_opobj1", 52),
    npc("ai_opobj2", 53),
    npc("ai_opobj3", 54),
    npc("ai_opobj4", 55),
    npc("ai_opobj5", 56),
    // ── Loc triggers ─────────────────────────────────────────────────
    loc("aploc1", 59),
    loc("aploc2", 60),
    loc("aploc3", 61),
    loc("aploc4", 62),
    loc("aploc5", 63),
    loc("aplocu", 64),
    loc("aploct", 65),
    loc("oploc1", 66),
    loc("oploc2", 67),
    loc("oploc3", 68),
    loc("oploc4", 69),
    loc("oploc5", 70),
    loc("oplocu", 71),
    loc("oploct", 72),
    // An npc's AI triggers name the npc (or `_category`) whatever its target:
    // the engine keys them by the acting npc's type, and RuneScriptTS gives them
    // SubjectMode.Type(NPC).
    npc("ai_aploc1", 73),
    npc("ai_aploc2", 74),
    npc("ai_aploc3", 75),
    npc("ai_aploc4", 76),
    npc("ai_aploc5", 77),
    npc("ai_oploc1", 80),
    npc("ai_oploc2", 81),
    npc("ai_oploc3", 82),
    npc("ai_oploc4", 83),
    npc("ai_oploc5", 84),
    // ── Player triggers (no preferred type — fall through to flat lookup) ──
    flat("applayer1", 87),
    flat("applayer2", 88),
    flat("applayer3", 89),
    flat("applayer4", 90),
    flat("applayer5", 91),
    flat("applayeru", 92),
    flat("applayert", 93),
    flat("opplayer1", 94),
    flat("opplayer2", 95),
    flat("opplayer3", 96),
    flat("opplayer4", 97),
    flat("opplayer5", 98),
    flat("opplayeru", 99),
    flat("opplayert", 100),
    // Rev-647 players have ops 6-10 (the client shows eight). Bytes 182-186 and
    // 189-193 must match the engine's ServerTriggerType ApPlayer6-10 and
    // OpPlayer6-10, which keeps each op 7 past its ap.
    flat("applayer6", 182),
    flat("applayer7", 183),
    flat("applayer8", 184),
    flat("applayer9", 185),
    flat("applayer10", 186),
    flat("opplayer6", 189),
    flat("opplayer7", 190),
    flat("opplayer8", 191),
    flat("opplayer9", 192),
    flat("opplayer10", 193),
    // Rev-647 ground targeting (APCOORDT): a component aimed at a tile,
    // approached only and keyed by the component. Byte 194 must match the
    // engine's ServerTriggerType ApCoordT, and 201, 7 past it, stays unused:
    // the engine looks an op trigger up there.
    flat("apcoordt", 194),
    // An npc's AI triggers name the npc (or `_category`) whatever its target:
    // the engine keys them by the acting npc's type, and RuneScriptTS gives them
    // SubjectMode.Type(NPC).
    npc("ai_applayer1", 101),
    npc("ai_applayer2", 102),
    npc("ai_applayer3", 103),
    npc("ai_applayer4", 104),
    npc("ai_applayer5", 105),
    npc("ai_opplayer1", 108),
    npc("ai_opplayer2", 109),
    npc("ai_opplayer3", 110),
    npc("ai_opplayer4", 111),
    npc("ai_opplayer5", 112),
    // ── AI queue / timer (NPC-keyed, but historically not validated) ──
    npc_no_validate("ai_queue1", 117),
    npc_no_validate("ai_queue2", 118),
    npc_no_validate("ai_queue3", 119),
    npc_no_validate("ai_queue4", 120),
    npc_no_validate("ai_queue5", 121),
    npc_no_validate("ai_queue6", 122),
    npc_no_validate("ai_queue7", 123),
    npc_no_validate("ai_queue8", 124),
    npc_no_validate("ai_queue9", 125),
    npc_no_validate("ai_queue10", 126),
    npc_no_validate("ai_queue11", 127),
    npc_no_validate("ai_queue12", 128),
    npc_no_validate("ai_queue13", 129),
    npc_no_validate("ai_queue14", 130),
    npc_no_validate("ai_queue15", 131),
    npc_no_validate("ai_queue16", 132),
    npc_no_validate("ai_queue17", 133),
    npc_no_validate("ai_queue18", 134),
    npc_no_validate("ai_queue19", 135),
    npc_no_validate("ai_queue20", 136),
    npc_no_validate("ai_timer", 139),
    // ── Held / inventory item triggers ───────────────────────────────
    obj("opheld1", 140),
    obj("opheld2", 141),
    obj("opheld3", 142),
    obj("opheld4", 143),
    obj("opheld5", 144),
    obj("opheldu", 145),
    obj("opheldt", 146),
    // ── Interface / button triggers ──────────────────────────────────
    iface("if_button", 147, true),
    iface("if_close", 148, false),
    iface("if_button1", 149, true),
    iface("if_button2", 150, true),
    iface("if_button3", 151, true),
    iface("if_button4", 152, true),
    iface("if_button5", 153, true),
    iface("if_buttond", 154, true),
    // Rev-647: client merged OpHeld/InvButton into IfButton1-10. Inventory
    // item ops 6-10 (e.g. Drop=op8) are handled in content via
    // `[if_button6..10,inv:component]` + `p_opheld(op, inv, slot)`.
    // Bytes 173-177 must match the engine's ServerTriggerType InvButton6-10.
    iface("if_button6", 173, true),
    iface("if_button7", 174, true),
    iface("if_button8", 175, true),
    iface("if_button9", 176, true),
    iface("if_button10", 177, true),
    component("inv_button1", 149),
    component("inv_button2", 150),
    component("inv_button3", 151),
    component("inv_button4", 152),
    component("inv_button5", 153),
    component("inv_buttond", 154),
    // ── Player lifecycle / coord / stat triggers ─────────────────────
    flat_no_validate("login", 157),
    flat_no_validate("logout", 158),
    flat("tutorial", 159),
    stat("advancestat", 160),
    flat_no_validate("mapzone", 161),
    flat_no_validate("mapzoneexit", 162),
    flat_no_validate("zone", 163),
    flat_no_validate("zoneexit", 164),
    stat("changestat", 165),
    // An npc's spawn and despawn name the npc, as its other AI triggers do.
    npc("ai_spawn", 166),
    npc("ai_despawn", 167),
    // A player entering or leaving an area painted on the map. 168-172 stay
    // unused: they were opheld6-10.
    area("area", 178),
    area("areaexit", 179),
    // A controller's one queue script (Jagex's `[ai_conqueue,<controller>]`,
    // the int queued with it in last_int) and its timer.
    controller("ai_conqueue", 180),
    controller("ai_contimer", 181),
];

// ── Constructor helpers ──────────────────────────────────────────────
//
// `const fn` so the static slice above stays trivially constructible.

const fn npc(name: &'static str, byte: u8) -> TriggerInfo {
    TriggerInfo {
        name,
        byte,
        subject_type: Some(Type::Npc),
        validates_subject: true,
        is_button: false,
    }
}

const fn npc_no_validate(name: &'static str, byte: u8) -> TriggerInfo {
    TriggerInfo {
        name,
        byte,
        subject_type: Some(Type::Npc),
        validates_subject: false,
        is_button: false,
    }
}

const fn obj(name: &'static str, byte: u8) -> TriggerInfo {
    TriggerInfo {
        name,
        byte,
        subject_type: Some(Type::Obj),
        validates_subject: true,
        is_button: false,
    }
}

const fn loc(name: &'static str, byte: u8) -> TriggerInfo {
    TriggerInfo {
        name,
        byte,
        subject_type: Some(Type::Loc),
        validates_subject: true,
        is_button: false,
    }
}

const fn iface(name: &'static str, byte: u8, is_button: bool) -> TriggerInfo {
    TriggerInfo {
        name,
        byte,
        subject_type: Some(Type::Interface),
        validates_subject: true,
        is_button,
    }
}

const fn component(name: &'static str, byte: u8) -> TriggerInfo {
    TriggerInfo {
        name,
        byte,
        subject_type: Some(Type::Component),
        validates_subject: true,
        is_button: true,
    }
}

const fn controller(name: &'static str, byte: u8) -> TriggerInfo {
    TriggerInfo {
        name,
        byte,
        subject_type: Some(Type::Controller),
        validates_subject: true,
        is_button: false,
    }
}

const fn area(name: &'static str, byte: u8) -> TriggerInfo {
    TriggerInfo {
        name,
        byte,
        subject_type: Some(Type::Area),
        validates_subject: true,
        is_button: false,
    }
}

/// `[advancestat,stat]` / `[changestat,stat]` — subject is a player stat.
/// Routes to `Type::Stat` so the subject resolves via the typed map instead
/// of the flat entity-id map, where a same-named entity of another type
/// (e.g. a `hunt` called `ranged`) would otherwise shadow the stat.
const fn stat(name: &'static str, byte: u8) -> TriggerInfo {
    TriggerInfo {
        name,
        byte,
        subject_type: Some(Type::Stat),
        validates_subject: true,
        is_button: false,
    }
}

const fn flat(name: &'static str, byte: u8) -> TriggerInfo {
    TriggerInfo {
        name,
        byte,
        subject_type: None,
        validates_subject: true,
        is_button: false,
    }
}

const fn flat_no_validate(name: &'static str, byte: u8) -> TriggerInfo {
    TriggerInfo {
        name,
        byte,
        subject_type: None,
        validates_subject: false,
        is_button: false,
    }
}

// ── Name-keyed triggers ─────────────────────────────────────────────
//
// Triggers dispatched by script name (no byte discriminant). These are
// valid trigger keywords but don't participate in lookup-key hashing.

const NAME_KEYED_TRIGGERS: &[&str] = &[
    "proc",
    "label",
    "debugproc",
    "command",
    "clientscript",
    "walktrigger",
    "queue",
    "timer",
    "softtimer",
    "controller_queue",
    "npc_queue",
];

// Rev-647 extensions: valid triggers whose byte discriminants are not
// yet standardized in the reference implementation.
const EXTENDED_TRIGGERS: &[&str] = &["switch_window_mode"];

// Lobbyscript accepts only these, with their server bytes.
const LOBBY_TRIGGERS: &[&str] = &[
    "proc",
    "label",
    "login",
    "logout",
    "if_button",
    "if_button1",
    "if_button2",
    "if_button3",
    "if_button4",
    "if_button5",
    "if_button6",
    "if_button7",
    "if_button8",
    "if_button9",
    "if_button10",
    "if_buttond",
    "if_close",
];

// ── Public lookup API ────────────────────────────────────────────────

fn table() -> &'static HashMap<&'static str, TriggerInfo> {
    static TABLE: OnceLock<HashMap<&'static str, TriggerInfo>> = OnceLock::new();
    TABLE.get_or_init(|| TRIGGERS.iter().map(|t| (t.name, *t)).collect())
}

pub fn lookup(name: &str) -> Option<TriggerInfo> {
    table().get(name).copied()
}

pub fn names() -> impl Iterator<Item = &'static str> {
    TRIGGERS
        .iter()
        .map(|trigger| trigger.name)
        .chain(NAME_KEYED_TRIGGERS.iter().copied())
        .chain(EXTENDED_TRIGGERS.iter().copied())
}

pub fn byte(name: &str) -> Option<u8> {
    lookup(name).map(|t| t.byte)
}

pub fn subject_type(name: &str) -> Option<Type> {
    lookup(name).and_then(|t| t.subject_type)
}

pub fn validates_subject(name: &str) -> bool {
    lookup(name).map(|t| t.validates_subject).unwrap_or(false)
}

pub fn is_button(name: &str) -> bool {
    lookup(name).map(|t| t.is_button).unwrap_or(false)
}

pub fn is_valid_trigger(name: &str) -> bool {
    lookup(name).is_some()
        || NAME_KEYED_TRIGGERS.contains(&name)
        || EXTENDED_TRIGGERS.contains(&name)
}

// `command` declares engine signatures, so only engine.ls2 takes it.
pub fn is_lobby_trigger(name: &str, path: &Path) -> bool {
    LOBBY_TRIGGERS.contains(&name) || name == "command" && is_lobby_engine(path)
}

pub fn lobby_names(path: &Path) -> impl Iterator<Item = &'static str> {
    LOBBY_TRIGGERS
        .iter()
        .copied()
        .chain(is_lobby_engine(path).then_some("command"))
}

fn is_lobby_engine(path: &Path) -> bool {
    path.file_name().is_some_and(|file| file == "engine.ls2")
}

pub fn allows_returns(name: &str) -> bool {
    matches!(name, "proc" | "clientscript" | "command" | "logout")
}

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

    #[test]
    fn no_duplicate_trigger_names() {
        let mut seen = std::collections::HashSet::new();
        for t in TRIGGERS {
            assert!(seen.insert(t.name), "duplicate trigger name: {}", t.name);
        }
    }

    #[test]
    fn lobby_triggers_are_server_triggers() {
        for name in lobby_names(Path::new("engine.ls2")) {
            assert!(is_valid_trigger(name), "{name} is not a server trigger");
        }
    }

    #[test]
    fn button_triggers_match_legacy_set() {
        // Locks in pointer_checker's old hand-maintained list.
        let expected = [
            "if_button",
            "if_button1",
            "if_button2",
            "if_button3",
            "if_button4",
            "if_button5",
            "if_button6",
            "if_button7",
            "if_button8",
            "if_button9",
            "if_button10",
            "if_buttond",
            "inv_button1",
            "inv_button2",
            "inv_button3",
            "inv_button4",
            "inv_button5",
            "inv_buttond",
        ];
        for name in expected {
            assert!(is_button(name), "{} should be a button trigger", name);
        }
        // Spot-check that non-buttons aren't flagged.
        assert!(!is_button("if_close"));
        assert!(!is_button("opnpc1"));
        assert!(!is_button("login"));
    }

    #[test]
    fn known_byte_values() {
        // Anchors against the RuneScriptTS ServerTriggerType.ts byte
        // discriminants. If any of these change, the engine's dispatch
        // will silently desync.
        assert_eq!(byte("opnpc1"), Some(10));
        assert_eq!(byte("opheld1"), Some(140));
        assert_eq!(byte("if_button"), Some(147));
        assert_eq!(byte("if_button1"), Some(149));
        assert_eq!(byte("inv_button1"), Some(149));
        assert_eq!(byte("login"), Some(157));
        assert_eq!(byte("ai_despawn"), Some(167));
        assert_eq!(byte("totally_unknown_trigger"), None);
    }

    #[test]
    fn subject_type_routing() {
        assert_eq!(subject_type("opnpc1"), Some(Type::Npc));
        assert_eq!(subject_type("opobj1"), Some(Type::Obj));
        assert_eq!(subject_type("oploc1"), Some(Type::Loc));
        assert_eq!(subject_type("opheld1"), Some(Type::Obj));
        assert_eq!(subject_type("if_button"), Some(Type::Interface));
        assert_eq!(subject_type("inv_button1"), Some(Type::Component));
        assert_eq!(subject_type("login"), None);
        assert_eq!(subject_type("opplayer1"), None); // flat lookup
        // advancestat/changestat take a stat subject. Without this routing,
        // `[advancestat,ranged]` flat-resolves `ranged` to whatever type was
        // registered last under that name (e.g. a hunt) instead of the stat.
        assert_eq!(subject_type("advancestat"), Some(Type::Stat));
        assert_eq!(subject_type("changestat"), Some(Type::Stat));
    }

    /// Every npc AI trigger's subject is the acting npc, whatever it targets
    /// (RuneScriptTS: SubjectMode.Type(NPC); the engine keys them by npc type).
    #[test]
    fn npc_ai_triggers_take_an_npc_subject() {
        let mut names: Vec<String> = Vec::new();
        for target in ["player", "loc", "obj", "npc"] {
            for op in ["ap", "op"] {
                for i in 1..=5 {
                    names.push(format!("ai_{op}{target}{i}"));
                }
            }
        }
        for i in 1..=20 {
            names.push(format!("ai_queue{i}"));
        }
        names.extend(["ai_timer", "ai_spawn", "ai_despawn"].map(String::from));
        for name in &names {
            assert_eq!(subject_type(name), Some(Type::Npc), "{name}");
        }
    }

    #[test]
    fn validation_skip_list() {
        // Triggers historically excluded from subject validation.
        assert!(!validates_subject("login"));
        assert!(!validates_subject("logout"));
        assert!(!validates_subject("zone"));
        assert!(!validates_subject("zoneexit"));
        assert!(!validates_subject("mapzone"));
        assert!(!validates_subject("mapzoneexit"));
        assert!(!validates_subject("ai_queue1"));
        assert!(!validates_subject("ai_timer"));
        // …but the rest of the byte-keyed triggers do validate.
        assert!(validates_subject("if_button"));
        assert!(validates_subject("opnpc1"));
        assert!(validates_subject("opheld1"));
        assert!(validates_subject("tutorial"));
    }

    #[test]
    fn is_valid_trigger_byte_keyed() {
        assert!(is_valid_trigger("opnpc1"));
        assert!(is_valid_trigger("opheld1"));
        assert!(is_valid_trigger("if_button"));
        assert!(is_valid_trigger("login"));
        assert!(is_valid_trigger("ai_queue1"));
        assert!(is_valid_trigger("ai_despawn"));
    }

    #[test]
    fn is_valid_trigger_name_keyed() {
        assert!(is_valid_trigger("proc"));
        assert!(is_valid_trigger("label"));
        assert!(is_valid_trigger("debugproc"));
        assert!(is_valid_trigger("command"));
        assert!(is_valid_trigger("clientscript"));
        assert!(is_valid_trigger("walktrigger"));
        assert!(is_valid_trigger("queue"));
        assert!(is_valid_trigger("timer"));
        assert!(is_valid_trigger("softtimer"));
    }

    #[test]
    fn is_valid_trigger_extended() {
        assert!(is_valid_trigger("switch_window_mode"));
    }

    #[test]
    fn opheld_only_goes_to_five() {
        // Rev-647 (and OSRS) have no opheld beyond 5. Inventory item ops 6-10
        // route through if_button6-10 + the p_opheld content command instead.
        assert!(is_valid_trigger("opheld5"));
        assert!(!is_valid_trigger("opheld6"));
        assert!(!is_valid_trigger("opheld7"));
        assert!(!is_valid_trigger("opheld10"));
    }

    #[test]
    fn if_button_extended_to_ten() {
        // Bytes 173-177 must stay in sync with the engine's ServerTriggerType
        // InvButton6-10.
        assert!(is_valid_trigger("if_button6"));
        assert!(is_valid_trigger("if_button10"));
        assert_eq!(byte("if_button6"), Some(173));
        assert_eq!(byte("if_button10"), Some(177));
    }

    #[test]
    fn player_triggers_extended_to_ten() {
        // Bytes 182-186 and 189-193 must stay in sync with the engine's
        // ServerTriggerType ApPlayer6-10/OpPlayer6-10.
        for op in 6..=10u8 {
            assert_eq!(byte(&format!("applayer{op}")), Some(176 + op));
            assert_eq!(byte(&format!("opplayer{op}")), Some(183 + op));
        }
        assert!(!is_valid_trigger("opplayer11"));
    }

    #[test]
    fn ground_target_trigger() {
        // Byte 194 must stay in sync with the engine's ServerTriggerType
        // ApCoordT; a tile is only approached, so there is no opcoordt.
        assert_eq!(byte("apcoordt"), Some(194));
        assert!(TRIGGERS.iter().all(|t| t.byte != 201));
        assert!(!is_valid_trigger("opcoordt"));
    }

    #[test]
    fn controller_triggers() {
        // Bytes 180-181 must stay in sync with the engine's ServerTriggerType
        // AiConQueue/AiConTimer.
        assert_eq!(byte("ai_conqueue"), Some(180));
        assert_eq!(byte("ai_contimer"), Some(181));
        assert_eq!(subject_type("ai_conqueue"), Some(Type::Controller));
        assert_eq!(subject_type("ai_contimer"), Some(Type::Controller));
    }

    #[test]
    fn inv_button_caps_at_five() {
        // inv_button is the 225-era spelling with ops 1-5 (+ d). 647 content
        // uses if_button, which extends to 6-10. Both 225 and 647 content
        // compile here, so inv_button1-5/inv_buttond stay valid while
        // inv_button6-10 must not exist.
        for n in ["inv_button1", "inv_button5", "inv_buttond"] {
            assert!(is_valid_trigger(n), "{n} should still be a trigger");
        }
        for n in ["inv_button6", "inv_button7", "inv_button10"] {
            assert!(
                !is_valid_trigger(n),
                "{n} must not exist (inv_button caps at 5)"
            );
            assert_eq!(byte(n), None);
        }
    }

    #[test]
    fn is_valid_trigger_unknown_rejected() {
        assert!(!is_valid_trigger("not_a_real_trigger"));
        assert!(!is_valid_trigger(""));
        assert!(!is_valid_trigger("procc"));
    }

    #[test]
    fn allows_returns_matches_lib_set() {
        assert!(allows_returns("proc"));
        assert!(allows_returns("clientscript"));
        assert!(allows_returns("command"));
        assert!(allows_returns("logout"));
        assert!(!allows_returns("label"));
        assert!(!allows_returns("debugproc"));
        assert!(!allows_returns("queue"));
        assert!(!allows_returns("timer"));
        assert!(!allows_returns("softtimer"));
        assert!(!allows_returns("walktrigger"));
        assert!(!allows_returns("login"));
        assert!(!allows_returns("opnpc1"));
    }

    #[test]
    fn name_keyed_triggers_have_no_byte() {
        assert_eq!(byte("proc"), None);
        assert_eq!(byte("label"), None);
        assert_eq!(byte("command"), None);
        assert_eq!(byte("walktrigger"), None);
    }
}