blucalm

Reproduce Events import change

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