; NAME: Get Item (Replace Current Item) (MP2) v2.1
; GAMES: MP2_USA
; EXECUTION: Direct
; PARAM: Number|ItemIndex

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, the item will be replaced by the new one.
; 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

;===Check for Bowser===
JAL GetCurrentPlayerIndex ; Get current player index at V0
NOP
SLTI T0 V0 4 ; If V0 < 4, T0 = 1
BEQZ T0 exit ; If T0 = 0, player is Bowser, exit
NOP

;===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
BEQ T0 T1 GetNullString ; If player has no item, get null string
NOP

;===Get Follow-up Item String===
LUI A3 hi(FollowString)
J GetItemString
ADDIU A3 A3 lo(FollowString) ; Convert FollowString to text

;===Get Null String===
GetNullString:
LUI A3 hi(NullString)
ADDIU A3 A3 lo(NullString) ; Convert NullString to text

;===Get Item String===
GetItemString:
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)
JAL CallMessage
ADDIU A0 A0 lo(ItemMessageHolder) ; Display ItemMessage with string

;===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

;===Play Happy Voice===
LBU A0 4(S0) ; 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

;===Play Happy Animation===
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:
LW RA 36(SP)
LW S0 32(SP)
LW S1 28(SP)
JR RA
ADDIU SP SP 40

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

; A0 = Message Address

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
LI A2 0
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

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

;===Follow-up Strings===
.align 32
NullString:
.byte 0xC2,0 ; Exclamation mark (!)

.align 32
FollowString:
.byte 0x82,0x0A ; Comma (,), New line (writes below)
.ascii "so you tossed your old item"
.byte 0x85,0 ; Period (.)