; Fast reload on death
; by total 2019 (total@viskos.org)
; Compile with "asar" (https://github.com/RPGHacker/asar/releases)

!QUICK_RELOAD = $1f60

lorom

; Hook Death Game event (13h)
org $82DC80
    php
    rep #$30
    lda #$0001
    sta !QUICK_RELOAD ; Currently "quick reloading"
    jsl $82be17       ; Stop sounds/ for the beeping
    lda $0952         ; Load saveslot
    jsl $818085       ; Load savefile
    lda #$0006        
    sta $0998         ; Goto game mode 6 (load game)
    plp
    rts

; Hook setting up game
org $80a088
    jsl setup_music : nop : nop

org $80A095
    jml setup_game_1

org $80a0ce
    jml setup_game_2

org $80a113
    jml setup_game_3

org $91e164
    jsl setup_samus : nop : nop

; Free space somewhere for hooked code
org $a0fe00
setup_music:
    lda !QUICK_RELOAD
    bne .quick
    stz $07f3
    stz $07f5
.quick
    rtl

setup_game_1:
	jsl $82be17       ; Stop sounds/ for the beeping
    lda !QUICK_RELOAD
    bne .quick
    lda #$ffff      ; Do regular things
    sta $05f5
    jml $80a09b
.quick
    jsl $80835d
    jsl $80985f
    jsl $82e76b
    jml $80a0aa

setup_game_2:
    jsl $82be17       ; Stop sounds/ for the beeping
    lda !QUICK_RELOAD
    bne .quick
    jsl $82e071
    jml $80a0d2
.quick
    jml $80a0d5

setup_game_3:
    jsl $82be17       ; Stop sounds/ for the beeping
    pha
    lda !QUICK_RELOAD
    bne .quick
    pla
    jsl $80982a
    jml $80a117
.quick
    pla
    jsl $80982a
    stz !QUICK_RELOAD
    lda $07c9
    cmp $07f5
    bne .loadmusic
    lda $07cb
    cmp $07f3
    bne .loadmusic
    jml $80a122

.loadmusic
    lda $07c9
    sta $07f5
    lda $07cb
    sta $07f3    

    lda $07cb
    ora #$ff00
    jsl $808fc1
    lda $07c9
    jsl $808fc1

    jml $80a122

setup_samus:
    lda !QUICK_RELOAD
    beq .normal
    lda #$e695
    sta $0a42
    lda #$e725
    sta $0a44
.normal    
    lda $09c2
    sta $0a12
    rtl