;-----------------
;   Front  End
;-----------------
                org #6000

Entry_Point     equ *

MainL           equ #4000
vOutIt          equ #7003

                jr Start

                defb "Lavinia's Massage Parlour:Tel(061) 223-3519"

Start           call FuckBasic

                ld hl,(23672)
                ld (Seed),hl

                ld hl,(23674)
                ld a,r
                ld h,a
                ld (Seed+2),hl

FrontLoop       call Screen1

                call MainP

                pop iy ; Success 0=Kilt 1=Ok 2=Quit
                pop bc ; Num Dead Things
                pop de ; Number of rings
                pop hl ; Score

                call FrontScore

                ld de,MessSpace
                call PrintString

Pause_Lp        call FrontReadKey
                cp " "
                jr nz Pause_Lp

                ld de,MessWait
                call PrintString

                jr FrontLoop

;Game            pop iy
                ld hl,1523
                push hl
                ld hl,8
                push hl
                ld hl,1
                push hl
                ld hl,1
                push hl
                jp (iy)

FuckBasic       di
                im 2
                pop iy
                ld sp,0
                push iy
                ld de,#5B00
                ld hl,#6000
                ld bc,#4F0
                ldir
                ld a,0
                out (#FE),a
                ret

Splodge2        defb "  I Had A Little Froggy  ",13,0

Splodge1        call Splodge2

Decode          ld hl,MainP
                ld bc,MainL
DecodL          ld a,(hl)
                xor l
                xor h
                ld (hl),a
                cpi
                jp v DecodL
                ret

Screen1         ld de,MessS1
                call PrintString

                ld de,HighScorer
                call PrintString

                ld de,MessHS
                call PrintString

                ld hl,(HighScore)
                call DecOut

                ld de,DecBuf
                call PrintString

                ld de,MessSpace
                call PrintString

Pause_Lp2       call FrontReadKey
                cp " "
                jr nz Pause_Lp2

                ld de,MessWait
                call PrintString
                ret

PrintString     ld hl,(Cursor)

PStringL        ld a,(de)
                inc de
                cp #20
                jr c PStringC

                call vOutIt

                call DoRight

                jr PStringL

PStringC        or a
                jr nz PStringC1

                ld (Cursor),hl
                ret

PStringC1       ld ix,PStringL
                push ix

                ld ix,Table
                ld b,9

PStringC2       cp (ix)
                jr z PStringC3

                inc ix
                inc ix
                inc ix

                djnz PStringC2

                jp 0

PStringC3       ld c,(ix+1)
                ld b,(ix+2)

                push bc
                ret

Table           defb #0D
                defw DoCR

                defb #0C
                defw DoCLS

                defb #11
                defw DoLeft

                defb #12
                defw DoRight

                defb #13
                defw DoUp

                defb #14
                defw DoDown

                defb #08
                defw DoDelete

                defb #0A
                defw DoSpace

                defb #10
                defw DoMC

DoCR            ld l,0
                inc h
                ret

                defb "Fuck Off !"

DoLeft          dec l
                ret

                defb "Fuck Off !!"

DoRight         inc l

                ld a,l
                cp 32
                jr z DoCR

                ret

                defb "Fuck Off !!!"

DoUp            dec h
                ret

                defb "Fuck Off !!!!"

DoDown          inc h
                ret

                defb "Fuck Off !!!!!"

DoDelete        dec l

                ld a,#20
                call vOutIt

                ret

                defb "Fuck Off !!!!!!"

DoSpace         ld a,(de)
                inc de

                add a,l
                ld l,a

                ret

DoCLS           push de

                ld hl,#4000
                ld de,#4001
                ld bc,#1B00
                ld (hl),0
                ldir

                pop de

                ld hl,0
                ret

DoMC            ld a,(de)
                ld l,a
                inc de

                ld a,(de)
                ld h,a
                inc de

                ret

                defb "Fuck Off !!!!!!!"

FrontReadKey    push hl
                push de
                push bc

                call K0

                pop bc
                pop de
                pop hl
                ret

K0              ld c,#FE
                ld ix,KMask

                ld e,8

K1              ld b,(ix+0)

                in a,(c)
                or (ix+8)
                ld d,a

                cpl
                and (ix+16)
                ld (ix+16),d

                jr nz K2

                inc ix
                dec e
                jr nz K1

                jr K0

K2              ld l,5

                ld h,a

K3              in a,(c)
                or (ix+8)

                cp d
                jr nz K0

                dec l
                jr nz K3

                dec e
                xor a
 KNew1          srl h
                jr c KNew

                inc a
                jr KNew1

KNew            add a,a
                add a,a
                add a,a

                or e

K9              ld hl,CharTab

                add a,l
                ld l,a
                ld a,h
                adc a,0
                ld h,a

                ld bc,#FEFE

                in a,(c)
                bit 0,a

                ld a,(hl)
                ret nz

                cp #40
                jr c KA

                xor #20
                ret

KA              cp #30
                ret nz

                ld a,8
                ret

CharTab         defb 8,"AQ10P",13,32
                defb "ZSW29OL",8
                defb "XDE38IKM"
                defb "CFR47UJN"
                defb "VGT56YHB"

                defb "Yes cunt - a keyboard table"

KMask           defb #7F,#BF,#DF,#EF,#F7,#FB,#FD,#FE
                defb #E2,#E0,#E0,#E0,#E0,#E0,#E0,#E1
                defb #FF,#FF,#FF,#FF,#FF,#FF,#FF,#FF

Delay1          push hl
                push de
                push bc
                push af
                pop af
                pop bc
                pop de
                pop hl
                ret

DecOut          ld c,1

                ld ix,DecBuf
                ld de,10000
                call Dec1

                ld de,1000
                call Dec1

                ld de,100
                call Dec1

                ld de,10
                call Dec1

                ld de,1
                ld c,0
                call Dec1

                ld (ix),0
                ret

Dec1            xor a

Dec2            inc a
                sbc hl,de
                jr nc Dec2

                add hl,de
                cp c
                ret z

                add a,"0"-1
                ld (ix),a
                inc ix

                ld c,0
                ret

;OutIt          push hl
                push af
                xor a
                srl h
                rra
                srl h
                rra
                or l
                ld l,a
                ld a,h
                and #7
                or #F0
                ld h,a
                pop af
                inc hl
                inc hl
                inc hl
                ld (hl),a
                pop hl
                ret

MessS1          defb #C
                defb "Halls of the Things",#D
                defb 13
                defb "MOVEMENT  N:Up"
                defb #A,7
                defb "M:Down"
                defb 13
                defb 10,10
                defb "Z:Left"
                defb #A,5
                defb "X:Right"
                defb #D,10,2
                defb "Doors"
                defb 10,3
                defb "O:Open"
                defb 10,5
                defb "C:Close"
                defb #D,#D
                defb "OBJECTS"
                defb 10,3
                defb "K:Keep"
                defb 10,5
                defb "D:Drop"
                defb #D,#D
                defb "WEAPONS"
                defb 10,3
                defb "S:Sword"
                defb #D,#D,10,2
                defb "Arrows  A:Shoot"
                defb #D,10,10
                defb "5:Left"
                defb 10,5
                defb "8:Right"
                defb #D,10,10
                defb "7:Up"
                defb 10,7
                defb "6:Down"
                defb #D,#D,10,2
                defb "Spells  F:Fireball L:Lightning"
                defb 10,10
                defb "H:Heal"
                defb #D,#D
                defb "VARIOUS"
                defb 10,3
                defb "1:Display status"
                defb #D,10,9
                defb "sp:Centre screen"
                defb #D,13
                defb "High Score : "
                defb 0

MessCLS         defb #C,0

MessKill        defb "You have been killed",13,13,0

MessQuit        defb "You gave up , coward",13,13,0

MessFini        defb "You are victorious !",13,13,0

MessNRs         defb "You collected ",0

MessNR1         defb "one",0

MessNR0         defb "no",0

MessNRe         defb " ring",0

MessNR7         defb "all seven",0

MessDTs         defb "You killed ",0

MessDTe         defb " monster",0

MessK           defb "and the golden key.",#D,0

MessSc          defb "and scored ",0

MessSD          defs 6
MessSe          defb " points.",13,0

Messs           defb "s",0

MessCR          defb 13,0

MessB1          defb "Well done !",13
                defb "you have the high score",13
                defb "and have bettered ",0

MessB2          defb "by ",0

MessQN          defb "Please enter your name",13
                defb "____________",#D,#13,0

MessSOne        defb "someone",0,0,0,0,0,0,0

MessACC         defw 0

MessBS          defb 8,"_",#11,0

MessHS          defb "  ",0

MessSpace       defb #10,#00,#17,"Press SPACE to continue",0
MessWait        defb #10,#00,#17,"Please wait for a second",0

FrontScore      ld (cScore),hl
                ld (cNRings),de
                ld (cNDTs),bc
                ld (cType),iy

                ld de,MessCLS
                call PrintString

                ld de,Score2
                push de

                ld a,(cType)

                ld de,MessKill
                or a
                jr nz Score1

                jp PrintString

Score1          cp 1
                ld de,MessFini
                jp z PrintString

                ld de,MessQuit
                jp PrintString

Score2          ld de,MessNRs
                call PrintString

                ld de,ScoreR2
                push de
                ld a,(cNRings)
                or a
                ld de,MessNR0
                jp z PrintString

                ld de,MessNR1
                cp 1
                jp z PrintString

                ld de,MessNR7
                cp 7
                jp nc PrintString

                ld hl,(cNRings)
                call DecOut
                ld de,DecBuf
                jp PrintString

ScoreR2         ld de,MessNRe
                call PrintString

                ld a,(cNRings)
                cp 1

                push af

                ld de,Messs
                call nz PrintString

                ld de,MessCR
                call PrintString

                pop af

                ld de,MessK
                cp 8
                call z PrintString

                ld de,MessDTs
                call PrintString

                ld de,ScoreR3
                push de

                ld a,(cNDTs)
                ld de,MessNR0
                or a
                jp z PrintString

                ld de,MessNR1
                cp 1
                jp z PrintString

                ld hl,(cNDTs)
                call DecOut

                ld de,DecBuf
                jp PrintString

ScoreR3         ld de,MessDTe
                call PrintString

                ld de,Messs
                ld a,(cNDTs)
                cp 1
                call nz PrintString

                ld de,MessCR
                call PrintString

                ld de,MessSc
                call PrintString

                ld de,ScoreR4
                push de

                ld hl,(cScore)
                ld de,MessNR0
                ld a,l
                or h
                jp z PrintString

                call DecOut

                ld de,DecBuf
                jp PrintString

ScoreR4         ld de,MessSe
                call PrintString

                ld hl,(HighScore)
                ld de,(cScore)
                or a
                sbc hl,de
                ret nc

                ex de,hl
                ld hl,0
                or a
                sbc hl,de
                call DecOut

                ld de,MessB1
                call PrintString

                ld de,HighScorer
                call PrintString

                ld de,MessCR
                call PrintString

                ld de,MessB2
                call PrintString

                ld de,DecBuf
                call PrintString

                ld de,MessSe
                call PrintString

                ld hl,(cScore)
                ld (HighScore),hl
                call GetInputName

                ret

PrintACC        push hl
                push de
                push bc
                push af

                ld de,MessACC
                ld (de),a
                call PrintString

                pop af
                pop bc
                pop de
                pop hl
                ret

defb            "So who's perfect?"

GetInputName    ld de,MessQN
                call PrintString

                ld hl,HighScorer
                ld c,0

GINLoop         call FrontReadKey

                ld b,a
                cp 8
                jr z GINDel

                cp 13
                jr z GINEnt

                ld a,c
                cp 12
                jr z GINLoop

                ld a,b
                ld (hl),a
                inc hl
                inc c

                call PrintACC

                jr GINLoop

GINDel          ld a,c
                or a
                jr z GINLoop

                ld (hl),0
                dec c
                dec hl

                ld de,MessBS

                push hl
                push bc
                call PrintString
                pop bc
                pop hl

                jr GINLoop

GINEnt          ld a,c
                or a
                jr z GINSomeOne

                ld (hl),0
                ld hl,HighScorer
                ld b,c
                ld c,#20

GINEntL         ld a,(hl)
                cp c
                jr nz GINFini

                inc hl

                djnz GINEntL

GINSomeOne      ld de,HighScorer
                ld hl,MessSOne
                ld bc,12
                ldir

GINFini         ret

MessS2          defb 0
DecBuf          defb "100",0,0,0
cScore          defw 0
cNRings         defw 0
cNDTs           defw 0
cType           defw 0
Cursor          defw #1717
HighScore       defw 100
HighScorer      defb "Kickaha",0
                defw 0,0,0,0,0,0,0

TheLastOne      equ *

                org #6800
                include.b "chars.bin"

                include "m"