+# 3-dimensional vector (position)
+class v3:
+ def __init__(self, x=0, y=0, z=0):
+ self.X = x
+ self.Y = y
+ self.Z = z
+
+class NodeMeta:
+ def __init__(self, type_id, data):
+ self.type_id = type_id
+ self.data = data
+
+class StaticObject:
+ def __init__(self):
+ self.type_id = 0
+ self.data = ""
+
+def ser_u16(i):
+ return chr((i>>8)&0xff) + chr((i>>0)&0xff)
+def ser_u32(i):
+ return (chr((i>>24)&0xff) + chr((i>>16)&0xff)
+ + chr((i>>8)&0xff) + chr((i>>0)&0xff))
+
+# A 16x16x16 chunk of map
+class MapBlock:
+ def __init__(self):
+ self.content = array.array('B')
+ self.param1 = array.array('B')
+ self.param2 = array.array('B')
+ for i in range(16*16*16):
+ # Initialize to air
+ self.content.append(254)
+ # Full light on sunlight, none when no sunlight
+ self.param1.append(15)
+ # No additional parameters
+ self.param2.append(0)
+
+ # key = v3 pos
+ # value = NodeMeta
+ self.nodemeta = {}
+
+ # key = v3 pos
+ # value = StaticObject
+ self.static_objects = {}
+
+ def set_content(self, v3, b):
+ self.content[v3.Z*16*16+v3.Y*16+v3.X] = b
+ def set_param1(self, v3, b):
+ self.param1[v3.Z*16*16+v3.Y*16+v3.X] = b
+ def set_param2(self, v3, b):
+ self.param2[v3.Z*16*16+v3.Y*16+v3.X] = b
+
+ # Get data for serialization. Returns a string.
+ def serialize_data(self):
+ s = ""
+ for i in range(16*16*16):
+ s += chr(self.content[i])
+ for i in range(16*16*16):
+ s += chr(self.param1[i])
+ for i in range(16*16*16):
+ s += chr(self.param2[i])
+ return s
+
+ def serialize_nodemeta(self):
+ s = ""
+ s += ser_u16(1)
+ s += ser_u16(len(self.nodemeta))
+ for pos, meta in self.nodemeta.items():
+ pos_i = pos.Z*16*16 + pos.Y*16 + pos.X
+ s += ser_u16(pos_i)
+ s += ser_u16(meta.type_id)
+ s += ser_u16(len(meta.data))
+ s += meta.data
+ return s
+
+ def serialize_staticobj(self):
+ s = ""
+ s += chr(0)
+ s += ser_u16(len(self.static_objects))
+ for pos, obj in self.static_objects.items():
+ pos_i = pos.Z*16*16 + pos.Y*16 + pos.X
+ s += ser_s32(pos.X*1000)
+ s += ser_s32(pos.Y*1000)
+ s += ser_s32(pos.Z*1000)
+ s += ser_u16(obj.type_id)
+ s += ser_u16(len(obj.data))
+ s += obj.data
+ return s
+
+def writeblock(mapdir, px,py,pz, block):
+
+ sectordir = mapdir + get_sector_dir(px, pz);
+
+ try:
+ os.makedirs(sectordir)
+ except OSError:
+ pass
+
+ path = sectordir+"/"+to4h(py)
+
+ print("writing block file "+path)
+
+ f = open(sectordir+"/"+to4h(py), "wb")
+
+ if f == None:
+ return
+
+ # version
+ version = 17
+ f.write(struct.pack('B', version))
+
+ # flags
+ # 0x01=is_undg, 0x02=dn_diff, 0x04=lighting_expired
+ flags = 0 + 0x02 + 0x04
+ f.write(struct.pack('B', flags))
+
+ # data
+ c_obj = zlib.compressobj()
+ c_obj.compress(block.serialize_data())
+ f.write(struct.pack('BB', 0x78, 0x9c)) # zlib magic number
+ f.write(c_obj.flush())
+
+ # node metadata
+ c_obj = zlib.compressobj()
+ c_obj.compress(block.serialize_nodemeta())
+ f.write(struct.pack('BB', 0x78, 0x9c)) # zlib magic number
+ f.write(c_obj.flush())
+
+ # mapblockobject count
+ f.write(ser_u16(0))
+
+ # static objects
+ f.write(block.serialize_staticobj())