freeslot("sfx_eggscr", "sfx_knuxsc", "sfx_exescm", "sfx_tdlaug", "sfx_tlsscr", "sfx_tdscum", "sfx_mariov", "sfx_haha", "sfx_scream", "sfx_lscrm")

addHook("PlayerThink", function(player)
	if (player.mo and player.mo.valid and player.playerstate == PST_LIVE)
		if player.mo.td_screamtails == nil
			player.mo.td_screamtails = 0
		elseif player.mo.td_screamtails > 0
			player.mo.td_screamtails = $1 - 1
			P_StartQuake(18*FRACUNIT, 35, {0, 0, 0}, player.mo.radius)
			if player.mo.td_screamtails == 1*TICRATE-1
				S_StartSound(player.mo, sfx_tlsscr)
			end
		end
		if player.mo.td_screamegg == nil
			player.mo.td_screamegg = 0
		elseif player.mo.td_screamegg > 0
			player.mo.td_screamegg = $1 - 1
			P_StartQuake(18*FRACUNIT, 35, {0, 0, 0}, player.mo.radius)
			if player.mo.td_screamegg == 1*TICRATE-1
				S_StartSound(player.mo, sfx_eggscr)
			end
		end
		if player.mo.td_screamknux == nil
			player.mo.td_screamknux = 0
		elseif player.mo.td_screamknux > 0
			player.mo.td_screamknux = $1 - 1
			P_StartQuake(18*FRACUNIT, 35, {0, 0, 0}, player.mo.radius)
			if player.mo.td_screamknux == 1*TICRATE-1
				S_StartSound(player.mo, sfx_knuxsc)
			end
		end
		if player.mo.td_screamsonic == nil
			player.mo.td_screamsonic = 0
		elseif player.mo.td_screamsonic > 0
			player.mo.td_screamsonic = $1 - 1
			P_StartQuake(18*FRACUNIT, 35, {0, 0, 0}, player.mo.radius)
			if player.mo.td_screamsonic == 1*TICRATE-1
				S_StartSound(player.mo, sfx_exescm)
				S_StartSound(player.mo, sfx_haha)
			end
		end
		if player.mo.td_screamtailsdoll == nil
			player.mo.td_screamtailsdoll = 0
		elseif player.mo.td_screamtailsdoll > 0
			player.mo.td_screamtailsdoll = $1 - 1
			P_StartQuake(18*FRACUNIT, 35, {0, 0, 0}, player.mo.radius)
			if player.mo.td_screamtailsdoll == 1*TICRATE-1
				S_StartSound(player.mo, sfx_tdlaug)
			end
		end
		if player.mo.td_scum == nil
			player.mo.td_scum = 0
		elseif player.mo.td_scum > 0
			player.mo.td_scum = $1 - 1
			P_StartQuake(18*FRACUNIT, 35, {0, 0, 0}, player.mo.radius)
			if player.mo.td_scum == 1*TICRATE-1
				S_StartSound(player.mo, sfx_tdscum)
			end
		end
		if player.mo.zombiefaironeggs == nil
			player.mo.zombiefaironeggs = 0
		elseif player.mo.zombiefaironeggs > 0
			player.mo.zombiefaironeggs = $1 - 1
			P_StartQuake(18*FRACUNIT, 35, {0, 0, 0}, player.mo.radius)
			if player.mo.zombiefaironeggs == 1*TICRATE-1
				S_StartSound(player.mo, sfx_scream)
			end
		end
		if player.mo.evilexepan95eggs == nil
			player.mo.evilexepan95eggs = 0
		elseif player.mo.evilexepan95eggs > 0
			player.mo.evilexepan95eggs = $1 - 1
			P_StartQuake(18*FRACUNIT, 35, {0, 0, 0}, player.mo.radius)
			if player.mo.evilexepan95eggs == 1*TICRATE-1
				S_StartSound(player.mo, sfx_haha)
			end
		end
		if player.mo.td_star == nil
			player.mo.td_star = 0
		elseif player.mo.td_star > 0
			player.mo.td_star = $1 - 1
			P_StartQuake(18*FRACUNIT, 35, {0, 0, 0}, player.mo.radius)
			if player.mo.td_star == 1*TICRATE-1
				S_StartSound(player.mo, sfx_lscrm)
			end
		end
		if player.mo.ghfrst_scrm == nil
			player.mo.ghfrst_scrm = 0
		elseif player.mo.ghfrst_scrm > 0
			player.mo.ghfrst_scrm = $1 - 1
			P_StartQuake(18*FRACUNIT, 35, {0, 0, 0}, player.mo.radius)
			if player.mo.ghfrst_scrm == 1*TICRATE-1
				S_StartSound(nil, sfx_s3k9c)
			end
		end
		if player.mo.scream_pampersov == nil
			player.mo.scream_pampersov = 0
		elseif player.mo.scream_pampersov > 0
			player.mo.scream_pampersov = $1 - 1
			P_StartQuake(18*FRACUNIT, 35, {0, 0, 0}, player.mo.radius)
			if player.mo.scream_pampersov == 1*TICRATE-1
				S_StartSound(nil, sfx_s3k9c)
			end
		end
	end
end)

local shakex = 0
local shakey = 0

addHook("PlayerThink", function(player)
    if (player.mo and player.mo.valid) then
        shakex = P_RandomRange(-2, 2)
        shakey = P_RandomRange(-2, 2)
    end
end)

local function jumpscare(v, player)
    if (player.mo and player.mo.valid) then
        if not (player.mo.td_screamtails or player.mo.td_screamegg or player.mo.td_screamknux or player.mo.td_screamsonic or player.mo.td_screamtailsdoll or player.mo.td_scum or player.mo.td_star) then return end
        local jstails = v.cachePatch("TAILS")
        local jseggman = v.cachePatch("EGGMAN")
        local jsknux = v.cachePatch("KNUCKLES")
        local jssonic = v.cachePatch("HAHA"+tostring((leveltime%8)/2))
        local jstailsdoll = v.cachePatch("DCYMY8H-")
		local tdscum = v.cachePatch("TDSCUM")
		local tdstar = v.cachePatch("TDOLL"+tostring((leveltime%8)/2))
        v.drawFill()
        if player.mo.td_screamtails then
            v.draw(shakex,shakey,jstails,V_OFFSET)
        elseif player.mo.td_screamegg then
            v.draw(shakex,shakey,jseggman,V_OFFSET)
        elseif player.mo.td_screamknux then
            v.draw(shakex,shakey,jsknux,V_OFFSET)
        elseif player.mo.td_screamsonic then
            v.draw(shakex,shakey,jssonic,V_OFFSET)
        elseif player.mo.td_screamtailsdoll then
            v.draw(shakex,shakey,jstailsdoll,V_OFFSET)
        elseif player.mo.td_scum then
            v.draw(shakex,shakey,tdscum,V_OFFSET)
        elseif player.mo.td_star then
            v.draw(shakex,shakey,tdstar,V_OFFSET)
        end
    end
end

hud.add(jumpscare, "game")

local function jumpscarechaoticadventure(v, player)
    if (player.mo and player.mo.valid) then
        if not (player.mo.zombiefaironeggs or player.mo.evilexepan95eggs) then return end
        local zfairon = v.cachePatch("ZFAIRON")
        local eexepan = v.cachePatch("EEXEPAN")
        v.drawFill()
        if player.mo.zombiefaironeggs then
            v.draw(shakex,shakey,zfairon,V_OFFSET)
        elseif player.mo.evilexepan95eggs then
            v.draw(shakex,shakey,eexepan,V_OFFSET)
        end
    end
end

hud.add(jumpscarechaoticadventure, "game")

local function theghoulforestscream(v, player)
    if (player.mo and player.mo.valid) then
        if not (player.mo.ghfrst_scrm or player.mo.scream_pampersov) then return end
        local zfairon = v.cachePatch("SCARY1")
        local eexepan = v.cachePatch("SCARY2")
        v.drawFill()
        if player.mo.ghfrst_scrm then
            v.draw(shakex,shakey,zfairon,V_OFFSET)
        elseif player.mo.scream_pampersov then
            v.draw(shakex,shakey,eexepan,V_OFFSET)
        end
    end
end

hud.add(theghoulforestscream, "game")

addHook("LinedefExecute", function(line, mo, sector)
	if (mo and mo.valid and mo.player and mo.player.playerstate == PST_LIVE)
		if mo.td_screamtails != nil
			S_StartSound(mo, sfx_s3k9c)
			mo.td_screamtails = 1*TICRATE
		end
	end
end, "TDCUT1")

addHook("LinedefExecute", function(line, mo, sector)
	if (mo and mo.valid and mo.player and mo.player.playerstate == PST_LIVE)
		if mo.td_screamknux != nil
			S_StartSound(mo, sfx_s3k9c)
			mo.td_screamknux = 1*TICRATE
		end
	end
end, "TDCUT2")

addHook("LinedefExecute", function(line, mo, sector)
	if (mo and mo.valid and mo.player and mo.player.playerstate == PST_LIVE)
		if mo.td_screamegg != nil
			S_StartSound(mo, sfx_s3k9c)
			mo.td_screamegg = 1*TICRATE
		end
	end
end, "TDCUT3")

addHook("LinedefExecute", function(line, mo, sector)
	if (mo and mo.valid and mo.player and mo.player.playerstate == PST_LIVE)
		if mo.td_screamsonic != nil
			S_StartSound(mo, sfx_s3k9c)
			mo.td_screamsonic = 1*TICRATE
		end
	end
end, "TDCUT4")

addHook("LinedefExecute", function(line, mo, sector)
	if (mo and mo.valid and mo.player and mo.player.playerstate == PST_LIVE)
		if mo.td_screamtailsdoll != nil
			S_StartSound(mo, sfx_s3k9c)
			mo.td_screamtailsdoll = 1*TICRATE
		end
	end
end, "TDCUT5")

addHook("LinedefExecute", function(line, mo, sector)
	if (mo and mo.valid and mo.player and mo.player.playerstate == PST_LIVE)
		if mo.td_star != nil
			S_StartSound(mo, sfx_s3k9c)
			mo.td_star = 1*TICRATE
		end
	end
end, "TDCUT6")

addHook("LinedefExecute", function(line, mo, sector)
	if (mo and mo.valid and mo.player and mo.player.playerstate == PST_LIVE)
		if mo.ghfrst_scrm != nil
			mo.ghfrst_scrm = 1*TICRATE
		end
	end
end, "SCRARY1")

addHook("LinedefExecute", function(line, mo, sector)
	if (mo and mo.valid and mo.player and mo.player.playerstate == PST_LIVE)
		if mo.scream_pampersov != nil
			mo.scream_pampersov = 1*TICRATE
		end
	end
end, "SCRARY2")
addHook("LinedefExecute", function(line, mo, sector)
	if (mo and mo.valid and mo.player and mo.player.playerstate == PST_LIVE)
		if mo.td_scum != nil
			S_StartSound(mo, sfx_s3k9c)
			mo.td_scum = 1*TICRATE
		end
	end
end, "TDSCUM")

addHook("LinedefExecute", function(line, mo, sector)
	if (mo and mo.valid and mo.player and mo.player.playerstate == PST_LIVE)
		if mo.zombiefaironeggs != nil
			S_StartSound(mo, sfx_s3k9c)
			mo.zombiefaironeggs = 1*TICRATE
		end
	end
end, "ZFAIRON")

addHook("LinedefExecute", function(line, mo, sector)
	if (mo and mo.valid and mo.player and mo.player.playerstate == PST_LIVE)
		if mo.evilexepan95eggs != nil
			S_StartSound(mo, sfx_s3k9c)
			mo.evilexepan95eggs = 1*TICRATE
		end
	end
end, "EEXEPAN")