Advertisement
Iavra

[Ace] Event Tag Reader

May 31st, 2015
470
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 6.11 KB | None | 0 0
  1. #==============================================================================
  2. # Iavra Event Tag Reader 1.06
  3. # -----------------------------------------------------------------------------
  4. # Description:
  5. # Offers a generic hook for other scripts to read notetags from the current
  6. # page of event.
  7. # -----------------------------------------------------------------------------
  8. # Prerequisites:
  9. # None
  10. # -----------------------------------------------------------------------------
  11. # How to Use:
  12. #
  13. # Register a regex in your script like this:
  14. #
  15. # IAVRA::EVENT_TAGS.register(:key, /regex/, :int, :int)
  16. #
  17. # This will attempt to capture the first 2 groups from your regex and convert
  18. # them to integers. Everything less than 2 will be set to nil in the result,
  19. # while everything more than 2 will be discarded.
  20. #
  21. # Get the matches for that regex during runtime like this:
  22. #
  23. # IAVRA::EVENT_TAGS.get_all(event_id, :key)    # returns all matches.
  24. # IAVRA::EVENT_TAGS.get(event_id, :key)        # returns the first match.
  25. # IAVRA::EVENT_TAGS.get(event_id, :key, index) # returns the (index)th match.
  26. #
  27. # Each match is an array containing your captured groups.
  28. # -----------------------------------------------------------------------------
  29. # Terms of Use:
  30. # Free to use for both commercial and non-commercial games. Please give credit.
  31. # -----------------------------------------------------------------------------
  32. # Credits:
  33. # Iavra
  34. # -----------------------------------------------------------------------------
  35. # Changelog:
  36. # - 1.01: Added support for named capture groups.
  37. # - 1.02: Renamed get to get_all and added a new get method for direct access.
  38. # - 1.03: Added an $imported entry for the script.
  39. # - 1.04: Shortened the hash building.
  40. # - 1.05: Rebuilt the script. Support for named groups was discarded, since i
  41. #         figured those don't get used much, anyway.
  42. # - 1.06: Reduced the number of iterations to improve performance.
  43. #==============================================================================
  44.  
  45. ($imported ||= {})[:iavra_event_tags] = true
  46.  
  47. #==============================================================================
  48. # ▼ IAVRA::EVENT_TAGS
  49. #==============================================================================
  50.  
  51. module IAVRA
  52.     module EVENT_TAGS
  53.        
  54.         #==========================================================================
  55.         # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
  56.         #==========================================================================
  57.        
  58.         #==========================================================================
  59.         # Procs responsible for converting the captured groups. Nil values will be
  60.         # checked beforehand, so the functions don't need to care about them.
  61.         #==========================================================================
  62.        
  63.         CAPTURES = {
  64.             :string => lambda {|value| value},
  65.             :symbol => lambda {|value| value.to_sym},
  66.             :int =>    lambda {|value| value.to_i},
  67.             :float =>  lambda {|value| value.to_f}
  68.         }
  69.        
  70.         #==========================================================================
  71.         # ■ ■ ■ ■ ■ CONFIGURATION ■ ■ ■ ■ ■
  72.         #==========================================================================
  73.        
  74.         #==========================================================================
  75.         # String used to join multi-line comments together.
  76.         #==========================================================================
  77.        
  78.         LINEBREAK = "\r\n"
  79.        
  80.         #==========================================================================
  81.         # Instance variable used to store registered regexes.
  82.         #==========================================================================
  83.        
  84.         class << self
  85.             attr_reader :registered
  86.         end
  87.        
  88.         @registered = {}
  89.        
  90.         #==========================================================================
  91.         # Call this in your script to register your regex and its capture groups.
  92.         # Make sure to pick an unused symbol as key.
  93.         #==========================================================================
  94.        
  95.         def self.register(key, regex, *captures)
  96.             @registered[key] = [regex, captures]
  97.         end
  98.        
  99.         #==========================================================================
  100.         # Gets all matches for a registered regex on an event.
  101.         #==========================================================================
  102.        
  103.         def self.get_all(event_id, key)
  104.             $game_map.events[event_id].iavra_event_tags[key] || []
  105.         end
  106.        
  107.         #==========================================================================
  108.         # Get one match for a registered regex on an event.
  109.         #==========================================================================
  110.        
  111.         def self.get(event_id, key, index = 0)
  112.             get_all(event_id, key)[index]
  113.         end
  114.        
  115.     end
  116. end
  117.  
  118. #==============================================================================
  119. # ▼ Game_Event
  120. #==============================================================================
  121.  
  122. class Game_Event < Game_Character
  123.    
  124.     attr_reader :iavra_event_tags
  125.    
  126.     alias :iavra_event_tags_setup_page :setup_page
  127.    
  128.     #============================================================================
  129.     # Parses the event's notetags, whenever a page becomes active.
  130.     #============================================================================
  131.    
  132.     def setup_page(*args)
  133.         iavra_event_tags_setup_page(*args)
  134.         iavra_event_tags_parse_notetags
  135.     end
  136.    
  137.     #============================================================================
  138.     # First collects all comments on the current page and afterwards iterates
  139.     # over them, matching each comment against the registered regexes and storing
  140.     # the captured groups.
  141.     #============================================================================
  142.    
  143.     def iavra_event_tags_parse_notetags
  144.         comments = (@page ? @page.list : []).reduce([]){|a, c|
  145.             a << c.parameters[0] if [108, 408].include?(c.code); a
  146.         }.join(IAVRA::EVENT_TAGS::LINEBREAK)
  147.         @iavra_event_tags = Hash[IAVRA::EVENT_TAGS.registered.map{|k, d|
  148.             [k, comments.scan(d[0]).map{d[1].map.with_index(1){|k, i|
  149.                 v = $~[i]; v ? IAVRA::EVENT_TAGS::CAPTURES[k].call(v) : nil
  150.             }}]
  151.         }]
  152.     end
  153.    
  154. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement