summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authoryenatch <yenatch@gmail.com>2015-10-24 15:43:23 -0700
committeryenatch <yenatch@gmail.com>2015-10-24 16:32:15 -0700
commite6ebf8af071980ecf76b09fdd3d65d506390321e (patch)
treea5afce3a496f138a342f350127d6967788bf9332
parent3a6ef54a5e26644699b98d9b73998df5673f4e72 (diff)
Fix condense_tiles_to_map.
-rw-r--r--pokemontools/gfx.py58
1 files changed, 47 insertions, 11 deletions
diff --git a/pokemontools/gfx.py b/pokemontools/gfx.py
index 049192f..6922b31 100644
--- a/pokemontools/gfx.py
+++ b/pokemontools/gfx.py
@@ -128,8 +128,25 @@ def deinterleave_tiles(image, width):
return connect(deinterleave(get_tiles(image), width))
-def condense_tiles_to_map(image, pic=0):
+def condense_image_to_map(image, pic=0):
+ """
+ Reduce an image of adjacent frames to an image containing a base frame and any unrepeated tiles.
+ Returns the new image and the corresponding tilemap used to reconstruct the input image.
+
+ If <pic> is 0, ignore the concept of frames. This behavior might be better off as another function.
+ """
tiles = get_tiles(image)
+ new_tiles, tilemap = condense_tiles_to_map(tiles, pic)
+ new_image = connect(new_tiles)
+ return new_image, tilemap
+
+def condense_tiles_to_map(tiles, pic=0):
+ """
+ Reduce a sequence of tiles representing adjacent frames to a base frame and any unrepeated tiles.
+ Returns the new tiles and the corresponding tilemap used to reconstruct the input tile sequence.
+
+ If <pic> is 0, ignore the concept of frames. This behavior might be better off as another function.
+ """
# Leave the first frame intact for pics.
new_tiles = tiles[:pic]
@@ -137,17 +154,36 @@ def condense_tiles_to_map(image, pic=0):
for i, tile in enumerate(tiles[pic:]):
if tile not in new_tiles:
- new_tiles += [tile]
-
- # Match the first frame where possible.
- this_i = i % pic if pic else i
- if tile == new_tiles[this_i]:
- tilemap += [this_i]
+ new_tiles.append(tile)
+
+ if pic:
+ # Match the first frame exactly where possible.
+ # This reduces the space needed to replace tiles in pic animations.
+ # For example, if a tile is repeated twice in the first frame,
+ # but at the same relative index as the second tile, use the second index.
+ # When creating a bitmask later, the second index would not require a replacement, but the first index would have.
+ pic_i = i % pic
+ if tile == new_tiles[pic_i]:
+ tilemap.append(pic_i)
+ else:
+ tilemap.append(new_tiles.index(tile))
else:
- tilemap += [new_tiles.index(tile)]
-
- new_image = connect(new_tiles)
- return new_image, tilemap
+ tilemap.append(new_tiles.index(tile))
+ return new_tiles, tilemap
+
+def test_condense_tiles_to_map():
+ test = condense_tiles_to_map(list('abcadbae'))
+ if test != (list('abcde'), [0, 1, 2, 0, 3, 1, 0, 4]):
+ raise Exception(test)
+ test = condense_tiles_to_map(list('abcadbae'), 2)
+ if test != (list('abcde'), [0, 1, 2, 0, 3, 1, 0, 4]):
+ raise Exception(test)
+ test = condense_tiles_to_map(list('abcadbae'), 4)
+ if test != (list('abcade'), [0, 1, 2, 3, 4, 1, 0, 5]):
+ raise Exception(test)
+ test = condense_tiles_to_map(list('abcadbea'), 4)
+ if test != (list('abcade'), [0, 1, 2, 3, 4, 1, 5, 3]):
+ raise Exception(test)
def to_file(filename, data):