;=================================================================
; [PSTATS.INC] -- Lives and HP handling for our Angel(a) ++
;=================================================================

; UNIVERSAL ROUTINE
WRITE_LIVES_REMAINING
    GOTOXY 1,8
    lda ang_lives
    sta a_byte
    dec a_byte
    PRINTNUM a_byte            ; print lives left
    rtl



; UNIVERSAL ROUTINE
WRITE_HP_REMAINING
    lda ang_hp
    cmp #$03
    beq WR_hpR_03hSYM
    bpl WR_hpR_03hSYM
    cmp #$02
    beq WR_hpR_02hSYM
    cmp #$01
    beq WR_hpR_01hSYM
    jmp WR_hpR_00h

WR_hpR_03hSYM
    jmp WR_hpR_03h
WR_hpR_02hSYM
    jmp WR_hpR_02h
WR_hpR_01hSYM
    jmp WR_hpR_01h


WR_hpR_00h
    GOTOXY 1,25
    PRINTS "abababq"
    GOTOXY 2,25
    PRINTS "cdcdcdq"
    jmp WR_hpR_09h
WR_hpR_01h
    GOTOXY 1,25
    PRINTS "efababq"
    GOTOXY 2,25
    PRINTS "ghcdcdq"
    jmp WR_hpR_09h
WR_hpR_02h
    GOTOXY 1,25
    PRINTS "efefabq"
    GOTOXY 2,25
    PRINTS "ghghcdq"
    jmp WR_hpR_09h
WR_hpR_03h
    GOTOXY 1,25
    PRINTS "efefefq"
    GOTOXY 2,25
    PRINTS "ghghghq"
WR_hpR_09h
    rtl



;================================



HP_DROP
    lda #V0SFX_EXPLOSION
    sta V0SFX
    jsr AUDIO_PLAY_V0SFX

    lda #$02                   ; select bg colour index #2
    sta $2121
    lda #%01111111
    sta $2122
    lda #%11100000
    sta $2122
    lda #$86                   ; select sprite index #6 (pal #0 of 7)
    sta $2121
    lda #%01111111
    sta $2122
    lda #%11100000
    sta $2122
    wai
    wai
    wai
    wai
    wai

    lda #$01
    sta shipInvulnerable
    lda #$0d;1a
    sta shipInvulnerable_w
    lda #$06
    sta shipInvulnerable_wd

    dec ang_hp
    lda ang_hp
    cmp #$03
    beq hpd_three
    bpl hpd_three
    cmp #$02
    beq hpd_two
    cmp #$01
    beq hpd_one
    jmp hpd_lostlife

hpd_three
    GOTOXY 1,25
    PRINTS "efefefq"
    GOTOXY 2,25
    PRINTS "ghghghq"
    jmp hpd_ende
hpd_two
    GOTOXY 1,25
    PRINTS "efefabq"
    GOTOXY 2,25
    PRINTS "ghghcdq"
    jmp hpd_ende
hpd_one
    GOTOXY 1,25
    PRINTS "efababq"
    GOTOXY 2,25
    PRINTS "ghcdcdq"
    jmp hpd_ende
hpd_ende
    lda #V1SFX_PING_HIGH
    sta V1SFX
    jsr AUDIO_PLAY_V1SFX

    LOAD_PAL BG_PAL
    wai
    wai
    wai

    rtl

;==============

hpd_gover
    lda areWeAtFirstBoss        ; don't end the game if we're
    cmp #$01                    ; fighting against the A520-STC64,
    beq hpd_symFFseqmovie       ; but play alternate sequence instead.

    lda #V1SFX_PING_HIGH
    sta V1SFX
    jsr AUDIO_PLAY_V1SFX

    jsr hpd_fo_mosaic
    jmp GAME_OVER


hpd_symFFseqmovie
    jml S1B_ENDSEQ_n2

;==============

hpd_lostlife
    GOTOXY 1,25
    PRINTS "abababq"
    GOTOXY 2,25
    PRINTS "cdcdcdq"

    lda #$00                    ; activate angela sprite
    sta SPRBUF2+3

    dec ang_lives
    lda ang_lives
    cmp #$00                   ; game over yet?
    beq hpd_gover

    jsr hpd_fo_mosaic          ; nope! so carry on 'ere, chap!

;   phx
;   ldx ang_startx
;   stx PLY_SHIPX
;   stx SPRBUF1+SX12
;   ldx ang_starty
;   stx PLY_SHIPY
;   stx SPRBUF1+SY12
;   plx

    lda lostLife_forceNoFLIP
    cmp #$01
    beq HPD_LL_sDOne

    lda playerDir
    cmp #$00
    beq HPD_LL_sleft
    lda #$30
    sta SPRBUF1+SPRIORITY12
    jmp HPD_LL_sDOne
HPD_LL_sleft    
    lda #%1100000
    sta SPRBUF1+SPRIORITY12
HPD_LL_sDOne
    lda #$03
    sta ang_hp

    GOTOXY 1,25
    PRINTS "efefefq"
    GOTOXY 2,25
    PRINTS "ghghghq"

    GOTOXY 1,8

    lda ang_lives
    sta a_byte
    dec a_byte

    PRINTNUM a_byte            ; print lives left

    lda #$00
    sta CURFRAME12             ; switch back to main ship frame

    stz shipAnim
    stz shipVel
    stz shipAdvNumPix
    stz shipAdvHalfFlag
    stz dec_vel_dir

    jsr hpd_fi_mosaic

    rtl

;==============

hpd_fo_mosaic                  ; same as the FADE_OUT_MOSAIC macro,
    LOAD_PAL BG_PAL            ; but affects only bg#1
    wai
    wai
    wai
    pha

    lda #$C0
    sta CURFRAME12

    lda #%00001111  ; F
    sta $2100
    lda #%00000001  ; 0
    sta $2106
    jsr hpd_fade_delay
    lda #%00001110  ; E
    sta $2100
    lda #%00010001  ; 1
    sta $2106

    lda #%00110000
    sta SPRBUF1+SPRIORITY12
    wai

    jsr hpd_sfx_01
    jsr hpd_fade_delay
    lda #%00001101  ; D
    sta $2100
    lda #%00100001  ; 2
    sta $2106
    jsr hpd_fade_delay
    lda #%00001100  ; C
    sta $2100
    lda #%00110001  ; 3
    sta $2106

    lda #%10110000
    sta SPRBUF1+SPRIORITY12
    wai

    jsr hpd_sfx_00
    jsr hpd_fade_delay
    lda #%00001111  ; B
    sta $2100
    lda #%00000001  ; 4
    sta $2106
    jsr hpd_fade_delay
    lda #%00001010  ; A
    sta $2100
    lda #%01010001  ; 5
    sta $2106

    lda #%11110000
    sta SPRBUF1+SPRIORITY12
    wai

    jsr hpd_sfx_00
    jsr hpd_fade_delay
    lda #%00001001  ; 9
    sta $2100
    lda #%01100001  ; 6
    sta $2106
    jsr hpd_fade_delay
    lda #%00001000  ; 8
    sta $2100
    lda #%01110001  ; 7
    sta $2106

    lda #%01110000
    sta SPRBUF1+SPRIORITY12
    wai

    jsr hpd_sfx_01
    jsr hpd_fade_delay
    lda #%00000111  ; 7
    sta $2100
    lda #%10000001  ; 8
    sta $2106
    jsr hpd_fade_delay
    lda #%00000110  ; 6
    sta $2100       ; + -> 69 *yay*
    lda #%10010001  ; 9
    sta $2106

    lda #%00110000
    sta SPRBUF1+SPRIORITY12
    wai
    lda #$C4
    sta CURFRAME12
    wai

    jsr hpd_sfx_00
    jsr hpd_fade_delay
    lda #%00000101  ; 5
    sta $2100
    lda #%10100001  ; A
    sta $2106
    jsr hpd_fade_delay
    lda #%00000100  ; 4
    sta $2100
    lda #%10110001  ; B
    sta $2106

    lda #$C8
    sta CURFRAME12
    wai

    jsr hpd_sfx_01
    jsr hpd_fade_delay
    lda #%00000011  ; 3
    sta $2100
    lda #%11000001  ; C
    sta $2106
    jsr hpd_fade_delay
    lda #%00000010  ; 2
    sta $2100
    lda #%11010001  ; D
    sta $2106

    jsr hpd_sfx_00
    jsr hpd_fade_delay
    lda #%00000001  ; 1
    sta $2100
    lda #%11100001  ; E
    sta $2106
    jsr hpd_fade_delay
    lda #%00000000  ; 0
    sta $2100
    lda #%11110001  ; F
    sta $2106

    jsr hpd_sfx_01
    jsr hpd_fade_delay

    lda #$8C
    sta CURFRAME12
    wai

    pla
    rts

hpd_fade_delay
    phx
    ldx #$02
-   wai
    dex
    cpx #$00
    bpl -
    plx
    rts

hpd_sfx_00
    lda #V0SFX_EXPLOSION
    sta V0SFX
    jsr AUDIO_PLAY_V0SFX
    rts
hpd_sfx_01
    lda #V0SFX_GUN_FIRE
    sta V0SFX
    jsr AUDIO_PLAY_V0SFX
    rts

hpd_fi_mosaic
    pha
    lda #%00000000  ; 0
    sta $2100
    lda #%11110001  ; F
    sta $2106
    jsr hpd_fade_delay
    lda #%00000001  ; 1
    sta $2100
    lda #%11100001  ; E
    sta $2106
    jsr hpd_fade_delay
    lda #%00000010  ; 2
    sta $2100
    lda #%11010001  ; D
    sta $2106
    jsr hpd_fade_delay
    lda #%00000011  ; 3
    sta $2100
    lda #%11000001  ; C
    sta $2106
    jsr hpd_fade_delay
    lda #%00000100  ; 4
    sta $2100
    lda #%10110001  ; B
    sta $2106
    jsr hpd_fade_delay
    lda #%00000101  ; 5
    sta $2100
    lda #%10100001  ; A
    sta $2106
    jsr hpd_fade_delay
    lda #%00000110  ; 6
    sta $2100       ; + -> 69 *yay*
    lda #%10010001  ; 9
    sta $2106
    jsr hpd_fade_delay
    lda #%00000111  ; 7
    sta $2100
    lda #%10000001  ; 8
    sta $2106
    jsr hpd_fade_delay
    lda #%00001000  ; 8
    sta $2100
    lda #%01110001  ; 7
    sta $2106
    jsr hpd_fade_delay
    lda #%00001001  ; 9
    sta $2100
    lda #%01100001  ; 6
    sta $2106
    jsr hpd_fade_delay
    lda #%00001010  ; A
    sta $2100
    lda #%01010001  ; 5
    sta $2106
    jsr hpd_fade_delay
    lda #%00001111  ; B
    sta $2100
    lda #%00000001  ; 4
    sta $2106
    jsr hpd_fade_delay
    lda #%00001100  ; C
    sta $2100
    lda #%00110001  ; 3
    sta $2106
    jsr hpd_fade_delay
    lda #%00001101  ; D
    sta $2100
    lda #%00100001  ; 2
    sta $2106
    jsr hpd_fade_delay
    lda #%00001110  ; E
    sta $2100
    lda #%00010001  ; 1
    sta $2106
    jsr hpd_fade_delay
    lda #%00001111  ; F
    sta $2100
    lda #%00000001  ; 0
    sta $2106
    jsr hpd_fade_delay
    pla
    rts

;================================


INVULNERABILITY_COUNTDOWN
    lda shipInvulnerable_w
    cmp #$04
    beq ICD_disableShip
    cmp #$06
    beq ICD_disableShip
    cmp #$08
    beq ICD_disableShip
    cmp #$0a
    beq ICD_disableShip
    cmp #$0c
    beq ICD_disableShip
    cmp #$0e
    beq ICD_disableShip
    cmp #$10
    beq ICD_disableShip
    cmp #$12
    beq ICD_disableShip
    cmp #$14
    beq ICD_disableShip
    cmp #$16
    beq ICD_disableShip
    cmp #$18
    beq ICD_disableShip
    cmp #$1a
    beq ICD_disableShip

    lda #$00                    ; activate angela sprite
    sta SPRBUF2+3
    jmp ICD_debbieDoesDallas
ICD_disableShip
    lda #$03                    ; deactivate angela sprite
    sta SPRBUF2+3
ICD_debbieDoesDallas

    dec shipInvulnerable_wd
    lda shipInvulnerable_wd
    cmp #$01
    bmi ICD_dec_w
    jmp ICD_ende
ICD_dec_w
    lda #$0a
    sta shipInvulnerable_wd
    dec shipInvulnerable_w
    lda shipInvulnerable_w
    cmp #$02
    bmi ICD_GodMOdeOFF
    jmp ICD_ende
ICD_GodMOdeOFF
    lda #$00                    ; activate angela sprite
    sta SPRBUF2+3
    stz shipInvulnerable
    stz shipInvulnerable_w
    stz shipInvulnerable_wd
ICD_ende
    rtl
