; NAME: Jeanie's Hut (Airsola™)
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: +Number|Price

ADDIU SP SP -80
SW RA 76(SP)
SW S4 72(SP)
SW S0 68(SP)
SW S7 64(SP)
SW S2 60(SP)


; S4 = Current Player's Index
; S0 = Flag for lack of Coins, 0 = exit
; S2 = Chosen Option Index
; S7 = Message to Display

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

JAL GetCurrentPlayerIndex
NOP
MOVE S4 V0

MOVE A0 S4 ; Copy Current Player Index to A0
JAL PlayerHasCoins ; Returns 0 if FALSE 
LI A1 Price ; A1 = The minimum amount of Coins to shop

BEQ V0 R0 NotEnoughCoins ; If the player can't afford it, branch
NOP

LI S0 1 ; Continue
LUI S7 hi(JeanieGreeting_Prompt)
ADDIU S7 S7 lo(JeanieGreeting_Prompt)
J DisplayMessage
NOP

NotEnoughCoins:
LI S0 0 ; Exit 
LUI S7 hi(JeanieNotEnoughCoins_Message)
ADDIU S7 S7 lo(JeanieNotEnoughCoins_Message)

DisplayMessage:
LUI A0 hi(Price_String_Location)
ADDIU A0 A0 lo(Price_String_Location) ; Parse Destination
LUI A1 hi(percent_d)
ADDIU A1 A1 lo(percent_d) ; String Format
JAL sprintf ; Creates String from Price
LI A2 Price ; Write Price unto A2

MOVE A0 S4
JAL GetCurrentPlayerCharacterString ; Character String at V0
NOP

SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 0x3C ; Character image (Jeanie)
MOVE A1 S7 ; Message Pointer
LUI A2 hi(Price_String_Location)
ADDIU A2 A2 lo(Price_String_Location)
JAL ShowMessage
MOVE A3 V0 ; Move Character String to A3

MOVE A0 S4
JAL AIDecision ; Returns AI Choice on V0
NOP

LI A0 2 ; Pick whatever is in A1 as AI's Choice
JAL GetBasicPromptSelection
MOVE A1 V0
MOVE S2 V0 ; Chosen Option Index

JAL CloseMessage
NOP

BEQ S0 R0 exit
NOP

LI T0 0
BEQ S2 T0 yes
NOP
LI T0 1
BEQ S2 T0 no
NOP

JAL ViewBoardMap
NOP
J DisplayMessage
NOP

yes:
LI T0 Price
MOVE A0 S4 ; Current Player's Index moved to A0
JAL AdjustPlayerCoinsGradual ; Changes Player's coin amount
SUBU A1 R0 T0 ; Amount to pay

LI T0 Price
MOVE A0 S4 ; Current Player's Index moved to A0
JAL ShowPlayerCoinChange ; Displays Player's coin loss on-screen 
SUBU A1 R0 T0 ; Amount to display

JAL SleepProcess ; Sleeps for 30 frames
LI A0 30

LI S0 1 ; Yes
LUI S7 hi(JeanieChant_Message)
ADDIU S7 S7 lo(JeanieChant_Message)
J DisplayMessage2
NOP

no:
LI S0 0 ; No
LUI S7 hi(JeanieGoodbye_Message)
ADDIU S7 S7 lo(JeanieGoodbye_Message)

DisplayMessage2:
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 0x3C ; Character image (Jeanie)
MOVE A1 S7 ; Message Pointer
LI A2 0 
JAL ShowMessage
LI A3 0

JAL CloseMessage
NOP

BEQ S0 R0 exit
NOP

JAL 0x800EDA58 ; ChangeStarLocation
NOP


exit:
LW S2 60(SP)
LW S0 68(SP)
LW S7 64(SP)
LW S4 72(SP)
LW RA 76(SP)
JR RA
ADDIU SP SP 80

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

Price_String_Location:
.fill 8

.align 4
GetCurrentPlayerCharacterString:
; A0 = Current Player's Index
ADDIU SP SP -32
SW RA 28(SP)

LUI T0 hi(p1_char)
ADDIU T0 T0 lo(p1_char) ; Load Player 1's Character address
LI T2 0x38 ; Distance between each player's addresses
MULT A0 T2 ; Multiply Current Player Index by T2
MFLO T2 ; Move Result to T2
ADDU T0 T0 T2 ; Add results to get current Player's Character address
LBU T2 0(T0) ; Load the value address value of S0 into S2

LI V0 0x1C00 ; Load value of MARIO string to T0
ADDU V0 T2 V0 ; Add T2 to the string value to obtain the character

LW RA 28(SP)
JR RA
ADDIU SP SP 32


.align 16
JeanieChant_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Fine"
.byte 0xC2 ; Exclamation Mark (!)
.ascii " Here we go"
.byte 0x85,0x85,0x85 ; Periods (...)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; Newline+Standard padding for picture
.ascii "FUNGUS"
.byte 0x3D ; - (minus)
.ascii "POCUS"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause

.align 16
JeanieGreeting_Prompt:
.byte 0x0B ; Start the message
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Huh"
.byte 0xC3 ; Question Mark (?)
.byte 0x10 ; Space ( )
.byte 0x12 ; String #2 - Player character's name
.byte 0x82 ; Comma (,)
.ascii " is it"
.byte 0xC3 ; Question Mark (?)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; Newline+Standard padding for picture
.ascii "What do you want"
.byte 0xC3 ; Question Mark (?)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; Newline+Standard padding for picture
.ascii "To change the star"
.byte 0x5C ; Apostrophe (')
.ascii "s location"
.byte 0xC3 ; Question Mark (?)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Newline+Picture+Option Indent
.byte 0x0C ; Start option
.ascii "Yeah"
.byte 0x1A,0x1A ; padding
.byte 0x29 ; Coin icon
.byte 0x3E ; x (multiply)
.byte 0x11 ; String #1 - Price
.byte 0x0D ; End option
.byte 0x0A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Picture + Option Indent
.byte 0x0C ; Start option
.ascii "Nope"
.byte 0x0D ; End option
.byte 0x0A,0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Picture + Option Indent
.byte 0x0C ; Start option
.ascii "View Map"
.byte 0x0D ; End option
.byte 0 ; End Prompt

.align 16
JeanieNotEnoughCoins_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Hmph"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; Newline+Standard padding for picture
.ascii "You don"
.byte 0x5C ; Apostrophe (')
.ascii "t have enough coins"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; Newline+Standard padding for picture
.ascii "Get lost"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause

.align 16
JeanieGoodbye_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Really"
.byte 0xC3 ; Question Mark (?)
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; Newline+Standard padding for picture
.ascii "Well"
.byte 0x82 ; Comma (,)
.ascii " whatever"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause

.align 4
AIDecision:
; A0 = Current Player's Index
ADDIU SP SP -24
SW RA 20(SP)

JAL PlayerHasCoins ; Ret 0 if FALSE
LI A1 30 ; Check if CPU has 30 Coins

BEQ V0 R0 AINotEnoughCoins
NOP

JAL RNGPercentChance ; Returns 0 or 1 at V0
LI A0 50 
J AIDecision_exit
NOP

AINotEnoughCoins:
LI V0 1

AIDecision_exit:
LW RA 20(SP)
JR RA
ADDIU SP SP 24