; NAME: Give/Take Range of Coins [Current Player] (v2)
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: +Number|MinCoins
; PARAM: +Number|MaxCoins
; PARAM: Boolean|GiveCoins

;====================================================================
; IMPORTANT: This code is specifically designed to give the landing
;            or passing player a random amount of coins from a range.
;            MinCoins is the minimum amount of coins to give/take out.
;            MaxCoins is the maximum amount of coins to give/take out.
;            GiveCoins will give the player coins if TRUE, or take
;			 coins if FALSE.
;===================================================================

GiveTakeRangeOfCoins_Function:
ADDIU SP SP -120
SW RA 116(SP)
SW S0 112(SP)
SW S1 108(SP) ; Value of GiveCoins
SW S2 104(SP)
SW S3 100(SP) ; Number of coins to give/take
SW S4 96(SP) ; Current Player's Struct
SW S5 92(SP) 
SW S6 88(SP)
SW S7 84(SP)
SW FP 80(SP)



JAL GetPlayerStruct ; at V0
LI A0 -1 ; Current Player
MOVE S4 V0 ; Copy to S4

LI A0 -1 ; Current Player
LI A1 -1 ; Idle Animation
JAL SetBoardPlayerAnimation ; at V0
LI A2 2 ; Loop

JAL GetRandomByte ; 0-255 at V0
NOP

LI T0 MinCoins
LI T1 MaxCoins

SUBU S3 T1 T0 ; S3 = MaxCoins - MinCoins
ADDIU S3 S3 1 ; Add 1 to get total number of outcomes
DIVU V0 S3 ; Divide by total number of outcomes
MFHI S3 ; Pull remainder into S3
ADDU S3 S3 T0 ; Add minimum coins to get value inside the range

LI S1 GiveCoins ; 0 if Taking coins - 1 if Giving coins

BEQ S1 R0 GiveTakeRangeOfCoins_checkcoincount ; If 0, check player's coins
NOP
; It's 1, no need to check

GiveTakeRangeOfCoins_prepmessage:
LUI A0 hi(GiveTakeRangeOfCoins_CoinString)
ADDIU A0 A0 lo(GiveTakeRangeOfCoins_CoinString) ; Output Pointer
LUI A1 hi(percent_d)
ADDIU A1 A1 lo(percent_d) ; String Format Pointer
JAL sprintf
MOVE A2 S3 ; Coin amount

LUI A1 hi(TakeCoins_Message)
ADDIU A1 A1 lo(TakeCoins_Message) ; Message Pointer

BEQZ S1 GiveTakeRangeOfCoins_showmessage
NOP

LUI A1 hi(GiveCoins_Message)
ADDIU A1 A1 lo(GiveCoins_Message) ; Message Pointer

GiveTakeRangeOfCoins_showmessage:
SW R0 16(SP) ; Parameter 3 (None)
SW R0 20(SP) ; Parameter 4 (None)
SW R0 24(SP) ; Parameter 5 (None)
LI A0 -1 ; Character image (None)
LUI A2 hi(GiveTakeRangeOfCoins_CoinString)
ADDIU A2 A2 lo(GiveTakeRangeOfCoins_CoinString) ; Parameter 1 (Coin Amount)
JAL ShowMessage
LI A3 0 ; Parameter 2 (None)

JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC ; These 3 functions close the message
NOP

BNEZ S1 GiveTakeRangeOfCoins_adjustplayercoins ; If 1, keep value positive
NOP
; It's 0, make amount of coins negative
SUBU S3 R0 S3 

GiveTakeRangeOfCoins_adjustplayercoins:
LBU A0 29(S4) ; Current Player Index
JAL AdjustPlayerCoinsGradual
MOVE A1 S3 ; Coins to give/take

LBU A0 29(S4) ; Current Player Index
JAL ShowPlayerCoinChange
MOVE A1 S3 ; Coins to give/take

BEQL S1 R0 GiveTakeRangeOfCoins_playanimation
LI A1 1 ; Sad Animation Index
LI A1 0 ; Happy Animation Index

GiveTakeRangeOfCoins_playanimation:
JAL SetBoardPlayerAnimationWithSound
LBU A0 29(S4) ; Current Player Index

J GiveTakeRangeOfCoins_Function_exit
NOP


GiveTakeRangeOfCoins_checkcoincount:
LBU A0 29(S4) ; Current Player Index
JAL PlayerHasCoins ; 0 if not enough - 1 if enough
LI A1 1 ; Coins

BEQZ V0 GiveTakeRangeOfCoins_nocoinstolose
NOP
; They have at least 1 coin, check if they have enough coins to lose

LBU A0 29(S4) ; Current Player Index
JAL PlayerHasCoins ; 0 if not enough - 1 if enough
MOVE A1 S3 ; Coins

BEQL V0 R0 GiveTakeRangeOfCoins_prepmessage 
LHU S3 10(S4) ; Current Player's Coins
; They have enough coins to lose, change nothing

J GiveTakeRangeOfCoins_prepmessage
NOP


GiveTakeRangeOfCoins_nocoinstolose:
SW R0 16(SP) ; Parameter 3 (None)
SW R0 20(SP) ; Parameter 4 (None)
SW R0 24(SP) ; Parameter 5 (None)
ADDIU A0 R0 -1 ; Character image (None)
LUI A1 hi(NoCoinsToLose_Message)
ADDIU A1 A1 lo(NoCoinsToLose_Message) ; Message Pointer
LI A2 0 ; Parameter 1 (None)
JAL ShowMessage
LI A3 0 ; Parameter 2 (None)

JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC ; These 3 functions close the message
NOP



GiveTakeRangeOfCoins_Function_exit:
LW S0 112(SP)
LW S1 108(SP) 
LW S2 104(SP)
LW S3 100(SP)
LW S4 96(SP) 
LW S5 92(SP) 
LW S6 88(SP)
LW S7 84(SP)
LW FP 80(SP)
LW RA 116(SP)
JR RA
ADDIU SP SP 120



.align 16
percent_d:
.asciiz "%d" ; 0x25640000

.align 16
GiveTakeRangeOfCoins_CoinString:
.fill 8

.align 16
GiveCoins_Message:
.ascii "Congratulations\xC2\n"
.ascii "You will receive \x06\x11 \x08coins\xC2\xFF\x00"

.align 16
TakeCoins_Message:
.ascii "Unlucky\x85\x85\x85\n"
.ascii "You will lose \x03\x11 \x08coins\xC2\xFF\x00"

.align 16
NoCoinsToLose_Message:
.ascii "You don\x5Ct have any coins to \x03lose\x08\xC2\xFF\x00"

; If you wish to edit these messages, please bear in mind
; that any special characters must be typed into the 
; message using \x and the value corresponding it.
; The byte \x11 is the amount of coins that the event will
; give or take, so make sure that is placed somewhere
; in the message. 
; Below is a list of the most relevant special characters.
; \x00 ; End of Message
; \x01 ; Black Font
; \x03 ; Red Font
; \x04 ; Purple Font
; \x05 ; Green Font
; \x06 ; Blue font
; \x07 ; Yellow Font
; \x08 ; White Font
; \x0A ; New Line (Writes Below)
; \n   ; New Line (Writes Below)
; \x10 ; Space ( )
; \x11 ; String #1 
; \x12 ; String #2
; \x13 ; String #3
; \x14 ; String #4
; \x15 ; String #5
; \x1A ; Tab (If your message has a character portrait, use 4 of these)
; \x29 ; Coin icon
; \x3D ; - (minus)
; \x3E ; x (multiply)
; \x5C ; Apostrophe (')
; \x82 ; Comma (,)
; \x85 ; Period (.)
; \xC2 ; Exclamation Mark (!)
; \xC3 ; Question Mark (?)
; \xFF ; (Makes message wait for A press)

; A0 = Player's Index
; A1 = 0 = Happy / 1 = Sad Index
.align 16
SetBoardPlayerAnimationWithSound:
ADDIU SP SP -80
SW RA 76(SP)
SW A0 72(SP)
SW A1 68(SP)

JAL GetPlayerStruct ; Struct Pointer at V0
NOP

LBU T0 3(V0) ; Character Index

BNEZ A1 SetBoardPlayerAnimationWithSound_sad
NOP

SetBoardPlayerAnimationWithSound_happy:
JAL PlaySound
ADDIU A0 T0 0x275 ; Mario's Happy Voice Index + Current Character Index

LW A0 72(SP) ; Player Index
LI A1 5 ; Happy Animation
JAL SetBoardPlayerAnimation
LI A2 0 ; Don't loop

JAL SleepProcess
LI A0 30 ; Frames

J SetBoardAnimationWithSound_exit
NOP

SetBoardPlayerAnimationWithSound_sad:
JAL PlaySound
ADDIU A0 T0 0x287 ; Mario's Sad Voice Index + Current Character Index

LW A0 72(SP) ; Player Index
LI A1 3 ; Despair Animation
JAL SetBoardPlayerAnimation
LI A2 0 ; Don't loop

JAL SleepProcess
LI A0 60 ; Frames

SetBoardAnimationWithSound_exit:
LW A0 72(SP) ; Player Index
LI A1 -1 ; Despair Animation
JAL SetBoardPlayerAnimation
LI A2 2 ; Loop

LW A1 68(SP)
LW A0 72(SP)
LW RA 76(SP)
JR RA
ADDIU SP SP 80