Advertisement
bulletxt

SwaptXT 1.1

Oct 29th, 2011
4,486
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Ruby 19.67 KB | None | 0 0
  1. =begin
  2.                                 SwapXT
  3.  
  4. Version: 1.1
  5. Author: Marco Di Antonio (bulletxt@gmail.com)
  6. Nickname: BulletXt
  7. Date: 30/10/2011
  8.  
  9. This is SwapXT script that must be put in your VX game script section.
  10. It's Plug&Play.
  11. Use SwapXT application to do swaps once you put this into your VX game.
  12.  
  13.  
  14. Changelog History:
  15. 1.1 30/10/2011
  16. - crash occured when doing real time swap with passage setting set to Default
  17.  
  18. 1.0  14/07/2010
  19. - first release
  20.  
  21.  
  22. =end
  23.  
  24.  
  25. ############################# CONFIGURATION ####################################
  26.  
  27. #Switch ID.
  28. #If ON, your events will not swap according to tiles and will use default tiles.
  29. DISABLE_EVENT_SWAPPING = 100
  30.  
  31.  
  32. =begin
  33. IGNORE the following two switches IDs unless you are using
  34. old 0.4 swapping script found at:
  35. http://lodestone2d.svn.sourceforge.net/viewvc/lodestone2d/Scripts-Dev/Swap_TileSet/Swap_tile_new_passage_setting.rb .
  36. If you are, set the 2 ID switches to ensure compatibility
  37. with your current "old system" swaps.
  38. The meaning of these two switches are described on the old 0.4 script, and you
  39. should already have confidence with them if you need these.
  40. =end
  41. SWAP_TILE = 9999
  42. LOAD_PASSAGE_SETTING = 9998
  43.  
  44.  
  45. ########################## END CONFIGURATION ###################################
  46.  
  47.  
  48. #==============================================================================
  49. # ** Cache
  50.  
  51. module Cache_Swap_Tiles
  52.  
  53.   #error message in case user is swapping tiles
  54.   #on a map with both old and new system
  55.   def self.error_message(map_id)
  56.   $bulletxt_error_message_mixed_swapping_configuration = "BulletXt Message:
  57.  on this map ID " + map_id.to_s() + "
  58.  you have a dangerous configuration.
  59.  You are swapping tilesets from events and from the swapping application.
  60.  Mixed configuration is not allowed.
  61.  Please decide which system to use to swap your tiles.
  62.  I highly suggest to swap tilesets using the swapping application.
  63.  If you decide to do this, please be sure you turn off the swapping switch
  64.  ID " + SWAP_TILE.to_s() + " before entering this map.
  65.  I will close the game for security reasons."  
  66.   #get rid of \n
  67.   array = $bulletxt_error_message_mixed_swapping_configuration.split("\n")
  68.   size = array.length()
  69.   n = 0
  70.   f = ""
  71.   while n < size do
  72.     message = array[n]
  73.     f = f + message
  74.     n = n + 1
  75.   end
  76.   $bulletxt_error_message_mixed_swapping_configuration = f
  77.   end
  78.  
  79.   #error message in case user is swapping passage settings
  80.   #on a map with both old and new system
  81.   def self.error_message_passage(map_id)
  82.   $bulletxt_error_message_mixed_swapping_configuration = "BulletXt Message:
  83.  on this map ID " + map_id.to_s() + "
  84.  you have a dangerous configuration.
  85.  You are swapping a passage settings from events and from the swapping application.
  86.  Mixed configuration is not allowed.
  87.  Please decide which system to use to swap your passage settings.
  88.  I highly suggest to swap passage settings using the swapping application.
  89.  If you decide to do this, please be sure you turn off the swapping passage
  90.  setting switch ID " + LOAD_PASSAGE_SETTING.to_s() + " before entering this map.
  91.  I will close the game for security reasons."  
  92.   #get rid of \n
  93.   array = $bulletxt_error_message_mixed_swapping_configuration.split("\n")
  94.   size = array.length()
  95.   n = 0
  96.   f = ""
  97.   while n < size do
  98.     message = array[n]
  99.     f = f + message
  100.     n = n + 1
  101.   end
  102.   $bulletxt_error_message_mixed_swapping_configuration = f
  103.   end  
  104.  
  105.  
  106.   #--------------------------------------------------------------------------
  107.   # * Get Character Graphic
  108.   #     filename : Filename
  109.   #--------------------------------------------------------------------------
  110.   def self.swap(filename)
  111.     load_bitmap("Graphics/System/extra_tiles/", filename)
  112.   end
  113.  
  114.   #--------------------------------------------------------------------------
  115.   # * Clear Cache
  116.   #--------------------------------------------------------------------------
  117.   def self.clear
  118.     @cache = {} if @cache == nil
  119.     @cache.clear
  120.     GC.start
  121.   end
  122.   #--------------------------------------------------------------------------
  123.   # * Load Bitmap
  124.   #--------------------------------------------------------------------------
  125.   def self.load_bitmap(folder_name, filename, hue = 0)
  126.     @cache = {} if @cache == nil
  127.     path = folder_name + filename
  128.     if not @cache.include?(path) or @cache[path].disposed?
  129.       if filename.empty?
  130.         @cache[path] = Bitmap.new(32, 32)
  131.       else
  132.         @cache[path] = Bitmap.new(path)
  133.       end
  134.     end
  135.     if hue == 0
  136.       return @cache[path]
  137.     else
  138.       key = [path, hue]
  139.       if not @cache.include?(key) or @cache[key].disposed?
  140.         @cache[key] = @cache[path].clone
  141.         @cache[key].hue_change(hue)
  142.       end
  143.       return @cache[key]
  144.     end
  145.   end
  146. end
  147.  
  148.  
  149. ################################################################################
  150.  
  151.  
  152.  
  153. ############ CHECK IF THERE ARE SWAPPED TILES AT START PLAY TIME ###############
  154. =begin
  155. We must ensure user is starting game and there are physically swapped tiles or
  156. passage settings,
  157. tiles that have been for example swapped from the application
  158. =end
  159. class Scene_Title < Scene_Base
  160.  
  161.   alias swapxt_start start
  162.   def start
  163.     swapxt_start
  164.     check_if_tileset_swapped_exist
  165.     check_if_passage_swapped_exist
  166.   end
  167.  
  168.   def check_if_tileset_swapped_exist
  169.     tile = "Graphics/System/swapped_"
  170.     tileset_swap_loaded_message_error if FileTest.exist?(tile + "A1.png")
  171.     tileset_swap_loaded_message_error if FileTest.exist?(tile + "A2.png")
  172.     tileset_swap_loaded_message_error if FileTest.exist?(tile + "A3.png")
  173.     tileset_swap_loaded_message_error if FileTest.exist?(tile + "A4.png")
  174.     tileset_swap_loaded_message_error if FileTest.exist?(tile + "A5.png")
  175.     tileset_swap_loaded_message_error if FileTest.exist?(tile + "B.png")
  176.     tileset_swap_loaded_message_error if FileTest.exist?(tile + "C.png")
  177.     tileset_swap_loaded_message_error if FileTest.exist?(tile + "D.png")
  178.     tileset_swap_loaded_message_error if FileTest.exist?(tile + "E.png")
  179.   end
  180.  
  181.   def tileset_swap_loaded_message_error
  182.     p sprintf("SwapXT Error: You are not allowed to start your game with swapped tilesets. You must unload your tilesets with SwapXT application first. Open SwapXT, click at top Tools->Restore all Tilesets")
  183.     exit
  184.   end
  185.  
  186.   def check_if_passage_swapped_exist
  187.     passage = "swapxt/passage_warning.stx"
  188.     passage_swap_loaded_message_error if FileTest.exist?(passage)
  189.   end
  190.  
  191.   def passage_swap_loaded_message_error
  192.     p sprintf("SwapXT Error: You are not allowed to start your game with swapped passage settings. You must unload your passage setting with SwapXT application first. Open SwapXT, go on PassageSetting tab and click on Restore to Default.")
  193.     exit
  194.   end
  195.  
  196. end
  197. ################################################################################
  198.  
  199.  
  200.  
  201. =begin
  202. this class takes care of checking if there is a map id file. this class does
  203. not swap tilesets but only prepares the tiles variables(if map id file found).
  204. this class handles loading the passage setting file, for both old and
  205. new system
  206. =end
  207.  
  208. class Game_Map
  209.   include Cache_Swap_Tiles  
  210.   alias bulletxt_goodbye_vx_limit_tile_setup setup
  211.   def setup(map_id)
  212.  
  213.     #must be false at load time
  214.     $swapxt_realtime = false
  215.    
  216.     #this variable must always be false at runtime.
  217.     #becomes true only if it finds a map id file
  218.     $swap_tileset_profile_bulletxt = false
  219.  
  220.     #check if it must load a saved profile TILESET for this map id
  221.     profile_path_tile = "swapxt/tiles/" + map_id.to_s() + ".stx"
  222.     if FileTest.exist?(profile_path_tile)
  223.       #we must now check if user isn't fooling around with his configuration.
  224.       #he must not be swapping tiles on this map with old and new system
  225.       #at the same time. if this is the case, warn him we don't want issues and
  226.       #close the game
  227.       if $game_switches[SWAP_TILE] == true
  228.         Cache_Swap_Tiles.error_message(map_id)
  229.         p sprintf($bulletxt_error_message_mixed_swapping_configuration)
  230.         exit
  231.       end
  232.       #set this to true so we know we want to swap with new system
  233.       $swap_tileset_profile_bulletxt = true
  234.       #p sprintf(profile_path_tile + " found!")
  235.       #read the map id file, it has 8 lines with the name of the tileset to swap.
  236.       #set each tileset global variable with the corresponding found line, in order.
  237.       $tileA1,$tileA2,$tileA3,$tileA4,$tileA5,$tileB,$tileC,$tileD,$tileE = File.read(profile_path_tile).split("\n")
  238.     end
  239.  
  240.  
  241.     ##################### SWAP PASSSAGE SETTINGS ##########################
  242.     @swap_passagesetting_bulletxt = false
  243.     #for how this works, look at the code above
  244.     profile_path_passages = "swapxt/passages/" + map_id.to_s() + ".stx"
  245.     if FileTest.exist?(profile_path_passages)
  246.       #p sprintf("passage exists on map " + map_id.to_s())
  247.       if $game_switches[LOAD_PASSAGE_SETTING] == true
  248.         Cache_Swap_Tiles.error_message_passage(map_id)
  249.         p sprintf($bulletxt_error_message_mixed_swapping_configuration)
  250.         exit
  251.       end
  252.       @swap_passagesetting_bulletxt = true
  253.       $swap_passages = (File.read(profile_path_passages).split("\n"))[0]
  254.     end
  255.  
  256.    
  257.     #checks if it should load default passage setting, both for old and new system
  258.     if $game_switches[LOAD_PASSAGE_SETTING] == false and @swap_passagesetting_bulletxt == false
  259.       bulletxt_goodbye_vx_limit_tile_setup(map_id)
  260.       $swap_passages = nil
  261.       return  
  262.     end
  263.    
  264.  
  265.     path = "Graphics/System/extra_tiles/" + $swap_passages.to_s() + ".rvdata" rescue nil
  266.  
  267.     #check if user set $swap_passages to nil cause he wants to load default setting.
  268.     #if user is loading with new system checks if it's equal to empty::*::
  269.     if $swap_passages == nil or $swap_passages == "empty::*::"
  270.       bulletxt_goodbye_vx_limit_tile_setup(map_id)
  271.       return
  272.     end
  273.  
  274.  
  275.     #when here, we are sure user wants to swap passage setting, either
  276.     #via old system or new system
  277.     #the file to swap is not default one, he wants to swap to a real new one
  278.     @map_id = map_id
  279.     @map = load_data(sprintf("Data/Map%03d.rvdata", @map_id))
  280.     @display_x = 0
  281.     @display_y = 0
  282.      
  283.     # load system settings from that file
  284.     new_system_rvdata = load_data(path)
  285.     # Use passage settings from that file
  286.     @passages = new_system_rvdata.passages
  287.      
  288.     #default vx code
  289.     referesh_vehicles
  290.     setup_events
  291.     setup_scroll
  292.     setup_parallax
  293.     @need_refresh = false
  294.   end
  295.  
  296.  
  297. ############################# REAL TIME SWAPS ##################################  
  298.   #if here, player has done a $game_map.tileset call. It must reload
  299.   #map with new passage setting (if it exists) and upate map.
  300.   #OLD SYSTEM
  301.   def tileset
  302.     #if false, we must update and load default passage
  303.     if $game_switches[LOAD_PASSAGE_SETTING] == false
  304.       @passages = $data_system.passages
  305.     else
  306.       #if nil, must reset passage setting to default  
  307.       if $swap_passages == nil
  308.         @passages = $data_system.passages
  309.         $scene = Scene_Map.new
  310.         return
  311.       end
  312.     path = "Graphics/System/extra_tiles/" + $swap_passages + ".rvdata" rescue nil
  313.     # load system settings from that file
  314.     new_system_rvdata = load_data(path)
  315.     # Use passage settings from that file
  316.     @passages = new_system_rvdata.passages
  317.     end  
  318.     #this updates the map tiles. it does not modify events or anything else.
  319.     $scene = Scene_Map.new
  320.   end
  321.  
  322.  
  323.  
  324.   #if here, player has done a $game_map.swapxt() call. It must reload
  325.   #map with new passage setting (if it exists) and upate map.
  326.   #NEW SWAPXT SYSTEM
  327.   def swapxt(integer)
  328.     rtx_path = "swapxt/realtime/" + integer.to_s() + ".rtx"
  329.     if !FileTest.exist?(rtx_path )
  330.       p sprintf("SwapXT Error: You have called $game_map.swapxt(" + integer.to_s() + ") , but that Real-Time swap doesn't exist. Please better check it inside SwapXT app, under the Real-Time section.")
  331.       exit
  332.     end
  333.     $tileA1,$tileA2,$tileA3,$tileA4,$tileA5,$tileB,$tileC,$tileD,$tileE,passage = File.read(rtx_path).split("\n")
  334.     path = "Graphics/System/extra_tiles/" + passage + ".rvdata" rescue nil
  335.     if !FileTest.exist?(path)
  336.       path = "Data/System.rvdata"
  337.     end
  338.     $swapxt_realtime = true #warning,realtime update must occur
  339.     # load system settings from that file
  340.     new_system_rvdata = load_data(path)
  341.     # Use passage settings from that file
  342.     @passages = new_system_rvdata.passages
  343.     #this updates the map tiles. it does not modify events or anything else.
  344.     $scene = Scene_Map.new
  345.   end
  346.  
  347.  
  348. end
  349.  
  350.  
  351. #this class handles loading tilesets
  352. class Spriteset_Map
  353. include Cache_Swap_Tiles  
  354.  
  355.   alias bulletxt_lodestone_create_tilemap create_tilemap
  356.   def create_tilemap
  357.     #if true, it means the map doesn't have to load tilesets,
  358.     #neither for old swapping system or new system.
  359.     #if true, also means no realtime update occured
  360.     #it must load DEFAULT tilesets
  361.     if $game_switches[SWAP_TILE] == false and $swap_tileset_profile_bulletxt == false and $swapxt_realtime == false
  362.       bulletxt_lodestone_create_tilemap
  363.       $tileA1 = nil
  364.       $tileA2 = nil
  365.       $tileA3 = nil
  366.       $tileA4 = nil
  367.       $tileA5 = nil
  368.       $tileB = nil
  369.       $tileC = nil
  370.       $tileD = nil
  371.       $tileE = nil
  372.       return
  373.     end
  374.  
  375.    
  376.    
  377. =begin
  378. calling the original function is a trick. if there are tiles to swap
  379. it will just replace them, otherwise if a tile is nil it won't swap anything,
  380. but still the tile will be loaded because it has already loaded the original tile
  381. when calling this function. in other words, if setting a tile$n = nil will simply
  382. make the script load default tile.
  383. =end
  384.     bulletxt_lodestone_create_tilemap
  385. =begin
  386. when here, some/all tiles must be swapped, either old or with new system.
  387. the tile can be equal to "empty::*::" only if it found the file id of the
  388. map and line$n is equal to empty::*::. in that case it must load default tileset.
  389. there are no chances a user with old system is swapping a tileset named
  390. "empty::*::" because you can't name files with ":" characters under
  391. Microsoft Windows OS.
  392. =end
  393.     path_to_graphic = "extra_tiles/"
  394.  
  395.     #tileA1
  396.     if $tileA1 == "empty::*::"
  397.       @tilemap.bitmaps[0] = Cache.system("TileA1")
  398.     else
  399.       tile1 = Cache_Swap_Tiles.swap($tileA1 + ".png") rescue nil
  400.       @tilemap.bitmaps[0] = tile1 if $tileA1 != nil
  401.     end
  402.  
  403.     #tileA2
  404.     if $tileA2 == "empty::*::"
  405.       @tilemap.bitmaps[1] = Cache.system("TileA2")
  406.     else
  407.       tile2 = Cache_Swap_Tiles.swap($tileA2 + ".png") rescue nil
  408.       @tilemap.bitmaps[1] = tile2 if $tileA2 != nil
  409.     end
  410.    
  411.     #tileA3
  412.     if $tileA3 == "empty::*::"
  413.       @tilemap.bitmaps[2] = Cache.system("TileA3")
  414.     else
  415.       tile3 = Cache_Swap_Tiles.swap($tileA3 + ".png") rescue nil
  416.       @tilemap.bitmaps[2] = tile3 if $tileA3 != nil
  417.     end
  418.      
  419.     #tileA4
  420.     if $tileA4 == "empty::*::"
  421.       @tilemap.bitmaps[3] = Cache.system("TileA4")
  422.     else
  423.       tile4 = Cache_Swap_Tiles.swap($tileA4 + ".png") rescue nil
  424.       @tilemap.bitmaps[3] = tile4 if $tileA4 != nil
  425.     end
  426.      
  427.     #tileA5
  428.     if $tileA5 == "empty::*::"
  429.       @tilemap.bitmaps[4] = Cache.system("TileA5")
  430.     else
  431.       tile5 = Cache_Swap_Tiles.swap($tileA5 + ".png") rescue nil
  432.       @tilemap.bitmaps[4] = tile5 if $tileA5 != nil
  433.     end
  434.  
  435.     #tileB
  436.     if $tileB == "empty::*::"
  437.       @tilemap.bitmaps[5] = Cache.system("TileB")
  438.     else
  439.       tile6 = Cache_Swap_Tiles.swap($tileB + ".png") rescue nil
  440.       @tilemap.bitmaps[5] = tile6 if $tileB != nil
  441.     end
  442.  
  443.     #tileC
  444.     if $tileC == "empty::*::"
  445.       @tilemap.bitmaps[6] = Cache.system("TileC")
  446.     else
  447.       tile7 = Cache_Swap_Tiles.swap($tileC + ".png") rescue nil
  448.       @tilemap.bitmaps[6] = tile7 if $tileC != nil
  449.     end
  450.  
  451.     #tileD
  452.     if $tileD == "empty::*::"
  453.       @tilemap.bitmaps[7] = Cache.system("TileD")
  454.     else
  455.       tile8 = Cache_Swap_Tiles.swap($tileD + ".png") rescue nil
  456.       @tilemap.bitmaps[7] = tile8 if $tileD != nil
  457.     end
  458.  
  459.     #tileE
  460.     if $tileE == "empty::*::"
  461.       @tilemap.bitmaps[8] = Cache.system("TileE")
  462.     else
  463.       tile9 = Cache_Swap_Tiles.swap($tileE + ".png") rescue nil
  464.       @tilemap.bitmaps[8] = tile9 if $tileE != nil
  465.     end
  466.    
  467.   #closes def
  468.   end
  469. #closes class
  470. end
  471.  
  472.  
  473. ########################## SWAP EVENT'S GRAPHICS #############################
  474. #the following class handles swapping event's graphics according to the tiles
  475. class Sprite_Character < Sprite_Base
  476. include Cache_Swap_Tiles
  477.  
  478.   alias swap_tileset_bitmap tileset_bitmap
  479.   def tileset_bitmap(tile_id)
  480.      #do not swap events if true
  481.      if $game_switches[DISABLE_EVENT_SWAPPING]
  482.         set_number = tile_id / 256
  483.         return Cache.system("TileB") if set_number == 0
  484.         return Cache.system("TileC") if set_number == 1
  485.         return Cache.system("TileD") if set_number == 2
  486.         return Cache.system("TileE") if set_number == 3
  487.         return nil
  488.         return
  489.      end
  490.      
  491.      if $game_switches[SWAP_TILE] == false and $swap_tileset_profile_bulletxt == false and $swapxt_realtime == false
  492.         set_number = tile_id / 256
  493.         return Cache.system("TileB") if set_number == 0
  494.         return Cache.system("TileC") if set_number == 1
  495.         return Cache.system("TileD") if set_number == 2
  496.         return Cache.system("TileE") if set_number == 3
  497.         return nil
  498.         return
  499.      end
  500.      
  501.     swap_tileset_bitmap(tile_id)
  502.     set_number = tile_id / 256
  503.    
  504.     if $tileB == "empty::*::"
  505.       return Cache.system("TileB") if set_number == 0
  506.     else
  507.       return Cache_Swap_Tiles.swap($tileB) if set_number == 0 rescue nil
  508.     end
  509.    
  510.     if $tileC == "empty::*::"
  511.       return Cache.system("TileC") if set_number == 1
  512.     else
  513.       return Cache_Swap_Tiles.swap($tileC) if set_number == 1 rescue nil
  514.     end
  515.    
  516.     if $tileD == "empty::*::"
  517.       return Cache.system("TileD") if set_number == 2
  518.     else
  519.       return Cache_Swap_Tiles.swap($tileD) if set_number == 2 rescue nil
  520.     end
  521.    
  522.     if $tileE == "empty::*::"
  523.       return Cache.system("TileE") if set_number == 3
  524.     else
  525.        return Cache_Swap_Tiles.swap($tileE) if set_number == 3 rescue nil
  526.     end
  527.  
  528.     return nil
  529.   end
  530. end
  531.  
  532.  
  533.  
  534. =begin
  535. saves tiles and passage setting variables,
  536. needed if you save on a map that has swapped stuff.
  537. this is only needed when using old system, because new system
  538. reads the value from the map id file
  539. =end
  540. class Scene_File < Scene_Base
  541.   alias bulletxt_swap_tiles_write_save_data write_save_data
  542.   def write_save_data(file)
  543.     bulletxt_swap_tiles_write_save_data(file)
  544.     Marshal.dump($tileA1,      file)
  545.     Marshal.dump($tileA2,      file)
  546.     Marshal.dump($tileA3,      file)
  547.     Marshal.dump($tileA4,      file)
  548.     Marshal.dump($tileA5,      file)
  549.     Marshal.dump($tileB,      file)
  550.     Marshal.dump($tileC,      file)
  551.     Marshal.dump($tileD,      file)
  552.     Marshal.dump($tileE,      file)
  553.     Marshal.dump($swap_passages.to_s(),   file)
  554.     Marshal.dump($swap_tileset_profile_bulletxt,  file)
  555.     Marshal.dump($swapxt_realtime, file)
  556.   end  
  557.  
  558.   alias bulletxt_swap_tiles_read_save_data read_save_data
  559.   def read_save_data(file)
  560.     bulletxt_swap_tiles_read_save_data(file)
  561.     $tileA1        = Marshal.load(file)
  562.     $tileA2        = Marshal.load(file)
  563.     $tileA3        = Marshal.load(file)
  564.     $tileA4        = Marshal.load(file)
  565.     $tileA5        = Marshal.load(file)
  566.     $tileB        = Marshal.load(file)
  567.     $tileC        = Marshal.load(file)
  568.     $tileD        = Marshal.load(file)
  569.     $tileE        = Marshal.load(file)
  570.     $swap_passages = Marshal.load(file)
  571.     $swap_tileset_profile_bulletxt = Marshal.load(file)
  572.     $swapxt_realtime = Marshal.load(file)
  573.   end
  574.  
  575. end
  576.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement