; NAME: Couch Game (MP2)
; GAMES: MP2_USA
; EXECUTION: Direct
; PARAM: Space|SpaceToFace
; PARAM: +Number|MIN_COINS
; PARAM: +Number|MAX_COINS

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

; This code is designed to give the current player coins. During the
; event, they will face the space determined by the parameter
; "SpaceToFace". The coins received is determined by the parameter
; "MIN_COINS" and "MAX_COINS" and will always be within that range.

; S0 = Current Player Struct
; S1 = Amount of Coins Gained

;===Set Amount of Coins Given===
JAL GetRandomByte ; Get random byte (0-255) at V0
NOP
LI A0 MAX_COINS ; Set the maximum coin reward
LI A1 MIN_COINS ; Set the minimum coin reward
SUBU S1 A0 A1 ; Set S1 to range of difference between max and min
DIV V0 S1 ; Divide random value by range of difference
MFHI S1 ; Get the reminder of the previous division
ADDU S1 S1 A1 ; Add that remainder to the minimum number of coins

;===Set Idle Animation===
LI A0 -1 ; Set current player
LI A1 -1 ; Set idle animation
JAL SetBoardPlayerAnimation
LI A2 2 ; Loop the animation

;===Rotate Character Model===
LI A0 -1 ; Set current player
LI A1 8 ; Rotate in 8 frames
JAL RotateCharacterModel
LI A2 SpaceToFace ; Face SpaceToFace

;===Prepare Message===
LUI A0 hi(CoinString)
ADDIU A0 A0 lo(CoinString) ; Parse destination
LUI A1 hi(percent_d)
ADDIU A1 A1 lo(percent_d) ; String formatter
JAL sprintf
MOVE A2 S1 ; Convert CoinsGained to text string

;===Display Message===
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 -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(Message)
ADDIU A1 A1 lo(Message) ; Display Message
LUI A2 hi(CoinString)
ADDIU A2 A2 lo(CoinString) ; Load CoinString into String #1
JAL ShowMessage
LI A3 0

; Obligatory message box closing/cleanup calls.
JAL CloseMessage
NOP
JAL 0x80056168
NOP

;===Receive Coins===
JAL GetPlayerStruct
LI A0 -1 ; Get current player struct at V0
MOVE S0 V0 ; Copy V0 to S0

LBU A0 0x1C(S0) ; Load current player index from offset of player struct
JAL AdjustPlayerCoinsGradual
MOVE A1 S1 ; Give coins

LBU A0 0x1C(S0) ; Load current player index from offset of player struct
JAL ShowPlayerCoinChange
MOVE A1 S1 ; Give coins

JAL SleepProcess
LI A0 30 ; Wait 30 frames for coin change

;===Play Happy Voice and Animation===
LBU A0 4(S0) ; Load character value from offset of player struct
JAL PlaySound
ADDIU A0 A0 0x103 ; Add character value to Mario's happy voice
; Character voices are their "character value" distance away from
; Mario's, e.g. Luigi's character value = 1, so Luigi's happy voice
; is 1 away from Mario's.
; 0x103 = Happy Voice

LI A0 -1 ; Set current player
LI A1 5 ; Set joy animation
JAL SetBoardPlayerAnimation
LI A2 0 ; Do not loop animation

JAL SleepProcess
LI A0 40 ; Wait 40 frames for animation

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

;===Prep for Displaying Coin String===
.align 16
percent_d:
.asciiz "%d" ; 0x25640000

.align 16
CoinString:
.fill 8

;===Message Text===
.align 16
Message:
.ascii "You dig around in the couch"
.byte 0x08,0x85 ; White font, Period (.)
.byte 0x08,0x85 ; White font, Period (.)
.byte 0x08,0x85 ; White font, Period (.)
.byte 0x0A ; New Line (Writes Below)
.ascii "Looks like you manage to find "
.byte 0x07 ; Yellow font
.byte 0x11 ; String #1 stored in A2 (CoinString)
.ascii " coins"
.byte 0x08,0xC2 ; White font, Exclamation mark (!)
.byte 0xFF,0 ; Wait, press A to confirm

; 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
