; NAME: Space Warp (Mario Party 2)
; GAMES: MP2_USA
; EXECUTION: Direct
; PARAM: Space|Destination
; PARAM: Space|Next_Space

ADDIU sp, sp, -96
SW s0, 92(sp)
SW s1, 88(sp)
SW s2, 84(sp)
SW s3, 80(sp)
SW s4, 76(sp)
SW s5, 72(sp)
SW ra, 68(sp)

;CODE BY SPONGYOSHI================================================
;Free to use in your own boards!
;Based off code by Airsola (main) and Rain (function)
;Set the Destination space as where you want the character to warp to
;Set the Next_Space space as the space after the Destination space
;Be sure that you apply it to a space type as the same as the destination
;Let the code do the rest and happy warping!!
;CODE BY SPONGYOSHI================================================

;FADE-OUT START------------------------------------------------------
LI A0 1 ; Circle Fade-out 
JAL InitFadeOut
LI A1 10 ; Fade Out for 10 Frames

JAL SleepProcess 
LI A0 10 ; Number of frames the game waits for fade to finish
;FADE-OUT END--------------------------------------------------------

;WARP START----------------------------------------------------------
LI A0 Destination ;Send the destination to Rain's function
JAL abs_space_index_to_chain_and_space_index
NOP

MOVE S1 V0 ;Put the destination chain index in S1
MOVE S2 V1 ;Put the destination space index in S2

LI A0 Next_Space ;Send the next space to Rain's function
JAL abs_space_index_to_chain_and_space_index
NOP

MOVE S0 V0 ;Put the destination chain index in S0
MOVE S4 V1 ;Put the destination space index in S4

JAL GetCurrentPlayerIndex
NOP

MOVE A0 V0 
MOVE A1 S1 
JAL SetPlayerOntoChain ;Set current player space to destination
MOVE A2 S2

JAL GetCurrentPlayerIndex
NOP

MOVE A0 V0
MOVE A1 S0
JAL SetNextChainAndSpace ;Set current player next space to next space
MOVE A2 S4
;WARP END------------------------------------------------------------

;VISUAL START--------------------------------------------------------
JAL GetPlayerStruct
LI A0 -1 ;Get current player struct
MOVE s0, v0 ;And put it in S0

LI a0, Destination //space destination index
JAL GetSpaceData ; Pointer to Space Data at V0
NOP

LW t0, 0x0008 (v0) // load space x pos
LW t1, 0x0010 (v0) // load space z pos
LI t2, 0x41200000 // float 10
MTC1 t2, f0 ; move 10 to f0
MTC1 t1, f1 ; move space z pos to f1
ADD.S f0, f0, f1 ; add 10 to space z pos
MFC1 t1, f0 ; move result to t1
LW v1, 0x0024 (s0) ;load player obj pointer
SW t0, 0x000C (v1) ;store new x pos
SW t1, 0x0014 (v1) ;store new z pos
;VISUAL 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/2VMDufgY
JAL PlaySound 
LI A0 0x36A ; Warp Block Sound Index

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

;FADE-IN & SOUND START-----------------------------------------------
LI A0 1 ; Circle Fade-in
JAL InitFadeIn
LI A1 10 ; Fade In for 10 Frames

JAL PlaySound 
LI A0 0x3EE ; Warp Block Sound Index

JAL SleepProcess 
LI A0 10 ; Number of frames the game waits for fade to finish
;FADE-IN & SOUND END-------------------------------------------------

;WINDOW START--------------------------------------------------------
SW R0 16(SP)
SW R0 20(SP)
SW R0 24(SP)
LUI A1 hi(Warp_Message)
ADDIU A1 A1 lo(Warp_Message)
LI A0 -1 ;No Image
LI A2 0
JAL 0x80056368 ; ShowMessage
LI A3 0
JAL CloseMessage
NOP
;WINDOW END----------------------------------------------------------

LW s0, 92(sp)
LW s1, 88(sp)
LW s2, 84(sp)
LW s3, 80(sp)
LW s4, 76(sp)
LW s5, 72(sp)
LW ra, 68(sp)
JR RA
ADDIU sp, sp, 96

; Overwrite with any message you'd like
; Then use the following link to add color or special characters
; https://github.com/PartyPlanner64/PartyPlanner64/wiki/String-Encoding
.align 16
Warp_Message:
.byte 0x05 ; Green Font
.ascii "You"
.byte 0x5C ; Apostrophe (')
.ascii "ve been warped"
.byte 0x85 ; Period (.)
.byte 0xFF,0 ; FF=Pause

.align 4
abs_space_index_to_chain_and_space_index:
//mp2
//takes arg a0, abs_space_index
//returns chain_index to v0
//returns space_index to v1
ADDIU sp, sp, -96
SW s0, 92(sp)
SW s1, 88(sp)
SW s2, 84(sp)
SW s3, 80(sp)
SW s4, 76(sp)
SW s5, 72(sp)
SW ra, 68(sp)
ORI s3, r0, 0x0000 //init s3 to 0
ADDIU s4, r0, 0xFFFF //init s4 to FFFF, loop1 counter
LUI s0, 0x800E
LW s0, 0x18D8 (s0) //load ptr to beginning of hydrated chains ptr
DADDU s1, a0, r0 ;move abs_space_index to s0
Loop:
ADDIU s4, s4, 0x0001 //add 1 to current loop total
LHU s2, 0x0000 (s0) //load total number of space_indicies in chain
BEQ s2, r0, error
NOP
ORI t0, r0, 0x0000 //going to be used for loop 2 counter
LW s3, 0x0004 (s0) //load pointer that points to abs space_indicies for current chain in loop
Loop2:
//
LHU t1, 0x0000 (s3) //load abs value from hydrated chain
BEQ t1, s1, isabs
NOP
ADDIU t0, t0, 0x0001 //add 1 to loop2 counter
BEQ t0, s2, backtoLoop1
NOP
BEQ r0, r0, Loop2
ADDIU s3, s3, 0x0002
returnvalues:
//
exit:
LW s0, 92(sp)
LW s1, 88(sp)
LW s2, 84(sp)
LW s3, 80(sp)
LW s4, 76(sp)
LW s5, 72(sp)
LW ra, 68(sp)
JR RA
ADDIU sp, sp, 96
error:
ORI v0, r0, 0xFFFF //error, return FFFF
BEQ r0, r0, exit
ORI v1, r0, 0xFFFF //error, return FFFF
isabs:
//s4 holds current chain index
//t1 holds current space index in chain
ADD v0, s4, r0 //move chain index to v0
BEQ r0, r0, exit
ADD v1, t0, r0 //move space_index to v1
backtoLoop1:
BEQ r0, r0, Loop
ADDIU s0, s0, 0x0008 //advance to next hydrated chains ptr