;************************************************
;* resetProperties
;*
;* Input : freshly loaded terrain
;* Output: HQs owned by their armies; cities/factories neutral
;* Trash: a,x,flags
;************************************************

resetProperties

        ldx	#MAP_CELLS-1

?propertyResetCell
        lda	terrain,x
        cmp	#7
        beq	?propertyOrangeHQ

        cmp	#8
        beq	?propertyBlueHQ

        lda	#NONE
        bne	?propertyStoreOwner

?propertyOrangeHQ
        lda	#0
        beq	?propertyStoreOwner

?propertyBlueHQ
        lda	#1

?propertyStoreOwner
        sta	property.owner,x
        dex
        cpx	#NONE
        bne	?propertyResetCell

        rts

;************************************************
;* unitCell
;*
;* Input : X = unit index
;* Output: Y/A = linear cell occupied by the unit; x preserved
;* Trash: a,y,flags
;************************************************

unitCell

        lda	units.y,x
        asl
        asl
        asl
        asl
        clc
        adc	units.x,x
        tay
        rts

;************************************************
;* canCapture
;*
;* Input : X = unit; current team and property state
;* Output: carry = ready infantry on neutral/enemy property;
;*         Y = its cell on success; own HQs cannot be captured
;* Trash: a,y,flags; x preserved
;************************************************

canCapture

        cpx	#NONE
        beq	?propertyUnavailable

        lda	game.winner
        bne	?propertyUnavailable

        lda	units.hp,x
        beq	?propertyUnavailable

        lda	units.acted,x
        bne	?propertyUnavailable

        lda	units.team,x
        cmp	game.team
        bne	?propertyUnavailable

        lda	units.kind,x
        bne	?propertyUnavailable

        jsr	unitCell

        lda	terrain,y
        cmp	#AIRPORT
        beq	?propertyCheckOwner

        cmp	#6
        beq	?propertyCheckOwner

        cmp	#9
        beq	?propertyCheckOwner

        lda	game.team
        eor	#1
        clc
        adc	#7
        cmp	terrain,y
        bne	?propertyUnavailable

?propertyCheckOwner
        lda	property.owner,y
        cmp	game.team
        beq	?propertyUnavailable

        sec
        rts

?propertyUnavailable
        clc
        rts

;************************************************
;* captureBase
;*
;* Input : game.selected = infantry; cursor need not be on it
;* Output: city/factory changes owner; enemy HQ still wins;
;*         capture spends the action, but does not heal immediately
;* Trash: a,x,y,flags; game/property and renderer scratch
;************************************************

captureBase

        ldx	game.selected
        jsr	canCapture

        bcs	?propertyCaptured

        lda	#message_id.CAPTURE
        jmp	actionMessage

?propertyCaptured
        lda	game.team
        sta	property.owner,y
        lda	terrain,y
        cmp	#7
        beq	?propertyCapturedHQ

        cmp	#8
        bne	?propertyCaptureDone

?propertyCapturedHQ
        lda	game.team
        clc
        adc	#1
        sta	game.winner

?propertyCaptureDone
        jsr	soundCapture
        jsr	finishAction

        lda	#message_id.CAPTURED
        sta	game.message
        jmp	updateHud

;************************************************
;* healUnit
;*
;* Input : X = unit of the army whose turn is beginning
;* Output: living allied property occupant gains 2/10 HP, capped at 10
;* Trash: a,y,flags; x preserved; no resurrection or enemy repairs
;************************************************

healUnit

        lda	units.hp,x
        beq	?propertyHealDone

        cmp	#AIRPORT+1
        bcs	?propertyHealDone

        lda	units.team,x
        cmp	game.team
        bne	?propertyHealDone

        jsr	unitCell

        lda	units.kind,x
        cmp	#unit_kind.HELICOPTER
        bne	?propertyHealTerrain

        lda	terrain,y
        cmp	#AIRPORT
        bne	?propertyHealDone

?propertyHealTerrain
        lda	terrain,y
        cmp	#6
        bcc	?propertyHealDone

        cmp	#AIRPORT+1
        bcs	?propertyHealDone

        lda	property.owner,y
        cmp	game.team
        bne	?propertyHealDone

        lda	units.hp,x
        clc
        adc	#2
        cmp	#10
        bcc	?propertyHealed

        lda	#10

?propertyHealed
        sta	units.hp,x

?propertyHealDone
        rts

;************************************************
;* propertyAddress
;*
;* Input : draw.mapIndex
;* Output: carry = owned building; dst/tileX/tileY and scratch = owner
;* Trash: a,x,flags,dst,scratch; y preserved
;************************************************

propertyAddress

        ldx	draw.mapIndex
        lda	property.owner,x
        cmp	#2
        bcs	?propertyNoAddress

        sta	scratch
        lda	terrain,x
        cmp	#6
        bcc	?propertyNoAddress

        cmp	#10
        bcs	?propertyNoAddress

        txa
        and	#15
        sta	draw.tileX
        txa
        lsr
        lsr
        lsr
        lsr
        sta	draw.tileY
        jsr	tileAddress

        sec
        rts

?propertyNoAddress
        clc
        rts

;************************************************
;* drawPropertyColors
;*
;* Input : draw.mapIndex after the terrain blit, before units/range
;* Output: faction pixels of owned buildings use only the owner's color
;* Trash: a,x,y,flags,dst,scratch,draw.lines/tileX/tileY
;************************************************

drawPropertyColors

        jsr	propertyAddress

        bcc	?propertyColorDone

        lda	#16
        sta	draw.lines

?propertyColorRow
        ldy	#1

?propertyColorByte
        lda	(dst),y
        and	#$aa
        lsr
        ldx	scratch
        bne	?propertyColorBlue

        eor	#$ff
        and	(dst),y
        jmp	?propertyColorStore

?propertyColorBlue
        ora	(dst),y

?propertyColorStore
        sta	(dst),y
        dey
        bpl	?propertyColorByte

        jsr	advanceRow

        dec	draw.lines
        bne	?propertyColorRow

?propertyColorDone
        rts

;************************************************
;* drawPropertyFlags
;*
;* Input : completed map/unit bitmap and property ownership
;* Output: 3x3 faction flags remain visible above unit/range overlays
;* Trash: a,x,y,flags,dst,scratch,draw.mapIndex/tileX/tileY
;************************************************

drawPropertyFlags

        lda	#0
        sta	draw.mapIndex

?propertyFlagCell
        jsr	propertyAddress

        bcc	?propertyFlagNext

        lda	#$2a
        ldx	scratch
        beq	?propertyFlagColor

        lda	#$3f

?propertyFlagColor
        sta	scratch
        ldx	#3

?propertyFlagRow
        ldy	#1
        lda	(dst),y
        and	#$c0
        ora	scratch
        sta	(dst),y
        jsr	advanceRow

        dex
        bne	?propertyFlagRow

?propertyFlagNext
        inc	draw.mapIndex
        lda	draw.mapIndex
        cmp	#MAP_CELLS
        bne	?propertyFlagCell

        rts
