summaryrefslogtreecommitdiff
path: root/extras/crystal.py
diff options
context:
space:
mode:
Diffstat (limited to 'extras/crystal.py')
-rw-r--r--extras/crystal.py189
1 files changed, 39 insertions, 150 deletions
diff --git a/extras/crystal.py b/extras/crystal.py
index 8a2b337f6..d1741f19c 100644
--- a/extras/crystal.py
+++ b/extras/crystal.py
@@ -6,9 +6,8 @@ import subprocess
from new import classobj
import random
-# for IntervalMap
-from bisect import bisect_left, bisect_right
-from itertools import izip
+# for capwords
+import string
# for testing all this crap
try:
@@ -16,9 +15,6 @@ try:
except ImportError:
import unittest
-# for capwords
-import string
-
# Check for things we need in unittest.
if not hasattr(unittest.TestCase, 'setUpClass'):
sys.stderr.write("The unittest2 module or Python 2.7 is required to run this script.")
@@ -31,6 +27,14 @@ if not hasattr(json, "dumps"):
if not hasattr(json, "read"):
json.read = json.loads
+from labels import (
+ remove_quoted_text,
+ line_has_comment_address,
+ line_has_label,
+ get_label_from_line,
+ get_address_from_line_comment
+)
+
spacing = "\t"
lousy_dragon_shrine_hack = [0x18d079, 0x18d0a9, 0x18d061, 0x18d091]
@@ -73,122 +77,14 @@ import re
trainer_group_pointer_table_address = 0x39999
trainer_group_pointer_table_address_gs = 0x3993E
-class Size():
- """a simple way to track whether or not a size
- includes the first value or not, like for
- whether or not the size of a command in a script
- also includes the command byte or not"""
-
- def __init__(self, size, inclusive=False):
- self.inclusive = inclusive
- if inclusive: size = size-1
- self.size = size
-
- def inclusive(self):
- return self.size + 1
-
- def exclusive(self):
- return self.size
-
-class IntervalMap(object):
- """
- This class maps a set of intervals to a set of values.
-
- >>> i = IntervalMap()
- >>> i[0:5] = "hello world"
- >>> i[6:10] = "hello cruel world"
- >>> print i[4]
- "hello world"
- """
-
- def __init__(self):
- """initializes an empty IntervalMap"""
- self._bounds = []
- self._items = []
- self._upperitem = None
-
- def __setitem__(self, _slice, _value):
- """sets an interval mapping"""
- assert isinstance(_slice, slice), 'The key must be a slice object'
-
- if _slice.start is None:
- start_point = -1
- else:
- start_point = bisect_left(self._bounds, _slice.start)
-
- if _slice.stop is None:
- end_point = -1
- else:
- end_point = bisect_left(self._bounds, _slice.stop)
-
- if start_point>=0:
- if start_point < len(self._bounds) and self._bounds[start_point]<_slice.start:
- start_point += 1
-
- if end_point>=0:
- self._bounds[start_point:end_point] = [_slice.start, _slice.stop]
- if start_point < len(self._items):
- self._items[start_point:end_point] = [self._items[start_point], _value]
- else:
- self._items[start_point:end_point] = [self._upperitem, _value]
- else:
- self._bounds[start_point:] = [_slice.start]
- if start_point < len(self._items):
- self._items[start_point:] = [self._items[start_point], _value]
- else:
- self._items[start_point:] = [self._upperitem]
- self._upperitem = _value
- else:
- if end_point>=0:
- self._bounds[:end_point] = [_slice.stop]
- self._items[:end_point] = [_value]
- else:
- self._bounds[:] = []
- self._items[:] = []
- self._upperitem = _value
-
- def __getitem__(self,_point):
- """gets a value from the mapping"""
- assert not isinstance(_point, slice), 'The key cannot be a slice object'
-
- index = bisect_right(self._bounds, _point)
- if index < len(self._bounds):
- return self._items[index]
- else:
- return self._upperitem
-
- def items(self):
- """returns an iterator with each item being
- ((low_bound, high_bound), value)
- these items are returned in order"""
- previous_bound = None
- for (b, v) in izip(self._bounds, self._items):
- if v is not None:
- yield (previous_bound, b), v
- previous_bound = b
- if self._upperitem is not None:
- yield (previous_bound, None), self._upperitem
-
- def values(self):
- """returns an iterator with each item being a stored value
- the items are returned in order"""
- for v in self._items:
- if v is not None:
- yield v
- if self._upperitem is not None:
- yield self._upperitem
-
- def __repr__(self):
- s = []
- for b,v in self.items():
- if v is not None:
- s.append('[%r, %r] => %r'%(
- b[0],
- b[1],
- v
- ))
- return '{'+', '.join(s)+'}'
+from interval_map import IntervalMap
+from pksv import (
+ pksv_gs,
+ pksv_crystal,
+ pksv_crystal_unknowns,
+ pksv_crystal_more_enders
+)
# ---- script_parse_table explanation ----
# This is an IntervalMap that keeps track of previously parsed scripts, texts
@@ -345,6 +241,28 @@ def clean_up_long_info(long_info):
long_info = "\n".join(new_lines)
return long_info
+from pokemon_constants import pokemon_constants
+
+def get_pokemon_constant_by_id(id):
+ if id == 0: return None
+ return pokemon_constants[id]
+
+from item_constants import item_constants
+
+def find_item_label_by_id(id):
+ if id in item_constants.keys():
+ return item_constants[id]
+ else: return None
+
+def generate_item_constants():
+ """make a list of items to put in constants.asm"""
+ output = ""
+ for (id, item) in item_constants.items():
+ val = ("$%.2x"%id).upper()
+ while len(item)<13: item+= " "
+ output += item + " EQU " + val + "\n"
+ return output
+
def command_debug_information(command_byte=None, map_group=None, map_id=None, address=0, info=None, long_info=None, pksv_name=None):
"used to help debug in parse_script_engine_script_at"
info1 = "parsing command byte " + hex(command_byte) + " for map " + \
@@ -1264,32 +1182,10 @@ def transform_wildmons(asm):
returnlines.append(line)
return "\n".join(returnlines)
-from pokemon_constants import pokemon_constants
-
-def get_pokemon_constant_by_id(id):
- if id == 0: return None
- return pokemon_constants[id]
-
def parse_script_asm_at(*args, **kwargs):
# XXX TODO
return None
-from item_constants import item_constants
-
-def find_item_label_by_id(id):
- if id in item_constants.keys():
- return item_constants[id]
- else: return None
-
-def generate_item_constants():
- """make a list of items to put in constants.asm"""
- output = ""
- for (id, item) in item_constants.items():
- val = ("$%.2x"%id).upper()
- while len(item)<13: item+= " "
- output += item + " EQU " + val + "\n"
- return output
-
def find_all_text_pointers_in_script_engine_script(script, bank=None, debug=False):
"""returns a list of text pointers
based on each script-engine script command"""
@@ -1337,9 +1233,6 @@ def translate_command_byte(crystal=None, gold=None):
if gold > 0xA3: raise Exception, "dunno yet if crystal has new insertions after gold:0xA3 (crystal:0xA5)"
else: raise Exception, "translate_command_byte needs either a crystal or gold command"
-from pksv import pksv_gs, pksv_crystal, pksv_crystal_unknowns,\
- pksv_crystal_more_enders
-
class SingleByteParam():
"""or SingleByte(CommandParam)"""
size = 1
@@ -7788,10 +7681,6 @@ class Label:
name = object.make_label()
return name
-from labels import remove_quoted_text, line_has_comment_address, \
- line_has_label, get_label_from_line, \
- get_address_from_line_comment
-
def find_labels_without_addresses():
"""scans the asm source and finds labels that are unmarked"""
without_addresses = []