	.nolist		
	#include "ion.inc"
	#include "keys.inc"
	.list		
#ifdef TI83P		
	.org progstart-2
	.db $BB,$6D

TEXT_MEM  = $8508
APD_BUF   = $86EC
MUL_HL = $4276

#else		
	.org progstart
TEXT_MEM  = $80c9
APD_BUF   = $8265
MUL_HL = $4382

#endif		
		xor a	
		jr nc,Start
progdesc: .DB "Plain Jump v1.2",0

;-------------------------------------------------------------------------------
;Variables in text-memory:
;-------------------------------------------------------------------------------
ORIGLVL = TEXT_MEM+17   ;pointer to start of level
StringPlace = TEXT_MEM+22
ZeroByte = TEXT_MEM+27
;HiScoreTemp = TEXT_MEM+28
Count = TEXT_MEM+30
TempByte = TEXT_MEM+31
MASK = TEXT_MEM+32
ANISCROLLC = TEXT_MEM+33
KEYPRESS = TEXT_MEM+34
BackgroundTemp = TEXT_MEM+35
LvlNo           = TEXT_MEM+50
Top             = TEXT_MEM+51



;-------------------------------------------------------------------------------
BITMAPMEM = APD_BUF ;this points to the graph mem

;-------------------------------------------------------------------------------
;Program start!
;-------------------------------------------------------------------------------
Start:

 res 1,(iy+13)	      


Title:
 set 7,(iy+$14)
 call clrtxtmem

 bcall(_cleargbuf)
 ld   de,plotsscreen
 ld   bc,400
 ld   hl,TitlePicture
 ldir
 ld   hl,SamStr
 ld   de,50*256+32
 call DisplayText
 ld   de, 36*256+44
 call DisplayText
 ld   de, 43*256+30
 call DisplayText



 ld de,57*256+10
 call DisplayText

 ld de,57*256+44
 call DisplayText

 ld  de,57*256+65
 ld hl,HiScore
 bcall(_ldhlind)
 call HL_DECIMAL_MENU

 call ionFastCopy

Wait:
 call OTH_PAUSE

 ld hl,LevelSaved
 ld a,(hl)
 and a
 jr nz,LoadGame
 call LoadLevel
 jr c,ExitGame
 jp RegularGame

OTH_PAUSE:
 call GET_KEY
 cp G_CLEAR
 jr z,ExitGame
 cp G_ENTER
 jr nz,OTH_PAUSE
 ret

LSaveGame:
 ld  (BALLpos), bc
 ld  a, 1
 jr DoExit
ExitGame:
 xor a
DoExit: 
 ld  hl, LevelSaved
 ld (hl),a
     
 set     1,(iy+13)       
 ret

clrtxtmem:
 ld bc,$7f
 ld hl, TEXT_MEM

ClearMem:
 ld (hl),0
 ld d,h
 ld e,l
 inc de
 ldir
 ret

LoadGame:
 ld (hl),0
 ld hl,PJLEVEL 
 push hl
 ld a,(LEVELY)
 ld e,a
 ld d,0
 add hl,de
 ld bc,(BALLpos)
 pop de			; keep trace of level origin (or else you'll get bonus too easily)
 call Setup
 jr StartGameLoop


Setup:
	ld      (ORIGLVL), de
	ld      (LEVELPT), hl
	ld      HL, BlackBall1
	ld      (PLAYERPT1), HL
      ret

LostLife:
	xor     A
	ld      (SPEED), A
NoFade:
	ld      A, (LIFE)
	dec     A
	jp      z, GameOver
	ld      (LIFE), A
lost2:
	xor     A
	ld      HL, SCROLLY
	ld      (HL), A
	ld      HL, (LEVELPT)
FindStone:
	dec     HL
	ld      (LEVELPT), HL
	ld      A, (LEVELY)
	dec     A
	ld      (LEVELY), A
	jr      z, StartAfterLife
	ld      C, $80
	ld      B, 1
FindStone1:
	ld      A, (HL)
	and     C
	jr      nz, FoundDone
	srl     C
	jr      z, FindStone
	ld      A, B
	add     A, 12
	ld      B, A
	jr      FindStone1
FoundDone:
	ld      C, 46
      ld      a,b
      cp      6
      jr      c,StartGameLoop
      dec     b
      dec     b
      dec     b
      dec     b
	jr      StartGameLoop



RegularGame:
      push hl
      ld     hl,LEVELY
      ld     bc,17
      call ClearMem
      pop hl
      ld a,6
      ld (LIFE),a
	ld d,h
	ld e,l
SetParams:
      call Setup
StartAfterLife:
 ld   b, 40
 ld   c, 46
StartGameLoop:
	push bc
      ld hl,APD_BUF
      ld bc,767
      call ClearMem
      set 7,(iy+$14)
      call DisplayInfo
      res 7,(iy+$14)
      call ionFastCopy    ;After save game, text wouldn't display otherwise
	ld      d, 0
	ld      HL, LifeStatus
      call DTX
	ld      A, (LIFE)
	ld      H, 0
	ld      L, A
	bcall(_disphl)
 jr PauseStart
PAUSE:
 push bc
 call DisplayInfo
 ld d,$03
 ld hl,PauseT
 call DTX

PauseStart:
 call OTH_PAUSE
 call DrawScreen
 pop bc
 call ScanBk
;-------------------------------------------------------------------------------
;The game loop:
;-------------------------------------------------------------------------------
GameLoop:
 call DrawBk  ;erase ball
 ld a,c
 cp 46
 jr nz,TestKeys
 push bc
 ld a,b
 add a,8
 ld b,a
 ld c,60
 call FINDPIXEL
 ld de,BITMAPMEM
 add hl,de
 and (hl)
 pop bc
 jp z,LostLife
TestKeys:
 push bc
 ld a,$FE
 out (1),a
 in a,(1)
 ld b,a
 ld a,$BF
 out (1),a
 in a,(1)
 or $0F
 and b
 pop bc
 cp $FF
 jp z,Redraw
TestLeftKey:
 ld d,a
 ld a,(SPEED)
 ld e,a
 bit 1,d
 jr nz,TestRightKey
 ld a,b
 cp 2				; X < 2 ?
 jr c,TestRightKey		; yeah ==> cannot move left
 dec b
 ld a,e
 cp 3
 jr nz,TestRightKey		; if speed = 3, move 2 steps left
 dec b
TestRightKey:
 bit 2,d
 jr nz,TestUpKey
 ld a,b
 cp 80				; x >= 96-16 ?
 jr nc,TestUpKey
 inc b
 ld a,e
 cp 3
 jr nz,TestUpKey
 inc b
TestUpKey:
 bit 3,d
 jr nz,TestDownKey
 ld a,e
 cp 3
 jr nc,TestDownKey
 inc e
TestDownKey:
 bit 0,d
 jr nz,Test2ndKey
 ld a,e
 or a
 jr z,Test2ndKey
 dec e
Test2ndKey:
 ld a,e
 ld (SPEED),a
 bit 5,d
 jr nz,TestExitKey
 ld a,(JUMPFLAG)
 or a
 jr nz,TestExitKey
 inc a
 ld (JUMPFLAG),a
TestExitKey:		; [MODE]
 bit 6,d
 jp z,PAUSE
TestPauseKey:
 bit 7,d
 jp z,LSaveGame
Redraw:
 ld a,$FD
 out (1),a
 in a,(1)
 and $40
 jp z, GameOver

 ld a,(JUMPFLAG)
 or a
 jr z,ReallyRedraw
 dec a
 jr nz,FlyDown
 ld a,c
 cp 20
 jr z,SetFlyDown
 dec c
 jr ReallyRedraw
SetFlyDown:
 ld a,2
 ld (JUMPFLAG),a
FlyDown:
 ld a,c
 cp 46
 jr nz,FlyDown1
 xor a
 ld (JUMPFLAG),a
 jr ReallyRedraw
FlyDown1:
 inc c
ReallyRedraw:
 ld a,(SPEED)
 or a
 jr z,RedrawBall
 push bc
 ld b,a
DoScrolling:
 call DoScroll
 djnz DoScrolling
 pop bc

RedrawBall:
 call ScanBk
 ld de,WhiteBall
 call WhiteSprite
 ld de,(PLAYERPT1)
 call BlackSprite

 ld a,(ANISCROLLC)
 ld hl,SPEED
 add a,(hl)
 ld (ANISCROLLC),a
 cp 6
 jr c,GoOnMainLoop

 xor a
 ld (ANISCROLLC),a
 ld hl,ANICOUNT
 inc (hl)
 ld a,(hl)
 cp 3
 jr nz,NextFrame
 ld (hl),0
 ld hl,BlackBall1
 ld (PLAYERPT1),hl
 jr GoOnMainLoop
NextFrame:
 ld hl,(PLAYERPT1)
 ld de,32
 add hl,de
 ld (PLAYERPT1),hl


GoOnMainLoop:

 ;copy GRAPH-MEM to LCD-MEM
 push bc
 ld de,plotsscreen
 ld hl,APD_BUF
 ld bc,768
 ldir
 ei
 halt
 call ionFastCopy
 pop bc
  jp GameLoop

 

;---------------------------------------------------------
; WhiteSprite: draws each white element of a 24*24 sprite
;---------------------------------------------------------
WhiteSprite:
 push bc
 push de
 ld a,32	
 ld (TempByte),a
 call FINDPIXEL
 ld de,BITMAPMEM
 add hl,de
 pop de
 ld c,a
 ld b,128
WSLoop:
 ld a,(de)
 and b
 jr nz,WSLoop1
 ld a,c
 cpl
 and (hl)
 ld (hl),a
WSLoop1:
 rrc c
 jr nc,WSLoop2
 inc hl
WSLoop2:
 rrc b
 jr nc,WSLoop
WSNextLine:
 inc de
 ld a,(TempByte)
 dec a
 ld (TempByte),a
 jr z,WSEndBMP
 bit 0,a
 jr nz,WSLoop
 push de
 ld de,10
 add hl,de
 pop de
 jr WSLoop
WSEndBMP:
 pop bc
 ret

;---------------------------------------------------------
; BlackSprite: draws each black element of a 24*24 sprite
;---------------------------------------------------------
BlackSprite:
 push bc
 push de	
 ld a,32
 ld (TempByte),a
 call FINDPIXEL
 ld de,APD_BUF
 add hl,de
 pop de
 ld c,a
 ld b,128
BSLoop:
 ld a,(de)
 and b
 jr z,BSLoop1
 ld a,(hl)
 or c
 ld (hl),a
BSLoop1:
 rrc c
 jr nc,BSLoop2
 inc hl
BSLoop2:
 rrc b
 jr nc,BSLoop
BSNextLine:
 inc de
 ld a,(TempByte)
 dec a
 ld (TempByte),a
 jr z,BSEndBMP
 bit 0,a
 jr nz,BSLoop
 push de
 ld de,10
 add hl,de
 pop de
 jr BSLoop
BSEndBMP:
 pop bc
 ret



DrawScreen:

 push bc
 ld b,32
 
 ld hl, (levelpt)
 dec hl
 ld (levelpt),hl
 ld hl, levely
 dec (hl)

DS_loop:
 ld hl, scrolly 
 inc (hl)		;increment SCROLLY
 ld d,(hl)
 ld a,d
 cp   32            ;SCROLLY = 32 ?
 jr nz, DS_same
 
 xor a			;yes, set it to 0,
 ld   d, a
 ld (hl), a
 ld hl, levely
 inc (hl)
 ld hl,(LEVELPT)
 inc hl
 ld (LEVELPT),hl
 
DS_same: 
 call LoadHLScr
 
 djnz DS_loop
 pop bc
 ret

;-------------------------------------------------------------------------------
;DoScroll: handles scrolling
;-------------------------------------------------------------------------------
DoScroll:
 ld   a, (SCROLLY)   ;increment SCROLLY
 inc  a
 ld   d, a
 ld   (SCROLLY), a   ;save it
 cp   32             ;SCROLLY = 32 ?
 jr   nz, LoadHLScr
 ld   a, 0           ;yes, set it to 0,
 ld   d, 0
 ld   (SCROLLY), a
 set 7,(iy+$14)    ;Write text to plotsscreen
 ld     de,$0000
 ld     HL, (SCORE)
 call   HL_DECIMAL_MENU      ;display HL menu style
 res 7,(iy+$14)     ;Stop Writing text to plotsscreen
 push bc            ;Save bc
 ld   de, BITMAPMEM ;Copy to APD_BUF
 ld   hl, plotsscreen ;Copy from plotsscreen
 ld   bc,72         ;Copy first 6 lines of GM
 ldir               ;Do it
 pop  bc            ;Restore BC

 ld a,(LEVELY)
 inc a
 cp 251
 jr nz,NoBonus
Bonus:
 ld hl,LIFE
 inc (hl)
NoBonus:
 cp 252
 jr nz,GoOnScroll
 xor a
 ld   (LEVELY), a
 ld   hl, (ORIGLVL)
 dec hl
 dec hl
 ld   (LEVELPT), hl
GoOnScroll:
 ld (LEVELY),a
 ld hl,(SCORE)
 ld d,0
 ld a,(SPEED)
 ld e,a
 add hl,de
 ld (SCORE),hl
 ld hl,(LEVELPT)
 inc hl
 ld (LEVELPT),hl

LoadHLScr:
 ld   hl, (LEVELPT)  ;get Levelinformation from LEVELPT
 inc  hl
 inc  hl
 ld   e, (hl)
 CALL DrawLine
 ld   a, (SCROLLY)
 add  a, 32
 ld   d, a
 ld   hl, (LEVELPT)
 inc hl
 ld   e, (hl)
;-------------------------------------------------------------------------------
;DrawLine: this draws a line at y-pos D, accoriding to bits in E
;-------------------------------------------------------------------------------
DrawLine:
 push   BC
 push   IX
 ld     IX, StartLine
 ld     B,  0
 ld     C,  D
 sla    C
 add    IX, BC
 add    IX, BC
;IX now contains pointer to StartLine1

 ld     L, $80
DrawLine_Loop1:
 ld     A, 47 ;63
 sub    (IX)
 ld     B, A
 ld     A, 47 ; 63
 sub    (IX+1)
 ld     C, A
 bit    4, L
 jr     z, NotMiddle
 ld     C, 47 ;63
NotMiddle:
 inc    IX

 ld     A, E
 and    L
 jr     z, WhiteLine_1
 call   BlackLine
 jr     DrawLine_Loop1_2
WhiteLine_1:
 call   WhiteLine
DrawLine_Loop1_2:
 rrc    L
 ld     A, L
 cp     $08
 jr     nz, DrawLine_Loop1

 dec    IX
 ld     B, 47 ;63
 jr     SkipMiddle
DrawLine_Loop2:
 ld     A, (IX+1)
 add    A, 47 ; 63
 ld     B, A
SkipMiddle:
 ld     A, (IX)
 add    A, 47 ;63
 ld     C, A
 dec    IX

 ld     A, E
 and    L
 jr     z, WhiteLine_2
 call   BlackLine
 jr     DrawLine_Loop2_2
WhiteLine_2:
 call   WhiteLine
DrawLine_Loop2_2:
 rrc    L
 jr     nc, DrawLine_Loop2
 pop    IX
 pop    BC
 ret

;---------------------------------------------------------------
; WhiteLine: draws a horizontal, white line from (B,D) to (C,D)
;---------------------------------------------------------------
WhiteLine:
 push hl
 push de
 ld a,c
 sub b
 push af
 ld c,d
 call FINDPIXEL
 ld de,BITMAPMEM
 add hl,de
 ld b,a
 pop af
 ld c,a
WLoop:
 ld a,b
 cpl
 and (hl)
 ld (hl),a
 dec c
 jr z,EndWhite
 rrc b
 jr nc,WLoop
 inc hl
 jr WLoop

EndWhite:
 pop  de
 pop  hl
 ret

;-------------------------------------------------------------------------------
;BlackLine: draws a horizontal, black line from (B,D) to (C,D)
;-------------------------------------------------------------------------------
BlackLine:
 push hl
 push de
 ld a,c
 sub b
 push af
 ld c,d
 call FINDPIXEL
 ld de,BITMAPMEM
 add hl,de
 ld b,a
 pop af
 ld c,a
BLoop:
 ld a,(hl)
 or b
 ld (hl),a
 dec c
 jr z,EndBlack
 rrc B
 jr nc,BLoop
 inc hl
 jr BLoop
EndBlack:
 pop de
 pop hl
 ret

;-------------------------------------------------------------------------------
;ScanBk: stores background from(B,C)-(B+24,C+24) in BKPT
;-------------------------------------------------------------------------------
ScanBk:
 push bc
 call FINDPIXEL
 ld de,BITMAPMEM
 add hl,de
 ld de,BackgroundTemp
 ld b,23
ScanLoop:
 ldi
 ldi
 ldi		;move 4 bytes from hl to de
 ldi
 push de          ;next line in vid-ram
 ld   de, 8						
 add  hl, de
 pop  de
 djnz ScanLoop
 pop  bc
 ret

;-------------------------------------------------------------------------------
;DrawBk: draws background from(B,C)-(B+24,C+24) in BKPT
;-------------------------------------------------------------------------------
DrawBk:
 push bc
 call FINDPIXEL
 ld de,BITMAPMEM
 add hl,de
 ld de,BackgroundTemp
 ex de,hl
 ld   b, 23       ;23 lines to draw
PutLoop:
 ldi
 ldi		;move 4 bytes from hl to de
 ldi
 ldi
 push hl          ;next line in vid-ram
 ld   hl, 8	
 add  hl, de
 ex   de, hl
 pop  hl
 djnz PutLoop
 pop  bc
 ret


;------------------------------------------------------------------------------
;DisplayText: displays the text in DE at position(H,L) in menustyle
;------------------------------------------------------------------------------
DisplayText:
 ld  (pencol), de
 bcall(_vputs)
 ret

DTX:
 ld e,$03
DTX2:
 ld (currow),de
 bcall(_puts)
 ret

;-------------------------------------------------------------------------------
;Some important variables:
;-------------------------------------------------------------------------------
Instr:	   .db "PJ v1.2 by A. Ess & "
SamStr:        .db "Sam Heald",0
ByStr:	   .db "By",0
EssStr:        .db "Andreas Ess",0
HiScrTxt:  .db "HiScore: ",0
HiScrName: .db "AND",0
HiScore:   .dw 10                       ;This is standard hi-score
HiScr:     .db "NEW HIGH SCORE!",0
EnterInit: .db "ENTER YOUR INITIALS:",0
PauseT: .db "* PAUSED *",0

StartLine:
 .db 16,12,8,4	  ;#0
 .db 16,12,8,4
 .db 17,12,8,4
 .db 17,13,8,4
 .db 18,13,9,4    ;#4
 .db 18,13,9,4
 .db 19,14,9,4
 .db 19,14,9,5
 .db 20,14,10,5    ;#8
 .db 20,15,10,5
 .db 21,15,10,5
 .db 21,15,10,5
 .db 22,16,11,5    ;#12
 .db 22,16,11,5
 .db 23,16,11,6
 .db 23,17,11,6
 .db 24,17,12,6    ;#16
 .db 24,17,12,6
 .db 25,18,12,6
 .db 25,18,12,6
 .db 26,18,13,6    ;#20
 .db 26,19,13,7
 .db 27,19,13,7
 .db 27,19,13,7
 .db 28,20,14,7    ;#24
 .db 28,20,14,7
 .db 29,20,14,7
 .db 29,21,14,7
 .db 30,21,15,8    ;#28
 .db 30,21,15,8
 .db 31,22,15,8
 .db 31,22,15,8
 .db 32,22,16,8    ;#32
 .db 32,23,16,8		
 .db 33,23,16,9			 
 .db 33,23,16,9
 .db 34,24,17,9    ;#36
 .db 34,24,17,9
 .db 35,24,17,9
 .db 35,25,17,9
 .db 36,25,18,9   ;#40
 .db 36,25,18,9
 .db 37,26,18,10
 .db 37,26,18,10
 .db 38,26,19,10   ;#44
 .db 38,27,19,10
 .db 39,27,19,10
 .db 39,27,19,10
 .db 40,28,20,10    ;#48
 .db 40,28,20,11
 .db 41,28,20,11
 .db 41,29,20,11
 .db 42,29,21,11    ;#52
 .db 42,29,21,11
 .db 43,30,21,11
 .db 43,30,21,11
 .db 44,30,22,12    ;#56
 .db 44,31,22,12
 .db 45,31,22,12
 .db 45,31,22,12
 .db 46,32,23,12    ;#60
 .db 46,32,23,12
 .db 47,32,23,12
 .db 47,32,23,12


;-------------------------------------------------------------------------------
;Bitmapdata:
;-------------------------------------------------------------------------------

WhiteBall:
 .db $FF,$FF,$FF,$FF,$F8,$3F,$E0,$0F,$C0,$07,$C0,$07,$80,$03,$80,$03
 .db $80,$03,$80,$03,$80,$03,$C0,$07,$C0,$07,$E0,$0F,$F8,$3F,$FF,$FF,

BlackBall1:
 .db $00,$00,$07,$C0,$18,$30,$20,$08,$40,$04,$40,$04,$80,$02,$80,$02
 .db $80,$02,$C0,$06,$A0,$0A,$54,$54,$6A,$AC,$35,$58,$1A,$B0,$07,$C0

 .db $00,$00,$07,$C0,$18,$30,$20,$08,$40,$04,$40,$04,$8A,$A2,$D5,$56
 .db $AA,$AA,$D5,$56,$8A,$A2,$40,$04,$40,$04,$20,$08,$18,$30,$07,$C0

 .db $00,$00,$07,$C0,$1A,$B0,$35,$58,$6A,$AC,$54,$54,$A0,$0A,$C0,$06
 .db $80,$02,$80,$02,$80,$02,$40,$04,$40,$04,$20,$08,$18,$30,$07,$C0

;---------------------------------------------------------------------------------
;Title Picture
;---------------------------------------------------------------------------------
TitlePicture:
    .db     16,0,8,0,0,0,0,0,0,0,0,16,0,0,0,0,0,0,0,0,0,8,0,48,16,8,4,0,0,0,0
    .db     0,0,1,131,248,0,0,2,0,0,0,0,0,0,0,127,252,16,16,3,0,0,0,0,198,2,0,62,6,48,96
    .db     35,224,0,2,3,239,128,0,24,131,60,224,15,48,0,0,7,109,224,128,26,113,255,192,6,28,0,8,14,120,112
    .db     72,52,249,195,192,6,159,144,0,28,56,24,121,180,252,1,193,6,220,0,16,56,48,184,61,241,252,0,228,119,48
    .db     0,48,112,17,176,63,241,252,60,120,255,240,112,96,232,139,112,55,113,252,126,120,223,225,252,224,118,56,102,119,112
    .db     249,126,113,143,243,15,192,57,246,111,99,114,3,126,97,143,158,3,192,31,179,253,226,49,143,60,225,143,30,0,224
    .db     31,51,249,224,17,254,1,227,143,31,0,112,63,51,240,192,16,248,135,247,7,31,32,48,63,51,248,64,18,192,127
    .db     255,7,31,112,56,103,51,248,64,16,96,63,255,39,31,60,24,195,49,248,64,9,96,31,222,115,31,63,29,131,24
    .db     249,80,137,96,31,142,115,31,63,31,3,88,48,77,1,184,15,158,115,95,63,31,143,76,1,71,64,190,71,156,3
    .db     30,63,31,254,44,2,71,164,219,163,28,49,94,191,63,252,108,134,199,184,216,177,28,9,92,62,56,240,222,124,135
    .db     180,220,216,184,113,156,190,58,0,63,57,142,205,204,209,62,120,153,188,54,3,243,195,204,124,60,230,65,120,250,121
    .db     19,31,193,254,252,55,248,111,58,120,253,250,0,255,131,252,124,7,192,127,196,124,127,187,7,253,131,28,24,3,0
    .db     121,255,236,118,25,175,224,196,12,24,2,0,48,127,207,224,28,31,0,64,4,8,6,0,0,31,199,192,63,252,0
    .db     0,2,8,4,0,0,3,128,0,63,240,0,32,0,0,4,0,0,1,128,0,97,192,0,0,0,136,0,0,0,0
    .db     192,1,0,128,0,0,0,0,8,0,0,0,64,0,0,128,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0





;-----------------------------------------------------------------------------
;HL_DECIMAL_MENU(Magnus Hagander):Writes the in HL stored word in menu font
;-----------------------------------------------------------------------------
HL_DECIMAL_MENU:
 ld (pencol),de
 push bc
 ld   de, StringPlace+4
 ld   b,  5
ConvLoop:
 bcall(_divHLby10)
 add  a,'0'
 ld   (de), a
 dec  de
 djnz ConvLoop
 ld   hl, StringPlace
 bcall(_vputs)
 pop bc
 ret




;-------------------------------------------------------------------------------
;FINDPIXEL Routine, by Patrick Davidson [From Zkart3d 82]
;-------------------------------------------------------------------------------
FINDPIXEL_DATA:
         .db      128,64,32,16,8,4,2,1
FINDPIXEL:
         push     bc
         ld       a,b
         and      7
         ld       hl,FINDPIXEL_DATA
         ld       e,a
         ld       d,0
         add      hl,de
         ld       e,(hl)            ; E = pixel mask

	
         ld a,c	

         add      a,a
         add      a,c               ; A = 3 * Y
         ld       l,a
         ld       h,0
         add      hl,hl
         add      hl,hl             ; HL = 12 * Y
         ld       a,b
         ld       b,0
         rrca
         rrca
         rrca
         and      15
         ld       c,a
         add      hl,bc
         ld       a,e               ; A = pixel mask
         pop      bc
         ret


LifeStatus: .db "Lives Left:",0



DisplayInfo:
 push bc
 bcall(_clrlcdf)
 bcall(_cleargbuf)
 ld hl,plotsscreen
 call Invert
 ld hl,plotsscreen+672
 call Invert2
 ld de,$0003
 set 3,(iy+$05)
 ld hl,Instr
 call DisplayText
 ld de,$3909
 ld hl,HiScrTxt
 call DisplayText

 ld de,$392D
 call DisplayText

 ld de,$3942
 ld hl,HiScore
 bcall(_ldhlind)
 call HL_DECIMAL_MENU
 res 3,(iy+$05)
 pop bc
 ret

ID: .db 3, 0


LoadLevel:
 call DisplayInfo
 xor a
 ld (LvlNo),a
 ld hl,(progptr)			; $FE6E
SearchLoop:
 ld ix,ID
 call ionDetect
 jr nz,SDone
 xor a
 cp (hl) \ jr nz,KeepSearching
 ld hl,LvlNo
 inc (hl)
KeepSearching:
 ex de,hl
 jr SearchLoop

SDone:
 xor a
 ld (SetD+1),a
 ld a,(LvlNo)
 or a
 jr nz,tt
NoLevels:
 ld de, $0801
 ld hl, NoLevel
 call DisplayText
 call ionFastCopy
 call OTH_PAUSE
 res 7,(iy+$14)
 scf
 ret

tt:
 xor a
 ld (Top),a
ShowLevels:
 ld hl,$0801
 ld (pencol),hl
 ld hl,GRAPH_MEM+(7*12)		;+112
 ld bc,599			;799
 call ClearMem
 ld b,8
 ld a,(LvlNo)
 cp b
 jr nc,ShowLevel2
 ld b,a
ShowLevel2:
 push bc
 dec b
 ld l,b
 ld h,6
 bcall(MUL_HL)
 ld de, $0801
 ld a,l
 add a,d
 ld d,a
 pop bc
ShowLevel:
 ld (pencol),de
 push de

 push bc
 ld a,(Top)
 add a,b
 call LoadLevelB
 ex de,hl
 inc hl
FindLengthLoop:
 inc hl
 ld a,(hl)
 cp 9
 jr nc,FindLengthLoop

 ld b,a
DisplayNameLoop:
 dec hl
 ld a,(hl)
 push hl
 bcall(_vputmap)
 pop hl
 djnz DisplayNameLoop

 pop bc
 pop de
 ld a,d
 sub 6
 ld d,a
 djnz ShowLevel
 ld hl,Top
 ld a,(LvlNo)
 sub (hl)
 cp 8
 jr c,SetMax
 ld a,8
SetMax:
 ld e,a
 dec e
SetD:
 ld d,0
 call ShowBar
CL_KeyLoop:
 push de
 bcall(_getcsc)
 pop de
 cp G_DOWN
 jr z,ScrollDown
 cp G_UP
 jr z,ScrollUp
 cp G_ENTER
 jr z,AcceptLevel
 cp G_CLEAR
 jr nz,CL_KeyLoop
 scf
 res 7,(iy+$14)
 ret
ScrollDown:
 call ShowBar
 ld a,d
 cp e
 jr z,ScrollL_Down
 inc d
 jr ShowIt
ScrollL_Down:
 ld (SetD+1),a
 ld a,(LvlNo)
 sub 8
 jr c,ShowIt
 ld hl,Top
 cp (hl)
 jr z,ShowIt
 inc (hl)
 jr Sh
ScrollL_Up:
 ld (SetD+1),a
 ld a,(Top)
 or a
 jr z, ShowIt
 dec a
 ld (Top),a
Sh:
 jp ShowLevels
ScrollUp:
 call ShowBar
 ld a,d
 or a
 jr z,ScrollL_Up
 dec d
ShowIt:
 call ShowBar
 jr CL_KeyLoop
AcceptLevel:
 ld a,(Top)
 add a,d
 inc a
 call LoadLevelB

 ld de,PJLEVEL
 push de
 ld bc,260
 ldir
 pop hl
 ret			; return hl = pointer to the program.

LoadLevelB:
 ld b,a
 ld de,(progptr)
LoadLevelB2:
 ex de,hl
 ld ix,ID
 push bc
 call iondetect
 pop bc
 ld a,(hl)
 inc hl
 or a
 jr nz,LoadLevelB2 ;not an Orzunoid Level
 djnz LoadLevelB2
 ret


ShowBar:
 push de
 ld l,d
 ld h,72
 bcall(MUL_HL)
 ld de,plotsscreen+(8*12)
 add hl,de
 call Invert
 pop de
 ret

Invert2:
 ld bc,96
 jr InvLoop

Invert:
 ld bc,84
InvLoop:
 ld a,(hl)
 cpl
 ld (hl),a
 inc hl
 dec bc
 ld a,b
 or c
 jr nz,InvLoop
 jp ionFastCopy


GameOver:
 call GET_KEY ;Clear last key press
 ld  hl, LevelSaved
 xor a
 ld (hl),a

HiScoreHand:
 ld hl, HiScore
 bcall(_ldhlind)
 ld de,(SCORE)
 bcall(_cphlde)
 jp nc,ExitGame
 ld hl,Hiscore
 ld (hl),e
 inc hl
 ld (hl),d
HiWrite:
 bcall(_clrlcdf)
 ld de,$0100
 ld hl,HiScr
 call DTX2
 ld de,$0A0F
 call DisplayText
 ld hl, $0603
 ld (currow),hl
 ld hl,HiScrName
         push     hl
         ld       b,3
space_loop:
         ld       (hl), ' '
         inc      hl
         djnz     space_loop
         pop      ix

enter_name_loop:
	   call    GET_KEY
         or       A
         jr       z,enter_name_loop
         cp       $38
         jr       z,backup
         SUB      9
         jp      z,ExitGame
         dec      a
         jp       m,enter_name_loop
         ld       hl,chartable
         ld       e,a
         ld       d,0
         add      hl,de
         ld       a,3
         cp       b
         jr       z,enter_name_loop
         ld       a,(hl)
         or       a
         jr       z,enter_name_loop
         ld       (ix),a
	 bcall(_putc)
         inc      b
         inc      ix
         jr       enter_name_loop
backup:  xor      a
         cp       b
         jr       z,enter_name_loop
         dec      b
         dec      ix
         ld       a,' '

         ld       (ix),a
         ld       hl,curcol
         dec      (hl)
	  bcall(_putc)
         dec      (hl)
         jr       enter_name_loop


chartable:
         .db      ":WRMH."
         .DB      "..0VQLG!..ZUPKFC"
         .DB      "..YTOJEBX.>SNIDA"
         .DB      ".12345.."

PJLEVEL:
 .ds 260

NoLevel:    .db "No levels found!",0

LevelSaved:	.db 0  ;standard: no level saved

LEVELY:		.db 0	;position in level
SCROLLY:	.db 0	;to_level_data	;position in level-y
LEVELPT:	.dw 0	;pointer 
BKPT:		.dw 0	;pointer to background
PLAYERPT:	.dw 0	;pointer to player
JUMPFLAG:	.db 0	;0 = no jump; 1 = up; 2 = down
PLAYERPT1:	.dw 0	;pointer to black-player image
ANICOUNT:	.db 0	;animation counter
SPEED:		.db 0	;speed of ball
LIFE:		.db 0	;lifes of player
SCORE:		.dw 0	;contains player's score
BALLpos:	.dw 0	;only needed for savegame

GET_KEY:
 push bc
 bcall(_getk)
 pop bc
 ret

.end


