; The old demon can learn new tricks! You don't have a fancy editor available
; like on TRS-80, but you can write your dance routine in a BASIC line i
; (with number >= 10) using Galaksija's command line and the demon will 
; perform it when you run the program.

include "galaksija.inc"

	org g_user_mem

	jp main

beat:		equ 5	

cursor_start: 	equ g_video_mem+3*32+8-32

title:	
	db "  D A N C I N G      D E M O N"

dance:
;	dm "LLLDEDECCCCBKKBKKDEDENOLMLMDENOLMLMDEFGFGCCCCBKKBKKDEDERSSRRSSRSRRSSRRSDECCMLMLM\r"
	dm "JJFJGJFJGJDSESFJGJQLQLFJGJRRRRRRJJKYJRVSVSVSVBXHHYXQMQMQMQMIKJIKJFJGJFJGJDJEJFJK\r"

applause:
	dm "::\r"

cursor:
	dw cursor_start

; CLEAR SCREEN 
; ============

clrscr:
	ld hl,g_video_mem
	ld b,0
	ld a,0xc0
clrscr_loop1:
	ld (hl),a
	inc hl
	djnz clrscr_loop1
clrscr_loop2:
	ld (hl),a
	inc hl
	djnz clrscr_loop2
	ret

; FLOOR
; =====

floor:
	ld hl,g_video_mem+14*32

	ld (hl),0xb8
	inc hl

	ld b,30
floor_loop:
	ld (hl),0xbf
	inc hl
	djnz floor_loop

	ld (hl),0xb4
	inc hl

	ld de,title
	ld bc,30

	ex de,hl
	ldir

	ret

; BLIT
; ====

blit:
				; HL = pointer to the screen
				; DE = pointer to the frame

	ld a,0c0h

	ld b,16
blit_topclear:
	ld (hl),a
	inc hl
	djnz blit_topclear	; B = 0

	ld a,10
blit_loop:
	ld c,16			; bytes in line (must be 16!)
	add hl,bc
	ex de,hl
	ldir
	ex de,hl

	dec a
	jr nz,blit_loop

	ld c,16	
	add hl,bc

	ld a,0c0h

	ld b,c
blit_botclear:
	ld (hl),a
	inc hl
	djnz blit_botclear	; B = 0

	ret

; CLICK
; =====
click:
	push hl
;	ld hl,0207fh
	ld hl,02038h

	ld a,0ffh
	ld (hl),a

	ld b,20
click_loop1:
	djnz click_loop1

	ld a,0bbh
	ld (hl),a

	ld b,20
click_loop2:
	djnz click_loop2

	ld a,0bfh
	ld (hl),a
	pop hl
	ret

; EXEC
; ====

exec: 
	pop hl			; top of stack = pointer to the
				; dance script

exec_loop:
	ld a,(hl)
	inc hl

	or a
	jr z,play_loop		; break loop if low byte of ram address is 0
	ld e,a

	ld a,(hl)
	inc hl

	ld c,a
	and 07fh
	ld d,a

	ld b,(hl)
	inc hl

exec_delay:
	halt
	djnz exec_delay		; B = 0

	bit 7,c			; if top bit of ram address was set, click
	call nz,click

	ld a,(hl)
	inc hl

	or a
	ld c,a

	jp p,exec_plus		; argh...
	ld b,0ffh
exec_plus:

	push hl
	ld hl,(cursor)
	add hl,bc
	ld (cursor),hl

	call blit

	pop hl
	jr exec_loop

; PLAY
; ====
	
play:
	push hl			; HL = pointer to the dance routine string
play_loop:
	pop hl
	ld a,(hl)
	inc hl
	push hl
	cp '\r'			; end character
	jr z,play_end

	jr script_A

play_end:
	pop hl
	ret

include "demon-scripts.inc"	

; MAIN
; ====
	
main:
	call clrscr
	call floor

	ld hl,cursor_start
	ld (cursor),hl

	ld hl,10
	call g_find_line

	jr nc, main_basic

	ld de,dance-2
main_basic:
	inc de
	inc de

	ex de,hl

	call play

	ld hl,applause
	call play

	jp 00066h

include "demon-frames.inc"
