forked from Zeg9/onomatopoeia
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathmap.py
142 lines (119 loc) · 4.04 KB
/
map.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
import sqlite3
import io
import zlib
import array
import os.path
from util import *
class Map(object):
def __init__(self, path):
self.conn = sqlite3.connect(os.path.join(path, "map.sqlite"))
def getCoordinatesToDraw(self):
result = set()
cur = self.conn.cursor()
cur.execute("SELECT `pos` FROM `blocks`")
while True:
r = cur.fetchone()
if not r:
break
x, y, z = getIntegerAsBlock(r[0])
result.add(coordsToGrid(x, z))
return result
def getBlock(self, x, y, z):
cur = self.conn.cursor()
cur.execute("SELECT `data` FROM `blocks` WHERE `pos`==? LIMIT 1", (getBlockAsInteger(x, y, z), ))
r = cur.fetchone()
if not r:
return DummyMapBlock()
f = io.BytesIO(r[0])
version = readU8(f)
flags = f.read(1)
# Check flags
is_underground = ((ord(flags) & 1) != 0)
day_night_differs = ((ord(flags) & 2) != 0)
lighting_expired = ((ord(flags) & 4) != 0)
generated = ((ord(flags) & 8) != 0)
if version >= 27:
lighting_complete = readU16(f)
if version >= 22:
content_width = readU8(f)
params_width = readU8(f)
# Node data
dec_o = zlib.decompressobj()
try:
mapdata = array.array("B", dec_o.decompress(f.read()))
except:
mapdata = []
# Reuse the unused tail of the file
f.close()
f = io.BytesIO(dec_o.unused_data)
# zlib-compressed node metadata list
dec_o = zlib.decompressobj()
try:
metaliststr = array.array("B", dec_o.decompress(f.read()))
# And do nothing with it
except:
metaliststr = []
# Reuse the unused tail of the file
f.close()
f = io.BytesIO(dec_o.unused_data)
data_after_node_metadata = dec_o.unused_data
if version <= 21:
# mapblockobject_count
readU16(f)
if version == 23:
readU8(f) # Unused node timer version (always 0)
if version == 24:
ver = readU8(f)
if ver == 1:
num = readU16(f)
for i in range(0, num):
readU16(f)
readS32(f)
readS32(f)
static_object_version = readU8(f)
static_object_count = readU16(f)
for i in range(0, static_object_count):
# u8 type (object type-id)
object_type = readU8(f)
# s32 pos_x_nodes * 10000
pos_x_nodes = readS32(f) / 10000
# s32 pos_y_nodes * 10000
pos_y_nodes = readS32(f) / 10000
# s32 pos_z_nodes * 10000
pos_z_nodes = readS32(f) / 10000
# u16 data_size
data_size = readU16(f)
# u8[data_size] data
data = f.read(data_size)
timestamp = readU32(f)
id_to_name = {}
if version >= 22:
name_id_mapping_version = readU8(f)
num_name_id_mappings = readU16(f)
for i in range(0, num_name_id_mappings):
node_id = readU16(f)
name_len = readU16(f)
name = f.read(name_len)
id_to_name[node_id] = name
# Node timers
if version >= 25:
timer_size = readU8(f)
num = readU16(f)
for i in range(0, num):
readU16(f)
readS32(f)
readS32(f)
#print(id_to_name)
#print(mapdata)
return MapBlock(id_to_name, mapdata)
class MapBlock(object):
def __init__(self, id_to_name, mapdata, version=99):
self.id_to_name = id_to_name
self.mapdata = mapdata
self.version = version
def get(self, x, y, z):
datapos = x + y * 16 + z * 256
return self.id_to_name[(self.mapdata[datapos * 2] << 8) | (self.mapdata[datapos * 2 + 1])]
class DummyMapBlock(object):
def get(self, x, y, z):
return "default:air"