; NAME: Bowser Item Shop
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: Number|MIN_COINS
; PARAM: Number|MAX_COINS

;====================================================================
; IMPORTANT: This code is specifically designed to give the landing
;            or passing player an item of your choice. If they have
;            no space to carry the item, they will receive nothing.
;===================================================================

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



;FIND CURRENT PLAYER START-------------------------------------------
JAL GetCurrentPlayerIndex ; Current Player Index at V0
NOP

LI S0 0x0 ; Start with Player 1 
LUI S1 hi(p1_item1)
ADDIU S1 S1 lo(p1_item1) ; S1 is the address of Player 1's First Item

START:
BEQ S0 V0 checkslots
NOP

ADDIU S0 S0 1 ; Add 1 to check the next Player
ADDIU S1 S1 0x38 ; This makes it load the next Player's First Item
J START
NOP
;FIND CURRENT PLAYER END---------------------------------------------

;CHECK ITEM SLOTS START----------------------------------------------
checkslots:
ADDIU S0 R0 0xFF ; 0xFF = No Item Value
LBU S2 0(S1) ; S2 is the value of the item in the first slot
BEQ S0 S2 itemmessage ; Go to itemmessage if the first slot is empty
NOP
ADDIU S1 S1 1 ; Add 1 to check Slot #2
LBU S2 0(S1) ; S2 is the value of the item in the second slot
BEQ S0 S2 itemmessage ; Go to itemmessage if the second slot is empty
NOP
ADDIU S1 S1 1 ; Add 1 to check Slot #3
LBU S2 0(S1) ; S2 is the value of the item in the third slot
BEQ S0 S2 itemmessage ; Go to itemmessage if the third slot is empty
NOP
; If the Player has 3 Items, they'll get the "No Item" message
;CHECK ITEM SLOTS END------------------------------------------------

;NO ITEM MESSAGE DISPLAY START---------------------------------------
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 -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(noitem)
ADDIU A1 A1 lo(noitem) ; This loads "noitem" into the Textbox
LI A2 0x0
JAL 0x800EC8EC ; ShowMessage
LI A3 0x0

; The rest here perform the "wait for confirmation"
; and probably messagebox teardown.
JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP

J exit ; Player got no item, exit
NOP
;NO ITEM MESSAGE DISPLAY END-----------------------------------------

;GOT ITEM MESSAGE DISPLAY START--------------------------------------
itemmessage:
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
ADDI A0 R0 0x5 ; 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(gotitem)
ADDIU A1 A1 lo(gotitem) ; This loads "gotitem" into the Textbox
LI A2 0x0
JAL 0x800EC8EC ; ShowMessage
LI A3 0x0

; The rest here perform the "wait for confirmation"
; and probably messagebox teardown.
JAL 0x800EC9DC
NOP
JAL 0x800EC6C8
NOP
JAL 0x800EC6EC
NOP
;GOT ITEM MESSAGE DISPLAY END----------------------------------------

;LOSE COINS START----------------------------------------------------
JAL GetRandomByte ; Generates a number from 0-255 into V0
NOP

ADDIU T0 R0 MAX_COINS ; T0 holds value of MAX_COINS
ADDIU T0 T0 1 
DIVU V0 T0 ; Divide the random number by T0
MFHI S0 ; HI contains the remainder, pull that into S0

JAL GetCurrentPlayerIndex ; Current Player's Index at V0
NOP

MOVE A0 V0 ; Assigns Current Player's Index to A0
ADDIU A1 S0 MIN_COINS ; A1 = S1 + MIN_COINS
SUBU A1 R0 A1 ; A1 = 0 - A1
JAL AdjustPlayerCoinsGradual
NOP

JAL GetCurrentPlayerIndex ; Current Player's Index at V0
NOP

MOVE A0 V0 ; Assigns Current Player's Index to A0
ADDIU A1 S0 MIN_COINS ; Add minimum amount of coins to remainder
SUBU A1 R0 A1 ; A1 = 0 - A1
JAL ShowPlayerCoinChange ; Shows how the amount on-screen
NOP
;GIVE COINS END------------------------------------------------------

;WAIT START----------------------------------------------------------
ADDIU A0 R0 30 ; Amount of frames the game waits for coin handouts
JAL SleepProcess
NOP
;WAIT END------------------------------------------------------------

JAL GetRandomByte ; Random Int at V0
NOP

;GIVE ITEM START-----------------------------------------------------
;LI S0 0x0E ; S0 contains Mushroom Value
;SB S0 0(S1) ; Place Mushroom on empty Item slot

; 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

ADDIU A0 R0 0x34 ; Is the number less than 52? (20% chance)
SLT S0 V0 A0
NOP

BNE S0 R0 getitemone ; If so, get item one.
NOP

; If we got here, it is not less than 52.

ADDIU A0 R0 0x67 ; Is it less than 103? (40% chance)
SLT S0 V0 A0
NOP

BNE S0 R0 getitemtwo ; If so, get item two.
NOP

; If we got here, it is not less than 103.

ADDIU A0 R0 0x9A ; Is it less than 154? (60% chance)
SLT S0 V0 A0
NOP

BNE S0 R0 getitemthree ; If so, get item three.
NOP

; If we got here, it is not less than 154.

ADDIU A0 R0 0xCD ; Is it less than 205? (80% chance)
SLT S0 V0 A0
NOP

BNE S0 R0 getitemfour ; If so, get item four.
NOP

; If we got here, it is not less than 205. Since we have a 20% chance
; of reaching this point, let's just get item five.

J getitemfive
NOP

; END DETERMINE ITEM CODE

; BEGIN GET ITEM CODE
; This code block gives the player one of the items chosen in
; the previous block. The item values can be changed to any value
; in this table:
; 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

getitemone:
LI S0 0x00 ; S0 contains Mushroom Value
SB S0 0(S1) ; Place Mushroom on empty Item slot
J Sound
NOP

getitemtwo:
LI S0 0x08 ; Dueling Glove value
SB S0 0(S1)
J Sound
NOP

getitemthree:
LI S0 0x02; Poison Mushroom value
SB S0 0(S1)
J Sound
NOP

getitemfour:
LI S0 0x05 ; Warp Block value
SB S0 0(S1)
J Sound
NOP

getitemfive:
LI S0 0x01 ; Skeleton Key value
SB S0 0(S1)
J Sound
NOP

; END GET ITEM CODE
;GIVE ITEM END-------------------------------------------------------

;SOUND START---------------------------------------------------------
LI A0 0x0A ; Turn Start Sound Index
; Use the Audio Player on PP64 to find other sound indexes to use
; Then use the following link to know what Sound Index you need
; https://pastebin.com/H1jxNFqr
Sound:
JAL PlaySound 
NOP

ADDIU A0 R0 0 ; Number of frames the game waits for sound to finish
JAL SleepProcess 
NOP
;SOUND END-----------------------------------------------------------



exit:
LW S1 32(SP)
LW S2 28(SP)
LW S0 24(SP)
LW RA 36(SP)
JR RA
ADDIU SP SP 40

; .ascii is used to output Text. Anything surrounded by " will be 
; displayed in the message (Special characters are not included).
; For those, you must use .byte and the respective byte value.
; Bytes that change text color will change the color of everything
; that is written after it, until another color is used.

.align 16
gotitem:
.byte 0x1A,0x1A,0x1A,0x1A ; indent
.ascii "Welcome"
.byte 0xC2 ; Exclamation Mark (!)
.ascii " This is the Item Shop"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; indent
.ascii "You"
.byte 0x5C ; Apostrophe (')
.ascii "ve come to the a great place"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; indent
.ascii "We"
.byte 0x5C ; Apostrophe (')
.ascii "ve got plenty of BARGAINS"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause

.align 16
noitem:
.byte 0x1A,0x1A,0x1A,0x1A ; indent
.ascii "What"
.byte 0x5C ; Apostrophe (')
.ascii "s that"
.byte 0xC3 ; Question Mark (?)
.byte 0x0A ; New Line (Writes Below)
.ascii " You"
.byte 0x5C ; Apostrophe (')
.ascii "ve got three Items"
.byte 0xC3 ; Question Mark (?)
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; indent
.ascii "Tch"
.byte 0xC2 ; Exclamation Mark (!)
.ascii " Speak up quicker"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0x0A ; New Line (Writes Below)
.byte 0x1A,0x1A,0x1A,0x1A ; indent
.ascii "Fine"
.byte 0xC2 ; Exclamation Mark (!)
.ascii " Come again"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause

; 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