;************************************************
;* canAttack
;*
;* Input : X = attacker, Y = defender; current team and unit state
;* Output: carry set for a ready attack in this unit's distance band
;* Trash: a,flags,scratch,attack.x/y/savedY; x/y preserved
;************************************************

canAttack

        cpx	#NONE
        beq	?attackUnavailable

        lda	game.winner
        bne	?attackUnavailable

        lda	units.hp,x
        beq	?attackUnavailable

        lda	units.acted,x
        bne	?attackUnavailable

        lda	units.kind,x
        cmp	#unit_kind.HOWITZER
        bne	?attackWeaponReady

        ; Movement spends all MP; artillery must still be stationary.
        lda	units.mp,x
        beq	?attackUnavailable

?attackWeaponReady
        lda	units.team,x
        cmp	game.team
        bne	?attackUnavailable

        lda	units.hp,y
        beq	?attackUnavailable

        lda	units.team,y
        cmp	game.team
        beq	?attackUnavailable

        jsr	canHitClass

        bcc	?attackUnavailable

        jmp	inAttackRange

?attackUnavailable
        clc
        rts

;************************************************
;* inAttackRange
;*
;* Input : X = attacker, Y = defender
;* Output: carry = Manhattan distance within the attacker's min/max
;* Trash: a,flags,scratch,attack.x/y/savedY; x/y preserved
;************************************************

inAttackRange

        jsr	unitDistance

;************************************************
;* distanceInRange
;*
;* Input : A = distance; X = attacker
;* Output: carry = distance within the unit's minimum/maximum
;* Trash: a,flags,attack.savedY; x/y preserved
;************************************************

distanceInRange

        sty	attack.savedY
        ldy	units.kind,x
        cmp	minAttack,y
        bcc	?attackRangeMiss

        cmp	attackLimit,y
        bcs	?attackRangeMiss

        ldy	attack.savedY
        sec
        rts

?attackRangeMiss
        ldy	attack.savedY
        clc
        rts

;************************************************
;* attackDamage
;*
;* Input : X = attacker, Y = defender
;* Output: A = base damage + initiative - defender cover, minimum 1
;* Trash: a,flags,scratch,attack.savedY; x/y preserved
;************************************************

attackDamage

        sty	attack.savedY
        jsr	attackPower

        clc
        adc	#ATTACK_BONUS
        sta	scratch
        ldy	attack.savedY
        jsr	defenderCover

        sta	attack.cover
        lda	scratch
        sec
        sbc	attack.cover
        bcc	?attackMinimum

        bne	?attackDamageReady

?attackMinimum
        lda	#1

?attackDamageReady
        ldy	attack.savedY
        rts

;************************************************
;* buildTargets
;*
;* Input : game.selected/cursor and current unit state
;* Output: target flags/count; attack.cursor = hovered target or NONE
;* Trash: a,x,y,flags,scratch; attack scratch; game cursor unchanged
;************************************************

buildTargets

        lda	#0
        sta	attack.count
        sta	attack.unit
        ldx	#MAX_UNITS-1

?attackClear
        sta	attack.targets,x
        dex
        bpl	?attackClear

        lda	#NONE
        sta	attack.cursor

?attackScan
        ldx	game.selected
        ldy	attack.unit
        jsr	canAttack

        bcc	?attackNext

        lda	#1
        sta	attack.targets,y
        inc	attack.count
        lda	units.x,y
        cmp	game.cursorX
        bne	?attackNext

        lda	units.y,y
        cmp	game.cursorY
        bne	?attackNext

        sty	attack.cursor

?attackNext
        inc	attack.unit
        lda	attack.unit
        cmp	app.unitCount
        bne	?attackScan

        rts

;************************************************
;* focusAttackTarget
;*
;* Input : freshly built legal targets and current hovered target
;* Output: cursor stays on a valid enemy or jumps to the first one
;* Trash: a,x,flags; no unit action or movement is consumed
;************************************************

focusAttackTarget

        ldx	attack.cursor
        cpx	#NONE
        bne	?attackFocusCursor

        ldx	#0

?attackFocusScan
        lda	attack.targets,x
        bne	?attackFocusCursor

        inx
        cpx	app.unitCount
        bne	?attackFocusScan

?attackNoTargets
        lda	#message_id.NO_TARGET
        sta	game.message
        rts

?attackFocusCursor
        stx	attack.cursor
        lda	units.x,x
        sta	game.cursorX
        lda	units.y,x
        sta	game.cursorY
        rts

;************************************************
;* cycleAttackTarget
;*
;* Input : selected attacker; active-low input.direction
;* Output: up/left selects previous legal enemy, down/right next;
;*         wraps the roster and explicitly reports an empty target list
;* Trash: a,x,y,flags,scratch; attack scratch; no bitmap redraw
;************************************************

cycleAttackTarget

        lda	#1
        sta	attack.step
        lda	input.direction
        and	#5
        cmp	#5
        beq	?attackCycleBuild

        lda	#NONE
        sta	attack.step

?attackCycleBuild
        jsr	buildTargets

        lda	attack.count
        beq	?attackNoTargets

        lda	#0
        sta	game.message
        lda	attack.cursor
        cmp	#NONE
        bne	?attackCycleStart

        lda	#0
        ldx	attack.step
        cpx	#NONE
        beq	?attackCycleStart

        lda	app.unitCount
        sec
        sbc	#1

?attackCycleStart
        sta	attack.unit

?attackCycleNext
        lda	attack.unit
        clc
        adc	attack.step
        bmi	?attackCyclePreviousWrap

        cmp	app.unitCount
        bcc	?attackCycleCandidate

        lda	#0
        beq	?attackCycleCandidate

?attackCyclePreviousWrap
        lda	app.unitCount
        sec
        sbc	#1

?attackCycleCandidate
        sta	attack.unit
        tax
        lda	attack.targets,x
        beq	?attackCycleNext

        jmp	?attackFocusCursor
