Advertisement
Zeriab

[RGSS3] Map copy WIP

Feb 25th, 2014
335
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 3.27 KB | None | 0 0
  1. module MapUtility
  2.   def self.copy_map(copy_mid, terrain_tag, ox = 0, oy = 0)
  3.     data = $game_map.data
  4.     last_id = $game_map.events.values.max {|e1, e2| e1.id <=> e2.id}.id
  5.     last_id += 1
  6.     map = load_data(sprintf("Data/Map%03d.rvdata2", copy_mid))
  7.     map_modified = false
  8.    
  9.     # Load map events
  10.     event_map = {}
  11.     for event in map.events.values
  12.       key = [event.x, event.y]
  13.       event_map[key] = [] unless event_map.include?(key)
  14.       event_map[key] << event
  15.     end
  16.    
  17.     # Load map data
  18.     mapdata = map.data
  19.     for copy_mx in 0...mapdata.xsize
  20.       for copy_my in 0...mapdata.ysize
  21.         game_mx = copy_mx + ox
  22.         game_my = copy_my + oy
  23.         if mapdata[copy_mx, copy_my, 3] >> 8 == terrain_tag &&
  24.             0 <= game_mx && game_mx < data.xsize &&
  25.             0 <= game_my && game_my < data.ysize
  26.           # Register that we will copy over data
  27.           map_modified = true
  28.           # Verbatim copy of first three layers
  29.           for layer in 0..2 do
  30.             data[game_mx, game_my, layer] = mapdata[copy_mx, copy_my, layer]
  31.           end
  32.           # Merge region id from current map with shadow data from copy map
  33.           tag = data[game_mx, game_my, 3] - (data[game_mx, game_my, 3] % 256)
  34.           data[game_mx, game_my, 3] = mapdata[copy_mx, copy_my, 3] % 256 + tag
  35.           # Copy event
  36.           event_key = [copy_mx, copy_my]
  37.           if event_map.include?(event_key)
  38.             for event in event_map[event_key]
  39.               event.id = last_id + event.id
  40.               event.x += ox
  41.               event.y += oy
  42.               $game_map.create_game_event(event)
  43.             end
  44.           end
  45.         end
  46.       end
  47.     end
  48.    
  49.     if map_modified
  50.       # This map is no longer safe to reload when loading
  51.       $game_map.data_modifications = true
  52.       # Refresh sprites
  53.       SceneManager.scene.instance_eval("@spriteset.reload_tilemap")
  54.     end
  55.   end
  56. end
  57.  
  58. ##
  59. # Integration
  60. #
  61. class Spriteset_Map
  62.   def reload_tilemap
  63.     @tilemap.map_data = $game_map.data
  64.     load_tileset
  65.     create_new_characters
  66.     refresh_characters
  67.   end
  68.  
  69.   def create_new_characters
  70.     return unless $game_map && $game_map.new_events
  71.     for event in $game_map.new_events
  72.       @character_sprites.push(Sprite_Character.new(@viewport1, event))
  73.     end
  74.     $game_map.new_events.clear
  75.   end
  76. end
  77.  
  78. class Game_Map
  79.   attr_accessor :data_modifications
  80.  
  81.   def new_events
  82.     @new_events ||= []
  83.     @new_events
  84.   end
  85.  
  86.   ##
  87.   # Create a new game event based on an RPG::Event instance
  88.   #
  89.   def create_game_event(event)
  90.     game_event = Game_Event.new(map_id, event)
  91.     new_events << game_event
  92.     events[event.id] = game_event
  93.   end
  94. end
  95.  
  96. ##
  97. # Change reload mechanism
  98. #
  99. module DataManager
  100.   class << self
  101.     unless self.method_defined?(:zeriab_copy_reload_map_if_updated)
  102.       alias_method(:zeriab_copy_reload_map_if_updated, :reload_map_if_updated)
  103.     end
  104.  
  105.     ##
  106.     # Reload must not occur when we have made any runtime data changes.
  107.     # Otherwise we may throw our changes away
  108.     #
  109.     def reload_map_if_updated
  110.       if !$game_map.data_modifications
  111.         # No copy changes so we are save to use the normal reload code
  112.         zeriab_copy_reload_map_if_updated
  113.       end
  114.     end
  115.   end
  116. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement