; NAME: Space Warp v3 (Airsola™)
; GAMES: MP3_USA
; EXECUTION: Direct
; PARAM: Space|Destination
; PARAM: Space|PreviousSpace
; PARAM: Space|NextSpace

;====================================================================
; IMPORTANT: This code is specifically designed to warp a player who
;            lands on a Space into a Space of the same type.
;            This is due to limitations on how warps execute.
;
;            Destination is where the player will warp to.
;            PreviousSpace is the space the player will step into if
;            they go in reverse after the warp.
;            NextSpace is the next space that the player will step
;            into if they roll normally.
;====================================================================

ADDIU SP SP -80
SW RA 76(SP)
SW S4 72(SP) ; Current Player's Index
SW S5 68(SP) ; Current Player's Struct

;FADE-OUT START------------------------------------------------------
LI A0 4 ; Happening Space Fade-out 
JAL InitFadeOut
LI A1 16 ; Fade Out for 16 Frames

JAL SleepProcess
LI A0 16 ; Sleep 16 Frames
;FADE-OUT END--------------------------------------------------------

JAL PlaySadVoice ; Plays Current Player Character's Scream
NOP

JAL SleepVProcess
NOP

;WARP START----------------------------------------------------------
JAL GetCurrentPlayerIndex ; Player Index located at V0
NOP
MOVE S4 V0 ; Copy Current Player's Index to S4

MOVE A0 S4 ; Assigns Current Player Index to A0
LI A1 Destination_chain_index ; Assigns Chain Index to A1
JAL SetPlayerOntoChain
LI A2 Destination_chain_space_index ; Assigns Space Index to A2

MOVE A0 S4 ; Assigns Current Player Index to A0
LI A1 PreviousSpace_chain_index ; Assigns Chain Index to A1
JAL SetPrevChainAndSpace
LI A2 PreviousSpace_chain_space_index ; Assigns Space Index to A2

MOVE A0 S4 ; Assigns Current Player Index to A0
LI A1 NextSpace_chain_index ; Assigns Chain Index to A1
JAL SetNextChainAndSpace
LI A2 NextSpace_chain_space_index ; Assigns Space Index to A2

JAL GetPlayerStruct ; Player Struct at V0
LI A0 -1 ; Current Player
MOVE S5 V0

LI A0 Destination_chain_index
JAL GetAbsSpaceIndexFromChainSpaceIndex ; Gets True Space Index at V0
LI A1 Destination_chain_space_index

JAL GetSpaceData ; Pointer to Space Data at V0
MOVE A0 V0 ; True Space Index

ADDIU V0 V0 8 ; This gets the X pos of the space
LW V1 0(V0) 
MOVE T0 V1 ; Copy to T0

ADDIU V0 V0 8 ; This gets the Y pos of the space
LW V1 0(V0)
MOVE T1 V1 ; Copy to T1

LI T2 0x41200000 ; Float value for 10?
MTC1 T2 F0 ; Move Float to F1?
MTC1 T1 F1 ; Move Space Y Pos float to F1?
ADD.S F0 F0 F1 ; Add the floats for maximum floatiness
MFC1 T1 F0 ; Move total to T1?

LW V1 0x24(S5) ; Load Offset 24 into V1
ADDIU V1 V1 0xC ; Add 0xC to get current player's X Coords
SW T0 0(V1) 

ADDIU V1 V1 8 ; This should load current player's Y Coords
SW T1 0(V1)
;WARP END------------------------------------------------------------

;SOUND START---------------------------------------------------------
; 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
JAL PlaySound 
LI A0 0x107 ; Warp Block Sound Index

JAL SleepProcess 
LI A0 15 ; Number of frames the game waits for sound to finish

JAL PlaySound 
LI A0 0x107 ; Warp Block Sound Index

JAL SleepProcess 
LI A0 15 ; Number of frames the game waits for sound to finish
;SOUND END-----------------------------------------------------------

;FADE-IN START-------------------------------------------------------
LI A0 4 ; Happening Space Fade-in
JAL InitFadeIn
LI A1 16 ; 16 Frames of Fade-in

JAL SleepProcess
LI A0 31 ; Sleep for 31 Frames
;FADE-IN END---------------------------------------------------------

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

JAL CloseMessage
NOP
;MESSAGE DISPLAY END-------------------------------------------------



LW RA 76(SP)
LW S4 72(SP)
LW S5 68(SP)
JR RA
ADDIU SP SP 80

; .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
Message:
.ascii "You"
.byte 0x5C ; Apostrophe (')
.ascii "ve been warped to another space"
.byte 0xC2 ; Exclamation Mark (!)
.byte 0xFF,0 ; FF=Pause (THIS ALWAYS GOES AT THE END)

; 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

.align 4
PlaySadVoice:
ADDIU SP SP -32
SW RA 28(SP)

JAL GetCurrentPlayerIndex
NOP 

LUI T1 hi(p1_char)
ADDIU T1 T1 lo(p1_char) ; Load Player 1's Character address
LI T0 0x38 ; Distance between each player's Character address
MULT V0 T0 ;  Multiply Current Player Index by T0
MFLO T0 ; Move the result to T0

ADDU T1 T1 T0 ; Add result to get current Player's Character address
LBU T0 0(T1) ; Load address value into T0

ADDIU V0 T0 0x287 ; Mario's Sad Voice Index

JAL PlaySound
MOVE A0 V0 ; Pass the Voice Index to A0

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