;************************************************
;* beginBattle
;*
;* Input : game.selected/target before damage is applied
;* Output: participant snapshots and scaled unit colors for the replay
;* Trash: a,x,y,flags; battle state and raster color tables
;*
;* game.s commits damage once; this scene only replays its result.
;************************************************

beginBattle

        jsr	stopSound

        lda	#1
        sta	battle.active
        lda	#0
        sta	battle.phase
        sta	battle.shot
        lda	#NONE
        sta	battle.lastShot
        lda	battleDurations
        sta	battle.timer
        lda	game.selected
        sta	battle.attacker
        tax
        lda	units.hp,x
        sta	battle.attackerHP
        ldx	game.target
        stx	battle.defender
        lda	units.hp,x
        sta	battle.beforeHP
        sta	battle.displayHP
        ldx	#RASTER_BANDS-1

?battlePalette
        lda	#MAP_ORANGE
        sta	rasterOrange,x
        lda	#MAP_BLUE
        sta	rasterBlue,x
        dex
        bpl	?battlePalette

; Scale the four source color bands to scanlines 64..111, three DLIs each.
        ldx	#16
        ldy	#0

?battleUnitPalette
        lda	rasterOrangeBands,y
        sta	rasterOrange,x
        sta	rasterOrange+1,x
        sta	rasterOrange+2,x
        lda	rasterBlueBands,y
        sta	rasterBlue,x
        sta	rasterBlue+1,x
        sta	rasterBlue+2,x
        inx
        inx
        inx
        iny
        cpy	#4
        bne	?battleUnitPalette

        rts

;************************************************
;* battleTick
;*
;* Input : battle phase/timer/result and help.active
;* Output: advances animation/energy display unless HELP pauses it
;* Trash: a,x,y,flags; battle and renderer scratch
;************************************************

battleTick

        lda	app.screen
        beq	?battleInGame

        rts

?battleInGame
        lda	help.active
        beq	?battleNotInHelp

        rts

?battleNotInHelp
        lda	battle.active
        bne	?battleRunning

        rts

?battleRunning
        dec	battle.timer
        bne	?battleAnimate

        inc	battle.phase
        ldx	battle.phase
        cpx	#BATTLE_PHASES
        beq	endBattle

        lda	battleDurations,x
        sta	battle.timer
        cpx	#3
        bne	?battlePhaseReady

        lda	battle.afterHP
        sta	battle.displayHP

?battlePhaseReady
        cpx	#1
        bne	?battleFrameReady

        ldx	battle.attacker
        jsr	soundAttack

?battleFrameReady
        jmp	battlePhaseFrame

?battleAnimate
        lda	battle.phase
        cmp	#1
        bne	?battleHealth

        lda	battle.timer
        cmp	#24
        beq	?battleNextShot

        cmp	#18
        beq	?battleNextShot

        cmp	#12
        beq	?battleNextShot

        cmp	#6
        bne	?battleTickDone

?battleNextShot
        inc	battle.shot
        jmp	battleShotFrame

?battleHealth
        cmp	#2
        bne	?battleTickDone

        lda	battle.timer
        and	#3
        bne	?battleTickDone

        lda	battle.displayHP
        cmp	battle.afterHP
        beq	?battleTickDone

        dec	battle.displayHP
        jsr	battleBars

        jmp	updateBattleHud

?battleTickDone
        rts

;************************************************
;* endBattle
;*
;* Input : committed battle result and current input levels
;* Output: map restored; a newly won battle starts its victory tune
;* Trash: a,x,y,flags; renderer zero page and battle.active
;************************************************

endBattle

        jsr	stopSound

        lda	battle.active
        beq	?battleRestoreMap

        lda	game.winner
        beq	?battleRestoreMap

        jsr	soundVictory

?battleRestoreMap
        jsr	resetRaster

        lda	#0
        sta	battle.active
        lda	STRIG0
        sta	input.fireOld
        lda	CONSOL
        and	#5
        sta	input.consoleOld
        lda	#NONE
        sta	CH
        jmp	render

;************************************************
;* renderBattle
;*
;* Input : battle participants, phase and displayed energy
;* Output: complete combat bitmap, energy bars and battle HUD
;* Trash: a,x,y,flags,dst,src,pmWrite; draw/battle/gfx scratch
;************************************************

renderBattle

        lda	#<frameBuffer
        sta	dst
        lda	#>frameBuffer
        sta	dst+1
        lda	#25
        sta	draw.lines
        lda	#0
        ldy	#0

?battleClear
        sta	(dst),y
        iny
        bne	?battleClear

        inc	dst+1
        dec	draw.lines
        bne	?battleClear

        lda	#<(frameBuffer+112*40)
        sta	dst
        lda	#>(frameBuffer+112*40)
        sta	dst+1
        lda	#48
        sta	draw.lines

?battleGround
        ldy	#39
        lda	#$55

?battleGroundByte
        sta	(dst),y
        dey
        bpl	?battleGroundByte

        jsr	advanceRow

        dec	draw.lines
        bne	?battleGround

        lda	#0
        sta	battle.side
        ldx	battle.attacker
        jsr	battleUnit
        jsr	battleDefender

        lda	battle.phase
        cmp	#1
        bne	?battleSceneImpact

        jsr	battleProjectile

        jmp	?battleSceneReady

?battleSceneImpact
        cmp	#2
        bne	?battleSceneReady

        jsr	battleExplosion

?battleSceneReady
        jsr	battleBars
        jsr	buildSprites

        jmp	updateBattleHud

;************************************************
;* battlePhaseFrame
;*
;* Input : new battle.phase and current committed result
;* Output: redraws only changed effects/defender/bar/HUD rectangles
;* Trash: a,x,y,flags,dst,src,scratch; draw/battle/menu scratch
;************************************************

battlePhaseFrame

        lda	battle.phase
        cmp	#1
        beq	battleShotFrame

        cmp	#2
        beq	battleImpactFrame

        jsr	battleDefender
        jsr	battleBars

        jmp	updateBattleHud

;************************************************
;* battleShotFrame
;*
;* Input : battle.shot and previous projectile position
;* Output: erases the old projectile, draws the new one and HUD
;* Trash: a,x,y,flags,dst,src,scratch; draw/battle/menu scratch
;************************************************

battleShotFrame

        jsr	battleEraseShot
        jsr	battleProjectile

        jmp	updateBattleHud

;************************************************
;* battleImpactFrame
;*
;* Input : last projectile and defender location in the combat scene
;* Output: projectile removed; impact flash and HUD displayed
;* Trash: a,x,y,flags,dst,src,scratch; draw/battle/menu scratch
;************************************************

battleImpactFrame

        jsr	battleEraseShot
        jsr	battleExplosion

        jmp	updateBattleHud

;************************************************
;* battleProjectile
;*
;* Input : battle.shot = projectile position table index
;* Output: projectile rectangle drawn and lastShot recorded
;* Trash: a,x,y,flags,dst; draw.lines/overlay
;************************************************

battleProjectile

        ldx	battle.shot
        stx	battle.lastShot
        jsr	battleShotAddress

        lda	#$aa
        sta	draw.overlay
        lda	#3
        sta	draw.lines
        jmp	battleFlash

;************************************************
;* battleEraseShot
;*
;* Input : battle.lastShot = previous position or NONE
;* Output: old projectile rectangle cleared; lastShot = NONE
;* Trash: a,x,y,flags,dst; draw.lines/overlay
;************************************************

battleEraseShot

        ldx	battle.lastShot
        cpx	#NONE
        beq	?battleEraseDone

        jsr	battleShotAddress

        lda	#0
        sta	draw.overlay
        lda	#3
        sta	draw.lines
        jsr	battleFlash

        lda	#NONE
        sta	battle.lastShot

?battleEraseDone
        rts

;************************************************
;* battleShotAddress
;*
;* Input : X = projectile position table index
;* Output: dst = framebuffer address at projectile scanline
;* Trash: a,flags,dst; x/y preserved
;************************************************

battleShotAddress

        ldy	battle.attacker
        lda	units.kind,y
        cmp	#unit_kind.HOWITZER
        bne	?battleFlatShot

        lda	battleArcLow,x
        sta	dst
        lda	battleArcHigh,x
        sta	dst+1
        rts

?battleFlatShot
        lda	battleShots,x
        clc
        adc	#<(frameBuffer+84*40)
        sta	dst
        lda	#>(frameBuffer+84*40)
        adc	#0
        sta	dst+1
        rts

;************************************************
;* battleExplosion
;*
;* Input : combat bitmap with defender on the right
;* Output: warm-colored impact flash over the defender
;* Trash: a,y,flags,dst; draw.lines/overlay; x preserved
;************************************************

battleExplosion

        lda	#<(frameBuffer+72*40+29)
        sta	dst
        lda	#>(frameBuffer+72*40+29)
        sta	dst+1
        lda	#30
        sta	draw.lines
        lda	#$aa
        sta	draw.overlay

;************************************************
;* battleFlash
;*
;* Input : dst, draw.lines and draw.overlay = fill byte
;* Output: two-byte-wide rectangle filled for the requested height
;* Trash: a,y,flags,dst,draw.lines; x preserved
;************************************************

battleFlash

        ldy	#1
        lda	draw.overlay
        sta	(dst),y
        dey
        sta	(dst),y
        jsr	advanceRow

        dec	draw.lines
        bne	battleFlash

        rts

;************************************************
;* battleDefender
;*
;* Input : battle.defender, phase and afterHP
;* Output: defender drawn, or its rectangle cleared after destruction
;* Trash: a,x,y,flags,dst,src; draw/battle scratch
;************************************************

battleDefender

        lda	#1
        sta	battle.side
        lda	battle.phase
        cmp	#3
        bne	?battleDefenderAlive

        lda	battle.afterHP
        beq	?battleDefenderGone

?battleDefenderAlive
        ldx	battle.defender
        jmp	battleUnit

?battleDefenderGone
        lda	#<(frameBuffer+64*40+27)
        sta	dst
        lda	#>(frameBuffer+64*40+27)
        sta	dst+1
        lda	#48
        sta	draw.lines

?battleClearDefender
        ldy	#7
        lda	#0

?battleClearDefenderByte
        sta	(dst),y
        dey
        bpl	?battleClearDefenderByte

        jsr	advanceRow

        dec	draw.lines
        bne	?battleClearDefender

        rts

;************************************************
;* battleUnit
;*
;* Input : X = unit index; battle.side = left/right
;* Output: enlarged unit drawn facing its opponent
;* Trash: a,x,y,flags,dst,src; draw.lines,battle.repeat
;*
;* Expand shared map art at draw time; do not keep enlarged duplicates in RAM.
;************************************************

battleUnit

        jsr	unitPicture
        jsr	pictureTile

        tax
        lda	tileLow,x
        sta	src
        lda	tileHigh,x
        sta	src+1
        lda	#<(frameBuffer+64*40+5)
        ldx	battle.side
        beq	?battleUnitPosition

        lda	#<(frameBuffer+64*40+27)

?battleUnitPosition
        sta	dst
        lda	#>(frameBuffer+64*40+5)
        sta	dst+1
        lda	#16
        sta	draw.lines

?battleUnitRow
        lda	#0
        sta	battle.columns
        ldy	#0
        lda	(src),y
        jsr	battleExpandByte

        ldy	#1
        lda	(src),y
        jsr	battleExpandByte

        lda	#3
        sta	battle.repeat

?battleUnitRepeat
        ldy	#7

?battleUnitByte
        lda	battle.row,y
        sta	(dst),y
        dey
        bpl	?battleUnitByte

        jsr	advanceRow

        dec	battle.repeat
        bne	?battleUnitRepeat

        clc
        lda	src
        adc	#2
        sta	src
        bcc	?battleUnitNext

        inc	src+1

?battleUnitNext
        dec	draw.lines
        bne	?battleUnitRow

        rts

battleExpandByte

        sta	battle.packed

?battleExpandPixel
        lda	battle.columns
        ldx	battle.side
        beq	?battlePixelFacing

        eor	#7

?battlePixelFacing
        tax
        lda	battle.packed
        lsr
        lsr
        lsr
        lsr
        lsr
        lsr
        tay
        lda	battlePixelBytes,y
        sta	battle.row,x
        asl	battle.packed
        asl	battle.packed
        inc	battle.columns
        lda	battle.columns
        and	#3
        bne	?battleExpandPixel

        rts

;************************************************
;* battleBars
;*
;* Input : attackerHP/displayHP and participants' teams
;* Output: both colored ten-segment energy bars refreshed
;* Trash: a,x,y,flags,dst; draw/battle scratch
;************************************************

battleBars

        lda	#0
        sta	battle.side
        lda	#<(frameBuffer+128*40+4)
        sta	dst
        lda	#>(frameBuffer+128*40+4)
        sta	dst+1
        ldx	battle.attacker
        lda	battle.attackerHP
        jsr	battleBar

        inc	battle.side
        lda	#<(frameBuffer+128*40+26)
        sta	dst
        lda	#>(frameBuffer+128*40+26)
        sta	dst+1
        ldx	battle.defender
        lda	battle.displayHP

;************************************************
;* battleBar
;*
;* Input : A = energy 0..10, X = unit index, dst = bar origin
;* Output: ten-byte-wide, four-scanline energy bar drawn
;* Trash: a,y,flags,dst; draw.lines/overlay,battle.columns
;************************************************

battleBar

        sta	battle.columns
        lda	units.team,x
        beq	?battleOrangeBar

        lda	#$ff
        bne	?battleBarColor

?battleOrangeBar
        lda	#$aa

?battleBarColor
        sta	draw.overlay
        lda	#4
        sta	draw.lines

?battleBarRow
        ldy	#0

?battleBarByte
        lda	#0
        cpy	battle.columns
        bcs	?battleBarStore

        lda	draw.overlay

?battleBarStore
        sta	(dst),y
        iny
        cpy	#10
        bne	?battleBarByte

        jsr	advanceRow

        dec	draw.lines
        bne	?battleBarRow

        rts

;************************************************
;* updateBattleHud
;*
;* Input : battle participants, terrain, weapon values, HP and phase
;* Output: five HUD rows with teams, HP, weapons, cover and result
;* Trash: a,x,y,flags,src,scratch; menu/battle scratch
;************************************************

updateBattleHud

        ldx	#199

?battleCopyHud
        lda	battleTemplate,x
        sta	hud,x
        dex
        cpx	#NONE
        bne	?battleCopyHud

        ldx	battle.attacker
        ldy	#BHUD_ATTACKER
        jsr	battleName

        ldx	battle.defender
        ldy	#BHUD_DEFENDER
        jsr	battleName

        lda	battle.attackerHP
        ldx	#BHUD_ATTACK_HP
        jsr	menuDecimal

        lda	battle.displayHP
        ldx	#BHUD_DEFEND_HP
        jsr	menuDecimal

        lda	battle.damage
        ldx	#BHUD_DAMAGE
        jsr	menuDecimal

        lda	battle.beforeHP
        ldx	#BHUD_BEFORE
        jsr	menuDecimal

        lda	battle.afterHP
        ldx	#BHUD_AFTER
        jsr	menuDecimal

        ldx	battle.attacker
        ldy	#BHUD_ATTACK_TEAM
        jsr	battleTeam

        ldx	battle.defender
        ldy	#BHUD_DEFEND_TEAM
        jsr	battleTeam

        ldx	battle.attacker
        ldy	#BHUD_ATTACK_WEAPON
        jsr	battleStats

        ldx	battle.defender
        ldy	#BHUD_DEFEND_WEAPON
        jsr	battleStats

        lda	#ATTACK_BONUS+16
        sta	hud+BHUD_BONUS
        lda	#<battleMessages
        sta	src
        lda	#>battleMessages
        sta	src+1
        ldx	battle.phase

?battleMessageOffset
        cpx	#0
        beq	?battleMessageReady

        clc
        lda	src
        adc	#40
        sta	src
        bcc	?battleMessageNext

        inc	src+1

?battleMessageNext
        dex
        jmp	?battleMessageOffset

?battleMessageReady
        lda	battle.phase
        cmp	#3
        bne	?battleCopyMessage

        lda	battle.afterHP
        bne	?battleCopyMessage

        lda	#<battleDestroyed
        sta	src
        lda	#>battleDestroyed
        sta	src+1

?battleCopyMessage
        ldy	#39

?battleMessageByte
        lda	(src),y
        sta	hud+160,y
        dey
        bpl	?battleMessageByte

        jmp	endHudUpdate

;************************************************
;* battleStats
;*
;* Input : X = participant, Y = its weapon field in the HUD
;* Output: base weapon value and cover at the unit's own position
;* Trash: a,y,flags,scratch; x preserved
;************************************************

battleStats

        sty	scratch
        ldy	battle.defender
        cpx	battle.attacker
        beq	?battleStatsOpponent

        ldy	battle.attacker

?battleStatsOpponent
        jsr	attackPower

        clc
        adc	#16
        ldy	scratch
        sta	hud,y
        txa
        tay
        jsr	defenderCover

        clc
        adc	#16
        ldy	scratch
        sta	hud+BHUD_ATTACK_COVER-BHUD_ATTACK_WEAPON,y
        rts

;************************************************
;* battleName
;*
;* Input : X = unit index, Y = destination HUD offset
;* Output: eight-character unit name copied into the HUD
;* Trash: a,x,y,flags,battle.columns
;************************************************

battleName

        lda	units.kind,x
        asl
        asl
        asl
        tax
        lda	#8
        sta	battle.columns

?battleNameByte
        lda	unitNames,x
        sta	hud,y
        inx
        iny
        dec	battle.columns
        bne	?battleNameByte

        rts

;************************************************
;* battleTeam
;*
;* Input : X = unit index, Y = destination HUD offset
;* Output: six-character team name copied into the HUD
;* Trash: a,x,y,flags,battle.columns
;************************************************

battleTeam

        lda	units.team,x
        beq	?battleTeamOrange

        ldx	#6
        bne	?battleTeamStart

?battleTeamOrange
        ldx	#0

?battleTeamStart
        lda	#6
        sta	battle.columns

?battleTeamByte
        lda	battleTeamNames,x
        sta	hud,y
        inx
        iny
        dec	battle.columns
        bne	?battleTeamByte

        rts
