; NAME: Burner Plant
; 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!
;Piranha Plant will burn one of your items at random
;If you have no item, they'll miss automatically
;The likelyhood of being burned is depending of Rubberbanding
;If Rubberbanding is True, trailing players will rarely get burned
;If Rubberbanding is False, there's a 1/3 chance to not get burned
;Thanks to Airsola and the Mario Party Legacy Discord for their help!
;===============================================================

JAL PlaySound ; Play Pirhana Plant Sound
LI A0 0x2A4

;===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 0x1E ; Character image (Piranha Plant)
LUI A1 hi(Fire_Plant)
ADDIU A1 A1 lo(Fire_Plant)

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

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

LI A0 0x444 ; Fire Heat-Up Sound
JAL PlaySound ; Play the Fire Heat-Up Sound
NOP

JAL SleepProcess ; Sleeps for 60 frames
ADDIU A0 R0 60

JAL PlayerHasEmptyItemSlot
LI A0 -1 ;current_player_index
LI S2 0
BEQ V0 S2 Missed_Shot ;Ensure miss if player has no items
NOP
;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 Landed_Shot
NOP

J Missed_Shot
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 LeadingPyroTest
NOP

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

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

J LoserPyroTest
NOP
;CHECKS MISSFIRE1 END===========================================

;CHECKS MISSFIRE2===============================================
;Check if first place
LeadingPyroTest:
JAL GetCurrentPlayerStarCount
NOP
MOVE S2 V0 ; S2 = V0
JAL GetFirstPlaceStarCount
NOP
BEQ S2 V0 Landed_Shot ;Does current player have the same number of stars as first place?
NOP
J Missed_Shot
NOP
;Check if 1 or 0 items
ItemPyroTest:
JAL PlayerHasEmptyItemSlot
LI A0 -1 ;current_player_index
LI S2 0
BEQ V0 S2 Missed_Shot ;Does current player has no items?
NOP
LI S2 1
BEQ V0 S2 Missed_Shot ;Does current player has one item?
NOP
J Landed_Shot
NOP
;Check if 20 coins or more
CoinPyroTest:
LI A1 20 ; A1 = The ammount of Coins to check for
JAL PlayerHasCoins ; Checks if Player has that ammount of Coins
LI A0 -1 ;current_player_index
BNE V0 R0 Landed_Shot 
NOP
J Missed_Shot
NOP
;Check if player in last place
LoserPyroTest:
JAL GetCurrentPlayerIndex
NOP
JAL GetPlayerPlacement
MOVE A0 V0
LI S2 3
BEQ V0 S2 Missed_Shot ;Does current player have a rank of 3? (last)
NOP
J Landed_Shot
NOP
;CHECKS MISSFIRE2 END===========================================

;LOOSE ITEMS SELECT START---------------------------------------------
Landed_Shot:
SLTI S0 V0 90 ; Is the number less than 90?
BNE S0 R0 LooseFirstItem
NOP

SLTI S0 V0 180 ; Is the number less than 180?
BNE S0 R0 LooseSecondItem
NOP

J LooseThirdItem
NOP
;LOOSE ITEMS SELECT END-----------------------------------------------

;LOOSE ACTUAL ITEM START----------------------------------------------
LooseFirstItem:
JAL GetPlayerStruct
LI A0 -1 ; This gets the current player's struct address on V0
LI T0 0xFF ; T0 = No item index
SB T0 24(V0) ; We're storing 0xFF into the player's first item slot
J Landed_Shot_Continue
NOP

LooseSecondItem:
JAL PlayerHasEmptyItemSlot
LI A0 -1 ;current_player_index
LI S2 1
BEQ V0 S2 LooseFirstItem ;Does current player has one item?
NOP
;Else keep going
JAL GetPlayerStruct
LI A0 -1 ; This gets the current player's struct address on V0
LI T0 0xFF ; T0 = No item index
SB T0 25(V0) ; We're storing 0xFF into the player's second item slot
J Landed_Shot_Continue
NOP

LooseThirdItem:
JAL PlayerHasEmptyItemSlot
LI A0 -1 ;current_player_index
LI S2 1
BEQ V0 S2 LooseFirstItem ;Does current player has one item?
NOP
LI S2 2
BEQ V0 S2 LooseFirstItem ;Does current player has two items?
NOP
;Else keep going
JAL GetPlayerStruct
LI A0 -1 ; This gets the current player's struct address on V0
LI T0 0xFF ; T0 = No item index
SB T0 26(V0) ; We're storing 0xFF into the player's third item slot
;LOOSE ACTUAL ITEM END------------------------------------------------

;LOOSE ITEMS ANIM START---------------------------------------------------
Landed_Shot_Continue:
LI A0 0x170 ; Fire Hit Sound
JAL PlaySound ; Play the Fire Hit Sound
NOP

JAL SleepProcess ; Sleeps for 5 frames
ADDIU A0 R0 5

LI A0 -1 ; Load Current Player Index
LI A1 04 ; Set player's dizzy animation
JAL SetBoardPlayerAnimation
LI A2 2 ; Loop the animation

JAL FixUpPlayerItemSlots
LI A0 -1 ;puts any empty item slot at the end

JAL RefreshHUD
LI A0 -1 ;refresh current player HUD

;!!!LONG ANIMATION, MIGHT NEED TO OPTIMIZE THIS!!!
JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x41200000 ;value 10 as float
SW t0, 0x0010 (v0) ;store 10 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x41A00000 ;value 20 as float
SW t0, 0x0010 (v0) ;store 20 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x41E00000 ;value 28 as float
SW t0, 0x0010 (v0) ;store 28 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x420C0000 ;value 35 as float
SW t0, 0x0010 (v0) ;store 35 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x42180000 ;value 38 as float
SW t0, 0x0010 (v0) ;store 38 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x42200000 ;value 40 as float
SW t0, 0x0010 (v0) ;store 40 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x42180000 ;value 38 as float
SW t0, 0x0010 (v0) ;store 38 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x420C0000 ;value 35 as float
SW t0, 0x0010 (v0) ;store 35 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x41E00000 ;value 28 as float
SW t0, 0x0010 (v0) ;store 28 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x41A00000 ;value 20 as float
SW t0, 0x0010 (v0) ;store 20 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x41200000 ;value 10 as float
SW t0, 0x0010 (v0) ;store 10 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x00000000 ;value 0 as float
SW t0, 0x0010 (v0) ;store 0 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

LI A0 0x179 ; Land Effect Sound
JAL PlaySound ; Play the Land Effect Sound
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x40000000 ;value 2 as float
SW t0, 0x0010 (v0) ;store 2 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x40800000 ;value 4 as float
SW t0, 0x0010 (v0) ;store 4 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x40A00000 ;value 5 as float
SW t0, 0x0010 (v0) ;store 5 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x40800000 ;value 4 as float
SW t0, 0x0010 (v0) ;store 4 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x40400000 ;value 3 as float
SW t0, 0x0010 (v0) ;store 3 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x40000000 ;value 2 as float
SW t0, 0x0010 (v0) ;store 2 to player y pos

JAL SleepVProcess ;Sleep for one frame
NOP

JAL GetPlayerStruct
LI a0, -1
LW v0, 0x0024 (v0)
LI t0, 0x00000000 ;value 0 as float
SW t0, 0x0010 (v0) ;store 0 to player y pos

JAL SleepProcess ; Sleeps for 20 frames
ADDIU A0 R0 20

LUI A1 hi(Fire_Plant_Hit)
ADDIU A1 A1 lo(Fire_Plant_Hit)

J Window_Exit
NOP
;LOOSE ITEMS ANIM END-----------------------------------------------------

;KEEP ITEMS START----------------------------------------------------
Missed_Shot:
LI A0 0x488 ; Fire Misses Sound
JAL PlaySound ; Play the Fire Misses Sound
NOP

JAL SleepProcess ; Sleeps for 40 frames
ADDIU A0 R0 40

LI A0 -1 ; Load Current Player Index
LI A1 05 ; Set player's happy animation
JAL SetBoardPlayerAnimation
LI A2 0 ; Don't loop the animation

JAL GetPlayerStruct
LI A0 -1
LBU T0 3(V0)
JAL PlaySound
ADDIU A0 T0 0x275 ; Add Mario's Happy Voice Index

JAL SleepProcess ; Sleeps for 30 frames
ADDIU A0 R0 30

LUI A1 hi(Fire_Plant_Miss)
ADDIU A1 A1 lo(Fire_Plant_Miss)

J Window_Exit
NOP
;KEEP ITEMS END------------------------------------------------------
Window_Exit:
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 0x1E ; Character image (Piranha Plant)

ADDU A2 R0 R0
JAL ShowMessage
ADDU A3 R0 R0

JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP
;===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
Fire_Plant:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "GRAWR CHARBLE GRAWR"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.byte 0x5D ; Paranthesis (
.ascii "Hey"
.byte 0x82 ; Comma (,)
.ascii " what"
.byte 0x5C ; Apostrophe (')
.ascii "s cooking"
.byte 0xC3 ; Question Mark (?)
.ascii " YOU ARE" 
.byte 0xC2, 0xC2 ; Exclamation Marks (!!)
.byte 0x5E ; Paranthesis )
.byte 0xFF,0 ; FF=Pause

.align 16
Fire_Plant_Hit:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "GRAWR HARHAR GRAWR"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.byte 0x5D ; Paranthesis (
.ascii "Haw"
.byte 0x82 ; Comma (,)
.ascii " haw"
.byte 0xC2 ; Exclamation Mark (!)
.ascii " Can"
.byte 0x5C ; Apostrophe (')
.ascii "t stand the heat"
.byte 0xC3 ; Question Mark (?)
.byte 0x5E ; Paranthesis )
.byte 0xFF,0 ; FF=Pause

.align 16
Fire_Plant_Miss:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "GRAK GLUB GROK"
.byte 0x85,0x85,0x85 ; Periods (...)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.byte 0x5D ; Paranthesis (
.ascii "Ack"
.byte 0xC2 ; Exclamation Mark (!)
.ascii " I fouled that up"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x5E ; Paranthesis )
.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
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