freeslot("sfx_mch3")
freeslot("sfx_mch4")
freeslot("sfx_mcbp")
freeslot("sfx_bump")
freeslot("sfx_grab")
freeslot("sfx_taunt")
freeslot("sfx_poww")
freeslot("sfx_ouchie")
freeslot("sfx_pizzah")
freeslot("sfx_lap2")
freeslot("sfx_ahh")
freeslot("sfx_ah")
freeslot("sfx_yeow")
freeslot("sfx_aww")

local pillarboi = nil
local PBPizzaTime = false
local PBStartX = 0
local PBStartY = 0
local PBStartZ = 0
local jind = 77*FRACUNIT
local MaxTime = 9999999
local TimeTilPF = 99999
local PIZZATIME = false
local SpawnSector = nil
local FirstTime = false
local pizzaboispawned = false
local pizzaboi = nil
local Lap2 = false
local SSF_REALEXIT = 1<<7
local lapportal = nil
local sword = nil
local gate = nil
local PRank = false

addHook("PlayerThink", function(player)
	if player.combo == nil
		player.combo = 0
	end
	if player.comboamount == nil
		player.comboamount = 0
	end
	if player.posed == nil
		player.posed = false
	end
	if (player.combo > 0) and not (player.pflags & PF_FINISHED) and mapheaderinfo[gamemap].bonustype ~= 1 and mapheaderinfo[gamemap].bonustype ~= 2
		player.combo = player.combo - 1
	end
	if player.ignoreexit == nil
		player.ignoreexit = false
	end
	if player.lostfirstcombo == nil
		player.lostfirstcombo = false
	end
	if player.combo == 1
		S_StartSound(player.mo, 163, player)
		player.lostfirstcombo = true
	end
	if player.combo > 200
		player.combo = 200
	end
	if player.enemieskilled == nil
		player.enemieskilled = 0
	end
	if player.supertaunt == nil
		player.supertaunt = false
	end
	if player.enemieskilled == 10
		player.supertaunt = true
		player.enemieskilled = 0
	end
	if player.haslostcombo == nil
		player.haslostcombo = false
	end
	if player.congrats == nil
		player.congrats = false
	end
	if player.exiting and player.lapimpossible == true
		PRank = true
	end
	if (player.exiting or (player.pflags & PF_FINISHED)) and (player.ignoreexit == false)
		player.combo = 0
		player.congrats = true
		player.ignoreexit = true
	end
	if player.combo == 0
		P_AddPlayerScore(player, player.comboamount*500)
		player.comboamount = 0
		player.enemieskilled = 0
		player.ones = 0
		player.tens = 0
		player.hund = 0
	end
	if player.lapimpossible == nil
		player.lapimpossible = false
	end
	if (player.lapimpossible == true) and (player.playerstate == PST_LIVE)
		player.lives = 1
		player.continues = 0
	end
	if player.lapimpossible == true and PRank == false and player.playerstate == PST_LIVE
		P_KillMobj(player.mo, nil, nil)
	end
	if player.combo < 0
		player.combo = 0
	end
	if player.ones == nil
		player.ones = 0
	end
	if player.tens == nil
		player.tens = 0
	end
	if player.hund == nil
		player.hund = 0
	end
	if player.ones >= 10
		player.tens = player.tens + 1
		player.ones = player.ones - 10
	end
	if player.tens >= 10
		player.hund = player.hund + 1
		player.tens = player.tens - 10
	end
	if player.machrun == nil
		player.machrun = false
	end
	if player.machrun3 == nil
		player.machrun3 = false
	end
	if player.machrun4 == nil
		player.machrun4 = false
	end
	if (player.cmd.buttons & BT_USE)
		player.machrun = true
	else
		player.machrun = false
	end
	if (player.charability2 ~= CA2_NONE)
		player.charability2 = CA2_NONE
	end
	if player.rolling == nil
		player.rolling = false
	end
	if player.diving == nil
		player.diving = false
	end
	if player.bodyslam == nil
		player.bodyslam = false
	end
	if player.grabbing == nil
		player.grabbing = 0
	end
	if player.grabbing > 0
		player.grabbing = player.grabbing - 1
	end
	if player.grabbing < 0
		player.grabbing = 0
	end
	if player.grabbing > 30
		player.grabbing = 30
	end
	if player.wallrunning == nil
		player.wallrunning = false
	end
	if player.sliding == nil
		player.sliding = 0
	end
	if player.sliding > 0
		player.sliding = player.sliding - 1
	end
	if player.superjumpbuild == nil
		player.superjumpbuild = false
	end
	if player.superjump == nil
		player.superjump = 0
	end
	if player.superjump > 0
		player.superjump = player.superjump - 1
	end
	if player.bash == nil
		player.bash = 0
	end
	if player.bash > 0
		player.bash = player.bash - 1
	end
	if player.redirecting == nil
		player.redirecting = false
	end
	if player.redirecttimer == nil
		player.redirecttimer = 0
	end
	if player.redirecttimer < 0
		player.redirecttimer = 0
	end
	if Lap2 == true and player.lostfirstcombo == false
		PRank = true
	end
	if player.lostfirstcombo == true and PRank == true
		PRank = false
	end
	if player.transformation == nil
		player.transformation = 0
	end
	if (player.charability ~= CA_NONE) and (player.transformation == 0)
		player.charability = CA_NONE
	elseif (player.charability ~= CA_DOUBLEJUMP) and (player.transformation == 1)
		player.charability = CA_DOUBLEJUMP
	end
	if player.normalspeed > 15*FRACUNIT and player.machrun == false and player.mach3 == false and player.mach4 == false
		player.normalspeed = 15*FRACUNIT
	end
	if player.knightslide == nil
		player.knightslide = false
	end
	if player.prevx == nil
		player.prevx = 0*FRACUNIT
	end
	if player.prevy == nil
		player.prevy = 0*FRACUNIT
	end
	if player.prevz == nil
		player.prevz = 0*FRACUNIT
	end
	if player.liquiddamage == nil
		player.liquiddamage = false
	end
end)

COM_AddCommand("finalchallenge", function(player, arg)
	if arg == "on" then
		player.combo = 200
		player.score = 25001
		PRank = true
		player.lapimpossible = true
		print "successfully activated"
	elseif arg == "off" then
		player.combo = 0
		player.score = 0
		PRank = false
		player.lapimpossible = false
		print "successfully disactivated"
	else
		print "Not valid, choose on or off"
	end
end)

//I just want some cool developer commands, not actually used during testing, more just used for fun!
//So uh, I actually ended up using it! That's cool!

//COM_AddCommand("PizzaTime", function(player, arg)
	//if arg == "yeth" then
		//PBPizzaTime = true
	//elseif arg == "nuhuh" then
		//PBPizzaTime = false
	//end
//end)


//This command doesnt work lol
//COM_AddCommand("panic", function(player, arg)
	//TimeTilPF = arg
//end)

addHook("MobjDeath", function(target, inflictor, source, damagetype)
	if source and source.valid and source.player and source.player.valid and not (source.player.pflags & PF_FINISHED) and (target.flags & MF_ENEMY or target.flags & MF_MONITOR)
		source.player.combo = 200
		source.player.comboamount = source.player.comboamount + 1
		source.player.enemieskilled = source.player.enemieskilled + 1
		source.player.ones = source.player.ones + 1
	end
end)

addHook("MobjDamage", function(target, inflictor, source, damagetype)
	if source and source.valid and source.player and source.player.valid and not (source.player.pflags & PF_FINISHED) and (target.flags & MF_ENEMY or target.flags & MF_MONITOR)
		source.player.combo = source.player.combo + 50
		if source.player.lapimpossible == true
			source.player.combo = source.player.combo + 50
		end
	end
end)

addHook("TouchSpecial", function(special, toucher)
	if toucher and toucher.valid and toucher.player.combo > 0 and not (toucher.player.pflags & PF_FINISHED)
		toucher.player.combo = toucher.player.combo + 20
		if not toucher.player.powers[pw_flashing]
			toucher.player.score = toucher.player.score + 150
		end
		if toucher.player.lapimpossible == true
			toucher.player.combo = toucher.player.combo + 40
		end
	end
end, MT_RING)

addHook("TouchSpecial", function(special, toucher)
	if toucher.player.starpostnum < special.health and toucher.player.combo > 0 and not (toucher.player.pflags & PF_FINISHED)
		toucher.player.combo = 200
		toucher.player.comboamount = toucher.player.comboamount + 1
		toucher.player.enemieskilled = toucher.player.enemieskilled + 1
		toucher.player.score = toucher.player.score + 1000
	end
end, MT_STARPOST)

addHook("PlayerSpawn", function(player)
	if player.congrats == true
		player.congrats = false
	end
	if player.ignoreexit == true
		player.ignoreexit = false
	end
	if PRank == true
		PRank = false
	end
	if player.mo and player.mo.valid and player.lapimpossible == false
		player.score = 0
		player.combo = 0
		player.comboamount = 0
		player.enemieskilled = 0
		player.supertaunt = false
		PRank = false
		player.lostfirstcombo = false
		player.transformation = 0
	end
	if player.mo and player.mo.valid and player.lapimpossible == true
		player.combo = 200
		player.ignoreexit = false
		player.congrats = false
		player.transformation = 0
	end
end)

addHook("ThinkFrame", function()
	for player in players.iterate do
		if player.congrats == true
			if player.score < 500
				chatprint(player.name+" just got a D! Sucks to be them!")
				player.congrats = false
			end
			if player.score >= 500 and player.score < 10000
				chatprint(player.name+" just got a C! That's alright...")
				player.congrats = false
			end
			if player.score >= 10000 and player.score < 15000
				chatprint(player.name+" just got a B! They're doing just fine!")
				player.congrats = false
			end
			if player.score >= 15000 and player.score < 20000
				chatprint(player.name+" just got a A! They're doing real good!")
				player.congrats = false
			end
			if player.score >= 20000 and PRank == false
				chatprint(player.name+" just got a S! I'm shocked!")
				player.congrats = false
			end
			if player.score >= 25000 and PRank == true and player.lapimpossible == false
				chatprint(player.name+" just got a P! A true perfectionist!")
				player.congrats = false
			end
			if player.score >= 25000 and PRank == true and player.lapimpossible == true
				chatprint("OMG! "+player.name+" IS ACTUALLY DOING IT!")
				player.congrats = false
			end
		end
	end
end)
			

-- The HUD code, developed by MARIOMARIO13531.
-- Some of the code is from boss hud mod made by Inuyasha.
--Lol I took this from Boost, well, the beginning part
local function comb(v, player)
	if player.mo and player.mo.valid
		if (maptol & TOL_NIGHTS) or player.combo == nil then -- Prevents the code from running in a Nights special stage
			return
		end 
			local p_comb = v.cachePatch("COMBR") --this junk is mine
			local p_ind = v.cachePatch("COMIND")
			local p_0 = v.cachePatch("NUMB0")
			local p_1 = v.cachePatch("NUMB1")
			local p_2 = v.cachePatch("NUMB2")
			local p_3 = v.cachePatch("NUMB3")
			local p_4 = v.cachePatch("NUMB4")
			local p_5 = v.cachePatch("NUMB5")
			local p_6 = v.cachePatch("NUMB6")
			local p_7 = v.cachePatch("NUMB7")
			local p_8 = v.cachePatch("NUMB8")
			local p_9 = v.cachePatch("NUMB9")
			local posx = (player.combo/3)

		if player.mo.valid and player.combo > 0
		v.drawScaled(posx*FRACUNIT, 15*FRACUNIT, FRACUNIT/2, p_ind, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		v.drawScaled(FRACUNIT, FRACUNIT, FRACUNIT/2, p_comb, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
			if (player.ones == 0) and (player.tens > 0) or (player.hund > 1)
				v.drawScaled(15*FRACUNIT, 45*FRACUNIT, FRACUNIT/2, p_0, V_PERPLAYER)
			elseif player.ones == 1
				v.drawScaled(15*FRACUNIT, 45*FRACUNIT, FRACUNIT/2, p_1, V_PERPLAYER)
			elseif player.ones == 2
				v.drawScaled(15*FRACUNIT, 45*FRACUNIT, FRACUNIT/2, p_2, V_PERPLAYER)
			elseif player.ones == 3
				v.drawScaled(15*FRACUNIT, 45*FRACUNIT, FRACUNIT/2, p_3, V_PERPLAYER)
			elseif player.ones == 4
				v.drawScaled(15*FRACUNIT, 45*FRACUNIT, FRACUNIT/2, p_4, V_PERPLAYER)
			elseif player.ones == 5
				v.drawScaled(15*FRACUNIT, 45*FRACUNIT, FRACUNIT/2, p_5, V_PERPLAYER)
			elseif player.ones == 6
				v.drawScaled(15*FRACUNIT, 45*FRACUNIT, FRACUNIT/2, p_6, V_PERPLAYER)
			elseif player.ones == 7
				v.drawScaled(15*FRACUNIT, 45*FRACUNIT, FRACUNIT/2, p_7, V_PERPLAYER)
			elseif player.ones == 8
				v.drawScaled(15*FRACUNIT, 45*FRACUNIT, FRACUNIT/2, p_8, V_PERPLAYER)
			elseif player.ones == 9
				v.drawScaled(15*FRACUNIT, 45*FRACUNIT, FRACUNIT/2, p_9, V_PERPLAYER)
			end
			if player.tens == 0 and player.hund > 0
				v.drawScaled(5*FRACUNIT, 40*FRACUNIT, FRACUNIT/2, p_0, V_PERPLAYER)
			elseif player.tens == 1
				v.drawScaled(5*FRACUNIT, 40*FRACUNIT, FRACUNIT/2, p_1, V_PERPLAYER)
			elseif player.tens == 2
				v.drawScaled(5*FRACUNIT, 40*FRACUNIT, FRACUNIT/2, p_2, V_PERPLAYER)
			elseif player.tens == 3
				v.drawScaled(5*FRACUNIT, 40*FRACUNIT, FRACUNIT/2, p_3, V_PERPLAYER)
			elseif player.tens == 4
				v.drawScaled(5*FRACUNIT, 40*FRACUNIT, FRACUNIT/2, p_4, V_PERPLAYER)
			elseif player.tens == 5
				v.drawScaled(5*FRACUNIT, 40*FRACUNIT, FRACUNIT/2, p_5, V_PERPLAYER)
			elseif player.tens == 6
				v.drawScaled(5*FRACUNIT, 40*FRACUNIT, FRACUNIT/2, p_6, V_PERPLAYER)
			elseif player.tens == 7
				v.drawScaled(5*FRACUNIT, 40*FRACUNIT, FRACUNIT/2, p_7, V_PERPLAYER)
			elseif player.tens == 8
				v.drawScaled(5*FRACUNIT, 40*FRACUNIT, FRACUNIT/2, p_8, V_PERPLAYER)
			elseif player.tens == 9
				v.drawScaled(5*FRACUNIT, 40*FRACUNIT, FRACUNIT/2, p_9, V_PERPLAYER)
			end
			if player.hund == 1
				v.drawScaled(0*FRACUNIT, 30*FRACUNIT, FRACUNIT/2, p_1, V_PERPLAYER)
			elseif player.hund == 2
				v.drawScaled(0*FRACUNIT, 30*FRACUNIT, FRACUNIT/2, p_2, V_PERPLAYER)
			elseif player.hund == 3
				v.drawScaled(0*FRACUNIT, 30*FRACUNIT, FRACUNIT/2, p_3, V_PERPLAYER)
			elseif player.hund == 4
				v.drawScaled(0*FRACUNIT, 30*FRACUNIT, FRACUNIT/2, p_4, V_PERPLAYER)
			elseif player.hund == 5
				v.drawScaled(0*FRACUNIT, 30*FRACUNIT, FRACUNIT/2, p_5, V_PERPLAYER)
			elseif player.hund == 6
				v.drawScaled(0*FRACUNIT, 30*FRACUNIT, FRACUNIT/2, p_6, V_PERPLAYER)
			elseif player.hund == 7
				v.drawScaled(0*FRACUNIT, 30*FRACUNIT, FRACUNIT/2, p_7, V_PERPLAYER)
			elseif player.hund == 8
				v.drawScaled(0*FRACUNIT, 30*FRACUNIT, FRACUNIT/2, p_8, V_PERPLAYER)
			elseif player.hund == 9
				v.drawScaled(0*FRACUNIT, 30*FRACUNIT, FRACUNIT/2, p_9, V_PERPLAYER)
			end
		end
	end
end
hud.add(comb, "game")

local function ranks(v, player)
	if player.mo and player.mo.valid
		if (maptol & TOL_NIGHTS) then -- Prevents the code from running in a Nights special stage
			return
		end 
			local p_rand = v.cachePatch("RANKD") --this junk is mine
			local p_ranc = v.cachePatch("RANKC")
			local p_ranb = v.cachePatch("RANKB")
			local p_rana = v.cachePatch("RANKA")
			local p_rans = v.cachePatch("RANPEE")
			local p_ranp = v.cachePatch("RANKP")
				
		if player.mo.valid and player.score < 500
			v.drawScaled(100*FRACUNIT, 25*FRACUNIT, FRACUNIT/2, p_rand, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		end
		if player.mo.valid and player.score >= 500 and player.score < 10000
			v.drawScaled(100*FRACUNIT, 25*FRACUNIT, FRACUNIT/2, p_ranc, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		end
		if player.mo.valid and player.score >= 10000 and player.score < 15000
			v.drawScaled(100*FRACUNIT, 25*FRACUNIT, FRACUNIT/2, p_ranb, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		end
		if player.mo.valid and player.score >= 15000 and player.score < 20000
			v.drawScaled(100*FRACUNIT, 25*FRACUNIT, FRACUNIT/2, p_rana, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		end
		if player.mo.valid and player.score >= 20000
			v.drawScaled(100*FRACUNIT, 25*FRACUNIT, FRACUNIT/2, p_rans, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		end
		if player.mo.valid and player.score >= 25000 and PRank == true
			v.drawScaled(100*FRACUNIT, 25*FRACUNIT, FRACUNIT/2, p_ranp, V_SNAPTOTOP|V_SNAPTOLEFT|V_PERPLAYER|V_HUDTRANS)
		end
	end
end
hud.add(ranks, "game")

addHook("PlayerThink", function(player)
	if (player.cmd.buttons & BT_FIRENORMAL) and (player.supertaunt == true) and not (P_IsObjectOnGround(player.mo)) and (player.mo.state == S_PLAY_JUMP) and (player.transformation == 0)
		P_InstaThrust(player.mo, player.mo.angle, 0*FRACUNIT)
		P_NukeEnemies(player.mo, player.mo, 2500*FRACUNIT)
		S_StartSound(player.mo, 93, player)
		player.supertaunt = false
	end
end)

//There was supposed to be a sponsored segment of PTAI, but instead, get *bonk*
addHook("PlayerThink", function(player) -- Resets if the player bonks when they touch the ground.
	if player and player.valid and player.mo.valid then
		if P_IsObjectOnGround(player.mo) or (player.mo.eflags & MFE_SPRUNG) -- Checks if player is on the ground or used a spring.
			player.bonk = false -- resets bonk to false..
		end
	end
end)
	

addHook("MobjMoveBlocked", function(mo) -- Checks if player is hitting a wall
local player = mo.player
	if (player.bonk == false) and (player.mach3 == true or (player.mach4 == true)) and (P_IsObjectOnGround(player.mo))-- checks if player can bonk.
		P_DoPlayerPain(player)
		player.normalspeed = 20*FRACUNIT
		if player.mach3 == true
			S_StartSoundAtVolume(player.mo, sfx_bump, 255) -- Plays the bonk sound effect.
		elseif player.mach4 == true
			S_StartSound(player.mo, sfx_mcbp)
		end
		if player.transformation > 0
			player.transformation = 0
		end
		player.bonk = true
	end
end, MT_PLAYER)
				
// Now it's back to me :D

addHook("PlayerThink", function(player)
	if (player.machrun == true) and (player.cmd.buttons & BT_CUSTOM1) and (P_IsObjectOnGround(player.mo)) and (player.grabbing == 0) and (player.sliding == 0) and (player.transformation == 0)
		player.rolling = true
	end
	if (player.machrun == false) or not (player.cmd.buttons & BT_CUSTOM1) or not (P_IsObjectOnGround(player.mo)) or (player.grabbing > 0) or (player.sliding > 0) or (player.transformation > 0) or (player.mo.eflags & MFE_SPRUNG)
		player.rolling = false
	end
	if player.rolling == true
		player.mo.state = S_PLAY_ROLL
		player.height = 1572864
	else
		player.height = 3145728
	end
	if (player.machrun == true) and (player.cmd.buttons & BT_CUSTOM1) and not (P_IsObjectOnGround(player.mo)) and (player.speed >= 40*FRACUNIT) and (player.grabbing == 0) and (player.sliding == 0) and (player.transformation == 0)
		player.diving = true
	end
	if (player.machrun == false) or not (player.cmd.buttons & BT_CUSTOM1) or (P_IsObjectOnGround(player.mo)) or (player.grabbing > 0) or (player.sliding > 0) or (player.mo.eflags & MFE_GOOWATER or player.powers[pw_carry] or P_PlayerInPain(player) or player.playerstate == PST_DEAD) or (player.transformation > 0) or (player.mo.eflags & MFE_SPRUNG)
		player.diving = false
	end
	if player.diving == true
		P_SetObjectMomZ(player.mo, -30*FRACUNIT, false)
	end
	if (player.cmd.buttons & BT_CUSTOM1) and not (P_IsObjectOnGround(player.mo)) and (player.speed <= 39*FRACUNIT) and (player.grabbing == 0) and (player.sliding == 0) and (player.transformation == 0)
		player.bodyslam = true
	end
	if P_IsObjectOnGround(player.mo) and player.bodyslam == true or player.grabbing > 0 and player.bodyslam == true or player.sliding > 0 and player.bodyslam == true or (player.mo.eflags & MFE_GOOWATER or player.powers[pw_carry] or P_PlayerInPain(player) or player.playerstate == PST_DEAD) or (player.transformation > 0) or (player.mo.eflags & MFE_SPRUNG)
		player.bodyslam = false
	end
	if player.bodyslam == true
		P_InstaThrust(player.mo, player.mo.angle, player.normalspeed)
		P_SetObjectMomZ(player.mo, -50*FRACUNIT, false)
	end
end)

addHook("PlayerThink", function(player)
	if (player.cmd.buttons & BT_ATTACK) and not (player.rolling) and not (player.diving) and not (player.bodyslam) and (player.grabbing == 0) and (player.superjump == 0) and not (player.bash) and (player.transformation == 0) and (player.uppercut == 0)
		player.grabbing = 30
	end
	if player.grabbing > 0
		player.normalspeed = 30*FRACUNIT
		P_InstaThrust(player.mo, player.mo.angle, 30*FRACUNIT)
		player.mo.state = S_PLAY_ROLL
		if not S_SoundPlaying(player.mo, sfx_grab)
			S_StartSound(player.mo, sfx_grab)
		end
	end
	if player.grabbing == 1
		player.normalspeed = 30*FRACUNIT
		player.mo.state = S_PLAY_RUN
	end
	if (player.grabbing > 0) and (player.cmd.buttons & BT_CUSTOM1) and (P_IsObjectOnGround(player.mo)) and (player.transformation == 0)
		player.sliding = 40
		player.grabbing = 0
	elseif not (P_IsObjectOnGround(player.mo)) or (player.transformation > 0)
		player.sliding = 0
		player.height = 3145728
	end
	if player.sliding > 15
		P_InstaThrust(player.mo, player.mo.angle, 40*FRACUNIT)
		player.mo.state = S_PLAY_PAIN
		player.height = 1572864
	elseif player.sliding > 1 and player.sliding < 15
		P_InstaThrust(player.mo, player.mo.angle, 40*FRACUNIT)
		player.mo.state = S_PLAY_ROLL
		player.height = 1572864
	elseif player.sliding == 1
		player.normalspeed = 40*FRACUNIT
		player.mo.state = S_PLAY_RUN
		player.height = 3145728
	end
end)

addHook("MobjMoveBlocked", function(mo)
local player = mo.player
	if (player.machrun == true) and not (P_IsObjectOnGround(player.mo)) and (player.pflags & PF_JUMPED) and (player.transformation == 0)
		player.wallrunning = true
	end
end, MT_PLAYER)

addHook("PlayerThink", function(player)
	if not (player.powers[pw_pushing]) and (player.wallrunning == true) or (player.transformation > 0)
		player.wallrunning = false
	end
	if (player.wallrunning == true) and (player.cmd.buttons & BT_JUMP)
		P_InstaThrust(player.mo, player.mo.angle, -10*FRACUNIT)
		P_SetObjectMomZ(player.mo, 15*FRACUNIT, false)
		player.mo.angle = R_PointToAngle2(0,0,player.mo.momx,player.mo.momy)
		player.wallrunning = false
	end
	if player.wallrunning == true
		player.pflags = $ + PF_STASIS
		if player.mach3
			P_SetObjectMomZ(player.mo, 10*FRACUNIT, false)
		elseif player.mach4
			P_SetObjectMomZ(player.mo, 30*FRACUNIT, false)
		elseif player.normalspeed == 80*FRACUNIT and player.mach4
			P_SetObjectMomZ(player.mo, 40*FRACUNIT, false)
		end
	end
	if (player.wallrunning == false) and (player.pflags & PF_STASIS)
		P_InstaThrust(player.mo, player.mo.angle, player.mo.momz*FRACUNIT)
		P_SetObjectMomZ(player.mo, 0*FRACUNIT, false)
		player.pflags = $  & ~PF_STASIS
	end
end)

addHook("PlayerThink", function(player)
	if (P_IsObjectOnGround(player.mo)) and (player.cmd.buttons & BT_CUSTOM2) and (player.mach3 == true or (player.mach4 == true)) and (player.sliding == 0) and (player.wallrunning == false) and (player.grabbing == 0) and not (player.diving) and not (player.rolling) and not (player.bodyslam) and (player.transformation == 0)
		player.superjumpbuild = true
	end
	if not (player.cmd.buttons & BT_CUSTOM2) and player.superjumpbuild == true
		player.superjump = 50
		player.superjumpbuild = false
	end
	if player.superjumpbuild == true
		player.normalspeed = 5*FRACUNIT
		player.mo.state = S_PLAY_SPINDASH
		player.pflags = $ + PF_JUMPSTASIS
	end
	if player.superjump > 1
		P_SetObjectMomZ(player.mo, 30*FRACUNIT, false)
		player.mo.state = S_PLAY_SPRING
		player.pflags = $ + PF_FULLSTASIS
		P_InstaThrust(player.mo, player.mo.angle, 0*FRACUNIT)
		player.normalspeed = 0*FRACUNIT
	end
	if player.superjump == 1
		P_SetObjectMomZ(player.mo, 35*FRACUNIT, false)
		player.mo.state = S_PLAY_FALL
		player.pflags = $  & ~PF_FULLSTASIS
		player.pflags = $  & ~PF_JUMPSTASIS
	end
	if player.superjump > 1 and (player.cmd.buttons & BT_ATTACK)
		player.superjump = 0
		player.bash = 20
	end
	if player.bash > 1
		P_SetObjectMomZ(player.mo, 0*FRACUNIT, false)
		player.mo.state = S_PLAY_ROLL
	end
	if player.bash == 1
		P_InstaThrust(player.mo, player.mo.angle, 80*FRACUNIT)
		player.normalspeed = 40*FRACUNIT
	end
end)

// Sponsored Segment for TGTLS!
addHook("PlayerThink", function(p)
	if not (p.cmd.buttons & BT_TOSSFLAG)
	and not p.taunting
	and p.mo and p.mo.valid
		p.prevmomx = p.mo.momx
		p.prevmomy = p.mo.momy
		p.prevmomz = p.mo.momz
		p.prevstate = p.mo.state
	end
	if p.tftics == nil
		p.tftics = 0
	end
	if (p.cmd.buttons & BT_TOSSFLAG) and (p.transformation == 0)
		p.tftics = $1+1
	else
		p.tftics = 0
	end
end)

addHook("ThinkFrame", do
	for p in players.iterate
		
		if p.tauntingtimer == nil
			p.tauntingtimer = 0
		end
		
		if p.tftics == 1
		and not p.taunting
		and p.mo and p.mo.valid
		and not p.powers[pw_carry]
		and p.playerstate == PST_LIVE
		and not P_PlayerInPain(p)
		and not p.powers[pw_nocontrol]
		and p.grabbing == 0
		and not p.bodyslam
		and not p.diving
		and not p.rolling
		and not p.superjumpbuild
		and p.superjump == 0
		and p.bash == 0
		and not p.wallrunning
		and p.sliding == 0
			S_StartSound(p.mo, sfx_taunt)
			p.taunting = true
		end
		if p.taunting == nil
			p.taunting = false
		end
		if p.taunting == true
			p.mo.momx = 0
			p.tauntbubble = P_SpawnMobj(p.mo.x,p.mo.y,p.mo.z,MT_TAUNT)
			p.tauntbubble.scale = p.mo.scale+FRACUNIT/6
			p.mo.momy = 0
			p.mo.momz = 0
			p.grabbing = 0
			p.pflags = $|PF_JUMPSTASIS
			p.tauntingtimer = $1+1
			if PBPizzaTime
				p.score = p.score + 5
			end
		end
		if (p.mo.eflags & MFE_VERTICALFLIP)
			p.tauntbubble.eflags = $|MFE_VERTICALFLIP
		end
		if p.tauntingtimer == 16
			p.taunting = false
			p.tauntingtimer = 0
			p.tauntbubble = nil
			p.mo.momx = p.prevmomx
			p.mo.momy = p.prevmomy
			p.pflags = $ & ~PF_JUMPSTASIS
			p.mo.momz = p.prevmomz
			p.mo.state = p.prevstate
			p.posed = false
		end
	end
end)

//made by clairbun
rawset(_G, "L_Choose", function(...)
	local args = {...}
	local choice = P_RandomRange(1,#args)
	return args[choice]
end)

addHook("PlayerThink", function(p)
	if p.taunting
	and p.mo and p.mo.valid
	and p.posed == false
		p.mo.state = L_Choose(S_PLAY_ROLL,S_PLAY_SPRING,S_PLAY_PAIN,S_PLAY_DEAD,S_PLAY_RUN,S_PLAY_WAIT,S_PLAY_SUPER_TRANS1,S_PLAY_EDGE,S_PLAY_STND,S_PLAY_SPINDASH,S_PLAY_DASH)
		p.posed = true
	end
end)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
    if target and target.valid
	and inflictor and inflictor.valid
    and target.player.taunting
		P_DamageMobj(inflictor, target, source, DMG_INSTAKILL)
		target.player.taunting = false
		target.player.tauntingtimer = 0
		S_StartSound(target, sfx_poww)
		P_InstaThrust(target, target.angle, -7*FRACUNIT)
		target.state = L_Choose(S_PLAY_SUPER_TRANS1, S_PLAY_ROLL, S_PLAY_STND)
		if inflictor.type == MT_PIZZAFACE
			target.player.powers[pw_invulnerability] = 1*TICRATE
			inflictor.state = S_PIZZAFACERECOVER
		end
        return false
    end
end, MT_PLAYER)

// Sponsored Segment Over, back to me!

addHook("PlayerThink", function(player) // My Check (garbage)
	if (PBPizzaTime == true) and (player.speed == 0*FRACUNIT) and (P_IsObjectOnGround(player.mo)) and (player.mo.state == S_PLAY_WALK or player.mo.state == S_PLAY_STND or player.mo.state == S_PLAY_WAIT)
		player.mo.sprite2 = SPR2_CNT1
	end
	if player.speed ~= 0*FRACUNIT and player.mo.sprite2 == SPR2_CNT1
		player.mo.state = S_PLAY_WALK
	end
end)


addHook("MapLoad", function() //omg guys, look, it;s the uhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhhh map thing load
local PBLapPortalReal = nil
	for thing in mapthings.iterate
		if thing.type == 501
			pillarboi = P_SpawnMobj(thing.x*FU,thing.y*FU,thing.z*FU,MT_PILLARBOI)
			pillarboi.inv = 0
			pillarboi.hits = 0
			pillarboi.isdead = false
			pillarboi.hitby = {}
			pillarboi.hittimer = 10*TICRATE
			pillarboi.deadtime = 5*TICRATE
			PBStartX = thing.x*FU
			PBStartY = thing.y*FU
			PBStartZ = thing.z*FU
		end
		if thing.type == 3002
			PBLapPortalReal = true
		end
	end
	for sector in sectors.iterate do
		if (sector.specialflags & SSF_REALEXIT)
			sector.specialflags = 0
		end
	end
end)


// Another sponsor, welcome in Nick!

addHook('TouchSpecial', function(lapportal, mo)
local player = mo.player
	if Lap2 == false and PBPizzaTime == true
		P_SetOrigin(player.mo, PBStartX, PBStartY, PBStartZ)
		S_ChangeMusic("DEAOLI", true, i)
		S_StartSound(player.mo, sfx_lap2)
		if player.comboamount > 0
			player.combo = 200
		end
		player.score = player.score + 5000
		Lap2 = true
	end
return true
end, MT_LAPPORTAL)

//Quick moment of me
addHook('TouchSpecial', function(sword, mo)
local player = mo.player
	if player.transformation == 0 and sword.state == S_SWORD_SPAWN
		player.transformation = 1
		sword.state = S_SWORD_GONE
	end
return true
end, MT_SWORD)

addHook("PlayerCanDamage", function(player)
	if player.transformation == 1
		return true
	end
end)
//K back to Nick

addHook('TouchSpecial', function(pillarboi, mo)
	if not mo.player then return true end
	if PBPizzaTime then return end
	local p = mo.player
	if P_PlayerCanDamage(p, pillarboi) and not pillarboi.inv then
		if pillarboi.hits
			if not executePTHook('Before_HitPillarBoi', {pillarboi, mo})
				if not L_TableContains(pillarboi.hitby, p) then
					table.insert(pillarboi.hitby, p)
				end
				S_StartSound(pillarboi, sfx_ouchie)
				S_ChangeMusic('PHOBIA', true, p)
				pillarboi.hits = $-1
				pillarboi.inv = TICRATE
				pillarboi.hittimer = 10*TICRATE
				if pillarboi.state ~= S_PILLARBOI_UHOH then pillarboi.state = S_PILLARBOI_UHOH end
			end
			executePTHook('HitPillarBoi', {pillarboi, mo})
		else
			if not executePTHook('KillPillarBoi', {pillarboi, mo})
				P_KillMobj(pillarboi, nil, mo)
				S_StartSound(pillarboi, sfx_ouchie)
				PBPizzaTime = true
				if MaxTime == nil then
					MaxTime = leveltime * 2
					TimeTilPF = MaxTime
				end
			end
			executePTHook('KillPillarBoi', {pillarboi, mo})
		end
		mo.momx = $*-1
		mo.momy = $*-1
		mo.momz = $*-1
	end
	return true
end, MT_PILLARBOI)

addHook('MobjSpawn', function(pillarboi)
	pillarboi.inv = 0
	pillarboi.hits = 0
	pillarboi.isdead = false
	pillarboi.hitby = {}
	pillarboi.hittimer = 10*TICRATE
	pillarboi.deadtime = 5*TICRATE
	PBPizzaTime = false
	PBStartX = 0*FRACUNIT
	PBStartY = 0*FRACUNIT
	PBStartZ = 0*FRACUNIT
	jind = 77*FRACUNIT
	PIZZATIME = false
	FirstTime = false
	pizzaboispawned = false
	Lap2 = false
	SpawnSector = nil
	PRank = false
end, MT_PILLARBOI)

addHook('MobjThinker', function(pillarboi)
	for player in players.iterate
		if R_PointToDist2(player.mo.x, player.mo.y, pillarboi.x, pillarboi.y) <= 1000*FRACUNIT and P_PlayerCanDamage(player, pillarboi) and pillarboi.valid and not PBPizzaTime
			S_ChangeMusic('PHOBIA', true, player)
			pillarboi.state = S_PILLARBOI_UHOH
		elseif R_PointToDist2(player.mo.x, player.mo.y, pillarboi.x, pillarboi.y) > 1000*FRACUNIT or not pillarboi.valid or PBPizzaTime
			if not PBPizzaTime
				S_ChangeMusic(mapmusname)
				pillarboi.state = S_PILLARBOI
			end
		end
	end
end, MT_PILLARBOI)

addHook('MobjThinker', function(pillarboi)
	if not pillarboi.valid then return end
	if pillarboi.hittimer and pillarboi.hitby[1] then
		pillarboi.hittimer = $-1
	elseif not pillarboi.hittimer and pillarboi.hitby[1]
		for _,i in pairs(pillarboi.hitby) do
			if i.valid then
				S_ChangeMusic("PHOBIA", true, i)
			end
		end
		pillarboi.hitby = {}
		pillarboi.state = S_PILLARBOI
	end
	if pillarboi.inv then
		pillarboi.inv = $-1
	end
	if pillarboi.isdead then
		if pillarboi.deadtime then
			pillarboi.momz = $-(FU/2)
			pillarboi.deadtime = $-1
		else
			P_RemoveMobj(pillarboi)
		end
	end
end, MT_PILLARBOI)

addHook('MobjDeath', function(pillarboi, i, s)
	if s then
		pillarboi.momx = s.momx
		pillarboi.momy = s.momy
	end
	pillarboi.momz = 16*FU
	pillarboi.flags = $|MF_NOCLIP|MF_NOCLIPHEIGHT
	pillarboi.isdead = true
	PBPizzaTime = true
	S_ChangeMusic("PIZTIM", true, i)
end, MT_PILLARBOI)

addHook("PlayerThink", function(player)
	if player.speed > 14*FRACUNIT and player.machrun == true and player.sliding == 0 and player.grabbing == 0 and (player.transformation == 0)
		player.normalspeed = player.normalspeed + (FU/5)
	elseif player.speed > 39*FRACUNIT and player.machrun == true and player.mach3 == true
		player.normalspeed = player.normalspeed + (FU/10)
	end
	if player.machrun == true and player.normalspeed >= 40*FRACUNIT and player.cmd.forwardmove > 0 and player.mach4 == false and (player.transformation == 0)
		player.mach3 = true
	else
		player.mach3 = false
	end
	if player.machrun == true and player.normalspeed >= 60*FRACUNIT and player.cmd.forwardmove > 0 and (player.transformation == 0)
		player.mach4 = true
	else
		player.mach4 = false
	end
	if player.mach4 and player.normalspeed > 80*FRACUNIT
		player.normalspeed = 80*FRACUNIT
	end
	if player.mach4 == true and player.mach3 == true
		player.mach3 = false
	end
	if player.mach3
		P_SpawnSkidDust(player, 10*FRACUNIT, 0)
		if not S_SoundPlaying(player.mo, sfx_mch3)
			S_StartSound(player.mo, sfx_mch3)
		end
	end
	if player.mach4
		if P_IsObjectOnGround(player.mo)
			P_ElementalFire(player)
		end
		if not S_SoundPlaying(player.mo, sfx_mch4)
			S_StartSound(player.mo, sfx_mch4)
		end
	end
	if not player.mach3
		if S_SoundPlaying(player.mo, sfx_mch3)
			S_StopSoundByID(player.mo, sfx_mch3)
		end
	end
	if not player.mach4
		if S_SoundPlaying(player.mo, sfx_mch4)
			S_StopSoundByID(player.mo, sfx_mch4)
		end
	end
	if player.mach3 or player.mach4
		player.charflags = $1|SF_RUNONWATER
	else
		player.charflags = $1 & ~SF_RUNONWATER
	end
end)

//Sponsor over

addHook("PlayerThink", function(player)
	if (player.cmd.buttons & BT_CUSTOM3) and (player.redirecting == false) and (P_IsObjectOnGround(player.mo)) and not (player.rolling) and not (player.grabbing) and not (player.sliding) and not (player.superjumpbuild) and (player.mach3 or (player.mach4)) and (player.transformation == 0)
		player.redirecting = true
		player.redirecttimer = 30
		player.mo.momx = (player.mo.momx/2)
		player.mo.momy = (player.mo.momy/2)
	end
	if player.redirecting == true
		if P_IsObjectOnGround(player.mo)
			player.mo.state = S_PLAY_SPINDASH
		elseif player.redirecttimer <= 0 and not P_IsObjectOnGround(player.mo)
			player.mo.state = S_PLAY_PAIN
		end
		player.redirecttimer = player.redirecttimer - 1
		player.pflags = $ + PF_FULLSTASIS
		player.normalspeed = player.speed
	end
	if player.redirecting == true and player.redirecttimer <= 0 and P_IsObjectOnGround(player.mo)
		player.pflags = $ & ~PF_FULLSTASIS
		P_InstaThrust(player.mo, player.mo.angle, 40*FRACUNIT)
		if player.mach4
			player.mach4 = false
			player.mach3 = true
		end
		player.mo.state = S_PLAY_RUN
		player.normalspeed = 40*FRACUNIT
		player.redirecting = false
	end
	if (player.mo.eflags & MFE_SPRUNG) and (player.redirecting == true)
		player.redirecting = false
		player.redirecttimer = 0
	end
end)
//ITS PIZZA TIME!!!!!!!!

local levelTimes = {
    ["Greenflower Zone 1"] = 70*TICRATE,
    ["Greenflower Zone 2"] = 95*TICRATE,
    ["Techno Hill Zone 1"] = 105*TICRATE,
    ["Techno Hill Zone 2"] = 150*TICRATE,
	["Deep Sea Zone 1"] = 85*TICRATE,
	["Deep Sea Zone 2"] = 85*TICRATE,
	["Castle Eggman Zone 1"] = 95*TICRATE,
	["Castle Eggman Zone 2"] = 120*TICRATE,
	["Arid Canyon Zone 1"] = 95*TICRATE,
	["Arid Canyon Zone 2"] = 180*TICRATE
}
rawset(_G, "levelTimes", levelTimes)

addHook("MapLoad", function()
	local time = levelTimes[G_BuildMapTitle(gamemap)]
    if time then
        MaxTime = time
    else -- no preset time
        MaxTime = nil
    end
	TimeTilPF = MaxTime
end)


local function time(v, player)
	if player.mo and player.mo.valid
		if (maptol & TOL_NIGHTS) or PBPizzaTime == nil then -- Prevents the code from running in a Nights special stage
			return
		end
		local jind = (
			77*FU + 
			FU*(MaxTime-TimeTilPF)/MaxTime*148
		)
		local p_bar = v.cachePatch("PTTBAR") --this junk is mine
		local p_time = v.cachePatch("SMOLJ")
		local p_pizza = v.cachePatch("PIZZAB")
		local p_showt = v.cachePatch("SHOWT")

		if player.mo.valid and PBPizzaTime == true
			v.drawScaled(85*FRACUNIT, 175*FRACUNIT, FRACUNIT/2, p_bar, V_SNAPTOTOP|V_SNAPTOLEFT|V_HUDTRANS)
			v.drawScaled(jind, 178*FRACUNIT, FRACUNIT/2, p_time, V_SNAPTOTOP|V_SNAPTOLEFT|V_HUDTRANS)
			v.drawString(150, 175, (TimeTilPF))
			if jind < 225*FRACUNIT
				v.drawScaled(245*FRACUNIT,150*FRACUNIT, FRACUNIT/2, p_pizza, V_SNAPTOTOP|V_SNAPTOLEFT|V_HUDTRANS)
			elseif jind == 225*FRACUNIT
				v.drawScaled(245*FRACUNIT,135*FRACUNIT, FRACUNIT/2, p_showt, V_SNAPTOTOP|V_SNAPTOLEFT|V_HUDTRANS)
			end
		end
	end
end
hud.add(time, "game")

local function SpawnPizzaFace(x,y,z,p) //AI Pizza Pasta Put it in the box, come on, lets go
	if not pizzaboispawned and TimeTilPF <= 0
		if not executePTHook('SpawnAIPizzaface', {x,y,z,p})
			pizzaboi = P_SpawnMobjFromMobj(p.mo, 10*FRACUNIT,10*FRACUNIT,10*FRACUNIT, MT_PIZZAFACE)
			pizzaboi.gofor = p.mo
			p.powers[pw_invulnerability] = 10*TICRATE
			pizzaboi.speedToGo = p.normalspeed - (25 * FRACUNIT) / FRACUNIT
		end
	elseif pizzaboi and pizzaboi.valid and not pizzaboi.pizzaface_stun and p.mo and p.mo == pizzaboi.gofor
		if not executePTHook('TeleportAIPizzaface', {x,y,z,p})
			pizzaboi.pizzaface_stun = 10*TICRATE
			P_SetOrigin(pizzaboi, p.mo.x,p.mo.y,p.mo.z)
		end
	end
end

addHook("PlayerThink", function(player)
	if TimeTilPF > 0
		pizzaboispawned = false
	end
end)

addHook("PlayerThink", function(p)
	if pizzaboispawned == true and p.lapimpossible == false
		pizzaboi.speedToGo = p.normalspeed - (25 * FRACUNIT) / FRACUNIT
	elseif pizzaboispawned == true and p.lapimpossible == true
		pizzaboi.speedToGo = p.normalspeed - (8 * FRACUNIT) / FRACUNIT
	elseif pizzaboispawned == true and p.lapimpossible == false and p.powers[pw_underwater]
		pizzaboi.speedToGo = p.speed - (2 * FRACUNIT) / FRACUNIT
	elseif pizzaboispawned == true and p.lapimpossible == true and p.powers[pw_underwater]
		pizzaboi.speedToGo = p.speed - (1 * FRACUNIT) / FRACUNIT
	end
end)

addHook("ThinkFrame", function ()
	if TimeTilPF <= 0
		PIZZATIME = true
	elseif PBPizzaTime
		TimeTilPF = $ - 1
	end
end)

addHook("PlayerThink", function(p)	
	if PIZZATIME == true and pizzaboispawned == false
		SpawnPizzaFace(x,y,z,p)
		pizzaboispawned = true
	end
end)

addHook("MapLoad", function()
	for thing in mapthings.iterate
		if thing.type == 1
			P_SpawnMobj(thing.x*FU, thing.y*FU, thing.z*FU, MT_GATE)
		end
	end
end)

addHook("MobjThinker", function(gate)
	if gate and gate.valid
		SpawnSector = gate.subsector.sector
	end
end, MT_GATE)

addHook("TouchSpecial", function(gate, mo)
	if gate and gate.valid
		return true
	end
end, MT_GATE)

addHook("MobjThinker", function(gate)
	if PBPizzaTime == true
		gate.state = S_GATE_OPEN
	end
	if PBPizzaTime == false
		gate.state = S_GATE_CLOSED
	end
end, MT_GATE)

addHook("PlayerThink", function(player)
	if player.mo.subsector.sector == SpawnSector and PBPizzaTime == true
		P_DoPlayerFinish(player)
	end
end)

//Pizza Time Over

addHook("PlayerThink", function(player)
	if player.divebomb == nil
		player.divebomb = false
	end
	if player.uppercut == nil
		player.uppercut = 0
	end
	if player.cantuppercut == nil
		player.cantuppercut = false
	end
	if (player.diving) and (player.cmd.buttons & BT_JUMP) and (player.transformation == 0)
		player.diving = false
		player.divebomb = true
	end
	if P_IsObjectOnGround(player.mo) and player.divebomb or (player.mo.eflags & MFE_GOOWATER or player.powers[pw_carry] or P_PlayerInPain(player) or player.playerstate == PST_DEAD) and player.divebomb or player.transformation > 0 and player.divebomb
		player.divebomb = false
	end
	if player.divebomb == true
		P_SetObjectMomZ(player.mo, -50*FRACUNIT, false)
		P_InstaThrust(player.mo, player.mo.angle, 0*FRACUNIT)
		player.mo.state = S_PLAY_FALL
		player.normalspeed = 15*FRACUNIT
		player.mach3 = false
		player.mach4 = false
	end
	if (player.cmd.buttons & BT_CUSTOM2) and (player.cmd.buttons & BT_JUMP) and not player.superjumpbuild and player.superjump == 0 and player.uppercut == 0 and not player.cantuppercut and (player.transformation == 0)
		player.uppercut = 30
	end
	if player.uppercut > 0
		player.uppercut = player.uppercut - 1
	end
	if player.uppercut < 0
		player.uppercut = 0
	end
	if player.uppercut > 1
		player.mo.state = S_PLAY_SPRING
		P_SetObjectMomZ(player.mo, 10*FRACUNIT, false)
	end
	if player.uppercut == 1
		player.mo.state = S_PLAY_SUPER_TRANS1
	end
	if player.uppercut == 1 and not P_IsObjectOnGround(player.mo)
		player.cantuppercut = true
	end
	if player.cantuppercut == true and P_IsObjectOnGround(player.mo)
		player.cantuppercut = false
	end
end)

addHook("PlayerCanDamage", function(player)
	if (player.mach3) or (player.mach4) or (player.grabbing > 0) or (player.uppercut > 0) or (player.divebomb)
		return true
	end
	if (player.rolling) or (player.diving) or (player.wallrunning)
		return false
	end
end)

//Pizza Face Stuff
//Listen man, I've overworked myself to the max, idk if this does anything, but I'm putting it in anyways
//Have a good day
-- BUGGIE THE GOAAAAT WOOOOOOO (HE HELPED ALOT WITH AI PIZZAFACE)

local function P_FlyTo(mo, fx, fy, fz, sped, addques) --A very useful command honestly.
	if mo.valid
		local flyto = P_AproxDistance(P_AproxDistance(fx - mo.x, fy - mo.y), fz - mo.z)
		if flyto < 1
			flyto = 1
		end
		
		if addques
			mo.momx = $ + FixedMul(FixedDiv(fx - mo.x, flyto), sped)
			mo.momy = $ + FixedMul(FixedDiv(fy - mo.y, flyto), sped)
			mo.momz = $ + FixedMul(FixedDiv(fz - mo.z, flyto), sped)
		else
			mo.momx = FixedMul(FixedDiv(fx - mo.x, flyto), sped)
			mo.momy = FixedMul(FixedDiv(fy - mo.y, flyto), sped)
			mo.momz = FixedMul(FixedDiv(fz - mo.z, flyto), sped)
		end	
	end	
end

-- Pizza Mask Thinker
addHook("MobjThinker", function(mobj)
	if not mobj.valid return end
	if mobj.targetplayer and mobj.targetplayer.valid and mobj.targetplayer.realmo and mobj.targetplayer.realmo.valid then
		local targetplayer = mobj.targetplayer
		P_MoveOrigin(mobj, targetplayer.realmo.x, targetplayer.realmo.y, targetplayer.realmo.z)
	else
		P_RemoveMobj(mobj)
	end
end, MT_PIZZAMASK)

addHook("MobjThinker", function(mobj) --Stuff
	if mobj and mobj.valid
		if mobj.gofor and mobj.gofor.valid
		and mobj.gofor.health
		and not pizzaboi.pizzaface_stun
			local t = mobj.gofor
			local di = P_AproxDistance(P_AproxDistance(t.x-mobj.x,t.y-mobj.y),t.z-mobj.z)
			if not pizzaboi.pizzaface_stun
				P_FlyTo(mobj,t.x,t.y,t.z-(mobj.height/2),mobj.speedToGo)
				if not pizzaboi.LaughedLol
					S_StartSound(nil, sfx_pizzah)
					pizzaboi.LaughedLol = true
				end
			end
			if (di <= P_AproxDistance(t.radius,t.height)*2)
				if t.player and t.player.valid
					if t.player.powers[pw_flashing]
						t.player.powers[pw_flashing] = 0
					end
					mobj.targname = t.player.name
				end
				mobj.flags = $&~MF_NOCLIP
			else
				mobj.flags = $|MF_NOCLIP
			end
			mobj.hadtarget = true
		elseif pizzaboi.pizzaface_stun
			mobj.momx,mobj.momy,mobj.momz = 0,0,0
			pizzaboi.pizzaface_stun = $-1
		elseif not mobj.gofor or (mobj.gofor and not mobj.gofor.valid)
			if mobj.hadtarget
				mobj.momx = 0
				mobj.momy = 0
				mobj.momz = 0
				mobj.flags = $|MF_NOCLIP
			end
			local newp = PizzaTime.functions.GetNearestPlayer(mobj)
			if newp
				mobj.gofor = getNearestPlayer(mobj).mo
				mobj.speedToGo = (skins[newp].normalspeed - (8 * FRACUNIT)) / FRACUNIT
			end
			mobj.hadtarget = false
		end
		if not mobj.bepain
			mobj.flags = $|MF_PAIN
		elseif mobj.bepain
			mobj.flags = $&~MF_PAIN
			mobj.bepain = false
		end
	end
end,MT_PIZZAFACE)

//:sad:

//Enemy Spawner by Cole207
--Escape Spawner from Pizza Tower
freeslot(
"MT_ESCAPESPAWNER",
"S_ESCAPESPAWNERIDLE",
"S_ESCAPESPAWNER1",
"S_ESCAPESPAWNER2",
"S_ESCAPESPAWNER3",
"S_ESCAPESPAWNER4",
"S_ESCAPESPAWNER5",
"S_ESCAPESPAWNER6",
"S_ESCAPESPAWNER7",
"S_ESCAPESPAWNER8",
"S_ESCAPESPAWNER9",
"S_ESCAPESPAWNER10",
"S_ESCAPESPAWNER11",
"S_ESCAPESPAWNER12",
"S_ESCAPESPAWNER13",
"S_ESCAPESPAWNER14",
"S_ESCAPESPAWNER15",
"S_ESCAPESPAWNER16",
"S_ESCAPESPAWNER17",
"S_ESCAPESPAWNER18",
"S_ESCAPESPAWNER19",
"S_ESCAPESPAWNER20",
"SPR_SPWE",
"sfx_espawn"
)

sfxinfo[sfx_espawn] = {
	flags = SF_TOTALLYSINGLE|SF_X4AWAYSOUND,
	caption = "Escape Spawner"
}

--Animation
states[S_ESCAPESPAWNERIDLE] = { sprite = SPR_NULL, frame = A, tics = -1, nextstate = S_ESCAPESPAWNERIDLE }
states[S_ESCAPESPAWNER1] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|A, tics = 1, nextstate = S_ESCAPESPAWNER2 }
states[S_ESCAPESPAWNER2] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|B, tics = 1, nextstate = S_ESCAPESPAWNER3 }
states[S_ESCAPESPAWNER3] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|C, tics = 1, nextstate = S_ESCAPESPAWNER4 }
states[S_ESCAPESPAWNER4] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|D, tics = 1, nextstate = S_ESCAPESPAWNER5 }
states[S_ESCAPESPAWNER5] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|E, tics = 1, nextstate = S_ESCAPESPAWNER6 }
states[S_ESCAPESPAWNER6] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|F, tics = 1, nextstate = S_ESCAPESPAWNER7 }
states[S_ESCAPESPAWNER7] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|G, tics = 1, nextstate = S_ESCAPESPAWNER8 }
states[S_ESCAPESPAWNER8] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|H, tics = 1, nextstate = S_ESCAPESPAWNER9 }
states[S_ESCAPESPAWNER9] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|I, tics = 1, nextstate = S_ESCAPESPAWNER10 }
states[S_ESCAPESPAWNER10] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|J, tics = 1, nextstate = S_ESCAPESPAWNER11 }
states[S_ESCAPESPAWNER11] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|K, tics = 3, nextstate = S_ESCAPESPAWNER12 }
states[S_ESCAPESPAWNER12] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|L, tics = 1, nextstate = S_ESCAPESPAWNER13 }
states[S_ESCAPESPAWNER13] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|M, tics = 1, nextstate = S_ESCAPESPAWNER14 }
states[S_ESCAPESPAWNER14] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|N, tics = 1, nextstate = S_ESCAPESPAWNER15 }
states[S_ESCAPESPAWNER15] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|O, tics = 1, nextstate = S_ESCAPESPAWNER16 }
states[S_ESCAPESPAWNER16] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|P, tics = 1, nextstate = S_ESCAPESPAWNER17 }
states[S_ESCAPESPAWNER17] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|Q, tics = 1, nextstate = S_ESCAPESPAWNER18 }
states[S_ESCAPESPAWNER18] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|R, tics = 1, nextstate = S_ESCAPESPAWNER19 }
states[S_ESCAPESPAWNER19] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|S, tics = 1, nextstate = S_ESCAPESPAWNER20 }
states[S_ESCAPESPAWNER20] = { sprite = SPR_SPWE, frame = FF_FULLBRIGHT|T, tics = 1, nextstate = S_ESCAPESPAWNERIDLE }

--The Spawning Action
function A_PizzaTowerEscapeSpawn(actor, var1, var2)
	A_PlaySeeSound(actor,var1,var2)
	local z = actor.z
	if actor.eflags&MFE_VERTICALFLIP then
		z = $1+FixedMul(actor.info.height-mobjinfo[actor.health].height,actor.scale)
	end
	local enemy = P_SpawnMobj(actor.x,actor.y,z,actor.health)
	if actor.eflags&MFE_VERTICALFLIP then
		enemy.eflags = $1|MFE_VERTICALFLIP
		enemy.flags2 = $1|MF2_OBJECTFLIP
	end
	enemy.scale = actor.scale
	P_MoveOrigin(enemy,actor.x,actor.y,z)
	if P_SupermanLook4Players(enemy) then A_FaceTarget(enemy,0,0) end
	actor.target = enemy
end

states[S_ESCAPESPAWNER6].action = A_PizzaTowerEscapeSpawn

--Now Let's do The Spawner
mobjinfo[MT_ESCAPESPAWNER] = {
        doomednum = -1,
        spawnstate = S_ESCAPESPAWNERIDLE,
        spawnhealth = MT_BLUECRAWLA, --Object to Spawn in
        seestate = S_ESCAPESPAWNER1,
        seesound = sfx_espawn,
        reactiontime = 350, --Time that takes to Spawn the Enemy
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = 200,
        painsound = sfx_None,
        meleestate = S_NULL,
        missilestate = S_NULL,
        deathstate = S_NULL,
        xdeathstate = S_NULL,
        deathsound = sfx_None,
        speed = 0,
        radius = 50*FRACUNIT,
        height = 100*FRACUNIT,
        dispoffset = 0,
        mass = 100,
        damage = 0,
        activesound = sfx_None,
        flags = MF_NOGRAVITY|MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOCLIPTHING,
        raisestate = S_NULL
}

--The Pizza Tower Escape Spawning List
if not(PT_EscapeSpawnList)
	rawset(_G, "PT_EscapeSpawnList", {})
end
PT_EscapeSpawnList[MT_BLUECRAWLA] = MT_BLUECRAWLA
PT_EscapeSpawnList[MT_REDCRAWLA] = MT_REDCRAWLA
PT_EscapeSpawnList[MT_GFZFISH] = MT_JETTGUNNER
PT_EscapeSpawnList[MT_GOLDBUZZ] = MT_GOLDBUZZ
PT_EscapeSpawnList[MT_REDBUZZ] = MT_REDBUZZ
PT_EscapeSpawnList[MT_JETTBOMBER] = MT_JETTBOMBER
PT_EscapeSpawnList[MT_JETTGUNNER] = MT_JETTGUNNER
PT_EscapeSpawnList[MT_CRAWLACOMMANDER] = MT_CRAWLACOMMANDER
PT_EscapeSpawnList[MT_SKIM] = MT_SKIM
PT_EscapeSpawnList[MT_POPUPTURRET] = MT_JETTGUNNER
PT_EscapeSpawnList[MT_SPINCUSHION] = MT_SPINCUSHION
PT_EscapeSpawnList[MT_CRUSHSTACEAN] = MT_REDCRAWLA
PT_EscapeSpawnList[MT_BANPYURA] = MT_SPRINGSHELL
PT_EscapeSpawnList[MT_JETJAW] = MT_JETJAW
PT_EscapeSpawnList[MT_VULTURE] = MT_VULTURE
PT_EscapeSpawnList[MT_POINTY] = MT_JETTBOMBER
PT_EscapeSpawnList[MT_ROBOHOOD] = MT_ROBOHOOD
PT_EscapeSpawnList[MT_FACESTABBER] = MT_FACESTABBER
PT_EscapeSpawnList[MT_EGGGUARD] = MT_FACESTABBER
PT_EscapeSpawnList[MT_GSNAPPER] = MT_MINUS
PT_EscapeSpawnList[MT_MINUS] = MT_MINUS
PT_EscapeSpawnList[MT_SPRINGSHELL] = MT_SPRINGSHELL
PT_EscapeSpawnList[MT_YELLOWSHELL] = MT_SPRINGSHELL
PT_EscapeSpawnList[MT_UNIDUS] = MT_JETTBOMBER
PT_EscapeSpawnList[MT_CANARIVORE] = MT_CANARIVORE
PT_EscapeSpawnList[MT_PYREFLY] = MT_PYREFLY
PT_EscapeSpawnList[MT_PTERABYTE] = MT_PYREFLY
PT_EscapeSpawnList[MT_DRAGONBOMBER] = MT_PYREFLY
PT_EscapeSpawnList[MT_CRAWLASTATUE] = MT_REDCRAWLA
PT_EscapeSpawnList[MT_FACESTABBERSTATUE] = MT_FACESTABBER
PT_EscapeSpawnList[MT_SUSPICIOUSFACESTABBERSTATUE] = MT_FACESTABBER
PT_EscapeSpawnList[MT_ROSY] = MT_METALSONIC_BATTLE --In Sonic CD, Amy get's kidnapped by Metal Sonic!
PT_EscapeSpawnList[MT_GOOMBA] = MT_GOOMBA
PT_EscapeSpawnList[MT_BLUEGOOMBA] = MT_BLUEGOOMBA
PT_EscapeSpawnList[MT_PIAN] = MT_SHLEEP
PT_EscapeSpawnList[MT_SHLEEP] = MT_SHLEEP
PT_EscapeSpawnList[MT_PENGUINATOR] = MT_PENGUINATOR
PT_EscapeSpawnList[MT_POPHAT] = MT_POPHAT
PT_EscapeSpawnList[MT_HIVEELEMENTAL] = MT_BUMBLEBORE
PT_EscapeSpawnList[MT_BUMBLEBORE] = MT_BUMBLEBORE
PT_EscapeSpawnList[MT_BUGGLE] = MT_JETJAW
PT_EscapeSpawnList[MT_CACOLANTERN] = MT_CACOLANTERN
PT_EscapeSpawnList[MT_SPINBOBERT] = MT_CACOLANTERN
PT_EscapeSpawnList[MT_HANGSTER] = MT_HANGSTER
PT_EscapeSpawnList[MT_RING] = MT_RING

local escapespawner_bool = CV_RegisterVar({name = "pt_escapespawner", defaultvalue = "PizzaTimeOnly", flags = CV_NETVAR, PossibleValue = {Off = 0, On = 1, PizzaTimeOnly = 2}})
local escapespawner_timer = CV_RegisterVar({name = "pt_escapespawner_timetillspawn", defaultvalue = "5", flags = CV_NETVAR, PossibleValue = {MIN = 5, MAX = 60}})

addHook("MapLoad", function()
	if escapespawner_bool.value == 0 then return end
	local listofspawners = {}
	for enemy in mobjs.iterate()
		if PT_EscapeSpawnList[enemy.type] == nil then continue end --You want the Object to spawn in
		local spawnlocation = {
			x = enemy.x,
			y = enemy.y,
			z = enemy.z,
			objtype = PT_EscapeSpawnList[enemy.type],
			scale = enemy.scale,
			flip = false,
			target = enemy
		}
		if enemy.eflags&MFE_VERTICALFLIP then
			spawnlocation.flip = true
			spawnlocation.z = $1+enemy.height-FixedMul(100*FRACUNIT,enemy.scale)
		end
		table.insert(listofspawners,spawnlocation)
	end
	for _,v in ipairs(listofspawners) do
		--Add a lot of Escape Spawners
		local escapespawner = P_SpawnMobj(v.x,v.y,v.z,MT_ESCAPESPAWNER)
		escapespawner.health = v.objtype
		escapespawner.scale = v.scale
		if v.flip == true then
			escapespawner.eflags = $1|MFE_VERTICALFLIP
		end
		escapespawner.target = v.target
		escapespawner.reactiontime = escapespawner_timer.value*TICRATE
	end
end)

addHook("MobjThinker", function(spawner)
	if spawner.state ~= S_ESCAPESPAWNERIDLE then
		if mobjinfo[spawner.health].flags&MF_SPAWNCEILING then
			spawner.renderflags = RF_VERTICALFLIP|RF_FULLBRIGHT|RF_NOCOLORMAPS
		else
			spawner.renderflags = RF_FULLBRIGHT|RF_NOCOLORMAPS
		end
		return false
	end
	spawner.flags2 = $1|MF2_DONTDRAW --Make him Invisible
	local spawnrange = 600*FRACUNIT
	if mobjinfo[spawner.health].flags&MF_NOGRAVITY then spawnrange = $1+400*FRACUNIT end
	if mobjinfo[spawner.health].flags&MF_BOSS then spawnrange = FixedMul($1,0x00018000) end
	if spawner.target ~= nil and spawner.target.valid == true then --The Enemy is still there!
		spawner.reactiontime = escapespawner_timer.value*TICRATE
	elseif spawner.reactiontime > 0 then
		spawner.reactiontime = $1-1
	elseif (escapespawner_bool.value == 1 or (escapespawner_bool.value == 2 and gametype ~= nil and (
	((gametype == GT_PIZZATIME or gametype == GT_PIZZATIME1V1) and leveltime > 34*TICRATE)//PIZZA TIME
	or (PBPizzaTime == true)//PIZZA TIME JISK EDITION
	or (gametype == GT_PIZZATIME2 and PizzaTime.sync.PizzaTime)//PIZZA TIME 2.0
	))) and P_LookForPlayers(spawner,FixedMul(spawnrange,spawner.scale),true,false) == true then
		--Spawn in
		spawner.state = S_ESCAPESPAWNER1
		spawner.tics = 1
		spawner.flags2 = $1&~MF2_DONTDRAW
	end
	return true
end,MT_ESCAPESPAWNER)

//K, Transformation time

addHook("PlayerThink", function(player)
	if player.transformation == 1 and player.mo.standingslope or player.knightslide == true
		P_InstaThrust(player.mo, player.mo.angle, 60*FRACUNIT)
		player.mach4 = true
		player.normalspeed = 60*FRACUNIT
		if P_IsObjectOnGround(player.mo)
			player.mo.state = S_PLAY_PAIN
		elseif not P_IsObjectOnGround(player.mo) and player.mo.momz > 0
			player.mo.state = S_PLAY_SPRING
		elseif not P_IsObjectOnGround(player.mo) and player.mo.momz < 0
			player.mo.state = S_PLAY_FALL
		end
		player.knightslide = true
	end
	if player.transformation == 0 and player.knightslide == true
		player.knightslide = false
	end
	if (player.mo.eflags & MFE_SPRUNG) and player.transformation == 1
		player.knightslide = false
	end
	if (player.transformation == 1) and (player.cmd.buttons & BT_CUSTOM1) and not (P_IsObjectOnGround(player.mo))
		P_InstaThrust(player.mo, player.mo.angle, 0*FRACUNIT)
		P_SetObjectMomZ(player.mo, -40*FRACUNIT, false)
		player.knightslide = false
		player.normalspeed = 40*FRACUNIT
	end
end)

// Knight time done
//Lap 3 soon!?!?!?

addHook("ThinkFrame", function()
	if PBPizzaTime and S_MusicName(mapmusname)
		if not Lap2
			S_ChangeMusic("PIZTIM", true, i)
		elseif Lap2
			S_ChangeMusic("DEAOLI", true, i)
		end
	end
end)

//Death PACT

addHook("PlayerThink", function(player)
	if P_IsObjectOnGround(player.mo) and not player.powers[pw_underwater] and not player.powers[pw_spacetime]
		player.prevx = player.mo.x
		player.prevy = player.mo.y
		player.prevz = player.mo.z
	end
end)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target and target.valid and target.player and target.player.valid and damagetype == DMG_DEATHPIT
		P_SetOrigin(target.player.mo, target.player.prevx, target.player.prevy, target.player.prevz)
		if target.player.score >= 100
			P_AddPlayerScore(target.player, -100)
		end
		target.player.combo = target.player.combo - 50
		return false
	end
end)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target and target.valid and target.player and target.player.valid and damagetype == DMG_DROWNED
		P_SetOrigin(target.player.mo, target.player.prevx, target.player.prevy, target.player.prevz)
		if target.player.score >= 100
			P_AddPlayerScore(target.player, -100)
		end
		target.player.combo = target.player.combo - 50
		return false
	end
end)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target and target.valid and target.player and target.player.valid and damagetype == DMG_CRUSHED
		if target.player.score >= 100
			P_AddPlayerScore(target.player, -100)
		end
		target.player.combo = target.player.combo - 50
		return false
	end
end)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target and target.valid and target.player and target.player.valid and damagetype == DMG_SPIKE and not target.player.taunting
		P_SetObjectMomZ(target.player.mo, 15*FRACUNIT, false)
		P_InstaThrust(target.player.mo, target.player.mo.angle, 0*FRACUNIT)
		target.player.mo.state = S_PLAY_PAIN
		S_StartSound(target.player.mo, L_Choose(sfx_ahh,sfx_ah,sfx_yeow,sfx_aww))
		if target.player.score >= 50
			P_AddPlayerScore(target.player, -50)
		end
		P_InstaThrust(target.player.mo, target.player.mo.angle, -20*FRACUNIT)
		target.player.combo = target.player.combo - 50
		target.player.liquiddamage = true
		return false
	end
end)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target and target.valid and target.player and target.player.valid and damagetype == DMG_WATER and not target.player.taunting
		P_SetObjectMomZ(target.player.mo, 15*FRACUNIT, false)
		P_InstaThrust(target.player.mo, target.player.mo.angle, 0*FRACUNIT)
		target.player.mo.state = S_PLAY_PAIN
		S_StartSound(target.player.mo, L_Choose(sfx_ahh,sfx_ah,sfx_yeow,sfx_aww))
		if target.player.score >= 50
			P_AddPlayerScore(target.player, -50)
		end
		target.player.combo = target.player.combo - 50
		target.player.liquiddamage = true
		return false
	end
end)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target and target.valid and target.player and target.player.valid and damagetype == DMG_FIRE and not target.player.taunting
		P_SetObjectMomZ(target.player.mo, 15*FRACUNIT, false)
		P_InstaThrust(target.player.mo, target.player.mo.angle, 0*FRACUNIT)
		target.player.mo.state = S_PLAY_PAIN
		S_StartSound(target.player.mo, L_Choose(sfx_ahh,sfx_ah,sfx_yeow,sfx_aww))
		if target.player.score >= 50
			P_AddPlayerScore(target.player, -50)
		end
		target.player.combo = target.player.combo - 50
		target.player.liquiddamage = true
		return false
	end
end)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target and target.valid and target.player and target.player.valid and damagetype == DMG_ELECTRIC and not target.player.taunting
		P_SetObjectMomZ(target.player.mo, 15*FRACUNIT, false)
		P_InstaThrust(target.player.mo, target.player.mo.angle, 0*FRACUNIT)
		target.player.mo.state = S_PLAY_PAIN
		S_StartSound(target.player.mo, L_Choose(sfx_ahh,sfx_ah,sfx_yeow,sfx_aww))
		if target.player.score >= 50
			P_AddPlayerScore(target.player, -50)
		end
		target.player.combo = target.player.combo - 50
		target.player.liquiddamage = true
		return false
	end
end)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target and target.valid and target.player and target.player.valid and damagetype == DMG_SPACEDROWN and not target.player.taunting
		P_SetOrigin(target.player.mo, target.player.prevx, target.player.prevy, target.player.prevz)
		if target.player.score >= 50
			P_AddPlayerScore(target.player, -50)
		end
		target.player.combo = target.player.combo - 50
		return false
	end
end)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if (target) and (target.valid) and (target.player) and (target.player.valid) and (damagetype == DMG_INSTAKILL or damagetype == DMG_DEATHMASK) and not (target.player.taunting)
		P_SetObjectMomZ(target.player.mo, 15*FRACUNIT, false)
		P_InstaThrust(target.player.mo, target.player.mo.angle, -10*FRACUNIT)
		target.player.mo.state = S_PLAY_PAIN
		S_StartSound(target.player.mo, L_Choose(sfx_ahh,sfx_ah,sfx_yeow,sfx_aww))
		if target.player.score >= 50
			P_AddPlayerScore(target.player, -50)
		end
		target.player.combo = target.player.combo - 50
		target.player.liquiddamage = true
		return false
	end
end)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target and target.valid and target.player and target.player.valid and damagetype ~= DMG_ELECTRIC and damagetype ~= DMG_SPACEDROWN and damagetype ~= DMG_FIRE and damagetype ~= DMG_WATER and damagetype ~= DMG_SPIKE and damagetype ~= DMG_INSTAKILL and damagetype ~= DMG_CRUSHED and damagetype ~= DMG_DROWNED and damagetype ~= DMG_DEATHPIT and damagetype ~= DMG_NUKE and not target.player.taunting and inflictor ~= pizzaboi // Remove this inflictor for DBZ mode
		P_SetObjectMomZ(target.player.mo, 20*FRACUNIT, false)
		P_InstaThrust(target.player.mo, target.player.mo.angle, -20*FRACUNIT)
		target.player.pflags = $ + PF_STASIS
		S_StartSound(target.player.mo, L_Choose(sfx_ahh,sfx_ah,sfx_yeow,sfx_aww))
		if target.player.score >= 50
			P_AddPlayerScore(target.player, -50)
		end
		target.player.combo = target.player.combo - 50
		target.player.liquiddamage = true
		return false
	end
end)

addHook("ShouldDamage", function(target, inflictor, source, damage, damagetype)
	if target and target.valid and target.player and target.player.valid and damagetype == DMG_NUKE and not target.player.taunting
		P_SetObjectMomZ(target.player.mo, 15*FRACUNIT, false)
		P_InstaThrust(target.player.mo, target.player.mo.angle, 0*FRACUNIT)
		target.player.mo.state = S_PLAY_PAIN
		S_StartSound(target.player.mo, L_Choose(sfx_ahh,sfx_ah,sfx_yeow,sfx_aww))
		if target.player.score >= 50
			P_AddPlayerScore(target.player, -50)
		end
		P_InstaThrust(target.player.mo, target.player.mo.angle, -20*FRACUNIT)
		target.player.combo = target.player.combo - 50
		target.player.liquiddamage = true
		return false
	end
end)

addHook("PlayerThink", function(player)
	if player.liquiddamage == true
		player.mo.state = S_PLAY_PAIN
		player.normalspeed = 15*FRACUNIT
	end
	if P_IsObjectOnGround(player.mo) and player.liquiddamage == true
		player.liquiddamage = false
	end
end)

//Face man

local function face(v, player)
	if player.mo and player.mo.valid
		if (maptol & TOL_NIGHTS) then -- Prevents the code from running in a Nights special stage
			return
		end
		
		local facesprite = v.getSprite2Patch(player.skin, player.mo.sprite2, false, player.mo.frame, 3)
		local facetranssprite = v.getSprite2Patch(player.skin, player.mo.sprite2, true, player.mo.frame, 3)
		local facespritewater = v.getSprite2Patch(player.skin, SPR2_GASP, false, A, 3)
		local facetransspritewater = v.getSprite2Patch(player.skin, SPR2_GASP, true, A, 3)
		local facespritegravity = v.getSprite2Patch(player.skin, SPR2_EDGE, false, ((leveltime/11) % 2), 3, ANGLE_180)
		local facetransspritegravity = v.getSprite2Patch(player.skin, SPR2_EDGE, true, ((leveltime/11) % 2), 3, ANGLE_180)
		local facesupersprite = v.getSprite2Patch(player.skin, player.mo.sprite2, true, player.mo.frame, 3)
		local facesuperspritewater = v.getSprite2Patch(player.skin, SPR2_GASP, true, A, 3)
		local facesuperspritegravity = v.getSprite2Patch(player.skin, SPR2_EDGE, true, ((leveltime/11) % 2), 3, ANGLE_180)
		local facecolor = v.getColormap(player.skin, player.mo.color)
		
		if player.transformation == 0 and not ((player.mo.eflags & MFE_UNDERWATER) or (player.powers[pw_spacetime]) or (player.mo.eflags & MFE_VERTICALFLIP) or (player.powers[pw_super]))
			v.draw(50, 167, facesprite, V_PERPLAYER, facecolor)
		elseif player.transformation > 0 and not ((player.mo.eflags & MFE_UNDERWATER) or (player.powers[pw_spacetime]) or (player.mo.eflags & MFE_VERTICALFLIP) or (player.powers[pw_super]))
			v.draw(50, 167, facetranssprite, V_PERPLAYER, facecolor)
		elseif (player.mo.eflags & MFE_UNDERWATER) or (player.powers[pw_spacetime]) and (player.transformation == 0) and not (player.mo.eflags & MFE_VERTICALFLIP) and not (player.powers[pw_super])
			v.draw(50, 167, facespritewater, V_PERPLAYER, facecolor)
		elseif (player.mo.eflags & MFE_UNDERWATER) or (player.powers[pw_spacetime]) and (player.transformation > 0) and not (player.mo.eflags & MFE_VERTICALFLIP) and not (player.powers[pw_super])
			v.draw(50, 167, facetransspritewater, V_PERPLAYER, facecolor)
		elseif (player.mo.eflags & MFE_VERTICALFLIP) and (player.transformation == 0) and not (player.powers[pw_super])
			v.draw(50, 167, facespritegravity, V_PERPLAYER, facecolor)
		elseif (player.mo.eflags & MFE_VERTICALFLIP) and (player.transformation > 0) and not (player.powers[pw_super])
			v.draw(50, 167, facetransspritegravity, V_PERPLAYER, facecolor)
		elseif not ((player.mo.eflags & MFE_UNDERWATER) or (player.powers[pw_spacetime]) or (player.mo.eflags & MFE_VERTICALFLIP)) and (player.powers[pw_super])
			v.draw(50, 167, facesupersprite, V_PERPLAYER, facecolor)
		elseif (player.mo.eflags & MFE_UNDERWATER) or (player.powers[pw_spacetime]) and not (player.mo.eflags & MFE_VERTICALFLIP) and (player.powers[pw_super])
			v.draw(50, 167, facesuperspritewater, V_PERPLAYER, facecolor)
		elseif (player.mo.eflags & MFE_VERTICALFLIP) and (player.powers[pw_super])
			v.draw(50, 167, facesuperspritegravity, V_PERPLAYER, facecolor)
		end
	end
end
hud.add(face, "game")

//it's been a while since I wrote that much code, I feel proud of myself