Advertisement
SethBling

suppress_triggers.lua

May 15th, 2023
458
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.30 KB | None | 0 0
  1. local function regex_escape_line(str)
  2. return "^" .. str:gsub("([%(%)%.%+%-%*%?%[%^%$])", "\\%1") .. "$"
  3. end
  4.  
  5. local SuppressAllSequence = 12
  6. local HeaderSequence = 13
  7. local FooterSequence = 11
  8.  
  9. local suppress_trigger_index = 0
  10. local suppress_triggers_headers = {}
  11.  
  12. function suppress_triggers_for_all_tags()
  13. suppress_triggers_between_tags("<MAPSTART>", "<MAPEND>")
  14. suppress_triggers_between_tags("{BIGMAP}", "{/BIGMAP}")
  15. suppress_triggers_between_tags("{edit}", "{/edit}")
  16. suppress_triggers_between_tags("{equip}", "{/equip}")
  17. suppress_triggers_between_tags("{help}", "{/help}")
  18. suppress_triggers_between_tags("{inventory}", "{/inventory}")
  19. suppress_triggers_between_tags("{rdesc}", "{/rdesc}")
  20. suppress_triggers_between_tags("{score}", "{/score}")
  21. suppress_triggers_between_tags("{roomchars}", "{/roomchars}")
  22. suppress_triggers_between_tags("{scan}", "{/scan}")
  23. end
  24.  
  25. -- Create the trigger to suppress all triggers between the header and footer
  26. -- @param header The literal header string to match
  27. -- @param footer The literal footer string to match
  28. function suppress_triggers_between_tags(header, footer)
  29. suppress_trigger_index = suppress_trigger_index + 1
  30.  
  31. -- Add the pattern for the header
  32. local match = GetTriggerOption("suppress_triggers_header", "match")
  33. if match then
  34. local combined = match .. "|" .. regex_escape_line(header)
  35. SetTriggerOption("suppress_triggers_header", "match", combined)
  36. else
  37. AddTriggerEx("suppress_triggers_header", regex_escape_line(header), "", trigger_flag.Enabled + trigger_flag.Replace + trigger_flag.RegularExpression + trigger_flag.Temporary, -1, 0, "", "suppress_triggers_header", 0, HeaderSequence)
  38. end
  39.  
  40. -- Create a trigger to suppress all triggers if it doesn't exist
  41. AddTriggerEx("suppress_all_triggers", "*", "", trigger_flag.Replace + trigger_flag.Temporary, -1, 0, "", "", 0, SuppressAllSequence)
  42.  
  43. -- Create the trigger for the footer
  44. local footer_name = "suppress_triggers_footer_" .. suppress_trigger_index
  45. AddTriggerEx(footer_name, regex_escape_line(footer), "", trigger_flag.Replace + trigger_flag.RegularExpression + trigger_flag.Temporary, -1, 0, "", "suppress_triggers_footer", 0, FooterSequence)
  46.  
  47. -- Store data for the header trigger
  48. suppress_triggers_headers[header] = {
  49. footer = footer,
  50. footer_name = footer_name,
  51. }
  52. end
  53.  
  54. -- When any of the headers are matched, find the corresponding footer
  55. -- and enable it. Also enable the trigger to suppress all triggers.
  56. function suppress_triggers_header(name, line)
  57. local data = suppress_triggers_headers[line]
  58.  
  59. if not data then
  60. ColourNote("red", "", "Error suppressing triggers. Invalid header line: " .. line)
  61. EnableTrigger("suppress_all_triggers", false)
  62. return
  63. end
  64.  
  65. EnableTrigger(data.footer_name, true)
  66. EnableTrigger("suppress_all_triggers", true)
  67. EnableTrigger("suppress_triggers_header", false)
  68. end
  69.  
  70. -- When the footer is matched, disable the footer and the trigger to
  71. -- suppress all triggers. Also enable the trigger to match the headers.
  72. function suppress_triggers_footer(name)
  73. EnableTrigger(name, false)
  74. EnableTrigger("suppress_all_triggers", false)
  75. EnableTrigger("suppress_triggers_header", true)
  76. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement