-
Notifications
You must be signed in to change notification settings - Fork 9
/
dump_levels_and_enemies.py
131 lines (94 loc) · 3.5 KB
/
dump_levels_and_enemies.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
import struct
enemy_names = []
with open("enemies.asm", "r") as f:
for line in f:
enemy_names.append(line.split(" ")[0])
with open("baserom.gb", "rb") as f:
rom = f.read()
BANKS = 4
BANK_SIZE = 1 << 16 >> 2
ROM_SIZE = BANKS * BANK_SIZE
assert len(rom) == ROM_SIZE
# World "5" is the start menu and the hangar after a boss level
world_banks = [2, 1, 3, 1, 2]
def rom_offset(bank, address):
assert address >= 0x4000
return (bank - 1) * 0x4000 + address
def read_byte(bank, address):
return rom[rom_offset(bank, address)]
def read_word(bank, address):
offset = rom_offset(bank, address)
return struct.unpack("<H", rom[offset: offset + 2])[0]
def dump_block(bank, block_pointer):
column_pointer = block_pointer
columns = 0
byte = 0
while columns < 20:
byte = read_byte(bank, column_pointer)
column_pointer += 1
if byte == 0xFE:
columns += 1
print("%04x - %04x" % (block_pointer, column_pointer))
filename = "levels/block-%d:%4x.bin" % (bank, block_pointer)
with open(filename, "wb") as f:
f.write(rom[block_pointer: column_pointer])
blocks_dumped[block_pointer] = column_pointer
def level_index_to_world_level(index):
world = (index // 3)
level = index - world*3
return "%d-%d" % (world+1,level+1)
def dump_level(levelname, bank, level_pointer):
f.write('SECTION "level %s", ROMX[$%x], BANK[%d]\n\n' % (levelname, level_pointer, bank))
f.write("level_%s:\n" % levelname)
block = 0
while True:
sentinel = read_byte(bank, level_pointer + block * 2)
if sentinel == 0xFF:
break
block_pointer = read_word(bank, level_pointer + block * 2)
if block_pointer not in blocks_dumped:
print("%02d: " % (block), end='')
dump_block(bank, block_pointer)
else:
print("%02d: " % (block))
f.write("\tdw block_%4x\n" % block_pointer)
block += 1
f.write("\tdb $ff\n\n")
def dump_enemy_locations(levelname, bank, enemy_pointer):
g.write('SECTION "level %s enemies", ROMX[$%X], BANK[%d]\n\n' % (levelname, enemy_pointer, bank))
g.write("level_%s_enemies:\n" % levelname)
pointer = enemy_pointer
while True:
progress = read_byte(bank, pointer)
pointer += 1
position = read_byte(bank, pointer)
pointer += 1
enemy_id = read_byte(bank, pointer)
pointer += 1
if progress == 0xFF:
g.write("\tdb $FF ; end of list\n")
if position == 0xFF:
g.write("\tdb $FF ; end of list (again, sigh)\n")
break
g.write("\tdb $%02X, $%02X, %s" % (progress, position, enemy_names[enemy_id & 0x7F]))
if enemy_id & 0x80 == 0x80:
g.write(" | $80")
g.write("\n")
g.write("\n")
f = open("levels/levels.asm", "w")
g = open("levels/enemy_locations.asm", "w")
g.write('INCLUDE "enemies.asm"\n\n')
blocks_dumped = {}
for level in range(12):
levelname = "%d_%d" % (level // 3 + 1, level % 3 + 1)
bank = world_banks[level // 3]
level_pointer = read_word(bank, 0x4000 + 2 * level)
dump_level(levelname, bank, level_pointer)
enemy_pointer = read_word(bank, 0x401A + 2 * level)
dump_enemy_locations(levelname, bank, enemy_pointer)
f.write('SECTION "level %s blocks", ROMX[$%x], BANK[%d]\n\n' % (levelname, min(blocks_dumped.keys()), bank))
for ptr in sorted(blocks_dumped.keys()):
f.write('block_%4x:\n' % ptr)
f.write('INCBIN "block%4x.bin"\n' % ptr)
f.close()
g.close()