1use crate::types::Type;
17use std::collections::HashMap;
18use std::sync::OnceLock;
19
20#[derive(Debug, Clone, Copy)]
21pub struct TriggerInfo {
22 pub name: &'static str,
23 pub byte: u8,
24 pub subject_type: Option<Type>,
27 pub validates_subject: bool,
36 pub is_button: bool,
40}
41
42const TRIGGERS: &[TriggerInfo] = &[
43 npc("apnpc1", 3),
45 npc("apnpc2", 4),
46 npc("apnpc3", 5),
47 npc("apnpc4", 6),
48 npc("apnpc5", 7),
49 npc("apnpcu", 8),
50 npc("apnpct", 9),
51 npc("opnpc1", 10),
52 npc("opnpc2", 11),
53 npc("opnpc3", 12),
54 npc("opnpc4", 13),
55 npc("opnpc5", 14),
56 npc("opnpcu", 15),
57 npc("opnpct", 16),
58 npc("ai_apnpc1", 17),
59 npc("ai_apnpc2", 18),
60 npc("ai_apnpc3", 19),
61 npc("ai_apnpc4", 20),
62 npc("ai_apnpc5", 21),
63 npc("ai_opnpc1", 24),
64 npc("ai_opnpc2", 25),
65 npc("ai_opnpc3", 26),
66 npc("ai_opnpc4", 27),
67 npc("ai_opnpc5", 28),
68 obj("apobj1", 31),
70 obj("apobj2", 32),
71 obj("apobj3", 33),
72 obj("apobj4", 34),
73 obj("apobj5", 35),
74 obj("apobju", 36),
75 obj("apobjt", 37),
76 obj("opobj1", 38),
77 obj("opobj2", 39),
78 obj("opobj3", 40),
79 obj("opobj4", 41),
80 obj("opobj5", 42),
81 obj("opobju", 43),
82 obj("opobjt", 44),
83 npc("ai_apobj1", 45),
87 npc("ai_apobj2", 46),
88 npc("ai_apobj3", 47),
89 npc("ai_apobj4", 48),
90 npc("ai_apobj5", 49),
91 npc("ai_opobj1", 52),
92 npc("ai_opobj2", 53),
93 npc("ai_opobj3", 54),
94 npc("ai_opobj4", 55),
95 npc("ai_opobj5", 56),
96 loc("aploc1", 59),
98 loc("aploc2", 60),
99 loc("aploc3", 61),
100 loc("aploc4", 62),
101 loc("aploc5", 63),
102 loc("aplocu", 64),
103 loc("aploct", 65),
104 loc("oploc1", 66),
105 loc("oploc2", 67),
106 loc("oploc3", 68),
107 loc("oploc4", 69),
108 loc("oploc5", 70),
109 loc("oplocu", 71),
110 loc("oploct", 72),
111 npc("ai_aploc1", 73),
115 npc("ai_aploc2", 74),
116 npc("ai_aploc3", 75),
117 npc("ai_aploc4", 76),
118 npc("ai_aploc5", 77),
119 npc("ai_oploc1", 80),
120 npc("ai_oploc2", 81),
121 npc("ai_oploc3", 82),
122 npc("ai_oploc4", 83),
123 npc("ai_oploc5", 84),
124 flat("applayer1", 87),
126 flat("applayer2", 88),
127 flat("applayer3", 89),
128 flat("applayer4", 90),
129 flat("applayer5", 91),
130 flat("applayeru", 92),
131 flat("applayert", 93),
132 flat("opplayer1", 94),
133 flat("opplayer2", 95),
134 flat("opplayer3", 96),
135 flat("opplayer4", 97),
136 flat("opplayer5", 98),
137 flat("opplayeru", 99),
138 flat("opplayert", 100),
139 flat("applayer6", 182),
143 flat("applayer7", 183),
144 flat("applayer8", 184),
145 flat("applayer9", 185),
146 flat("applayer10", 186),
147 flat("opplayer6", 189),
148 flat("opplayer7", 190),
149 flat("opplayer8", 191),
150 flat("opplayer9", 192),
151 flat("opplayer10", 193),
152 flat("apcoordt", 194),
157 npc("ai_applayer1", 101),
161 npc("ai_applayer2", 102),
162 npc("ai_applayer3", 103),
163 npc("ai_applayer4", 104),
164 npc("ai_applayer5", 105),
165 npc("ai_opplayer1", 108),
166 npc("ai_opplayer2", 109),
167 npc("ai_opplayer3", 110),
168 npc("ai_opplayer4", 111),
169 npc("ai_opplayer5", 112),
170 npc_no_validate("ai_queue1", 117),
172 npc_no_validate("ai_queue2", 118),
173 npc_no_validate("ai_queue3", 119),
174 npc_no_validate("ai_queue4", 120),
175 npc_no_validate("ai_queue5", 121),
176 npc_no_validate("ai_queue6", 122),
177 npc_no_validate("ai_queue7", 123),
178 npc_no_validate("ai_queue8", 124),
179 npc_no_validate("ai_queue9", 125),
180 npc_no_validate("ai_queue10", 126),
181 npc_no_validate("ai_queue11", 127),
182 npc_no_validate("ai_queue12", 128),
183 npc_no_validate("ai_queue13", 129),
184 npc_no_validate("ai_queue14", 130),
185 npc_no_validate("ai_queue15", 131),
186 npc_no_validate("ai_queue16", 132),
187 npc_no_validate("ai_queue17", 133),
188 npc_no_validate("ai_queue18", 134),
189 npc_no_validate("ai_queue19", 135),
190 npc_no_validate("ai_queue20", 136),
191 npc_no_validate("ai_timer", 139),
192 obj("opheld1", 140),
194 obj("opheld2", 141),
195 obj("opheld3", 142),
196 obj("opheld4", 143),
197 obj("opheld5", 144),
198 obj("opheldu", 145),
199 obj("opheldt", 146),
200 iface("if_button", 147, true),
202 iface("if_close", 148, false),
203 iface("if_button1", 149, true),
204 iface("if_button2", 150, true),
205 iface("if_button3", 151, true),
206 iface("if_button4", 152, true),
207 iface("if_button5", 153, true),
208 iface("if_buttond", 154, true),
209 iface("if_button6", 173, true),
214 iface("if_button7", 174, true),
215 iface("if_button8", 175, true),
216 iface("if_button9", 176, true),
217 iface("if_button10", 177, true),
218 component("inv_button1", 149),
219 component("inv_button2", 150),
220 component("inv_button3", 151),
221 component("inv_button4", 152),
222 component("inv_button5", 153),
223 component("inv_buttond", 154),
224 flat_no_validate("login", 157),
226 flat_no_validate("logout", 158),
227 flat("tutorial", 159),
228 stat("advancestat", 160),
229 flat_no_validate("mapzone", 161),
230 flat_no_validate("mapzoneexit", 162),
231 flat_no_validate("zone", 163),
232 flat_no_validate("zoneexit", 164),
233 stat("changestat", 165),
234 npc("ai_spawn", 166),
236 npc("ai_despawn", 167),
237 area("area", 178),
240 area("areaexit", 179),
241 controller("ai_conqueue", 180),
244 controller("ai_contimer", 181),
245];
246
247const fn npc(name: &'static str, byte: u8) -> TriggerInfo {
252 TriggerInfo {
253 name,
254 byte,
255 subject_type: Some(Type::Npc),
256 validates_subject: true,
257 is_button: false,
258 }
259}
260
261const fn npc_no_validate(name: &'static str, byte: u8) -> TriggerInfo {
262 TriggerInfo {
263 name,
264 byte,
265 subject_type: Some(Type::Npc),
266 validates_subject: false,
267 is_button: false,
268 }
269}
270
271const fn obj(name: &'static str, byte: u8) -> TriggerInfo {
272 TriggerInfo {
273 name,
274 byte,
275 subject_type: Some(Type::Obj),
276 validates_subject: true,
277 is_button: false,
278 }
279}
280
281const fn loc(name: &'static str, byte: u8) -> TriggerInfo {
282 TriggerInfo {
283 name,
284 byte,
285 subject_type: Some(Type::Loc),
286 validates_subject: true,
287 is_button: false,
288 }
289}
290
291const fn iface(name: &'static str, byte: u8, is_button: bool) -> TriggerInfo {
292 TriggerInfo {
293 name,
294 byte,
295 subject_type: Some(Type::Interface),
296 validates_subject: true,
297 is_button,
298 }
299}
300
301const fn component(name: &'static str, byte: u8) -> TriggerInfo {
302 TriggerInfo {
303 name,
304 byte,
305 subject_type: Some(Type::Component),
306 validates_subject: true,
307 is_button: true,
308 }
309}
310
311const fn controller(name: &'static str, byte: u8) -> TriggerInfo {
312 TriggerInfo {
313 name,
314 byte,
315 subject_type: Some(Type::Controller),
316 validates_subject: true,
317 is_button: false,
318 }
319}
320
321const fn area(name: &'static str, byte: u8) -> TriggerInfo {
322 TriggerInfo {
323 name,
324 byte,
325 subject_type: Some(Type::Area),
326 validates_subject: true,
327 is_button: false,
328 }
329}
330
331const fn stat(name: &'static str, byte: u8) -> TriggerInfo {
336 TriggerInfo {
337 name,
338 byte,
339 subject_type: Some(Type::Stat),
340 validates_subject: true,
341 is_button: false,
342 }
343}
344
345const fn flat(name: &'static str, byte: u8) -> TriggerInfo {
346 TriggerInfo {
347 name,
348 byte,
349 subject_type: None,
350 validates_subject: true,
351 is_button: false,
352 }
353}
354
355const fn flat_no_validate(name: &'static str, byte: u8) -> TriggerInfo {
356 TriggerInfo {
357 name,
358 byte,
359 subject_type: None,
360 validates_subject: false,
361 is_button: false,
362 }
363}
364
365const NAME_KEYED_TRIGGERS: &[&str] = &[
371 "proc",
372 "label",
373 "debugproc",
374 "command",
375 "clientscript",
376 "walktrigger",
377 "queue",
378 "timer",
379 "softtimer",
380 "controller_queue",
381 "npc_queue",
382];
383
384const EXTENDED_TRIGGERS: &[&str] = &["switch_window_mode"];
387
388fn table() -> &'static HashMap<&'static str, TriggerInfo> {
391 static TABLE: OnceLock<HashMap<&'static str, TriggerInfo>> = OnceLock::new();
392 TABLE.get_or_init(|| TRIGGERS.iter().map(|t| (t.name, *t)).collect())
393}
394
395pub fn lookup(name: &str) -> Option<TriggerInfo> {
396 table().get(name).copied()
397}
398
399pub fn names() -> impl Iterator<Item = &'static str> {
400 TRIGGERS
401 .iter()
402 .map(|trigger| trigger.name)
403 .chain(NAME_KEYED_TRIGGERS.iter().copied())
404 .chain(EXTENDED_TRIGGERS.iter().copied())
405}
406
407pub fn byte(name: &str) -> Option<u8> {
408 lookup(name).map(|t| t.byte)
409}
410
411pub fn subject_type(name: &str) -> Option<Type> {
412 lookup(name).and_then(|t| t.subject_type)
413}
414
415pub fn validates_subject(name: &str) -> bool {
416 lookup(name).map(|t| t.validates_subject).unwrap_or(false)
417}
418
419pub fn is_button(name: &str) -> bool {
420 lookup(name).map(|t| t.is_button).unwrap_or(false)
421}
422
423pub fn is_valid_trigger(name: &str) -> bool {
424 lookup(name).is_some()
425 || NAME_KEYED_TRIGGERS.contains(&name)
426 || EXTENDED_TRIGGERS.contains(&name)
427}
428
429pub fn allows_returns(name: &str) -> bool {
430 matches!(name, "proc" | "clientscript" | "command" | "logout")
431}
432
433#[cfg(test)]
434mod tests {
435 use super::*;
436
437 #[test]
438 fn no_duplicate_trigger_names() {
439 let mut seen = std::collections::HashSet::new();
440 for t in TRIGGERS {
441 assert!(seen.insert(t.name), "duplicate trigger name: {}", t.name);
442 }
443 }
444
445 #[test]
446 fn button_triggers_match_legacy_set() {
447 let expected = [
449 "if_button",
450 "if_button1",
451 "if_button2",
452 "if_button3",
453 "if_button4",
454 "if_button5",
455 "if_button6",
456 "if_button7",
457 "if_button8",
458 "if_button9",
459 "if_button10",
460 "if_buttond",
461 "inv_button1",
462 "inv_button2",
463 "inv_button3",
464 "inv_button4",
465 "inv_button5",
466 "inv_buttond",
467 ];
468 for name in expected {
469 assert!(is_button(name), "{} should be a button trigger", name);
470 }
471 assert!(!is_button("if_close"));
473 assert!(!is_button("opnpc1"));
474 assert!(!is_button("login"));
475 }
476
477 #[test]
478 fn known_byte_values() {
479 assert_eq!(byte("opnpc1"), Some(10));
483 assert_eq!(byte("opheld1"), Some(140));
484 assert_eq!(byte("if_button"), Some(147));
485 assert_eq!(byte("if_button1"), Some(149));
486 assert_eq!(byte("inv_button1"), Some(149));
487 assert_eq!(byte("login"), Some(157));
488 assert_eq!(byte("ai_despawn"), Some(167));
489 assert_eq!(byte("totally_unknown_trigger"), None);
490 }
491
492 #[test]
493 fn subject_type_routing() {
494 assert_eq!(subject_type("opnpc1"), Some(Type::Npc));
495 assert_eq!(subject_type("opobj1"), Some(Type::Obj));
496 assert_eq!(subject_type("oploc1"), Some(Type::Loc));
497 assert_eq!(subject_type("opheld1"), Some(Type::Obj));
498 assert_eq!(subject_type("if_button"), Some(Type::Interface));
499 assert_eq!(subject_type("inv_button1"), Some(Type::Component));
500 assert_eq!(subject_type("login"), None);
501 assert_eq!(subject_type("opplayer1"), None); assert_eq!(subject_type("advancestat"), Some(Type::Stat));
506 assert_eq!(subject_type("changestat"), Some(Type::Stat));
507 }
508
509 #[test]
512 fn npc_ai_triggers_take_an_npc_subject() {
513 let mut names: Vec<String> = Vec::new();
514 for target in ["player", "loc", "obj", "npc"] {
515 for op in ["ap", "op"] {
516 for i in 1..=5 {
517 names.push(format!("ai_{op}{target}{i}"));
518 }
519 }
520 }
521 for i in 1..=20 {
522 names.push(format!("ai_queue{i}"));
523 }
524 names.extend(["ai_timer", "ai_spawn", "ai_despawn"].map(String::from));
525 for name in &names {
526 assert_eq!(subject_type(name), Some(Type::Npc), "{name}");
527 }
528 }
529
530 #[test]
531 fn validation_skip_list() {
532 assert!(!validates_subject("login"));
534 assert!(!validates_subject("logout"));
535 assert!(!validates_subject("zone"));
536 assert!(!validates_subject("zoneexit"));
537 assert!(!validates_subject("mapzone"));
538 assert!(!validates_subject("mapzoneexit"));
539 assert!(!validates_subject("ai_queue1"));
540 assert!(!validates_subject("ai_timer"));
541 assert!(validates_subject("if_button"));
543 assert!(validates_subject("opnpc1"));
544 assert!(validates_subject("opheld1"));
545 assert!(validates_subject("tutorial"));
546 }
547
548 #[test]
549 fn is_valid_trigger_byte_keyed() {
550 assert!(is_valid_trigger("opnpc1"));
551 assert!(is_valid_trigger("opheld1"));
552 assert!(is_valid_trigger("if_button"));
553 assert!(is_valid_trigger("login"));
554 assert!(is_valid_trigger("ai_queue1"));
555 assert!(is_valid_trigger("ai_despawn"));
556 }
557
558 #[test]
559 fn is_valid_trigger_name_keyed() {
560 assert!(is_valid_trigger("proc"));
561 assert!(is_valid_trigger("label"));
562 assert!(is_valid_trigger("debugproc"));
563 assert!(is_valid_trigger("command"));
564 assert!(is_valid_trigger("clientscript"));
565 assert!(is_valid_trigger("walktrigger"));
566 assert!(is_valid_trigger("queue"));
567 assert!(is_valid_trigger("timer"));
568 assert!(is_valid_trigger("softtimer"));
569 }
570
571 #[test]
572 fn is_valid_trigger_extended() {
573 assert!(is_valid_trigger("switch_window_mode"));
574 }
575
576 #[test]
577 fn opheld_only_goes_to_five() {
578 assert!(is_valid_trigger("opheld5"));
581 assert!(!is_valid_trigger("opheld6"));
582 assert!(!is_valid_trigger("opheld7"));
583 assert!(!is_valid_trigger("opheld10"));
584 }
585
586 #[test]
587 fn if_button_extended_to_ten() {
588 assert!(is_valid_trigger("if_button6"));
591 assert!(is_valid_trigger("if_button10"));
592 assert_eq!(byte("if_button6"), Some(173));
593 assert_eq!(byte("if_button10"), Some(177));
594 }
595
596 #[test]
597 fn player_triggers_extended_to_ten() {
598 for op in 6..=10u8 {
601 assert_eq!(byte(&format!("applayer{op}")), Some(176 + op));
602 assert_eq!(byte(&format!("opplayer{op}")), Some(183 + op));
603 }
604 assert!(!is_valid_trigger("opplayer11"));
605 }
606
607 #[test]
608 fn ground_target_trigger() {
609 assert_eq!(byte("apcoordt"), Some(194));
612 assert!(TRIGGERS.iter().all(|t| t.byte != 201));
613 assert!(!is_valid_trigger("opcoordt"));
614 }
615
616 #[test]
617 fn controller_triggers() {
618 assert_eq!(byte("ai_conqueue"), Some(180));
621 assert_eq!(byte("ai_contimer"), Some(181));
622 assert_eq!(subject_type("ai_conqueue"), Some(Type::Controller));
623 assert_eq!(subject_type("ai_contimer"), Some(Type::Controller));
624 }
625
626 #[test]
627 fn inv_button_caps_at_five() {
628 for n in ["inv_button1", "inv_button5", "inv_buttond"] {
633 assert!(is_valid_trigger(n), "{n} should still be a trigger");
634 }
635 for n in ["inv_button6", "inv_button7", "inv_button10"] {
636 assert!(
637 !is_valid_trigger(n),
638 "{n} must not exist (inv_button caps at 5)"
639 );
640 assert_eq!(byte(n), None);
641 }
642 }
643
644 #[test]
645 fn is_valid_trigger_unknown_rejected() {
646 assert!(!is_valid_trigger("not_a_real_trigger"));
647 assert!(!is_valid_trigger(""));
648 assert!(!is_valid_trigger("procc"));
649 }
650
651 #[test]
652 fn allows_returns_matches_lib_set() {
653 assert!(allows_returns("proc"));
654 assert!(allows_returns("clientscript"));
655 assert!(allows_returns("command"));
656 assert!(allows_returns("logout"));
657 assert!(!allows_returns("label"));
658 assert!(!allows_returns("debugproc"));
659 assert!(!allows_returns("queue"));
660 assert!(!allows_returns("timer"));
661 assert!(!allows_returns("softtimer"));
662 assert!(!allows_returns("walktrigger"));
663 assert!(!allows_returns("login"));
664 assert!(!allows_returns("opnpc1"));
665 }
666
667 #[test]
668 fn name_keyed_triggers_have_no_byte() {
669 assert_eq!(byte("proc"), None);
670 assert_eq!(byte("label"), None);
671 assert_eq!(byte("command"), None);
672 assert_eq!(byte("walktrigger"), None);
673 }
674}