local isdelayset

addHook("MobjThinker", function(mobj)
	local player = mobj.player

	// Hey look, it's Nitemare, he does stuff
	if (player.mo and player.mo.skin == "nitemare")

		player.drowncheck = false


		// Reset firerate delay
		if player.weapondelay == 0
			isdelayset = false
		end
		

		// Not Super
		if not player.powers[pw_super] and not All7Emeralds(player.powers[pw_emeralds])
			// Reset actionspd
			player.actionspd = FRACUNIT*20
			// Check firerate delay
			if not isdelayset and player.weapondelay > 0
				player.weapondelay = 3*$1
				isdelayset = true
			end
		else
			// Is Super
			// Double actionspd
			player.actionspd = FRACUNIT*40

			// Check firerate delay
			if not isdelayset and player.weapondelay > 0
			// Even slower firerate when Super
				player.weapondelay = 4*$1
				isdelayset = true
			end
		end

		// Is Super
		if player.powers[pw_super] and All7Emeralds(player.powers[pw_emeralds])
			// Spawn flame trail when running
			if player.panim == PA_RUN
				P_ElementalFireTrail(player)
				// Drain rings at 2x speed while Super
				if (leveltime % TICRATE == TICRATE/2) and !(player.exiting)
					player.health = $1-1
					player.mo.health = $1-1
				end
			end
		end


		// Nitemare can breathe in lava
		if P_PlayerTouchingSectorSpecial(player, 1, 3) and player.mo.eflags & MFE_UNDERWATER
			// This appearently gives infinite air
			player.powers[pw_underwater] = 25*TICRATE
		else
			if player.powers[pw_underwater] == 30*TICRATE // Default underwater timer
				player.powers[pw_underwater] = $1-($1/5) // Nitemare underwater timer
			end
		end

		// Remove from the timer
		if not player.drowncheck and player.powers[pw_underwater] <= 30*TICRATE and player.powers[pw_underwater] >25*TICRATE
			player.powers[pw_underwater] = $1-5*TICRATE
			player.drowncheck = true
		end


		// Climbing
		if player.climbing

			// Vertical
			if (player.cmd.forwardmove)
				P_SetObjectMomZ(player.mo, FixedDiv(player.cmd.forwardmove*FRACUNIT, 25*FRACUNIT/4), false) // Originally 5*FRACUNIT
				if player.powers[pw_super] and All7Emeralds(player.powers[pw_emeralds])
					player.mo.momz = 3*$1/2 // Originally *= 2
				end
			end

			// Horizontal
			if (player.cmd.sidemove)
				if player.powers[pw_super] and All7Emeralds(player.powers[pw_emeralds])
					P_InstaThrust(player.mo, player.mo.angle-ANGLE_90, FixedMul(FixedDiv(player.cmd.sidemove*FRACUNIT, 25*FRACUNIT/4), player.mo.scale)) // Originally 5*FRACUNIT
				else
					P_InstaThrust(player.mo, player.mo.angle-ANGLE_90, FixedMul(FixedDiv(player.cmd.sidemove*FRACUNIT, 50*FRACUNIT/4), player.mo.scale)) // Originally 10*FRACUNIT
				end
			end
		end


		// Gliding
		if (player.pflags & PF_GLIDING)

			// Upsidedown
			if (player.mo.eflags & MFE_VERTICALFLIP)

				if player.mo.momz > FixedMul(1*FRACUNIT/2, player.mo.scale) // Originally 2*FRACUNIT
					player.mo.momz = $1-FixedMul(7*FRACUNIT/32, player.mo.scale) // Originally 3*FRACUNIT/4
					if player.powers[pw_super] and All7Emeralds(player.powers[pw_emeralds])
						player.mo.momz = $1-FixedMul(FRACUNIT, player.mo.scale)
					end
				end
			else

			// Rightsideup
				if player.mo.momz < FixedMul(-1*FRACUNIT/2, player.mo.scale) // Originally -2*FRACUNIT
					player.mo.momz = $1+FixedMul(7*FRACUNIT/32, player.mo.scale) // Originally 3*FRACUNIT/4
					if player.powers[pw_super] and All7Emeralds(player.powers[pw_emeralds])
						player.mo.momz = $1+FixedMul(FRACUNIT, player.mo.scale)
					end
				end
			end
		end


	// Revert the original 5 second removal
	else
		if player.drowncheck and player.powers[pw_underwater]
			player.powers[pw_underwater] = $1+5*TICRATE
		end
	end
end, MT_PLAYER)


addHook("ShouldDamage", function(target, inflictor, source, damage)

	// I am immune to fiery things
	if (target.skin == "nitemare")
		// Fire objects
		if (inflictor and inflictor.flags & MF_FIRE) or (source and source.flags & MF_FIRE)
			return false
		end
		// Lava
		if not inflictor and not source and P_PlayerTouchingSectorSpecial(target.player, 1, 3)
			return false
		end
	end


end, MT_PLAYER)