Code: Select all
#!/usr/bin/env ruby
require 'RMagick'
require 'zip/zipfilesystem'
def image_from_archive archive, image_name
blob = archive.file.read(image_name)
terrain = Magick::ImageList.new
terrain.from_blob(blob)
end
def cut image, size, table
table.each do |current|
x = size * current[0]
y = size * current[1]
tile = image.crop(x, y, size, size, true)
tile.write current[2]
end
end
def fix_greens
files = ["grass.png", "grass_side.png"]
files.each do |current|
img = Magick::ImageList.new(current)
img = img.colorize(0.6, 0.6, 0.6, "#003F00")
img.write(current)
end
img = Magick::ImageList.new("leaves.png")
img = img.colorize(0.8, 0.8, 0.8, "#003F00")
img.write("leaves.png")
end
def generate_rails size
straight = Magick::ImageList.new("rail.png")
curved = Magick::ImageList.new("rail_curved.png")
tile = straight.composite(curved, 0, 0, Magick::OverCompositeOp)
tile.write("rail_t_junction.png")
horizontal = straight.rotate(90)
tile = straight.composite(horizontal, 0, 0, Magick::OverCompositeOp)
tile.write("rail_crossing.png")
end
def extract_crack terrain
size = terrain.rows / 16
nstages = 5
crack = Magick::Image.new(size, size * nstages) {
self.background_color = "#00000000"
}
selected_stages = [2, 3, 5, 6, 9]
nstages.times do |stage_number|
x = selected_stages[stage_number] * size
y = 15 * size
tile = terrain.crop(x, y, size, size, true)
x = 0
y = stage_number * size
crack.composite!(tile, x, y, Magick::CopyCompositeOp)
end
crack.write("crack.png")
end
terrain_table = [
[3, 2, "bookshelf.png"],
[7, 0, "brick.png"],
[6, 4, "cactus_side.png"],
[5, 4, "cactus_top.png"],
[11, 1, "chest_front.png"],
[10, 1, "chest_side.png"],
[9, 1, "chest_top.png"],
[8, 4, "clay.png"],
[6, 1, "cloud.png"],
[0, 1, "cobble.png"],
[12, 2, "furnace_front.png"],
[14, 3, "furnace_side.png"],
[1, 3, "glass.png"],
[0, 0, "grass.png"],
[6, 2, "grass_side.png"],
[3, 1, "gravel.png"],
[3, 5, "ladder.png"],
[15, 15, "lava.png"],
[4, 3, "leaves.png"],
[2, 2, "mineral_coal.png"],
[1, 2, "mineral_iron.png"],
[2, 0, "mud.png"],
[4, 2, "mossycobble.png"],
[0, 7, "rail_curved.png"],
[0, 8, "rail.png"],
[2, 1, "sand.png"],
[0, 13, "sandstone.png"],
[6, 1, "steel_block.png"],
[1, 0, "stone.png"],
[4, 1, "tree.png"],
[5, 1, "tree_top.png"],
[4, 0, "wood.png"],
[15, 13, "water.png"],
]
items_table = [
[10, 0, "apple.png"],
[11, 3, "book.png"],
[6, 1, "clay_brick.png"],
[9, 3, "lump_of_clay.png"],
[7, 0, "lump_of_coal.png"],
[10, 3, "paper.png"],
[7, 1, "steel_ingot.png"],
[5, 3, "stick.png"],
[4, 6, "tool_mesepick.png"],
[2, 7, "tool_steelaxe.png"],
[2, 6, "tool_steelpick.png"],
[2, 5, "tool_steelshovel.png"],
[2, 4, "tool_steelsword.png"],
[1, 7, "tool_stoneaxe.png"],
[1, 6, "tool_stonepick.png"],
[1, 5, "tool_stoneshovel.png"],
[1, 4, "tool_stonesword.png"],
[0, 7, "tool_woodaxe.png"],
[0, 6, "tool_woodpick.png"],
[0, 5, "tool_woodshovel.png"],
[0, 4, "tool_woodsword.png"],
]
def usage
puts "Usaage: #{__FILE__} <zipfile> [directory]"
end
if (ARGV.count == 0 || ARGV.count > 2)
usage
exit
end
Zip::ZipFile.open(ARGV[0]) do |pack|
Dir.chdir ARGV[1] if ARGV[1]
terrain = image_from_archive(pack, "terrain.png")
size = terrain.rows / 16
cut(terrain, size, terrain_table)
extract_crack(terrain)
fix_greens
generate_rails(size)
items = image_from_archive(pack, "gui/items.png")
cut(items, items.rows / 16, items_table)
icons = image_from_archive(pack, "gui/icons.png")
scale = icons.rows / 256
tile = icons.crop(scale * (16 + 4*9), 0, scale * 9, scale * 9, true)
tile.write("heart.png")
end
It must work with any texturepack, howerer there are some problems with transparency.
Here is a list of what does not work:
- I don't know how to properly colorize the grass and leaves
- With the default texturepack (minecraft.jar) cracking texture (crack.png) makes the block completely transparent but with other texturepacks it works right
- To make crossing rails texture i combined two others and it looks ugly
- Fences and signes don't convert