////////////////
// SPECCYHUGS //
////////////////
// Reused for like all of my mods, I might as well just allow for public use.
// Made by Spectra (speccyspec) and Rosy.iso (rosy.iso) on Discord.
// Change line 134, probably. It should say the skinname.
// also you might want to keep these credits
// pwease ;w;

if not SpeccyHugs then
	freeslot("S_PLAY_SPECCYHUGS", "SPR2_HUG_")
	states[S_PLAY_SPECCYHUGS] = {SPR_PLAY, SPR2_HUG_, -1, nil, 0, 0, S_PLAY_STND}
	rawset(_G, "SpeccyHugs", {})

	local function CheckObject(refmo, foundmo)
		local p2 = foundmo.player

		-- Make sure the player exists.
		if not p2 or not p2.valid then return end

		local p = refmo.player

		-- Make sure we're on the floor.
		if not P_IsObjectOnGround(refmo) or not P_IsObjectOnGround(foundmo) then return end

		-- No NiGHTS.
		if (p.powers[pw_carry] & CR_NIGHTSMODE) return end

		-- Make sure the player is alive.
		if p2.playerstate ~= PST_LIVE return end

		-- No hugging the enemy!
		if p.ctfteam ~= p2.ctfteam return end

		-- Distance check.
		local dist = P_AproxDistance(P_AproxDistance(foundmo.x - refmo.x, foundmo.y - refmo.y), foundmo.z - refmo.z)

		if dist <= 48*refmo.scale then
			P_SpawnLockOn(p, foundmo, S_LOCKON2)

			if (p.cmd.buttons & BT_TOSSFLAG) then
				p.tossflagbtn = $ and $+1 or 1
			else
				p.tossflagbtn = nil
			end

			if p.tossflagbtn == 1 then
				if not p.hugging then
					p.hugging = {p2}
				else
					p.hugging[#p.hugging + 1] = p2
				end
				p2.hugged = p
				S_StartSound(refmo, sfx_s3k4a)
				S_StartSound(refmo, sfx_cdpcm6)
			end
		end
	end

	addHook("PlayerThink", function(p)
		-- lmao.
		if not p.mo or not p.mo.valid then return end

		-- There would be nobody to hug.
		if not multiplayer then return end

		-- Only do this for characters that support it.
		if not SpeccyHugs[p.mo.skin] then return end

		if p.hugging then
			local p2, cancel

			for i = 1, #p.hugging do
				p2 = p.hugging[i]
				p2.hugged = p

				-- Force to look at eachother.
				p.drawangle = R_PointToAngle2(p.mo.x, p.mo.y, p2.mo.x, p2.mo.y)

				-- Stop movement.
				p.powers[pw_nocontrol] = 5
				p2.powers[pw_nocontrol] = 5
				p.pflags = $|PF_STASIS -- Recognise as
				p2.pflags = $|PF_STASIS -- a hug.
				p.mo.momx, p.mo.momy = 0, 0
				p2.mo.momx, p2.mo.momy = 0, 0

				-- Force state.
				if p.mo.state ~= S_PLAY_SPECCYHUGS then p.mo.state = S_PLAY_SPECCYHUGS end
				p.mo.frame = A

				-- Cancel Hug.
				if (p.cmd.buttons & BT_JUMP) or (p2.cmd.buttons & BT_JUMP) or P_PlayerInPain(p) or p.mo.dying or not P_IsObjectOnGround(p.mo) then
					cancel = true
				end
			end

			-- The hug was cancelled.
			if cancel then
				for i = 1, #p.hugging do
					p2 = p.hugging[i]
					p2.hugged = nil
					p2.pflags = $ & ~PF_STASIS
				end

				p.hugging = nil
				p.pflags = $ & ~PF_STASIS
				p.mo.state = S_PLAY_STND
			end

			return
		end

		searchBlockmap("objects", CheckObject, p.mo)
	end)

	addHook("MapChange", function()
		for p in players.iterate do
			if p.hugging then
				for i = 1, #p.hugging do
					p2 = p.hugging[i]
					p2.hugged = nil
					p2.pflags = $ & ~PF_STASIS
				end

				p.hugging = nil
				p.pflags = $ & ~PF_STASIS
				p.mo.state = S_PLAY_STND
			end
		end
	end)
end

SpeccyHugs.superguide = true

//Footsteps support
if not PlayerAnimInfo
	rawset(_G, "PlayerAnimInfo", {})
end

PlayerAnimInfo["superguide"] = {
		["runFrames"] = {3, 7},
		["dashFrames"] = {-1},
		["walkFrames"] = {3, 7},
		["waitFrames"] = {0},
		["milnekickFrames"] = {0, 4},
		["run"] = true,
		["dash"] = false,
		["walk"] = true,
		["idle"] = true,
		["wait"] = true,
		["milnekick"] = true,
		["superRun"] = false,
		["superDash"] = false,
		["superWalk"] = true,
		["superIdle"] = true,
		["superWait"] = false
}

//Battle!
local BattleFuncSet = false

addHook("ThinkFrame",function()
	if (BattleFuncSet) or not(CBW_Battle and CBW_Battle.SkinVars) then return end
	BattleFuncSet = true

	CBW_Battle.SkinVars["superguide"] = {
		flags = SKINVARS_GUARD,
		weight = 75,
		shields = 2,
		special = CBW_Battle.Action.Panik,
		guard_frame = 1
	}
end)

addHook("ThinkFrame", function(player)
	for player in players.iterate do
	player.superguidecheered = player.superguidecheered or 0
    if (player.mo and player.mo.valid and player.mo.skin == "superguide") and player.superguidecheered == 0 then 
	if player.exiting then
		player.superguidecheered = 1
        S_StartSound(player.mo, sfx_stnt1)
		else
		player.superguidecheered = 0
		end
		end
    end
end)

addHook("ThinkFrame", do
	for player in players.iterate
	player.sair = player.sair or false
		if player.mo.valid
		and(player.mo.skin == "superguide") and player.sair == false and P_IsObjectOnGround(player.mo) == false then
		player.sair = true
		elseif (player.mo.skin == "superguide") and player.sair == true and P_IsObjectOnGround(player.mo) == true then
		player.sair = false
		S_StartSound(player.mo, sfx_slnd)
		end
	end
end)

//Winning Animation stuff borrowed from Rafael
freeslot("S_PLAY_WINR", "SPR2_WINR")
states[S_PLAY_WINR] = {SPR_PLAY, SPR2_WINR, 4, nil, 0, 0, S_PLAY_WINR}
addHook("PlayerThink", function(player)
	if not (player.mo and player.mo.valid and player.mo.skin == "superguide") return end
	if player.exiting and (player.mo.state == S_PLAY_STND or player.mo.state == S_PLAY_WAIT) then
			player.mo.state = S_PLAY_WINR
		player.mo.momx = 0 player.mo.momy = 0
	end
end)