; NAME: Sell Star for Coins (MP2) v1.2
; GAMES: MP2_USA
; EXECUTION: Direct
; PARAM: Number|SellPrice
; PARAM: Space|SpaceToFace

; This code is designed to allow you to sell a Star to receive coins.
; The coins received is set by the parameter "SellPrice".

ADDIU SP SP -80
SW RA 76(SP)
SW S0 72(SP)
SW S1 68(SP)
SW S2 64(SP)
SW S3 60(SP)
SW S4 56(SP)
SW S5 52(SP)

; S0 = Current Player Struct
; S1 = Prompt Choice
; S2 = Coin Profit
; S3 = Stars to Sell
; S4 = Player's Stars
; S5 = SellPrice

;===Check for Bowser===
JAL GetCurrentPlayerIndex ; Get current player index at V0
NOP
SLTI T0 V0 4 ; If V0 < 4, T0 = 1
BEQZ T0 exit ; If T0 = 0, player is Bowser, exit
NOP

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

;===Rotate Toward Space===
LI A0 -1 ; Set current player
LI A1 8 ; Rotate over 8 frames
JAL RotateCharacterModel
LI A2 SpaceToFace ; Face SpaceToFace

;===Check for Stars===
JAL GetPlayerStruct
LI A0 -1 ; Get current player struct at V0
MOVE S0 V0 ; Copy V0 to S0

LHU S4 0x0E(S0) ; Load stars into S1 from offset of player struct
BEQZ S4 NoStars ; If S1 = 0, player has no stars
NOP

;===Prepare Coin String to Display in Message===
LI S5 SellPrice

LUI A0 hi(CoinString)
ADDIU A0 A0 lo(CoinString) ; Saves # of coins to CoinString
LUI A1 hi(percent_d)
ADDIU A1 A1 lo(percent_d)
JAL sprintf ; Converts a number to a text string
MOVE A2 S5 ; Converts the number of coins to a string

;===Prepare Star String to Display in Message===
LUI A0 hi(StarString)
ADDIU A0 A0 lo(StarString) ; Location of converted string
LUI A1 hi(percent_d)
ADDIU A1 A1 lo(percent_d)
JAL sprintf
LI A2 1 ; Convert # of stars for sale into string (default 1)

;===Display Prompt to Sell a Star===
SellSingleStar:
LUI A0 hi(SellStarPrompt)
ADDIU A0 A0 lo(SellStarPrompt)
LI A1 1 ; Display prompt
LUI A2 hi(CoinString)
JAL CallMessage
ADDIU A2 A2 lo(CoinString) ; Load CoinString into A2 (string #1)

; Get the selection, either from the player or CPU.
JAL GetRandPromptSelection
NOP
MOVE S1 V0 ; S1 now has the chosen option index

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

; Change the player's destination based on the choice.
LI T0 0
BEQ S1 T0 SellStar
NOP
LI T0 1
BEQ S1 T0 SayNo
NOP
JAL 0x80103A64 ; ViewBoardMap
NOP
J SellSingleStar
NOP

;===Sell Star(s)===
SellStar:
LI S3 1 ; Sell 1 star
LHU T0 0x0E(S0) ; Load stars into T0 from offset of player struct
SUBU T0 T0 S3 ; T0 minus S3
SH T0 0x0E(S0) ; Store new star value into offset of player struct

;===Gain Coins===
MULT S3 S5 ; Multiply stars sold by price per star
MFLO S2 ; Store result in S2
SLTI T0 S2 1000 ; T0 = 1 if S2 < 1000
BNEZ T0 GainCoins
NOP ; else S2 = 1000+
LI S2 999

GainCoins:
LBU A0 0x1C(S0) ; Give coins to current player
JAL AdjustPlayerCoinsGradual
MOVE A1 S2 ; Give coins

LBU A0 0x1C(S0) ; Give coins to current player
JAL ShowPlayerCoinChange
MOVE A1 S2 ; Give coins

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

;===Play Happy Voice===
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

; Visit the following link to get SFX Indexes:
; https://pastebin.com/H1jxNFqr

;===Play Happy Animation===
LI A0 -1 ; Set current player
LI A1 5 ; Joy animation
JAL SetBoardPlayerAnimation
LI A2 0 ; Do not loop animation

JAL SleepProcess
LI A0 40 ; Wait 40 frames for animation

;===Sold Message===
LUI A0 hi(SoldMessage)
ADDIU A0 A0 lo(SoldMessage)
LI A1 0 ; Display message
JAL CallMessage
LI A2 0 ; No string

J exit
NOP

;===Say No Message===
SayNo:
LUI A0 hi(DeclineMessage)
ADDIU A0 A0 lo(DeclineMessage)
LI A1 0 ; Display message
LUI A2 hi(CoinString)
JAL CallMessage
ADDIU A2 A2 lo(CoinString) ; Load CoinString into A2 (string #1)

J exit
NOP

;===No Stars Message===
NoStars:
LUI A0 hi(NoStarsMessage)
ADDIU A0 A0 lo(NoStarsMessage)
LI A1 0 ; Display message
JAL CallMessage
LI A2 0 ; No string

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

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

.align 16
CoinString:
.fill 8

.align 16
StarString:
.fill 8

;===Mini Func to Call Static Message===
.align 4
CallMessage:
ADDIU SP SP -48
SW RA 44(SP)
SW S0 40(SP)
SW S1 36(SP)
SW S2 32(SP)

; A0 = Message Address
; A1 = Message or Prompt (0 or 1)
; A2 = String #1 (byte 0x11)

MOVE S1 A1 ; Copy A1 to S1
MOVE S0 A0 ; Copy A0 to S0
MOVE S2 A2 ; Copy A2 to S2

JAL PlaySound
LI A0 0xE3 ; Bob-omb voice

SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 0x1D ; Character image (-1 for none, 0x1D for Red Bob-omb)
; 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
MOVE A1 S0 ; Copy S0 to A0
MOVE A2 S2 ; Copy S2 to A2
JAL ShowMessage
LI A3 0

BNEZ S1 CallMessageExit ; If S0 = 1, exit to close prompt
NOP

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

CallMessageExit:
LW RA 44(SP)
LW S0 40(SP)
LW S1 36(SP)
LW S2 32(SP)
JR RA
ADDIU SP SP 48

;===Message Text===
.align 16
SellStarPrompt:
.byte 0x0B ; Begin prompt
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "Ten"
.byte 0x3D ; Dash (-)
.ascii "hut"
.byte 0x82 ; Comma (,)
.ascii " soldier"
.byte 0xC2 ; Exclamation mark (!)
.ascii " It looks like"
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "you can sell a"
.byte 0x07 ; Yellow font
.ascii " Star"
.byte 0x08 ; White font
.ascii " for"
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.byte 0x06 ; Blue font
.byte 0x11 ; String #1 stored in A2 (CoinString)
.ascii " coins"
.byte 0x08,0xC2 ; White font, Exclamation mark (!)
.ascii " Deal or no deal"
.byte 0xC3 ; Question mark (?)
.byte 0x0A,0x0A ; Two new lines
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Little more for option indent
.byte 0x0C ; Start option
.ascii "Sell"
.byte 0x07 ; Yellow font
.ascii " a Star"
.byte 0x08 ; White font
.ascii " for "
.byte 0x06 ; Blue font
.byte 0x11 ; String #1 stored in A2 (CoinString)
.ascii " coins"
.byte 0x08,0x0D ; White font, End option
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Little more for option indent
.byte 0x0C ; Start option
.ascii "No deal"
.byte 0x0D ; End option
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Little more for option indent
.byte 0x0C ; Start option
.ascii "View map"
.byte 0x0D ; End option
.byte 0

.align 16
SoldMessage:
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "Many thanks"
.byte 0x82 ; Comma (,)
.ascii " soldier"
.byte 0xC2 ; Exclamation mark (!)
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "Ten"
.byte 0x3D ; Dash (-)
.ascii "hut"
.byte 0xC2,0xC2 ; Two exclamation marks (!!)
.byte 0xFF,0 ; Wait, press A to confirm

.align 16
DeclineMessage:
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "Ten"
.byte 0x3D ; Dash (-)
.ascii "hut"
.byte 0x82 ; Comma (,)
.ascii " soldier"
.byte 0xC2 ; Exclamation mark (!)
.ascii " The choice"
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "is yours"
.byte 0x85 ; Period (.)
.ascii " Just remember that"
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.byte 0x07 ; Yellow font
.ascii "one Star"
.byte 0x08 ; White font
.ascii " sells for "
.byte 0x06 ; Blue 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

.align 16
NoStarsMessage:
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "Ten"
.byte 0x3D ; Dash (-)
.ascii "hut"
.byte 0x82 ; Comma (,)
.ascii " soldier"
.byte 0xC2 ; Exclamation mark (!)
.ascii " It looks like"
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "you have"
.byte 0x03 ; Red font
.ascii " 0 Stars"
.byte 0x08 ; White font
.ascii " to sell"
.byte 0xC2 ; Exclamation mark (!)
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "Come back when you"
.byte 0x5C ; Apostrophe (')
.ascii "ve collected some"
.byte 0xC2 ; 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 ; Wait, press A to confirm

; If your message has an image, use this at the start of each line
; .byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image