Vlue

Copy/Paste Map Data

Sep 16th, 2013
1,965
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 5.70 KB | None | 0 0
  1. #Copy/Paste Map Data v1.2
  2. #----------#
  3. #Features: Let's you copy and paste portions of one map to another! Fancy!
  4. #           And copy events around too!
  5. #
  6. #Usage:   map_copy("map_id",x,y,width,height)
  7. #
  8. #          Where... map_id is the id of the map as a string (including 0's!)
  9. #          For example, map 1 is "001", map 24 is "024" and map 987 is "987"
  10. #          And x, y are the starting point and width/heigth define the size
  11. #          to copy
  12. #
  13. #         map_paste(x,y,layer1,layer2,layer3,shadow - optional)
  14. #         map_paste_ex(id,x,y,layer1,layer2,layer3,shadow - optional)
  15. #
  16. #          Where... x, y are the starting points to paste to and layer1, layer2,
  17. #          and layer3 are which layers to copy (true/false form)
  18. #
  19. #         event_copy(map_id,event_id,x,y)
  20. #        
  21. #          Where... actually I probably don't really need to explain this.
  22. #          You know what, just in case. Map_id is the map with the event you
  23. #          want to copy, event_id is the event you want to copy, and x,y is where
  24. #          you want to paste the event to in the current map!
  25. #
  26. #Examples:
  27. #     map_copy("024",5,5,10,7)
  28. #     map_paste(0,0,true,true,false)
  29. #     event_copy(1,3,5,6)
  30. #
  31. #      This would copy a 10*7 portion from map 24 starting at 5,5 and then paste
  32. #       it into the current map at 0,0 but only layer 1 and layer 2
  33. #     (The layers as far as I recall are.. ground, path, anything from B-E)
  34. #
  35. #----------#
  36. #-- Script by: V.M of D.T
  37. #
  38. #- Questions or comments can be:
  39. #    given by email: [email protected]
  40. #    provided on facebook: http://www.facebook.com/DaimoniousTailsGames
  41. #   All my other scripts and projects can be found here: http://daimonioustails.weebly.com/
  42. #
  43. #--- Free to use in any project, commercial or non-commercial, with credit given
  44. # - - Though a donation's always a nice way to say thank you~ (I also accept actual thank you's)
  45.  
  46. class Game_Party
  47.   attr_accessor  :saved_maps
  48.   attr_accessor  :saved_events
  49. end
  50.  
  51. class Game_Interpreter
  52.   def map_copy(map_id,x,y,w,h)
  53.     $game_map.copy_portion(map_id,x,y,w,h)
  54.   end
  55.   def map_paste(x,y,l1,l2,l3,sh = false)
  56.     $game_map.paste_portion(x,y,l1,l2,l3,sh)
  57.   end
  58.   def map_paste_ex(id,x,y,l1,l2,l3,sh = false)
  59.     $game_map.paste_portion_ex(id,x,y,l1,l2,l3,sh)
  60.   end
  61.   def event_copy(map,event,x,y)
  62.     $game_map.copy_event(map,event,x,y)
  63.   end
  64. end
  65.  
  66. class Game_Event
  67.   alias sc_initialize initialize
  68.   def initialize(*args)
  69.     sc_initialize(*args)
  70.     @origin = [@event.x,@event.y]
  71.   end
  72.   def reset_event
  73.     moveto(@origin[0],@origin[1])
  74.     @page = nil
  75.     refresh
  76.   end
  77.   def change_id(id)
  78.     @id = @event.id = id
  79.   end
  80. end
  81.  
  82. class Scene_Map
  83.   def refresh_spriteset
  84.     @spriteset.dispose_characters
  85.     @spriteset.create_characters
  86.   end
  87. end
  88.  
  89. class Game_Map
  90.   alias sc_setup setup
  91.   def setup(id)
  92.     sc_setup(id)
  93.     load_map(id)
  94.     load_events(id)
  95.   end
  96.   def copy_portion(map_id,x,y,w,h)
  97.     map = load_data("Data/Map" + map_id + ".rvdata2")
  98.     @copy_hash = {};kx = 0;ky = 0;@w = w;@h = h;ox = x
  99.     (w*h).times do |i|
  100.       @copy_hash[[kx,ky,0]] = map.data[x,y,0]
  101.       @copy_hash[[kx,ky,1]] = map.data[x,y,1]
  102.       @copy_hash[[kx,ky,2]] = map.data[x,y,2]
  103.       @copy_hash[[kx,ky,3]] = map.data[x,y,3]
  104.       kx += 1;x += 1
  105.       if kx > w - 1
  106.         x = ox;kx = 0;y += 1; ky += 1
  107.       end
  108.     end
  109.   end
  110.   def paste_portion(x,y,l1,l2,l3,sh)
  111.     return unless @copy_hash
  112.     kx = 0;ky = 0;ox = x;oy = y
  113.     (@w*@h).times do |i|
  114.       @map.data[x,y,0] = @copy_hash[[kx,ky,0]] if l1
  115.       @map.data[x,y,1] = @copy_hash[[kx,ky,1]] if l2
  116.       @map.data[x,y,2] = @copy_hash[[kx,ky,2]] if l3
  117.       @map.data[x,y,3] = @copy_hash[[kx,ky,3]] if sh
  118.       kx += 1;x += 1
  119.       if kx > @w - 1
  120.         x = ox;kx = 0;y += 1; ky += 1
  121.       end
  122.     end
  123.     if Game_Map.instance_methods(false).include?(:update_autotile)
  124.       kx = 0;ky = 0;x = ox;y = oy
  125.       (@w*@h).times do |i|
  126.         update_autotile(x,y,0)
  127.         update_autotile(x,y,1)
  128.         kx += 1;x += 1
  129.         if kx > @w - 1
  130.           x = ox;kx = 0;y += 1; ky += 1
  131.         end
  132.       end
  133.     end
  134.     save_map(@map_id)
  135.   end
  136.   def paste_portion_ex(id,x,y,l1,l2,l3,sh)
  137.     if @map_id == id
  138.       paste_portion(x,y,l1,l2,l3,sh)
  139.     else
  140.       ex_map = Game_Map.new
  141.       ex_map.setup(id)
  142.       ex_map.set_copy_hash(@copy_hash,@w,@h)
  143.       ex_map.paste_portion(x,y,l1,l2,l3,sh)
  144.     end
  145.   end
  146.   def set_copy_hash(hash,sw,sh)
  147.     @copy_hash = hash
  148.     @w = sw
  149.     @h = sh
  150.   end
  151.   def copy_event(map_id, event_id, x, y)
  152.     begin
  153.       if map_id == @map_id
  154.         event = @map.events[event_id]
  155.       else
  156.         map = load_data(sprintf("Data/Map%03d.rvdata2", map_id))
  157.         event = map.events[event_id]
  158.       end
  159.     rescue
  160.       event = nil
  161.     end
  162.     return if event.nil?
  163.     i = 500
  164.     while @events.has_key?(i)
  165.       i += 1
  166.     end
  167.     event.x = x;event.y = y
  168.     @events[i] = Game_Event.new(@map_id,event)
  169.     @events[i].change_id(i)
  170.     SceneManager.scene.refresh_spriteset
  171.     save_events(@map_id)
  172.   end
  173.   def save_events(id)
  174.     $game_party.saved_events = {} if $game_party.saved_events.nil?
  175.     $game_party.saved_events[id] = @events
  176.   end
  177.   def save_map(id)
  178.     $game_party.saved_maps = {} if $game_party.saved_maps.nil?
  179.     $game_party.saved_maps[id] = @map.data
  180.   end
  181.   def load_events(id)
  182.     return if $game_party.saved_events.nil?
  183.     return if !$game_party.saved_events.include?(id)
  184.     @events = $game_party.saved_events[id]
  185.     @events.each do |i,event|
  186.       event.reset_event
  187.     end
  188.   end
  189.   def load_map(id)
  190.     return if $game_party.saved_maps.nil?
  191.     return if !$game_party.saved_maps.include?(id)
  192.     @map.data = $game_party.saved_maps[id]
  193.   end
  194. end
Advertisement
Add Comment
Please, Sign In to add comment