        icl	"src/inspect.i"

;************************************************
;* toggleInspect
;*
;* Input : map cursor, living units and game.rangeVisible/menuOpen
;* Output: hovered unit's weapon range toggled; selection unchanged
;* Trash: a,x,y,flags; inspect/game and renderer scratch
;************************************************

toggleInspect

        lda	game.rangeVisible
        bne	clearInspect

        lda	game.menuOpen
        bne	?inspectReturn

        jsr	findUnit

        cpx	#NONE
        beq	?inspectEmpty

        stx	inspect.unit
        lda	#1
        sta	game.rangeVisible
        lda	#message_id.RANGE
        bne	?inspectMessage

?inspectEmpty
        lda	#message_id.RANGE_EMPTY

?inspectMessage
        sta	game.message
        jmp	render

;************************************************
;* clearInspect
;*
;* Input : game.rangeVisible; map state preserved during inspection
;* Output: range overlay removed without changing selection/actions
;* Trash: a,x,y,flags; game and renderer scratch when visible
;************************************************

clearInspect

        lda	game.rangeVisible
        beq	?inspectReturn

        lda	#0
        sta	game.rangeVisible
        sta	game.message
        jmp	render

?inspectReturn
        rts

;************************************************
;* drawInspect
;*
;* Input : inspect.unit, current positions and game.rangeVisible
;* Output: corner marks on all on-map fields in the weapon's range
;* Trash: a,x,y,flags; inspect/attack/draw scratch and dst
;*
;* This is geometric range, not readiness or a movement forecast.
;* Enemy and spent units use the same weapon distance tables.
;************************************************

drawInspect

        lda	game.rangeVisible
        beq	?inspectReturn

        lda	#0
        sta	inspect.cell

?inspectTile
        lda	inspect.cell
        and	#15
        sta	draw.tileX
        sta	attack.x
        lda	inspect.cell
        lsr
        lsr
        lsr
        lsr
        sta	draw.tileY
        sta	attack.y
        ldx	inspect.unit
        jsr	distanceToPosition
        jsr	distanceInRange

        bcc	?inspectNext

        jsr	drawAttackTile

?inspectNext
        inc	inspect.cell
        lda	inspect.cell
        cmp	#MAP_CELLS
        bne	?inspectTile

        rts
