; NAME: Warukio Purge
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: Number|4_to_1
; PARAM: Number|2_to_3

;====================================================================
; IMPORTANT: This code is specifically designed to give every player
;            a fixed amount of coins except for the player who lands
;            or passes through the space.
;====================================================================
.align 0x10
ADDIU sp, sp, -0x200
SW RA 40(SP)
SW S0 36(SP)
SW S1 32(SP)
SW S2 28(SP)
SW S3 24(SP)
SW S4 20(SP)
SW S5 16(SP)
SW S6 12(SP)
SW S7 8(SP)

;DONT DELETE BELOW-------------------------------------------------------
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
JAL PlaySound
ADDIU A0 R0 0xA7 ; Warukio
LI A0 0x3F ; Character image (Warukio)
LUI A1 hi(Message)
ADDIU A1 A1 lo(Message) ; This needs to be here to not crash
LI A2 0x0
JAL 0x800EC8EC ; ShowMessage
LI A3 0x0


JAL 0x800EC9DC ; I think these have to be here for messages
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP

;WHO IS RICHEST PLAYER----------------------------------------------------
LI S0 0x0 
LI S4 0x0 
LI S6 0

START:
NOP

MOVE A0 S0

JAL GetPlayerStruct
NOP
LHU S5, 0x000A (V0); Put into S5

LI T0 0x4 ; If all Players have been 
BEQ S0 T0 wait
NOP

LI S7 1
SLT A2 S5 S6 ; A2 = 1 if S5 is less than S6
BEQ A2 S7 poorer
NOP

LI S6 0
ADDU S6 S6 S5
LI S4 0
ADDU S4 S0 S4

poorer:

ADDIU S0 S0 1 ; Add 1 to S0 to check next player
J START
NOP

;WAIT START----------------------------------------------------------
wait:
ADDIU A0 R0 30 ; Amount of frames the game waits for coin handouts
JAL SleepProcess
NOP

;WAIT END------------------------------------------------------------

;WHO IS POOREST PLAYER----------------------------------------------------
LI S0 0x0 
LI S3 0X0
LI S6 0
LI A2 0
LI S7 0

STARTb:
NOP

MOVE A0 S0

JAL GetPlayerStruct
NOP
LHU S5, 0x000A (V0); Put into S5

LI T0 0x4 ; If all Players have been 
BEQ S0 T0 waitb
NOP

SLT A2 S5 S6 ; A2 = 1 if S5 is less than S6
BEQ A2 S7 rich
NOP



LI S6 0
ADDU S6 S6 S5
LI S3 0
ADDU S3 S0 S3

rich:
ADDIU S0 S0 1 ; Add 1 to S0 to check next player

J STARTb
NOP

waitb:

;SUBTRACT POOREST--------------------------------------------------
LI S6 100
LI S7 4_to_1

MOVE A0 S3

JAL GetPlayerStruct
NOP
LHU S5, 0x000A (V0); Put into S5

DIVU S6 S7
MFLO S1 ; This should create divisor for player's coins
NOP
NOP
DIVU S5 S1 ;
MFLO S5 ; This should create amount of coins from player to pot
NOP

MOVE A0 S3 ; Current Player's Index moved to A0
JAL AdjustPlayerCoinsGradual ; Changes Player's coin amount
SUBU A1 R0 S5 ; Amount player loses
NOP
;ADD RICHEST-----------------------------------------------------------
MOVE A0 S4 ; Current Player's Index moved to A0
JAL AdjustPlayerCoinsGradual ; Changes Player's coin amount
ADDU A1 R0 S5 ; Amount player gains


ADDIU A0 R0 30 ; Amount of frames the game waits for coin handouts
JAL SleepProcess
NOP
;3_TO_2--------------------------------------------------------------------
;WHO IS THIRD PLAYER----------------------------------------------------
LI S0 0
LI S1 0
LI S2 0

Third:

BEQ S0 S3 IsNot
NOP
BEQ S0 S4 IsNot
NOP

ADDU S1 S0 S1

J Done
NOP

IsNot:
ADDIU S0 S0 1
NOP

J Third
NOP

Done:
LI S0 0


Second:

BEQ S0 S3 IsNotB
NOP
BEQ S0 S4 IsNotB
NOP
BEQ S0 S1 IsNotB
NOP

ADDU S2 S2 S0

J Done2
NOP

IsNotB:
ADDIU S0 S0 1
NOP

J Second
NOP

Done2:
;COMPARE SECOND AND THIRD----------------------------------------------
MOVE A0 S1

JAL GetPlayerStruct
NOP
LHU S5, 0x000A (V0); Put into S5
NOP

MOVE A0 S2

JAL GetPlayerStruct
NOP
LHU S6, 0x000A (V0); Put into S5
NOP


LI S7 1
SLT A2 S5 S6 ; A2 = 1 if S2 is third
BEQ A2 S7 S2Third
NOP

LI S6 100
LI S7 2_to_3

MOVE A0 S2

JAL GetPlayerStruct
NOP
LHU S5, 0x000A (V0); Put into S5

DIVU S6 S7
MFLO S0 ; This should create divisor for player's coins
NOP
NOP
DIVU S5 S0 ;
MFLO S5 ; This should create amount of coins from player to pot
NOP

MOVE A0 S2 ; Current Player's Index moved to A0
JAL AdjustPlayerCoinsGradual ; Changes Player's coin amount
SUBU A1 R0 S5 ; Amount player loses
NOP
ADDIU A0 R0 30 ; Amount of frames the game waits for coin handouts
JAL SleepProcess
NOP

MOVE A0 S1 ; Current Player's Index moved to A0
JAL AdjustPlayerCoinsGradual ; Changes Player's coin amount
ADDU A1 R0 S5 ; Amount player gains
NOP
ADDIU A0 R0 30 ; Amount of frames the game waits for coin handouts
JAL SleepProcess
NOP

S2Third:

LI S6 100
LI S7 3_to_2

MOVE A0 S1

JAL GetPlayerStruct
NOP
LHU S5, 0x000A (V0); Put into S5

DIVU S6 S7
MFLO S0 ; This should create divisor for player's coins
NOP
NOP
DIVU S5 S0 ;
MFLO S5 ; This should create amount of coins from player to pot
NOP

MOVE A0 S1 ; Current Player's Index moved to A0
JAL AdjustPlayerCoinsGradual ; Changes Player's coin amount
SUBU A1 R0 S5 ; Amount player loses
NOP
ADDIU A0 R0 30 ; Amount of frames the game waits for coin handouts
JAL SleepProcess
NOP

MOVE A0 S2 ; Current Player's Index moved to A0
JAL AdjustPlayerCoinsGradual ; Changes Player's coin amount
ADDU A1 R0 S5 ; Amount player gains


ADDIU A0 R0 30 ; Amount of frames the game waits for coin handouts
JAL SleepProcess
NOP

Exit:
;WAIT START----------------------------------------------------------
waitagain:
ADDIU A0 R0 30 ; Amount of frames the game waits for coin handouts
JAL SleepProcess
NOP
;WAIT END------------------------------------------------------------


LW S7 8(SP)
LW S6 12(SP)
LW S5 16(SP)
LW S4 20(SP)
LW S3 24(SP)
LW S2 28(SP)
LW S1 32(SP)
LW S0 36(SP)
LW RA 40(SP)
JR RA
ADDIU sp, sp, 0x200

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

.align 10
Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Take from the poor and "
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "give to the rich"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause (THIS ALWAYS GOES AT THE END)

; 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