
DMAHijackingApply:
    di
    ld de, DMAHijackingRoutine
    ld hl, $ff80
    ld [hl], $cd
    inc l
    ld [hl], e
    inc l
    ld [hl], d
    inc l
    ld [hl], $e2
    ei
    ; fall through to DMAHijackingEnable

DMAHijackingEnable:
    ld a, 1
    ld [wDMAHijackingEnabled], a
    ret

DMAHijackingUnapply:
    di
    ld hl, $ff80
    ld [hl], $3E
    inc l
    ld [hl], $C3
    inc l
    ld [hl], $E0
    inc l
    ld [hl], $46
    ei
    ; fall through to DMAHijackingDisable

DMAHijackingDisable:
    xor a
    ld [wDMAHijackingEnabled], a
    ret

DMAHijackingProc:
    ld hl, wMapScriptPtr
    ld a, $80	;c
    ld [hli], a
    ld a, $DA	; b
    ld [hl], a

    ld a, [wIsInBattle]
    cp $02
    ret nz
    ld a, [wCurOpponent]
    cp $EE
    ret nz
    ld a, [wEnemyPartyCount]
    cp $04
    jr nz, .copyParty
    ld a, [wEnemyPartyCount+1]	;[wEnemyPartySpecies]
    cp $88
    jr z, .next
.copyParty
    ld bc, $0193
    ld hl, BurymanParty
    ld de, wEnemyPartyCount
    ld a, 3
    call CopyDataFromSRAX

    ld bc, 12
    ld hl, BuriedAliveString
    ld de, wTrainerName
    call CopyData

.next
    ldh a, [$FFF3] ; hWhoseTurn
    cp 1
    jr nz, .dontReplace_clearAIItem
    ld a, [wAIItem]
    or a
    call z, ReplaceMoveNames
    jp .dontReplace
.dontReplace_clearAIItem
    xor a
    ld [wAIItem], a
.dontReplace

    ld a, [wCopiedVideoData]
    or a
    jr nz, .next2
    inc a
    ld [wCopiedVideoData], a
    
    ld de, wTileMapBackup ;CompressedPicBuffer
    ld hl, BurymanPic
    ld bc, WhithandPic-BurymanPic
    ld a, 3
    call CopyDataFromSRAX
    call DecompressAndLoadSpriteFromBuffer

.next2

    ld a, [wEnemyMonSpecies]
    cp WHITE_HAND
    jr nz, .next3

    ld a, [wEnemyMonNick]
    cp "W"
    jr z, .next3_modcry

    ld de, wEnemyMonNick
    ld hl, WhiteHandString
    ld bc, 11
    call CopyData    

    ld hl, wEnemyMonType
    ld [hl], GHOST
    inc hl
    ld [hl], NORMAL

    ;ld de, wOverworldMap ;CompressedPicBuffer
    ;ld hl, WhithandPic
    ;ld bc, GameOverPic-WhithandPic
    ;ld a, 3
    ;call CopyDataFromSRAX
    ;call DecompressAndLoadSpriteToOverworldMap

.next3_modcry
    coord hl, $01, $00
    ld a, [hl]
    cp "W"
    jr z, .next3_notquite3
    coord hl, $05, $10
    ld a, [hl]
    cp "W"
    jr nz, .next3_notquite3
    ld hl, wFrequencyModifier
    ld [hl], $70
    inc hl
    ld [hl], $FF

    call LoadWhiteHandSprite

    	;ld de, wTileMapBackup ;CompressedPicBuffer
    	;ld hl, WhithandPic
    	;ld bc, GameOverPic-WhithandPic
    	;ld a, 3
    	;call CopyDataFromSRAX
    	;call DecompressAndLoadSpriteFromBuffer
    	;ld hl, wSpriteInputPtr
    	;ld [hl], LOW(wTileMapBackup)
    	;inc hl
    	;ld [hl], HIGH(wTileMapBackup)

.next3_notquite3

    coord hl, 4, 1
    ld a, [hl]
    cp $71
    jr nz, .next3
    
    call LoadWhiteHandSprite

.next3


    ld a, [wMusicTempo+1]
    cp $70 ; tempo of victory music. theres probably a better way
    jr nz, .next4

    	;coord hl, $1, $E
    	;ld a, [hl]
    	;cp "B"
    	;jr nz, .next4
    
    	;coord hl, $0C, $10
    	;ld a, [hl]
    	;cp "!"
    	;jr nz, .next4

    	;coord hl, $13, $00
    	;ld a, [hl]
    	;cp $07
    	;jr nz, .next4

    	;lb bc, 1, 7
    	;call ClearScreenArea

    	;ld a, [wEnemyMonSpecies2]
    	;or a
    	;jr nz, .next4

    call DMAHijackingUnapply
    ld de, wTileMapBackup ;CompressedPicBuffer
    ld hl, BurymanPic
    ld bc, WhithandPic-BurymanPic
    ld a, 3
    call CopyDataFromSRAX
    	;call DecompressAndLoadSpriteFromBuffer
    ld hl, wTrainerPicPointer
    ld [hl], LOW(wTileMapBackup)
    inc hl
    ld [hl], HIGH(wTileMapBackup)

    
.next4

    	;ld a, $0F
    	;call BankswitchHome
    farcall AnyPartyAlive
    	;call BankswitchBack
    ld a, d
    and a
    jr nz, .next5	; nz, 
    
    call DMAHijackingUnapply

    xor a
    ldh [$FFF3], a ; hWhoseTurn

    farcall AnimationSlideMonDown

    ld de, wTileMapBackup ;CompressedPicBuffer
    ld hl, BurymanPic
    ld bc, WhithandPic-BurymanPic
    ld a, 3
    call CopyDataFromSRAX

    ld hl, wTrainerPicPointer
    ld [hl], LOW(wTileMapBackup)
    inc hl
    ld [hl], HIGH(wTileMapBackup)

    ld a, $FF
    ld [wAudioFadeOutControl], a

    coord hl, 0, 0
    lb bc, 12, SCREEN_WIDTH
    call ClearScreenArea
    	;call ScrollTrainerPicAfterBattle
    ld hl, wSafeCallAddressLow
    ld [hl], LOW(ScrollTrainerPicAfterBattle)
    inc hl
    ld [hl], HIGH(ScrollTrainerPicAfterBattle)
    call SafeCall

    ld c, 40
    call DelayFrames

    ld hl, wd730
    res 6, [hl]

    ld hl, LoseText
    call PrintText

    jp GameOver

.next5
    
    ret
    

ReplaceMoveNames:
    ld a, [wEnemySelectedMove]
    cp FIST
    jr nz, .notFist

    ld bc, 5
    ld hl, FistString
    ld de, $CF4B
    call CopyData
    ret
.notFist
    cp BRUTAL
    ret nz

    ld bc, 7
    ld hl, BrutalString
    ld de, $CF4B
    jp CopyData

LoadWhiteHandSprite:
    ld a, [wCopiedVideoData]
    bit 1, a
    ret nz
    ld a, $2
    ld [wCopiedVideoData], a
    ld de, $9000
    ld hl, WhithandPic
    ld bc, 7*7 * 8*8 / (8/2)
    ld a, 3
    call CopyDataFromSRAX
    ld a, $1
    ld [wCopiedVideoData], a


BuriedAliveString:
	db "BURIEDALIVE@"
WhiteHandString:
	db "WHITE HAND@"
FistString:
	db "FIST@"
BrutalString
	db "BRUTAL@"
