DerTraveler

Persistent Map Events v2.0

Sep 9th, 2013
624
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 15.09 KB | None | 0 0
  1. #==============================================================================
  2. #
  3. # "Persistent Map Events"
  4. # Version 2.0
  5. # Last Update: September 9th, 2013
  6. # Author: DerTraveler (dertraveler [at] gmail.com)
  7. #
  8. #==============================================================================
  9. #
  10. # Description:
  11. #
  12. # Adds the possibility to make map events persistent between map and/or page
  13. # changes. That means that you can leave a map (change a page) and when you
  14. # return it will be in the exact same state as last time.
  15. # This includes following properties:
  16. #  - position
  17. #  - direction
  18. #  - position in the current move route
  19. #  - changes to the events appearance by the "Set Move Route..." command
  20. #  - changes to the movement style (speed, frequency) by the "Set Move Route..."
  21. #    command
  22. #  - execution progress of parallel events
  23. #
  24. # It is also possible to only make some of these properties persistents by
  25. # using notetags on event pages.
  26. #
  27. # NOTE #1: Autostart events are NEVER saved.
  28. #
  29. # NOTE #2: All the changes are also included in your save files. Thus, old save
  30. #          files probably won't be compatible anymore.
  31. #
  32. # NOTE #3: Even though I don't think that this will be any problem with today's
  33. #          hardware, you should note that the event data of potentially every
  34. #          map event the player has met is stored in memory and in your save
  35. #          files. If you have exceptionally many events in your game, this could
  36. #          become a problem.
  37. #
  38. #==============================================================================
  39. #
  40. # How to use:
  41. #
  42. # Paste it anywhere in the Materials section.
  43. # Be sure to set the default behaviour in the options section.
  44. #
  45. #------------------------------------------------------------------------------
  46. # Feature: Set persistence of single events pages with comment notetags
  47. #------------------------------------------------------------------------------
  48. # If you want to overwrite the standard persistence behaviour of a particular
  49. # event page, just add one or more of the following notetags into a Comment-Box.
  50. #
  51. #   <persistence [on/off]>
  52. #     Activates or deactivates the persistence of this event. If activated the
  53. #     behaviour is as defined in the standard behaviour configuration.
  54. #
  55. #   <persistent [property 1] ([property 2]...)>
  56. #     Saves the given properties in addition to the basic behaviour (either
  57. #     given by configuration or by the persistence-notetag)
  58. #
  59. #   <transient [property 1] ([property 2]...)>
  60. #     Prevents the given properties from being saved.
  61. #     This modifies the basic behaviour given by configuration or by the
  62. #     persistence-notetag.
  63. #     This also overrides the properties given in a persistent-notetag.
  64. #
  65. #   List of properties:
  66. #     position    - the event position (map change only)
  67. #     direction   - the event direction
  68. #     move_route  - the execution state of the move route
  69. #     appearance  - includes event graphic, transparent flag, opacity and
  70. #                   blending mode
  71. #     move_style  - includes move speed, move frequency, walking animation flag,
  72. #                   stepping animation flag, direction fix flag, through flag
  73. #     interpreter - the current execution progress of a parallel process event
  74. #     erase       - the erase flag
  75. #     all         - all of the above.
  76. #                   overwrites all other given properties in the notetag.
  77. #                  
  78. #
  79. #==============================================================================
  80. #
  81. # Changelog:
  82. #   2.0:
  83. #     - Major rewriting of the script. Added support for page changes. Now the
  84. #       state of every page is saved separately and will be restored
  85. #       accordingly.
  86. #     - Added own notetag code, so the dependency on EST Notetag Grabber was
  87. #       removed
  88. #     - Added more notetags, so that only particular properties will be saved
  89. #
  90. #==============================================================================
  91. #
  92. # Terms of use:
  93. #
  94. # - Free to use in any non-commercial project. For use in commercial projects
  95. #   please mail me.
  96. # - Please mail me if you find bugs so I can fix them.
  97. # - If you have feature requests, please also mail me, but I can't guarantee
  98. #   that I will add every requested feature.
  99. # - Credit DerTraveler in your project.
  100. #
  101. #==============================================================================
  102.  
  103. ###############################################################################
  104. # OPTIONS
  105. ###############################################################################
  106. module PersistentMapEvents
  107.  
  108.   CONFIG = {}
  109.   #---------------------------------------------------------------------------
  110.   # GLOBAL_PERSISTENCE
  111.   # Set this to true, if every map event should be persistent by default.
  112.   #---------------------------------------------------------------------------
  113.   CONFIG[:GLOBAL_PERSISTENCE] = false
  114.   #---------------------------------------------------------------------------
  115.   # PERSISTENT_PROPERTIES
  116.   # The list of properties that should be saved by default.
  117.   # This list applies when GLOBAL_PERSISTENCE is true and when the notetag
  118.   # <persistence on> is used.
  119.   #---------------------------------------------------------------------------
  120.   CONFIG[:PERSISTENT_PROPERTIES] = [:position, :direction, :move_route,
  121.                                     :appearance, :move_style, :interpreter]
  122.  
  123. end
  124.  
  125. ###############################################################################
  126. #
  127. # Do not edit past this point, if you have no idea, what you're doing ;)
  128. #
  129. ###############################################################################
  130.  
  131. module NotetagSupport
  132.  
  133.   def any_notetag?
  134.     !notetags.empty?
  135.   end
  136.  
  137.   def have_notetag?(name, *args)
  138.     notetags.keys.include?(name) &&
  139.     (args.empty? || args == notetags[name])
  140.   end
  141.  
  142.   def any_notetag_from?(names)
  143.     notetags.keys.any? { |k| names.include?(k) }
  144.   end
  145.  
  146.   def notetags
  147.     result = {}
  148.     note.scan(/<([^>]+)>/) { |match|
  149.       args = match[0].split
  150.       result[args[0]] = args[1..-1]
  151.     }
  152.     result
  153.   end
  154.  
  155. end
  156.  
  157. class Game_Event < Game_Character
  158.  
  159.   include NotetagSupport
  160.  
  161.   #-----------------------------------------------
  162.   # new method : note
  163.   #-----------------------------------------------
  164.   def note
  165.     return "" if !@page
  166.     all_comments = ""
  167.     @list.each { |command|
  168.       if command.code == 108 || command.code == 408
  169.         all_comments += command.parameters[0] + "\n"
  170.       end
  171.     }
  172.     return all_comments
  173.   end
  174.  
  175. end
  176.  
  177. #==============================================================================
  178. # Game_System
  179. #==============================================================================
  180. class Game_System
  181.  
  182.   attr_accessor :saved_events
  183.  
  184.   #-----------------------------------------------
  185.   # alias method : initialize
  186.   #-----------------------------------------------  
  187.   alias pme_initialize initialize
  188.   def initialize
  189.     pme_initialize
  190.     @saved_events = {}
  191.   end
  192.  
  193. end
  194.  
  195. #==============================================================================
  196. # Game_Event
  197. #==============================================================================
  198. class Game_Event < Game_Character
  199.  
  200.   ALL_PROPERTIES = [:position, :direction, :move_route, :appearance,
  201.                     :move_style, :interpreter, :erase]
  202.  
  203.   attr_accessor :persistent_properties
  204.  
  205.   #-----------------------------------------------
  206.   # overwrite method : clone
  207.   #-----------------------------------------------
  208.   def clone
  209.     result = super()
  210.     result.instance_variable_set("@interpreter",
  211.                                  @interpreter.clone) if @interpreter
  212.     result
  213.   end
  214.  
  215.   #-----------------------------------------------
  216.   # new method : page_id
  217.   #-----------------------------------------------
  218.   def page_id
  219.     @event.pages.index(@page)
  220.   end
  221.  
  222.   #-----------------------------------------------
  223.   # new method : save_state
  224.   #-----------------------------------------------
  225.   def save_state
  226.     @persistent_properties = determine_persistent_properties
  227.     $game_system.saved_events[[@map_id, @id]] = page_id
  228.     $game_system.saved_events[[@map_id, @id, page_id]] = self.clone
  229.   end
  230.  
  231.   #-----------------------------------------------
  232.   # new method : get_saved_state
  233.   #-----------------------------------------------
  234.   def get_saved_state
  235.     pid = $game_system.saved_events[[@map_id, @id]]
  236.     $game_system.saved_events[[@map_id, @id, pid]]
  237.   end
  238.  
  239.   #-----------------------------------------------
  240.   # new method : get_saved_page
  241.   #-----------------------------------------------
  242.   def get_saved_page
  243.     $game_system.saved_events[[@map_id, @id, page_id]]
  244.   end
  245.  
  246.   #-----------------------------------------------
  247.   # new method : has_saved_state?
  248.   #-----------------------------------------------
  249.   def has_saved_state?
  250.     $game_system.saved_events.has_key?([@map_id, @id])
  251.   end
  252.  
  253.   #-----------------------------------------------
  254.   # new method : has_saved_page?
  255.   #-----------------------------------------------
  256.   def has_saved_page?
  257.     $game_system.saved_events.has_key?([@map_id, @id, page_id])
  258.   end
  259.  
  260.   #-----------------------------------------------
  261.   # new method : persistent?
  262.   #-----------------------------------------------
  263.   def persistent?(property)
  264.     @persistent_properties.include?(property)
  265.   end
  266.  
  267.   def determine_persistent_properties
  268.     transient = have_notetag?("transient") ?
  269.                 notetags["transient"].map { |p| p.to_sym } : []
  270.     return [] if transient.include?(:all)
  271.  
  272.     standard = Array.new(PersistentMapEvents::CONFIG[:PERSISTENT_PROPERTIES])
  273.     persistent = have_notetag?("persistent") ?
  274.                  notetags["persistent"].map { |p| p.to_sym } : []
  275.    
  276.     if persistent.include?(:all)
  277.       result = Array.new(ALL_PROPERTIES)
  278.     else
  279.       result = ( PersistentMapEvents::CONFIG[:GLOBAL_PERSISTENCE] &&
  280.                  !have_notetag?("persistence", "off") ) ||
  281.                have_notetag?("persistence", "on") ? standard : []
  282.       result += persistent
  283.       result.uniq!
  284.     end
  285.    
  286.     return result - transient
  287.   end
  288.  
  289.   def restore_event
  290.     restored = get_saved_state
  291.    
  292.     @persistent_properties = restored.persistent_properties
  293.    
  294.     if persistent?(:position)
  295.       @x = restored.x
  296.       @y = restored.y
  297.       @real_x = x
  298.       @real_y = y
  299.       @bush_depth = restored.bush_depth
  300.     end
  301.    
  302.     if persistent?(:direction)
  303.       @direction = restored.direction
  304.     end
  305.    
  306.     if persistent?(:appearance)
  307.       @tile_id = restored.tile_id
  308.       @character_name = restored.character_name
  309.       @character_index = restored.character_index
  310.       @opacity = restored.opacity
  311.       @blend_type = restored.blend_type
  312.       @transparent = restored.transparent
  313.     end
  314.    
  315.     if persistent?(:move_style)
  316.       @move_speed = restored.move_speed
  317.       @move_frequency = restored.move_frequency
  318.       @walk_anime = restored.walk_anime
  319.       @step_anime = restored.step_anime
  320.       @direction_fix = restored.direction_fix
  321.       @through = restored.through
  322.     end
  323.    
  324.     if persistent?(:move_route)
  325.       @move_route_forcing = restored.move_route_forcing
  326.       @pattern = restored.pattern
  327.       @anime_count = restored.instance_variable_get("@anime_count")
  328.       @stop_count = restored.instance_variable_get("@stop_count")
  329.       @jump_count = restored.instance_variable_get("@jump_count")
  330.       @jump_peak = restored.instance_variable_get("@jump_peak")
  331.       @move_route = restored.instance_variable_get("@move_route")
  332.       @move_route_index = restored.instance_variable_get("@move_route_index")
  333.       @original_move_route = restored.instance_variable_get("@original_move_route")
  334.       @original_move_route_index = restored.instance_variable_get("@original_move_route_index")
  335.       @wait_count = restored.instance_variable_get("@wait_count")
  336.       @move_succeed = restored.instance_variable_get("@move_succeed")
  337.     end
  338.    
  339.     if persistent?(:erase)
  340.       @erased = restored.instance_variable_get("@erased")
  341.     end
  342.    
  343.     refresh
  344.   end
  345.  
  346.   def restore_page
  347.     restored = get_saved_page
  348.    
  349.     @persistent_properties = restored.persistent_properties
  350.    
  351.     if persistent?(:direction)
  352.       @direction          = restored.direction
  353.     end
  354.    
  355.     if persistent?(:appearance)
  356.       @tile_id            = restored.tile_id
  357.       @character_name     = restored.character_name
  358.       @character_index    = restored.character_index
  359.     end
  360.    
  361.     if persistent?(:move_style)
  362.       @move_speed         = restored.move_speed
  363.       @move_frequency     = restored.move_frequency
  364.       @walk_anime         = restored.walk_anime
  365.       @step_anime         = restored.step_anime
  366.       @direction_fix      = restored.direction_fix
  367.       @through            = restored.through
  368.     end
  369.    
  370.     if persistent?(:move_route)
  371.       @pattern            = restored.pattern
  372.       @move_route_forcing = restored.move_route_forcing
  373.       @move_route         = restored.instance_variable_get("@move_route")
  374.       @move_route_index   = restored.instance_variable_get("@move_route_index")
  375.       @original_move_route = restored.instance_variable_get("@original_move_route")
  376.       @original_move_route_index = restored.instance_variable_get("@original_move_route_index")
  377.       @wait_count = restored.instance_variable_get("@wait_count")
  378.     end
  379.    
  380.     if persistent?(:interpreter)
  381.       @interpreter        = restored.instance_variable_get("@interpreter")
  382.       @interpreter.create_fiber if @interpreter
  383.     end
  384.    
  385.     @original_direction = @page.graphic.direction    
  386.     @original_pattern   = @page.graphic.pattern
  387.   end
  388.  
  389.   #-----------------------------------------------
  390.   # alias method : initialize
  391.   #-----------------------------------------------
  392.   alias pme_initialize initialize
  393.   def initialize(map_id, event)
  394.     pme_initialize(map_id, event)
  395.     @persistent_properties = []
  396.    
  397.     restore_event if has_saved_state?
  398.   end
  399.  
  400.   #-----------------------------------------------
  401.   # alias method : setup_page
  402.   #-----------------------------------------------
  403.   alias pme_setup_page setup_page
  404.   def setup_page(new_page)
  405.     save_state if @page
  406.    
  407.     pme_setup_page(new_page)
  408.   end
  409.  
  410.   #-----------------------------------------------
  411.   # alias method : setup_page_settings
  412.   #-----------------------------------------------
  413.   alias pme_setup_page_settings setup_page_settings
  414.   def setup_page_settings
  415.     pme_setup_page_settings
  416.    
  417.     restore_page if has_saved_page?
  418.   end
  419.  
  420. end
  421.  
  422. #==============================================================================
  423. # Game_Player
  424. #==============================================================================
  425. class Game_Player < Game_Character
  426.  
  427.   #-----------------------------------------------
  428.   # alias method : perform_transfer
  429.   #-----------------------------------------------
  430.   alias pme_perform_transfer perform_transfer
  431.   def perform_transfer
  432.     if transfer?
  433.       $game_map.events.each { |id, event|
  434.         event.save_state
  435.       }
  436.     end
  437.     pme_perform_transfer
  438.   end
  439.  
  440. end
Advertisement
Add Comment
Please, Sign In to add comment