-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmkmap
executable file
·409 lines (385 loc) · 10.9 KB
/
mkmap
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
#!/usr/bin/python3
import sys
from PIL import Image, ImageDraw
data = open('Metal Gear (Japan).rom' if len(sys.argv) < 2 else sys.argv[1], 'rb').read()
def read16(addr):
return data[addr] | data[addr + 1] << 8
def makefloor(start, rooms):
board = {start: (0, 0)}
xy = {board[start]: start}
stack = [start]
repeat = {}
def get(room):
origroom = room
if 0x7e <= room < 0xd0 or 0xe3 <= room < 0xf0:
#print('wrong room: %x' % room)
return (0xff,) * 4
if 0xd0 <= room < 0xe3:
room += 0x7e - 0xd0
if 0xf0 <= room:
room += 0x7e - 0xd0
room += 0xe3 - 0xf0
u, d, l, r = data[0xae0e + 4 * room:0xae0e + 4 * (room + 1)]
if origroom in (0xde, 0xf0): # 0xde does not exist; f0 has wrong exits.
# Special case, because it's wrong in the source (in a harmless and unexploitable way).
print('original %x exits: %x %x %x %x' % (origroom, u, d, l, r))
return (0xff,) * 4
return u, d, l, r
dirs = ((0, -1), (0, 1), (-1, 0), (1, 0))
bbox = [0, 0, 0, 0]
def bind(pos):
if pos[0] < bbox[0]:
bbox[0] = pos[0]
if pos[1] < bbox[1]:
bbox[1] = pos[1]
if pos[0] > bbox[2]:
bbox[2] = pos[0]
if pos[1] > bbox[3]:
bbox[3] = pos[1]
while len(stack) > 0:
room = stack.pop()
exits = get(room)
pos = board[room]
rooms.difference_update((room,))
#print('handling %x' % room)
for d, exit in zip(dirs, exits):
if exit == 0xff:
continue
target = pos[0] + d[0], pos[1] + d[1]
if room == exit:
if d[1] != 0:
print('wrong repeating exit')
repeat[room] = True
else:
if exit in board:
#print('%x already on board' % exit)
if target != board[exit]:
print('mismatch: %x at %s has exit %s to %x, but that is already on the board at %s' % (room, board[room], d, exit, board[exit]))
else:
#print('adding %x to board at %s' % (exit, target))
board[exit] = target
xy[target] = exit
#if exit < 0xc0:
stack.append(exit)
bind(target)
ret = []
im = Image.new('RGBA', (32 * 8 * (bbox[2] - bbox[0] + 1), 24 * 8 * (bbox[3] - bbox[1] + 1)), (0, 0, 0, 0))
for y in range(bbox[1], bbox[3] + 1):
line = []
for x in range(bbox[0], bbox[2] + 1):
if (x, y) in xy:
room = xy[(x, y)]
if room not in repeat:
line.append(' %02x ' % room)
else:
line.append('<%02x>' % room)
im.paste(build_floor(room), (32 * 8 * (x - bbox[0]), 24 * 8 * (y - bbox[1])))
else:
line.append(' ')
ret.append(' '.join(line))
return ret, im, bbox[0] != bbox[2] or bbox[1] != bbox[3]
desc = (
None,
'handgun',
'smg',
'grenade launcher',
'rocket launcher',
'plastic explosives',
'mine',
'missile',
'silencer',
'armor',
'bomb blast suit',
'flashlight',
'infrared goggles',
'gas mask',
'cigarettes',
'mine detector',
'antenna',
'binoculars',
'oxygen tanks',
'compass',
'parachute',
'antidote',
'card 1',
'card 2',
'card 3',
'card 4',
'card 5',
'card 6',
'card 7',
'card 8',
'ration',
'transceiver',
'uniform',
'box',
'bag',
'ammo',
)
def get_exits(room):
'''Get special exits from room (through doors)'''
if 0xe1 <= room < 0xf0 or room >= 0xfb:
return '', ()
if room >= 0xf0:
room -= 0xf0 - 0xe1
page = 0xd * 0x2000 - 0x6000
addr = read16(page + 0xa7c0 + 2 * room)
ret = []
line = []
# For some reason, the direction is encoded as where to leave + 1.
# This is strange, because the code is used for where to enter.
while data[page + addr] != 0xff:
ret.append({'dir': data[page + addr + 1], 'x': data[page + addr + 3], 'y': data[page + addr + 2], 'room': data[page + addr + 4], 'door': data[page + addr]})
line.append('%02x' % data[page + addr + 4])
addr += 5
return ' '.join(line), ret
#start = {
# 0x00: '*.1',
# 0x10: '1.2',
# 0x1c: '1.3',
# 0x28: '1.4',
# 0x36: '1.0',
# 0x4f: '2.2',
# 0x58: '2.3',
# 0x5d: '2.0',
# 0x6f: '3.0',
# 0xf2: 'e0',
# }
def print_floors(single = False):
rooms = set(range(0xfb))
#rooms.difference_update(range(0x7e, 0xd0))
#rooms.difference_update(range(0xe3, 0xf0))
while len(rooms) > 0:
start = min(rooms)
floors, im, multi = makefloor(start, rooms)
if multi:
print('%02x:' % start)
print('\n'.join(floors))
im.save('/tmp/floor-%02x.png' % start)
if single:
break
def build_line(defs, num, line):
src = defs + 16 * (num - 1) + 4 * line
return tuple(data[src + i] for i in range(4))
def read_nybble(page, base, addr):
a = data[page + base + addr // 2]
if not (addr & 1):
a >>= 4
return a & 0xf
def build_floor(room):
chars = build_tiles(room)
page = 0xd * 0x2000 - 0x6000
# Page d, address 6000[room]
blocks = page + read16(page + 0x6000 + 2 * room)
a = read_nybble(page, 0x61f6, room)
block = page + read16(page + 0x6274 + (a - 1) * 2)
im = Image.new('RGB', (8 * 4 * 8, 6 * 4 * 8))
draw = ImageDraw.ImageDraw(im)
for y in range(6):
for y2 in range(4):
for x in range(8):
for ic, c in enumerate(build_line(block, data[blocks + y * 8 + x], y2)):
if c in chars:
im.paste(chars[c], (8 * (4 * x + ic), 8 * (4 * y + y2)))
else:
#print('not placing char %02x' % c)
pass
for x, y, item in get_objs(room):
item -= 1
text = None
if item < 0x4:
w = 4
start = 4 * item
elif item < 0xc:
w = 2
start = 4 * 4 + 2 * (item - 0x4)
elif item < 0x15:
w = 2
start = 0x40 + 2 * (item - 0xc)
elif item < 0x15 + 8:
w = 2
start = 0x40 + 2 * (0x15 - 0xc)
text = '%d' % (item - 0x15 + 1)
else:
w = 2
start = 0x40 + 2 * (item - 0xc - 7)
for iy in range(2):
for ix in range(w):
c = start + 0x20 * iy + ix
if (0x100 + c) in chars:
im.paste(chars[0x100 + c], (x + 8 * ix, y + 8 * iy), chars[0x100 + c])
else:
print('invalid char %x' % c)
if text is not None:
draw.text((x + 8, y + 8), text)
draw.text((4, 4), '%02x' % room)
used = {}
for exit in get_exits(room)[1]:
pos = (exit['x'], exit['y'])
if pos not in used:
used[pos] = 0
else:
used[pos] += 1
pos = (pos[0], pos[1] + 20 * used[pos])
doortype = data[page + 0xb046 + exit['door'] - 1]
if 2 <= doortype < 2 + 8:
typetext = '%d' % (doortype - 1)
elif 0x42 <= doortype < 0x42 + 8:
typetext = '[%d]' % (doortype - 0x41)
elif doortype in (0x10, 0x30):
typetext = 'explode'
elif doortype == 0x41:
typetext = 'elevator'
elif doortype == 0x81:
typetext = 'open'
elif doortype == 0x8a:
typetext = 'moving lorry'
elif doortype == 0x8b:
typetext = 'lorry'
elif doortype == 0x4c:
typetext = 'uniform'
elif doortype == 0x2f:
typetext = 'wallpunch'
elif doortype == 0xa:
typetext = 'doorpunch'
elif doortype == 0xd:
typetext = 'jennifer'
elif doortype == 0xe:
typetext = 'kill'
else:
print('unknown door type %x for door %x from room %x to %x' % (doortype, exit['door'], room, exit['room']))
typetext = 'unknown'
if room != exit['room']:
draw.text((pos[0], pos[1] + (-20 if pos[1] > 0x60 else 10)), '%02x' % exit['room'], fill = '#ffff00')
draw.text((pos[0], pos[1] + (-10 if pos[1] > 0x60 else 20)), typetext, fill = '#ffff00')
return im
def add_chars(first, num, addr, pilpalette, items = False):
ret = {}
#print('building char %x+%x from %x' % (first, num, addr))
if items:
palette = [0, 6, 7, 8, 0xa, 0xc, 0xe, 0xf]
else:
palette = [1, 3, 5, 8, 9, 0xc, 0xe, 0xf]
sampledpilpalette = [
(73, 73, 36, 255),
(36, 36, 0, 255),
(73, 73, 73, 255),
(255, 0, 0, 255),
(36, 36, 36, 255),
(109, 109, 109, 255),
(255, 255, 255, 255),
(0, 0, 0, 255)
]
for i in range(8):
if i not in pilpalette:
pilpalette[i] = sampledpilpalette[i]
for n in range(num):
ret[first + n] = Image.new('RGBA', (8, 8))
for y in range(8):
e = data[addr + 0 + (n * 8 + y) * 3]
d = data[addr + 1 + (n * 8 + y) * 3]
c = data[addr + 2 + (n * 8 + y) * 3]
line = []
for x in range(8):
pixel = (((e << 3) >> (10 - x)) & 1) | (((d << 3) >> (9 - x)) & 2) | (((c << 3) >> (8 - x)) & 4)
line.append(palette[pixel])
if pixel not in pilpalette:
print('wrong color %x in tile; not %s' % (pixel, tuple(pilpalette.keys())))
continue
ret[first + n].putpixel((x, y), pilpalette[pixel])
#print(' '.join('%x' % k for k in line))
return ret
def build_palette(room):
ret = {0: (0, 0, 0, 0)}
if room is None: # items
page = 0x1 * 0x2000 - 0x6000
palette = [0, 6, 7, 8, 0xa, 0xc, 0xe, 0xf]
addr = 0x4aa3
code = 0xbad # for error message
else:
page = 0x4 * 0x2000 - 0x6000
palette = [1, 3, 5, 8, 9, 0xc, 0xe, 0xf]
code = read_nybble(page, 0x8b40, room)
addr = read16(page + 0x8bbe + 2 * code)
while True:
c = data[page + addr]
if c == 0xff:
break
if c not in palette:
print('wrong color %x in palette type %x' % (c, code))
addr += 3
continue
r = (((data[page + addr + 1] >> 4) & 0x7) * 0x49) >> 1
b = ((data[page + addr + 1] & 0x7) * 0x49) >> 1
g = ((data[page + addr + 2] & 0x7) * 0x49) >> 1
ret[palette.index(c)] = (r, g, b, 255 if c != 0 else 0)
addr += 3
#print('palette: %x: %s' % (code, ret.keys()))
return ret
def build_tiles(room):
itempalette = build_palette(None)
palette = build_palette(room)
ret = {}
ret.update(add_chars(0x100, 0x84, 0xa75d + 0x4 * 0x2000 - 0x6000, itempalette))
ret.update(add_chars(0x92, 4, 0xb2bf + 0xd * 0x2000 - 0x6000, palette))
ret.update(add_chars(0xa0, 8, 0x9509 + 0x7 * 0x2000 - 0x6000, palette))
for n in range(8):
ret[0xd0 + n] = ret[0xa0 + n].transpose(Image.FLIP_LEFT_RIGHT)
page = 0x7 * 0x2000 - 0x6000
charset = read_nybble(page, 0x6000, room)
base_ix = read16(page + 0x607e + 2 * charset)
for i in range(3):
ix = base_ix + 5 * i
if data[page + ix + 0] & 0x80:
#print('stop')
break
if data[page + ix + 0] & 0x40:
# Use first and num from last time.
copy = data[page + ix + 1]
#print('copy %x+%x->%x' % (first, num, copy))
for n in range(num):
ret[copy + n] = ret[first + n].transpose(Image.FLIP_LEFT_RIGHT)
continue
num = data[page + ix + 1]
first = data[page + ix + 2]
addr = read16(page + ix + 3)
ret.update(add_chars(first, num, page + addr, palette))
if True:
maxchar = max(ret)
im = Image.new('RGBA', (maxchar * 8 + 8, 8))
for c in ret:
im.paste(ret[c], (c * 8, 0))
im.save('/tmp/tiles-%02x.png' % room)
return ret
def get_objs(room):
if not 0x7a <= room < 0xdb:
return ()
page = 0x4 * 0x2000 - 0x6000
code = data[page + 0xb9fc + (room - 0x7a)]
if code == 0:
return ()
addr = read16(page + 0xba5d + 2 * (code - 1))
items = []
while data[page + addr] != 0xff:
item = data[page + addr]
y = data[page + addr + 1]
x = data[page + addr + 2]
items.append((x, y, item))
addr += 3
return items
def print_objs():
for room in range(0x7a, 0xdb):
items = [desc[item] for x, y, item in get_objs(room)]
print('Room %02x (%s): %s' % (room, get_exits(room)[0], ', '.join(items)))
def print_exits():
for room in range(0xe1):
print('%x: %s' % (room, get_exits(room)[0]))
print_floors()
#print_objs()
#print_exits()
#build_floor(0x1f)
#build_tiles(0xfb)
#for i in range(0x100):
# build_floor(i).save('/tmp/floor-%02x.png' % i)
#for i in range(0x10):
# build_palette(i)