blucalm

Reproduce Events bug fix 5 Dec 2012

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