; NAME: Game Coin Winner
; GAMES: MP3_USA
; EXECUTION: Direct

ADDIU SP SP -80
SW RA 76(SP)
SW S1 72(SP) ;Total Turn/Biggest Game Coin Gain
SW S2 68(SP) ;Current Turn/Game Coin Winner
SW S0 64(SP) ;Test Value/Loop Value
SW S3 60(SP) ;Game Coin Second Winner
SW S4 56(SP) ;Game Coin Third Winner

;====================================================================
;Code by Spongyoshi, feel free to use in your own boards!
;Only activates on the last turn, put it as an after turn event
;Displays a text that can be validated by every player
;And then gives 99 stars to every player that has the Coin Star
;====================================================================

LUI S1 hi(0x800CD05A)
ADDIU S1 S1 lo(0x800CD05A) ; Loading RAM address of total_turn
LBU S1 0(S1)

LUI S2 hi(0x800CD05B)
ADDIU S2 S2 lo(0x800CD05B) ; Loading RAM address of current_turn
LBU S2 0(S2)

BNE S1 S2 skipping ;If S1 != S2, skip the event
NOP ;Else, it's the last turn, do the event!
LI S1 0
LI S2 4
LI S3 4
LI S4 4

;===Displaying Window Message===
SW R0 16(SP)
SW R0 20(SP)
SW R0 24(SP)
LI T0 0x64 ; 100 Units Y pos
SW T0 28(SP)
LI A0 0x16 ;Millenium Star
LUI A1 hi(Star_Message)
ADDIU A1 A1 lo(Star_Message)
LI A2 0
JAL 0x800EC980 ; ShowMessage2 
LI A3 0
JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP

;===Loop checking for every player===
LI S0 0
loop:
JAL GetPlayerStruct
MOVE A0 S0 ;Get the struct of the currently checked player
LHU T0 0x2A(V0) ;Get the value of Coin Star for that player
BEQ T0 S1 equal ;If the coin star value & the max coin value is the same, skip
NOP ;else continue
SLT T1 T0 S1 ;Check if that player has more coins than the current max
BNE T1 R0 checkloop ;If not, skip to the next loop
NOP ;else

;===Have a new higher value!===
MOVE S1 T0 ;T0 is the new biggest coin star value
MOVE S2 S0 ;S0 is the new coin star winner
LI S3 4 ;Ties are emptied
LI S4 4 ;Ties are emptied
J checkloop ;And loop!
NOP

equal:
LI T0 4
BEQ T0 S2 firstgive ;If S2 is unset, give it the current player value
NOP ;else
BEQ T0 S3 secondgive ;If S3 is unset, give it the current player value
NOP ;else
BEQ T0 S4 thirdgive ;If S4 is unset, give it the current player value
NOP ;else...

;===Give 99 stars to every player because it's a complete tie===
LI S0 99
LUI S1 hi(0x800D1116)
ADDIU S1 S1 lo(0x800D1116) ; Loading P1's stars
SB S0 0(S1) ;Send 99 to it
LUI S1 hi(0x800D114E)
ADDIU S1 S1 lo(0x800D114E) ; Loading P2's stars
SB S0 0(S1) ;Send 99 to it
LUI S1 hi(0x800D1186)
ADDIU S1 S1 lo(0x800D1186) ; Loading P3's stars
SB S0 0(S1) ;Send 99 to it
LUI S1 hi(0x800D11BE)
ADDIU S1 S1 lo(0x800D11BE) ; Loading P4's stars
SB S0 0(S1) ;Send 99 to it
J skipping
NOP

;===Give the value to the single winner===
firstgive:
MOVE S2 S0
J checkloop ;And loop!
NOP

;===Give the value to the second winner===
secondgive:
MOVE S3 S0
J checkloop ;And loop!
NOP

;===Give the value to the third winner===
thirdgive:
MOVE S4 S0
;And loop!
checkloop:
ADDIU S0 S0 1
NOP
SLTI T0 S0 4 ; Is the number less than 4?
BNE T0 R0 loop ;If so, we're below the 5th loop, loop back
NOP ;else, escape

LI T0 4
BEQ T0 S4 thirdwinnerskip
NOP
;===Give 99 stars to the third winner===
LI T0 99
JAL GetPlayerStruct
MOVE A0 S4 ;Get the struct to that player
SB T0 0x0E(V0) ;And send 99 to their star count
LI T0 4

thirdwinnerskip:
BEQ T0 S3 secondwinnerskip
NOP
;===Give 99 stars to the second winner===
LI T0 99
JAL GetPlayerStruct
MOVE A0 S3 ;Get the struct to that player
SB T0 0x0E(V0) ;And send 99 to their star count
LI T0 4

secondwinnerskip:
BEQ T0 S2 skipping
NOP
;===Give 99 stars to the winner===
LI T0 99
JAL GetPlayerStruct
MOVE A0 S2 ;Get the struct to that player
SB T0 0x0E(V0) ;And send 99 to their star count

skipping:
LW RA 76(SP)
LW S1 72(SP)
LW S2 68(SP)
LW S0 64(SP)
LW S3 60(SP)
LW S4 56(SP)
JR RA
ADDIU SP SP 80

.align 16
Star_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "And now"
.byte 0x82 ; Comma (,)
.ascii " I"
.byte 0x5C ; Apostrophe (')
.ascii "ll award the winner"
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "of the Coin Star"
.byte 0x82 ; Comma (,)
.byte 0x07 ; Yellow Text
.ascii " 99 Stars"
.byte 0xC2,0xC2 ; Exclamation Marks (!!)
.byte 0x08 ; White Text
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "And the winner is"
.byte 0x85,0x85,0x85 ; triple dots (...)
.byte 0x05 ; Green Text
.ascii " YOU"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause