blucalm

Reproduce Events Bug Fix June 4 2013

Jun 4th, 2013
1,467
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 17.38 KB | None | 0 0
  1. #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  2. # ** Reproduce Events
  3. #    Author: Eshra
  4. #    First Version: 21 Sept. 2012
  5. #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  6. #
  7. #   Allows events to be loaded from other maps in your project and placed on
  8. #   the current map. Also adds functionality for persistent events which will
  9. #   stay on the map even if it is reloaded (switching maps or loading save
  10. #   files).
  11. #-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~-~
  12. #
  13. #------------------------------------------------------------------------------
  14. # * How to Use
  15. #------------------------------------------------------------------------------
  16. # Use the EventSpawn.spawn_event(...) to place an event on the map, see source
  17. # the parameters it takes are:
  18. #
  19. # spawn_from_map_id - the id of the map the event will be spawned from
  20. # event_id - the id of the event to spawn
  21. # x - the x coordinate to place the event
  22. # y - the y coordinate to place the event
  23. # persistent - if the event is persistant
  24. # init_dir - currently not used
  25. # opts = {} - an optional hash representing the self switches that will be on when
  26. #             the event is spawned
  27. #
  28. # Example: EventSpawn.spawn_event(2,15,$game_player.x+1, $game_player.y, false,2)
  29. #
  30. # Heavily modified aliases:
  31. # straighten inside Game_CharacterBase
  32. # events are now "straightened" to correspond to their original pattern, not 1
  33. #
  34. # tileset_bitmap inside Sprite_Character
  35. # now checks if the character has a specific map which the bitmap should be
  36. # loaded from.
  37. #
  38.  
  39. # Original Release Date: 7 Nov.2012
  40. #
  41. # Update Log
  42. # June 4, 2013, Corrected alias to move_straight random event routes work now
  43. # Novemeber 18, 2012: v0.2, Events can now be spawned by name.
  44. # October 28, 2012: Can now remove spawned events.
  45. # October 27, 2012: Finished adding support for persistent events, made code
  46. #                   more extendible. Added code to allow use of tile graphics.
  47. #
  48. # Sepetember 21, 2012: Code reorganized made into it's own script for portability.
  49. #
  50. # August 2012: Version 0.0 completed
  51.  
  52. ($imported ||= {})["Ra Event Spawn"] = 0.2
  53.  
  54. #==============================================================================
  55. # * EventSpawn
  56. #==============================================================================
  57. module EventSpawn
  58.   #----------------------------------------------------------------------------
  59.   # * Constants
  60.   #----------------------------------------------------------------------------
  61.   module Constants
  62.     SS = ['A','B','C','D']
  63.     Tiles_On = true # can use tilesets from offscreen maps as event graphics
  64.   end
  65.   #----------------------------------------------------------------------------
  66.   # * Spawn Event
  67.   #----------------------------------------------------------------------------
  68.   def self.spawn_event(spawn_from_map_id, _event_id, _x, _y, persistent, init_dir, opts = {}, name = nil)
  69.     return unless SceneManager.scene_is?(Scene_Map)
  70.  
  71.     return if _event_id.nil? || spawn_from_map_id.nil?
  72.    
  73.     options = { :A => false, :B => false, :C => false, :D => false}.merge(opts)
  74.     map = load_data(sprintf("Data/Map%03d.rvdata2", spawn_from_map_id))
  75.     event_id = next_id
  76.    
  77.     _event_id = name.nil? ? _event_id : map.event_from_name(name).id # get from name if passed
  78.  
  79.     preprocessed_event = map.events[_event_id]
  80.  
  81.     preprocessed_event.id = event_id
  82.     event = create_event(preprocessed_event, event_id)
  83.  
  84.     event.gen_from_map_id = spawn_from_map_id
  85.     event.moveto(_x, _y)
  86.     add_to_spmap(event)
  87.    
  88.     event.persistent = persistent
  89.     persistent ? store_pev(event) : $game_map.spawned_event_ids.push(event_id)
  90.     $game_map.need_refresh = true
  91.    
  92.     EventSpawn::Constants::SS.each{|letter|
  93.       is_on = options[letter.to_sym]
  94.       $game_self_switches[[$game_map.map_id, event_id, letter]] = is_on
  95.     }
  96.     return event
  97.   end # End - spawn_event
  98.  
  99.   def self.next_id
  100.     max_id = $game_system.max_evn_id
  101.     keys = $game_map.events.keys
  102.     id = keys==[] ? 1 : keys.max + 1
  103.     max_id = id > max_id ? max_id = id : max_id+1
  104.    
  105.     $game_system.max_evn_id = max_id # store the largest event id used so far
  106.     return max_id
  107.   end
  108.   #----------------------------------------------------------------------------
  109.   # * Helper method
  110.   #----------------------------------------------------------------------------
  111.   def self.add_to_spmap(ev)
  112.     spm = SceneManager.scene.instance_eval('@spriteset')
  113.     sp_map_sps = spm.instance_eval('@character_sprites')
  114.     sp_map_sps.push(Sprite_Character.new(spm.instance_eval('@viewport1'), ev))
  115.   end
  116.   #----------------------------------------------------------------------------
  117.   # * Create Event
  118.   #----------------------------------------------------------------------------
  119.   def self.create_event(preprocessed_event, event_id)
  120.     $game_map.events[event_id] = Game_Event.new($game_map.map_id, preprocessed_event)
  121.   end
  122.   #----------------------------------------------------------------------------
  123.   # * Process Persistent Events
  124.   #----------------------------------------------------------------------------
  125.   def self.store_pev(event)
  126.     sys = $game_system
  127.     $game_map.spawned_event_ids.push(event.id) # note: ** 1
  128.     (sys.persistent_events[$game_map.map_id] ||= []).push(event) # Set persistance data
  129.    
  130.     # Update the wrapped event's coordinates for reloading
  131.     event.event.x = event.x
  132.     event.event.y = event.y
  133.   end # store_pev
  134.   #----------------------------------------------------------------------------
  135.   # * Set all selfswitches to false for the event on the current map
  136.   #----------------------------------------------------------------------------
  137.   def self.clean_self_switches(event_id)
  138.     map_id = $game_map.map_id
  139.     EventSpawn::Constants::SS.each{ |ch|
  140.       $game_self_switches[[map_id,event_id,ch]] = false
  141.     }
  142.   end
  143. end # EventSpawn
  144.  
  145. #==============================================================================
  146. # * Game_System
  147. #==============================================================================
  148. class Game_System
  149.  
  150.   attr_accessor :persistent_events #stores persistent events information
  151.   attr_accessor :max_evn_id # save the largest id used so far to prevent using
  152.                               # the save id twice.
  153.   #----------------------------------------------------------------------------
  154.   # * Alias initialize
  155.   #----------------------------------------------------------------------------
  156.   alias reprod_evs_initialize_event_hash initialize
  157.   def initialize
  158.     reprod_evs_initialize_event_hash
  159.     @persistent_events = {}
  160.     @max_evn_id = 0
  161.   end # End - initialize
  162.   #----------------------------------------------------------------------------
  163.   # * Alias on_after_load
  164.   #----------------------------------------------------------------------------
  165.   alias rep_evs_on_after_load on_after_load
  166.   def on_after_load
  167.     $game_map.remove_temporary_spawned_events #remove previously spawned events
  168.     rep_evs_on_after_load
  169.   end # on_after_load
  170.  
  171. end # Game_System
  172.  
  173. #==============================================================================
  174. # * Game_Map
  175. #==============================================================================
  176. class Game_Map
  177.   attr_reader :spawned_event_ids
  178.   attr_accessor :spawned_event_ids
  179.   attr_accessor :events
  180.   #----------------------------------------------------------------------------
  181.   # * Alias setup
  182.   #----------------------------------------------------------------------------
  183.   alias reprod_evs_game_map_setup setup
  184.   def setup(map_id)
  185.     $game_map.clean_self_switches
  186.     @spawned_event_ids = Array.new #the ids of the events that have been spawned
  187.     reprod_evs_game_map_setup(map_id)
  188.   end # End - setup
  189.   #----------------------------------------------------------------------------
  190.   # * Alias setup_events
  191.   #----------------------------------------------------------------------------
  192.   alias ra_dep_setup_evs_al_meth setup_events
  193.   def setup_events
  194.     ra_dep_setup_evs_al_meth
  195.    
  196.     # Correct the self switches if there are persistent events on the map
  197.     correct_self_switches if $game_system.persistent_events[@map_id] != nil
  198.   end # End - setup_events
  199.   #----------------------------------------------------------------------------
  200.   # * Correct Self Switches
  201.   #     Corrects $selfswitches hash so that they correctly correlate to the events'
  202.   #     ids upon reloading the map (The ids may change b/c of removed events).
  203.   #----------------------------------------------------------------------------
  204.   def correct_self_switches
  205.     p_events = $game_system.persistent_events[@map_id]
  206.     new_self_switches = {}     # the new global self switch hash
  207.     loaded_events = []
  208.     @spawned_event_ids = []    # reset spawned events
  209.     p_events.each{ |ev|
  210.        
  211.         #initialize new id and self_switches
  212.         new_event_id = @events.keys.max + 1
  213.         new_self_switches[new_event_id] = get_self_switches(ev.id)
  214.        
  215.         ev.event.id = new_event_id # update interior events id
  216.        
  217.         loaded_event = Game_Event.new(@map_id, ev.event)
  218.        
  219.         #update extra unhandled information for the new event
  220.         loaded_event.persistent = true
  221.        
  222.         #place the new event in the map's event hash
  223.         @events[loaded_event.id] = loaded_event
  224.        
  225.         loaded_events.push(loaded_event)
  226.         @spawned_event_ids.push(loaded_event.id) #update spawned events
  227.       } # End - p_events.each
  228.      
  229.       #update the persistent_events hash with the new information
  230.       $game_system.persistent_events[@map_id] = loaded_events
  231.      
  232.       local_ss = $game_self_switches
  233.       new_self_switches.keys.each{|id|
  234.         count = 0
  235.         EventSpawn::Constants::SS.each{ |letter|
  236.           $game_self_switches[[@map_id,id,letter]] = new_self_switches[id][count]
  237.           count += 1
  238.         }
  239.       }
  240.      
  241.       # Refresh the events so they are placed on the map on the correct page
  242.       # also reset the ids of the events inside @spawned_event_ids
  243.       @events.keys.each{ |id|
  244.         @events[id].refresh
  245.       }
  246.       refresh_tile_events
  247.   end # End - correct_self_switches
  248.   #----------------------------------------------------------------------------
  249.   # * Get Self Switches
  250.   #     returns an array of the selfswitches for the given event id and then
  251.   #     deletes those values from $game_self_switches.
  252.   #----------------------------------------------------------------------------
  253.   def get_self_switches(event_id)
  254.     local_ss = $game_self_switches
  255.     vals = []
  256.     EventSpawn::Constants::SS.each{ |letter|
  257.       vals.push(local_ss[[@map_id, event_id, letter]])
  258.       local_ss[[@map_id, event_id, letter]] = false
  259.     }
  260.     vals
  261.   end # End - get_self_switches
  262.   #----------------------------------------------------------------------------
  263.   # * Clean Self Switches
  264.   #     self-switches are handled globally, not on an event by event basis
  265.   #     so the global hash needs to be cleaned out of all the temporarily
  266.   #     spawned events when changing maps/loading the game, etc.
  267.   #----------------------------------------------------------------------------
  268.   def clean_self_switches
  269.     local_ss = $game_self_switches
  270.    
  271.     return if @spawned_event_ids == nil
  272.     @spawned_event_ids.each{ |id|
  273.       if !@events[id].persistent #don't clean up persistant event self switches
  274.         EventSpawn::Constants::SS.each{ |letter|
  275.           local_ss[[@map_id, id, letter]] = false #reset self_switches of temp events
  276.         }
  277.       end # End if
  278.     }
  279.   end # End - clean_self_switches
  280.   #----------------------------------------------------------------------------
  281.   # Removes all of the nonpersistent spawned events from the current map
  282.   #----------------------------------------------------------------------------
  283.   def remove_temporary_spawned_events
  284.     return if @spawned_event_ids.nil?
  285.     clean_self_switches
  286.     clean_temp_evs_helper
  287.     need_refresh = true
  288.   end # End - remove_temporary_spawned_events
  289.   #----------------------------------------------------------------------------
  290.   # * Cleans out temporary events
  291.   #----------------------------------------------------------------------------
  292.   def clean_temp_evs_helper
  293.     @spawned_event_ids.each{|id| @events.delete(id) unless @events[id].persistent}
  294.     @spawned_event_ids = []
  295.   end # End - clean_temp_evs_helper
  296.  
  297.   #----------------------------------------------------------------------------
  298.   # * Removes an event from the current map, including persistant events
  299.   #----------------------------------------------------------------------------
  300.   def destroy_event_any(id)
  301.     return unless !(ev = @events[id]).nil?
  302.     EventSpawn.clean_self_switches(id)
  303.     ev.set_intp_repr_ev(0)
  304.    
  305.     ev.erase # to hide the graphic
  306.    
  307.     if persistants = $game_system.persistent_events[@map_id]
  308.       persistants.each_with_index{ |ev, i|
  309.         if ev.id == id
  310.           persistants.delete_at(i)
  311.           break
  312.         end
  313.       }
  314.     end
  315.    
  316.     @spawned_event_ids.delete(id)   # delete from array
  317.     @events.delete(id)              #   "     "   hash
  318.    
  319.     refresh
  320.   end
  321. end # End - Game_Map
  322.  
  323. #==============================================================================
  324. # * Game_Character
  325. #==============================================================================
  326. class Game_Character < Game_CharacterBase
  327.   attr_accessor :gen_from_map_id           # map the event was generated from
  328. end
  329.  
  330. #==============================================================================
  331. # * Game_Event
  332. #==============================================================================
  333. class Game_Event < Game_Character
  334.   attr_accessor :event                   # wrapped event
  335.   attr_accessor :id                      # id of this event
  336.   attr_accessor :persistent              # does event stay after switching maps
  337.  
  338.   #attr_accessor :init_dir                # used to specify direction with throw!
  339.   #----------------------------------------------------------------------------
  340.   # * Alias method initialize
  341.   #----------------------------------------------------------------------------
  342.   alias ra_extra_fields_dep_init initialize
  343.   def initialize(map_id, event)
  344.     ra_extra_fields_dep_init(map_id,event)
  345.     @persistent = false
  346.     #@init_dir = -1
  347.   end # End - initialize
  348.  
  349.   #--------------------------------------------------------------------------
  350.   # * Alias straighten - sets pattern to orig_pattern
  351.   #--------------------------------------------------------------------------
  352.   alias straight_ev_sp_ed_st_orig straighten
  353.   def straighten
  354.     straight_ev_sp_ed_st_orig
  355.     @pattern = @original_pattern ? @original_pattern : 1 if @walk_anime || @step_anime
  356.   end
  357.    
  358.   #--------------------------------------------------------------------------
  359.   # * Alias move_straight - Store updated postion for persistent events
  360.   #--------------------------------------------------------------------------
  361.   alias move_str_ed_fr_persis_support move_straight
  362.   def move_straight(dir, turn_ok = true)
  363.     move_str_ed_fr_persis_support(dir)
  364.     @event.x, @event.y = @x, @y if @persistent
  365.   end
  366.  
  367.   #--------------------------------------------------------------------------
  368.   # * Alias move_diagonal- Store updated postion for persistent events
  369.   #--------------------------------------------------------------------------
  370.   alias move_diag_ed_fr_pesis_support move_diagonal
  371.   def move_diagonal(horz, vert)
  372.     move_diag_ed_fr_pesis_support(horz, vert)
  373.     @event.x, @event.y = @x, @y if @persistent
  374.   end
  375.  
  376.   def set_intp_repr_ev(id)
  377.     @interpreter.set_ev_id_repr_ev(id) unless @interpreter.nil?
  378.   end
  379. end # Game_Event
  380.  
  381. #==============================================================================
  382. # * Sprite_Character
  383. #==============================================================================
  384. class Sprite_Character < Sprite_Base
  385.  
  386.   #--------------------------------------------------------------------------
  387.   # * Alias tileset_bitmap - load tiles from offscreen maps
  388.   #--------------------------------------------------------------------------
  389.   alias ts_bm_sprt_fr_offscr_maps tileset_bitmap
  390.   def tileset_bitmap(tile_id)
  391.    
  392.     if (id = @character.gen_from_map_id) && id > 0
  393.        map = load_data(sprintf("Data/Map%03d.rvdata2", id))
  394.       return Cache.tileset($data_tilesets[map.tileset_id].tileset_names[5 + tile_id / 256])
  395.     else
  396.       return ts_bm_sprt_fr_offscr_maps(tile_id)
  397.     end
  398.    
  399.   end # tileset_bitmap
  400.  
  401. end # Sprite_Character
  402. #==============================================================================
  403. # ** Game_Interpreter
  404. #==============================================================================
  405. class Game_Interpreter
  406.   def set_ev_id_repr_ev(id)
  407.     @event_id = id
  408.   end # set_ev_id_repr_ev
  409. end # Game_Interpreter
  410. #==============================================================================
  411. # ** RPG::Map
  412. #==============================================================================
  413. class RPG::Map
  414.   #----------------------------------------------------------------------------
  415.   # * Get Event From Name
  416.   #----------------------------------------------------------------------------
  417.   def event_from_name(name)
  418.     @events.values.each{|ev| return ev if ev.name.eql?(name)}
  419.     return nil
  420.   end
  421. end # RPG::Map
  422. #End of File
Advertisement
Add Comment
Please, Sign In to add comment