; NAME: Bowser Takes Star or Coins (MP3)
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: Space|BowserSpace
; PARAM: +Number|CoinsToLose

; This code will cause Bowser to steal a Star from the current player.
; If the player does not have a Star, they will lose coins instead.
; The number of coins lost is decided by the parameter CoinsToLose.

ADDIU SP SP -48
SW RA 44(SP)
SW S0 40(SP)
SW S1 36(SP)
SW S2 32(SP)

; S0 = Current Player Struct
; S1 = Current Player Index
; S2 = CoinsToLose

;===Set Player Idle Animation===
LI A0 -1 ; Set current player
LI A1 -1 ; Set idle animation
JAL SetBoardPlayerAnimation
LI A2 2 ; Loop the animation

;===Rotate the player===
LI A0 -1 ; Rotate current player
LI A1 8 ; Take 8 frames to rotate
JAL 0x800ED20C ; Rotate Player Function
LI A2 BowserSpace ; Space to rotate towards, this parameter should be
; the space the Bowser model is standing on.

;===Bowser's Star Steal Message===
JAL PlaySound
LI A0 0x29D ; Bowser roar

SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 0x4 ; Character image (-1 for none, 0x4 for Bowser)
; Visit the following link to get the full list of Character Images:
; https://github.com/PartyPlanner64/PartyPlanner64/wiki/Displaying-Messages
; If you use a character image, check the bottom of the code
LUI A1 hi(StarStealMessage)
ADDIU A1 A1 lo(StarStealMessage) ; Load StarStealMessage into A1
LI A2 0
JAL ShowMessage
LI A3 0

JAL 0x800EC9DC
NOP
JAL CloseMessage
NOP
JAL 0x800EC6EC
NOP

;===Check Player's Star Count===
JAL GetPlayerStruct
LI A0 -1 ; Get current player struct at V0

MOVE S0 V0 ; Copy V0 to S0
LBU T0 0xE(S0) ; Load Stars address from offset of player struct
BEQZ T0 LoseCoins ; If no Stars (T0 = 0) branch to LoseCoins
NOP

;===Lose Star===
ADDIU T0 T0 -1 ; Subtract 1 Star
SB T0 0xE(S0) ; Store new Star count into player Stars address

JAL PlayMusic
LI A0 0x71 ; Play sad music

;====Play Current Player Sad Voice====
LBU T0 3(S0) ; Load character value from offset of player struct
JAL PlaySound
ADDIU A0 T0 0x287 ; Add character value to Mario's sad voice
; Character voices are their "character value" distance away from
; Mario's, e.g. Luigi's character value = 1, so Luigi's happy voice
; is 1 away from Mario's.
; 0x2BE = Happy Voice, 0x287 = Sad Voice

; Visit the following link to get SFX Indexes:
; https://pastebin.com/H1jxNFqr

;====Set Despair Animation====
LI A0 -1 ; Set current player
LI A1 3 ; Set despair animation
JAL SetBoardPlayerAnimation
LI A2 0 ; Do not loop animation

JAL SleepProcess
LI A0 110 ; Wait 110 frames to play animation

JAL GetBoardAudioIndex ; Get original board music at V0
NOP

JAL PlayMusic ; Play original board music
MOVE A0 V0

J exit ; Jump to exit
NOP

LoseCoins:
;===Prepare Coin String to Display in Message===
LUI A0 hi(CoinString)
ADDIU A0 A0 lo(CoinString) ; Saves # of coins to CoinString
LUI A1 hi(percent_d)
ADDIU A1 A1 lo(percent_d)
JAL sprintf ; Converts a number to a text string
LI A2 CoinsToLose ; Converts the number of coins to a string

;===Bowser's Lose Coins Message===
JAL PlaySound
LI A0 0x29D ; Bowser roar

SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 0x4 ; Character image (-1 for none, 0x4 for Bowser)
; Visit the following link to get the full list of Character Images:
; https://github.com/PartyPlanner64/PartyPlanner64/wiki/Displaying-Messages
; If you use a character image, check the bottom of the code
LUI A1 hi(LoseCoinsMessage)
ADDIU A1 A1 lo(LoseCoinsMessage) ; Load StarStealMessage into A1
LUI A2 hi(CoinString)
ADDIU A2 A2 lo(CoinString) ; Load CoinString into A2 (string #1)
JAL ShowMessage
LI A3 0

JAL 0x800EC9DC
NOP
JAL CloseMessage
NOP
JAL 0x800EC6EC
NOP

;===Get Current Player Index===
JAL GetCurrentPlayerIndex ; Get current player index at V0
NOP
MOVE S1 V0 ; Copy V0 to S1
LI S2 CoinsToLose ; Store CoinsToLose in S2

;===Lose Coins===
MOVE A0 S1 ; Set current player
JAL AdjustPlayerCoinsGradual
SUBU A1 R0 S2 ; Make player lose coins

MOVE A0 S1 ; Set current player
JAL ShowPlayerCoinChange
SUBU A1 R0 S2 ; Show coin change HUD

;====Play Current Player Sad Voice====
LBU T0 3(S0) ; Load character value from offset of player struct
JAL PlaySound
ADDIU A0 T0 0x287 ; Add character value to Mario's sad voice
; Character voices are their "character value" distance away from
; Mario's, e.g. Luigi's character value = 1, so Luigi's happy voice
; is 1 away from Mario's.
; 0x2BE = Happy Voice, 0x287 = Sad Voice

; Visit the following link to get SFX Indexes:
; https://pastebin.com/H1jxNFqr

;====Set Despair Animation====
LI A0 -1 ; Set current player
LI A1 3 ; Set despair animation
JAL SetBoardPlayerAnimation
LI A2 0 ; Do not loop animation

JAL SleepProcess
LI A0 60 ; Wait 60 frames to play animation

exit:
LW S0 40(SP)
LW S1 36(SP)
LW S2 32(SP)
LW RA 44(SP)
JR RA
ADDIU SP SP 48

;===Prep for Displaying Coin String===
percent_d:
.asciiz "%d" ; 0x25640000

CoinString:
.fill 8

;===Message Text===
.align 16
StarStealMessage:
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "Thanks for visiting your resident"
.byte 0x03 ; Red font
.ascii " Bowser"
.byte 0x08 ; White font
.byte 0xC2 ; Exclamation mark (!)
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "I"
.byte 0x5C ; Apostrophe (')
.ascii "ll just help myself to a"
.byte 0x03 ; Red font
.ascii " Star"
.byte 0x08 ; White font
.byte 0xC2 ; Exclamation mark (!)
.byte 0xFF,0 ; Wait, press A to close message

.align 16
LoseCoinsMessage:
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "You don"
.byte 0x5C ; Apostrophe (')
.ascii "t have a"
.byte 0x03 ; Red font
.ascii " Star"
.byte 0x08 ; White font
.byte 0xC3 ; Question mark (?)
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "Very well"
.byte 0x82 ; Comma (,)
.ascii " then hand over "
.byte 0x03 ; Red font
.byte 0x11 ; String #1 stored in A2 (CoinString)
.ascii " coins"
.byte 0x08 ; White font
.byte 0xC2 ; Exclamation mark (!)
.byte 0xFF,0 ; Wait, press A to close message

; Here's a list of the most common bytes you'll need
; .byte 0x01 ; Black Font
; .byte 0x03 ; Red Font
; .byte 0x04 ; Purple Font
; .byte 0x05 ; Green Font
; .byte 0x06 ; Blue font
; .byte 0x07 ; Yellow Font
; .byte 0x08 ; White Font
; .byte 0x85 ; Period (.)
; .byte 0xC2 ; Exclamation Mark (!)
; .byte 0xC3 ; Question Mark (?)
; .byte 0x82 ; Comma (,)
; .byte 0x0A ; New Line (Writes Below)
; .byte 0x5C ; Apostrophe (')
; .byte 0x29 ; Coin icon
; .byte 0x3D ; - (minus)
; .byte 0x3E ; x (multiply)
; .byte 0xFF,0 ; FF=Pause

; If your message has an image, use this at the start of each line
; .byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image