better code for MapScriptHeader

This commit is contained in:
Bryan Bishop 2012-04-15 00:50:48 -05:00
parent 746317f90e
commit 49add12c45

View File

@ -5356,7 +5356,7 @@ class SecondMapHeader:
#TODO: process blockdata ? #TODO: process blockdata ?
#bank appears first #bank appears first
###self.blockdata_address = PointerLabelBeforeBank(address+3) ###self.blockdata_address = PointerLabelBeforeBank(address+3)
self.blockdata = MapBlockData(address+3, map_group=self.map_group, map_id=self.map_id, debug=self.debug) self.blockdata = MapBlockData(address+3, map_group=self.map_group, map_id=self.map_id, debug=self.debug, width=self.width, height=self.height)
#bank appears first #bank appears first
#TODO: process MapScriptHeader #TODO: process MapScriptHeader
###self.script_address = PointerLabelBeforeBank(address+6) ###self.script_address = PointerLabelBeforeBank(address+6)
@ -5539,11 +5539,7 @@ def parse_map_event_header_at(address, map_group=None, map_id=None, debug=True,
"""parse crystal map event header byte structure thing""" """parse crystal map event header byte structure thing"""
return MapEventHeader(address, map_group=map_group, map_id=map_id, debug=debug, bank=bank) return MapEventHeader(address, map_group=map_group, map_id=map_id, debug=debug, bank=bank)
class MapScriptHeader(Command): class MapScriptHeader:
def __init__(self, *args, **kwargs):
def parse(self):
def to_asm(self):
def parse_map_script_header_at(address, map_group=None, map_id=None, debug=True):
"""parses a script header """parses a script header
This structure allows the game to have e.g. one-time only events on a map This structure allows the game to have e.g. one-time only events on a map
@ -5553,7 +5549,7 @@ def parse_map_script_header_at(address, map_group=None, map_id=None, debug=True)
This header a combination of a trigger script section and a callback script This header a combination of a trigger script section and a callback script
section. I don't know if these 'trigger scripts' are the same as the others section. I don't know if these 'trigger scripts' are the same as the others
referenced in the map event header, so this might need to be renamed very referenced in the map event header, so this might need to be renamed very
soon. soon. The scripts in MapEventHeader are called XYTrigger.
trigger scripts: trigger scripts:
[[Number1 of pointers] Number1 * [2byte pointer to script][00][00]] [[Number1 of pointers] Number1 * [2byte pointer to script][00][00]]
@ -5595,59 +5591,57 @@ def parse_map_script_header_at(address, map_group=None, map_id=None, debug=True)
after battle: after battle:
01, 04 01, 04
""" """
print "starting to parse the map's script header.." base_label = "MapScriptHeader_"
def __init__(self, address, map_group=None, map_id=None, debug=True, bank=None, label=None):
self.address = address
self.map_group = map_group
self.map_id = map_id
self.debug = debug
self.bank = bank
if label:
self.label = label
else:
self.label = self.base_label + hex(address)
self.parse()
script_parse_table[address : self.last_address] = self
def parse(self):
address = self.address
map_group = self.map_group
map_id = self.map_id
debug = self.debug
#[[Number1 of pointers] Number1 * [2byte pointer to script][00][00]] #[[Number1 of pointers] Number1 * [2byte pointer to script][00][00]]
ptr_line_size = 4 #[2byte pointer to script][00][00] self.trigger_count = ord(rom[address])
trigger_ptr_cnt = ord(rom[address]) self.triggers = []
trigger_pointers = grouper(rom_interval(address+1, trigger_ptr_cnt * ptr_line_size, strings=False), count=ptr_line_size) ptr_line_size = 4
triggers = {} groups = grouper(rom_interval(address+1, self.trigger_count * ptr_line_size, strings=False), count=ptr_line_size)
for index, trigger_pointer in enumerate(trigger_pointers): current_address = address
for (index, trigger_bytes) in enumerate(groups):
print "parsing a trigger header..." print "parsing a trigger header..."
byte1 = trigger_pointer[0] script = ScriptPointerLabelParam(current_address, map_group=map_group, map_id=map_id, debug=debug)
byte2 = trigger_pointer[1] self.triggers.append(script)
ptr = byte1 + (byte2 << 8) current_address += ptr_line_size
trigger_address = calculate_pointer(ptr, calculate_bank(address)) current_address = address + (self.trigger_count * ptr_line_size) + 1
trigger_script = parse_script_engine_script_at(trigger_address, map_group=map_group, map_id=map_id)
triggers[index] = {
"script": trigger_script,
"address": trigger_address,
"pointer": {"1": byte1, "2": byte2},
}
#bump ahead in the byte stream
address += trigger_ptr_cnt * ptr_line_size + 1
#[[Number2 of pointers] Number2 * [hook number][2byte pointer to script]] #[[Number2 of pointers] Number2 * [hook number][2byte pointer to script]]
callback_ptr_line_size = 3 callback_ptr_line_size = 3
callback_ptr_cnt = ord(rom[address]) self.callback_count = DecimalParam(current_address)
callback_ptrs = grouper(rom_interval(address+1, callback_ptr_cnt * callback_ptr_line_size, strings=False), count=callback_ptr_line_size) self.callbacks = []
callback_pointers = {} for index in range(callback_count):
callbacks = {} hook_byte = HexByte(current_address)
for index, callback_line in enumerate(callback_ptrs): callback = ScriptPointerLabelParam(current_address+1, map_group=map_group, map_id=map_id, debug=debug)
print "parsing a callback header..." callbacks.append({"hook": hook_byte, "callback": callback})
hook_byte = callback_line[0] #1, 2, 3, 4, 5 return True
callback_byte1 = callback_line[1] def to_asm(self):
callback_byte2 = callback_line[2] output = ""
callback_ptr = callback_byte1 + (callback_byte2 << 8) output += "; trigger count\n"
callback_address = calculate_pointer(callback_ptr, calculate_bank(address)) output += "db %d\n\n"%self.trigger_count
callback_script = parse_script_engine_script_at(callback_address) output += "; triggers\n"
callback_pointers[len(callback_pointers.keys())] = [hook_byte, callback_ptr] output += "dw " + "\n".join([p.to_asm() for p in self.triggers]) + "\n\n"
callbacks[index] = { output += "; callbacks\n"
"script": callback_script, #not so sure about this next one
"address": callback_address, output += "\n".join(["dbw "+str(p["hook"])+", "+p["callback"].to_asm() for p in self.callbacks])
"pointer": {"1": callback_byte1, "2": callback_byte2}, return output
} def parse_map_script_header_at(address, map_group=None, map_id=None, debug=True):
return MapScriptHeader(address, map_group=map_group, map_id=map_id, debug=debug)
#XXX do these triggers/callbacks call asm or script engine scripts?
return {
#"trigger_ptr_cnt": trigger_ptr_cnt,
"trigger_pointers": trigger_pointers,
#"callback_ptr_cnt": callback_ptr_cnt,
#"callback_ptr_scripts": callback_ptrs,
"callback_pointers": callback_pointers,
"trigger_scripts": triggers,
"callback_scripts": callbacks,
}
def old_parse_trainer_header_at(address, map_group=None, map_id=None, debug=True): def old_parse_trainer_header_at(address, map_group=None, map_id=None, debug=True):
bank = calculate_bank(address) bank = calculate_bank(address)