        .nolist

        #include "ion.inc"
        #include "keys.inc"
        .list
sprite = saferam1		;current man sprite (left/right)
timer = sprite+2		;timer to dec time
time = timer+1			;time left
y = time+1			;ycoord of man
x = y+1				;xcoord of man
spot = x+1			;curent spot on map
ld = spot+2			;is the level done?
die = ld+1			;are u dead?
level = die+1			;what level are u on
levels = level+1		;how many levels are there
offset = levels+1		;for IONDetect


#ifdef TI83P
        .org    progstart-2
        .db     $BB,$6D
#else
        .org    progstart
#endif
        ret				

        jr      nc,begin
        .db     "Panic!!!",0
begin:
;////////SHOWS TITLE PIC\\\\\\\\
	bcall(_cleargbuf)	;clears buffer
	ld b,24			;height
	ld c,12			;width/8
	ld ix,titlepic		;sprite
	ld a,0			;xcoord
	ld l,0			;ycoord
	call ionLargeSprite	;puts title sprite
	call ionFastCopy	;copys it to buffer
	ld	a,23
	ld	(PENCOL),a	; x coordinate			
	ld	a,56
	ld	(PENROW),a	; y coordinate
	ld	hl,txtauthor	; text
	bcall(_vputs) 		;puts text
begwait:
	ld hl,0
	ld (offset),hl		;loads 0 into offset for IonDetect
	call waitkey		;waits for keypress
	cp gClear		;ret if clear
	ret z
	cp gDel			;or del
	ret z
	cp gMode		;shut off if mode
	call z,calcoff
	cp g2nd			;if not 2nd, wait some more
	jr nz,begwait

	ld a,0			;u are on level 0
	ld (level),a
;////////DETECTS AND LOADS A LEVELSET\\\\\\\\
leveload:
	ld	hl,(vat)
	ld	ix,txtdetect
	call	ionDetect
	jp nz,nolevels
	inc hl
	ld a,(hl)		;number of levels
	ld (levels),a
	inc hl
loadthelev:
	ld de,(offset)		;adds the offset
	add hl,de
	ld (cur1),hl		;and puts data in 'cur1'
	ld de,96		;adds with 96 for 'cur2'
	add hl,de
	ld (cur2),hl
;////////INITIALIZE THE GAME\\\\\\\\
initialize:
	ld hl,rmrpanic		;initializes vars...
	ld (sprite),hl
	ld a,20
	ld (timer),a
	ld a,5
	ld (time),a
	ld a,0
	ld (spot),a
	ld (ld),a
	ld (die),a
	ld hl,0
	ld (y),hl
	

;////////MAIN LOOP\\\\\\\\
mainloop:
	ld a,(die)		;if dead, jump to gameover
	cp 1
	jp z,gameover
	ld a,(ld)		;if leveldone, jump to leveldpmleted
	cp 1
	jp z,levelcompleted
	bcall(_cleargbuf)	;clear buffer
	ld de,(cur1)		;draws cur1 and mrPanic
	call DrawMap
	call DrawMrPanic
	call ionFastCopy	;and puts them on the screen
	ld de,(cur2)		;draws cur2 and mrPanic
	call DrawMap
	call DrawMrPanic
	call ionFastCopy	;and puts on screen
	call WriteTime		;displays time
	ld a,(timer)		;decreases timer
	dec a
	ld (timer),a
	call z,res		;if it hit 0, reset it

	ld b,KeyRow_5		;takes input from row5
	call directinput
	cp kClear		;return to ION if clear pressed
	ret z
	ld b,KeyRow_Top		;gets input from 2nd/mode/del
	call directinput
	cp kDel
	ret z			;ret if Del
	cp kMode
	call z,calcoff		;shutoff if mode
	ld b,KeyRow_Pad		;gets arrow input
	call directinput
	cp kLeft		;left, call left, etc.
	call z,Left
	cp kRight
	call z,Right
	cp kUp
	call z,Up
	cp kDown
	call z,Down
	


	jr mainloop		;repeats loop
res:
	ld a,20			;resets timer 
	ld (timer),a
	ld a,(time)		;and dec time
	dec a
	jr z,timeout		;if time was '0'
	ld (time),a		;go to makedie
	ret
timeout:
	call dectime		;puts time at 0
	call writetime		;writes the time
	call makedie		;kills dude
	ret

drawmrpanic:
	ld hl,(y)		;loads y into l and x into h
	ld a,h
	ld ix,(sprite)		;loads sprite into ix
	ld b,8			;8 tall
	call ionPutSprite	;puts it
	ret

writetime:
	ld a,0			;displays time in 'hl' at bottom
	ld (curcol),a
	ld a,7
	ld (currow),a
	ld hl,txttime
	bcall(_puts)
	ld h,0
	ld a,(time)
	ld l,a
	bcall(_disphl)
	ret
	

Left:
	ld a,(spot)
	dec a
	call CheckSpot
	cp 1
	ret z
	call SpotActions
	ld a,(x)
	sub 8
	ret c
	ld (x),a
	ld a,(spot)
	dec a
	ld (spot),a
	ld hl,lmrpanic
	ld (sprite),hl
	ret

Right:
	ld a,(spot)
	inc a
	call CheckSpot
	cp 1
	ret z
	call SpotActions
	ld a,(x)
	add a,8
	cp 96-7
	ret nc
	ld (x),a
	ld a,(spot)
	inc a
	ld (spot),a
	ld hl,rmrpanic
	ld (sprite),hl
	ret

Up:
	ld a,(spot)
	sub 12		;12 tiles in a row
	call CheckSpot
	cp 1
	ret z
	call SpotActions
	ld a,(y)
	sub 8
	ret c
	ld (y),a
	ld a,(spot)
	sub 12
	ld (spot),a
	ret

Down:
	ld a,(spot)
	add a,12
	call CheckSpot
	cp 1
	ret z
	call SpotActions
	ld a,(y)
	add a,8
	cp 64-8
	ret nc
	ld (y),a
	ld a,(spot)
	add a,12
	ld (spot),a
	ret
SpotActions:
	ret c
	cp 3		;bomb tile
	jr z,leveldone
	cp 5		;clock up tile
	jr z,inctime
	cp 6		;clock down tile
	jr z,dectime
	cp 7		;dead tile
	jr z,makedie
	ret

leveldone:
	ld a,1
	ld (ld),a
	ret
makedie:
	ld a,1
	ld (die),a
	ret
inctime:
	ld a,(time)
	inc a
	ld (time),a
	ret

dectime:
	ld a,(time)
	dec a
	ld (time),a
	call z,makedie
	ret

levelcompleted:		;puts level done text
	set textinverse,(iy+textflags)
	ld a,0
	ld (ld),a
	ld a,3
	ld (currow),a
	ld a,0
	ld (curcol),a
	ld hl,txtlevelcomp
	bcall(_puts)
	res textinverse,(iy+textflags)
wait1:
	call waitkey
	cp g2nd
	jr nz,wait1
	ld a,(levels)
	ld b,a
	ld a,(level)
	inc a
	ld (level),a
	cp b
	jr z,worldcompleted
	ld hl,(offset)
	ld de,192		;each screen is 96 (96*2=192)
	add hl,de
	ld (offset),hl		;increases offset by 192 for next level
	jp leveload
worldcompleted:
	set textinverse,(iy+textflags)
	bcall(_homeup)
	ld hl,txtworldcomp	;puts inverted worldcompleted text
	bcall(_puts)
	call waitkey
	res textinverse,(iy+textflags)
	jp begin
nolevels:
	bcall(_clrlcdf)		;displays no levels text
	bcall(_homeup)
	ld hl,txtnolevels
	bcall(_puts)
	call waitkey
	ret
gameover:
	set textinverse,(iy+textflags)
	ld a,3
	ld (curcol),a
	ld a,3
	ld (currow),a
	ld hl,txtgameover	;puts gameover text inverted in midscreen
	bcall(_puts)
	res textinverse,(iy+textflags)
gowait:
	call waitkey
	cp g2nd
	jp z,begin
	jr gowait
;////////ROUTINES\\\\\\\\
CheckSpot:			;WRITTEN BY ME!!!
	ld de,(cur1)		;loads cur1 map into de
	add a,e			;then it adds e and spot
	ld e,a			;and puts that into e
	ld a,(de)		;then loads value into a
	ret

WaitKey:				;this routine is much better than bcallgetkey
	ei			;because it disaples APD(autopowerdown)
WaitKeyLoop:			;and allows use of 2nd and ALPHA
	halt
	bcall(_getcsc)		;calls getcsc
	or a
	jr z,WaitKeyLoop		;if nothing pressed, jp waitkey loop
	ret

Drawmap:			;BY DAN E.!!!
   ld hl, plotsscreen
   ld bc, 8*256+0
DrawmapLoop:
   ld a, (de)
   push de
   push hl
   ld l, a
   ld h, 0
   add hl, hl
   add hl, hl
   add hl, hl
   ld de, tiles
   add hl, de
   ex de, hl
   pop hl
   push bc
   ld b, 8
   push hl
DrawmapLoop2:
   ld a, (de)
   ld (hl),a
   inc de
   push bc
   ld bc, 12
   add hl, bc
   pop bc
   djnz DrawmapLoop2
   pop hl
   pop bc
   pop de
   inc de
   inc hl
   inc c
   ld a, c
   cp 12
   jr nz, Drawmaploop
   ld c,0
   push de
   ld de, 7*12
   add hl, de
   pop de
   djnz Drawmaploop
   ret

directinput:
	ld	a,0ffh			; reset keyport and querys all keys in group
	out	(1),a			;these next lines look for a keypress
	ld	a,b			;using direct input (VERY FAST)
	out	(1),a
	in	a,(1)
	ret			;returns to where it was called from

calcoff:
	ld      a,1                     ; turn off calc
	out     (3),a                   ;
	ei                              ;
	halt                            ;
	ret


;////////SPRITES\\\\\\\\
titlepic:
 .db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
 .db $00,$00,$00,$00,$00,$00,$20,$00,$00,$00,$00,$00,$00,$00,$00,$00
 .db $00,$00,$02,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$07,$00
 .db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$04,$00,$00,$00,$00,$00
 .db $00,$00,$00,$00,$E0,$00,$18,$40,$01,$D0,$01,$00,$C0,$01,$80,$00
 .db $E0,$00,$38,$00,$07,$FE,$03,$81,$C0,$27,$80,$F8,$E0,$00,$7C,$80
 .db $06,$03,$03,$C0,$F0,$67,$83,$F8,$E0,$00,$7D,$00,$08,$C1,$87,$80
 .db $70,$E7,$87,$B8,$C0,$00,$F8,$00,$18,$C1,$8F,$80,$F0,$C7,$0F,$18
 .db $C0,$1F,$E0,$00,$19,$C1,$8D,$80,$F9,$83,$1E,$00,$C0,$7F,$C0,$00
 .db $19,$87,$09,$C0,$F9,$83,$1C,$00,$C0,$FF,$E0,$00,$07,$9C,$09,$C0
 .db $DD,$87,$38,$00,$00,$FF,$E0,$00,$03,$F8,$33,$E1,$9F,$06,$70,$00
 .db $81,$FF,$F0,$00,$03,$C0,$3D,$F1,$8F,$0E,$30,$00,$01,$FF,$F0,$00
 .db $07,$00,$7C,$F3,$8F,$0E,$30,$60,$01,$FF,$F0,$00,$07,$00,$70,$E3
 .db $83,$06,$3B,$C3,$C1,$FF,$F0,$00,$02,$00,$60,$E1,$03,$06,$1F,$03
 .db $C0,$FF,$E0,$00,$02,$00,$00,$01,$02,$04,$00,$01,$C0,$FF,$E0,$00
 .db $00,$00,$00,$00,$00,$00,$00,$00,$00,$7F,$C0,$00,$00,$00,$00,$00
 .db $00,$00,$00,$00,$00,$1F,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
 .db $00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00,$00
rMrpanic:
 .db $3C,$7C,$BC,$18,$3C,$5A,$18,$24
lMrpanic:
 .db $3C,$3E,$3D,$18,$3C,$5A,$18,$24
;////////TILES\\\\\\\\
tiles:
 ;Tile 0
 .db $00,$00,$00,$00,$00,$00,$00,$00
 ;Tile 1
 .db $AA,$55,$AA,$55,$AA,$55,$AA,$55
 ;Tile 2
 .db $55,$AA,$55,$AA,$55,$AA,$55,$AA
 ;Tile 3
 .db $03,$04,$3C,$7E,$7E,$7E,$3C,$00
 ;Tile 4
 .db $02,$04,$3C,$7E,$7E,$7E,$3C,$00
 ;Tile 5
 .db $3C,$5A,$18,$00,$18,$2C,$24,$18
 ;Tile 6
 .db $18,$2C,$24,$18,$00,$18,$5A,$3C
 ;Tile 7
 .db $C3,$BD,$5A,$3C,$3C,$66,$BD,$C3

cur1:
	.ds 96
cur2:
	.ds 96

;////////TEXT STRINGS\\\\\\\\
txtTime:
	.db "Time: ",0
txtGameOver:
	.db "GAME OVER!",0
txtNoTime:
	.db "Out of Time",0
txtdetect:
	.db "PNCLVL",0
txtlevelcomp:
	.db "Level  Completed",0
txtlevel:
	.db " Level: ",0
txtauthor:
	.db "By Cole South",0
txtnolevels:
	.db "Sorry, No Levels"
	.db " Were Found On  "
	.db "Your Calculator.",0
txtworldcomp:
	.db "Congratulations!"
	.db " You  Completed "
	.db "  All Levels in "
	.db "  This World!!! ",0
.end
END