Solistra

SES Event Memory

Dec 29th, 2012
288
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 15.45 KB | None | 0 0
  1. #═╦═════════════════════════════════════════════════════════════════════════════
  2. # ║ § Event Memory (1.0) by Solistra                     [License: CC BY-SA 3.0]
  3. # ║                                                            <RMVX Ace>
  4. #═╬═════════════════════════════════════════════════════════════════════════════
  5. # ║ § Change Log
  6. #─╫─────────────────────────────────────────────────────────────────────────────
  7. # ║ v1.0 (December 29th, 2012) - Initial release
  8. # ║
  9. #═╬═════════════════════════════════════════════════════════════════════════════
  10. # ║ § Summary
  11. #─╫─────────────────────────────────────────────────────────────────────────────
  12. # ║ This script allows events to remember their positions on the map, even
  13. # ║ through map changes. This script also provides a way to permanently erase
  14. # ║ events (unlike the VX Ace default, which only erases events temporarily).
  15. # ║
  16. #═╬═════════════════════════════════════════════════════════════════════════════
  17. # ║ § Required Scripts
  18. #─╫─────────────────────────────────────────────────────────────────────────────
  19. # ║ None.
  20. # ║
  21. #═╬═════════════════════════════════════════════════════════════════════════════
  22. # ║ § Known Incompatibilities
  23. #─╫─────────────────────────────────────────────────────────────────────────────
  24. # ║ None.
  25. # ║
  26. #═╬═════════════════════════════════════════════════════════════════════════════
  27. # ║ § Installation
  28. #─╫─────────────────────────────────────────────────────────────────────────────
  29. # ║ Place this script below Materials, but above Main -- preferably relatively
  30. # ║ high in your list of custom scripts.
  31. # ║
  32. #═╬═════════════════════════════════════════════════════════════════════════════
  33. # ║ § Configuration
  34. #─╫─────────────────────────────────────────────────────────────────────────────
  35. # ║ This script only has three very simple customization options: REMEMBER_ALL,
  36. # ║ REMEMBER_TAG, and EXCLUDE_TAG. The REMEMBER_ALL constant may be set to one
  37. # ║ of "true" or "false" (without quotes). If it is true, all events will have
  38. # ║ their positions remembered automatically unless otherwise excluded -- if it
  39. # ║ is false, events will only remember their positions if explicitly told to
  40. # ║ do so.
  41. # ║
  42. # ║ You can also configure the tags used to determine whether or not an event
  43. # ║ should remember its position with the REMEMBER_TAG and EXCLUDE_TAG options.
  44. # ║ An event is tagged if it includes the value of one of these constants in its
  45. # ║ name.
  46. # ║
  47. #═╬═════════════════════════════════════════════════════════════════════════════
  48. # ║ § Script Calls
  49. #─╫─────────────────────────────────────────────────────────────────────────────
  50. # ║ To permanently erase an event, use the erase! method like so:
  51. # ║   $game_map.events[event_id].erase!
  52. # ║
  53. # ║ You can also check if an event has been erased or permanently erased with
  54. # ║ the script calls erased? or permanently_erased? respectively.
  55. # ║
  56. # ║ In addition to this, you can also restore an event that has been previously
  57. # ║ erased (whether permanently or otherwise) with the restore method:
  58. # ║   $game_map.events[event_id].restore
  59. # ║
  60. #═╬═════════════════════════════════════════════════════════════════════════════
  61. # ║ § Aliased Methods
  62. #─╫─────────────────────────────────────────────────────────────────────────────
  63. # ║ ● Game_System
  64. # ║     initialize
  65. # ║ ● Game_Map
  66. # ║     setup_events
  67. # ║ ● Game_Event
  68. # ║     initialize
  69. # ║ ● Scene_Map
  70. # ║     pre_transfer
  71. # ║
  72. #═╬═════════════════════════════════════════════════════════════════════════════
  73. # ║ § New Methods
  74. #─╫─────────────────────────────────────────────────────────────────────────────
  75. # ║ ● Game_Event
  76. # ║     restore
  77. # ║     erase!
  78. # ║     erased?
  79. # ║     permanently_erased?
  80. # ║     name
  81. # ║
  82. #═╬═════════════════════════════════════════════════════════════════════════════
  83. # ║ ▼ module SES::Event_Extension
  84. #═╩═════════════════════════════════════════════════════════════════════════════
  85. module SES module Event_Memory
  86.   #═╦═══════════════════════════════════════════════════════════════════════════
  87.   # ║ α BEGIN CONFIGURATION
  88.   #═╩═══════════════════════════════════════════════════════════════════════════
  89.   # Set this to "true" if you want the variables of all events to be stored by
  90.   # default.
  91.   REMEMBER_ALL = true
  92.  
  93.   # Tag events to remember if REMEMBER_ALL has been set to "false" by including
  94.   # this string in the event's name.
  95.   REMEMBER_TAG = "<R>"
  96.  
  97.   # Likewise, tag events to exclude from memory if REMEMBER_ALL has been set to
  98.   # "true" with this string.
  99.   EXCLUDE_TAG = "<E>"
  100.   #═╦═══════════════════════════════════════════════════════════════════════════
  101.   # ║ Ω END CONFIGURATION
  102.   #═╩═══════════════════════════════════════════════════════════════════════════
  103.   # A hash of reader methods for Game_Event objects that are used to remember
  104.   # their values. Used by the self.memory_variables method.
  105.   @event_memory = {x: :x, y: :y, dir: :direction, erased: :permanently_erased?}
  106.  
  107.   # Method used in Scene_Map's pre_transfer method to store event variables in
  108.   # $game_system.events.
  109.   def self.store_event_variables(map_id)
  110.     $game_map.events.values.each do |event|
  111.       if storage_requirements_met?(event)
  112.         ($game_system.events[map_id] ||= {})[event.id] = memory_variables(event)
  113.       end
  114.     end
  115.   end
  116.  
  117.   # Determine whether or not to store variables for the passed event.
  118.   def self.storage_requirements_met?(event)
  119.     return true if !REMEMBER_ALL && event.name.include?(REMEMBER_TAG)
  120.     return true if REMEMBER_ALL && !event.name.include?(EXCLUDE_TAG)
  121.     return true if event.permanently_erased?
  122.     return false
  123.   end
  124.  
  125.   # Used to get the variables that are remembered for events.
  126.   def self.memory_variables(event)
  127.     hash = {}; @event_memory.each{|k, v| hash[k] = event.send(v)}
  128.     return hash
  129.   end
  130. end end
  131. #═╦═════════════════════════════════════════════════════════════════════════════
  132. # ║ ▲ module SES::Event_Memory
  133. #═╩═════════════════════════════════════════════════════════════════════════════
  134.                 ($imported ||= {})["SES - Event Memory"] = 1.0
  135. #═╦═════════════════════════════════════════════════════════════════════════════
  136. # ║ ▼ class Game_System
  137. #═╩═════════════════════════════════════════════════════════════════════════════
  138. class Game_System
  139.   attr_accessor :events
  140.  
  141.   # Aliased to initialize the new instance variable @events, which stores the
  142.   # x, y, and direction values for events.
  143.   alias :ses_evmem_gs_init :initialize
  144.   def initialize(*a, &b) ses_evmem_gs_init(*a, &b); @events = {} end
  145. end
  146. #═╦═════════════════════════════════════════════════════════════════════════════
  147. # ║ ▲ class Game_System
  148. #─╫─────────────────────────────────────────────────────────────────────────────
  149. # ║ ▼ class Game_Map
  150. #═╩═════════════════════════════════════════════════════════════════════════════
  151. class Game_Map
  152.   # Aliased to move events to their remembered positions if the positions have
  153.   # been stored in $game_system.events and to erase permanently erased events.
  154.   alias :ses_evmem_gm_setupev :setup_events
  155.   def setup_events(*args, &block)
  156.     ses_evmem_gm_setupev(*args, &block)
  157.     $game_system.events[@map_id].each do |id, vars|
  158.       @events[id].erase! if vars[:erased] == true
  159.       @events[id].moveto(vars[:x], vars[:y])
  160.       @events[id].set_direction(vars[:dir])
  161.     end if $game_system.events[@map_id]
  162.   end
  163. end
  164. #═╦═════════════════════════════════════════════════════════════════════════════
  165. # ║ ▲ class Game_Map
  166. #─╫─────────────────────────────────────────────────────────────────────────────
  167. # ║ ▼ class Game_Event
  168. #═╩═════════════════════════════════════════════════════════════════════════════
  169. class Game_Event < Game_Character
  170.   # Aliased to initialize the new instance variable @permanently_erased.
  171.   alias :ses_evmem_ge_init :initialize
  172.   def initialize(*args, &block)
  173.     ses_evmem_ge_init(*args, &block)
  174.     @permanently_erased = false
  175.   end
  176.  
  177.   # Restores an event that has been erased (permanently or otherwise).
  178.   def restore
  179.     @erased, @permanently_erased = false, false
  180.     refresh
  181.   end
  182.  
  183.   # Permanently erases a method.
  184.   def erase!() @permanently_erased = true; erase end
  185.  
  186.   # Convenience methods to determine if an event has been erased (permanently or
  187.   # otherwise) and to get the event's name.
  188.   def erased?() @erased end
  189.   def permanently_erased?() @permanently_erased end
  190.   def name() @event.name end
  191. end
  192. #═╦═════════════════════════════════════════════════════════════════════════════
  193. # ║ ▲ class Game_Event
  194. #─╫─────────────────────────────────────────────────────────────────────────────
  195. # ║ ▼ class Scene_Map
  196. #═╩═════════════════════════════════════════════════════════════════════════════
  197. class Scene_Map < Scene_Base
  198.   # Aliased to remember event positions on the current map before the next
  199.   # one is loaded.
  200.   alias :ses_evmem_sm_pre :pre_transfer
  201.   def pre_transfer(*args, &block)
  202.     SES::Event_Memory.store_event_variables($game_map.map_id)
  203.     ses_evmem_sm_pre(*args, &block)
  204.   end
  205. end
  206. #═╦═════════════════════════════════════════════════════════════════════════════
  207. # ║ ▲ class Scene_Map
  208. #═╩═════════════════════════════════════════════════════════════════════════════
Advertisement
Add Comment
Please, Sign In to add comment