; NAME: Switch Item Slots (MP2) v1.4
; GAMES: MP2_USA
; EXECUTION: Direct

ADDIU SP SP -64
SW RA 60(SP)
SW S0 56(SP)
SW S1 52(SP)
SW S2 48(SP)
SW S3 44(SP)
SW S4 40(SP)

; This code is designed to allow the player to switch between their
; main and reserve item slots, effectively allowing them to have an
; inventory of three items. The main item slot is the item usable
; on the player's turn. Select a reserve item slot to switch it with
; the main item slot, allowing you to open up an empty slot to get
; another item or to use a different item.

; Set this event as "Before dice roll". Ensure that your board also
; has the "Setup Item Slots" event ON. If you are also using the
; "Skeleton Key Prompt" event, it is recommended to add this event
; to your board first.

; S0 = Current Player Struct
; S1 = Main Item
; S2 = Reserve Item #1
; S3 = Reserve Item #2
; S4 = Prompt Choice

;===Check Multiple Item Slots Flag===
LUI T0 0x800F ; 0x800F0000
ORI T0 T0 0x8CE2 ; 0x800F8CE2, Multiple Item Slots Flag
LBU T0 0(T0) ; Load flag into T0
BEQZ T0 exit ; If Toggle is OFF, exit
NOP

;===Get Current Player Struct===
JAL GetPlayerStruct
LI A0 -1 ; Get current player struct at V0
MOVE S0 V0 ; Copy V0 to S0

;===Check Item Slots===
LI T0 0xFF ; No item index
LBU S1 0x19(S0) ; Load main item index into S1
LBU S2 0x1E(S0) ; Load reserve item index #1 into S2
LBU S3 0x1F(S0) ; Load reserve item index #2 into S3
BNE S1 T0 PrepareItemPrompt
NOP
BNE S2 T0 PrepareItemPrompt
NOP
BNE S3 T0 PrepareItemPrompt
NOP
; else, player has no items, so exit
J exit
NOP

;===Prepare Item Prompt===
PrepareItemPrompt:
LUI A2 hi(WhiteFont)
ADDIU A2 A2 lo(WhiteFont) ; Default to white font
MOVE T4 A2 ; Copy A2 to T4
MOVE T6 A2 ; Copy A2 to T6

LUI A3 hi(NullString)
ADDIU A3 A3 lo(NullString) ; Default to NullString
MOVE T5 A3 ; Copy A3 to T5
MOVE T7 A3 ; Copy A3 to T7

LUI T8 hi(ItemStrings)
ADDIU T8 T8 lo(ItemStrings)

BEQ S1 T0 ItemStringTwo ; If main item slot is empty, check next slot
NOP
LUI A2 hi(BlueFont)
ADDIU A2 A2 lo(BlueFont) ; Use blue font for main item
SLL T1 S1 5 ; Multiply item index by 32 (distance between item strings)
ADDU A3 T8 T1 ; Add to get main item string

ItemStringTwo:
BEQ S2 T0 ItemStringThree ; If reserve slot is empty, check next slot
NOP
LUI T4 hi(BlueFont)
ADDIU T4 T4 lo(BlueFont) ; Use blue font for reserve item #1
SLL T2 S2 5 ; Multiply item index by 32 (distance between item strings)
ADDU T5 T8 T2 ; Add to get reserve item string #1

ItemStringThree:
BEQ S3 T0 PrintItemPrompt ; If reserve slot is empty, print item prompt
NOP
LUI T6 hi(BlueFont)
ADDIU T6 T6 lo(BlueFont) ; Use blue font for reserve item #2
SLL T3 S3 5 ; Multiply item index by 32 (distance between item strings)
ADDU T7 T8 T3 ; Add to get reserve item string #2

;===Print Item Prompt===
PrintItemPrompt:
LUI A0 hi(PromptHolder)
ADDIU A0 A0 lo(PromptHolder) ; Parse destination
LUI A1 hi(ItemPrompt)
ADDIU A1 A1 lo(ItemPrompt) ; String formatter (%s)
; Pass main item font on A2
; Pass main item string on A3
SW T4 16(SP) ; Pass reserve item font #1 on A4
SW T5 20(SP) ; Pass reserve item string #1 on A5
SW T6 24(SP) ; Pass reserve item font #2 on A6
SW T7 28(SP) ; Pass reserve item string #2 on A7
JAL sprintf ; Convert strings to text
SW R0 32(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 S4 V0 ; S4 now has the chosen option index

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

; Change the player's destination based on the choice.
BEQZ S4 exit
NOP
LI T0 1
BEQ S4 T0 ReserveOne
NOP
LI T0 2
BEQ S4 T0 ReserveTwo
NOP
; else pick "View map"
JAL 0x80103A64 ; ViewBoardMap
NOP
J StartPrompt
NOP

;===Switch Main Item and Reserve Item #1===
ReserveOne:
SB S1 0x1E(S0) ; Store main item into reserve slot #1
J exit
SB S2 0x19(S0) ; Store reserve item #1 into main slot

;===Switch Main Item and Reserve Item #2===
ReserveTwo:
SB S1 0x1F(S0) ; Store main item into reserve slot #2
SB S3 0x19(S0) ; Store reserve item #2 into main slot

exit:
LW RA 60(SP)
LW S0 56(SP)
LW S1 52(SP)
LW S2 48(SP)
LW S3 44(SP)
LW S4 40(SP)
JR RA
ADDIU SP SP 64

;===Item Fonts===
.align 16
WhiteFont:
.byte 0x08,0

.align 16
BlueFont:
.byte 0x06,0

;===Message Text===
.align 16
ItemPrompt:
.byte 0x0B ; Start prompt
.ascii "Which item slot do you want to use"
.byte 0xC3 ; Question mark (?)
.byte 0x0A,0x0A,0x1A,0x1A ; Two new lines + option indent
.byte 0x0C ; Start option
.ascii "Main item slot"
.byte 0x7B ; Colon (:)
.ascii "%s" ; Main item font
.ascii "%s" ; Main item string
.byte 0x0D ; End option
.byte 0x0A,0x1A,0x1A ; New line + option indent
.byte 0x0C ; Start option
.byte 0x08 ; White font
.ascii "Reserve item slot 1"
.byte 0x7B ; Colon (:)
.ascii "%s" ; Reserve item font #1
.ascii "%s" ; Reserve item string #1
.byte 0x0D ; End option
.byte 0x0A,0x1A,0x1A ; New line + option indent
.byte 0x0C ; Start option
.byte 0x08 ; White font
.ascii "Reserve item slot 2"
.byte 0x7B ; Colon (:)
.ascii "%s" ; Reserve item font #2
.ascii "%s" ; Reserve item string #2
.byte 0x0D ; End option
.byte 0x0A,0x1A,0x1A ; New line + option indent
.byte 0x0C ; Start option
.byte 0x08 ; White font
.ascii "View map"
.byte 0x0D ; End option
.byte 0 ; End prompt

; 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 Prompt Placeholder===
.align 16
PromptHolder:
.fill 0x1024,0

;===Item Strings===
.align 32
NullString:
.asciiz " No item"
.align 32
ItemStrings:
.asciiz " Mushroom"
.align 32
.asciiz " Skeleton Key"
.align 32
.asciiz " Plunder Chest"
.align 32
.asciiz " Bowser Bomb"
.align 32
.asciiz " Dueling Glove"
.align 32
.asciiz " Warp Block"
.align 32
.asciiz " Golden Mushroom"
.align 32
.asciiz " Boo Bell"
.align 32
.asciiz " Bowser Suit"
.align 32
.asciiz " Magic Lamp"