; NAME: Skeleton Key Path v1.4
; GAMES: MP2_USA
; EXECUTION: Direct
; PARAM: Space|KeySpace
; PARAM: Space|NoKeySpace

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

; This code is designed to allow you to use a Skeleton Key to access
; a different path or space towards the space parameter KeySpace. If
; the player doesn't have a Skeleton Key, they will just continue
; along the main path towards the space parameter NoKeySpace.

; S0 = Current Player Struct
; S1 = Prompt Choice

;===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

;===Check for Skeleton Key===
JAL GetPlayerStruct
LI A0 -1 ; Get current player struct at V0
MOVE S0 V0 ; Copy V0 to S0
LBU T0 0x19(S0) ; Load item value into T0 from offset of player struct
LI T1 0x01 ; Load Skeleton Key item index into T1
BEQ T0 T1 StartKey ; If item is Skeleton Key, start key
NOP

LUI T2 0x800F ; 0x800F0000
ORI T2 T2 0x8CE2 ; 0x800F8CE2, Multiple Item Slots Flag
LBU T2 0(T2) ; Load flag into T2
BEQZ T2 NoKey ; If T2 = 0, multiple item slots are OFF, go to NoKey
NOP
; else, multiple item slots are ON

LBU T0 0x1E(S0) ; Load reserve item #1 from offset of player struct
BEQ T0 T1 StartKey ; If item is Skeleton Key, start key
NOP
LBU T0 0x1F(S0) ; Load reserve item #2 from offset of player struct
BNE T0 T1 NoKey ; If item is not Skeleton Key, go to NoKey
NOP

;===Hide Dice Roll===
StartKey:
JAL 0x80046D2C ; HideDiceRoll
LBU A0 0x1C(S0) ; Pass current player index on A0

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

;===Give Prompt===
StartPrompt:
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 20 ; Character image (-1 for none, 20 is for Key)
; 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(SkeletonKeyPrompt)
ADDIU A1 A1 lo(SkeletonKeyPrompt) ; This loads "SkeletonKeyPrompt" into the Textbox
LI A2 0
JAL ShowMessage
LI A3 0

; 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.
BEQZ S1 yes
NOP
LI T0 1
BEQ S1 T0 ExitPrompt
NOP
; else pick "View map"
JAL 0x80103A64 ; ViewBoardMap
NOP
J StartPrompt
NOP

yes:
;===Key Says Farewell===
SW R0 16(SP) ; A4
SW R0 20(SP) ; A5
SW R0 24(SP) ; A6
LI A0 20 ; Character image (-1 for none, 20 is for Key)
; 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(FarewellKey)
ADDIU A1 A1 lo(FarewellKey) ; This loads "FarewellKey" into the Textbox
LI A2 0
JAL ShowMessage
LI A3 0

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

;===Remove Key from Inventory===
LBU T0 0x19(S0) ; Load item value from offset of player struct
LI T1 0x01 ; Skeleton Key index
LI T2 0xFF ; Load "no item" index into T2
BEQL T0 T1 SetKey ; If T0 = T1, set key
SB T2 0x19(S0) ; Store "no item" index into item slot (on likely)
LBU T0 0x1E(S0) ; Load reserve item #1 from offset of player struct
BEQL T0 T1 SetKey ; If T0 = T1, set key
SB T2 0x1E(S0) ; Store "no item" index into reserve slot #1 (on likely)
SB T2 0x1F(S0) ; Store "no item" index into reserve slot #2

;===Set Player Towards KeySpace===
SetKey:
LI A0 -1 ; Set current player
LI A1 KeySpace_chain_index
JAL SetNextChainAndSpace
LI A2 KeySpace_chain_space_index

;===Show Dice Roll===
ExitPrompt:
JAL 0x8004655C ; ShowDiceRoll
LBU A0 0x1C(S0) ; Pass current player index on A0

JAL SleepProcess
LI A0 10 ; Wait 10 frames for dice roll to show

BEQZ S1 exit ; If S1 = 0, used key, exit
NOP
; else, had no key

;===Set Player Towards NoKeySpace===
NoKey:
LI A0 -1 ; Set current player
LI A1 NoKeySpace_chain_index
JAL SetNextChainAndSpace
LI A2 NoKeySpace_chain_space_index

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

;===Message Text===
.align 16
SkeletonKeyPrompt:
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "I can take you to"
.byte 0x0A,0x1A,0x1A,0x1A,0x1A ; New line + character image padding
.ascii "the other path"
.byte 0xC2 ; Exclamation mark (!)
.ascii " Use me now"
.byte 0xC3 ; Question mark (?)
.byte 0x0A,0x0A ; Two new lines
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Character image + option indent
.byte 0x0C ; Start option
.ascii "Yes please"
.byte 0x0D ; End option
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Character image + option indent
.byte 0x0C ; Start option
.ascii "No thanks"
.byte 0x0D ; End option
.byte 0x0A ; New line (writes below)
.byte 0x1A,0x1A,0x1A,0x1A,0x1A,0x1A ; Character image + option indent
.byte 0x0C ; Start option
.ascii "View map"
.byte 0x0D ; End option
.byte 0

.align 16
FarewellKey:
.byte 0x1A,0x1A,0x1A,0x1A ; Padding for character image
.ascii "Okay then"
.byte 0x85 ; Period (.)
.ascii " Guess this is goodbye"
.byte 0x85,0x85,0x85 ; Period (.)
.byte 0xFF,0 ; Wait, press A to continue

; 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