//Worst Ending - Secret
freeslot("sfx_ftlt")

addHook("PlayerThink", function(player)
	//Worst Ending - Part 1
	if (player.mo and player.mo.valid and player.playerstate == PST_LIVE)
	and mapheaderinfo[gamemap].td_forest
		if player.mo.tdworst == nil
			player.mo.tdworst = 0
		elseif player.mo.tdworst > 0
			player.mo.tdworst = $1 - 1
			player.mo.flags2 = $1|MF2_DONTDRAW
			player.powers[pw_nocontrol] = 1
			if player.mo.tdworst <= 4
			and player.mo.tdworst > 0
				if (leveltime % 2) == 1
					G_SetCustomExitVars(791, 1)
					G_ExitLevel()
				end
			end
			if player.mo.tdworst == 1*TICRATE-1
				S_StartSound(player.mo, sfx_ftlt)
			end
		end
	end
	//Worst Ending - Part 2
	if (player.mo and player.mo.valid and player.playerstate == PST_LIVE)
	and mapheaderinfo[gamemap].tdworst
		if player.mo.worstending == nil
			player.mo.worstending = 0
		elseif player.mo.worstending > 0
			player.mo.worstending = $1 - 1
			if player.mo.worstending <= 4
			and player.mo.worstending > 0
				if (leveltime % 2) == 1
					G_SetCustomExitVars(1101, 1)
					G_ExitLevel()
				end
			end
		end
	end
end)

addHook("LinedefExecute", function(line, mo, sector)
	if (mo and mo.valid and mo.player and mo.player.playerstate == PST_LIVE)
	and mapheaderinfo[gamemap].td_forest
		if mo.tdworst != nil
			mo.tdworst = 1*TICRATE
			COM_BufInsertText(player, "cecho Hypnozing from Sonic's by Tails Doll")
			S_StartSound(nil, sfx_cmfmnu)
			local thok = P_SpawnMobjFromMobj(mo, 0, 0, 0, MT_THOK)
			thok.state = S_FAIRONRINGPORTAL
		end
	end
end, "TDITOG2")

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