; NAME: Bad Luck Space
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: Boolean|Timeable

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)


; Set Timeable to TRUE so that the wheel can be timed
; Set Timeable to FALSE so that the result is random

JAL PlayMusic
LI A0 113 ; Game Guy Lose

JAL PlaySound
LI A0 0x4CF ; Crowd Surprised

LUI T0 hi(total_turns)
ADDIU T0 T0 lo(total_turns)
LBU T1 0(T0) ; Total Turns

LUI T0 hi(current_turn)
ADDIU T0 T0 lo(current_turn)
LBU T6 0(T0) ; Current Turn 

SUBU T3 T1 T6 ; Subtract
SLTI T2 T3 5 ; ON IF 0-4 Last 5 Turns
BEQL T2 R0 badluckspacemessagesetup
LI FP 0 ; Bad Luck Space Event Flag
; Extra Bad Luck Space 
LI FP 1 ; Extra Bad Lucl Space Event Flag

LUI A1 hi(ExtraBadLuckSpace_Message)
ADDIU A1 A1 lo(ExtraBadLuckSpace_Message)
J badluckspacemessage
NOP

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

JAL SleepProcess
LI A0 60

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

JAL PlayMusic
LI A0 33 ; Foolish Bowser Music

JAL GetPlayerStruct
LI A0 -1 
MOVE S4 V0

JAL GetPlayerPlacement ; 0-3 at V0
LBU A0 29(S4) ; Player Index

SLL V0 V0 4 ; Shift bit by 4 

LUI S2 hi(EBL1stPlace_Array)
ADDIU S2 S2 lo(EBL1stPlace_Array)

BNEZ FP getroulettearray ; Take the Extra Bad Luck Array
NOP
; Take regular array

LUI S2 hi(1stPlace_Array)
ADDIU S2 S2 lo(1stPlace_Array)
getroulettearray:
ADDU S2 S2 V0 ; Add to get correct Array

LUI T3 hi(RouletteOutcomes_Array)
ADDIU T3 T3 lo(RouletteOutcomes_Array)
LI T0 0xFF ; No Chest Index
SB T0 0(T3)
SB T0 1(T3)
SB T0 2(T3)
SB T0 3(T3) ; Make all addresses -1
SB T0 4(T3) 

JAL GetRandomByte
NOP

LI T0 5
DIVU V0 T0 ; Divide by 5
MFHI S6 ; 0-4, where the Roulette highlight will begin
SB S6 72(SP) ; Store this value in case they select Random

LI S0 4 ; Loop Counter
outcomeloop:
BLTZ S0 setuproulette ; exit once all roulette slots are filled
NOP

JAL GetRandomByte ; 0-255 at V0
NOP

BNEZL FP outcomeloopextraornormal
LI T0 6 ; 6 Outcomes for Extra Bad Luck Event

LI T0 5 ; 5 Outcomes Bad Luck Event
outcomeloopextraornormal:
DIVU V0 T0
MFHI V0 ; 0-4
ADDU T2 S2 V0 ; Add to get correct Array spot
LBU S1 0(T2) ; Index of option to put on Roulette

loadroulettearray:
LUI T3 hi(RouletteOutcomes_Array)
ADDIU T3 T3 lo(RouletteOutcomes_Array)
MOVE S3 T3 ; Copy to S3
LI S7 4 ; Loop Counter
outcomeloop2:
ADDU T5 T3 S7 ; Add loop counter to get Array spot
LB T4 0(T5) ; Current Outcome Index

BLTZ T4 writeonroulette ; If -1, there's nothing here, store it on roulette
NOP
; There's an outcome written here
BEQ T4 S1 outcomeloop ; If it's the same outcome, reroll
NOP
; It's not the same, check next slot
J outcomeloop2
ADDIU S7 S7 -1 ; Subtract from loop counter

writeonroulette:
SB S1 0(T5) ; Write outcome on Array
J outcomeloop
ADDIU S0 S0 -1 ; Subtract 1 from loop counter


setuproulette:
LUI T0 hi(Outcomes_Strings)
ADDIU T0 T0 lo(Outcomes_Strings)
LBU T2 0(S3) ; Load 1st Outcome Index
SLL T1 T2 5 ; Multiply by 20 for alignment
LBU T2 1(S3) ; Load 2nd Outcome Index
SLL T3 T2 5 ; Multiply by 20 for alignment
LBU T2 2(S3) ; Load 3rd Outcome Index
SLL T5 T2 5 ; Multiply by 20 for alignment
ADDU T5 T5 T0
LBU T2 3(S3) ; Load 4th Outcome Index
SLL T7 T2 5 ; Multiply by 20 for alignment
ADDU T7 T7 T0 
LBU T2 4(S3) ; Load 5th Outcome Index
SLL T9 T2 5 ; Multiply by 20 for alignment
ADDU T9 T9 T0 

LUI A0 hi(A_Pointer_With_Enough_Space) 
ADDIU A0 A0 lo(A_Pointer_With_Enough_Space) ; String destination
LUI A1 hi(Roulette_Prompt) 
ADDIU A1 A1 lo(Roulette_Prompt) ; String destination
ADDU A2 T0 T1 ; 1st Outcome String pointer
ADDU A3 T0 T3 ; 2nd Outcome String pointer
SW T5 16(SP) ; 3rd Outcome String pointer
SW T7 20(SP) ; 4th Outcome String pointer
SW T9 24(SP) ; 5th Outcome String pointer
JAL sprintf
SW R0 28(SP) ; Terminator 

LUI A2 hi(Arg1_Pointer)
ADDIU A2 A2 lo(Arg1_Pointer)
LUI A3 hi(Arg2_Pointer)
ADDIU A3 A3 lo(Arg2_Pointer)
LUI T0 hi(Arg3_Pointer)
ADDIU T0 T0 lo(Arg3_Pointer)
SW T0 16(SP) ; A4
LUI T0 hi(Arg4_Pointer)
ADDIU T0 T0 lo(Arg4_Pointer)
SW T0 20(SP) ; A5
LUI T0 hi(Arg5_Pointer)
ADDIU T0 T0 lo(Arg5_Pointer)
SW T0 24(SP)

LUI T6 hi(Arg1_Pointer)
ADDIU T6 T6 lo(Arg1_Pointer)
SLL T8 S6 4 ; Shift by 4 
ADDU T6 T6 T8 ; Add to get correct Array

LI T1 0x21 ; A button
SB T1 0(T6) ; Insert Roulette Highlight

displayroulette:
LI A0 -1 ; Character image (None)
LUI A1 hi(A_Pointer_With_Enough_Space)
ADDIU A1 A1 lo(A_Pointer_With_Enough_Space)
JAL ShowMessage 
NOP
SW A0 76(SP) ; Window ID


LI S0 0 ; Indicator of whether to move the arrow or not 
refreshloop:
BEQZ S0 checkinput ; If 0 don't update, just check controller input
NOP
; It's 1, move the arrow

LUI S1 hi(Arg1_Pointer)
ADDIU S1 S1 lo(Arg1_Pointer)
SLL T8 S6 4 ; Shift by 4 
ADDU T6 S1 T8 ; Add to get correct Array

LI T1 0x24 ; Nothing
SB T1 0(T6) ; Delete Roulette Highlight

ADDIU S6 S6 1 ; Add 1 to Roulette Highlight Index
LI T0 5 
BEQL S6 T0 updateroulettehighlight
MOVE S6 R0 ; Make it 0 if it's 5
; Do not change the Highlight Index

updateroulettehighlight:
SLL T8 S6 4 ; Shift by 4 
ADDU T6 S1 T8 ; Add to get correct Array

LI T1 0x21 ; A Button
SB T1 0(T6) ; Insert Roulette Highlight

LW A0 76(SP) ; Window ID
LUI A1 hi(A_Pointer_With_Enough_Space)
ADDIU A1 A1 lo(A_Pointer_With_Enough_Space)
LI A2 -1
JAL 0x8005B43C
LI A3 -1

JAL PlaySound 
LI A0 0x104 ; Scrolling through Minigame Roulette Sound Index

checkinput:
JAL PlayerIsCPU ; 1 if CPU
LBU A0 29(S4) ; Player Index

BEQZ V0 checkinputbutactually ; It's a human player, ignore
NOP
; It's  CPU
JAL GetRandomByte
NOP

SLTI T5 V0 4 ; ON IF 0-3
BNEZ T5 exitloop ; If 1, the CPU pressed A
NOP
; They didn't press A
J checkinputsleepframe ; Skip Player controller check
NOP

checkinputbutactually:
JAL DetectABInput
LBU A0 29(S4) ; Player Index

BNEZ V0 exitloop ; If 1, they pressed A
NOP 

checkinputsleepframe:
JAL SleepVProcess ; Sleep 1 Frame
NOP 
ADDIU S0 S0 1 ; Add 1 to loop counter
LI T0 2 ; check to make sure it's not 2
BEQL S0 T0 refreshloop
MOVE S0 R0 ; Make it 0 if it's 2
J refreshloop
NOP

exitloop:
LI T0 Timeable ; 1 if Timeable, 0 if Random

BNEZ T0 exitloop2 ; If it's Timeable, continue
NOP
; They selected Random, change the result
LUI S1 hi(Arg1_Pointer)
ADDIU S1 S1 lo(Arg1_Pointer)
SLL T8 S6 4 ; Shift by 4 
ADDU T6 S1 T8 ; Add to get correct Array

LI T1 0x24 ; Nothing
SB T1 0(T6) ; Delete Roulette Highlight


LB S6 72(SP) ; Load the randomized result


SLL T8 S6 4 ; Shift by 4 
ADDU T6 S1 T8 ; Add to get correct Array

LI T1 0x21 ; A Button
SB T1 0(T6) ; Insert Roulette Highlight

LW A0 76(SP) ; Window ID
LUI A1 hi(A_Pointer_With_Enough_Space)
ADDIU A1 A1 lo(A_Pointer_With_Enough_Space)
LI A2 -1
JAL 0x8005B43C
LI A3 -1

JAL PlaySound 
LI A0 0x104 ; Scrolling through Minigame Roulette Sound Index

exitloop2:
JAL PlaySound 
LI A0 0x4FD ; Scrolling through Minigame Roulette Sound Index

JAL SleepProcess
LI A0 45 ; Frames

LBU A0 76(SP) ; Window ID
JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP

LUI T0 hi(RouletteOutcomes_Array)
ADDIU T0 T0 lo(RouletteOutcomes_Array)
ADDU T0 T0 S6 ; Add Index of Roulette Option
LBU T1 0(T0) ; Load Index Value

BEQZ T1 losecoins ; Outcome 0 is Lose 5 Coins
LI S5 5 ; Coins

LI T0 1
BEQ T0 T1 losecoins ; Outcome 1 is Lose 10 Coins
LI S5 10 ; Coins

LI T0 2
BEQ T0 T1 givecoins ; Outcome 2 is Give 3 coins to rivals -TODO
LI S5 3 ; Coins

LI T0 3
BEQ T0 T1 givecoins ; Outcome 3 is Give 5 coins to rivals -TODO
LI S5 5 ; Coins

LI T0 4
BEQ T0 T1 give4th ; Outcome 4 is Give 5 coins to 4th Place
LI S5 5 ; Coins

LI T0 5
BEQ T0 T1 give4th ; Outcome 5 is Give 10 coins to 4th Place 
LI S5 10 ; Coins

LI T0 6
BEQ T0 T1 giverandom ; Outcome 6 is Give 5 coins to a player
LI S5 5 ; Coins

LI T0 7
BEQ T0 T1 starmoves ; Outcome 7 is The Star Moves
NOP

LI T0 8
BEQ T0 T1 loseitem ; Outcome 8 is Lose 1 Item
NOP

LI T0 9
BEQ T0 T1 losecoins ; Outcome 9 is Lose 20 Coins
LI S5 20 ; Coins

LI T0 10
BEQ T0 T1 givecoins ; Outcome 10 is Give 10 coins to rivals
LI S5 10 ; Coins

LI T0 11
BEQ T0 T1 giverandom ; Outcome 11 is Give 10 coins to a player
LI S5 10 ; Coins

LI T0 12
BEQ T0 T1 getcoins ; Outcome 12 is Get 10 Coins
LI S5 10 ; Coins

LI T0 13
BEQ T0 T1 losehalf ; Outcome 13 is Lose half your Coins
NOP

LI T0 14
BEQ T0 T1 losestar ; Outcome 14 is Lose 1 Star
NOP

LI T0 15
BEQ T0 T1 givestar ; Outcome 15 is Give 1 Star to 4th Place
NOP
; There is no outcome 16, it should never go through here

losecoins:
LBU A0 29(S4) ; Player Index
JAL AdjustPlayerCoinsGradual
SUBU A1 R0 S5 ; Coins to lose

LBU A0 29(S4) ; Player Index
JAL ShowPlayerCoinChange
SUBU A1 R0 S5 ; Coins to lose

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

J exit
NOP

give4th:
LI S0 99 ; Loop Counter
LI S3 3 ; 4th Place
give4thloop:
BLTZL S0 give4thresetloop ; Player in this placing was never found, try another placing
NOP

JAL GetRandomByte
NOP
LI T0 4 ; Players
DIVU V0 T0 
MFHI V0 ; 0-3 

LBU T1 29(S4) ; Current Player Index
BEQ V0 T1 give4thloop ; If it's the current player, roll a new number
NOP
MOVE S7 V0

JAL GetPlayerPlacement ; Placement at V0 (0-3)
MOVE A0 S7 ; Chosen Player Index

BNEL V0 S3 give4thloop
ADDIU S0 S0 -1 ; Subtract 1 to try to find another player
; They're in the correct placing

LBU A0 29(S4) ; Player Index
JAL PlayerHasCoins
MOVE A1 S5 ; Coins

BEQL V0 R0 give4thgivecoins ; Give them all player's coins if they don't have enough
LHU S5 10(S4) ; Current Player's Coins
; They have enough coins

give4thgivecoins:
LBU A0 29(S4) ; Player Index
JAL AdjustPlayerCoinsGradual
SUBU A1 R0 S5 ; Coins to lose

LBU A0 29(S4) ; Player Index
JAL 0x800E1F28 
SUBU A1 R0 S5 ; Coins to lose

MOVE A0 S7 ; Player Index
JAL AdjustPlayerCoinsGradual
MOVE A1 S5 ; Coins to lose

MOVE A0 S7 ; Player Index
JAL 0x800E1F28 
MOVE A1 S5 ; Coins to lose

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

J exit
NOP

give4thresetloop:
ADDIU S3 S3 -1 ; Subtract 1 to find a player in another placing
J give4thloop
LI S0 99 ; Reset Loop Counter

giverandom:
JAL GetRandomByte
NOP
LI T0 4 ; Players
DIVU V0 T0 ; Divide by 4
MFHI V0 ; 0-3

LBU T1 29(S4) ; Current Player Index
BEQ V0 T1 giverandom ; If it's the current player, find another
NOP
MOVE S7 V0

LBU A0 29(S4) ; Player Index
JAL PlayerHasCoins
MOVE A1 S5 ; Coins

BEQL V0 R0 giverandomgivecoins ; Give them all player's coins if they don't have enough
LHU S5 10(S4) ; Current Player's Coins
; They have enough coins

giverandomgivecoins:
LBU A0 29(S4) ; Player Index
JAL AdjustPlayerCoinsGradual
SUBU A1 R0 S5 ; Coins to lose

LBU A0 29(S4) ; Player Index
JAL 0x800E1F28 
SUBU A1 R0 S5 ; Coins to lose

MOVE A0 S7 ; Player Index
JAL AdjustPlayerCoinsGradual
MOVE A1 S5 ; Coins to lose

MOVE A0 S7 ; Player Index
JAL 0x800E1F28 
MOVE A1 S5 ; Coins to lose

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

J exit
NOP

starmoves: 
JAL 0x800EDA58 ; ChangeStarLocation
NOP

J exit
NOP

loseitem:
JAL PlayerHasEmptyItemSlot ; 0 if no items
LBU A0 29(S4) ; Player Index

BEQZ V0 exit ; Exit if they have no items
NOP
; They have at least 1 item
loseitemroll:
JAL GetRandomByte
NOP
LI T0 3 ; Item Slots
DIVU V0 T0 
MFHI V0

ADDIU T4 S4 24 ; Add to get Item Slot #1
ADDU T4 T4 V0 ; Add to get correct Item Slot
LB T1 0(T4) ; Item Slot Value

BLTZ T1 loseitemroll ; Reroll if this slot is empty
NOP
; There's an item here
LI T0 0xFF
SB T0 0(T4) ; Delete Item

JAL PlaySound
LI A0 0x126 ; Item Delete sfx

JAL FixUpPlayerItemSlots
LBU A0 29(S4) ; Player Index

JAL RefreshHUD
LBU A0 29(S4) ; Player Index

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

J exit
NOP

getcoins:
LBU A0 29(S4) ; Player Index
JAL AdjustPlayerCoinsGradual
MOVE A1 S5 ; Coins to gain

LBU A0 29(S4) ; Player Index
JAL ShowPlayerCoinChange
MOVE A1 S5 ; Coins to gain

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

J exit
NOP

losehalf:
LHU T0 10(S4) ; Coins
LI T1 2 
DIVU T0 T1 ; Divide by 2
MFLO S5

LBU A0 29(S4) ; Player Index
JAL AdjustPlayerCoinsGradual
SUBU A1 R0 S5 ; Coins to lose

LBU A0 29(S4) ; Player Index
JAL ShowPlayerCoinChange
SUBU A1 R0 S5 ; Coins to lose

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

J exit
NOP

losestar:
LBU T0 14(S4) ; Stars

BEQZ T0 exit ; Exit if no stars 
NOP

ADDIU T0 T0 -1 ; Subtract 1 Star
SB T0 14(S4) ; Store new Star amount

JAL PlayMusic
LI A0 0x6B ; Game Over 

JAL PlaySound
LI A0 283 ; Star Space Sound Index

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

JAL GetBoardAudioIndex
NOP

JAL PlayMusic
MOVE A0 V0 

J exit
NOP

givestar: 
LBU T0 14(S4) ; Stars

BEQZ T0 exit ; Exit if no stars 
NOP

LI S0 99 ; Loop Counter
LI S3 3 ; 4th Place
givestarloop:
BLTZL S0 givestarresetloop ; Player in this placing was never found, try another placing
NOP

JAL GetRandomByte
NOP
LI T0 4 ; Players
DIVU V0 T0 
MFHI V0 ; 0-3 

LBU T1 29(S4) ; Current Player Index
BEQ V0 T1 givestarloop ; If it's the current player, roll a new number
NOP
MOVE S7 V0

JAL GetPlayerPlacement ; Placement at V0 (0-3)
MOVE A0 S7 ; Chosen Player Index

BNEL V0 S3 givestarloop
ADDIU S0 S0 -1 ; Subtract 1 to try to find another player
; They're in the correct placing

givestargivestar:
LBU T0 14(S4) ; Stars
ADDIU T0 T0 -1 ; Subtract 1 Star
SB T0 14(S4) ; Stars

JAL PlayMusic
LI A0 0x6B ; Game Over 

JAL PlaySound
LI A0 283 ; Star Space Sound Index

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

JAL GetPlayerStruct
MOVE A0 S7 ; Chosen Player Index

LBU T0 14(V0) ; Stars
ADDIU T0 T0 1 ; Add 1 Star
SB T0 14(V0) 

JAL PlaySound
LI A0 283 ; Star Space Sound Index

JAL SleepProcess
LI A0 20 

J exit
NOP

givestarresetloop:
ADDIU S3 S3 -1 ; Subtract 1 to find a player in another placing
J givestarloop
LI S0 99 ; Reset Loop Counter


givecoins:
LI T0 3 
MULT S5 T0
MFLO S7

LBU A0 29(S4) ; Player Index
JAL PlayerHasCoins
MOVE A1 S7 ; Coins to give * 3 
SUBU S7 R0 S7 ; Turn the value negative

BNEZ V0 givecoinssetuploop ; If they have enough coins, continue normally
NOP
; They're missing coins
LHU S7 10(S4) ; Coins

LBU A0 29(S4) ; Player Index
JAL AdjustPlayerCoinsGradual
SUBU A1 R0 S7 ; Coins

LBU A0 29(S4) ; Player Index
JAL 0x800E1F28 ; ShowCoinChangeHUD
SUBU A1 R0 S7 ; Coins

LI S0 0 ; Player Flag
LI S3 3 ; Divide by 3
givecoinsrollindex:
JAL GetRandomByte
NOP
LI T0 4 ; Players
DIVU V0 T0
MFHI V0 ; Player Index 

LBU T0 29(S4) ; Current Player Index
BEQ T0 V0 givecoinsrollindex ; Reroll if it's the turn player
NOP
LI T1 1 ; bit 
SLLV T0 T1 V0 ; Shift by Player Index
AND T2 S0 T0 ; 1 if that player has already been given coins

BNEZ T2 givecoinsrollindex ; Loop if that player has already been given coins
NOP
; that player has not been given coins

ADDU S0 S0 T0 ; Add flag 
DIVU S7 S3 ; Divide by S3
MFLO S5
SUBU S7 S7 S5 ; Subtract from total coins to give

MOVE S6 V0 ; Player Index
MOVE A0 S6
JAL AdjustPlayerCoinsGradual
MOVE A1 S5 ; Amount to give

MOVE A0 S6
JAL 0x800E1F28 ; ShowCoinChangeHUD
MOVE A1 S5 ; Amount to give

BEQZ S7 givecoinsloopexit ; exit once there's no more coins left to give
NOP

J givecoinsrollindex
ADDIU S3 S3 -1 ; Subtract 1 to divide by less 



givecoinssetuploop:
LI S0 3 ; Player 4
givecoinsloop:
BLTZ S0 givecoinsloopexit ; exit once all players are effected
NOP
LBU T0 29(S4) ; Current Player Index

BEQL S0 T0 givecoinsbutactually
MOVE S3 S7 ; Coins to lose
; It's a different player, give them coins
MOVE S3 S5 ; Coins to gain

givecoinsbutactually:
MOVE A0 S0 ; Player Index
JAL AdjustPlayerCoinsGradual
MOVE A1 S3 ; Coins

MOVE A0 S0 ; Player Index
JAL 0x800E1F28 ; ShowCoinChangeHUD
MOVE A1 S3 ; Coins

J givecoinsloop
ADDIU S0 S0 -1 ; Subtract 1 to get next player

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


exit:
JAL GetBoardAudioIndex
NOP

JAL PlayMusic
MOVE A0 V0 ; Return Music to normal



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



.align 16
Arg1_Pointer:
.byte 0x24

.align 16
Arg2_Pointer:
.byte 0x24

.align 16
Arg3_Pointer:
.byte 0x24

.align 16
Arg4_Pointer:
.byte 0x24

.align 16
Arg5_Pointer:
.byte 0x24

.align 16
RouletteOutcomes_Array:
.byte 0xFF,0xFF,0xFF,0xFF,0xFF

.align 16
1stPlace_Array:
.byte 1,3,5,6,8

.align 16
2ndPlace_Array:
.byte 1,3,4,6,8

.align 16
3rdPlace_Array:
.byte 0,2,6,7,8

.align 16
4thPlace_Array:
.byte 0,2,6,7,8

.align 16
EBL1stPlace_Array:
.byte 9,10,11,13,14,15

.align 16
EBL2ndPlace_Array:
.byte 9,10,11,13,14,15

.align 16
EBL3rdPlace_Array:
.byte 9,10,11,12,13,14

.align 16
EBL4thPlace_Array:
.byte 9,10,11,12,13,14

.align 16
BadLuckSpace_Message:
.ascii "\x03B \x03A \x03D  \x03L \x03U \x03C \x03K  \x03S \x03P \x03A \x03C \x03E\x00"

.align 16
ExtraBadLuckSpace_Message:
.ascii "\x01E \x01X \x01T \x01R \x01A  \x03B \x03A \x03D  \x03L \x03U \x03C \x03K  \x03S \x03P \x03A \x03C \x03E\x00"

.align 16
Roulette_Prompt:
.ascii "\x0B\x1A\x1A\x0EPress \x21 to stop the wheel\x0A\x0A"
.ascii "\x1A\x1A\x11\x0E" "%s" "\x0E   \x0E\x0A"
.ascii "\x1A\x1A\x12\x0E" "%s" "\x0E   \x0E\x0A"
.ascii "\x1A\x1A\x13\x0E" "%s" "\x0E   \x0E\x0A"
.ascii "\x1A\x1A\x14\x0E" "%s" "\x0E   \x0E\x0A"
.ascii "\x1A\x1A\x15\x0E" "%s" "\x0E   \x0E\x0A\x00"


.align 16
A_Pointer_With_Enough_Space:
.fill 0x1024

.align 0x20
Outcomes_Strings:
.asciiz "Lose 5 Coins" ; 00
.align 0x20
.asciiz "Lose 10 Coins" ; 01
.align 0x20
.asciiz "Give 3 coins to rivals" ; 02
.align 0x20
.asciiz "Give 5 coins to rivals" ; 03
.align 0x20
.asciiz "Give 5 coins to 4th place" ; 04
.align 0x20
.asciiz "Give 10 coins to 4th place" ; 05
.align 0x20
.asciiz "Give 5 coins to a player" ; 06
.align 0x20
.asciiz "The \x2A Star moves" ; 07
.align 0x20
.asciiz "Lose 1 item" ; 08
.align 0x20
.asciiz "Lose 20 Coins" ; 9
.align 0x20
.asciiz "Give 10 coins to rivals" ; 10
.align 0x20
.asciiz "Give 10 coins to a player" ; 11
.align 0x20
.asciiz "Receive 10 Coins" ; 12
.align 0x20
.asciiz "Lose half your coins" ; 13
.align 0x20
.asciiz "Lose 1 \x2A Star" ; 14
.align 0x20
.asciiz "Give 1 \x2A Star to 4th place" ; 15


.align 0x20
Item_Strings:
.asciiz "Mushroom" ; 00
.align 0x20
.asciiz "Skeleton Key" ; 01
.align 0x20
.asciiz "Poison Mushroom" ; 02
.align 0x20
.asciiz "Reverse Mushroom" ; 03
.align 0x20
.asciiz "Cellular Shopper" ; 04
.align 0x20
.asciiz "Warp Block" ; 05
.align 0x20
.asciiz "Plunder Chest" ; 06
.align 0x20
.asciiz "Bowser Phone" ; 07
.align 0x20
.asciiz "Dueling Glove" ; 08
.align 0x20
.asciiz "Lucky Lamp" ; 09
.align 0x20
.asciiz "Golden Mushroom" ; 10
.align 0x20
.asciiz "Boo Bell" ; 11
.align 0x20
.asciiz "Boo Repellant" ; 12
.align 0x20
.asciiz "Bowser Suit" ; 13
.align 0x20
.asciiz "Magic Lamp" ; 14
.align 0x20
.asciiz "Koopa Kard"
.align 0x20
.asciiz "Barter Box"
.align 0x20
.asciiz "Lucky Charm"
.align 0x20
.asciiz "Wacky Watch"
.align 0x20
.asciiz "Item Bag"

.align 16
DetectABInput:
ADDIU SP SP -20
SW RA 16(SP)

LUI T1 hi(p1_controller)
ADDIU T1 T1 lo(p1_controller) ; Load P1's Controller
LI T0 0x38 ; This is the distance between each player's Controller
MULT A0 T0 ;  Multiply Current Player Index by S3
MFLO T0 ; Move the result to S3

ADDU T1 T1 T0 ; Add result to get Current Player's Controller Addr
LBU T0 0(T1) ; Load address value into A1

LUI T1 hi(0x800C9520)
ADDIU T1 T1 lo(0x800C9520) ; Load P1's Button Press Address
LI A2 0x2 ; This is the distance between each player's Button Address
MULT A2 T0 ;  Multiply Controller value by 2
MFLO T0 ; Move the result to S3

ADDU T1 T1 T0 ; Add result to get Current Player's Y Axis
LBU T0 0(T1) ; Load address value into A1

LI T1 0x80
BEQ T0 T1 APress
NOP

NoPress:
LI V0 0
J DetectABInputexit
NOP

APress:
LI V0 1
J DetectABInputexit
NOP

DetectABInputexit:
LW RA 16(SP)
JR RA
ADDIU SP SP 20

; 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 75 ; 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