freeslot("MT_TIKAL_ORB", "MT_TIKAL_THISWAY", "MT_TIKAL_LINETRIGGER", "S_TIKAL_ORB", "S_TIKAL_ORB2", "S_TIKAL_THATWAY", "S_TIKAL_THISWAY", "SPR_TORB", "sfx_disway", "MT_TIKAL_ORB_FINDER", "S_TIKAL_WAIT", "S_TIKAL_RESPAWN", "S_ORB_FINDER", "S_ORB_FINDER_FINDEMERALD", "S_ORB_READY", "S_ORB_WAITING1", "S_ORB_WAITING2", "S_ORB_FLY", "S_TIKAL_NOTHATWAY", "S_TIKAL_LOOP", "S_ORB_GHOST", "S_ORB_THEREYET", "S_ORB_FLY2", "S_ORB_GHOST2", "S_ORB_THEREYET2")

--- Tikal's Emerald Locator Orb by Glaber
// placeholder sprites riped by Proman01
//Coding and instruction by Glaber and Midiman

local function CustomTikalSpawn(mobj, mapthing)
    local offset
    
    if mapthing.z != 0
        offset = mapthing.z
    else
        offset = 33*FRACUNIT
    end
    
    if (mapthing.options & MTF_OBJECTFLIP)
        if offset != 0
            mobj.z = mobj.z - offset
        else
            mobj.z = mobj.ceilingz
        end
    else
        if offset != 0
            mobj.z = mobj.z + offset
        else
            mobj.z = mobj.floorz
        end
    end
    
end

addHook("MapThingSpawn", CustomTikalSpawn, MT_TIKAL_ORB)

// Tikal Hint orb
mobjinfo[MT_TIKAL_ORB] = {
	//$Name "Tikal Hint Orb"
	//$Sprite TORBA0
	//$Category Sonic Adventure
	doomednum = 339,
	spawnstate = S_TIKAL_ORB,
	spawnhealth = 1000,
	reactiontime = 8,
	deathstate = S_TIKAL_THATWAY,
	deathsound = sfx_disway,
	speed = 8,
	radius = 8*FRACUNIT,
	height = 16*FRACUNIT,
	mass = 16,
	flags = MF_SPECIAL|MF_FLOAT|MF_NOGRAVITY
}

states[S_TIKAL_ORB] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|A, 3, nil, 0, 0, S_TIKAL_ORB2}
states[S_TIKAL_ORB2] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|B, 3, nil, 0, 0, S_TIKAL_ORB}
states[S_TIKAL_THATWAY] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|A, 3, nil, 0, 1, S_TIKAL_NOTHATWAY}
states[S_TIKAL_NOTHATWAY] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|B, 3, nil, 0, 1, S_TIKAL_LOOP}
states[S_TIKAL_LOOP] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|A, 1, A_Repeat, 6, S_TIKAL_THATWAY, S_TIKAL_THISWAY}
states[S_TIKAL_THISWAY] = {SPR_NULL, A, 1, A_SpawnObjectRelative, 0, (0<<16)+MT_TIKAL_ORB_FINDER, S_TIKAL_WAIT}
states[S_TIKAL_WAIT] = {SPR_NULL, A, 80, nil, 0, 1, S_TIKAL_RESPAWN}
states[S_TIKAL_RESPAWN] = {SPR_NULL, A, 1, A_SpawnObjectRelative, 0, (0<<16)+MT_TIKAL_ORB, S_NULL}

// Tikal Orb shard finder

//Needs to check for how close the orb is to the emerald shard and if its target still exists.
//If the shard is gone, the orb should dissapear.

local ORB_SPEED = 30*FRACUNIT

// Checks for how close the orb is to the emerald shard and if its target still exists
local function Orb_CheckTarget(actor, var1, var2)
	if not (actor and actor.valid) then return end
	
	// If the shard is gone, the orb should disappear
	if not (actor.target and actor.target.valid) then
		P_RemoveMobj(actor)
		return
	end
	
	// Store the orb's target and the current distance between the two in variables
	// to help with readability
	local target = actor.target
	local dist = FixedHypot(FixedHypot(target.x - actor.x, target.y - actor.y), target.z - actor.z)
	
	// Check if the orb is less than 1.5x its speed from its target
	if dist <= FixedMul(3*ORB_SPEED/2, actor.scale) then
		// If the orb is close to the shard, the orb should disappear
		P_RemoveMobj(actor)
	end
end

//Ghosting effect With help from MACHTURNE
addHook("MobjThinker", function(mo)
  if not (mo and mo.valid) then return end
  P_SpawnGhostMobj(mo)
end, MT_TIKAL_ORB_FINDER)

//The Orb
mobjinfo[MT_TIKAL_ORB_FINDER] = {
	doomednum = -1,
	spawnstate = S_ORB_FINDER,
	spawnhealth = 1000,
	reactiontime = 8,
	deathstate = S_NULL,
	speed = 8,
	radius = 8*FRACUNIT,
	height = 16*FRACUNIT,
	mass = 16,
	flags = MF_RUNSPAWNFUNC|MF_NOCLIP|MF_NOCLIPHEIGHT|MF_FLOAT|MF_NOGRAVITY
}

states[S_ORB_FINDER] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|A, 1, nil, 0, 0, S_ORB_FINDER_FINDEMERALD}
states[S_ORB_FINDER_FINDEMERALD] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|A, 1, A_FindTarget,  MT_EMERHUNT, 0, S_ORB_READY}
states[S_ORB_READY] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|A, 1, A_PlaySound, sfx_spdpad, 0, S_ORB_WAITING1}
states[S_ORB_WAITING1] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|B, 3, nil, 0, 0, S_ORB_WAITING2}
states[S_ORB_WAITING2] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|A, 3, nil, 0, 0, S_ORB_FLY}
states[S_ORB_FLY] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|B, 1, A_HomingChase, ORB_SPEED, 0, S_ORB_GHOST}
states[S_ORB_GHOST] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|B, 1, nil, 0, 0, S_ORB_THEREYET}
states[S_ORB_THEREYET] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|B, 1, Orb_CheckTarget, 0, 0, S_ORB_FLY2}
states[S_ORB_FLY2] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|A, 1, A_HomingChase, ORB_SPEED, 0, S_ORB_GHOST2}
states[S_ORB_GHOST2] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|A, 1, nil, 0, 0, S_ORB_THEREYET2}
states[S_ORB_THEREYET2] = {SPR_TORB, FF_FULLBRIGHT|FF_TRANS20|A, 1, Orb_CheckTarget, 0, 0, S_ORB_FLY}
