92 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
			
		
		
	
	
			92 lines
		
	
	
		
			3.9 KiB
		
	
	
	
		
			Python
		
	
	
	
	
	
import re
 | 
						|
 | 
						|
def splitfour(the_string):
 | 
						|
    """ takes a block of sprites output by gb-export.lua from aseprite.
 | 
						|
    divides it into four line-by-line and returns them as separate sprite maps.
 | 
						|
    if you arranged your sprites horizontally, this divides them up."""
 | 
						|
    out = ["", "", "", ""]
 | 
						|
    for l in the_string.split("\n"):
 | 
						|
        sub = list(l.strip()[3:].split(","))
 | 
						|
        length = len(sub)
 | 
						|
        out[0] += "db " + ", ".join(sub[0:length//4]) + "\n"
 | 
						|
        out[1] += "db " + ", ".join(sub[length//4 : length//2]) + "\n"
 | 
						|
        out[2] += "db " + ", ".join(sub[length//2 : 3*length//4]) + "\n"
 | 
						|
        out[3] += "db " + ", ".join(sub[3*length//4 : length]) + "\n"
 | 
						|
    for d in out:
 | 
						|
        print(d)
 | 
						|
    return out
 | 
						|
 | 
						|
def splitn(n, the_string):
 | 
						|
    """ takes a block of sprites output by gb-export.lua from aseprite.
 | 
						|
    divides it into n line-by-line and returns them as separate sprite maps.
 | 
						|
 | 
						|
    if you arranged your sprites horizontally, this divides them up."""
 | 
						|
    out = ["" for _ in range(n)]
 | 
						|
    for l in the_string.split("\n"):
 | 
						|
        sub = list(l.strip()[3:].split(", "))
 | 
						|
        length = len(sub)
 | 
						|
        for i in range(n):
 | 
						|
            out[i] += "db " + ", ".join(sub[(i*length)//n:((i+1)*length)//n]) + "\n"
 | 
						|
    for d in out:
 | 
						|
        print(d)
 | 
						|
    return out
 | 
						|
 | 
						|
def transpose_gb_tiles(the_string):
 | 
						|
    out = [[] for _ in range(max(map(lambda l: len(l.split(", ")), the_string.split("\n"))))]
 | 
						|
 | 
						|
    for l in the_string.split("\n"):
 | 
						|
        sub = list(l.strip()[3:].split(", "))
 | 
						|
        for i, byte in enumerate(sub):
 | 
						|
            out[i].append(byte)
 | 
						|
 | 
						|
    for d in out:
 | 
						|
        print("db " + ", ".join(d))
 | 
						|
 | 
						|
    return out
 | 
						|
 | 
						|
def list_from_length_prefix(the_string):
 | 
						|
    """ converts a string containing a length-prefixed list of entries into a python list"""
 | 
						|
    raw_list = the_string.strip(', \t').split(",")
 | 
						|
    length = int(re.search(r'db ([0-9]+)', raw_list[0]).group(1))
 | 
						|
    rest = [int(s) for s in raw_list[1:]]
 | 
						|
 | 
						|
    step = len(rest) // length
 | 
						|
    return [rest[i : i+step] for i in range(0, len(rest), step)]
 | 
						|
 | 
						|
def length_prefix_from_list(l, size=None):
 | 
						|
    """converts a python list into a length-prefixed asssembly thing"""
 | 
						|
    flat_list = ", ".join(", ".join(f'{x}' for x in s) for s in l)
 | 
						|
    
 | 
						|
    return f'db {len(l)}, {flat_list}'
 | 
						|
 | 
						|
def transpose_paths(paths):
 | 
						|
    """takes a list of paths. returns a list of paths: the first element of each list, etc"""
 | 
						|
    new_paths = [[] for point in paths[0]]
 | 
						|
    
 | 
						|
    for path in paths:
 | 
						|
        for i, point in enumerate(path):
 | 
						|
            new_paths[i].append(point)
 | 
						|
 | 
						|
    return new_paths
 | 
						|
 | 
						|
def transpose_and_length_prefix_path(text):
 | 
						|
    lines = list(text.strip().splitlines())
 | 
						|
    paths = [list_from_length_prefix(s) for s in lines if s.strip()]
 | 
						|
    new_paths = transpose_paths(paths)
 | 
						|
    output = "\n".join(length_prefix_from_list(p) for p in new_paths)
 | 
						|
 | 
						|
    return output
 | 
						|
 | 
						|
chain_lines = """
 | 
						|
db 13, 37, 31, 43, 30, 51, 32, 56, 37, 61, 43, 67, 49, 71, 54, 76, 59, 83, 61, 91, 61, 98, 60, 105, 58, 110, 52, 
 | 
						|
db 13, 37, 31, 44, 30, 52, 34, 57, 39, 61, 44, 67, 50, 71, 55, 76, 59, 83, 61, 91, 61, 98, 60, 104, 57, 110, 52, 
 | 
						|
db 13, 37, 31, 45, 32, 53, 36, 58, 42, 61, 46, 66, 52, 71, 55, 77, 58, 84, 59, 92, 60, 98, 59, 104, 56, 110, 52, 
 | 
						|
db 13, 37, 31, 45, 33, 51, 37, 56, 42, 60, 47, 65, 51, 71, 54, 77, 57, 84, 58, 91, 58, 97, 57, 103, 55, 110, 52, 
 | 
						|
db 13, 37, 31, 43, 34, 48, 38, 53, 43, 59, 48, 66, 51, 71, 53, 77, 55, 84, 57, 91, 59, 98, 58, 104, 56, 110, 52, 
 | 
						|
db 13, 37, 31, 43, 35, 48, 39, 53, 45, 59, 49, 65, 52, 71, 54, 78, 55, 85, 56, 91, 57, 98, 56, 104, 55, 110, 52, 
 | 
						|
db 13, 37, 31, 43, 34, 49, 37, 54, 43, 60, 49, 66, 52, 72, 54, 78, 54, 84, 56, 91, 56, 97, 56, 104, 56, 110, 52, 
 | 
						|
db 13, 37, 31, 43, 32, 51, 35, 56, 41, 61, 46, 66, 51, 72, 53, 78, 55, 84, 57, 91, 58, 98, 57, 105, 57, 110, 52, 
 | 
						|
db 13, 37, 31, 43, 31, 50, 34, 56, 39, 61, 44, 66, 50, 71, 54, 77, 58, 84, 59, 91, 60, 98, 59, 106, 57, 110, 52, 
 | 
						|
"""
 | 
						|
#db 13, 1, 4, 3, 3, 0, 3, 0, 0, 2, 3, 2, 4, 2, 3, 4, 2, 4, 3, 1, 1, 0, 1, 2, 1, 1, 2, 
 | 
						|
print(transpose_and_length_prefix_path(chain_lines)) |