; NAME: Get Item (Coins If Full) (MP2)
; GAMES: MP2_USA
; EXECUTION: Direct
; PARAM: Number|ItemIndex
; PARAM: Number|CoinsGained

ADDIU SP SP -40
SW RA 36(SP)
SW S0 32(SP)
SW S1 28(SP)

; This code is designed to give the current player an item. If they
; already have an item, they receive coins instead. The parameter
; CoinsGained is the number of coins the player receives if they
; already have an item. The parameter ItemIndex is a value from 0-9
; corresponding to a different item:
; 	Mushroom = 00
; 	Skeleton Key = 01
; 	Plunder Chest = 02
; 	Bowser Bomb = 03
; 	Dueling Glove = 04
; 	Warp Block = 05
; 	Golden Mushroom = 06
; 	Boo Bell = 07
; 	Bowser Suit = 08
; 	Magic Lamp = 09

; S0 = Current Player Struct
; S1 = Item Index / Coins Gained

;===Set Idle Animation===
LI A0 -1 ; Set current player
LI A1 -1 ; Set idle animation
JAL SetBoardPlayerAnimation
LI A2 2 ; Loop the animation

;===Get Current Player Struct===
JAL GetPlayerStruct
LI A0 -1 ; Get current player struct at V0
MOVE S0 V0 ; Copy V0 to S0

;===Check Inventory===
LBU T0 0x19(S0) ; Load item value from offset of player struct
LI T1 0xFF ; No item index
BNE T0 T1 GetCoins ; If player has no item, get coins
NOP

;===Get Item String===
LI S1 ItemIndex ; Load item index into S1
LI T0 32 ; Distance beetween item strings
MULT S1 T0 ; Multiply by item index
MFLO T0 ; Move result into T0
LUI A2 hi(ItemStrings)
ADDIU A2 A2 lo(ItemStrings)
ADDU A2 A2 T0 ; Add to get chosen item string

;===Prepare Item Message===
LUI A0 hi(ItemMessageHolder)
ADDIU A0 A0 lo(ItemMessageHolder) ; Parse destination
LUI A1 hi(ItemMessage)
ADDIU A1 A1 lo(ItemMessage) ; String formatter (%s)
; Pass item string to convert on A2
; Pass follow-up string to convert on A3
JAL sprintf
SW R0 16(SP) ; Terminator byte

;===Display Item Message===
LUI A0 hi(ItemMessageHolder)
ADDIU A0 A0 lo(ItemMessageHolder) ; Display ItemMessage with string
JAL CallMessage
LI A1 0

;===Store Item===
SB S1 0x19(S0) ; Store item index into offset of player struct

;===Play Sound===
JAL PlaySound
LI A0 0x340 ; Turn Start sound

JAL SleepProcess
LI A0 20 ; Wait 20 frames for sound

JAL HappyVoice
MOVE A0 S0 ; Pass current player struct on A0

J exit
NOP

;===Prepare Coins Message===
GetCoins:
LUI A0 hi(CoinString)
ADDIU A0 A0 lo(CoinString) ; Parse destination
LUI A1 hi(percent_d)
ADDIU A1 A1 lo(percent_d) ; String formatter
JAL sprintf
LI A2 CoinsGained ; Convert CoinsGained number to a text string

;===Display Coins Message===
LUI A0 hi(CoinsMessage)
ADDIU A0 A0 lo(CoinsMessage) ; Display CoinsMessage
LUI A1 hi(CoinString)
JAL CallMessage
ADDIU A1 A1 lo(CoinString) ; Load CoinString into String #1

;===Receive Coins===
LBU A0 0x1C(S0) ; Load player index from offset of player struct?
JAL AdjustPlayerCoinsGradual
LI A1 CoinsGained ; Gain coins

LBU A0 0x1C(S0) ; Load player index from offset of player struct?
JAL ShowPlayerCoinChange
LI A1 CoinsGained ; Gain coins

JAL SleepProcess
LI A0 30 ; Wait 30 frames for coin change

JAL HappyVoice
MOVE A0 S0 ; Pass current player struct on A0

exit:
LW RA 36(SP)
LW S0 32(SP)
LW S1 28(SP)
JR RA
ADDIU SP SP 40

;===Prep for Displaying Coin String===
percent_d:
.asciiz "%d" ; 0x25640000

CoinString:
.fill 8

;===Mini Func to Call Message===
.align 4
CallMessage:
ADDIU SP SP -40
SW RA 36(SP)

; A0 = Message Address
; A1 = String #1

MOVE A2 A1 ; Copy A1 to A2
MOVE A1 A0 ; Copy A0 to A1
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 -1 ; Character image (-1 for none)
; Visit the following link to get the full list of Character Images
; https://github.com/PartyPlanner64/PartyPlanner64/wiki/Displaying-Messages
; If you use a character image, check the bottom of the code
JAL ShowMessage
LI A3 0

; Obligatory message box closing/cleanup calls.
JAL CloseMessage
NOP
JAL 0x80056168
NOP

LW RA 36(SP)
JR RA
ADDIU SP SP 40

;===Mini Func to Play Happy Voice===
.align 4
HappyVoice:
ADDIU SP SP -40
SW RA 36(SP)

; A0 = Current Player Struct

LBU A0 4(A0) ; Load character value from offset of player struct
JAL PlaySound
ADDIU A0 A0 0x103 ; Add character value to Mario's happy voice
; Character voices are their "character value" distance away from
; Mario's, e.g. Luigi's character value = 1, so Luigi's happy voice
; is 1 away from Mario's.
; 0x103 = Happy Voice

LI A0 -1 ; Set current player
LI A1 5 ; Set joy animation
JAL SetBoardPlayerAnimation
LI A2 0 ; Do not loop animation

JAL SleepProcess
LI A0 40 ; Wait 40 frames for animation

LW RA 36(SP)
JR RA
ADDIU SP SP 40

;===Message Text===
.align 16
ItemMessage:
.ascii "You got a "
.byte 0x06 ; Blue font
.ascii "%s" ; Item string formatter
.byte 0x08,0xC2 ; White font, Exclamation mark (!)
.byte 0xFF,0 ; Wait, press A to confirm

.align 16
CoinsMessage:
.ascii "You don"
.byte 0x5C ; Apostrophe (')
.ascii "t have space for another"
.byte 0x06 ; Blue font
.ascii " item"
.byte 0x08,0x82 ; White font, Comma (,)
.byte 0x0A ; New line (writes below)
.ascii "so have "
.byte 0x07 ; Yellow font
.byte 0x11 ; String #1 stored in A2 (CoinString)
.ascii " coins"
.byte 0x08 ; White font
.ascii " instead"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; Wait, press A to confirm

; Here's a list of the most common bytes you'll need
; .byte 0x01 ; Black Font
; .byte 0x03 ; Red Font
; .byte 0x04 ; Purple Font
; .byte 0x05 ; Green Font
; .byte 0x06 ; Blue font
; .byte 0x07 ; Yellow Font
; .byte 0x08 ; White Font
; .byte 0x85 ; Period (.)
; .byte 0xC2 ; Exclamation Mark (!)
; .byte 0xC3 ; Question Mark (?)
; .byte 0x82 ; Comma (,)
; .byte 0x0A ; New Line (Writes Below)
; .byte 0x5C ; Apostrophe (')
; .byte 0x29 ; Coin icon
; .byte 0x3D ; - (minus)
; .byte 0x3E ; x (multiply)
; .byte 0xFF,0 ; FF=Pause

; If your message has an image, use this at the start of each line
; .byte 0x1A,0x1A,0x1A,0x1A ; Padding for picture

;===Item Message Placeholder===
.align 16
ItemMessageHolder:
.fill 0x1024,0

;===Item Strings===
.align 32
ItemStrings:
.asciiz "Mushroom" ; Mushroom string
.align 32
.asciiz "Skeleton Key" ; Skeleton Key string
.align 32
.asciiz "Plunder Chest" ; Plunder Chest string
.align 32
.asciiz "Bowser Bomb" ; Bowser Bomb string
.align 32
.asciiz "Dueling Glove" ; Dueling Glove string
.align 32
.asciiz "Warp Block" ; Warp Block string
.align 32
.asciiz "Golden Mushroom" ; Golden Mushroom string
.align 32
.asciiz "Boo Bell" ; Boo Bell string
.align 32
.asciiz "Bowser Suit" ; Bowser Suit string
.align 32
.asciiz "Magic Lamp" ; Magic Lamp string