; NAME: Bowser Star Bank (Landing)
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: Space|CurrentSpace

;====================================================================
; IMPORTANT: This code is specifically designed to take a passing 
; player's star and add it to the bank
;===================================================================

ADDIU SP SP -96
SW RA 76(SP)
SW S1 72(SP) ; Current Player's Star Address
SW S0 64(SP) ; Message to load
SW S4 60(SP) ; Current Player's Indeed
SW S3 56(SP) ; Flag to exit if full
SW S2 52(SP) ; Star Debt
SW S5 88(SP) ; Hold1
SW S6 80(SP) ; Hold2

JAL RNGPercentChance
LI A0 100

BNE V0 R0 hit
NOP
J exit
NOP
hit:

.align 0x10

LI S3 0 ; Exit = False

LI A0 -1 ; Current Player
LI A1 -1 ; Animation Index (Idle)
JAL SetBoardPlayerAnimation
LI A2 2 ; Loop Animation

JAL GetCurrentPlayerIndex ; Current Player's Index at V0
NOP
MOVE S4 V0 ; Copy Current Player Index to S4

LUI S1 hi(p1_stars)
ADDIU S1 S1 lo(p1_stars) ; Load Player 1's Star Address
LI T0 0x38 ; This is the distance between each player's Star Address
MULT S4 T0 ;  Multiply Current Player Index by T0
MFLO T0 ; Move the result to T0
ADDU S1 S1 T0 ; Add result to get current Player's Star Address
LBU T1 0(S1) ; Load address value into T1

;CHECK PLAYER'S STAR COUNT===========================================
LI T2 0 ; Make sure someone with 0 stars is ignored
BEQ T2 T1 TooManyStars
NOP
;CHECK PLAYER'S STAR COUNT===========================================

LUI S2 hi(0x800CD0A0) ; This part is to load star debt
ADDIU S2 S2 lo(0x800CD0A0) ; Load Address of Board-Specific RAM
LBU S5 0(S2) ; Load the Bank Value into S5

LBU S6 0(S2) ;Load bank stars into S6

BEQ T2 S6 exit
NOP

SLT t2, t1, s5 ;if stars in bank > player's stars, T2 = 1
BEQZ t2, Skip
NOP
J BankIsMore

BankIsMore:
LBU S6 0(S1) ; Load player stars into s6

Skip:

LUI A0 hi(coin_string_loc)
ADDIU A0 A0 lo(coin_string_loc)
LUI A1 hi(percent_d)
ADDIU A1 A1 lo(percent_d)
JAL sprintf
MOVE A2 S5 ; Copy Bank Total Value into A2
ADD A3 R0 R0 



LUI S0 hi(GotStar_Message)
ADDIU S0 S0 lo(GotStar_Message)
J MessageSetup
NOP

TooManyStars:
LUI S0 hi(TooManyStars_Message)
ADDIU S0 S0 lo(TooManyStars_Message)
LI S3 1 ; Exit = TRUE


;GOT STAR MESSAGE DISPLAY START--------------------------------------
MessageSetup:
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 -1 ; Character image (-1 for none)
MOVE A1 S0 ; Pass message pointer to A1
LUI A2 hi(coin_string_loc)
ADDIU A2 A2 lo(coin_string_loc)
JAL ShowMessage
LI A3 0x0

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

BNE S3 R0 exit
NOP
;GOT STAR MESSAGE DISPLAY END----------------------------------------

;GIVE STAR START-----------------------------------------------------
LB T2 0(S1) ; Load Player's Star count into S2
SUBU T2 T2 S6  ; Minus their Star count
SB T2 0(S1) ; Insert new Star count into the address
SUBU S5 S5 S6
SB S5 0(S2)

;GIVE STAR END-------------------------------------------------------

;SOUND START---------------------------------------------------------
LI A0 CurrentSpace_chain_index
JAL GetAbsSpaceIndexFromChainSpaceIndex ; Current Space Index at V0
LI A1 CurrentSpace_chain_space_index

LI A0 -1 ; Current Player's Index
LI A1  1 ; Frames it takes to turn
JAL 0x800ED20C ; RotatePlayerModel
MOVE A2 V0 ; Space Index to face towards

LI A0 -1 ; Current Player
LI A1 3 ; Animation Index (Star Get Dance)
JAL SetBoardPlayerAnimation
LI A2 0 ; Loop Animation

JAL PlayMusic
LI A0 113 ; Sad Jingle

.align 4

JAL GetPlayerStruct
LI A0 -1

LBU T0 3(V0)
JAL PlaySound
ADDIU A0 T0 0x287 ; Mario's Despair Index

JAL SleepProcess 
LI A0 110 ; Number of frames the game waits for sound to finish

JAL GetBoardAudioIndex ; Original Board Audio Index at V0
NOP
JAL PlayMusic
MOVE A0 V0
;SOUND END-----------------------------------------------------------


exit:
LW RA 76(SP)
LW S1 72(SP) ; Current Player's Star Address
LW S0 64(SP) ; Message to load
LW S4 60(SP) ; Current Player's Indeed
LW S3 56(SP) ; Flag to exit if full
LW S2 52(SP) ; Star Debt
LW S5 88(SP) ; Hold1
LW S6 80(SP) ; Hold2
JR RA
ADDIU SP SP 96

; .ascii is used to output Text. Anything surrounded by " will be 
; displayed in the message (Special characters are not included).
; For those, you must use .byte and the respective byte value.
; Bytes that change text color will change the color of everything
; that is written after it, until another color is used.

percent_d:
.asciiz "%d" ; 0x25640000

coin_string_loc:
.fill 8


.align 16
GotStar_Message:
.ascii "The debt of "
.byte 0x03 ; Red Font
.byte 0x11 ; Coin Amount String
.byte 0x07 ; Yellow Font
.ascii " Stars "
.byte 0x08 ; White Font
.ascii "is now due"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause
.byte 0xFF,0 ; FF=Pause

.align 16 
TooManyStars_Message:
.ascii "You have no Stars"
.byte 0x0A ; New Line (Writes Below)
.ascii "so you can"
.byte 0x5C ; Apostrophe (')
.ascii "t lose any more"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause

PlaySadVoice:
ADDIU SP SP -32
SW RA 28(SP)

JAL GetCurrentPlayerIndex
NOP 

LUI T1 hi(p1_char)
ADDIU T1 T1 lo(p1_char) ; Load Player 1's Character address
LI T0 0x38 ; Distance between each player's Character address
MULT V0 T0 ;  Multiply Current Player Index by T0
MFLO T0 ; Move the result to T0

ADDU T1 T1 T0 ; Add result to get current Player's Character address
LBU T0 0(T1) ; Load address value into T0

ADDIU V0 T0 0x287 ; Mario's Star Get Voice Index

JAL PlaySound
MOVE A0 V0 ; Pass the Voice Index to A0

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

; 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 picture