; NAME: Warp (MP1) v2
; GAMES: MP1_USA
; EXECUTION: Direct
; PARAM: Space|SpaceToFace
; PARAM: Space[]|Destinations
; PARAM: Space[]|DestNext

; This event is designed to warp the player to another space. Any
; number of destinations is supported, with the destination being
; chosen randomly. The parameter "Destinations" should be set to each
; of the target spaces, while the parameter "DestNext" should be the
; next space the player will walk toward once they've warped. Ensure
; that the order of spaces set for both of these parameter arrays is
; the same. During the event, the player will face the space
; designated by the parameter "SpaceToFace".

ADDIU SP SP -48
SW RA 44(SP)
SW S0 40(SP)
SW S1 36(SP)
SW S2 32(SP)

; S0 = Current Player Struct
; S1 = Destination Array / Window ID
; S2 = Randomised Destination

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

;===Fade-out and Sound===
LI A0 1 ; Assigns circle 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

;===Determine Destination===
JAL GetRandomByte ; Get random byte (0-255) at V0
NOP
LI T0 Destinations_length ; Load no. of destinations into T0
DIVU V0 T0 ; Divide V0 by T0
MFHI S2 ; Move the remainder (T0 - 1) into S2
SLL S2 S2 1 ; Shift left by 1 (multiply by 2)

LUI S1 hi(DestinationArray)
ADDIU S1 S1 lo(DestinationArray)
ADDU S1 S1 S2 ; Add random number to DestinationArray

;===Set Destination===
LUI T1 hi(DestChainArray)
ADDIU T1 T1 lo(DestChainArray)
ADDU T1 T1 S2 ; Add random number to DestChainArray

LUI T2 hi(DestChainSpaceArray)
ADDIU T2 T2 lo(DestChainSpaceArray)
ADDU T2 T2 S2 ; Add random number to DestChainSpaceArray

LI A0 -1 ; Set current player
LHU A1 0(T1) ; Pass Destination chain index on A1
JAL SetPlayerOntoChain
LHU A2 0(T2) ; Pass Destination chain space index on A2

;===Set Dest Next===
LUI T1 hi(DestNextChainArray)
ADDIU T1 T1 lo(DestNextChainArray)
ADDU T1 T1 S2 ; Add random number to DestNextChainArray

LUI T2 hi(DestNextChainSpaceArray)
ADDIU T2 T2 lo(DestNextChainSpaceArray)
ADDU T2 T2 S2 ; Add random number to DestNextChainSpaceArray

LI A0 -1 ; Set current player
LHU A1 0(T1) ; Pass Destination next chain index on A1
JAL SetNextChainAndSpace
LHU A2 0(T2) ; Pass Destination next chain space index on A2

;===Set Player Coords===
JAL GetSpaceData
LHU A0 0(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 coords pointer from offset of player struct
SW T0 12(T2) ; Store space x coords into player coords
SW T1 20(T2) ; Store space y coords into player coords

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

LI A0 1 ; Assigns circle 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

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

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

;===Destination Arrays===
.align 16
DestinationArray:
.halfword Destinations ; Destination space indexes

.align 16
DestChainArray:
.halfword Destinations_chain_indices ; Destination chain indexes

.align 16
DestChainSpaceArray:
.halfword Destinations_chain_space_indices ; Destination chain space indexes

.align 16
DestNextChainArray:
.halfword DestNext_chain_indices ; Destination next chain indexes

.align 16
DestNextChainSpaceArray:
.halfword DestNext_chain_space_indices ; Destination next chain space indexes

;===Message Text===
.align 16
Message:
.ascii "You were"
.byte 0x04 ; Purple font
.ascii " warped"
.byte 0x08,0x0A ; White font, New line (writes below)
.ascii "to another 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