Created
December 18, 2011 07:12
-
-
Save raws/1492621 to your computer and use it in GitHub Desktop.
Quick Ruby script to convert Minecraft schematics to BOB objects
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convert a Minecraft schematic to a BO2 object template | |
# Usage: ruby bobuilder.rb [-f, --force] <path to schematic> <path to bob output> [z offset] | |
# See: http://www.minecraftwiki.net/wiki/Schematic_File_Format | |
# https://github.com/Wickth/TerrainControll/blob/master/bo2spec.txt | |
# | |
# Copyright (c) 2011 Ross Paffett | |
# | |
# Permission is hereby granted, free of charge, to any person obtaining a copy | |
# of this software and associated documentation files (the "Software"), to deal | |
# in the Software without restriction, including without limitation the rights | |
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | |
# copies of the Software, and to permit persons to whom the Software is | |
# furnished to do so, subject to the following conditions: | |
# | |
# The above copyright notice and this permission notice shall be included in all | |
# copies or substantial portions of the Software. | |
# | |
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | |
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | |
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | |
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | |
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | |
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE | |
# SOFTWARE. | |
require "nbtfile" | |
# Needy blocks are items like torches and doors, which require other blocks to | |
# be in place -- otherwise they'll simply fall to the ground as entities. We | |
# defer them to the end of the BOB output to ensure they're "built" last. | |
NEEDY_BLOCKS = [26, 27, 28, 31, 32, 37, 38, 39, 40, 50, 55, 59, 63, 64, 65, | |
66, 68, 69, 70, 71, 72, 75, 76, 77, 81, 83, 85, 96, 104, 105, 106, 111, 115] | |
@options = ARGV.select { |arg| arg =~ /^\-+[a-z]+/ } | |
@args = ARGV - @options | |
if @args.size >= 2 && !(@args.include?("-h") || @args.include?("--help")) | |
@schematic_path = File.expand_path(@args[0]) | |
@bob_path = File.expand_path(@args[1]) | |
@z_offset = (@args[2] || 0).to_i | |
else | |
STDERR.puts "usage: ruby bobuilder.rb [-f, --force] <path to schematic> <path to bob output> [z offset]" | |
STDERR.puts " -f, --force will overwrite existing files" | |
exit 1 | |
end | |
if !(@options.include?("-f") || @options.include?("--force")) && File.exists?(@bob_path) | |
STDERR.puts "error: #{@bob_path} already exists or cannot be written to" | |
exit 1 | |
end | |
open(@schematic_path, "r") do |io| | |
@schematic = NBTFile.load(io)[1] | |
end | |
# Slice blocks and block metadata by the size of the schematic's axes, then | |
# zip the slices up into pairs of [id, metadata] for convenient consumption. | |
blocks = @schematic["Blocks"].bytes.each_slice(@schematic["Width"]).to_a | |
blocks = blocks.each_slice(@schematic["Length"]).to_a | |
data = @schematic["Data"].bytes.each_slice(@schematic["Width"]).to_a | |
data = data.each_slice(@schematic["Length"]).to_a | |
layers = blocks.zip(data).map { |blocks, data| blocks.zip(data).map { |blocks, data| blocks.zip(data) } } | |
deferred = [] | |
open(@bob_path, "w") do |io| | |
io.write(DATA.read) | |
layers.each_with_index do |rows, z| | |
z += @z_offset | |
rows.each_with_index do |columns, x| | |
x -= @schematic["Width"] / 2 # Center the object on the X axis | |
columns.each_with_index do |(block, data), y| | |
y -= @schematic["Length"] / 2 # Center the object on the Y axis | |
line = "#{y},#{x},#{z}:#{block}.#{data}" | |
if NEEDY_BLOCKS.include?(block) | |
deferred << line | |
else | |
io.puts(line) | |
end | |
end | |
end | |
end | |
# Write needy blocks to the end of the BOB file, respecting the order of | |
# NEEDY_BLOCKS, in case some blocks are needier than others. | |
deferred.sort! { |a, b| NEEDY_BLOCKS.index(a) <=> NEEDY_BLOCKS.index(b) } | |
deferred.reverse.each { |line| io.puts(line) } | |
end | |
__END__ | |
[META] | |
version=2.0 | |
randomRotation=False | |
[DATA] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Copyright (c) 2011 Ross Paffett | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment