; NAME: Mr. Blizzard's Blizzard
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: Space|SpaceDirection
; PARAM: Boolean|Rubberbanding

ADDIU SP SP -80
SW RA 76(SP)
SW S1 72(SP) ;holds rubberbanding variable
SW S2 68(SP) ;used to store various values
SW S0 64(SP) ;used to store various values

;===============================================================
;Code by Spongyoshi, feel free to use on your own boards!
;The snow will either take or give 5 coins to your opponents
;The fate of your opponent depends of the Rubberbanding value
;If Rubberbanding is True, they will win coins if they're poor
;If Rubberbanding is False, they have a 1/3 chance to win coins
;Thanks to Airsola and the Mario Party Legacy Discord for their help!
;===============================================================

JAL PlaySound ; Play Weird Voice Sound
LI A0 0x146

;===Set Player Idle Animation===
LI A0 -1 ; Load Current Player Index
LI A1 -1 ; Set player's idle animation
JAL SetBoardPlayerAnimation
LI A2 2 ; Loop the animation

;===Rotate Player Model Towards Space===
LI A0 -1 ; Load Current Player Index
LI A1 10 ; Frames it takes to turn
JAL 0x800ED20C ; RotatePlayerModel function
LI A2 SpaceDirection ; Space Index to face towards

;FIRST WINDOW=========================================================
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 0x37 ; Character image (Giant Mr. Blizzard)
LUI A1 hi(Blizzard_Start)
ADDIU A1 A1 lo(Blizzard_Start)

ADDU A2 R0 R0
JAL 0x800EC8EC ; ShowMessage
ADDU A3 R0 R0

JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP

LI A0 0x1EB ; Wind Sound
JAL PlaySound ; Play the Wind Sound
NOP

JAL SleepProcess ; Sleeps for 60 frames
ADDIU A0 R0 60
;FIRST WINDOW END=====================================================

;CHECK FOR RUBBERBAND=================================================
LI S1 Rubberbanding
BNE S1 R0 TestTest ; if yes, do the checks
NOP ; If not, randomly pick
;CHECK FOR RUBBERBAND END=============================================

;COMPLETLY RANDOM PICK===============================================
JAL GetRandomByte ; Random Int at V0
NOP

SLTI S0 V0 175 ; Is the number less than 175?
BNE S0 R0 Spiny_Rain
NOP

J Coin_Rain
NOP
;COMPLETLY RANDOM PICK END===========================================

;CHECKS MISSFIRE1==============================================
TestTest:
JAL GetRandomByte ; Random Int at V0
NOP

SLTI S0 V0 80 ; Is the number less than 80?
BNE S0 R0 LeadingTest
NOP

SLTI S0 V0 160 ; Is the number less than 160?
BNE S0 R0 TotalCoinTest
NOP

SLTI S0 V0 240 ; Is the number less than 240?
BNE S0 R0 SingleCoinTest
NOP

J LoserTest
NOP
;CHECKS MISSFIRE1 END==========================================

;CHECKS MISSFIRE2==============================================
;Check if first place
LeadingTest:
JAL GetCurrentPlayerStarCount
NOP
MOVE S2 V0 ; S2 = V0
JAL GetFirstPlaceStarCount
NOP
BEQ S2 V0 Coin_Rain ;Does current player have the same number of stars as first place?
NOP
J Spiny_Rain
NOP
;Check if the three other players have less than 40 coins
TotalCoinTest:
LI S2 40;
JAL GetCurrentPlayerCoinCount
NOP
ADDU S2 V0 S2 ; Add V0 to the value
LI S1 0;

LUI T2 hi(p1_coins)
ADDIU T2 T2 lo(p1_coins) ; Loading P1 coins
LHU T2 0(T2)
ADDU S1 T2 S1 ; Add T2 to the value

LUI T2 hi(p2_coins)
ADDIU T2 T2 lo(p2_coins) ; Loading P2 coins
LHU T2 0(T2)
ADDU S1 T2 S1 ; Add T2 to the value

LUI T2 hi(p3_coins)
ADDIU T2 T2 lo(p3_coins) ; Loading P3 coins
LHU T2 0(T2)
ADDU S1 T2 S1 ; Add T2 to the value

LUI T2 hi(p4_coins)
ADDIU T2 T2 lo(p4_coins) ; Loading P4 coins
LHU T2 0(T2)
ADDU S1 T2 S1 ; Add T2 to the value

SLT S0 S2 S1 ; Are opponents coins higher than 40?
NOP
BNE S0 R0 Spiny_Rain
NOP

J Coin_Rain
NOP
;Check if everyone has less coins than current player
SingleCoinTest:
JAL GetCurrentPlayerCoinCount
NOP
MOVE S1 V0 ; S1 = V0

LUI S2 hi(p1_coins)
ADDIU S2 S2 lo(p1_coins) ; Loading P1 coins
LHU S2 0(S2)
LI T2 0x2;
ADDU S2 T2 S2 ; Add T2 to the value

SLT S0 S1 S2 ; Is P1 coins higher than current player?
NOP
BNE S0 R0 Spiny_Rain
NOP

LUI S2 hi(p2_coins)
ADDIU S2 S2 lo(p2_coins) ; Loading P2 coins
LHU S2 0(S2)
LI T2 0x2;
ADDU S2 T2 S2 ; Add T2 to the value

SLT S0 S1 S2 ; Is P2 coins higher than current player?
NOP
BNE S0 R0 Spiny_Rain
NOP

LUI S2 hi(p3_coins)
ADDIU S2 S2 lo(p3_coins) ; Loading P3 coins
LHU S2 0(S2)
LI T2 0x2;
ADDU S2 T2 S2 ; Add T2 to the value

SLT S0 S1 S2 ; Is P3 coins higher than current player?
NOP
BNE S0 R0 Spiny_Rain
NOP

LUI S2 hi(p4_coins)
ADDIU S2 S2 lo(p4_coins) ; Loading P4 coins
LHU S2 0(S2)
LI T2 0x2;
ADDU S2 T2 S2 ; Add T2 to the value

SLT S0 S1 S2 ; Is P4 coins higher than current player?
NOP
BNE S0 R0 Spiny_Rain
NOP

J Coin_Rain
NOP
;Check if player in last place
LoserTest:
JAL GetCurrentPlayerIndex
NOP
JAL GetPlayerPlacement
MOVE A0 V0
LI S2 3
BEQ V0 S2 Spiny_Rain ;Does current player have a rank of 3? (last)
NOP
J Coin_Rain
NOP
;CHECKS MISSFIRE2 END==========================================

;OPPONENT WIN COINS START------------------------------------------
Coin_Rain:
LI S0 0x0 ; Begin with Player 1

RESTART:
JAL GetCurrentPlayerIndex ; Current Player Index at V0
NOP

LI T0 0x4 ; If all Players have been given coins, stop
BEQ S0 T0 rewait
NOP

BEQ V0 S0 renextplayer ; If the current player is S0, skip them
NOP

MOVE A0 S0 ; A0 = S0
JAL AdjustPlayerCoinsGradual ; Changes player's coin amount
ADDIU A1 R0 5
MOVE A0 S0 ; A0 = S0
JAL 0x800E1F28 ; ShowPlayerCoinChangeHUD
LI A1 5

renextplayer:
ADDIU S0 S0 1 ; Add 1 to S0 to check next player
J RESTART
NOP

rewait:
; Sleep for 30 frames to let coin change take effect
JAL SleepProcess
ADDIU A0 R0 30

LUI A1 hi(Blizzard_Miss)
ADDIU A1 A1 lo(Blizzard_Miss)

J Window_Exit
NOP
;OPPONENT WIN COINS END--------------------------------------------

;OPPONENT LOOSE COINS START------------------------------------------
Spiny_Rain:
LI S0 0x0 ; Begin with Player 1

START:
JAL GetCurrentPlayerIndex ; Current Player Index at V0
NOP

LI T0 0x4 ; If all Players have been given coins, stop
BEQ S0 T0 wait
NOP

BEQ V0 S0 nextplayer ; If the current player is S0, skip them
NOP

MOVE A0 S0 ; A0 = S0
JAL AdjustPlayerCoinsGradual ; Changes player's coin amount
ADDIU A1 R0 -5
MOVE A0 S0 ; A0 = S0
JAL 0x800E1F28 ; ShowPlayerCoinChangeHUD
LI A1 -5

nextplayer:
ADDIU S0 S0 1 ; Add 1 to S0 to check next player
J START
NOP

wait:
; Sleep for 30 frames to let coin change take effect
JAL SleepProcess
ADDIU A0 R0 30

LUI A1 hi(Blizzard_Hit)
ADDIU A1 A1 lo(Blizzard_Hit)
;OPPONENT LOOSE COINS END--------------------------------------------
Window_Exit:
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 0x37 ; Character image (Giant Mr. Blizzard)

ADDU A2 R0 R0
JAL ShowMessage
ADDU A3 R0 R0

JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP

exit:

;===Set Player Idle Animation===
LI A0 -1 ; Load Current Player Index
LI A1 -1 ; Set player's idle animation
JAL SetBoardPlayerAnimation
LI A2 2 ; Loop the animation

LW RA 76(SP)
LW S1 72(SP)
LW S2 68(SP)
LW S0 64(SP)
JR RA
ADDIU SP SP 80

.align 16
Blizzard_Start:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "The sky is looking cloudy today"
.byte 0x85, 0x85, 0x85 ; Periods (...)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Don"
.byte 0x5C ; Apostrophe (')
.ascii "t worry"
.byte 0xC2 ; Exclamation Mark (!)
.ascii " I"
.byte 0x5C ; Apostrophe (')
.ascii "ll shelter you from it"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause

.align 16
Blizzard_Miss:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Oops"
.byte 0xC2 ; Exclamation Mark (!)
.ascii " False alarm"
.byte 0x85, 0x85, 0x85 ; Periods (...)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "It was just a bunch of coins"
.byte 0x85, 0x85, 0x85 ; Periods (...)
.byte 0xFF,0 ; FF=Pause

.align 16
Blizzard_Hit:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Phew"
.byte 0x82 ; Comma (,)
.ascii " It finally stopped"
.byte 0x85, 0x85, 0x85 ; Periods (...)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "I"
.byte 0x5C ; Apostrophe (')
.ascii "m glad you weren"
.byte 0x5C ; Apostrophe (')
.ascii "t hurt"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause

.align 16
GetCurrentPlayerStarCount:
ADDIU SP SP -40
SW RA 36(SP)
JAL GetPlayerStruct
LI A0, -1
LBU V0, 0x000A (v0) ;load current player star count to V0
LW RA 36(SP)
JR RA
ADDIU SP SP 40

.align 16
GetCurrentPlayerCoinCount:
ADDIU SP SP -40
SW RA 36(SP)
JAL GetPlayerStruct
LI A0, -1
LHU V0, 0x0006 (v0) ;load current player coin count to V0
LW RA 36(SP)
JR RA
ADDIU SP SP 40

.align 16
GetFirstPlaceStarCount:
ADDIU SP SP -40
SW RA 36(SP)
LI T0 0x0
;Test if player one is in first place
JAL GetPlayerPlacement
LI A0 0x0
BEQ T0 V0 GetPlayerOneStarCount
NOP
;Test if player two is in first place
JAL GetPlayerPlacement
LI A0 0x1
BEQ T0 V0 GetPlayerTwoStarCount
NOP
;Test if player three is in first place
JAL GetPlayerPlacement
LI A0 0x2
BEQ T0 V0 GetPlayerThreeStarCount
NOP
;Test if player four is in first place
JAL GetPlayerPlacement
LI A0 0x3
BEQ T0 V0 GetPlayerFourStarCount
NOP
;Get player one star count into V0
GetPlayerOneStarCount:
LUI V0 hi(p1_stars)
ADDIU V0 V0 lo(p1_stars)
LBU V0 0(V0)
J starcountend
NOP
;Get player two star count into V0
GetPlayerTwoStarCount:
LUI V0 hi(p2_stars)
ADDIU V0 V0 lo(p2_stars)
LBU V0 0(V0)
J starcountend
NOP
;Get player three star count into V0
GetPlayerThreeStarCount:
LUI V0 hi(p3_stars)
ADDIU V0 V0 lo(p3_stars)
LBU V0 0(V0)
J starcountend
NOP
;Get player four star count into V0
GetPlayerFourStarCount:
LUI V0 hi(p4_stars)
ADDIU V0 V0 lo(p4_stars)
LBU V0 0(V0)
starcountend:
LW RA 36(SP)
JR RA
ADDIU SP SP 40