auto_return	EQU REND_AutoRet

temp_tile	EQU	STARS_Sprite

.AUTO_Generate_Reversed
	push IY
	push IX
	push DE
	pop IY
	ld IX,temp_tile		;store temporary 
;IY = old , IX = new

;line 0 = 7
	ld A,(IY+10)
	ld (IX+1),A
	ld A,(IY+11)
	ld (IX+0),A
;line 1 = 6
	ld A,(IY+8)
	ld (IX+3),A
	ld A,(IY+9)
	ld (IX+2),A
;line 2 = 5
	ld A,(IY+12)
	ld (IX+7),A
	ld A,(IY+13)
	ld (IX+6),A
;line 3 = 4
	ld A,(IY+14)
	ld (IX+5),A
	ld A,(IY+15)
	ld (IX+4),A
;line 4 = 3
	ld A,(IY+4)
	ld (IX+15),A
	ld A,(IY+5)
	ld (IX+14),A
;line 5 = 2
	ld A,(IY+6)
	ld (IX+13),A
	ld A,(IY+7)
	ld (IX+12),A
;line 6 = 1
	ld A,(IY+2)
	ld (IX+9),A
	ld A,(IY+3)
	ld (IX+8),A
;line 7 = 0
	ld A,(IY+0)
	ld (IX+11),A
	ld A,(IY+1)
	ld (IX+10),A


	ld DE,temp_tile
	pop IX
	pop IY


;*** TILES AUTOGENERATION CODE***
; auto generation rules
; DE = is supposed to be screen address
; AF = free / trash - H = 8 -  L is supposed to be NULL
; other registers not altered

;generate a sprite
; DE = sprite address - IX = address to write
;return next address in IX
.AUTO_Generate_Sprite
	ld HL,auto_structure

.AUTO_Generate_Sprite_loop

;what the chunk ?
	ld A,(HL)
	cp #FF	;type #FF ->end
	ret z
	
	or A
	jr nz,AUTO_Generate_Sprite_Dynamic
	
	
;static chunk - no parameters	
.AUTO_Generate_Sprite_Static
	push DE
	inc HL
	ld E,(HL)
	inc HL
	ld D,(HL)
	inc HL
	ld B,(HL)
	inc HL
	ex DE,HL
	call AUTO_WriteChunk
	ex DE,HL
	pop DE
	jr AUTO_Generate_Sprite_loop
.AUTO_Generate_Sprite_Dynamic
	ld A,(DE)
	inc DE
	or A
	jr nz,AUTO_Generate_Sprite_DynamicMitData
;data = 0 - write second chunk static
	inc HL	;type
	inc HL	;address 1
	inc HL
	jr AUTO_Generate_Sprite_Static
;data > 0 , write chunk w/ data	
.AUTO_Generate_Sprite_DynamicMitData
	push DE		;save data position
	
	push AF		;save data
	ld A,(HL)
	push AF		;save offset
	
	inc HL
	ld E,(HL)
	inc HL
	ld D,(HL)
	inc HL
	ld B,(HL)
	inc HL
	
	;save current structure address
	ld (AUTO_Generate_Sprite_DynamicMitData_saveStructPos),HL
	
	ex DE,HL
	
	;save code adress address
	ld (AUTO_Generate_Sprite_DynamicMitData_savecodeAddress),HL	
	
		
	pop AF		;get offset
	
	ld E,A
	ld D,0		;in DE
	add HL,DE	; point where to put parameter
	
	pop AF		;get data
	ld (HL),A	;write it

	;restore address
AUTO_Generate_Sprite_DynamicMitData_savecodeAddress	EQU $+1
	ld HL,0
	
	call AUTO_WriteChunk ;write chunk
	
AUTO_Generate_Sprite_DynamicMitData_saveStructPos EQU $+1

	ld HL,0
	pop DE
	
	inc HL
	inc HL
	inc HL
	
	jp AUTO_Generate_Sprite_loop


;write a chunk , HL = chunk address B= size , IX = where to write
.AUTO_WriteChunk
	ld A,(HL)
	ld (IX),A
	inc HL
	inc IX
	djnz AUTO_WriteChunk
	ret


		
;*** auto sprite structure ***
; byte type (0) - word address - byte size
; OR
; byte type (>0) - word address - byte size - word address 2 - byte size 2
; OR
; byte type (#FF)
;
; type = param offset
; if param = null address 2 is used and param is not written

.auto_structure
	db 0
	dw auto_header:db auto_header_size AND 255
;line 0-1
	db 1
	dw auto_first_byte_pair_notnull:db auto_first_byte_pair_notnull_size AND 255
	dw auto_first_byte_pair_null:db auto_first_byte_pair_null_size AND 255
	db 1
	dw auto_second_byte_pair_notnull:db auto_second_byte_pair_notnull_size AND 255
	dw auto_second_byte_pair_null:db auto_second_byte_pair_null_size AND 255
	db 0
	dw auto_nextline_1_code:db auto_nextline_1_code_size AND 255
	db 1
	dw auto_first_byte_unpair_notnull:db auto_first_byte_unpair_notnull_size AND 255
	dw auto_first_byte_unpair_null:db auto_first_byte_unpair_null_size AND 255
	db 1
	dw auto_second_byte_unpair_notnull:db auto_second_byte_unpair_notnull_size AND 255
	dw auto_second_byte_unpair_null:db auto_second_byte_unpair_null_size AND 255
	db 0
	dw auto_nextline_2_code:db auto_nextline_2_code_size AND 255
;line 2-3
	db 1
	dw auto_first_byte_pair_notnull:db auto_first_byte_pair_notnull_size AND 255
	dw auto_first_byte_pair_null:db auto_first_byte_pair_null_size AND 255
	db 1
	dw auto_second_byte_pair_notnull:db auto_second_byte_pair_notnull_size AND 255
	dw auto_second_byte_pair_null:db auto_second_byte_pair_null_size AND 255
	db 0
	dw auto_nextline_3_code:db auto_nextline_3_code_size AND 255
	db 1
	dw auto_first_byte_unpair_notnull:db auto_first_byte_unpair_notnull_size AND 255
	dw auto_first_byte_unpair_null:db auto_first_byte_unpair_null_size AND 255
	db 1
	dw auto_second_byte_unpair_notnull:db auto_second_byte_unpair_notnull_size AND 255
	dw auto_second_byte_unpair_null:db auto_second_byte_unpair_null_size AND 255
	db 0
	dw auto_nextline_4_code:db auto_nextline_4_code_size AND 255
;line 4-5
	db 1
	dw auto_first_byte_pair_notnull:db auto_first_byte_pair_notnull_size AND 255
	dw auto_first_byte_pair_null:db auto_first_byte_pair_null_size AND 255
	db 1
	dw auto_second_byte_pair_notnull:db auto_second_byte_pair_notnull_size AND 255
	dw auto_second_byte_pair_null:db auto_second_byte_pair_null_size AND 255
	db 0
	dw auto_nextline_5_code:db auto_nextline_5_code_size AND 255
	db 1
	dw auto_first_byte_unpair_notnull:db auto_first_byte_unpair_notnull_size AND 255
	dw auto_first_byte_unpair_null:db auto_first_byte_unpair_null_size AND 255
	db 1
	dw auto_second_byte_unpair_notnull:db auto_second_byte_unpair_notnull_size AND 255
	dw auto_second_byte_unpair_null:db auto_second_byte_unpair_null_size AND 255
	db 0
	dw auto_nextline_6_code:db auto_nextline_6_code_size AND 255
;line 6-7
	db 1
	dw auto_first_byte_pair_notnull:db auto_first_byte_pair_notnull_size AND 255
	dw auto_first_byte_pair_null:db auto_first_byte_pair_null_size AND 255
	db 1
	dw auto_second_byte_pair_notnull:db auto_second_byte_pair_notnull_size AND 255
	dw auto_second_byte_pair_null:db auto_second_byte_pair_null_size AND 255
	db 0
	dw auto_nextline_7_code:db auto_nextline_7_code_size AND 255
	db 1
	dw auto_first_byte_unpair_notnull:db auto_first_byte_unpair_notnull_size AND 255
	dw auto_first_byte_unpair_null:db auto_first_byte_unpair_null_size AND 255
	db 1
	dw auto_second_byte_unpair_notnull:db auto_second_byte_unpair_notnull_size AND 255
	dw auto_second_byte_unpair_null:db auto_second_byte_unpair_null_size AND 255
;the end
	db 0
	dw auto_end:db auto_end_size AND 255
	db #FF
		
;*** header ***
.auto_header
	ex DE,HL
	xor A
auto_header_size EQU $-auto_header

;*** end ***
.auto_end
	jp auto_return
auto_end_size EQU $-auto_end
	

;*** next line ***
.auto_nextline_1_code
	set 3,H			;2/2
auto_nextline_1_code_size EQU $-auto_nextline_1_code

.auto_nextline_2_code
	set 4,H			;2/2
auto_nextline_2_code_size EQU $-auto_nextline_2_code

.auto_nextline_3_code
	res 3,H			;2/2
auto_nextline_3_code_size EQU $-auto_nextline_3_code

.auto_nextline_4_code
	set 5,H			;2/2
auto_nextline_4_code_size EQU $-auto_nextline_4_code

.auto_nextline_5_code
	set 3,H			;2/2
auto_nextline_5_code_size EQU $-auto_nextline_5_code

.auto_nextline_6_code
	res 4,H			;2/2
auto_nextline_6_code_size EQU $-auto_nextline_6_code

.auto_nextline_7_code
	res 3,H			;2/2
auto_nextline_7_code_size EQU $-auto_nextline_7_code


;*** 1st & 2nd byte pair not null ***
.auto_first_byte_pair_notnull
.auto_second_byte_pair_notnull
	ld (HL),0		;2
auto_second_byte_pair_notnull_size EQU $-auto_second_byte_pair_notnull
	inc L			;1
auto_first_byte_pair_notnull_size EQU $-auto_first_byte_pair_notnull

;*** 1st & 2nd byte pair null***
.auto_first_byte_pair_null
.auto_second_byte_pair_null
	ld (HL),A		;1
auto_second_byte_pair_null_size EQU $-auto_second_byte_pair_null	
	inc L			;1
auto_first_byte_pair_null_size EQU $-auto_first_byte_pair_null

;*** 1st & 2nd byte unpair not null ***
.auto_first_byte_unpair_notnull
.auto_second_byte_unpair_notnull
	ld (HL),0		;2
auto_second_byte_unpair_notnull_size EQU $-auto_second_byte_unpair_notnull
	dec L			;1
auto_first_byte_unpair_notnull_size EQU $-auto_first_byte_unpair_notnull	

;*** 1st & 2nd byte unpair null ***
.auto_first_byte_unpair_null
.auto_second_byte_unpair_null
	ld (HL),A		;2
auto_second_byte_unpair_null_size EQU $-auto_second_byte_unpair_null
	dec L			;1
auto_first_byte_unpair_null_size EQU $-auto_first_byte_unpair_null
	
	