Advertisement
Zeriab

[RGSS3] Map copy WIP

Mar 16th, 2014
326
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 4.14 KB | None | 0 0
  1. ####
  2. # License Terms
  3. ##
  4. # Copyright (c) 2014 Zeriab
  5. #
  6. # This software is provided 'as-is', without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software.
  7. # Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions:
  8. #     1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required.
  9. #     2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software.
  10. #     3. This notice may not be removed or altered from any source distribution.
  11. #
  12. module MapUtility
  13.   def self.copy_map(copy_mid, terrain_tag, ox = 0, oy = 0)
  14.     data = $game_map.data
  15.     last_id = $game_map.events.values.max {|e1, e2| e1.id <=> e2.id}.id
  16.     last_id += 1
  17.     map = load_data(sprintf("Data/Map%03d.rvdata2", copy_mid))
  18.     map_modified = false
  19.    
  20.     # Load map events
  21.     event_map = {}
  22.     for event in map.events.values
  23.       key = [event.x, event.y]
  24.       event_map[key] = [] unless event_map.include?(key)
  25.       event_map[key] << event
  26.     end
  27.    
  28.     # Load map data
  29.     mapdata = map.data
  30.     for copy_mx in 0...mapdata.xsize
  31.       for copy_my in 0...mapdata.ysize
  32.         game_mx = copy_mx + ox
  33.         game_my = copy_my + oy
  34.         if mapdata[copy_mx, copy_my, 3] >> 8 == terrain_tag &&
  35.             0 <= game_mx && game_mx < data.xsize &&
  36.             0 <= game_my && game_my < data.ysize
  37.           # Register that we will copy over data
  38.           map_modified = true
  39.           # Verbatim copy of first three layers
  40.           for layer in 0..2 do
  41.             data[game_mx, game_my, layer] = mapdata[copy_mx, copy_my, layer]
  42.           end
  43.           # Merge region id from current map with shadow data from copy map
  44.           tag = data[game_mx, game_my, 3] - (data[game_mx, game_my, 3] % 256)
  45.           data[game_mx, game_my, 3] = mapdata[copy_mx, copy_my, 3] % 256 + tag
  46.           # Copy event
  47.           event_key = [copy_mx, copy_my]
  48.           if event_map.include?(event_key)
  49.             for event in event_map[event_key]
  50.               event.id = last_id + event.id
  51.               event.x += ox
  52.               event.y += oy
  53.               $game_map.create_game_event(event)
  54.             end
  55.           end
  56.         end
  57.       end
  58.     end
  59.    
  60.     if map_modified
  61.       # This map is no longer safe to reload when loading
  62.       $game_map.data_modifications = true
  63.       # Refresh sprites
  64.       SceneManager.scene.instance_eval("@spriteset.reload_tilemap")
  65.     end
  66.   end
  67. end
  68.  
  69. ##
  70. # Integration
  71. #
  72. class Spriteset_Map
  73.   def reload_tilemap
  74.     @tilemap.map_data = $game_map.data
  75.     load_tileset
  76.     create_new_characters
  77.     refresh_characters
  78.   end
  79.  
  80.   def create_new_characters
  81.     return unless $game_map && $game_map.new_events
  82.     for event in $game_map.new_events
  83.       @character_sprites.push(Sprite_Character.new(@viewport1, event))
  84.     end
  85.     $game_map.new_events.clear
  86.   end
  87. end
  88.  
  89. class Game_Map
  90.   attr_accessor :data_modifications
  91.  
  92.   def new_events
  93.     @new_events ||= []
  94.     @new_events
  95.   end
  96.  
  97.   ##
  98.   # Create a new game event based on an RPG::Event instance
  99.   #
  100.   def create_game_event(event)
  101.     game_event = Game_Event.new(map_id, event)
  102.     new_events << game_event
  103.     events[event.id] = game_event
  104.   end
  105. end
  106.  
  107. ##
  108. # Change reload mechanism
  109. #
  110. module DataManager
  111.   class << self
  112.     unless self.method_defined?(:zeriab_copy_reload_map_if_updated)
  113.       alias_method(:zeriab_copy_reload_map_if_updated, :reload_map_if_updated)
  114.     end
  115.  
  116.     ##
  117.     # Reload must not occur when we have made any runtime data changes.
  118.     # Otherwise we may throw our changes away
  119.     #
  120.     def reload_map_if_updated
  121.       if !$game_map.data_modifications
  122.         # No copy changes so we are save to use the normal reload code
  123.         zeriab_copy_reload_map_if_updated
  124.       end
  125.     end
  126.   end
  127. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement