summaryrefslogtreecommitdiff
path: root/tools/script_extractor.py
blob: ce8764cfa1ce1ca00da00b4c5c90cbbef80f62f5 (plain)
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
#!/usr/bin/env python3

import argparse

# - (Possibly) add new type of word that looks for matches in the sym file
# - add new one for event flag
def decodeLine(scriptList, game_data, loc, ignore_broken, branchList):
	currLine = scriptList[game_data[loc]]
	ret = "\trun_script " + currLine[0] + "\n"
	loc+=1
	quit = currLine[2]
	for c in currLine[1]:
		if c == "b":
			ret += "\tdb $" + format(game_data[loc],"02x") + "\n"
			loc += 1
		elif c == "w":
			ret += "\tdw $" + format((game_data[loc] + (game_data[loc+1]<<8)),"04x") + "\n"
			loc += 2
		elif c == "j":
			wordLoc = (game_data[loc] + (game_data[loc+1]<<8))
			if wordLoc == 0000:
				ret += "\tdw NO_JUMP\n"
			else:
				ret += "\tdw .ows_" + format(wordLoc+0x8000,"04x") + "\n"
				branchList.append(wordLoc)
			loc += 2
		elif c == "t":
			ret += "\ttx Text" + format((game_data[loc] + (game_data[loc+1]<<8)),"04x") + "\n"
			loc += 2
		elif c == "q":
			print("haven't updated data for this yet")
			if not ignore_broken:
				quit = True
		else:
			print("UNACCEPTED CHARACTER: " + c)
	return (loc, ret, quit)
			
def main2(): # temp
	with open("tcg.gbc", "rb") as file:
		game_data = file.read()
	loc = 0xcb37
	start = 0x36
	end = 0x76 # inclusive
	for i in range(start,end+1):
		print("\tflag_def EVENT_FLAG_" + format(i,"02X") + ","+(" "*11)+"$"\
		 + format(game_data[loc+2*i],"02x") + ", %" + format(game_data[loc+2*i + 1],"08b"))
	print("; " + format(loc + 2*(end+1),"02x"))

def main():
	scriptList = createList()

	with open("tcg.gbc", "rb") as file:
	    game_data = file.read()
	auto = True
	end = False
	ignore_broken = True
	branchList = [0x65ee] # all are bank 3 offsets
	while (len(branchList) > 0):
		loc = branchList.pop(0) + 0x8000
		printScript(game_data, loc, auto, end, ignore_broken, scriptList, branchList)

def printScript(game_data, loc, auto, end, ignore_broken, scriptList, branchList):
	script = ""
	if game_data[loc] != 0xe7:
		#print("Error: first byte was not start_script")
		print(".ows_" + format(loc,"04x"))
	else:
 		
		# TODO this is hacky please don't do this 
		ls = format(loc,"04x")
		lsa = format(loc-0x8000,"04x")
		print("OWSequence_" + ls + ": ; " + ls + " (3:" + lsa + ")" )
		loc += 1
		print("\tstart_script")
	while not end:
		loc, outstr, end = decodeLine(scriptList,game_data,loc,ignore_broken,branchList)
		outstr = outstr[:-1] # [:-1] strips the newline at the end
		if auto:
			print(outstr)
		else:
			input(outstr)
	print("; " + hex(loc))
		

def createList(): # this is a func just so all this can go at the bottom
	# name, arg list, is an ender
	return [
	("OWScript_EndScriptLoop1", "", True),
	("OWScript_CloseAdvancedTextBox", "", False),
	("OWScript_PrintTextString", "t", False),
	("Func_ccdc", "bb", False),
	("OWScript_AskQuestionJump", "tj", False), # more complex behavior too (jumping)
	("OWScript_StartBattle", "bbb", False),
	("OWScript_PrintVariableText", "tt", False),
	("Func_cda8", "bbbb", False),
	("OWScript_PrintTextCloseBox", "t", True),
	("Func_cdcb", "bb", False),
	("Func_ce26", "bb", False),
	("OWScript_CloseTextBox", "", False),
	("OWScript_GiveBoosterPacks", "bbb", False),
	("Func_cf0c", "bbb", False), # more complex behavior too (jumping)
	("Func_cf12", "bbb", False),
	("Func_cf3f", "b", False),
	("Func_cf4c", "b", False),
	("Func_cf53", "w", False), # more complex behavior too (jumping)
	("Func_cf7b", "", False),
	("Func_cf2d", "bbbb", False), # more complex behavior too (jumping + ??)
	("Func_cf96", "w", False), # only jumps? still needs args to do that though
	("Func_cfc6", "b", False),
	("Func_cfd4", "", False),
	("Func_d00b", "", False), # includes something with random and extra data
	("Func_d025", "w", False), # possibly only jumps, still needs args
	("Func_d032", "w", False), # see above
	("Func_d03f", "", False),
	("OWScript_Jump", "w", True), # jumps to d
	("Func_d04f", "", False),
	("Func_d055", "b", False),
	("OWScript_MovePlayer", "bb", False),
	("Func_cee2", "b", False),
	("OWScript_SetDialogName", "b", False),
	("Func_d088", "bw", False),
	("Func_d095", "bbb", False),
	("Func_d0be", "bb", False),
	("OWScript_DoFrames", "b", False),
	("Func_d0d9", "bbw", False), # jumps but still needs args
	("Func_d0f2", "bbw", False), # jumps but still needs args
	("Func_ce4a", "bb", False),
	("Func_ceba", "q", False),
	("Func_d103", "q", False),
	("Func_d125", "b", False),
	("Func_d135", "q", False),
	("Func_d16b", "q", False),
	("Func_cd4f", "q", False),
	("Func_cd94", "q", False),
	("Func_ce52", "q", False),
	("Func_cdd8", "q", False),
	("Func_cdf5", "q", False),
	("Func_d195", "q", False),
	("Func_d1ad", "", False),
	("Func_d1b3", "q", False),
	("OWScript_EndScriptCloseText", "", True), # it calls inc twice but it ends anyway?
	("Func_d244", "q", False),
	("Func_d24c", "q", False),
	("OWScript_OpenDeckMachine", "q", False),
	("Func_d271", "q", False),
	("Func_d36d", "bbbbb", False),
	("Func_ce6f", "bbb", False),
	("Func_d209", "q", False),
	("Func_d38f", "q", False),
	("Func_d396", "q", False),
	("Func_cd76", "q", False),
	("Func_d39d", "q", False),
	("Func_d3b9", "q", False),
	("OWScript_GivePCPack", "b", False),
	("Func_d3d1", "q", False),
	("Func_d3d4", "q", False),
	("Func_d3e0", "", False),
	("Func_d3fe", "q", False),
	("Func_d408", "q", False),
	("Func_d40f", "q", False),
	("Func_d416", "q", False),
	("Func_d423", "q", False),
	("Func_d429", "q", False),
	("Func_d41d", "q", False),
	("Func_d42f", "q", False),
	("Func_d435", "b", False),
	("Func_cce4", "q", False),
	("Func_d2f6", "q", False),
	("Func_d317", "q", False),
	("Func_d43d", "q", False),
	("OWScript_EndScriptLoop2", "q", True),
	("OWScript_EndScriptLoop3", "q", True),
	("OWScript_EndScriptLoop4", "q", True),
	("OWScript_EndScriptLoop5", "q", True),
	("OWScript_EndScriptLoop6", "q", True),
	("OWScript_SetFlagValue", "bb", False),
	("OWScript_JumpIfFlagZero1", "q", False),
	("OWScript_JumpIfFlagNonzero1", "q", False),
	("OWScript_JumpIfFlagEqual", "bbj", False), # also capable of jumping
	("OWScript_JumpIfFlagNotEqual", "bbj", False), # jumps
	("OWScript_JumpIfFlagNotLessThan", "bbj", False),
	("OWScript_JumpIfFlagLessThan", "bbj", False),
	("OWScript_MaxOutFlagValue", "b", False),
	("OWScript_ZeroOutFlagValue", "q", False),
	("OWScript_JumpIfFlagNonzero2", "bj", False),
	("OWScript_JumpIfFlagZero2", "bj", False),
	("OWScript_IncrementFlagValue", "b", False),
	("OWScript_EndScriptLoop7", "q", True),
	("OWScript_EndScriptLoop8", "q", True),
	("OWScript_EndScriptLoop9", "q", True),
	("OWScript_EndScriptLoop10", "q", True)
	]

main()