; NAME: Pay Coins to Make Lose Stars or Coins (MP3) v3
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: +Number|CoinsToPay
; PARAM: +Number|StarsToLose
; PARAM: +Number|CoinsToLose

; This code will force the landing or passing player to pay a number of
; coins to subtract Stars from all other rivals. The number of Stars
; lost is given by the parameter StarsToLose. If the rival does not have
; any Stars, they will lose a number of coins given by the parameter
; CoinsToLose. If the player does not have enough coins to pay, the
; event will not occur.

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

; S0 = Current Player Index
; S1 = CoinsToPay / Other Player Stars Address
; S2 = Other Player Index

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

;===Set Coin String===
LUI A0 hi(CoinString)
ADDIU A0 A0 lo(CoinString) ; Location of converted string
LUI A1 hi(percent_d)
ADDIU A1 A1 lo(percent_d)
JAL sprintf
LI A2 CoinsToPay ; Convert # of CoinsToPay into string

LI S1 CoinsToPay

;===Set Star String===
LUI A0 hi(StarString)
ADDIU A0 A0 lo(StarString) ; Location of converted string
LUI A1 hi(percent_d)
ADDIU A1 A1 lo(percent_d)
JAL sprintf
LI A2 StarsToLose ; Convert # of StarsToLose into string

;===Introduction Message===
LI A0 -1 ; Character image (-1 for none)
; 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(IntroMessage)
ADDIU A1 A1 lo(IntroMessage) ; Load IntroMessage into text box
LUI A2 hi(CoinString)
ADDIU A2 A2 lo(CoinString) ; Store CoinString into String #1
LUI A3 hi(StarString)
JAL CallMessage
ADDIU A3 A3 lo(StarString) ; Store StarString into String #2

;===Check Player Coins===
JAL GetCurrentPlayerIndex
NOP
MOVE S0 V0

MOVE A0 S0
JAL PlayerHasCoins
MOVE A1 S1 ; Check if current player has CoinsToPay

BEQZ V0 NotEnoughCoins ; If not enough coins, branch to exit
NOP

;===Pay Coins===
MOVE A0 S0
JAL AdjustPlayerCoinsGradual
SUBU A1 R0 S1 ; Subtract CoinsToPay from player

MOVE A0 S0
JAL ShowPlayerCoinChange
SUBU A1 R0 S1 ; Show coin change HUD

JAL SleepProcess
LI A0 30 ; Wait 30 frames

;===Fade Out===
LI A0 4 ; Happening (?) Fadeout
JAL InitFadeOut
LI A1 16 ; 16 frames of fadeout

JAL SleepProcess
LI A0 16 ; Wait 16 frames

;===Check Player Stars===
LI S2 0 ; Begin with Player 1

LUI S1 hi(p1_stars)
ADDIU S1 S1 lo(p1_stars) ; S1 is the address of Player 1's Stars

CheckPlayers:
LI T0 4
BEQ S2 T0 FinishLoop ; If all Players have been given coins, stop
NOP

BEQ S2 S0 NextPlayer ; If the current player is S2, skip them
NOP

;===Play Sad Voice===
JAL GetPlayerStruct
MOVE A0 S2 ; Get other player struct

LBU T0 3(V0) ; 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, 0x263 = Get Star Voice

JAL SleepProcess
LI A0 15 ; Wait 15 frames

;===Subtract Stars===
LB T2 0(S1) ; Load Player's Star count into T2
BEQZ T2 LoseCoins ; If player has 0 Stars, lose coins
LI T1 StarsToLose
SUBU T2 T2 T1 ; Subtract StarsToLose from their Star count
BLTZ T2 SetZero ; If Star count is less than zero, branch
NOP
SB T2 0(S1) ; Insert new Star count into the address

NextPlayer:
ADDIU S2 S2 1 ; Add 1 to check the next Player
J CheckPlayers
ADDIU S1 S1 0x38 ; This makes it load the next Player's Stars

SetZero:
J NextPlayer
SB R0 0(S1) ; Write zero into Stars address

LoseCoins:
MOVE A0 S2
LI T3 CoinsToLose
JAL AdjustPlayerCoins
SUBU A1 R0 T3 ; Subtract CoinsToLose
J NextPlayer
NOP

FinishLoop:
JAL PlaySound
LI A0 0x122 ; "Shrinking" sound
; Use the Audio Player on PP64 to find other sound indexes to use
; Then use the following link to know what Sound Index you need
; https://pastebin.com/H1jxNFqr

JAL SleepProcess
LI A0 20 ; Wait 20 frames

LI A0 4 ; Happening (?) Fadein
JAL InitFadeIn
LI A1 16 ; 16 frames of Fadein

JAL SleepProcess
LI A0 16 ; Wait 16 frames

;===Final Message===
LI A0 -1 ; Character image (-1 for none)
; 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(FinalMessage)
ADDIU A1 A1 lo(FinalMessage)
LI A2 0
JAL CallMessage
LI A3 0

J exit
NOP

;===Not Enough Coins Message===
NotEnoughCoins:
LI A0 -1 ; Character image (-1 for none)
; 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(NoCoinsMessage)
ADDIU A1 A1 lo(NoCoinsMessage)
LI A2 0
JAL CallMessage
LI A3 0

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

.align 4
;===Mini Function to Display Messages===
CallMessage:
ADDIU SP SP -32
SW RA 28(SP)

SW R0 16(SP)
SW R0 20(SP)
JAL ShowMessage
SW R0 24(SP)

JAL 0x800EC9DC
NOP
JAL CloseMessage
NOP
JAL 0x800EC6EC
NOP

LW RA 28(SP)
JR RA
ADDIU SP SP 32

;===String Formatters===
percent_d:
.asciiz "%d" ; 0x25640000

CoinString:
.fill 8

StarString:
.fill 8

;===Message Text===
.align 16
IntroMessage:
.ascii "You shall now pay "
.byte 0x03 ; Red Font
.byte 0x11 ; String #1 (CoinsToPay)
.ascii " coins"
.byte 0x0A ; New line (writes below)
.byte 0x08 ; White Font
.ascii "to make others lose "
.byte 0x03 ; Red Font
.byte 0x12 ; String #2 (StarsToLose)
.ascii " Star"
.byte 0x5D ; Left Bracket (
.ascii "s"
.byte 0x5E ; Right Bracket )
.byte 0x08 ; White Font
.ascii " or"
.byte 0x03 ; Red Font
.ascii " coins"
.byte 0x08 ; White Font
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; Wait, press A to close message

.align 16
FinalMessage:
.ascii "The deed has been done"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; Wait, press A to close message

.align 16
NoCoinsMessage:
.ascii "But you don"
.byte 0x5C ; Apostrophe (')
.ascii "t have enough"
.byte 0x03 ; Red Font
.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