; NAME: GOURMETPORIUMEVENT
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: +Number|MinToShop
; PARAM: +Number|MushroomPrice
; PARAM: +Number|ReverseMushroomPrice
; PARAM: +Number|PoisonMushroomPrice
; PARAM: +Number|GoldenMushroomPrice
; PARAM: +Number|WarpBlockPrice
; PARAM: +Number|SkeletonKeyPrice
; PARAM: +Number|BarterBoxPrice
; PARAM: +Number|WackyWatchPrice


;====================================================================
; IMPORTANT: This code is specifically designed to allow the landing
;            or passing player to purchase an item from an inventory
;            of your choice. If the player's inventory is full, they
;            don't get to buy anything.
;===================================================================

ADDIU SP SP -40
SW RA 36(SP)
SW S1 32(SP)
SW S2 28(SP)
SW S0 24(SP)
SW S3 20(SP)
SW S4 16(SP)
SW S5 8(SP)


;BEGIN Checks that prevent purchase===================================
LUI S1 hi(total_turns)
ADDIU S1 S1 lo(total_turns) ; Load address of Total Turns to S1
LBU S1 0(S1) ; Load value of Total Turns to S1

LUI S2 hi(current_turn)
ADDIU S2 S2 lo(current_turn) ; Load address of Current Turn to S2
LBU S2 0(S2) ; Load value of Current Turn to S2

BEQ S2 S1 LastTurn ; If it's the last turn, branch
NOP

JAL GetCurrentPlayerIndex ; Current Player's Index at V0
NOP

MOVE S4 V0 ; Copy Current Player Index to S4
MOVE A0 S4 ; Copy Current Player Index to A0
LI A1 MinToShop ; A1 = The minimum amount of Coins to shop
JAL PlayerHasCoins ; Checks if Player has the minimum Coins 
NOP

BEQ V0 R0 NoCoinsToShop ; If the player doesn't have the Coins, exit
NOP

LUI S1 hi(p1_item3)
ADDIU S1 S1 lo(p1_item3) ; Load Player 1's Item slot #3 address
LI S3 0x38 ; This is the distance between each player's Item #3
MULT S4 S3 ;  Multiply Current Player Index by S3
MFLO S3 ; Move the result to S3

ADDU S1 S1 S3 ; Add result to get current Player's item slot #3
LBU S2 0(S1) ; Load address value into S2

LI T0 0xFF ; No Item Value
BNE S2 T0 TooManyItems ; If the player's inventory is full, exit
NOP
;END Checks that prevent purchase=====================================

;BEGIN GREETING=======================================================
JAL PlayMusic
LI A0 0x43 ; Toad: Yahoo! ^_^/

SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 0xA ; Character image (Alt Toad)
LUI A1 hi(Greeting_Message)
ADDIU A1 A1 lo(Greeting_Message)
ADDU A2 R0 R0
JAL 0x800EC8EC ; ShowMessage
ADDU A3 R0 R0

JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP
;END GREETING=========================================================

;BEGIN ITEM SELECTION PROMPT==========================================
START:
SW R0 20(SP) ; A4
SW R0 24(SP) ; A5
SW R0 28(SP) ; A6
LI A0 0xA ; Character image (Alt Toad)
LUI A1 hi(ItemSelection_Prompt)
ADDIU A1 A1 lo(ItemSelection_Prompt)
ADDU A2 R0 R0
JAL 0x800EC8EC ; ShowMessage
ADDU A3 R0 R0

; Get the selection, either from the player or CPU.
; If A0 is a pointer to AI data, AI logic is ran to pick for CPUs.
; If A0 is 0 or 1, the 0th or 1st option is chosen by CPUs.
; If A0 is 2, then the value of A1 is the CPUs option index choice.
LI A0 0
JAL GetBasicPromptSelection
LI A1 0 ; A1 now has the AI's decision
MOVE S0 V0 ; S0 now has the chosen option index

; Obligatory message box closing/cleanup calls.
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP

; Change the player's destination based on the choice.
LI A0 0
BEQ S0 A0 item1
NOP
LI A0 1
BEQ S0 A0 item2
NOP
LI A0 2
BEQ S0 A0 item3
NOP
LI A0 3
BEQ S0 A0 item4
NOP
LI A0 4
BEQ S0 A0 item5
NOP
LI A0 5
BEQ S0 A0 item6
NOP
LI A0 6
BEQ S0 A0 item7
NOP
LI A0 7
BEQ S0 A0 item7
NOP
LI A0 8
BEQ S0 A0 exit
NOP
LI A0 9
BEQ S0 A0 view_map
NOP
;END ITEM SELECTION PROMPT============================================

;BEGIN Item Values====================================================
; 0x00 = Mushroom
; 0x01 = Skeleton Key
; 0x02 = Poison Mushroom
; 0x03 = Reverse Mushroom
; 0x04 = Cellular Shopper
; 0x05 = Warp Block
; 0x06 = Plunder Chest
; 0x07 = Bowser Phone
; 0x08 = Dueling Glove
; 0x09 = Lucky Lamp
; 0x0A = Golden Mushroom
; 0x0B = Boo Bell
; 0x0C = Boo Repellant
; 0x0D = Bowser Suit
; 0x0E = Magic Lamp
; 0x0F = Koopa Card
; 0x10 = Barter Box
; 0x11 = Lucky Coin
; 0x12 = Wacky Watch
;END Item Values======================================================

;BEGIN Selected Option================================================
item1:
LI S0 0x0 ; Mushroom Value
LI S3 MushroomPrice ; S3 contains Mushroom Price
J payprice
NOP
item2:
LI S0 0x3 ; Reverse Mushroom Value
LI S3 ReverseMushroomPrice ; S3 contains Reverse Mushroom Price
J payprice
NOP
item3:
LI S0 0x2 ; Poison Mushroom Value
LI S3 PoisonMushroomPrice ; S3 contains Poison Mushroom Price
J payprice
NOP
item4:
LI S0 0xA ; Golden Mushroom Value
LI S3 GoldenMushroomPrice ; S3 contains Golden Mushroom Price
J payprice
NOP
item5:
LI S0 0x05 ; Warp Block Value
LI S3 WarpBlockPrice ; S3 contains Wacky Watch Price
J payprice
NOP
item6:
LI S0 0x01 ; Skeleton Key Value
LI S3 SkeletonKeyPrice ; S3 contains Barter Box Price
J payprice
NOP
item7:
LI S0 0x10 ; Barter Box Value
LI S3 BarterBoxPrice ; S3 contains Barter Box Price
J payprice
NOP
item8:
LI S0 0x12 ; Wacky Watch Value
LI S3 WackyWatchPrice ; S3 contains Wacky Watch Price
J payprice
NOP

MOVE A0 S4 ; Current Player's Index moved to A0
MOVE A1 S3 ; Amount of coins to check
JAL PlayerHasCoins ; Checks if Player has A1's amount of Coins
NOP

BEQ V0 R0 NotEnoughCoins ; If insufficient funds, branch
NOP

MOVE A0 S4 ; Current Player's Index moved to A0
SUBU A1 R0 S3 ; Amount to pay
JAL AdjustPlayerCoinsGradual ; Changes Player's coin amount
NOP

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

ADDIU A0 R0 30
JAL SleepProcess ; Sleeps for 30 frames
NOP

LI T1 2 
SUBU S1 S1 T1 ; This loads the current player's first Item address
LI S5 1 ; S5 is the counter to stop giving Items once full
Loop:
LI T0 0x4
SLT A2 S5 T0 ; A2 = 1 if S5 is less than 4
NOP
BEQ A2 R0 exit ; exit once player's item inventory is full
NOP

LBU S2 0(S1) ; Load slot #1's Item Value into S2
LI T1 0xFF ; No Item Value
BNE S2 T1 next ; If the player has an item, load next address
NOP


sound:
JAL PlayMusic
ADDIU A0 R0 0x46 ; Turn Start sound

ADDIU A0 R0 30
JAL SleepProcess ; Sleeps for 30 frames
NOP

next:
ADDIU S1 S1 1 ; Load next item's address
ADDIU S5 S5 1 ; Raise the item counter by 1
J Loop ; Loop back to give the next item
NOP

view_map:
JAL ViewBoardMap
NOP
J START
NOP
;END Item Values======================================================

;BEGIN Checking Player For Necessary Coins To Purchase================
payprice:
MOVE A0 S4 ; Current Player's Index moved to A0
MOVE A1 S3 ; Amount of coins to check
JAL PlayerHasCoins ; Checks if Player has A1's amount of Coins
NOP

BEQ V0 R0 NotEnoughCoins ; If insufficient funds, branch
NOP
;END Checking Player For Necessary Coins To Purchase==================

;BEGIN Taking Coins From Player=======================================
MOVE A0 S4 ; Current Player's Index moved to A0
SUBU A1 R0 S3 ; Amount to pay
JAL AdjustPlayerCoinsGradual ; Changes Player's coin amount
NOP

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

ADDIU A0 R0 30
JAL SleepProcess ; Sleeps for 30 frames
NOP
;END Taking Coins From Player=========================================

;BEGIN Checking Player Item Slots=====================================
LI T1 2
SUBU S1 S1 T1 ; This loads the current player's first Item address
LBU S2 0(S1) ; Load slot #1's Item Value into S2
LI T1 0xFF ; No Item Value
BEQ S2 T1 giveitem ; Go to giveitem if the slot is empty
NOP
ADDIU S1 S1 1 ; Add 1 to load current player's second Item address
LBU S2 0(S1) ; Load slot #2's Item Value into S2
BEQ S2 T1 giveitem ; Go to giveitem if the slot is empty
NOP
ADDIU S1 S1 1 ; Add 1 to load current player's third Item address

giveitem:
SB S0 0(S1)
;END Checking Player Item Slots=======================================

;BEGIN Item Sound Effect==============================================
JAL PlayMusic
LI A0 0x46 ; Turn Start sound

LI A0 15 ; Frames to wait after the sound plays
JAL SleepProcess ; Sleeps for 15 frames
NOP

J exit
NOP
;END Item Sound Effect================================================

;BEGIN CANNOT BUY ITEM MESSAGE========================================
NotEnoughCoins:
JAL PlayMusic
LI A0 0x47 ; Toad: Oooh :(

SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 0xA ; Character image (Alt Toad)
LUI A1 hi(NotEnoughCoins_Message)
ADDIU A1 A1 lo(NotEnoughCoins_Message)
ADDU A2 R0 R0
JAL 0x800EC8EC ; ShowMessage
ADDU A3 R0 R0

JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP

J START
NOP
;END CANNOT BUY ITEM MESSAGE==========================================

;BEGIN TOO MANY ITEMS MESSAGE=========================================
TooManyItems:
JAL PlaySound
LI A0 0x29A ; Toad: Oooh :(

SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 0xA ; Character image (Alt Toad)
LUI A1 hi(TooManyItems_Message)
ADDIU A1 A1 lo(TooManyItems_Message)

J FinalizeMessage
NOP
;END TOO MANY ITEMS MESSAGE===========================================

;BEGIN NOT ENOUGH COINS MESSAGE=======================================
NoCoinsToShop:
JAL PlaySound
LI A0 0x29A ; Toad: Oooh :(

SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 0xA ; Character image (Alt Toad)
LUI A1 hi(NoCoinsToShop_Message)
ADDIU A1 A1 lo(NoCoinsToShop_Message)

J FinalizeMessage
NOP
;END NOT ENOUGH COINS MESSAGE=========================================

;BEGIN Last Turn MESSAGE==============================================
LastTurn:
JAL PlaySound
LI A0 0x29A ; Toad: Oooh :(

SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 0xA ; Character image (Alt Toad)
LUI A1 hi(LastTurn_Message)
ADDIU A1 A1 lo(LastTurn_Message)
;END Last Turn MESSAGE================================================

FinalizeMessage:
ADDU A2 R0 R0
JAL 0x800EC8EC ; ShowMessage
ADDU A3 R0 R0

JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP



exit:

JAL GetBoardAudioIndex
NOP

JAL PlayMusic
MOVE A0 V0

LW S5 8(SP)
LW S4 16(SP)
LW S3 20(SP)
LW S1 32(SP)
LW S2 28(SP)
LW S0 24(SP)
LW RA 36(SP)
JR RA
ADDIU SP SP 40



.align 16
Greeting_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "I am the Gourmetporium Guy"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.ascii " this is the"
.byte 0x03 ; red font
.ascii " Gourmetporium"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii " Buy some of my"
.byte 0x03
.ascii " Gourmet"
.byte 0x08
.ascii " will you"
.byte 0xC3 ; Question Mark (?)
.byte 0xFF,0 ; FF=Pause

.align 16
LastTurn_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "I am the Gourmetporium Guy"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.ascii "This is a"
.byte 0x03 ; red font
.ascii " Gourmetporium"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "But"
.byte 0x82 ; Comma (,)
.ascii " Right now"
.ascii " I am out of stuff to give ya"
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Heh"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.ascii "Too bad"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause

.align 16
NoCoinsToShop_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "I am the Gourmetporium Guy"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.ascii "This is a"
.byte 0x03 ; red font
.ascii " Gourmetporium"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "You don"
.byte 0x5C ; Apostrophe (')
.ascii "t have ANY coins"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Heh"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.ascii "Too bad"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause

.align 16
NotEnoughCoins_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "I am the Gourmetporium Guy"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.ascii " this is the"
.byte 0x03 ; red font
.ascii " Gourmetporium"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.ascii "You don"
.byte 0x5C ; Apostrophe (')
.ascii "t have"
.byte 0x03
.ascii " ENOUGH"
.byte 0x08 ; white font
.ascii " my"
.byte 0x06 ; blue font
.ascii " friend"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Heh"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.ascii "Too bad"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause

.align 16
TooManyItems_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "I am the Gourmetporium Guy"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.ascii " this is the"
.byte 0x03 ; red font
.ascii " Gourmetporium"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Oh REALLY"
.byte 0x85 ; Period (.)
.ascii " you can"
.byte 0x5C ; Apostrophe (')
.ascii "t"
.ascii "carry any more of my" 
.byte 0x03  ; red font
.ascii " Gourmet" 
.byte 0x08
.ascii " huh"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.ascii "Heh"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause
.ascii "Too bad"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause

.align 20
ItemSelection_Prompt:
.byte 0x0B ; Start the message
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Would you like to buy some Gourmet"
.byte 0xC3 ; Question Mark (?)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Little more for option indent
.byte 0x0C ; Start option
.byte 0x03 ; Blue font
.ascii "Mushroom    "
.byte 0x08 ; White Font
.byte 0x10 ; Space ( )
.byte 0x29 ; Coin icon
.byte 0x3E ; x (multiply)
.ascii " 5"
.byte 0x0D ; End option
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Little more for option indent
.byte 0x0C ; Start option
.byte 0x05 ; Blue font
.ascii "Reverse Mushroom   "
.byte 0x08 ; White Font
.byte 0x10 ; Space ( )
.byte 0x29 ; Coin icon
.byte 0x3E ; x (multiply)
.ascii " 15"
.byte 0x0D ; End option
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Little more for option indent
.byte 0x0C ; Start option
.byte 0x04 ; Blue font
.ascii "Poison Mushroom"
.byte 0x10 ; Space ( )
.byte 0x08 ; White Font
.byte 0x1A ; padding
.byte 0x29 ; Coin icon
.byte 0x3E ; x (multiply)
.ascii " 5"
.byte 0x0D ; End option
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Little more for option indent
.byte 0x0C ; Start option
.byte 0x07 ; Blue font
.ascii "Golden Mushroom"
.byte 0x08 ; White Font
.byte 0x29 ; Coin icon
.byte 0x3E ; x (multiply)
.ascii " 30"
.byte 0x0D ; End option
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Little more for option indent
.byte 0x0C ; Start option
.byte 0x04 ; Blue font
.ascii "Warp Block"
.byte 0x10, ; Space (  )
.byte 0x08 ; White Font
.byte 0x1A ; padding
.byte 0x29 ; Coin icon
.byte 0x3E ; x (multiply)
.ascii "10"
.byte 0x0D ; End option
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Little more for option indent
.byte 0x0C ; Start option; Little more for option indent
.byte 0x0C ; Start option
.byte 0x07 ; Blue font
.ascii "Skeleton Key"
.byte 0x08 ; White Font ; padding
.byte 0x29 ; Coin icon
.byte 0x3E ; x (multiply)
.ascii "10"
.byte 0x0D ; End option
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A, ; Little more for option indent
.byte 0x0C ; Start option
.byte 0x01 ; Blue font
.ascii "Barter Box"
.byte 0x08 ; White Font
.byte 0x29 ; Coin icon
.byte 0x3E ; x (multiply)
.ascii "50"
.byte 0x0D ; End option
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Little more for option indent
.byte 0x0C ; Start option
.byte 0x04 ; Blue font
.ascii "Wacky Watch"
.byte 0x08 ; White Font
.byte 0x29 ; Coin icon
.byte 0x3E ; x (multiply)
.ascii "100"
.byte 0x0D ; End option
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A 
.byte 0x0C ; Start option
.ascii "No Gourmet for me"
.byte 0x0D ; End option
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A 
.byte 0x0C ; Start option
.ascii "View Map"
.byte 0x0D ; End option
.byte 0

.align 16
NotEnoughCoins_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "You don"
.byte 0x5C ; Apostrophe (')
.ascii "t have enough coins"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Please select another delectable"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause