Skip to content

Instantly share code, notes, and snippets.

@zeroeth
Created May 18, 2018 02:17
Show Gist options
  • Select an option

  • Save zeroeth/47d5d3943a9725ab912607add7b212fd to your computer and use it in GitHub Desktop.

Select an option

Save zeroeth/47d5d3943a9725ab912607add7b212fd to your computer and use it in GitHub Desktop.
World of Warcraft map tiler.
# WoW Map Tiler
require 'chunky_png'
# find tiles
print "Finding tiles..."
tile_image_names = Dir.glob("input/**/map*.png").sort
tile_datum = Array.new
tile_image_names.each do |image_name|
tile_data = {}
coord_reg = /(\d{1,})_(\d{1,})/;
x_coord = coord_reg.match(image_name)[1].to_i
y_coord = coord_reg.match(image_name)[2].to_i
tile_data['file_name'] = image_name
tile_data['x_map_coord'] = x_coord
tile_data['y_map_coord'] = y_coord
tile_datum.push tile_data
end
print "done!\n"
# Find coordinate dimension extents
coords = {}
coords['x_min'] = tile_datum.map{|data| data['x_map_coord'] }.min
coords['x_max'] = tile_datum.map{|data| data['x_map_coord'] }.max
coords['y_min'] = tile_datum.map{|data| data['y_map_coord'] }.min
coords['y_max'] = tile_datum.map{|data| data['y_map_coord'] }.max
puts "COORDS [#{coords['x_min']},#{coords['y_min']}]-[#{coords['x_max']},#{coords['y_max']}]"
# create matrix of image locations
print "Finding tile image offsets..."
# sample first image
first_tile = ChunkyPNG::Image.from_file tile_datum.first['file_name']
tile_width = first_tile.width
tile_height = first_tile.height
tile_datum.each do |tile_data|
# offset from min
x_tile_offset = tile_data['x_map_coord'] - coords['x_min']
y_tile_offset = tile_data['y_map_coord'] - coords['y_min']
tile_data['x_pixel_coord'] = x_tile_offset * tile_width
tile_data['y_pixel_coord'] = y_tile_offset * tile_height
end
print "done!\n"
coords['x_pmin'] = tile_datum.map{|data| data['x_pixel_coord'] }.min
coords['x_pmax'] = tile_datum.map{|data| data['x_pixel_coord'] }.max
coords['y_pmin'] = tile_datum.map{|data| data['y_pixel_coord'] }.min
coords['y_pmax'] = tile_datum.map{|data| data['y_pixel_coord'] }.max
puts "COORDS [#{coords['x_pmin']},#{coords['y_pmin']}]-[#{coords['x_pmax']},#{coords['y_pmax']}]"
# paste in tiles to master map (loaded one at a time)
print "Creating blank map image..."
map_width = coords['x_pmax'] + tile_width
map_height = coords['y_pmax'] + tile_height
map_image = ChunkyPNG::Image.new map_width, map_height, ChunkyPNG::Color::TRANSPARENT
print "done!\n"
print "Pasting tiles\n"
tile_datum.each_with_index do |tile_data, index|
puts "Inserting tile #{index+1}/#{tile_datum.size}"
image_x = tile_data['x_pixel_coord']
image_y = tile_data['y_pixel_coord']
image = ChunkyPNG::Image.from_file tile_data['file_name']
map_image.compose! image, image_x, image_y
end
print "done!\n"
print "Saving image..."
map_image.save("mastermap.png");
print "done!\n"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment