; NAME: Give Current Player a Range of Coins (Airsola™)
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: +Number|MIN_COINS
; PARAM: +Number|MAX_COINS

;====================================================================
; IMPORTANT: This code is specifically designed to give the landing
;            or passing player a random amount of coins from a range.
;            MIN_COINS is the minimum amount of coins to give out.
;            MAX_COINS is added to MIN_COINS to determine the maximum
;            amount of coins to give out.
;
;            Example: If MIN_COINS is 5 and MAX_COINS is 6, the
;            minimum amount of coins given will be 5, and the maximum
;            will be 11.
;===================================================================

ADDIU SP SP -40
SW RA 36(SP)
SW S1 32(SP)



;GOT COINS MESSAGE DISPLAY START-------------------------------------
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 -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(coins)
ADDIU A1 A1 lo(coins) ; This loads "coins" into the Textbox
LI A2 0x0
JAL 0x800EC8EC ; ShowMessage
LI A3 0x0

; The rest here perform the "wait for confirmation"
; and probably messagebox teardown.
JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP
;GOT COINS MESSAGE DISPLAY END---------------------------------------

;GIVE COINS START----------------------------------------------------
JAL GetRandomByte ; Generates a number from 0-255 into V0
NOP

ADDIU T0 R0 MAX_COINS ; T0 holds value of MAX_COINS
ADDIU T0 T0 1 
DIVU V0 T0 ; Divide the random number by T0
MFHI S1 ; HI contains the remainder, pull that into S1

JAL GetCurrentPlayerIndex ; Current Player's Index at V0
NOP

MOVE A0 V0 ; Assigns Current Player's Index to A0
ADDIU A1 S1 MIN_COINS ; Add minimum amount of coins to remainder
JAL AdjustPlayerCoinsGradual ; Changes player's coin amount
NOP

JAL GetCurrentPlayerIndex ; Current Player's Index at V0
NOP

MOVE A0 V0 ; Assigns Current Player's Index to A0
ADDIU A1 S1 MIN_COINS ; Add minimum amount of coins to remainder
JAL ShowPlayerCoinChange ; Shows how the amount on-screen
NOP
;GIVE COINS END------------------------------------------------------

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



LW S1 32(SP)
LW RA 36(SP)
JR RA
ADDIU SP SP 40

; .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 16
coins:
.ascii "You got a bunch of"
.byte 0x06 ; Blue font
.ascii "coins"
.byte 0x08 ; White Font
.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