This repository has been archived by the owner on Sep 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
mmapper.py
426 lines (365 loc) · 11.2 KB
/
mmapper.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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
# This module was originally written by Chris Brannon.
# https://github.com/CMB
# Chris has graciously placed the source code of this module in the public domain.
import cStringIO
import struct
import zlib
from jd2gcal import jd2gcal
from rooms import Room, Exit
UINT8_MAX = 0xff
UINT32_MAX = 0xffffffff
MMAPPER_MAGIC = 0xffb2af01
MMAPPER_VERSIONS = (031, 040, 041, 042)
class MMapperException(Exception):
pass
class IncompleteDataFileException(MMapperException):
pass
class BadMagicNumberException(MMapperException):
pass
class UnsupportedVersionException(MMapperException):
def __init__(self, number):
MMapperException.__init__(self, "Do not support version %d of mmapper data" % number)
class NamedBitFlags(object):
def __init__(self, names_and_bits):
self.map_by_name = {}
self.map_by_number = {}
for name, bit in names_and_bits:
self.map_by_number[1 << (bit - 1)] = name
self.map_by_name[name] = 1 << (bit - 1)
def bits_to_flag_set(self, bits):
flag_set = set()
for num in self.map_by_number.keys():
if bits & num:
flag_set.add(self.map_by_number[num])
return flag_set
# Need to clean up these classes later.
# There's no encapsulation here at all.
class MMapperData(object):
# Fields: rooms, selected (a position)
def __init__(self):
self.rooms = {}
self.marks = []
mobflags = NamedBitFlags([
("rent", 1),
("shop", 2),
("weaponshop", 3),
("armourshop", 4),
("foodshop", 5),
("petshop", 6),
("guild", 7),
("scoutguild", 8),
("mageguild", 9),
("clericguild", 10),
("warriorguild", 11),
("rangerguild", 12),
("smob", 13), # Aggressive mob.
("quest", 14),
("any", 15), # Peaseful mob.
("reserved2", 16)
])
loadflags = NamedBitFlags([
("treasure", 1),
("armour", 2),
("weapon", 3),
("water", 4),
("food", 5),
("herb", 6),
("key", 7),
("mule", 8),
("horse", 9),
("packhorse", 10),
("trainedhorse", 11),
("rohirrim", 12),
("warg", 13),
("boat", 14),
("attention", 15),
("tower", 16), # Player can 'watch' surrounding rooms from this one.
("clock", 17),
("mail", 18),
("stable", 19)
])
exitflags = NamedBitFlags([
("exit", 1),
("door", 2),
("road", 3),
("climb", 4),
("random", 5),
("special", 6),
("no_match", 7),
("flow", 8),
("no_flee", 9),
("damage", 10),
("fall", 11),
("guarded", 12)
])
doorflags = NamedBitFlags([
("hidden", 1),
("needkey", 2),
("noblock", 3),
("nobreak", 4),
("nopick", 5),
("delayed", 6),
("callable", 7),
("knockable", 8),
("magic", 9),
("action", 10)
])
align_type = {
0: "undefined",
1: "good",
2: "neutral",
3: "evil"}
info_mark_type = {
0: "text",
1: "line",
2: "arrow"
}
info_mark_class = {
0: "generic",
1: "herb",
2: "river",
3: "place",
4: "mob",
5: "comment",
6: "road",
7: "object",
8: "action",
9: "locality"
}
light_type = {
0: "undefined",
1: "dark",
2: "lit"}
portable_type = {
0: "undefined",
1: "portable",
2: "notportable"}
ridable_type = {
0: "undefined",
1: "ridable",
2: "notridable"}
sundeath_type = {
0: "undefined",
1: "sundeath",
2: "nosundeath"}
terrain_type = {
0: "undefined",
1: "indoors",
2: "city",
3: "field",
4: "forest",
5: "hills",
6: "mountains",
7: "shallowwater",
8: "water",
9: "rapids",
10: "underwater",
11: "road",
12: "brush",
13: "tunnel",
14: "cavern",
15: "death",
16: "random"
}
def read_uint32(infileobj):
data = infileobj.read(4)
if len(data) != 4:
raise IncompleteDataFileException()
return struct.unpack(">I", data)[0]
def read_int32(infileobj):
data = infileobj.read(4)
if len(data) != 4:
raise IncompleteDataFileException()
return struct.unpack(">i", data)[0]
def read_uint16(infileobj):
data = infileobj.read(2)
if len(data) != 2:
raise IncompleteDataFileException()
return struct.unpack(">H", data)[0]
def read_int16(infileobj):
data = infileobj.read(2)
if len(data) != 2:
raise IncompleteDataFileException()
return struct.unpack(">h", data)[0]
def read_uint8(infileobj):
data = infileobj.read(1)
if len(data) != 1:
raise IncompleteDataFileException()
return struct.unpack("B", data)[0]
def read_int8(infileobj):
data = infileobj.read(1)
if len(data) != 1:
raise IncompleteDataFileException()
return struct.unpack("b", data)[0]
def read_qstring(infileobj):
length = read_uint32(infileobj)
if length == UINT32_MAX:
return ""
ucs_data = infileobj.read(length)
if len(ucs_data) != length:
raise IncompleteDataFileException()
return ucs_data.decode("UTF_16_BE")
def read_exit(version, infileobj):
new_exit = Exit()
if version >= 041:
new_exit.exitFlags = exitflags.bits_to_flag_set(read_uint16(infileobj))
else:
new_exit.exitFlags = exitflags.bits_to_flag_set(read_uint8(infileobj))
if version >= 040:
new_exit.doorFlags = doorflags.bits_to_flag_set(read_uint16(infileobj))
else:
new_exit.doorFlags = doorflags.bits_to_flag_set(read_uint8(infileobj))
new_exit.door = read_qstring(infileobj)
if "door" in new_exit.exitFlags:
new_exit.exitFlags.add("exit")
if not new_exit.door:
new_exit.door = "exit"
# Inbound connections are unneeded.
connection = read_uint32(infileobj)
while connection != UINT32_MAX:
connection = read_uint32(infileobj)
outConnections = []
connection = read_uint32(infileobj)
while connection != UINT32_MAX:
outConnections.append(str(connection))
connection = read_uint32(infileobj)
if not outConnections:
new_exit.to = "undefined"
else:
# We want the last outbound connection.
new_exit.to = outConnections[-1]
return new_exit
def read_exits(version, infileobj):
exits = []
exit_names = ("north", "south", "east", "west", "up", "down", "unknown")
for exit_name in exit_names:
new_exit = read_exit(version, infileobj)
if new_exit.exitFlags:
new_exit.dir = exit_name
exits.append(new_exit)
return exits
def read_room(version, infileobj):
new_room = Room()
new_room.name = read_qstring(infileobj)
new_room.desc = read_qstring(infileobj)
new_room.dynamicDesc = read_qstring(infileobj)
new_room.id = str(read_uint32(infileobj))
new_room.note = read_qstring(infileobj)
new_room.terrain = terrain_type[read_uint8(infileobj)]
new_room.light = light_type[read_uint8(infileobj)]
new_room.align = align_type[read_uint8(infileobj)]
new_room.portable = portable_type[read_uint8(infileobj)]
if version >= 030:
new_room.ridable = ridable_type[read_uint8(infileobj)]
if version >= 041:
new_room.sundeath = sundeath_type[read_uint8(infileobj)]
new_room.mobFlags = mobflags.bits_to_flag_set(read_uint32(infileobj))
new_room.loadFlags = loadflags.bits_to_flag_set(read_uint32(infileobj))
else:
new_room.mobFlags = mobflags.bits_to_flag_set(read_uint16(infileobj))
new_room.loadFlags = loadflags.bits_to_flag_set(read_uint16(infileobj))
new_room.updated = bool(read_uint8(infileobj))
new_room.x = read_int32(infileobj)
new_room.y = read_int32(infileobj)
new_room.z = read_int32(infileobj)
new_room.exits = read_exits(version, infileobj) #[x for x in read_exits(version, infileobj)]
return new_room
class InfoMark(object):
type = "text"
cls = "generic"
rotation_angle = 0.0
def read_mark(version, infileobj):
mark = InfoMark()
mark.name = read_qstring(infileobj)
mark.text = read_qstring(infileobj)
jd = read_uint32(infileobj) # Julian day number
if jd == 0:
# QDate objects don't have a year 0.
jd = None
ms = read_uint32(infileobj) # Milliseconds since midnight
if ms == UINT32_MAX:
ms = None
tz = read_uint8(infileobj) # 0 = local time, 1 = UTC
if tz == UINT8_MAX:
tz = None
mark.time_stamp = jd2gcal(jd, ms, tz)
mark.type = info_mark_type[read_uint8(infileobj)]
if version >= 040:
mark.cls = info_mark_class[read_uint8(infileobj)]
mark.rotation_angle = read_uint32(infileobj) / 100.0
mark.pos1 = {
"x": read_int32(infileobj) / 100.0,
"y": read_int32(infileobj) / 100.0,
"z": read_int32(infileobj)
}
mark.pos2 = {
"x": read_int32(infileobj) / 100.0,
"y": read_int32(infileobj) / 100.0,
"z": read_int32(infileobj)
}
return mark
def decompress_mmapper_data(version, infileobj):
if version >= 042:
# As of version 042 of the MMapper data format, MMapper uses qCompress and qUncompress from the QByteArray class for data compression.
# From the web page at
# https://doc.qt.io/archives/qt-5.7/qbytearray.html#qUncompress
# "Note: If you want to use this function to uncompress external data that was compressed using zlib, you first need to prepend a four byte header to the byte array containing the data. The header must contain the expected length (in bytes) of the uncompressed data, expressed as an unsigned, big-endian, 32-bit integer."
# We can therefore assume that MMapper data files with version 042 or later are compressed using standard zlib with a non-standard 4-byte header.
header = read_uint32(infileobj)
BLOCK_SIZE = 8192
decompressor = zlib.decompressobj()
decompressed_stream = cStringIO.StringIO()
compressed_data = infileobj.read(BLOCK_SIZE)
while compressed_data:
decompressed_stream.write(decompressor.decompress(compressed_data))
compressed_data = infileobj.read(BLOCK_SIZE)
decompressed_stream.seek(0)
return decompressed_stream
def read_mmapper_data(filename):
with open(filename, "rb") as infileobj:
num = read_uint32(infileobj)
if num != MMAPPER_MAGIC:
raise BadMagicNumberException()
version = read_int32(infileobj)
if version not in MMAPPER_VERSIONS:
raise UnsupportedVersionException(version)
decompressed_stream = decompress_mmapper_data(version, infileobj)
data = MMapperData()
data.version = version
rooms_count = read_uint32(decompressed_stream)
marks_count = read_uint32(decompressed_stream)
data.selected = (read_int32(decompressed_stream), read_int32(decompressed_stream), read_int32(decompressed_stream))
for i in xrange(rooms_count):
room = read_room(version, decompressed_stream)
data.rooms[room.id] = room
for i in xrange(marks_count):
data.marks.append(read_mark(version, decompressed_stream))
return data
class Database(object):
"""MMapper database class"""
def __init__(self, fileName):
with open(fileName, 'rb') as infileobj:
num = read_uint32(infileobj)
if num != MMAPPER_MAGIC:
raise BadMagicNumberException()
version = read_int32(infileobj)
if version not in MMAPPER_VERSIONS:
raise UnsupportedVersionException(version)
decompressedStream = decompress_mmapper_data(version, infileobj)
roomsCount = read_uint32(decompressedStream)
marksCount = read_uint32(decompressedStream)
self.selected = (read_int32(decompressedStream), read_int32(decompressedStream), read_int32(decompressedStream))
# iterate through the rooms in the database, creating an object for each room.
deathIDs = []
self.rooms = {}
for i in xrange(roomsCount):
newRoom = read_room(version, decompressedStream)
if newRoom.terrain == "DEATH":
deathIDs.append(newRoom.id)
else:
newRoom.setCost(newRoom.terrain)
self.rooms[newRoom.id] = newRoom
for roomID, room in self.rooms.iteritems():
for item in room.exits:
if item.to in deathIDs:
item.to = "DEATH"