; NAME: Warp to Opponent (MP1) v1.1
; GAMES: MP1_USA
; EXECUTION: Direct
; PARAM: Space|SpaceToFace

; This event is designed to warp the player to an opponent's space.
; The opponent is decided randomly. There is one message which is
; displayed before the screen fades out.

; During the event, the player will face the space designated by the
; parameter "SpaceToFace".

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

; S0 = Current Player Struct
; S1 = Window ID / Opponent Player Struct / Opponent Player's Space Index

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

;===Display Message===
LI A0 40 ; Upper left x coord
LI A1 60 ; Upper left y coord
LI A2 20 ; X axis length
JAL CreateTextWindow
LI A3 2 ; Y axis length
MOVE S1 V0 ; Copy V0 to S1

MOVE A0 S1 ; Pass window ID on A0
LUI A1 hi(Message)
ADDIU A1 A1 lo(Message) ; Display Message
LI A2 -1
JAL LoadStringIntoWindow
LI A3 -1

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

JAL ShowTextWindow
MOVE A0 S1 ; Pass window ID on A0

MOVE A0 S1 ; Pass window ID on A0
JAL 0x8004DBD4 ; BasicTextConfirmation
LBU A1 0x18(S0) ; Pass current player index on A1

JAL HideTextWindow
MOVE A0 S1 ; Pass window ID on A0

;===Fade-out and Sound===
LI A0 4 ; Assigns Happening fade-out
JAL 0x800726AC ; InitFadeOut
LI A1 16 ; Assigns 16 frames of fade-out

JAL SleepProcess
LI A0 17 ; Wait 17 frames for fade-out

JAL PlaySound
LI A0 0x47 ; Warp Block sound

JAL SleepProcess
LI A0 20 ; Wait 20 frames for sound

;===Randomise Opponent===
JAL GetRandomByte ; Get random byte (0-255) at V0
NOP
LI T0 3
DIVU V0 T0 ; Divide V0 by T0
MFHI V0 ; Move the remainder (0-2) into V0
LBU T0 0x18(S0) ; Load current player index into T0
BEQL T0 R0 Continue ; If current player is P1, continue
ADDIU V0 V0 1 ; Add 1 to get opponent index (1-3) (on likely)
LI T1 3 ; P4 index
BEQ T0 T1 Continue ; If current player is P4, continue
NOP
; else, current player is P2 or P3 (might need correcting)
BEQL V0 T0 Continue ; If selecting current player, continue
LI V0 3 ; Select P4 instead (on likely)
; else, didn't select current player, no correcting needed

;===Warp to Opponent===
Continue:
JAL GetPlayerStruct
MOVE A0 V0 ; Get opponent player struct at V0
MOVE S1 V0 ; Copy V0 to S1

LI A0 -1 ; Set current player
LHU A1 0xE(S1) ; Pass opponent player chain index on A1
JAL SetPlayerOntoChain
LHU A2 0x10(S1) ; Pass opponent player chain space index on A2

LI A0 -1 ; Set current player
LHU A1 0x12(S1) ; Pass opponent player next chain index on A1
JAL SetNextChainAndSpace
LHU A2 0x14(S1) ; Pass opponent player next chain space index on A2

LHU A0 0xE(S0) ; Pass destination chain index on A0
JAL GetAbsSpaceIndexFromChainSpaceIndex
LHU A1 0x10(S0) ; Pass destination chain space index on A1
MOVE S1 V0 ; Copy V0 to S1

JAL GetSpaceData
MOVE A0 S1 ; Get destination space data at V0
LW T0 4(V0) ; Load space x coords into T0
LW T1 12(V0) ; Load space y coords into T1

LW T2 0x20(S0) ; Load current player coords pointer into T2
SW T0 12(T2) ; Store opponent player x coords into pointer
SW T1 20(T2) ; Store opponent player y coords into pointer

;===Finish Warp===
LI A0 -1 ; Set current player
LI A1 1 ; Rotate in 1 frame
JAL 0x8004D2A4 ; RotatePlayerModel
MOVE A2 S1 ; Face destination space (south)

LI A0 2 ; Assigns star fade-in
JAL 0x80072644 ; InitFadeIn
LI A1 16 ; Assigns 16 frames of fade-in

JAL SleepProcess
LI A0 17 ; Wait 17 frames for fade-in

;===Play Happy Voice and Animation===
LBU A0 4(S0) ; Load character value from offset of player struct
JAL PlaySound
ADDIU A0 A0 0x451 ; Add character value to Mario's happy voice
; Character voices are their "character value" distance away from
; Mario's, e.g. Luigi's character value = 1, so Luigi's happy voice
; is 1 away from Mario's.
; 0x451 = Happy Voice, 0x44A = Sad Voice

LI A0 -1 ; Set current player
LI A1 5 ; Set happy animation
JAL 0x80052BE8 ; SetBoardPlayerAnimation
LI A2 0 ; Do not loop animation

JAL SleepProcess
LI A0 40 ; Wait 40 frames to play animation

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

;===Message Text===
.align 16
Message:
.ascii "You"
.byte 0x5C ; Apostrophe (')
.ascii "re being"
.byte 0x04 ; Purple font
.ascii " warped"
.byte 0x08 ; White font
.ascii " to"
.byte 0x0A ; New line (writes below)
.ascii "an opponent"
.byte 0x5C ; Apostrophe (')
.ascii "s space"
.byte 0xC2 ; Exclamation mark (!)
.byte 0xFF,0 ; Wait, press A to confirm

; 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