; NAME: Custom Item Shop [SMP-Style] (Airsola™)
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: +Number|MinToShop
; PARAM: +Number|MushroomPrice
; PARAM: +Number|WarpBlockPrice
; PARAM: +Number|PoisonMushroomPrice
; PARAM: +Number|SkeletonKeyPrice
; PARAM: +Number|BowserPhonePrice
; PARAM: +Number|BowserSuitPrice
; PARAM: +Number|LuckyLampPrice
; PARAM: +Number|ItemBagPrice

;====================================================================
; 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 PlaySound
LI A0 0x298 ; Toad: Yahoo! ^_^/

SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 0xD ; 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 0xD ; 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 itembag
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 0x5 ; Warp Block Value
LI S3 WarpBlockPrice ; S3 contains Warp Block 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 0x1 ; Skeleton Key Value
LI S3 SkeletonKeyPrice ; S3 contains Skeleton Key Price
J payprice
NOP
item5:
LI S0 0x7 ; Bowser Phone Value
LI S3 BowserPhonePrice ; S3 contains Bowser Phone Price
J payprice
NOP
item6:
LI S0 0xD ; Bowser Suit Value
LI S3 BowserSuitPrice ; S3 contains Bowser Suit Price
J payprice
NOP
item7:
LI S0 0x9 ; Lucky Lamp Value
LI S3 LuckyLampPrice ; S3 contains Lucky Lamp Price
J payprice
NOP
itembag:
LI S3 ItemBagPrice ; S3 contains Item Bag Price
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

JAL GetRandomByte ; Integer at V0
NOP

LI A0 0x6 
SLT S0 V0 A0 ; S0 = 1 if the number is less than 6
NOP
BNE S0 R0 getKoopaKard ; Branch if true
NOP

LI A0 0xF 
SLT S0 V0 A0 ; S0 = 1 if the number is less than 15
NOP
BNE S0 R0 getLuckyCharm ; Branch if true
NOP

LI A0 0x21 
SLT S0 V0 A0 ; S0 = 1 if the number is less than 33
NOP
BNE S0 R0 getBowserPhone ; Branch if true
NOP

LI A0 0x33
SLT S0 V0 A0 ; S0 = 1 if the number is less than 51
NOP
BNE S0 R0 getBowserSuit ; Branch if true
NOP

LI A0 0x45
SLT S0 V0 A0 ; S0 = 1 if the number is less than 69
NOP
BNE S0 R0 getLuckyLamp ; Branch if true
NOP

LI A0 0x74
SLT S0 V0 A0 ; S0 = 1 if the number is less than 116
NOP
BNE S0 R0 getWarpBlock ; Branch if true
NOP

LI A0 0xA3
SLT S0 V0 A0 ; S0 = 1 if the number is less than 163
NOP
BNE S0 R0 getPoisonMushroom ; Branch if true
NOP

LI A0 0xD2
SLT S0 V0 A0 ; S0 = 1 if the number is less than 210
NOP
BNE S0 R0 getMushroom ; Branch if true
NOP

J getSkeletonKey
NOP

getKoopaKard:
LI T0 0xF ; Koopa Kard value
SB T0 0(S1)
J sound
NOP

getLuckyCharm:
LI T0 0x11 ; Lucky Charm value
SB T0 0(S1)
J sound
NOP

getBowserPhone:
LI T0 0x7 ; Bowser Phone value
SB T0 0(S1)
J sound
NOP

getBowserSuit:
LI T0 0xD ; Bowser Suit value
SB T0 0(S1)
J sound
NOP

getLuckyLamp:
LI T0 0x9 ; Lucky Lamp value
SB T0 0(S1)
J sound
NOP

getWarpBlock:
LI T0 0x5 ; Warp Block value
SB T0 0(S1)
J sound
NOP

getPoisonMushroom:
LI T0 0x2 ; Poison Mushroom value
SB T0 0(S1)
J sound
NOP

getMushroom:
LI T0 0x0 ; Mushroom value
SB T0 0(S1)
J sound
NOP

getSkeletonKey:
LI T0 0x1 ; Skeleton Key value
SB T0 0(S1)

sound:
JAL PlaySound
ADDIU A0 R0 0x10C ; 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 PlaySound
LI A0 0x10C ; 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 PlaySound
LI A0 0x29A ; Toad: Oooh :(

SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 0xD ; 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 0xD ; 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 0xD ; 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 0xD ; 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:
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
LastTurn_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Welcome to the"
.byte 0x06 ; Blue font
.ascii " Item Emporium"
.byte 0x08 ; White Font
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Sadly"
.byte 0x82 ; Comma (,)
.ascii " I"
.byte 0x5C ; Apostrophe (')
.ascii "m out of stock"
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Please come back another time"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause

.align 16
NoCoinsToShop_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Oh dear"
.byte 0x85,0x85,0x85 ; Period (...)
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "You don"
.byte 0x5C ; Apostrophe (')
.ascii "t have enough coins"
.byte 0x85 ; Period (.)
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Come back when you have more coins"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause

.align 16
TooManyItems_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "It seems you can"
.byte 0x5C ; Apostrophe (')
.ascii "t carry any more items"
.byte 0x85 ; Period (.)
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Come back later when you have space"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause

.align 16
Greeting_Message:
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Hello there"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Welcome to the"
.byte 0x06 ; Blue font
.ascii " Item Emporium"
.byte 0x08 ; White Font
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; Standard padding for picture
.ascii "Check out my wares"
.byte 0xC2 ; Exclamation Mark (!)
.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 an item"
.byte 0xC3 ; Question Mark (?)
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Little more for option indent
.byte 0x0C ; Start option
.byte 0x06 ; Blue font
.ascii "Mushroom    "
.byte 0x08 ; White Font
.byte 0x10 ; Space ( )
.byte 0x1A,0x1A,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 0x06 ; Blue font
.ascii "Warp Block   "
.byte 0x08 ; White Font
.byte 0x10 ; Space ( )
.byte 0x1A,0x1A,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 0x06 ; 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 0x06 ; Blue font
.ascii "Skeleton Key  "
.byte 0x08 ; White Font
.byte 0x1A,0x1A,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 0x06 ; Blue font
.ascii "Bowser Phone   "
.byte 0x10,0x10 ; Space 2x (  )
.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
.byte 0x06 ; Blue font
.ascii "Bowser Suit   "
.byte 0x08 ; White Font
.byte 0x1A,0x1A,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
.byte 0x06 ; Blue font
.ascii "Lucky Lamp   "
.byte 0x08 ; White Font
.byte 0x1A,0x1A,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
.byte 0x06 ; Blue font
.ascii "Item Bag  "
.byte 0x08 ; White Font
.byte 0x1A,0x1A,0x1A,0x1A,0x1A ; padding
.byte 0x29 ; Coin icon
.byte 0x3E ; x (multiply)
.ascii "30"
.byte 0x0D ; End option
.byte 0x0A ; Newline
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A 
.byte 0x0C ; Start option
.ascii "Nah"
.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 something else"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause