; NAME: Get Item (Choose to Replace) (MP2) v1.3
; GAMES: MP2_USA
; EXECUTION: Direct
; PARAM: Number|ItemIndex

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

; This code is designed to give the current player an item. If they
; already have an item, they will be given a prompt to choose whether
; to keep the new item or the old 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 String / Item Index
; S2 = Prompt Choice

;===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 GetItem ; If player has no item, get item
NOP

;===Get Item Strings===
JAL GetItemString
MOVE A0 T0 ; Get old item string at V0
MOVE S1 V0 ; Copy V0 to S1

JAL GetItemString
LI A0 ItemIndex ; Get new item string at V0

;===Prepare Item Prompt===
LUI A0 hi(PromptHolder)
ADDIU A0 A0 lo(PromptHolder) ; Parse destination
LUI A1 hi(ItemPrompt)
ADDIU A1 A1 lo(ItemPrompt) ; String formatter (%s)
MOVE A2 V0 ; Pass new item string to convert on A2
MOVE A3 S1 ; Pass old item string to convert on A3
JAL sprintf
SW R0 16(SP) ; Terminator byte

;===Item Prompt===
StartPrompt:
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
LUI A1 hi(PromptHolder)
ADDIU A1 A1 lo(PromptHolder) ; Display ItemPrompt with strings
LI A2 0
JAL ShowMessage
LI A3 0

; Get the selection, either from the player or CPU.
JAL GetRandPromptSelection
NOP
MOVE S2 V0 ; S2 now has the chosen option index

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

; Change the player's destination based on the choice.
BEQZ S2 GetItem
NOP
LI T0 1
BEQ S2 T0 KeepItem
NOP
; else pick "View map"
JAL 0x80103A64 ; ViewBoardMap
NOP
J StartPrompt
NOP

;===Prepare Item Message===
GetItem:
LI S1 ItemIndex ; Load new item index into S1
JAL GetItemString
MOVE A0 S1 ; Get new item string at V0

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

;===Display Item Message===
LUI A0 hi(MessageHolder)
JAL CallMessage
ADDIU A0 A0 lo(MessageHolder) ; 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

;===Prepare Keep Item Message===
KeepItem:
JAL GetItemString
LBU A0 0x19(S0) ; Get old item string at V0 from offset of player struct

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

LUI A0 hi(MessageHolder)
JAL CallMessage
ADDIU A0 A0 lo(MessageHolder) ; Display KeepItemMessage with string

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

;===Mini Func to Get Item String===
.align 4
GetItemString:
ADDIU SP SP -40
SW RA 36(SP)

; A0 = Item Index
; V0 = Chosen Item String

LI T0 32 ; Distance beetween item strings
MULT A0 T0 ; Multiply by item index
MFLO T0 ; Move result into T0
LUI V0 hi(ItemStrings)
ADDIU V0 V0 lo(ItemStrings)
ADDU V0 V0 T0 ; Add to get chosen item string

LW RA 36(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
ItemPrompt:
.byte 0x0B ; Start prompt
.ascii "You could receive a "
.byte 0x06 ; Blue font
.ascii "%s" ; New item string formatter
.byte 0x08,0x82 ; White font, Comma (,)
.byte 0x0A ; New line (writes below)
.ascii "but you don"
.byte 0x5C ; Apostrophe (')
.ascii "t have space for it"
.byte 0x85 ; Period (.)
.byte 0x0A ; New line (writes below)
.ascii "Will you toss your "
.byte 0x06 ; Blue font
.ascii "%s" ; Old item string formatter
.byte 0x08 ; White font
.ascii " instead"
.byte 0xC3 ; Question mark (?)
.byte 0x0A,0x0A,0x1A,0x1A ; Two new lines + option indent
.byte 0x0C ; Start option
.ascii "Take the"
.byte 0x06 ; Blue font
.ascii " new item"
.byte 0x08 ; White font
.byte 0x0D ; End option
.byte 0x0A,0x1A,0x1A ; New line + option indent
.byte 0x0C ; Start option
.ascii "Keep the"
.byte 0x06 ; Blue font
.ascii " old item"
.byte 0x08 ; White font
.byte 0x0D ; End option
.byte 0x0A,0x1A,0x1A ; New line + option indent
.byte 0x0C ; Start option
.ascii "View map"
.byte 0x0D ; End option
.byte 0 ; End prompt

.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
KeepItemMessage:
.ascii "You kept your "
.byte 0x06 ; Blue font
.ascii "%s" ; Item string formatter
.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 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

;===Message Placeholders===
.align 16
PromptHolder:
.fill 0x1024,0

.align 16
MessageHolder:
.fill 0x1024,0