Tileset Combiner

Tileset Combiner Version 1.0
by Neo-Bahamut aka Wurstinator

Hi,
I won't spend much time on this description since it just took me about half an hour to write the one above the script.
Ever thought "Oh wow, this tileset looks totally awesome. But this one does as well. I wish, I could use them both together but all this passages-terraintag-priority-options are too annoying D:"
Now, here comes my Tileset Combiner !!!
It automatically copies most of the tileset options from two or more tilesets together. All you have to set yourself are the autotiles, the fog, the panorama and the battleback.

Script:
  1. #============================================================================
  2. # ** Tileset Combiner
  3. #----------------------------------------------------------------------------
  4. # by Neo-Bahamut (also known as Wurstinator)
  5. # V 1.0
  6. # 04.09.2010 (DD.MM.YYYY)
  7. #----------------------------------------------------------------------------
  8. # This script combines two or more tilesets from the database for you.
  9. # All you have to do yourself is setting the Autotiles, the fog, the panorama
  10. # and the battleback. The tileset graphic, the tile's passabilities, priorities
  11. # and all the other stuff are set by the script.
  12. # This script require the "Bitmap to PNG" script by http://www.66rpg.com which is
  13. # included in this demo.
  14. #----------------------------------------------------------------------------
  15. # Script Usage:
  16. # You do not have to change anything in the code. If you know what you do,
  17. # you can change the constant variables at the beginning.
  18. # To start the process use the following line:
  19. #   Tileset_Combiner.start_combine(ids)
  20. # It is not important whether you do this in a call script or in the script
  21. # editor somewhere below this script.
  22. # You have to replace "ids" with (obviously) the IDs of the tilesets.
  23. # You can find them in the Database at the "Tilesets" tab. On the left side
  24. # there is the list of all tilesets. They are listed like "ID: Name".
  25. # When you got the IDs you can start it like this (e.g. IDs 1, 2, 3, 5 and 9):
  26. #   Tileset_Combiner.start_combine(1, 2, 3, 5, 9)
  27. # This is the easiest way to use it.
  28. # For those, who know what that means: You can also pass an Array or a Range.
  29. #----------------------------------------------------------------------------
  30. # Conditions:
  31. #   If you use this script it would be nice to put me somewhere on your
  32. #   credits list. As I can't check if you created your tilesets with this script
  33. #   or you did by hand, I can't force you to do so.
  34. #   But since putting me in the credits doesn't harm you, it would be pretty
  35. #   inglorious to "betray" me :P
  36. #   You may publish this script somewhere else, as long as you do not put
  37. #   yourself into the authors list or remove me from it.
  38. #   You may modify this script as long as you do not claim its ownership.
  39. #============================================================================
  40.  
  41. module Tileset_Combiner
  42.  
  43.   TILESET_FILE = "./Data/Tilesets.rxdata"
  44.   TILESET_DIRECTORY = "./Graphics/Tilesets/"
  45.   PRINT_MESSAGE_WHEN_DONE = true
  46.   USE_ANTI_FREEZE_THREAD = true
  47.   ERROR_DETECTION = true
  48.  
  49.   def Tileset_Combiner.start_combine(*args)
  50.     # Start Anti Freeze
  51.     if USE_ANTI_FREEZE_THREAD
  52.       anti_freeze = Thread.new {loop {self.update_anti_freeze}}
  53.     end
  54.     # Can give an Array or Range of Integers as argument
  55.     if args[0].is_a?(Range)
  56.       args[0] = args[0].to_a
  57.     end
  58.     if args[0].is_a?(Array)
  59.       args = args[0]
  60.     end
  61.     # Load Data
  62.     data = load_data(TILESET_FILE)
  63.     used_data = args.collect {|id| data[id]}
  64.     # Error if used_data contains nil
  65.     if ERROR_DETECTION
  66.       if used_data.include?(nil)
  67.         p "Error occured. You tried to combine tilesets which do not exist." +
  68.           "Their IDs will be removed from the list and the combination will continue."
  69.         used_data = used_data.compact
  70.       end
  71.     end
  72.     # Combine Bitmap
  73.     new_bitmap = self.combine_bitmap(used_data)
  74.     # Combine RPG::Tilesets
  75.     new_tileset = self.combine_data(used_data)
  76.     # Add Tileset
  77.     new_tileset.id = data.size - 1
  78.     data.push(new_tileset)
  79.     # Save Data
  80.     save_data(data, TILESET_FILE)
  81.     # End Anti Freeze
  82.     if USE_ANTI_FREEZE_THREAD
  83.       anti_freeze.exit
  84.       anti_freeze = nil
  85.     end
  86.     # Save Bitmap
  87.     self.save_bitmap(new_bitmap, used_data)
  88.     # Print Message
  89.     if PRINT_MESSAGE_WHEN_DONE
  90.       p 'Finished!'
  91.     end
  92.   end
  93.  
  94.   def Tileset_Combiner.make_tilesetname(data)
  95.     string = "Combined Tileset ("
  96.     data.each_with_index do |i, n|
  97.       if i.is_a?(RPG::Tileset)
  98.         string << i.id.to_s
  99.       else
  100.         string << i.to_s
  101.       end
  102.       if n < data.size - 1
  103.         string << ','
  104.       end
  105.     end
  106.     string << ')'
  107.     return string
  108.   end
  109.  
  110.   private
  111.  
  112.   def self.combine_bitmap(data)
  113.     # Get Bitmaps
  114.     bitmaps = data.collect do |tileset|
  115.       Bitmap.new(TILESET_DIRECTORY + tileset.tileset_name)
  116.     end
  117.     # Create new Bitmap
  118.     height = 0
  119.     bitmaps.each {|i| height += i.height}
  120.     combined = Bitmap.new(256, (height / 32.0).ceil * 32)
  121.     # Copy content
  122.     y = 0
  123.     bitmaps.each do |i|
  124.       combined.blt(0, y, i, i.rect)
  125.       y += i.height
  126.     end
  127.     # Return
  128.     return combined
  129.   end
  130.  
  131.   def self.combine_data(data)
  132.     # Create new Tileset and set the simple parameters
  133.     new_tileset = RPG::Tileset.new
  134.     new_tileset.name = Tileset_Combiner.make_tilesetname(data)
  135.     new_tileset.tileset_name = new_tileset.name
  136.     # Create new Tables
  137.     tile_num = 0
  138.     data.each {|tileset| tile_num += tileset.passages.xsize-384}
  139.     table = Table.new(tile_num + 384)
  140.     new_tileset.passages, new_tileset.priorities, new_tileset.terrain_tags =
  141.       table.dup, table.dup, table.dup
  142.     # Copy the data
  143.     index = 383
  144.     data.each do |tileset|
  145.       size = tileset.passages.xsize - 384
  146.       size.times do |n|
  147.         index += 1
  148.         new_tileset.passages[index] = tileset.passages[n + 384]
  149.         new_tileset.priorities[index] = tileset.priorities[n + 384]
  150.         new_tileset.terrain_tags[index] = tileset.terrain_tags[n + 384]
  151.       end
  152.     end
  153.     # Return
  154.     return new_tileset
  155.   end
  156.  
  157.   def self.save_bitmap(bitmap, data)
  158.     # Get name
  159.     name = Tileset_Combiner.make_tilesetname(data)
  160.     # Make dirs
  161.     dir = ''
  162.     TILESET_DIRECTORY.split('/').each do |d|
  163.       dir << d << '/'
  164.       if !FileTest.exist?(dir)
  165.         Dir.mkdir(dir)
  166.       end
  167.     end
  168.     # Save
  169.     bitmap.make_png(TILESET_DIRECTORY + name)
  170.   end
  171.  
  172.   def self.update_anti_freeze
  173.     Graphics.update
  174.     Graphics.frame_count -= 1
  175.     sleep(3)
  176.   end
  177. end
Expand to see the code.


Download:
http://www.multiupload.com/RMK8OKLZY2
This is brilliant! Too bad I already combined my game's tilesets manually, wasting hours in the process. You're going to save a lot of people a lot of time.
i also have already made my own custom tilesets, however i can still see my self using this in the future. i was way surprised on how fast this ran :)
@ tuatha: Thanks :)
@ Plague180: It takes some time to save the picture (.png) in my opinion :0
Anyway, thanks too :)
it combines the tilesets alright, just doesn't copy the tileset options / passages ect.
Oh wow, this is pretty ace.

Original thread: https://www.hbgames.org/forums/viewtopic.php?t=72396