; NAME: Get Item (Coins If Full) (MP3) v2
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: Number|Coins
; PARAM: Space|SpaceToFace

; This code is designed to give the landing or passing player an item
; of your choice. If they have no space to carry the item, they will
; receive coins. The number of coins is given by the parameter Coins.

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

; S0 = Current Player Struct / Current Player Index

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

;===Rotate Player Towards Space===
LI A0 -1 ; Set current player
LI A1 8 ; Take 8 frames to rotate
JAL 0x800ED20C ; Rotate player func
LI A2 SpaceToFace ; Face towards SpaceToFace

;===Check Player Item Slots===
JAL GetPlayerStruct
LI A0 -1 ; Get current player struct

MOVE S0 V0
LI T2 0 ; Loop counter
checkslots:
LI T1 0xFF ; No item value
LBU T0 0x18(S0) ; Load item from offset of player struct
BEQ T0 T1 getitem ; If empty, get item
ADDIU T2 T2 1 ; Add 1 to loop
LI T1 3
BEQ T2 T1 getcoins ; If all item slots are full, get coins
ADDIU S0 S0 1 ; Add 1 to check next item slot
J checkslots
NOP

getcoins:
;===Set Coin String===
LUI A0 hi(CoinString)
ADDIU A0 A0 lo(CoinString) ; Location of converted string
LUI A1 hi(percent_d)
ADDIU A1 A1 lo(percent_d)
JAL sprintf
LI A2 Coins ; Convert # of Coins into string

;===Get Coins Message===
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
LUI A1 hi(CoinsMessage)
ADDIU A1 A1 lo(CoinsMessage)
LUI A2 hi(CoinString)
JAL CallMessage
ADDIU A2 A2 lo(CoinString) ; Store CoinString into String #1

;===Get Coins===
JAL GetCurrentPlayerIndex
NOP
MOVE S0 V0

MOVE A0 S0
JAL AdjustPlayerCoinsGradual
LI A1 Coins ; Give current player Coins

MOVE A0 S0
JAL ShowPlayerCoinChange
LI A1 Coins ; Show coin change HUD

JAL SleepProcess
LI A0 30 ; Wait 30 frames

J exit
NOP

;===Get Item Message===
getitem:
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
LUI A1 hi(ItemMessage)
ADDIU A1 A1 lo(ItemMessage)
JAL CallMessage
LI A2 0

;===Give Item===
JAL PlaySound
LI A0 0x112 ; Turn Start Sound Index
; Use the Audio Player on PP64 to find other sound indexes to use
; Then use the following link to know what Sound Index you need
; https://pastebin.com/H1jxNFqr

LI T0 0x00 ; T0 contains Item Index
SB T0 0x18(S0) ; Place item on last-checked item slot

; Item Indexes:
; 0x00 = Mushroom
; 0x01 = Skeleton Key
; 0x02 = Poison Mushroom
; 0x03 = Reverse Mushroom
; 0x04 = Cellular Shopper
; 0x05 = Warp Block
; 0x06 = Plunder Chest
; 0x07 = Bowser Phone
; 0x08 = Dueling Glove
; 0x09 = Lucky Lamp
; 0x0A = Golden Mushroom
; 0x0B = Boo Bell
; 0x0C = Boo Repellant
; 0x0D = Bowser Suit
; 0x0E = Magic Lamp
; 0x0F = Koopa Card
; 0x10 = Barter Box
; 0x11 = Lucky Coin
; 0x12 = Wacky Watch

exit:
;===Play Happy Voice===
JAL GetPlayerStruct
LI A0 -1 ; Get current player struct

LBU T0 3(V0) ; Load character value from offset of player struct
JAL PlaySound
ADDIU A0 T0 0x2BE ; 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.
; 0x2BE = Happy Voice, 0x287 = Sad Voice, 0x263 = Get Star Voice

;===Play Happy Animation===
LI A0 -1 ; Set current player
LI A1 5 ; Set happy animation
JAL SetBoardPlayerAnimation
LI A2 0 ; Do not loop animation

JAL SleepProcess
LI A0 40 ; Wait 40 frames to play animation

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

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

SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
JAL ShowMessage
LI A3 0

JAL 0x800EC9DC
NOP
JAL CloseMessage
NOP
JAL 0x800EC6EC
NOP

LW RA 28(SP)
JR RA
ADDIU SP SP 32

;===String Formatters===
percent_d:
.asciiz "%d" ; 0x25640000

CoinString:
.fill 8

;===Message Text===
.align 16
ItemMessage:
.ascii "You got a"
.byte 0x06 ; Blue Font
.ascii " Mushroom"
.byte 0x08 ; White Font
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; Wait, press A to close message

.align 16
CoinsMessage:
.ascii "You got "
.byte 0x07 ; Yellow Font
.byte 0x11 ; String #1 (# of Coins)
.ascii " coins"
.byte 0x08 ; White Font
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; Wait, press A to close message

; 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 character image