freeslot("sfx_pttaun", "sfx_sstaun", "sfx_parry", "MT_TAUNTPARTICLE")

local globalVars = {
	taunting = false,
	tauntDeb = false,
	tauntTimer = 0,
	tauntTimer2 = 0,
	breakdancing = false,
}

COM_AddCommand("tauntsound", function(player, arg1)
    if arg1 == nil
        CONS_Printf(player, 'tauntsound <Taunt Sound>: Changes the pizza tower taunt sound. You can pick between "Pizza Tower" and "Sugary Spire".')
        return
    end

    if arg1 == "1" or arg1 == "sugaryspire" then
	    CONS_Printf(player, "You will now hear the sugary spire taunt sound.")
        player.tauntsound = 1
    elseif arg1 == "0" or arg1 == "pizzatower" then
	    CONS_Printf(player, "You will now hear the pizza tower taunt sound.")
        player.tauntsound = 0
    end
end)

addHook("MobjSpawn", function(mo)
    mo.tauntTimer2 = 0
    
end, MT_PLAYER)

local function L_Choose(...)
	local args = {...}
	local choice = P_RandomRange(1,#args)
	return args[choice]
end

local function Taunt(player)
	local mo = player.mo
	if (player.cmd.buttons & BT_TOSSFLAG) and not player.mo.tauntDeb then
		if not player.mo.taunting and mo.player.tauntsound == 0 or not player.mo.taunting and mo.player.tauntsound == nil then
			player.mo.taunting = true
			player.mo.prevState = mo.state
			player.mo.prevMoms = {mo.momx, mo.momy, mo.momz}
			player.mo.lastPFlags = player.pflags
			S_StartSound(mo, sfx_pttaun)
		elseif not player.mo.taunting and mo.player.tauntsound == 1 then
			player.mo.taunting = true
			player.mo.prevState = mo.state
			player.mo.prevMoms = {mo.momx, mo.momy, mo.momz}
			player.mo.lastPFlags = player.pflags
			S_StartSound(mo, sfx_sstaun)
		end
		player.mo.tauntDeb = true
	elseif not (player.cmd.buttons & BT_TOSSFLAG) and player.mo.tauntDeb then
		player.mo.tauntDeb = false
	end

	if (player.cmd.buttons & BT_TOSSFLAG) then
		player.mo.tauntTimer = $+1
	else
		player.mo.tauntTimer = 0
	end
	if player.mo.taunting then
		player.mo.tauntTimer2 = $+1
		mo.momx = 0
		mo.momy = 0
		mo.momz = 0
		mo.state = mo.tauntstate
		local tauntparticle = P_SpawnMobjFromMobj(mo, 0, 0, 0, MT_TAUNTPARTICLE)
		tauntparticle.timer = 1
		
		if player.mo.tauntTimer2 >= 12 then
			mo.momx = player.mo.prevMoms[1]
			mo.momy = player.mo.prevMoms[2]
			mo.momz = player.mo.prevMoms[3]
			mo.state = player.mo.prevState
			player.pflags = player.mo.lastPFlags
			player.mo.taunting = false
			player.mo.tauntTimer2 = 0
			player.mo.tauntingState = nil
		end
	end
end

addHook("PlayerThink", function(player)
	if player.mo and player.mo.skin == "henry" then
		Taunt(player)
	end
end)

addHook("PlayerThink", function(player)
	if player.mo and not player.mo.taunting then
		player.mo.tauntstate = L_Choose(
		S_PLAY_GASP, 
		S_PLAY_EDGE, 
		S_PLAY_RIDE,
		S_PLAY_DEAD, 
		S_PLAY_DRWN, 
		S_PLAY_PAIN, 
		S_PLAY_SKID, 
		S_PLAY_WAIT,
		S_PLAY_SPINDASH,
		S_PLAY_SPRING,
		S_PLAY_FALL,
		S_PLAY_NIGHTS_STUN,
		S_PLAY_NIGHTS_TRANS1)
	end
end)


addHook("MobjDamage", function(mo, inflictor, source)
    
    if (mo.player.powers[pw_carry] == CR_NIGHTSMODE)
    return
    end    
		if mo and mo.valid and mo.taunting
        S_StartSound(mo, sfx_parry)
		P_SetObjectMomZ(mo, 20*FRACUNIT, false)
		P_InstaThrust(mo, mo.angle, -30*FRACUNIT)
		mo.player.powers[pw_flashing]=2*TICRATE
		mo.tauntTimer2 = 12
		return true
		else return end
        
        
end, MT_PLAYER)

addHook("MobjThinker", function(tauntparticle)
	tauntparticle.tics = $1 + 1
	if (tauntparticle.timer)
		tauntparticle.timer = $1 - 1
	else
		P_RemoveMobj(tauntparticle)
	end
end, MT_TAUNTPARTICLE)

addHook("MobjMoveCollide", function(mo, touch)
	
	if mo.taunting
	if not touch return end
	if not (touch.player or touch.target) return end
	if mo.target == touch return end
	if touch.player and (touch.player.powers[pw_invulnerability] or touch.player.powers[pw_flashing]) return end
	if touch.player and (touch.player.powers[pw_super] or touch.player.hypermode) return end
	if mo.z+mo.height*2 >= touch.z
	and mo.z < touch.z+touch.height
		P_InstaThrust(touch, mo.angle, -45*FRACUNIT)
		P_SetObjectMomZ(touch, 20*FRACUNIT, false)
		
		P_DamageMobj(touch, mo, mo, 2, DMG_NUKE)
		S_StartSound(mo, sfx_parry)
		mo.player.powers[pw_flashing]=2*TICRATE
	end
	end
end, MT_PLAYER)