; NAME: Mushroom Space
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: Boolean|HappeningPoint
; PARAM: Boolean|AllowItemUse

ADDIU SP SP -120
SW RA 116(SP)
SW S0 112(SP)
SW S1 108(SP) 
SW S2 104(SP)
SW S3 100(SP)
SW S4 96(SP) 
SW S5 92(SP) 
SW S6 88(SP)
SW S7 84(SP)
SW FP 80(SP)



; This event MUST be set on a Happening Space as a Landing Event to work.
; Make sure no other event on your board uses the address 0x800CD09D. It will cause issues.
; Set HappeningPoint to "false" if you do not want players to receive a Happening Bonus point.
; Set HappeningPoint to "true" if you want players to receive a Happening Bonus point.
; Set AllowItemUse to "false" to not allow player to use an item on their reroll.
; Set AllowItemUse to "true" to allow player to use an item on their reroll"

JAL GetPlayerStruct ; Struct pointer at V0
LI A0 -1 ; Current Player
MOVE S4 V0 ; Copy to S4

JAL PlaySound
LI A0 0x10E ; Hidden Block sfx index

SW R0 16(SP) ; Parameter 3
SW R0 20(SP) ; Parameter 4
SW R0 24(SP) ; Parameter 5
LI A0 -1; Character image (None)
LUI A1 hi(MushroomSpace_Message)
ADDIU A1 A1 lo(MushroomSpace_Message)
LI A2 0 ; Parameter 1
JAL ShowMessage
LI A3 0 ; Parameter 2

JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP ; These 3 close the message

JAL RNGPercentChance ; 1 if Mushroom, 0 if Poison Mushroom
LI A0 60 ; 60% Mushroom

BEQZ V0 poisonmushroom ; Jump to poisonmushroom if 0
NOP
; It's 1, continue to mushroom

mushroom:
JAL PlaySound
LI A0 0x0C ; DING! sfx index

LBU A0 29(S4) ; Current Player Index
JAL SetBoardPlayerAnimationWithSound
LI A1 0 ; Happy

LI T0 0x800FD670 ; Instruction that checks board index
SW R0 0(T0) ; Delete
LI T0 0x800FE314 ; Instruction that checks board index
SW R0 0(T0) ; Delete
; This forces the game to think it's on Woody Woods, allowing reroll

LI T0 0x800CD09D ; Reroll flag address (MAKE SURE NO OTHER EVENT USES THIS ADDRESS)
LI T1 1 
SB T1 0(T0) ; Store Flag

LI T0 0x800CD0A9 ; Flag for "Player Used Item"
LI T2 AllowItemUse ; 0 for Do not allow - 1 for Allow
BEQZ T2 itemuseflag ; If 0, do not allow item use
LI T1 1 ; Mark as "Already used item"

LI T1 0 ; Mark as "Has not used an item"
itemuseflag:
SB T1 0(T0) ; Store flag value

J happeningpoint
NOP

poisonmushroom:
JAL PlaySound
LI A0 0x0D ; Poison Mushroom sfx index

LBU A0 29(S4) ; Current Player Index
JAL SetBoardPlayerAnimationWithSound
LI A1 1 ; Sad

SW R0 16(SP) ; Parameter 3
SW R0 20(SP) ; Parameter 4
SW R0 24(SP) ; Parameter 5
LI A0 -1; Character image (None)
LUI A1 hi(PoisonMushroom_Message)
ADDIU A1 A1 lo(PoisonMushroom_Message)
LI A2 0 ; Parameter 1
JAL ShowMessage
LI A3 0 ; Parameter 2

JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP ; These 3 close the message

LI T0 0x800CD0AF ; Cursed Dice Flag Address
LBU T1 0(T0) ; Load Flag Value
LBU T4 29(S4) ; Current Player Index
LI T3 1 ; bit flag
SLLV T5 T3 T4 ; Shift the bit flag to the left * Player Index

AND T6 T5 T1 ; T6 = 0 if the player is not poisoned

BNEZ T6 happeningpoint ; exit if the flag is already on
NOP
; They're not poisoned, continue

ADDU T1 T1 T5 ; Add player bit flag to current flag to poison them
SB T1 0(T0) ; Store new flag 

LBU T1 23(S4) ; Player Status flag value
LI T0 2 ; Poison Mushroom icon
ADDU T1 T1 T0 ; Add Poison Mushroom icon
SB T1 23(S4) ; Store new Flag



happeningpoint:
LI T0 HappeningPoint ; 0 if No, 1 is Yes

BNEZ T0 exit ; exit if Happening Point is allowed
NOP
; The creator marked no, remove Happening Bonus point

LBU T0 44(S4) ; Happening Spaces Landed On
ADDIU T0 T0 -1 ; Subtract 1 to remove Happening point
SB T0 44(S4) ; Store new value

exit:
LW S0 112(SP)
LW S1 108(SP) 
LW S2 104(SP)
LW S3 100(SP)
LW S4 96(SP) 
LW S5 92(SP) 
LW S6 88(SP)
LW S7 84(SP)
LW FP 80(SP)
LW RA 116(SP)
JR RA
ADDIU SP SP 120



; A0 = Player's Index
; A1 = 0 = Happy / 1 = Sad Index
.align 16
SetBoardPlayerAnimationWithSound:
ADDIU SP SP -80
SW RA 76(SP)
SW A0 72(SP)
SW A1 68(SP)

JAL GetPlayerStruct ; Struct Pointer at V0
NOP

LBU T0 3(V0) ; Character Index

BNEZ A1 sad
NOP

happy:
JAL PlaySound
ADDIU A0 T0 0x275 ; Mario's Happy Voice Index + Current Character Index

LW A0 72(SP) ; Player Index
LI A1 5 ; Happy Animation
JAL SetBoardPlayerAnimation
LI A2 0 ; Don't loop

JAL SleepProcess
LI A0 30 ; Frames

J SetBoardAnimationWithSound_exit
NOP

sad:
JAL PlaySound
ADDIU A0 T0 0x287 ; Mario's Sad Voice Index + Current Character Index

LW A0 72(SP) ; Player Index
LI A1 3 ; Despair Animation
JAL SetBoardPlayerAnimation
LI A2 0 ; Don't loop

JAL SleepProcess
LI A0 60 ; Frames

SetBoardAnimationWithSound_exit:
LW A0 72(SP) ; Player Index
LI A1 -1 ; Despair Animation
JAL SetBoardPlayerAnimation
LI A2 2 ; Loop

LW A1 68(SP)
LW A0 72(SP)
LW RA 76(SP)
JR RA
ADDIU SP SP 80


.align 16
MushroomSpace_Message:
.ascii "Welcome to the Mushroom Space\xC2\x0A"
.ascii "Let\x5Cs see what you get\xC2\xFF\x00"

.align 16
PoisonMushroom_Message:
.ascii "A \x03Poison Mushroom\x08 will limit your\x0A"
.ascii "move on your next turn to three\x0A"
.ascii "spaces or less\x85\xFF\x00"