#INCLUDE "TI-85.H"

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ; 
Program data stored in text memory (80DF)
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
PicAddress	=$80DF
MoveCount	=$80E1
ResetMoves	=$80E3	; if 0, movecount=0 and resetmoves=1
ShowScore	=$80E4	; if 1, print score
StringPlace	=$80E5
;=$80EB

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ; 
Program Title
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
.org 0
.db "Tiles  1.4", 0

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ; 
PROGRAM BEGINS HERE
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
	set 3,(IY+5)	; display white on black

	ROM_CALL(CLEARLCD)

	ld hl, $0003
	ld (CURSOR_ROW), hl
	ld hl, (PROGRAM_ADDR)
	push hl
	ld de, TitleStr		; title
	add hl,de
	ROM_CALL(D_ZT_STR)

	ld hl, $3964	; "Hit Enter"
	ld (CURSOR_X), hl
	pop hl
	push hl
	ld de, HitEnter
	add hl, de
	ROM_CALL(D_ZM_STR)

	pop hl
	ld de, PicStruct
	add hl, de
	rst 20h		; copy PicStruct to find buffer
	rst 10h
	ex de,hl
	jr nc, PicFound

	ld hl, $3900	; "Can't find TILEPIC"
	ld (CURSOR_X), hl
	ld hl, (PROGRAM_ADDR)
	ld de, PicNotFound
	add hl, de
	ROM_CALL(D_ZM_STR)
	ld hl, -2

PicFound:
	inc hl
	inc hl
	push hl	; save picture address

EnterLoop:
	call GET_KEY
	cp 9
	jr nz, EnterLoop

	pop hl	; load picture address
	ld a, l
	or h
	ret z		; hl=0 (pic not found)
	ld de, $FC00
	ld bc, $03F0	; size of picture
	ldir

	ld hl, $0303	; clear the bottom right corner piece
SlideNowhere:
	ld d,h
	ld e,l

SlidePanel:
	push ix
	push hl
	or a
	sbc hl,de
	jr z, NoIncCount	; don't increment count if move is to same 
place
	ld hl, (MoveCount)
	inc hl			; increment move count
	ld (MoveCount), hl
NoIncCount:
	pop hl
	push hl
	rlc e
	rlc e
	rlc l
	rlc l
	ld ix, $FC00
	add ix,de
	dec h		; sub 04 = add FC
	dec h
	dec h
	dec h
	ld de, 12
	ld b, 16
RowLoop:
	push bc
	ld b, 4
ColLoop:
	ld a, (hl)
	ld (ix+0), a
	ld (hl), 0
	inc hl
	inc ix
	djnz ColLoop
	add hl,de	; go down one row
	add ix,de
	pop bc
	djnz RowLoop
	ld a, (ShowScore)
	or a		; cp 0 (save a byte)
	jr z, SkipScore
	pop hl
	push hl
	ld a,h
	rlca	; multiply y by 16
	rlca
	rlca
	rlca
	add a, 4	; center the score in the empty block
	ld (CURSOR_Y), a
	ld a,l
	rrca	; multiply x by 32
	rrca
	rrca
	add a, 6
	ld (CURSOR_X), a
	ld hl, (MoveCount)	; print moves
	CALL_(HL_Decimal)
SkipScore:
	pop hl
	pop ix
	jr KeyLoop

Leap2Slide:
	jr SlidePanel
Leap2SlideNo:
	jr SlideNowhere


KeyLoop:
	push hl
	call GET_KEY
	pop hl
	cp 1		; down
	jr z, K_Down
	cp 2		; left
	jr z, K_Left
	cp 3		; right
	jr z, K_Right
	cp 4		; up
	jr z, K_Up
	cp $35		; F1 : reset score or hide score
	jr z, K_F1
	cp $37		; Exit
	ret z
	jr KeyLoop

K_Down:
	sub a	; nothing above
	cp h
	jr z, KeyLoop
	ld d,h
	ld e,l
	dec h
	jr Leap2Slide

K_Left:
	ld a,3	; nothing to the right
	cp l
	jr z, KeyLoop
	ld d,h
	ld e,l
	inc l
	jr Leap2Slide

K_Right:
	sub a	; nothing to the left
	cp l
	jr z, KeyLoop
	ld d,h
	ld e,l
	dec l
	jr Leap2Slide

K_Up:
	ld a,3	; nothing above
	cp h
	jr z, KeyLoop
	ld d,h
	ld e,l
	inc h
	jr Leap2Slide

K_F1:
	ld a, (ResetMoves)
	or a		; cp 0 (save a byte)
	jr nz, NoReset
	ld (MoveCount),a	; reset move count
	ld (MoveCount+1),a
	inc a
	ld (ResetMoves), a
NoReset:			; toggle score hiding
	ld a, (ShowScore)
	xor 1
	ld (ShowScore), a
	jr Leap2SlideNo

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ; 
Strings
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
PicStruct: .db $11, 7, "TILEPIC" 
PicNotFound: .db "Can",$27,"t find TILEPIC",0	; $27 is '
TitleStr: .db " Tiles          v1.4 "
	  .db " Copr. 1995 Dan Eble ",0

HitEnter: .db "Hit Enter",0

;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ; Change 
HL to decimal number and display it as a menu string
;ÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄÄ
HL_Decimal:
	ld de, StringPlace+4
	ld b, 5
ConvLoop:
	call UNPACK_HL
	add a, '0'
	ld (de), a
	dec de
	djnz ConvLoop
	ld hl, StringPlace
	ROM_CALL(D_ZM_STR)
	ret

.end
