; NAME: A/B Prompt (MP1)
; GAMES: MP1_USA
; EXECUTION: Direct
; PARAM: Space|SpaceToFace

; This event is designed to initiate a prompt with two options - the
; options are selected by pressing either A or B. During the event,
; the player will face a space designated by the parameter
; "SpaceToFace".

; This event also makes use of a mini function to display a message.
; Follow the format for the arguments if you need to display more
; messages than just the prompt. Otherwise, the A/B Prompt will lead
; to two branches where you can insert your code for the two options.

ADDIU SP SP -64
SW RA 60(SP)
SW S0 56(SP)
SW S1 52(SP)
SW S2 48(SP)
SW S3 44(SP)
SW S4 40(SP)
SW S5 36(SP)
SW S6 32(SP)
SW S7 28(SP)

; S0 = Current Player Struct

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

;===Rotate Toward Space===
LI A0 -1 ; Set current player
LI A1 8 ; Rotate in 8 frames
JAL 0x8004D2A4 ; RotatePlayerModel
LI A2 SpaceToFace ; Face Space

;===Get Current Player Struct===
JAL GetPlayerStruct
LI A0 -1 ; Get current player struct at V0
MOVE S0 V0 ; Copy V0 to S0

;===Start Prompt===
StartPrompt:
LI A0 1 ; Display prompt
LUI A1 hi(Prompt)
ADDIU A1 A1 lo(Prompt) ; Display Prompt
MOVE A2 S0 ; Pass current player struct on A2
JAL DisplayMessage
LI A3 4 ; Pass y axis length on A3

; If you want to display a message, use the mini func DisplayMessage.
; The value of A0 will be 0 for a message or 1 for a prompt.
; The value of A1 will be your message address.
; The value of A2 does not change (always use MOVE A2 S0).
; The value of A3 is the number of lines your message has.

; Change the player's destination based on the choice.
BEQZ V0 OptionOne ; If V0 = 0, say yes
NOP
LI T0 1
BEQ V0 T0 OptionTwo ; If V0 = 1, say no
NOP

;===Option One===
OptionOne:

; Insert your Option 1 code here



J exit
NOP

;===Option Two===
OptionTwo:

; Insert your Option 2 code here



J exit
NOP

exit:
LW RA 60(SP)
LW S0 56(SP)
LW S1 52(SP)
LW S2 48(SP)
LW S3 44(SP)
LW S4 40(SP)
LW S5 36(SP)
LW S6 32(SP)
LW S7 28(SP)
JR RA
ADDIU SP SP 64

;===Mini Func to Display Message===
.align 4
DisplayMessage:
ADDIU SP SP -48
SW RA 44(SP)
SW S0 40(SP)
SW S1 36(SP)
SW S2 32(SP)
SW S3 28(SP)

; A0 / S0 = Message or Prompt / Prompt Choice
; A1 / S1 = Message Address
; A2 / S2 = Current Player Struct
; A3 = Y Axis Length
; S3 = Window ID

MOVE S0 A0 ; Copy A0 to S0
MOVE S1 A1 ; Copy A1 to S1
MOVE S2 A2 ; Copy A2 to S2

; Pass y axis length on A3
LI A0 40 ; Upper left x coord
LI A1 60 ; Upper left y coord
JAL CreateTextWindow
LI A2 20 ; X Axis length
MOVE S3 V0 ; Copy V0 to S3

MOVE A0 S3 ; Pass window ID on A0
MOVE A1 S1 ; Pass message address on A1
LI A2 -1
JAL LoadStringIntoWindow
LI A3 -1

MOVE A0 S3 ; Pass window ID on A0
JAL SetTextCharsPerFrame
LI A1 0 ; 0 frames between characters

JAL ShowTextWindow
MOVE A0 S3 ; Pass window ID on A0

BEQZ S0 SkipSelection ; If displaying message, skip selection
NOP
JAL ABPrompt
MOVE A0 S2 ; Pass current player struct on A0
MOVE S0 V0 ; Copy V0 to S0

JAL PlaySound
ADDIU A0 V0 0xF7 ; Confirmation or Decline sound

SkipSelection:
MOVE A0 S3 ; Pass window ID on A0
JAL 0x8004DBD4 ; BasicTextConfirmation
LBU A1 0x18(S2) ; Pass current player index on A1

JAL HideTextWindow
MOVE A0 S3 ; Pass window ID on A0
MOVE V0 S0 ; Copy S0 to V0

LW RA 44(SP)
LW S0 40(SP)
LW S1 36(SP)
LW S2 32(SP)
LW S3 28(SP)
JR RA
ADDIU SP SP 48

;===Mini Func for A/B Prompt===
.align 4
ABPrompt:
ADDIU SP SP -40
SW RA 36(SP)
SW S0 32(SP)

; A0 / S0 = Current Player Struct
; V0 = Prompt Choice

MOVE S0 A0 ; Copy A0 to S0
JAL PlayerIsCPU
LBU A0 0x18(S0) ; Pass current player index on A0

BNEZ V0 CPULogic ; If V0 = 1, go to CPULogic
NOP
; else, player is human, check for button inputs

ABPromptLoop:
JAL DetectABInput
LBU A0 3(S0) ; Pass player controller port on A0

LI T0 1
BEQ V0 T0 ABPromptExit ; If V0 = 1, player pressed B, exit
NOP
LI T0 2
BEQL V0 T0 ABPromptExit ; If V0 = 2, player pressed A, exit
LI V0 0 ; Select yes (on likely)
; else, no button was pressed

JAL SleepProcess
LI A0 3 ; Wait 3 frames

J ABPromptLoop
NOP

CPULogic:
JAL SleepProcess
LI A0 10 ; Wait 10 frames for textbox to show

JAL RNGPercentChance
LI A0 50 ; 50% chance of V0 = 1
; CPUs have a 50% chance of selecting Option 2

ABPromptExit:
LW RA 36(SP)
LW S0 32(SP)
JR RA
ADDIU SP SP 40

;===Mini Func to Detect A/B Input===
.align 4
DetectABInput:
ADDIU SP SP -32
SW RA 28(SP)

; A0 = Player Controller Port
; V0 = Button Press (1 for B, 2 for A)

LUI T0 0x800E ; 0x800E0000
ORI T0 T0 0xD3F4 ; 0x800ED3F4, P1 Button Input Address
SLL A0 A0 3 ; Shift left by 3 (multiply by 8)
ADDU T0 T0 A0 ; Add to get current player's button input address

LBU V0 0(T0) ; Load player button input into V0
SRL V0 V0 6 ; Shift right by 6 (0x80 > 0x02, 0x40 > 0x01)

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

;===Message Text===
.align 16
Prompt:
.ascii "What do you want to do"
.byte 0xC3 ; Question mark (?)
.byte 0x0A,0x0E,0x0E ; New line + option indent
.byte 0x21,0x0E ; A button, Tab
.ascii "Option 1"
.byte 0x0A,0x0E,0x0E ; New line + option indent
.byte 0x22,0x0E ; B button, Tab
.ascii "Option 2"
.byte 0 ; End prompt

; 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 ; Multiply (x)
; .byte 0xFF,0 ; Wait, press A to confirm