rubycraft 0.1.0 → 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -1,191 +0,0 @@
1
- BlockTypeDSL = proc do
2
- transparent_block 0, :air
3
- block 1, :stone
4
- block 2, :grass
5
- block 3, :dirt
6
- block 4, :cobblestone
7
- block 5, :planks
8
- transparent_block 6, :sapling
9
- block 7, :bedrock
10
- block 8, :watersource
11
- block 9, :water
12
- block 10, :lavasource
13
- block 11, :lava
14
- block 12, :sand
15
- block 13, :gravel
16
- block 14, :goldore
17
- block 15, :ironore
18
- block 16, :coal
19
- block 17, :log
20
- block 18, :leaves
21
- block 19, :sponge
22
- transparent_block 20, :glass
23
- block 21, :lapisore
24
- block 22, :lapis
25
- block 23, :dispenser
26
- block 24, :sandstone
27
- block 25, :note
28
- block 26, :bed
29
- transparent_block 27, :powered_rail
30
- transparent_block 28, :detector_rail
31
- block 29, :sticky_piston
32
- transparent_block 30, :cobweb
33
- transparent_block 31, :tall_grass
34
- transparent_block 32, :dead_shrubs
35
- block 33, :piston
36
- block 34, :piston_extension
37
- block 35, :wool
38
- transparent_block 37, :dandelion
39
- transparent_block 38, :rose
40
- transparent_block 39, :brown_mushroom
41
- transparent_block 40, :red_mushroom
42
- block 41, :gold
43
- block 42, :iron
44
- block 43, :slabs
45
- block 44, :slab
46
- block 45, :brick
47
- block 46, :tnt
48
- block 47, :bookshelf
49
- block 48, :mossy
50
- block 49, :obsidian
51
- transparent_block 50, :torch
52
- transparent_block 51, :fire
53
- block 52, :spawner
54
- block 53, :stairs
55
- block 54, :chest
56
- transparent_block 55, :redstone_wire
57
- block 56, :diamond_ore
58
- block 57, :diamond_block
59
- block 58, :crafting_table
60
- block 59, :seeds
61
- block 60, :farmland
62
- block 61, :furnace
63
- block 62, :burning_furnace
64
- transparent_block 63, :signpost
65
- transparent_block 64, :door
66
- transparent_block 65, :ladder
67
- transparent_block 66, :rails
68
- block 67, :cobblestone_stairs
69
- transparent_block 68, :wall_sign
70
- transparent_block 69, :lever
71
- transparent_block 70, :stone_pressure_plate
72
- transparent_block 71, :iron_door
73
- transparent_block 72, :wooden_pressure_plate
74
- block 73, :redstone_ore
75
- block 74, :glowing_redstone_ore
76
- transparent_block 75, :redstone_torch_off
77
- transparent_block 76, :redstone_torch_on
78
- transparent_block 77, :stone_button
79
- block 78, :snow
80
- block 79, :ice
81
- block 80, :snow_block
82
- transparent_block 81, :cactus
83
- block 82, :clay
84
- block 83, :sugar_cane
85
- block 84, :jukebox
86
- transparent_block 85, :fence
87
- block 86, :pumpkin
88
- block 87, :netherrack
89
- block 88, :soulsand
90
- block 89, :glowstone
91
- transparent_block 90, :portal
92
- block 91, :jock_o_lantern
93
- transparent_block 92, :cake
94
- transparent_block 93, :repeater_off
95
- transparent_block 94, :repeater_on
96
- block 95, :locked_chest
97
- transparent_block 96, :trapdoor
98
- end
99
-
100
-
101
- # DSL: color name r, g, b
102
- BlockColorDSL = proc do
103
- white 221, 221, 221
104
- orange 233, 126, 55
105
- magenta 179, 75, 200
106
- light_blue 103, 137, 211
107
- yellow 192, 179, 28
108
- light_green 59, 187, 47
109
- pink 217, 132, 153
110
- dark_gray 66, 67, 67
111
- gray 157, 164, 165
112
- cyan 39, 116, 148
113
- purple 128, 53, 195
114
- blue 39, 51, 153
115
- brown 85, 51, 27
116
- dark_green 55, 76, 24
117
- red 162, 44, 42
118
- black 26, 23, 23
119
- end
120
-
121
- class BlockColor
122
- @typeColor = []
123
-
124
- def self.method_missing(name, *args)
125
- args << @typeColor.size
126
- @typeColor << new(name, *args)
127
- end
128
-
129
- def self.typeColor
130
- @typeColor
131
- end
132
-
133
- attr_reader :name, :r, :g, :b, :data
134
- def initialize(name, r, g, b, data)
135
- @name = name
136
- @r = r
137
- @g = g
138
- @b = b
139
- @data = data
140
- end
141
-
142
- def rgb
143
- [r, g, b]
144
- end
145
-
146
- class_eval &BlockColorDSL
147
- InvertedColor = Hash[typeColor.each_with_index.map { |obj, i| [obj.name, i] }]
148
- end
149
-
150
- # class methods and dsl for block
151
- class BlockType
152
- @blocks = {}
153
- @blocks_by_name = {}
154
- attr_reader :id, :name, :transparent
155
-
156
- def initialize(id, name, transparent)
157
- @id = id
158
- @name = name.to_s
159
- @transparent = transparent
160
- end
161
-
162
- def self.block(id, name, transparent = false)
163
- block = new id, name, transparent
164
- @blocks[id] = block
165
- @blocks_by_name[name.to_s] = block
166
-
167
- end
168
-
169
- def self.transparent_block(id, name)
170
- block id, name, true
171
- end
172
-
173
- def self.get(key)
174
- if @blocks.has_key?(key)
175
- return @blocks[key].clone
176
- end
177
- new(key, "unknown(#{key})", false)
178
- end
179
-
180
- def self.of(key)
181
- self[key]
182
- end
183
-
184
- def self.[](key)
185
- key = key.to_s
186
- return @blocks_by_name[key] if @blocks_by_name.has_key?(key)
187
- raise "no such name: #{key}"
188
- end
189
-
190
- class_eval &BlockTypeDSL
191
- end
@@ -1,46 +0,0 @@
1
- require 'stringio'
2
-
3
- # Utils for manipulating bytes back and forth as strings, strings and numbers
4
- module ByteConverter
5
- def toByteString(array)
6
- array.pack('C*')
7
- end
8
-
9
- def intBytes(i)
10
- [i >> 24, (i >> 16) & 0xFF, (i >> 8) & 0xFF, i & 0xFF]
11
- end
12
-
13
- def stringToByteArray(str)
14
- str.bytes.to_a
15
- end
16
-
17
- def arrayToIO(arr)
18
- io = StringIO.new
19
- io.write toByteString arr
20
- io.rewind
21
- io
22
- end
23
-
24
- def stringToIo(str)
25
- arrayToIO stringToByteArray(str)
26
- end
27
-
28
- def concat(array, enum)
29
- for i in enum
30
- array << i
31
- end
32
- end
33
-
34
- def bytesToInt(array)
35
- array.pack('C*').unpack("N").first
36
- end
37
-
38
- def pad(array, count, value = 0)
39
- count.times do
40
- array << value
41
- end
42
- array
43
- end
44
-
45
- extend self
46
- end
@@ -1,118 +0,0 @@
1
- # Represents a chunk data
2
- require 'nbt_helper'
3
- require 'byte_converter'
4
- require 'block'
5
- require 'matrix3d'
6
-
7
- # Chunks are enumerable over blocks
8
- class Chunk
9
- include Enumerable
10
- include ZlibHelper
11
-
12
- Width = 16
13
- Length = 16
14
- Height = 128
15
-
16
- def self.fromNbt(bytes)
17
- new NbtHelper.fromNbt bytes
18
- end
19
-
20
- def initialize(nbtData)
21
- name, @nbtBody = nbtData
22
- bytes = level["Blocks"].value.bytes
23
- @blocks = matrixfromBytes bytes
24
- @blocks.each_triple_index do |b, z, x, y|
25
- b.pos = [z, x, y]
26
- end
27
- data = level["Data"].value.bytes.to_a
28
- @blocks.each_with_index do |b, index|
29
- v = data[index / 2]
30
- if index % 2 == 0
31
- b.data = v & 0xF
32
- else
33
- b.data = v >> 4
34
- end
35
- end
36
- end
37
-
38
- # Iterates over the blocks
39
- def each(&block)
40
- @blocks.each &block
41
- end
42
-
43
-
44
- # Converts all blocks on data do another type. Gives the block and sets
45
- # the received name
46
- def block_map(&block)
47
- each { |b| b.name = yield b }
48
- end
49
-
50
- # Converts all blocks on data do another type. Gives the block name sets
51
- # the received name
52
- def block_type_map(&block)
53
- each { |b| b.name = yield b.name.to_sym }
54
- end
55
-
56
- def [](z, x, y)
57
- @blocks[z, x, y]
58
- end
59
-
60
- def []=(z, x, y, value)
61
- @blocks[z, x, y] = value
62
- end
63
-
64
- def export
65
- level["Data"] = byteArray exportLevelData
66
- level["Blocks"] = byteArray @blocks.map { |b| b.id }
67
- level["HeightMap"] = byteArray exportHeightMap
68
- ["", @nbtBody]
69
- end
70
-
71
- def toNbt
72
- NbtHelper.toBytes export
73
- end
74
-
75
- protected
76
- def exportHeightMap
77
- zwidth, xwidth, ywidth = @blocks.bounds
78
- matrix = Array.new(zwidth) { Array.new(xwidth) { 1 }}
79
- @blocks.each_triple_index do |b, z, x, y|
80
- unless b.transparent
81
- matrix[z][x] = [matrix[z][x], y + 1].max
82
- end
83
- end
84
- ret = []
85
- matrix.each do |line|
86
- line.each do |height|
87
- ret << height
88
- end
89
- end
90
- ret
91
- end
92
-
93
- def level
94
- @nbtBody["Level"]
95
- end
96
-
97
- def exportLevelData
98
- data = []
99
- @blocks.each_with_index do |b, i|
100
- if i % 2 == 0
101
- data << b.data
102
- else
103
- data[i / 2] += (b.data << 4)
104
- end
105
- end
106
- data
107
- end
108
-
109
- def byteArray(data)
110
- NBTFile::Types::ByteArray.new ByteConverter.toByteString(data)
111
- end
112
-
113
- def matrixfromBytes(bytes)
114
- Matrix3d.new(Width, Length, Height).fromArray bytes.map {|byte| Block.get(byte) }
115
- end
116
-
117
-
118
- end
@@ -1,86 +0,0 @@
1
- #!/usr/bin/env ruby
2
- class IndexOutOfBoundsError < StandardError
3
-
4
- end
5
-
6
- class Matrix3d
7
- include Enumerable
8
-
9
- def bounds
10
- [@xlimit, @ylimit, @zlimit]
11
- end
12
-
13
- def initialize(d1,d2,d3)
14
- @xlimit = d1
15
- @ylimit = d2
16
- @zlimit = d3
17
- @data = Array.new(d1) { Array.new(d2) { Array.new(d3) } }
18
- end
19
-
20
- def [](x, y, z)
21
- @data[x][y][z]
22
- end
23
-
24
- def []=(x, y, z, value)
25
- @data[x][y][z] = value
26
- end
27
-
28
- def put(index, value)
29
- ar = indexToArray(index)
30
- self[*ar] = value
31
- end
32
-
33
- def get(index)
34
- ar = indexToArray(index)
35
- self[*ar]
36
- end
37
-
38
- def each(&block)
39
- for z in @data
40
- for y in z
41
- for x in y
42
- yield x
43
- end
44
- end
45
- end
46
- end
47
-
48
- def each_triple_index(&block)
49
- return enum_for:each_triple_index unless block_given?
50
- @data.each_with_index do |plane, x|
51
- plane.each_with_index do |column, y|
52
- column.each_with_index do |value, z|
53
- yield value, x ,y ,z
54
- end
55
- end
56
- end
57
- end
58
-
59
- #Actually from any iterable
60
- def fromArray(ar)
61
- ar.each_with_index { |obj,i| put i, obj }
62
- return self
63
- end
64
-
65
-
66
- def to_a(default = nil)
67
- map do |x|
68
- if x.nil?
69
- default
70
- else
71
- x
72
- end
73
- end
74
- end
75
-
76
- protected
77
- def indexToArray(index)
78
- x = index / (@zlimit * @ylimit)
79
- index -= x * (@zlimit * @ylimit)
80
- y = index / @zlimit
81
- z = index % @zlimit
82
- return x, y, z
83
- end
84
-
85
-
86
- end
@@ -1,48 +0,0 @@
1
- #!/usr/bin/env ruby
2
- require 'nbtfile'
3
- require 'zlib'
4
- require 'byte_converter'
5
- # Patching nbtfile clases so that they don't gzip/ungzip incorrectly the zlib bytes from
6
- #mcr files. Use the methods from ZlibHelper
7
- class NBTFile::Private::Tokenizer
8
- def initialize(io)
9
- @gz = io
10
- @state = NBTFile::Private::TopTokenizerState.new
11
- end
12
- end
13
-
14
- class NBTFile::Emitter
15
- def initialize(stream)
16
- @gz = stream
17
- @state = NBTFile::Private::TopEmitterState.new
18
- end
19
- end
20
-
21
- module ZlibHelper
22
- def compress(str)
23
- Zlib::Deflate.deflate(str)
24
- end
25
-
26
- def decompress(str)
27
- Zlib::Inflate.inflate(str)
28
- end
29
- extend self
30
- end
31
-
32
- # Handles converting bytes to/from nbt regions, which are compressesed/decompress
33
- module NbtHelper
34
- extend ByteConverter
35
- extend ZlibHelper
36
-
37
- module_function
38
- def fromNbt(bytes)
39
- NBTFile.read stringToIo decompress toByteString bytes
40
- end
41
-
42
- def toBytes(nbt)
43
- output = StringIO.new
44
- name, body = nbt
45
- NBTFile.write(output, name, body)
46
- stringToByteArray compress output.string
47
- end
48
- end