;************************************************
;* resetEconomy
;*
;* Input : app.startingCash in thousands, game.team = orange
;* Output: map funds, three replacements per army, no round-one income
;* Trash: a,x,y,flags; economy state
;************************************************

resetEconomy

        lda	#0
        ldx	#.sizeof(economy_t)-1

?economyResetByte
        sta	economy,x
        dex
        bpl	?economyResetByte

        lda	app.startingCash
        sta	economy.cash
        sta	economy.cash+1
        jmp	startEconomyTurn

;************************************************
;* countIncome
;*
;* Input : current army and property ownership
;* Output: economy.income in thousands, capped at MAX_INCOME
;* Trash: a,x,flags
;************************************************

countIncome

        lda	#0
        sta	economy.income
        ldx	#MAP_CELLS-1

?incomeCell
        lda	property.owner,x
        cmp	game.team
        bne	?incomeNext

        lda	terrain,x
        cmp	#6
        bcc	?incomeNext

        cmp	#AIRPORT+1
        bcs	?incomeNext

        inc	economy.income
        lda	economy.income
        cmp	#MAX_INCOME
        beq	?incomeDone

?incomeNext
        dex
        cpx	#NONE
        bne	?incomeCell

?incomeDone
        rts

;************************************************
;* startEconomyTurn
;*
;* Input : army whose turn is beginning
;* Output: income once from round two; resets only this army's purchase quota
;* Trash: a,x,y,flags
;************************************************

startEconomyTurn

        lda	#0
        sta	economy.buyOpen
        ldy	game.team
        sta	economy.bought,y
        jsr	advanceSupply
        jsr	countIncome

        lda	game.round
        cmp	#1
        beq	?incomeTurnDone

        lda	economy.cash,y
        clc
        adc	economy.income
        bcs	?incomeCap

        cmp	#100
        bcc	?incomeStore

?incomeCap
        lda	#99

?incomeStore
        sta	economy.cash,y

?incomeTurnDone
        rts

;************************************************
;* shopAvailable
;*
;* Input : app/help/battle/winner state and mission number
;* Output: carry = active match with purchases; otherwise A = refusal
;* Trash: a,flags
;************************************************

shopAvailable

        lda	app.screen
        ora	help.active
        ora	battle.active
        ora	game.winner
        beq	?shopAllowed

        lda	#message_id.NO_BUY
        clc
        rts

?shopAllowed
        jsr	purchasesAllowed

        bcs	?shopPurchaseAllowed

        lda	#message_id.NO_REINFORCEMENTS
        clc
        rts

?shopPurchaseAllowed
        sec
        rts

;************************************************
;* tryPurchase
;*
;* Input : economy.choice = kind 0..5; current army and supply
;* Output: carry = purchase; A = success/error message.
;*         A purchased unit occupies the free own HQ, acted with 0 MP.
;* Trash: a,x,y,flags; economy scratch, roster/cash on success only
;************************************************

tryPurchase

        jsr	shopAvailable

        bcs	?purchaseMatch

        rts

?purchaseMatch
        jsr	purchaseKindLimit

        ldx	economy.choice
        cpx	scratch
        bcc	?purchaseChoice

        lda	#message_id.NO_BUY
        clc
        rts

?purchaseChoice
        lda	unitPrices,x
        sta	economy.cost
        ldy	game.team
        lda	economy.bought,y
        beq	?purchaseFunds

        lda	#message_id.BOUGHT
        clc
        rts

?purchaseFunds
        jsr	reinforcementAvailable

        bcs	?purchaseMoney

        rts

?purchaseMoney
        lda	economy.cash,y
        cmp	economy.cost
        bcs	?purchaseCheckArmy

        lda	#message_id.NO_MONEY
        clc
        rts

?purchaseCheckArmy
        lda	#0
        sta	economy.slot
        ldx	app.unitCount
        dex

?purchaseCountArmy
        lda	units.hp,x
        beq	?purchaseCountNext

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

        inc	economy.slot
        lda	economy.slot
        cmp	#MAX_ARMY_UNITS
        bcs	?purchaseArmyFull

?purchaseCountNext
        dex
        bpl	?purchaseCountArmy

        jmp	?purchaseFindHQ

?purchaseArmyFull
        lda	#message_id.ARMY_FULL
        clc
        rts

?purchaseFindHQ
        jmp	findProductionSite

purchaseHQFound

        stx	economy.hq
        lda	#NONE
        sta	economy.slot
        ldx	app.unitCount
        dex

?purchaseOccupant
        lda	units.hp,x
        beq	?purchaseDeadSlot

        jsr	unitCell

        cpy	economy.hq
        bne	?purchaseNextUnit

        lda	#message_id.HQ_BLOCKED
        clc
        rts

?purchaseDeadSlot
        stx	economy.slot

?purchaseNextUnit
        dex
        bpl	?purchaseOccupant

        ldx	economy.slot
        cpx	#NONE
        bne	?purchaseSpawn

        ldx	app.unitCount
        cpx	#MAX_UNITS
        bcc	?purchaseAppend

        lda	#message_id.ARMY_FULL
        clc
        rts

?purchaseAppend
        inc	app.unitCount

?purchaseSpawn
        lda	economy.hq
        and	#15
        sta	units.x,x
        lda	economy.hq
        lsr
        lsr
        lsr
        lsr
        sta	units.y,x
        lda	economy.choice
        sta	units.kind,x
        lda	game.team
        sta	units.team,x
        lda	#10
        sta	units.hp,x
        lda	#0
        sta	units.mp,x
        jsr	initUnitFuel

        lda	#1
        sta	units.acted,x
        ldy	game.team
        sta	economy.bought,y
        lda	economy.cash,y
        sec
        sbc	economy.cost
        sta	economy.cash,y
        jsr	recordPurchase

        lda	#message_id.PURCHASED
        sec
        rts

;************************************************
;* toggleShop / openShop / closeShop
;*
;* Input : active human turn; current selection is preserved
;* Output: purchase panel replaces map HUD; bitmap/focus restored on close
;* Trash: a,x,y,flags; HUD/economy scratch
;************************************************

toggleShop

        lda	economy.buyOpen
        bne	closeShop

openShop

        jsr	shopAvailable

        bcs	?shopOpen

        sta	game.message
        jmp	updateHud

?shopOpen
        lda	#1
        sta	economy.buyOpen
        sta	game.menuOpen
        lda	#0
        sta	economy.choice
        sta	economy.message
        jmp	shopHud

closeShop

        lda	#0
        sta	economy.buyOpen
        sta	game.menuOpen
        sta	game.message
        jmp	updateHud

;************************************************
;* shopNavigate
;*
;* Input : active-low keyboard/joystick direction
;* Output: wraps the four choices; up returns to map
;* Trash: a,x,y,flags; HUD scratch
;************************************************

shopNavigate

        lda	input.direction
        and	#1
        bne	?shopLeft

        jmp	closeShop

?shopLeft
        lda	input.direction
        and	#4
        bne	?shopRight

        dec	economy.choice
        jmp	?shopWrapChoice

?shopRight
        lda	input.direction
        and	#8
        bne	?shopChoiceReady

        inc	economy.choice

?shopWrapChoice
        jsr	wrapShopChoice

?shopChoiceReady
        lda	#0
        sta	economy.message
        jmp	shopHud

;************************************************
;* buyUnit
;*
;* Input : purchase panel choice
;* Output: explicit outcome; successful spawn rebuilds map occupancy
;* Trash: a,x,y,flags; game/economy/renderer scratch
;************************************************

buyUnit

        jsr	tryPurchase

        sta	economy.message
        bcc	shopHud

        jmp	render

;************************************************
;* shopHud
;*
;* Input : active army, money, quota and selected purchase kind
;* Output: price, funds, next income, quota and error/status rows
;* Trash: a,x,y,flags,src,scratch; menu/economy scratch
;************************************************

shopHud

        ldx	#199

?shopCopyTemplate
        lda	shopTemplate,x
        sta	hud,x
        dex
        cpx	#NONE
        bne	?shopCopyTemplate

        lda	game.team
        clc
        adc	#17
        sta	hud+SHUD_TEAM
        ldy	game.team
        lda	economy.bought,y
        beq	?shopMoney

        lda	#57
        sta	hud+SHUD_BOUGHT

?shopMoney
        lda	economy.cash,y
        ldx	#SHUD_CASH
        jsr	menuDecimal
        jsr	countIncome

        lda	economy.income
        ldx	#SHUD_INCOME
        jsr	menuDecimal
        jsr	shopSupplyHud

        ldy	economy.choice
        lda	unitPrices,y
        ldx	#SHUD_COST
        jsr	menuDecimal

        tya
        asl
        asl
        asl
        tay
        ldx	#0

?shopCopyName
        lda	unitNames,y
        sta	hud+SHUD_UNIT,x
        iny
        inx
        cpx	#8
        bne	?shopCopyName

        jsr	airShopLabel
        jsr	shopChoiceAddress

?shopHighlight
        lda	hud,x
        ora	#$80
        sta	hud,x
        inx
        dec	menu.index
        bne	?shopHighlight

        lda	economy.message
        bne	?shopStatus

        lda	#message_id.SHOP

?shopStatus
        jmp	menuCopyMessage
