; NAME: Koopa Bank Ruleset
; GAMES: MP3_USA
; EXECUTION: Direct

ADDIU SP SP -80
SW RA 76(SP)
SW S1 72(SP) ;holds the fee
SW S2 68(SP) ;holds test values
SW S3 64(SP) ;holds the addresses values
SW S4 60(SP) ;holds current player index

;====================================================================
; Made by Spongyoshi, feel free to use for your own boards!!
; Part of the last five turns custom event, put it on every bank space
; Adds an additional fee on every bank pass and gives it to bank total
; Thanks to MarioComix and the Mario Party Legacy Discord as always!!
;===================================================================
;TESTING BEGIN;
JAL GetCurrentPlayerIndex
NOP
MOVE S4 V0 ; Player index into S4
LUI S3 hi(0x800CD0A1)
ADDIU S3 S3 lo(0x800CD0A1) ; Loading ram address
LBU S3 0(S3)
LI S2 3
BEQ S3 S2 Start ; If rule 3 is active then begin
NOP
J exit ;else, exit
NOP
;TESTING END;
;CHECK IF PLAYER HAS COINS BEGIN;
Start:
MOVE A0 S4 ; A0 = The player's index 
JAL PlayerHasCoins ; Checks if Player has that ammount of Coins
LI A1 1 ; A1 = The ammount of Coins to check for
BEQ V0 R0 exit ;if not, skip the window
NOP
;CHECK IF PLAYER HAS COINS END;
;GET RANK BEGIN;
JAL GetCurrentPlayerIndex
NOP
MOVE A0 V0
JAL GetPlayerPlacement
NOP
MOVE S3 V0
LI S2 0
BEQ S3 S2 FirstPayement ; If rank is first, then 1st Place Payout
NOP
LI S2 1
BEQ S3 S2 SecondPayement ; If rank is second, then 2nd Place Payout
NOP
LI S2 2
BEQ S3 S2 ThirdPayement ; If rank is third, then 3rd Place Payout
NOP
J exit ;else, exit
NOP
;GET RANK END;
;GET PRICE BEGIN;
FullPayement:
JAL GetPlayerStruct
MOVE A0 S4 ; get the struct of the opponent being checked at V0
LHU S1 0xA(V0) ; load into S1 the opponent's coins, using offset of their struct
J WindowDisplayFinal
NOP
FirstPayement:
LI S1 9 ; Fee for first place
J WindowDisplay
NOP
SecondPayement:
LI S1 6 ; Fee for second place
J WindowDisplay
NOP
ThirdPayement:
LI S1 3 ; Fee for third place
;GET PRICE END;
WindowDisplay:
JAL PlaySound ; Play Koopa Sound
LI A0 0x25A
;===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
;CHECKING IF ENOUGH COINS BEGIN;
MOVE A0 S4 ; A0 = The player's index 
JAL PlayerHasCoins ; Checks if Player has that ammount of Coins
MOVE A1 S1 ; A1 = The ammount of Coins to check for
BEQ V0 R0 FullPayement ;if not, get all the coins
NOP
;CHECKING IF ENOUGH COINS END;
;CONVERT TO STRING BEGIN;
WindowDisplayFinal:
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
MOVE A2 S1 ; Converts the payout number of coins to a string
;CONVERT TO STRING END;
;WINDOW DISPLAY BEGIN;
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 0x2 ; Character image (Bank Koopa)
LUI A1 hi(KoopaMessage)
ADDIU A1 A1 lo(KoopaMessage)
LUI A2 hi(CoinString)
ADDIU A2 A2 lo(CoinString)
JAL ShowMessage
LI A3 0
JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP
;WINDOW DISPLAY END;
;GIVE TO BANK BEGIN;
LUI S3 hi(bank_coin_total)
ADDIU S3 S3 lo(bank_coin_total) ; Loading ram address
LHU S2 0(S3) ; Load address value
ADDU S2 S2 S1 ; add the new fee
SH S2 0(S3); and make it the new bank coin total
;GIVE TO BANK END;
;PAYOUT BEGIN;
SUBU S1 R0 S1
JAL GetCurrentPlayerIndex
NOP
ADDIU A0 V0 0
JAL AdjustPlayerCoinsGradual ; Give the player the coins
MOVE A1 S1
JAL GetCurrentPlayerIndex
NOP
ADDIU A0 V0 0
JAL ShowPlayerCoinChange ; Display the coin change
MOVE A1 S1 
JAL SleepProcess ; Sleep for 30 frames
ADDIU A0 R0 30
;PAYOUT END;
exit:
LW RA 76(SP)
LW S1 72(SP)
LW S2 68(SP)
LW S3 64(SP)
LW S4 60(SP)
JR RA
ADDIU SP SP 80

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

CoinString:
.fill 8

.align 16
KoopaMessage:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Following our newly added policies"
.byte 0x82 ; Comma (,)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for picture
.ascii "an added fee of "
.byte 0x29 ; Coin icon
.byte 0x3E ; x (multiply)
.ascii " "
.byte 0x11 ; Price
.ascii " is now"
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for picture
.ascii "required to access your account"
.byte 0x85 ; Period (.)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for picture
.ascii "Thank you for your understanding"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause