; NAME: Get Item (Coins If Full) (MP3) v3
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: Number|Item
; 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 item given is decided by the parameter Item and
; the number of coins given is decided by the parameter Coins. The
; parameter SpaceToFace is the space the player will face during the
; event.

ADDIU SP SP -48
SW RA 44(SP)
SW S0 40(SP)
SW S1 36(SP)
SW S2 32(SP)

; S0 = Current Player Struct
; S1 = Empty Item Slot Offset
; S2 = Item Index

;===Hide Dice Roll===
JAL GetPlayerStruct
LI A0 -1 ; Get current player struct at V0
MOVE S0 V0 ; Copy V0 to S0

JAL 0x800DBEC0 ; HideDiceRoll
LBU A0 0x1D(S0) ; Pass current player index on A0

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

;===Rotate Toward Space===
LI A0 -1 ; Set current player
LI A1 8 ; Rotate in 8 frames
JAL RotatePlayerModel
LI A2 SpaceToFace ; Rotate toward SpaceToFace

;===Check Player Item Slots===
JAL PlayerHasEmptyItemSlot
LBU A0 0x1D(S0) ; Pass current player index on A0

LI T0 -1
BEQ V0 T0 GetCoins ; If player has full inventory, get coins
NOP
; else, get item
MOVE S1 V0 ; Copy V0 to S1
LI S2 Item ; Select item

; Item Indexes:
; 0 = Mushroom
; 1 = Skeleton Key
; 2 = Poison Mushroom
; 3 = Reverse Mushroom
; 4 = Cellular Shopper
; 5 = Warp Block
; 6 = Plunder Chest
; 7 = Bowser Phone
; 8 = Dueling Glove
; 9 = Lucky Lamp
; 10 = Golden Mushroom
; 11 = Boo Bell
; 12 = Boo Repellant
; 13 = Bowser Suit
; 14 = Magic Lamp
; 15 = Koopa Card
; 16 = Barter Box
; 17 = Lucky Coin
; 18 = Wacky Watch

;===Prepare Item String===
LUI A2 hi(ItemStrings)
ADDIU A2 A2 lo(ItemStrings) ; Get item string
SLL T2 S2 5 ; Multiply item index by 5 (distance between item strings)
ADDU A2 A2 T2 ; Add distance between item strings

LUI A0 hi(MessageHolder)
ADDIU A0 A0 lo(MessageHolder) ; Parse destination
LUI A1 hi(ItemMessage)
ADDIU A1 A1 lo(ItemMessage) ; String formatter (%s)
; Pass item string on A2
JAL sprintf
LI A3 0 ; Terminator byte

;===Item Message===
LI A0 0 ; Pass String #1 on A0
LUI A1 hi(MessageHolder)
JAL CallMessage
ADDIU A1 A1 lo(MessageHolder) ; Pass message address on A1

;===Get Item===
ADDU S1 S0 S1 ; Add empty slot offset to player struct
SB S2 0x18(S1) ; Store item into offset of player struct

JAL PlaySound
LI A0 0x10C ; Turn Start sound

JAL SleepProcess
LI A0 10 ; Wait 10 frames for sound

J HappyVoice
NOP

;===Prepare Coin String===
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 (%d)
JAL sprintf
LI A2 Coins ; Convert number of coins into string

;===Get Coins Message===
LUI A0 hi(CoinString)
ADDIU A0 A0 lo(CoinString) ; Pass String #1 on A0
LUI A1 hi(CoinsMessage)
JAL CallMessage
ADDIU A1 A1 lo(CoinsMessage) ; Pass message address on A1

;===Get Coins===
LBU A0 0x1D(S0) ; Pass current player index on A0
JAL AdjustPlayerCoinsGradual
LI A1 Coins ; Pass number of coins on A1

LBU A0 0x1D(S0) ; Pass current player index on A0
JAL ShowPlayerCoinChange
LI A1 Coins ; Pass number of coins on A1

JAL SleepProcess
LI A0 30 ; Wait 30 frames

;===Happy Voice and Animation===
HappyVoice:
LBU A0 3(S0) ; Load character value from offset of player struct
JAL PlaySound
ADDIU A0 A0 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

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

exit:
JAL 0x800DB884 ; ShowDiceRoll
LBU A0 0x1D(S0) ; Pass current player index on A0

JAL SleepProcess
LI A0 10 ; Wait 10 frames for dice roll to show

LW RA 44(SP)
LW S0 40(SP)
LW S1 36(SP)
LW S2 32(SP)
JR RA
ADDIU SP SP 48

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

.align 16
CoinString:
.fill 8

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

; A0 = String #1
; A1 = Message Address

MOVE A2 A0 ; Copy A0 to A2
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
; Passing message address on A1
JAL ShowMessage
LI A3 0

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

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

;===Message Text===
.align 16
ItemMessage:
.ascii "You got a "
.byte 0x06 ; Blue Font
.ascii "%s" ; Item string
.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

;===Message Holder===
.align 16
MessageHolder:
.fill 0x1024,0

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