; NAME: Get Item (None If Full) (MP2)
; 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, they get nothing. 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

;===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 NoItem ; If player has an item, go to NoItem
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)
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

J exit
NOP

;===No Item Message===
NoItem:
LUI A0 hi(NoItemMessage)
JAL CallMessage
ADDIU A0 A0 lo(NoItemMessage) ; Display NoItemMessage

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,0xC2 ; White font, Exclamation mark (!)
.byte 0xFF,0 ; Wait, press A to confirm

.align 16
NoItemMessage:
.ascii "You don"
.byte 0x5C ; Apostrophe (')
.ascii "t have space for another"
.byte 0x06 ; Blue font
.ascii " item"
.byte 0x08,0x85 ; White font, 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