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