; NAME: Setup Item Slots (MP2) v1.4
; GAMES: MP2_USA
; EXECUTION: Direct
; PARAM: Boolean|MultipleItems

ADDIU SP SP -40
SW RA 36(SP)
SW S0 32(SP)

; This code is designed to set up the reserve item slots by writing
; the "no item" index as their value, so that players don't start
; with two Mushrooms each. Set this event as a "Before turn" event.
; Set the parameter "MultipleItems" to "true" to turn this event ON.

; S0 = Player Index

;===Check MultipleItems Parameter===
LI T0 MultipleItems
BEQZ T0 exit ; If MultipleItems is OFF, exit
NOP

;===Check Turn Count===
LUI T0 hi(current_turn)
ADDIU T0 T0 lo(current_turn)
LHU T2 0(T0) ; Load current turn into T0
LI T1 1 ; Check if current turn is turn 1
BNE T2 T1 exit ; If it's not turn 1, exit
NOP
; else, need to setup multiple item slots

;===Turn On Board RAM===
LUI T0 0x800F ; 0x800F0000
ORI T0 T0 0x8CE2 ; 0x800F8CE2, Multiple Item Slots Flag
LI T1 MultipleItems
SB T1 0(T0) ; Turn Multiple Item Slots Flag ON

;===Write No Item Into Reserve Slots===
LI S0 0 ; Start with P1
RewriteLoop:
JAL GetPlayerStruct
MOVE A0 S0 ; Get player struct at V0
LI T1 0xFF ; No item index
SB T1 0x1E(V0) ; Store no item index into reserve slot #1
SB T1 0x1F(V0) ; Store no item index into reserve slot #2
LI T0 3
BEQ S0 T0 exit ; If all players rewritten, exit
NOP
J RewriteLoop
ADDIU S0 S0 1 ; Add 1 to get next player

exit:
LW RA 36(SP)
LW S0 32(SP)
JR RA
ADDIU SP SP 40