// ------------------------------------------
// Lua_Tatl
// Handles Tatl
// ------------------------------------------
freeslot("MT_TATL", "MT_LINKSPARKLE",
"SPR_TATL", "SPR_LSPK",
"S_TATL1", "S_TATL2", "S_TATL3",
"S_LINKSPARKLE1", "S_LINKSPARKLE2", "S_LINKSPARKLE3", "S_LINKSPARKLE4", "S_LINKSPARKLE5",
"S_LINKSPARKLE6", "S_LINKSPARKLE7", "S_LINKSPARKLE8", "S_LINKSPARKLE9")

COM_AddCommand("toggletatl", function(player)
	if not(player and player.valid)
		return
	end
	
	if not(player.tatlon)
		player.tatlon = true
	else
		player.tatlon = false
	end
end)

COM_AddCommand("tatlcolor", function(player)
	if not(player and player.valid)
		return
	end
	
	if not(player.navicolors)
		player.navicolors = true
	else
		player.navicolors = false
	end
end)

// Sparkles
local function RandomOff(num)
	return (P_RandomByte()%num)-(num/2)
end
local function SpawnSparkles(mo, scale, size)
	local zoff = -32*scale*P_MobjFlip(mo)
	if(P_MobjFlip(mo))
		zoff = $1 + mo.height
	end
	local sparkle = P_SpawnMobj(mo.x+(RandomOff(size)*scale), mo.y+(RandomOff(size)*scale), mo.z+(size*scale/2)+(RandomOff(size)*scale)+zoff, MT_LINKSPARKLE)
	sparkle.scale = scale
	if(mo.eflags & MFE_VERTICALFLIP)
		sparkle.eflags = $1|MFE_VERTICALFLIP
	end
	sparkle.color = mo.color
end

addHook("ThinkFrame", function()
	for player in players.iterate
		// Tatl toggle
		if(player.tatlon == nil)
			player.tatlon = true
		end
		if(player.navicolors == nil)
			player.navicolors = false
		end
		
		if(player.mo and player.mo.valid and player.mo.skin == "linkandepona" and player.tatlon)
			// Tatl object
			if not(player.tatl and player.tatl.valid)
				player.tatl = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_TATL)
				player.tatlo = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_TATL)
			end
			
			// Tatl AI
			if(player.tatltargangle == nil)
				player.tatltargangle = player.mo.angle
			end
			if(player.tatlsin == nil)
				player.tatlsin = 0
			end
			player.tatltargangle = $1 + FRACUNIT*3
			player.tatlsin = $1 + FRACUNIT*20
			
			local tatldist = 20*player.mo.scale
			local tatldist2 = 4*player.mo.scale
			local targx = player.mo.x + FixedMul(cos(FixedAngle(player.tatltargangle)), tatldist)
			local targy = player.mo.y + FixedMul(sin(FixedAngle(player.tatltargangle)), tatldist)
			local targz = (player.mo.z+player.mo.height)+(45*player.mo.scale)+FixedMul(sin(FixedAngle(player.tatlsin)), tatldist2)
			
			local tox = FixedMul(player.tatl.x, FRACUNIT*60/100)+FixedMul(targx, FRACUNIT*40/100)
			local toy = FixedMul(player.tatl.y, FRACUNIT*60/100)+FixedMul(targy, FRACUNIT*40/100)
			local toz = FixedMul(player.tatl.z, FRACUNIT*60/100)+FixedMul(targz, FRACUNIT*40/100)
			
			player.tatl.angle = R_PointToAngle2(0, 0, tox-player.tatl.x, toy-player.tatl.y)
			player.tatlo.angle = player.tatl.angle
			
			local spd = R_PointToDist2(0, 0, tox-player.tatl.x, toy-player.tatl.y)
			if(spd > 4*player.mo.scale)
				player.tatlsin = $1 + FRACUNIT*20
			//	player.tatltargangle = $1 + FRACUNIT*5
				if(player.tatlo.tics > 2)
					player.tatlo.tics = 2
				end
			end
			
			local tocolor = SKINCOLOR_TANGERINE
			if(player.navicolors)
				tocolor = SKINCOLOR_JAWZ
			end
			
			P_TeleportMove(player.tatl, tox, toy, toz)
			P_TeleportMove(player.tatlo, player.tatl.x, player.tatl.y, player.tatl.z)
			player.tatl.scale = (player.mo.scale*30)/100
			player.tatlo.scale = (player.mo.scale*30)/100
			A_ChangeColorAbsolute(player.tatl, 0, tocolor)
			A_ChangeColorAbsolute(player.tatlo, 0, tocolor)
			player.tatl.state = S_TATL1
			if not(player.tatlo.state >= S_TATL2 and player.tatlo.state <= S_TATL3)
				player.tatlo.state = S_TATL2
			end
			
			if(player.tatlo.tics == 1)
				or(spd > 30*player.mo.scale)	// Boosting
				SpawnSparkles(player.tatl, player.tatl.scale*3/2, 25)
			end
		else
			if(player.tatl)
				if(player.tatl.valid)
					P_RemoveMobj(player.tatl)
				end
				player.tatl = nil
			end
			if(player.tatlo)
				if(player.tatlo.valid)
					P_RemoveMobj(player.tatlo)
				end
				player.tatlo = nil
			end
		end
	end
end)
