summaryrefslogtreecommitdiff
path: root/pokemontools/map_editor.py
diff options
context:
space:
mode:
authorBryan Bishop <kanzure@gmail.com>2013-09-26 10:46:04 -0500
committerBryan Bishop <kanzure@gmail.com>2013-09-26 10:46:04 -0500
commitc02c65fe74089176889e2e9b78b1dcbecdf869fc (patch)
tree61e1232a00befbb0c8ea6364f0deeea6af65df02 /pokemontools/map_editor.py
parentcff5ce9bc81c78d8f841570480f1716fb91c1fac (diff)
parent3496b81eb3b5af6fa7d694cbac1610b5ff2c85f2 (diff)
Merge branch 'yenatch/map-editor' into master
Conflicts: pokemontools/crystal.py Fixes #40.
Diffstat (limited to 'pokemontools/map_editor.py')
-rw-r--r--pokemontools/map_editor.py78
1 files changed, 55 insertions, 23 deletions
diff --git a/pokemontools/map_editor.py b/pokemontools/map_editor.py
index d4b5047..4c0bec5 100644
--- a/pokemontools/map_editor.py
+++ b/pokemontools/map_editor.py
@@ -6,12 +6,12 @@ from ttk import Frame, Style
import PIL
from PIL import Image, ImageTk
-import configuration
-conf = configuration.Config()
+import config
+conf = config.Config()
-version = 'crystal'
-#version = 'red'
+#version = 'crystal'
+version = 'red'
if version == 'crystal':
map_dir = os.path.join(conf.path, 'maps/')
@@ -317,12 +317,15 @@ class Map:
# Draw one block (4x4 tiles)
block = self.blockdata[block_y * self.width + block_x]
for j, tile in enumerate(self.tileset.blocks[block]):
- # Tile gfx are split in half to make vram mapping easier
- if tile >= 0x80:
- tile -= 0x20
- tile_x = block_x * 32 + (j % 4) * 8
- tile_y = block_y * 32 + (j / 4) * 8
- self.canvas.create_image(index + tile_x, indey + tile_y, image=self.tileset.tiles[tile])
+ try:
+ # Tile gfx are split in half to make vram mapping easier
+ if tile >= 0x80:
+ tile -= 0x20
+ tile_x = block_x * 32 + (j % 4) * 8
+ tile_y = block_y * 32 + (j / 4) * 8
+ self.canvas.create_image(index + tile_x, indey + tile_y, image=self.tileset.tiles[tile])
+ except:
+ pass
class Tileset:
@@ -343,11 +346,30 @@ class Tileset:
self.get_blocks()
self.get_tiles()
+ def get_tileset_gfx_filename(self):
+ filename = None
+
+ if version == 'red':
+ tileset_defs = open(os.path.join(conf.path, 'main.asm'), 'r').read()
+ incbin = asm_at_label(tileset_defs, 'Tset%.2X_GFX' % self.id)
+ print incbin
+ filename = read_header_macros(incbin, ['filename'], ['INCBIN'])[0][0].replace('"','').replace('.2bpp','.png')
+ filename = os.path.join(conf.path, filename)
+ print filename
+
+ if not filename:
+ filename = os.path.join(
+ gfx_dir,
+ to_gfx_name(self.id) + '.png'
+ )
+
+ return filename
+
def get_tiles(self):
- filename = os.path.join(
- gfx_dir,
- to_gfx_name(self.id) + '.png'
- )
+ filename = self.get_tileset_gfx_filename()
+ if not os.path.exists(filename):
+ import gfx
+ gfx.to_png(filename.replace('.png','.2bpp'), filename)
self.img = Image.open(filename)
self.img.width, self.img.height = self.img.size
self.tiles = []
@@ -452,7 +474,7 @@ def map_header(name):
headers = open(os.path.join(header_dir, 'map_headers.asm'), 'r').read()
label = name + '_MapHeader'
header = asm_at_label(headers, label)
- macros = [ 'db', 'dw', 'db' ]
+ macros = [ 'db', 'db', 'db', 'dw', 'db', 'db', 'db', 'db' ]
attributes = [
'bank',
'tileset_id',
@@ -478,7 +500,7 @@ def map_header(name):
label = headers[i:i+len(lower_label)]
header = asm_at_label(headers, label)
- macros = [ 'db', 'db', 'dw', 'db' ]
+ macros = [ 'db', 'db', 'db', 'dw', 'dw', 'dw', 'db' ]
attributes = [
'tileset_id',
'height',
@@ -509,7 +531,7 @@ def second_map_header(name):
headers = open(os.path.join(header_dir, 'second_map_headers.asm'), 'r').read()
label = name + '_SecondMapHeader'
header = asm_at_label(headers, label)
- macros = [ 'db', 'db', 'dbw', 'dbw', 'dw', 'db' ]
+ macros = [ 'db', 'db', 'db', 'db', 'dw', 'db', 'dw', 'dw', 'db' ]
attributes = [
'border_block',
'height',
@@ -531,19 +553,21 @@ def second_map_header(name):
def connections(which_connections, header, l=0):
directions = { 'north': {}, 'south': {}, 'west': {}, 'east': {} }
- macros = [ 'db', 'dw', 'dw', 'db', 'db', 'dw' ]
if version == 'crystal':
+ macros = [ 'db', 'db' ]
attributes = [
'map_group',
'map_no',
]
elif version == 'red':
+ macros = [ 'db' ]
attributes = [
'map_id',
]
+ macros += [ 'dw', 'dw', 'db', 'db', 'db', 'db', 'dw' ]
attributes += [
'strip_pointer',
'strip_destination',
@@ -570,8 +594,9 @@ def read_header_macros(header, attributes, macros):
l = 0
for l, (asm, comment) in enumerate(header):
if asm.strip() != '':
- values += macro_values(asm, macros[i])
- i += 1
+ mvalues = macro_values(asm, macros[i])
+ values += mvalues
+ i += len(mvalues)
if len(values) >= len(attributes):
l += 1
break
@@ -587,7 +612,10 @@ def script_header(asm, name):
def macro_values(line, macro):
values = line[line.find(macro) + len(macro):].split(',')
- return [v.replace('$','0x').strip() for v in values]
+ values = [v.replace('$','0x').strip() for v in values]
+ if values[0] == 'w': # dbw
+ values = values[1:]
+ return values
def db_value(line):
macro = 'db'
@@ -602,8 +630,12 @@ from preprocessor import separate_comment
def asm_at_label(asm, label):
label_def = label + ':'
- start = asm.find(label_def) + len(label_def)
- lines = asm[start:].split('\n')
+ lines = asm.split('\n')
+ for line in lines:
+ if line.startswith(label_def):
+ lines = lines[lines.index(line):]
+ lines[0] = lines[0][len(label_def):]
+ break
# go until the next label
content = []
for line in lines: