<s> """<STR_LIT>""" <EOL> __all__ = [ '<STR_LIT>' ] <EOL> from . . import util <EOL> BLACK = <NUM_LIT:0> <EOL> DARK_BLUE = <NUM_LIT:1> <EOL> DARK_PURPLE = <NUM_LIT:2> <EOL> DARK_GREEN = <NUM_LIT:3> <EOL> BROWN = <NUM_LIT:4> <EOL> DARK_GRAY = <NUM_LIT:5> <EOL> LIGHT_GRAY = <NUM_LIT:6> <EOL> WHITE = <NUM_LIT:7> <EOL> RED = <NUM_LIT:8> <EOL> ORANGE = <NUM_LIT:9> <EOL> YELLOW = <NUM_LIT:10> <EOL> GREEN = <NUM_LIT:11> <EOL> BLUE = <NUM_LIT:12> <EOL> INDIGO = <NUM_LIT> <EOL> PINK = <NUM_LIT> <EOL> PEACH = <NUM_LIT:15> <EOL> TRANSPARENT = <NUM_LIT:16> <EOL> class Gfx ( util . BaseSection ) : <EOL> """<STR_LIT>""" <EOL> HEX_LINE_LENGTH_BYTES = <NUM_LIT:64> <EOL> @ classmethod <EOL> def empty ( cls , version = <NUM_LIT:4> ) : <EOL> """<STR_LIT>""" <EOL> return cls ( data = bytearray ( b'<STR_LIT:\x00>' * <NUM_LIT> * <NUM_LIT:64> ) , version = version ) <EOL> @ classmethod <EOL> def from_lines ( cls , lines , version ) : <EOL> """<STR_LIT>""" <EOL> datastrs = [ ] <EOL> for l in lines : <EOL> if len ( l ) != <NUM_LIT> : <EOL> continue <EOL> larray = list ( l . rstrip ( ) ) <EOL> for i in range ( <NUM_LIT:0> , <NUM_LIT> , <NUM_LIT:2> ) : <EOL> ( larray [ i ] , larray [ i + <NUM_LIT:1> ] ) = ( larray [ i + <NUM_LIT:1> ] , larray [ i ] ) <EOL> datastrs . append ( bytearray . fromhex ( '<STR_LIT>' . join ( larray ) ) ) <EOL> data = b'<STR_LIT>' . join ( datastrs ) <EOL> return cls ( data = data , version = version ) <EOL> def to_lines ( self ) : <EOL> """<STR_LIT>""" <EOL> for start_i in range ( <NUM_LIT:0> , len ( self . _data ) , self . HEX_LINE_LENGTH_BYTES ) : <EOL> end_i = start_i + self . HEX_LINE_LENGTH_BYTES <EOL> newdata = [ ] <EOL> for b in self . _data [ start_i : end_i ] : <EOL> newdata . append ( ( b & <NUM_LIT> ) << <NUM_LIT:4> | ( b & <NUM_LIT> ) >> <NUM_LIT:4> ) <EOL> yield bytes ( newdata ) . hex ( ) + '<STR_LIT:\n>' <EOL> def get_sprite ( self , id , tile_width = <NUM_LIT:1> , tile_height = <NUM_LIT:1> ) : <EOL> """<STR_LIT>""" <EOL> assert <NUM_LIT:0> <= id <= <NUM_LIT:255> <EOL> assert <NUM_LIT:1> <= tile_width <EOL> assert <NUM_LIT:1> <= tile_height <EOL> first_tile_row = id // <NUM_LIT:16> <EOL> first_tile_col = id % <NUM_LIT:16> <EOL> result = [ ] <EOL> for ty in range ( first_tile_row , first_tile_row + tile_height ) : <EOL> for y_offset in range ( <NUM_LIT:8> ) : <EOL> row = bytearray ( ) <EOL> for tx in range ( first_tile_col , first_tile_col + tile_width ) : <EOL> if tx > <NUM_LIT:15> or ty > <NUM_LIT:15> : <EOL> row . extend ( [ <NUM_LIT:0> ] * <NUM_LIT:8> ) <EOL> else : <EOL> for x_offset in range ( <NUM_LIT:8> ) : <EOL> data_loc = ( ty * <NUM_LIT:64> * <NUM_LIT:8> + <EOL> y_offset * <NUM_LIT:64> + <EOL> tx * <NUM_LIT:4> + <EOL> x_offset // <NUM_LIT:2> ) <EOL> b = self . _data [ data_loc ] <EOL> if x_offset % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> row . append ( b & <NUM_LIT> ) <EOL> else : <EOL> row . append ( ( b & <NUM_LIT> ) >> <NUM_LIT:4> ) <EOL> result . append ( row ) <EOL> return result <EOL> def set_sprite ( self , id , sprite , tile_x_offset = <NUM_LIT:0> , tile_y_offset = <NUM_LIT:0> ) : <EOL> """<STR_LIT>""" <EOL> first_tile_row = id // <NUM_LIT:16> <EOL> first_tile_col = id % <NUM_LIT:16> <EOL> first_x_coord = first_tile_col * <NUM_LIT:8> + tile_x_offset <EOL> first_y_coord = first_tile_row * <NUM_LIT:8> + tile_y_offset <EOL> for y , row in enumerate ( sprite ) : <EOL> for x , val in enumerate ( row ) : <EOL> if ( ( val == TRANSPARENT ) or <EOL> ( ( first_y_coord + y ) > <NUM_LIT> ) or <EOL> ( ( first_x_coord + x ) > <NUM_LIT> ) ) : <EOL> continue <EOL> data_loc = ( first_y_coord + y ) * <NUM_LIT:64> + ( first_x_coord + x ) // <NUM_LIT:2> <EOL> b = self . _data [ data_loc ] <EOL> if ( first_x_coord + x ) % <NUM_LIT:2> == <NUM_LIT:0> : <EOL> b = ( b & <NUM_LIT> ) + val <EOL> else : <EOL> b = ( b & <NUM_LIT> ) + ( val << <NUM_LIT:4> ) <EOL> self . _data [ data_loc ] = b </s>
