freeslot("sfx_lwastr", "sfx_asrbgs", "sfx_sclasr", "sfx_scdril", "sfx_wispno", "sfx_scrckt", "sfx_rocket", "sfx_boxopn", "sfx_wispof", "sfx_wispon", "sfx_wspget", "sfx_bstwnd", "sfx_bsttap", "sfx_bsthld", "sfx_burst1", "sfx_bstfx1", "sfx_bstfx2", "sfx_bstfx3", "sfx_airspn", "sfx_jumpsc", "sfx_jumpwt", "sfx_spnwtr", "sfx_bswtrw", "sfx_bswtr1", "sfx_bswtr2")
freeslot("MT_ASTEROIDDEBRIS", "MT_BURSTEFFECT1")
freeslot("MT_ASTEROIDBOX")
freeslot("MT_LASERBOX")
freeslot("MT_LASERPLACEHOLDER", "MT_DRILLPLACEHOLDER", "MT_ROCKETPLACEHOLDER", "MT_BOOSTPLACEHOLDER", "MT_BURSTPLACEHOLDER", "MT_ASTEROIDPLACEHOLDER")
freeslot("sfx_lrbeep", "sfx_lsrpip", "sfx_lrchrg", "sfx_rcrglp", "sfx_rcrg", "sfx_lrfin", "sfx_lrzoom")
freeslot("sfx_dj1", "sfx_dj2", "sfx_dj3", "sfx_hm1", "sfx_hm2", "sfx_hm3")
freeslot("MT_ROCKETBOX", "MT_DRILLBOX", "MT_BURSTBOX", "MT_BOOSTBOX")
freeslot("SPR_SFLY")
freeslot("SPR_RETT", "S_LASERRETICLE", "MT_LASERRETICLE")
//RETTA-F = spinning red cross, animated by FF_ANIMATE (6 frames, 2 tics each)
states[S_LASERRETICLE] = {SPR_RETT, FF_FULLBRIGHT|FF_ANIMATE|A, -1, nil, 5, 2, S_LASERRETICLE}
mobjinfo[MT_LASERRETICLE] = {
	doomednum = -1,
	spawnstate = S_LASERRETICLE,
	spawnhealth = 1000,
	radius = 16*FRACUNIT,
	height = 32*FRACUNIT,
	flags = MF_NOGRAVITY|MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOBLOCKMAP|MF_SCENERY
}
local wisps = {
ringdrain = true,
allowsuperfly = true,
cheats = false
}

addHook("NetVars", function(net)
	wisps = net(wisps)
end)

addHook("ThinkFrame", do
	for p in players.iterate
		if not p.mo then continue end
		if not p.secondaryability then p.secondaryability = 0 end
		if (p.secondaryability == 0)
			//Disable all midair spin abilities
			p.maniadropdash = false
			p.stompattack = false
			p.lostworldbounce = false
		elseif (p.secondaryability == 1)
			//Mania Drop Dash Enabled
			p.maniadropdash = true
			p.stompattack = false
			p.lostworldbounce = false
		elseif (p.secondaryability == 2)
			//Colours' Stomp Enabled
			p.maniadropdash = false
			p.stompattack = true
			p.lostworldbounce = false
		elseif (p.secondaryability == 3)
			//Lost World's Bounce attack Enabled
			p.maniadropdash = false
			p.stompattack = false
			p.lostworldbounce = true
		end
	end
end)

COM_AddCommand("wisp_spinability", function(player, arg1)
	if not arg1
		CONS_Printf(player, "wisp_spinability <number>: change your midair spin ability")
		CONS_Printf(player, "Possible Values:")
		CONS_Printf(player, "0: Disable [Default]")
		CONS_Printf(player, "1: Mania Drop Dash")
		CONS_Printf(player, "2: Colours Stomp Attack")
		CONS_Printf(player, "3: Lost World Bounce Attack")
		return
	end
	if tonumber(arg1) > 3
		CONS_Printf(player, "Values higher than 3 aren't valid")
		return
	end
	if tonumber(arg1) < 0
		CONS_Printf(player, "Values lower than 0 aren't valid")
		return
	end
	if tonumber(arg1) == 0
		player.secondaryability = 0
		CONS_Printf(player, "Secondary ability disabled")
	elseif tonumber(arg1) == 1
		player.secondaryability = 1
		CONS_Printf(player, "Secondary ability is now the drop dash")
	elseif tonumber(arg1) == 2
		player.secondaryability = 2
		CONS_Printf(player, "Secondary ability is now the stomp attack")
	elseif tonumber(arg1) == 3
		player.secondaryability = 3
		CONS_Printf(player, "Secondary ability is now the bounce attack")
	end
end)

local axes
	
local function AsteroidFofBreak(player,bustsec)
	for rover in bustsec.ffloors() // search through FOFs in sector
		if not (rover.fofflags & FOF_EXISTS) continue end // FOF does not "exist" in sector!
		if not (rover.fofflags & FOF_BUSTUP) continue end
		if player.mo.z >= rover.topheight
			continue
		end

		if player.mo.z+player.mo.momz+player.mo.height < rover.bottomheight
			continue
		end

		EV_CrumbleChain(bustsec, rover)
	end
end

local function DebrisFofBreak(debris,bustsec)
	for rover in bustsec.ffloors() // search through FOFs in sector
		if not (rover.fofflags & FOF_EXISTS) continue end // FOF does not "exist" in sector!
		if not (rover.fofflags & FOF_BUSTUP) continue end
		
		if debris.z >= rover.topheight
			continue
		end

		if debris.z+debris.momz+debris.height < rover.bottomheight
			continue
		end
		
		EV_CrumbleChain(bustsec, rover)
	end
end

local function WispFofBreak(player,bustsec)
	for rover in bustsec.ffloors() // search through FOFs in sector
		if not (rover.fofflags & FOF_EXISTS) continue end // FOF does not "exist" in sector!
		if not (rover.fofflags & FOF_BUSTUP) continue end
		
		if player.mo.z >= rover.topheight
			continue
		end
		
		if player.mo.z+player.mo.momz+player.mo.height < rover.bottomheight
			continue
		end
		
		EV_CrumbleChain(bustsec, rover)
	end
end

addHook("MobjCollide", function(spike, wispcore)
	local play = wispcore.player
	if not play then return end
	if not play.mo then return end
	if not play.wisptimer
	or (play.bursting)
		return nil
	end
	if (wispcore.z > spike.z+spike.height)
	or (spike.z > wispcore.z+wispcore.height)
		return nil
	else	
		spike.health = 0
		S_StartSound(wispcore, spike.info.deathsound)
		P_KillMobj(spike, wispcore, wispcore)
		return false
	end
end, MT_SPIKE)

addHook("MobjCollide", function(spike, debriscore)
	if (debriscore.type == MT_ASTEROIDDEBRIS)
	
	if (debriscore.z > spike.z+spike.height)
	or (spike.z > debriscore.z+debriscore.height)
		return nil
	else
		if debriscore.target and debriscore.target.valid and debriscore.target.player
			debriscore.target.player.wisptimer = $1+(TICRATE/2)
		end
		spike.health = 0
		S_StartSound(debriscore, spike.info.deathsound)
		P_KillMobj(spike)
		return false
	end
	end
end, MT_SPIKE)	

local axes
local floattimer = 3*TICRATE

addHook("AbilitySpecial", function(player)
	if player.mo.skin == "sonic"
		if not (player.pflags & PF_THOKKED)
		and not P_LookForEnemies(player)
			player.charability = CA_NONE
			if (player.mo.eflags & MFE_UNDERWATER)
				S_StartSound(player.mo, sfx_spnwtr)
				local bubble = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_UWEXPLODE)
				bubble.scale = player.mo.scale
				P_SetObjectMomZ(player.mo, 5*FRACUNIT, false)
			else
				P_SetObjectMomZ(player.mo, 10*FRACUNIT, false)
				player.pflags = $1|PF_THOKKED
				local randomsound = P_RandomRange(1, 3)
				if not S_SoundPlaying(player.mo, sfx_dj1)
				and not S_SoundPlaying(player.mo, sfx_dj2)
				and not S_SoundPlaying(player.mo, sfx_dj3)
					if (randomsound == 1)
						S_StartSound(player.mo, sfx_dj1)
					elseif (randomsound == 2)
						S_StartSound(player.mo, sfx_dj2)
					elseif (randomsound == 3)
						S_StartSound(player.mo, sfx_dj3)
					end
					S_StartSound(player.mo, sfx_airspn)
				end
				player.pflags = $1 & ~PF_SPINNING
			end
		else
			player.charability = CA_HOMINGTHOK
		end
		if (player.pflags & PF_THOKKED)
		and P_LookForEnemies(player)
		and player.charability == CA_HOMINGTHOK
			player.pflags = $1 & ~PF_THOKKED
			if not (player.mo.eflags & MFE_UNDERWATER)
				local randomsoundh = P_RandomRange(1, 3)
				if not S_SoundPlaying(player.mo, sfx_hm1)
				and not S_SoundPlaying(player.mo, sfx_hm2)
				and not S_SoundPlaying(player.mo, sfx_hm3)
					if (randomsoundh == 1)
						S_StartSound(player.mo, sfx_hm1)
					elseif (randomsoundh == 2)
						S_StartSound(player.mo, sfx_hm2)
					elseif (randomsoundh == 3)
						S_StartSound(player.mo, sfx_hm3)
					end
				end
			end
		end
	end
end)

local function drillmusichandler(player)
	local randommusic = P_RandomRange(1, 20)
	if (randommusic >= 10)
		S_ChangeMusic("DRILL3", true, player)
	elseif ((randommusic < 10) and (randommusic >= 5))
		S_ChangeMusic("DRILL2", true, player)
	else
		S_ChangeMusic("DRILL1", true, player)
	end
end

local function lasermusichandler(player)
	local randommusic2 = P_RandomRange(1, 2)
	if (randommusic2 == 1)
		S_ChangeMusic("LASERL", true, player)
	else
		S_ChangeMusic("LASERM", true, player)
	end
end

freeslot("sfx_dready")

addHook("ThinkFrame", do
	for player in players.iterate
		if not player.mo then continue end
		if (player.mo and player.mo.valid)
			and (P_IsObjectOnGround(player.mo))
			player.prevgroundsonic = true
		else
			player.prevgroundsonic = false
		end
		//if not player.reservewisp then player.reservewisp = "none" end
		
		if (player.wisp2 == nil) then player.wisp2 = "none" end
		if (player.wisp1 == nil) then player.wisp1 = "none" end
		
		if (player.mo and player.mo.skin == "sonic")
		and not (player.powers[pw_carry] == CR_NIGHTSMODE)
			if not player.emeralds then player.emeralds = false end
			if (((All7Emeralds(emeralds)) or (player.powers[pw_emeralds] == 127)) and (player.rings >= 50)) then // are the conditions right for super?
				if not (player.wisp1 == "none") //if the current wisp in the first slot isn't occupied
				and not (player.wisptimer) //don't change if the wisp timer is not 0!
					player.wisp1 = "none"
				end
				if not (player.wisptimer)
					if not player.emeralds
						player.emeralds = true
						player.charflags = $1 & ~(SF_SUPER)
						player.powers[pw_emeralds] = 127
						S_StartSound(player.mo, sfx_dready, player)
					end
				end
			else
				if not (player.powers[pw_super])
					player.emeralds = false
				end
			end
			if player.wispuseon
			and player.emeralds
				if not player.powers[pw_super]
					player.powers[pw_emeralds] = 127
					P_DoSuperTransformation(player)
					player.charflags = $1 & ~(SF_SUPER)
				end
			end
			if player.powers[pw_super]
				player.powers[pw_emeralds] = 127 //GET ALL EMERALDS
				//Prevent the player from getting wisps
				player.wisp1 = "none"
				if not player.emeralds
					player.emeralds = true
					S_StartSound(player.mo, sfx_dready, player)
				end
				if (player.wisptimer > 0)
					player.wisptimer = 0
				end
			end
			
			if (player.custom3on)
			and not (player.wisptimer)
			and not (player.powers[pw_super] or player.emeralds)
				if (player.wisp1 ~= "none")
				or (player.wisp2 ~= "none")
					player.wisp1, player.wisp2 = $2, $1
					S_StartSound(player.mo, sfx_menu1, player)
				end
			end
			
			if S_SoundPlaying(player.mo, sfx_jump)
				S_StopSound(player.mo)
				S_StopSound(player.mo)
				if not (player.mo.eflags & MFE_UNDERWATER)
					S_StartSound(player.mo, sfx_jumpsc)
				else
					S_StartSound(player.mo, sfx_jumpwt)
				end
			end
			if player.wispgetdelay == nil
				player.wispgetdelay = 0
			end
			if player.wispgetdelay == 1
				S_StartSound(player.mo, sfx_wspget)
			end
			if player.wispgetdelay ~= 0
				player.wispgetdelay = $1 - 1
			end
			if not (player.cmd.buttons & BT_JUMP) then
				player.jumptapoff = true
				player.jumptapon = false
			elseif player.jumptapoff then
				player.jumptapon = true
				player.jumptapoff = false
			else
				player.jumptapon = false
			end
			if (player.pflags & PF_THOKKED)
			and (player.mo.eflags & MFE_UNDERWATER)
				player.pflags = $1 & ~PF_THOKKED
			end

			if player.pflags & PF_SPINNING
			and player.pflags & PF_JUMPED
				player.pflags = $1 & ~PF_SPINNING
			end
			if player.wisptimer == nil
				player.wisptimer = 0
			end
			if player.floattics == nil
			or (not player.asteroidactive)
				player.floattics = 0
			end
			if player.rocketon == nil
				player.rocketon = false
			end
			if player.asteroidactive == nil
				player.asteroidactive = false
			end
			if player.drilling == nil
				player.drilling = false
			end
			if player.bursting == nil
				player.bursting = false
			end
			
			if (player.wisptimer ~= 0)
			and not (player.mo.tracer and player.mo.tracer.type == MT_TUBEWAYPOINT)
			and not player.laserphase1
			and not player.laserzoom
				player.wisptimer = ($1 - 1)
			end
			/*if (player.wisptimer ~= 0)
			and (player.mo.tracer and player.mo.tracer.type == MT_TUBEWAYPOINT)
				player.powers[pw_invulnerability] = $1 + 1
			end*/
			
			if not (player.cmd.buttons & BT_CUSTOM1) then
				player.boostuseoff = true
				player.boostuseon = false
			elseif player.boostuseoff then
				player.boostuseon = true
				player.boostuseoff = false
			else
				player.boostuseon = false
			end
			
			if not (player.cmd.buttons & BT_CUSTOM2) then
				player.wispuseoff = true
				player.wispuseon = false
			elseif player.wispuseoff then
				player.wispuseon = true
				player.wispuseoff = false
			else
				player.wispuseon = false
			end
			
			if not (player.cmd.buttons & BT_CUSTOM3) then
				player.custom3off = true
				player.custom3on = false
			elseif player.custom3off then
				player.custom3on = true
				player.custom3off = false
			else
				player.custom3on = false
			end
			
			if player.prevrings == nil
				player.prevrings = 0
			end
			
			if player.boostmeter == nil
				player.boostmeter = 105
			end
			if player.boostmeter > 105
				player.boostmeter = 105
			end
			if player.boosting == nil
				player.boosting = false
			end
			if player.boostusetime == nil
				player.boostusetime = 0
			end
			if not player.laserphase1 then player.laserphase1 = false end
			
			if player.wisptimer
			and not player.rocketon
				player.powers[pw_invulnerability] = player.wisptimer
			end
			
			if player.wispuseon
			and (player.wisp1 == "asteroid")
			and (not player.asteroidactive)
			and not player.exiting
				if not (player.mo.tracer and player.mo.tracer.type == MT_TUBEWAYPOINT)
				and not (player.playerstate == PST_REBORN)
					player.mo.color = SKINCOLOR_PURPLE
					S_ChangeMusic("astero", true, player)
					S_StartSound(player.mo, sfx_lwastr)
					S_StartSound(player.mo, sfx_wispon)
					player.asteroidactive = true
					player.powers[pw_shield] = $1|SH_ATTRACT
					player.powers[pw_invulnerability] = 14*TICRATE
					player.jumpfactor = skins[player.mo.skin].jumpfactor + FRACUNIT/3
					player.wisptimer = 14*TICRATE
					player.mo.color = SKINCOLOR_PURPLE
				else
					S_StartSound(player.mo, sfx_wispno)
				end
			elseif (player.wisp1 == "rocket")
			and not player.exiting
			and (not player.rocketon)
			and player.wispuseon
				if not (player.mo.tracer and player.mo.tracer.type == MT_TUBEWAYPOINT)
				and not (player.playerstate == PST_REBORN)
					S_StartSound(player.mo, sfx_scrckt)
					S_StartSound(player.mo, sfx_rocket)
					player.rocketon = true
					player.wisptimer = 3*TICRATE
					player.rocketaim = true
				else
					S_StartSound(player.mo, sfx_wispno)
				end
			elseif (player.wisp1 == "drill")
			and not player.exiting
			and (not player.drilling)
			and player.wispuseon
				if (player.mo.eflags & (MFE_UNDERWATER|MFE_GOOWATER))
				and not (player.mo.tracer and player.mo.tracer.type == MT_TUBEWAYPOINT)
				and not (player.playerstate == PST_REBORN)
					S_StartSound(player.mo, sfx_scdril)
					S_StartSound(player.mo, sfx_wispon)
					player.mo.color = SKINCOLOR_YELLOW
					drillmusichandler(player)
					
					player.drilling = true
					player.wisptimer = 14*TICRATE
					player.powers[pw_invulnerability] = 14*TICRATE
					player.powers[pw_shield] = $1|SH_ELEMENTAL
				else
					S_StartSound(player.mo, sfx_wispno)
				end
			elseif (player.wisp1 == "burst")
			and not player.bursting
			and not player.exiting
			and player.wispuseon
				if not (player.mo.eflags & (MFE_UNDERWATER|MFE_GOOWATER)) or ((player.mo.eflags & (MFE_UNDERWATER|MFE_GOOWATER)) and P_PlayerTouchingSectorSpecial(player, 1, 3))
				and not (player.mo.tracer and player.mo.tracer.type == MT_TUBEWAYPOINT)
				and not (player.playerstate == PST_REBORN)
					S_StartSound(player.mo, sfx_burst1)
					S_StartSound(player.mo, sfx_wispon)
					if ((G_BuildMapName(gamemap) == "MAP03") or (G_BuildMapName(gamemap) == "MAP06") or (G_BuildMapName(gamemap) == "MAP09") or (G_BuildMapName(gamemap) == "MAP12") or (G_BuildMapName(gamemap) == "MAP24") or (G_BuildMapName(gamemap) == "MAP25"))
						S_ChangeMusic("DSBRST", true, player)
					else
						S_ChangeMusic("BRST3D", true, player)
					end
					
					player.bursting = true
					player.wisptimer = 14*TICRATE
					player.powers[pw_shield] = $1|SH_ELEMENTAL
					player.powers[pw_invulnerability] = 14*TICRATE
				else
					S_StartSound(player.mo, sfx_wispno)
				end
			elseif (player.wisp1 == "laser")
			and not player.laserphase1
			and not player.laserzoom
			and not player.exiting
			and player.wispuseon
				if not (player.mo.tracer and player.mo.tracer.type == MT_TUBEWAYPOINT)
				and not (player.playerstate == PST_REBORN)
					S_StartSound(player.mo, sfx_sclasr)
					S_StartSound(player.mo, sfx_lrbeep)
					S_StartSound(player.mo, sfx_wispon)
					S_StartSound(player.mo, sfx_rcrg)
					lasermusichandler(player)
					player.dz = 0 //reset value so the player can re-aim
					player.laserphase1 = true
					player.wisptimer = 14*TICRATE
					player.powers[pw_invulnerability] = 14*TICRATE
					player.pflags = $1 & ~(PF_JUMPED|PF_SPINNING)
				else
					S_StartSound(player.mo, sfx_wispno)
				end
			elseif (player.wisp1 == "laser")
			and player.laserphase1
			and not player.laserzoom
			and not player.exiting
			and player.wispuseon
				player.laserzoom = true
				player.laserphase1 = false
				S_StopSound(player.mo)
				S_StartSound(player.mo, sfx_lrzoom)
				S_StartSound(player.mo, sfx_lrchrg)
			end
			
			if not player.burstholdtime then player.burstholdtime = 0 end
			
			if ((player.wisptimer == 0) and (player.asteroidactive))
				player.asteroidactive = false
				player.wisptimer = 0
				player.powers[pw_shield] = $1 & ~SH_ATTRACT
				player.powers[pw_flashing] = 1*TICRATE
				player.mo.state = S_PLAY_FALL
				player.floattics = 0
				player.jumpfactor = skins[player.mo.skin].jumpfactor
				P_RestoreMusic(player)
				S_StartSound(player.mo, sfx_wispof)
				player.mo.color = player.skincolor
				player.wisp1 = "none"
			end
			if ((player.wisptimer == 0) and (player.bursting))
				player.bursting = false
				player.wisptimer = 0
				player.powers[pw_flashing] = 1*TICRATE
				player.powers[pw_shield] = $1 & ~SH_ELEMENTAL
				player.mo.state = S_PLAY_FALL
				player.wisp1 = "none"
				P_RestoreMusic(player)
				S_StartSound(player.mo, sfx_wispof)
				player.mo.color = player.skincolor
			end
			if player.asteroidactive
				player.mo.state = S_ASTEROID
				if not S_SoundPlaying(player.mo, sfx_asrbgs)
					S_StartSound(player.mo, sfx_asrbgs)
				end
				local checksector = R_PointInSubsector(player.mo.x, player.mo.y).sector
				AsteroidFofBreak(player, checksector)
				if player.mo.momz <= 0
					if player.pflags & PF_SPINDOWN
					and player.pflags & PF_JUMPED
						if not player.superwisp
							if player.floattics < floattimer
								player.mo.momz = 0
								player.floattics = $1 + 1
							elseif player.floattics == floattimer
								P_SetObjectMomZ(player.mo, -2*FRACUNIT, false)
								player.floattics = floattimer
							end
						else
							if player.floattics < floattimer*2
								player.mo.momz = 0
								player.floattics = $1 + 1
							elseif player.floattics == floattimer*2
								P_SetObjectMomZ(player.mo, -2*FRACUNIT, false)
								player.floattics = floattimer*2
							end
						end
					end
				elseif (player.mo.momz >= 0) and (player.mo.flags2 & MF2_OBJECTFLIP)
					if player.pflags & PF_SPINDOWN
					and player.pflags & PF_JUMPED
						if player.floattics < floattimer
							player.mo.momz = 0
							player.floattics = $1 + 1
						elseif player.floattics == floattimer
							P_SetObjectMomZ(player.mo, -2*FRACUNIT, false)
							player.floattics = floattimer
						end
					end
				end
				if P_IsObjectOnGround(player.mo) and player.mo.eflags & MFE_ONGROUND
					player.floattics = 0
				end
			end
			if player.rocketaim == nil
				player.rocketaim = false
			end
			if player.usemomz == nil
				player.usemomz = 0
			end
			if player.forwardmom == nil
				player.forwardmom = 0
			end
			if (player.rocketon)
				player.mo.momx = 0
				player.mo.momy = 0
				player.mo.state = S_ROCKETWISP
				player.pflags = $1 & ~(PF_JUMPED|PF_SPINNING)
				local checksector = R_PointInSubsector(player.mo.x, player.mo.y).sector
				WispFofBreak(player, checksector)
				
				player.momz = 0
				player.momx = 0
				player.momy = 0

				if player.pflags & PF_JUMPDOWN
					P_SetObjectMomZ(player.mo, 20*FRACUNIT, false)
				else
					P_SetObjectMomZ(player.mo, FixedMul(5*FRACUNIT, player.mo.scale), false)
				end
			end
			if player.wisptimer == 0
			and (player.rocketon)
				player.wisp1 = "none"
				player.rocketon = false
				player.mo.state = S_PLAY_SPRING
				player.superwisp = false
				player.canswitchwisps = false
				P_RestoreMusic(player)
			end
			if ((player.wisptimer == 0) or not (player.mo.eflags & (MFE_UNDERWATER|MFE_GOOWATER)))
			and (player.drilling)
				player.wisp1 = "none"
				player.drilling = false
				player.wisptimer = 0
				S_StartSound(player.mo, sfx_wispof)
				player.mo.state = S_PLAY_FALL
				player.powers[pw_shield] = $1 & ~SH_ELEMENTAL
				player.powers[pw_flashing] = TICRATE/3
				player.powers[pw_invulnerability] = 0
				player.mo.color = player.skincolor
				P_RestoreMusic(player)
				player.superwisp = false
				player.canswitchwisps = false
			end
			
			//end
			if ((player.wisptimer <= 0) or ((player.mo.eflags & (MFE_UNDERWATER|MFE_GOOWATER)) and not (P_PlayerTouchingSectorSpecial(player, 1, 3))))
			and (player.bursting)
				player.wisp1 = "none"
				player.bursting = false
				player.burstholdtime = 0
				player.wisptimer = 0
				S_StartSound(player.mo, sfx_wispof)
				player.mo.state = S_PLAY_FALL
				player.powers[pw_shield] = $1 & ~SH_ELEMENTAL
				player.powers[pw_flashing] = TICRATE/3
				player.powers[pw_invulnerability] = 0
				player.mo.color = player.skincolor
				P_RestoreMusic(player)
			end
			if player.bursting
			and P_PlayerTouchingSectorSpecial(player, 1, 3)
				player.wisptimer = 14*TICRATE
			end
			if (player.wisptimer <= 0)
			and (player.laserphase1 or player.laserzoom or player.laserzoomtube)
				player.wisp1 = "none"
				player.laserphase1 = false
				player.laserzoom = false
				player.laserzoomtube = false
				player.wisptimer = 0
				S_StartSound(player.mo, sfx_lrfin)
				player.mo.state = S_PLAY_FALL
				player.powers[pw_flashing] = TICRATE/3
				player.powers[pw_invulnerability] = 0
				player.mo.color = player.skincolor
				P_RestoreMusic(player)
				player.mo.flags = $1 & ~(MF_GRENADEBOUNCE)
			end
			if (player.playerstate & PST_REBORN)
			and (player.wisptimer ~= 0)
				player.wisptimer = 0
				player.powers[pw_invulnerability] = 0
				P_RestoreMusic(player)
			end
			if (player.wisptimer ~= 0)
			and player.exiting
				P_RestoreMusic(player)
				player.wisptimer = 0
				player.powers[pw_invulnerability] = 0
			end
			
			if player.canboost == nil
				player.canboost = false
			end
			if player.boostusetime == nil
				player.boostusetime = 0
			end
			if (player.speed < player.normalspeed/20)
			and (player.cmd.buttons & BT_CUSTOM1)
			and (player.boostmeter > 0)
				player.canboost = false
			elseif player.canboost == false
			and not (player.cmd.buttons & BT_CUSTOM1)
				player.canboost = true
			end
			
			if not player.superboost then player.superboost = false end
			
			if player.boostuseon
			and (player.boostmeter > 0)
			and (player.canboost == true)
			and not (player.airboost)
			and (player.mo.health > 0)
			and not (player.boosting)
			and not (player.exiting)
			and not (player.wisptimer)
				local boostsound = P_RandomRange(1, 3)
					if not (S_SoundPlaying(player.mo, sfx_bstfx1))
					and not (S_SoundPlaying(player.mo, sfx_bstfx2))
					and not (S_SoundPlaying(player.mo, sfx_bstfx3))
					and not (player.mo.eflags & MFE_UNDERWATER)
						if boostsound == 1
							S_StartSound(player.mo, sfx_bstfx1)
						elseif boostsound == 2
							S_StartSound(player.mo, sfx_bstfx2)
						elseif boostsound == 3
							S_StartSound(player.mo, sfx_bstfx3)
						end
					end
					if not player.powers[pw_super]
					and (player.boostmeter >= 5)
						player.boostmeter = $1 - 5
					end
					if not (P_IsObjectOnGround(player.mo))
					and player.powers[pw_super]
					and (wisps.allowsuperfly == true)
						player.superboost = true
					else
						player.superboost = false
					end
					if not (P_IsObjectOnGround(player.mo))
						player.mo.momz = 0
					end
					if not (player.mo.eflags & MFE_UNDERWATER)
						S_StartSound(player.mo, sfx_bsttap)
					else
						S_StartSound(player.mo, sfx_bswtr1)
					end
				player.pflags = $1 & ~PF_SPINNING
				player.boosting = true
				P_InstaThrust(player.mo, player.mo.angle, FixedMul(skins[player.mo.skin].normalspeed + (skins[player.mo.skin].normalspeed/3), player.mo.scale))
				if not (player.powers[pw_super])
				and not P_IsObjectOnGround(player.mo)
					player.airboost = true
				end
			end
			
			if not player.laserzoom then player.laserzoom = false end
			if not player.laserspeed then player.laserspeed = 128 end
			if not player.dz then player.dz = 0 end
			if not player.spinframe then player.spinframe = 0 end
			if (player.spinframe > 3) then player.spinframe = 0 end
			if player.laserphase1
				player.spinframe = $1 + 1
				if player.mo.state ~= S_PLAY_ROLL
					player.mo.state = S_PLAY_ROLL
				end
				if not (S_SoundPlaying(player.mo, sfx_lrbeep))
					S_StartSound(player.mo, sfx_lrbeep)
				end
				if not ((S_SoundPlaying(player.mo, sfx_rcrg)) and not (S_SoundPlaying(player.mo, sfx_rcrglp)))
					S_StartSound(player.mo, sfx_rcrglp)
				end
				if player.powers[pw_underwater]
					player.powers[pw_underwater] = $1 + 1
				end
				if player.powers[pw_spacetime]
					player.powers[pw_spacetime] = $1 + 1
				end
				player.mo.momx = 0
				player.mo.momy = 0
				player.mo.momz = 0
				player.jumpfactor = 0
				player.charability2 = CA2_NONE
				if not (player.mo.flags2 & MF2_OBJECTFLIP)
				local trail1 = P_SpawnMobj(player.mo.x + FixedMul(FixedMul((player.laserspeed)*FRACUNIT, player.mo.scale), cos(player.mo.angle)), player.mo.y + FixedMul(FixedMul((player.laserspeed)*FRACUNIT, player.mo.scale), sin(player.mo.angle)), player.mo.z + FixedMul(player.dz*FRACUNIT, player.mo.scale), MT_THOK)
				trail1.scale = player.mo.scale/2
				trail1.color = player.mo.color
				trail1.tics = 1
				local trail2 = P_SpawnMobj(player.mo.x + FixedMul(FixedMul((player.laserspeed*2)*FRACUNIT, player.mo.scale), cos(player.mo.angle)), player.mo.y + FixedMul(FixedMul((player.laserspeed*2)*FRACUNIT, player.mo.scale), sin(player.mo.angle)), player.mo.z + FixedMul((player.dz*2)*FRACUNIT, player.mo.scale), MT_THOK)
				trail2.scale = player.mo.scale/2
				trail2.color = player.mo.color
				trail2.tics = 1
				local trail3 = P_SpawnMobj(player.mo.x + FixedMul(FixedMul((player.laserspeed*3)*FRACUNIT, player.mo.scale), cos(player.mo.angle)), player.mo.y + FixedMul(FixedMul((player.laserspeed*3)*FRACUNIT, player.mo.scale), sin(player.mo.angle)), player.mo.z + FixedMul((player.dz*3)*FRACUNIT, player.mo.scale), MT_THOK)
				trail3.scale = player.mo.scale/2
				trail3.color = player.mo.color
				trail3.tics = 1
				else
				local trail1 = P_SpawnMobj(player.mo.x + FixedMul(FixedMul((player.laserspeed)*FRACUNIT, player.mo.scale), cos(player.mo.angle)), player.mo.y + FixedMul(FixedMul((player.laserspeed)*FRACUNIT, player.mo.scale), sin(player.mo.angle)), -(player.mo.z + FixedMul(player.dz*FRACUNIT, player.mo.scale)), MT_THOK)
				trail1.scale = player.mo.scale/2
				trail1.color = player.mo.color
				trail1.tics = 1
				local trail2 = P_SpawnMobj(player.mo.x + FixedMul(FixedMul((player.laserspeed*2)*FRACUNIT, player.mo.scale), cos(player.mo.angle)), player.mo.y + FixedMul(FixedMul((player.laserspeed*2)*FRACUNIT, player.mo.scale), sin(player.mo.angle)), -(player.mo.z + FixedMul((player.dz*2)*FRACUNIT, player.mo.scale)), MT_THOK)
				trail2.scale = player.mo.scale/2
				trail2.color = player.mo.color
				trail2.tics = 1
				local trail3 = P_SpawnMobj(player.mo.x + FixedMul(FixedMul((player.laserspeed*3)*FRACUNIT, player.mo.scale), cos(player.mo.angle)), player.mo.y + FixedMul(FixedMul((player.laserspeed*3)*FRACUNIT, player.mo.scale), sin(player.mo.angle)), -(player.mo.z + FixedMul((player.dz*3)*FRACUNIT, player.mo.scale)), MT_THOK)
				trail3.scale = player.mo.scale/2
				trail3.color = player.mo.color
				trail3.tics = 1
				end
				//if not axes
				//and not (player.mo.flags2 & MF2_TWOD)
					if (player.cmd.buttons & BT_JUMP)
						player.dz = $1 + 1
					end
					if (player.cmd.buttons & BT_SPIN)
						player.dz = $1 - 1
					end
				/*else
					if (player.pflags & PF_ANALOGMODE)
						if (player.cmd.forwardmove > 0)
							player.dz = $1 + (player.cmd.forwardmove/2)
						end
						if (player.cmd.forwardmove < 0)
							player.dz = $1 - (player.cmd.forwardmove/2)
						end
					else
						if (player.cmd.forwardmove > 0)
							player.dz = $1 + 1
						end
						if (player.cmd.forwardmove < 0)
							player.dz = $1 - 1
						end
					end
				end*/
			end
			if (player.dz > 90)
				player.dz = 90
			end
			if (player.dz < -90)
				player.dz = -90
			end
			if (player.dz >= 31 and player.dz <= 89)
				player.laserspeed = 128/2
			end
			if (player.dz >= -30 and player.dz <= 30)
				player.laserspeed = 128
			end
			if (player.dz >= -89 and player.dz <= -31)
				player.laserspeed = 128/2
			end
			if (player.dz == 90)
				player.laserspeed = 0
			end
			if (player.dz == -90)
				player.laserspeed = 0
			end
			if not player.laserzoomtube then player.laserzoomtube = false end
			if player.laserzoomtube
				if not S_SoundPlaying(player.mo, sfx_lsrpip)
					S_StartSound(player.mo, sfx_lsrpip)
				end
				player.powers[pw_invulnerability] = $1 + 1
				if player.powers[pw_underwater]
					player.powers[pw_underwater] = $1 + 1
				end
				if player.powers[pw_spacetime]
					player.powers[pw_spacetime] = $1 + 1
				end
				if not (player.mo.tracer and player.mo.tracer.type == MT_TUBEWAYPOINT)
					player.wisptimer = 0
				end
				P_SpawnGhostMobj(player.mo)
				P_SpawnThokMobj(player)
			end
			if player.laserzoom
				P_SpawnGhostMobj(player.mo)
				P_SpawnThokMobj(player)
				player.spinframe = $1 + 1
				if player.mo.state ~= S_PLAY_ROLL
					player.mo.state = S_PLAY_ROLL
				end
				player.charability2 = CA2_SPINDASH
				player.pflags = $1 & ~(PF_JUMPED|PF_SPINNING)
				if not (player.mo.tracer and player.mo.tracer.type == MT_TUBEWAYPOINT)
					player.wisptimer = $1 - 14
				end
				if (player.wisptimer < 0)
					player.wisptimer = 0
				end
				if not (player.mo.tracer and player.mo.tracer.type == MT_TUBEWAYPOINT)
					player.powers[pw_invulnerability] = $1 - 13
				end
				if player.laserspeed
					P_InstaThrust(player.mo, player.mo.angle, FixedMul(player.laserspeed*FRACUNIT, player.mo.scale))
				else
					player.mo.momx = 0
					player.mo.momy = 0
				end
				local nextsec = R_PointInSubsector(player.mo.x + FixedMul(cos(player.mo.angle)*35, player.mo.scale), player.mo.y + FixedMul(sin(player.mo.angle)*35, player.mo.scale)).sector
				if (nextsec.floorheight > player.mo.z + player.mo.height) or (nextsec.ceilingheight <= player.mo.z)
					P_BounceMove(player.mo)
					S_StartSound(player.mo, sfx_s3k5d)
					player.mo.angle = R_PointToAngle2(0, 0, player.mo.momx, player.mo.momy)
					P_InstaThrust(player.mo, player.mo.angle, FixedMul(player.laserspeed*FRACUNIT, player.mo.scale))
				end
				if (player.mo.z + player.mo.height >= player.mo.ceilingz)
					S_StartSound(player.mo, sfx_s3k5d)
					player.dz = -player.dz
				end
				if (player.mo.z <= player.mo.floorz)
					S_StartSound(player.mo, sfx_s3k5d)
					player.dz = -player.dz
				end
				player.mo.angle = R_PointToAngle2(0, 0, player.mo.momx, player.mo.momy)
				if (player.dz)
					P_SetObjectMomZ(player.mo, (player.dz*FRACUNIT)*2, false)
				else
					player.mo.momz = 0
				end
				if (player.mo.tracer and player.mo.tracer.type == MT_TUBEWAYPOINT)
					player.laserzoomtube = true
					player.laserzoom = false
				end
				player.mo.flags = $1|MF_GRENADEBOUNCE
			end
			if not player.superflyz then player.superflyz = 0 end
			if (player.cmd.buttons & BT_CUSTOM1)
			and (player.boostmeter > 0)
			and not (player.exiting)
			and not (player.wisptimer)
			and (player.boosting == true)
			and (player.canboost == true)
				player.charflags = $1|SF_RUNONWATER
				player.powers[pw_invulnerability] = 2	
				player.boostusetime = $1+1
				if (player.boostusetime >= 4)
				and (player.boostmeter >= 0)
					if not player.powers[pw_super]
						player.boostmeter = $1 - 1
					else
						if (player.bot == BOT_NONE)
							if (wisps.ringdrain == true)
								P_GivePlayerRings(player, -1) //super boost now drains rings instead of the boost bar... except for bots... also provided ring draining is enabled
							end
						end
					end
					player.boostusetime = 0
				end
				local afterimage = P_SpawnGhostMobj(player.mo)
				if (player.airboost)
					afterimage.sprite = SPR_SFLY
					afterimage.frame = A
					afterimage.tics = 3
				end
				afterimage.fuse = 3
				if (player.superboost)
				and (player.powers[pw_super]) //MAKE SURE YOU'RE SUPER FIRST!
					P_InstaThrust(player.mo, player.mo.angle, FixedMul(skins[player.mo.skin].normalspeed + (skins[player.mo.skin].normalspeed/3), player.mo.scale))
					player.pflags = $1 & ~(PF_JUMPED)
					if not (player.mo.state == S_PLAY_FALL)
						player.mo.state = S_PLAY_FALL
					end
					if (player.cmd.buttons & BT_JUMP)
						player.superflyz = $1 + FRACUNIT/2
					end
					if (player.cmd.buttons & BT_SPIN)
						player.superflyz = $1 - FRACUNIT/2
					end
					if not ((player.cmd.buttons & BT_JUMP) or (player.cmd.buttons & BT_SPIN))
						if (player.superflyz < 0)
							player.superflyz = $1 + FRACUNIT/4
						end
						if (player.superflyz > 0)
							player.superflyz = $1 - FRACUNIT/4
						end
					end
					if player.superflyz > 28*FRACUNIT
						player.superflyz = 28*FRACUNIT
					end
					if player.superflyz < -28*FRACUNIT
						player.superflyz = -28*FRACUNIT
					end
					P_SetObjectMomZ(player.mo, player.superflyz, false)
				end
				if P_IsObjectOnGround(player.mo)
				and (player.speed >= player.runspeed)
					player.pflags = $1 & ~(PF_SPINNING)
					if not (player.mo.state == S_PLAY_DASH)
						player.mo.state = S_PLAY_DASH
					end
				end
				player.normalspeed = skins[player.mo.skin].normalspeed + (skins[player.mo.skin].normalspeed/3)
				player.acceleration = skins[player.mo.skin].acceleration + (skins[player.mo.skin].acceleration/3)
				player.jumpfactor = skins[player.mo.skin].jumpfactor + (skins[player.mo.skin].jumpfactor/3)
				if not (player.boostmobj and player.boostmobj.valid) then
					player.boostmobj = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_GFZFLOWER1)
					player.boostmobj.flags2 = $1|MF2_DONTDRAW
					if not (player.mo.eflags & MFE_UNDERWATER)
						S_StartSound(player.boostmobj, sfx_bsthld)
					else
						S_StartSound(player.boostmobj, sfx_bswtr2)
					end
					P_SetOrigin(player.boostmobj, player.mo.x, player.mo.y, player.mo.z)
				elseif (player.boostmobj and player.boostmobj.valid)
					if not (player.mo.eflags & MFE_UNDERWATER)
						if not (S_SoundPlaying(player.boostmobj, sfx_bstwnd))
							S_StartSound(player.boostmobj, sfx_bstwnd)
						end
					else
						if not (S_SoundPlaying(player.boostmobj, sfx_bswtrw))
							S_StartSound(player.boostmobj, sfx_bswtrw)
						end
					end
					P_SetOrigin(player.boostmobj, player.mo.x, player.mo.y, player.mo.z)
				end
			else
				if (player.boostmobj and player.boostmobj.valid)
					S_StopSound(player.boostmobj)
					P_RemoveMobj(player.boostmobj)
				end
				player.boosting = false
				player.charflags = $1 & ~SF_RUNONWATER
				player.normalspeed = skins[player.mo.skin].normalspeed
				player.acceleration = skins[player.mo.skin].acceleration
				if not player.asteroidactive
					player.jumpfactor = skins[player.mo.skin].jumpfactor
				end
				player.superflyz = 0
			end
				
			if (player.prevrings < player.rings)
				player.boostmeter = $1 + (player.rings - player.prevrings)
			end
			if player.powers[pw_super]
				player.boostmeter = 105
			end
			if (player.cmd.buttons & BT_CUSTOM1)
			and (player.boostmeter > 0)
			and (player.canboost == true)
			and (player.boosting)
			and not (player.wisptimer)
			and not (player.exiting)
			and P_IsObjectOnGround(player.mo)
				if (player.mo.eflags & MFE_UNDERWATER)
					for i=1,8 do
						local bubble = P_SpawnMobj(player.mo.x, player.mo.y,
							player.mo.z + ((player.mo.eflags & MFE_VERTICALFLIP)/MFE_VERTICALFLIP * (player.mo.height - mobjinfo[MT_SMALLBUBBLE].height)),
							MT_SMALLBUBBLE)
							
						bubble.tics = 8
						bubble.eflags = $1 | (player.mo.eflags & MFE_VERTICALFLIP)
						bubble.scale = player.mo.scale >> 1
						bubble.destscale = player.mo.scale << 1
						bubble.fuse = 10
						bubble.scalespeed = FixedMul(bubble.scalespeed, player.mo.scale) -- scale the scaling speed!
						P_SetObjectMomZ(bubble, player.speed/30+P_RandomByte()<<10, false)
						
						P_InstaThrust(bubble, player.mo.angle+(P_RandomRange(-15, 15)*ANG1), -player.speed/9)
						P_TryMove(bubble, bubble.x+bubble.momx, bubble.y+bubble.momy, true)
					end
				elseif not (player.mo.eflags & MFE_UNDERWATER) and not (player.mo.eflags & MFE_GOOWATER)
					for i=1,8 do
						local particle = P_SpawnMobj(player.mo.x, player.mo.y,
							player.mo.z + ((player.mo.eflags & MFE_VERTICALFLIP)/MFE_VERTICALFLIP * (player.mo.height - mobjinfo[MT_PARTICLE].height)),
							MT_PARTICLE)
						
						particle.tics = 8
						particle.eflags = $1 | (player.mo.eflags & MFE_VERTICALFLIP)
						particle.scale = player.mo.scale >> 1
						particle.destscale = player.mo.scale << 3
						particle.scalespeed = FixedMul(particle.scalespeed, player.mo.scale) -- scale the scaling speed!
						P_SetObjectMomZ(particle, player.speed/30+P_RandomByte()<<10, false)
						
						P_InstaThrust(particle, player.mo.angle+(P_RandomRange(-15, 15)*ANG1), -player.speed/9)
						P_TryMove(particle, particle.x+particle.momx, particle.y+particle.momy, true)
					end
				end
			end
		end
		
		if player.idletime == nil
		or (player.cmd.buttons & BT_JUMP)
		or (player.cmd.buttons & BT_SPIN)
		or (player.cmd.buttons & BT_ATTACK)
		or (player.cmd.buttons & BT_FIRENORMAL)
		or (player.cmd.buttons & BT_TOSSFLAG)
		or (player.cmd.buttons & BT_WEAPONMASK)
		or (player.cmd.buttons & BT_WEAPONNEXT)
		or (player.cmd.buttons & BT_WEAPONPREV)
		or (player.cmd.buttons & BT_CUSTOM1)
		or (player.cmd.buttons & BT_CUSTOM2)
		or (player.cmd.buttons & BT_CUSTOM3)
		or player.cmd.forwardmove
		or player.cmd.sidemove then
			player.idletime = 0
		else
			player.idletime = $1 + 1
		end
		
		if player.airboost
			if not P_IsObjectOnGround(player.mo)
			and not (player.wisptimer)
			and not (player.powers[pw_super])
				player.mo.sprite = SPR_SFLY
				player.mo.frame = A
				player.mo.tics = 1
				player.pflags = $ & ~(PF_JUMPED|PF_SPINNING)
			else
				player.airboost = false
			end
			if player.boosting
			and not (player.cmd.buttons & BT_CUSTOM1)
				player.boosting = false
			end
		end
		
		player.prevrings = player.rings
	end
end)

addHook("MapLoad", do
for player in players.iterate
	player.wisptimer = 0
	player.boostmeter = 105
end
end)

addHook("MapChange", do
for player in players.iterate
	player.wisptimer = 0
	player.boostmeter = 105
end
end)

addHook("ThinkFrame", do
	for player in players.iterate
		if player.mo and not player.spectator
		and not player.exiting
		and not (player.powers[pw_carry] == CR_NIGHTSMODE)	
			if (player.mo.debrisangle == nil) then player.mo.debrisangle = 0 end
			if (player.mo.debrisangle >= 360*FRACUNIT) then player.mo.debrisangle = $1 - (360*FRACUNIT) end
			if (player.mo.debrisangle < 360*FRACUNIT)
				player.mo.debrisangle = $1 + 12*FRACUNIT
			end
			
			if (player.asteroidactive)
				local d1 = P_SpawnMobj(player.mo.x + cos(FixedAngle(player.mo.debrisangle))*100, player.mo.y + sin(FixedAngle(player.mo.debrisangle))*100, player.mo.z + player.mo.height/2, MT_ASTEROIDDEBRIS)
				local d2 = P_SpawnMobj(player.mo.x + cos(FixedAngle(player.mo.debrisangle))*-100, player.mo.y + sin(FixedAngle(player.mo.debrisangle))*-100, player.mo.z + player.mo.height/2, MT_ASTEROIDDEBRIS)
				if d1 ~= nil
					d1.fuse = 20
					P_SetOrigin(d1, player.mo.x + cos(FixedAngle(player.mo.debrisangle))*100, player.mo.y + sin(FixedAngle(player.mo.debrisangle))*100, player.mo.z + player.mo.height/2)
				end
				if d2 ~= nil
					d2.fuse = 20
					P_SetOrigin(d2, player.mo.x + cos(FixedAngle(player.mo.debrisangle))*-100, player.mo.y + sin(FixedAngle(player.mo.debrisangle))*-100, player.mo.z + player.mo.height/2)
				end
				local checksectord1 = R_PointInSubsector(d1.x, d1.y).sector
				local checksectord2 = R_PointInSubsector(d2.x, d2.y).sector
				DebrisFofBreak(d1, checksectord1)
				DebrisFofBreak(d2, checksectord2)
			end
			if (player.rocketon)
			and not (player.mo.eflags & (MFE_UNDERWATER|MFE_GOOWATER))
				local boom1 = P_SpawnMobj(player.mo.x + FixedMul(5*FRACUNIT, player.mo.scale), player.mo.y + FixedMul(5*FRACUNIT, player.mo.scale), player.mo.z - FixedMul(5*FRACUNIT, player.mo.scale), MT_EXPLODE)
				if (boom1 and boom1.valid)
					boom1.scale = player.mo.scale
				end
				local boom2 = P_SpawnMobj(player.mo.x - FixedMul(5*FRACUNIT, player.mo.scale), player.mo.y + FixedMul(5*FRACUNIT, player.mo.scale), player.mo.z - FixedMul(5*FRACUNIT, player.mo.scale), MT_EXPLODE)
				if (boom1 and boom1.valid)
					boom1.scale = player.mo.scale
				end
				local boom3 = P_SpawnMobj(player.mo.x - FixedMul(5*FRACUNIT, player.mo.scale), player.mo.y - FixedMul(5*FRACUNIT, player.mo.scale), player.mo.z - FixedMul(5*FRACUNIT, player.mo.scale), MT_EXPLODE)
				if (boom1 and boom1.valid)
					boom1.scale = player.mo.scale
				end
				local boom4 = P_SpawnMobj(player.mo.x + FixedMul(5*FRACUNIT, player.mo.scale), player.mo.y - FixedMul(5*FRACUNIT, player.mo.scale), player.mo.z - FixedMul(5*FRACUNIT, player.mo.scale), MT_EXPLODE)
				if (boom1 and boom1.valid)
					boom1.scale = player.mo.scale
				end
			end
			if (player.rocketon)
			and (player.mo.eflags & (MFE_UNDERWATER) or player.mo.eflags & (MFE_GOOWATER))
				local boom1 = P_SpawnMobj(player.mo.x + FixedMul(5*FRACUNIT, player.mo.scale), player.mo.y + FixedMul(5*FRACUNIT, player.mo.scale), player.mo.z - FixedMul(5*FRACUNIT, player.mo.scale), MT_UWEXPLODE)
				local boom2 = P_SpawnMobj(player.mo.x - FixedMul(5*FRACUNIT, player.mo.scale), player.mo.y + FixedMul(5*FRACUNIT, player.mo.scale), player.mo.z - FixedMul(5*FRACUNIT, player.mo.scale), MT_UWEXPLODE)
				local boom3 = P_SpawnMobj(player.mo.x - FixedMul(5*FRACUNIT, player.mo.scale), player.mo.y - FixedMul(5*FRACUNIT, player.mo.scale), player.mo.z - FixedMul(5*FRACUNIT, player.mo.scale), MT_UWEXPLODE)
				local boom4 = P_SpawnMobj(player.mo.x + FixedMul(5*FRACUNIT, player.mo.scale), player.mo.y - FixedMul(5*FRACUNIT, player.mo.scale), player.mo.z - FixedMul(5*FRACUNIT, player.mo.scale), MT_UWEXPLODE)
			end
			if player.bursting
				player.mo.state = S_BURSTWISP
				if not (player.burstmobj and player.burstmobj.valid)
					player.burstmobj = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_BURSTEFFECT1)
				elseif (player.burstmobj and player.burstmobj.valid)
					P_SetOrigin(player.burstmobj, player.mo.x, player.mo.y, player.mo.z)
				end
				player.jumpfactor = 0
				if (player.cmd.buttons & BT_JUMP)
					if (player.mo.momz*P_MobjFlip(player.mo) <= 0)
					and (not P_IsObjectOnGround(player.mo))
						P_SetObjectMomZ(player.mo, -3*FRACUNIT, false)
					end
					player.burstholdtime = $1 + 1
					local burstthokmobj = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_THOK)
					burstthokmobj.scale = player.mo.scale
					if ((player.burstholdtime >= 1) and (player.burstholdtime <=9))
						burstthokmobj.color = SKINCOLOR_WHITE
					elseif ((player.burstholdtime >= 10) and (player.burstholdtime <=19))
						burstthokmobj.color = SKINCOLOR_RED
					else
						burstthokmobj.color = SKINCOLOR_CRIMSON
					end
					if player.burstholdtime == 1
					or player.burstholdtime == 11
					or player.burstholdtime == 20
						S_StartSound(player.mo, sfx_spndsh)
					end
					burstthokmobj.tics = 1
				else
					if player.burstholdtime
						if ((player.burstholdtime >= 0) and (player.burstholdtime <=9))
							P_SetObjectMomZ(player.mo, 10*FRACUNIT, false)
							S_StartSound(player.mo, sfx_jump)
						elseif ((player.burstholdtime >= 10) and (player.burstholdtime <=19))
							P_SetObjectMomZ(player.mo, 20*FRACUNIT, false)
							S_StartSound(player.mo, sfx_jump)
						elseif ((player.burstholdtime >= 20))
							P_NukeEnemies(player.mo, player.mo, 1536*FRACUNIT)
							P_StartQuake(10*FRACUNIT, 10)
							S_StartSound(player.mo, sfx_bkpoof)
							P_FlashPal(player, PAL_NUKE, 5)
							P_SetObjectMomZ(player.mo, 40*FRACUNIT, false)
						end
						player.burstholdtime = 0
					end
				end
					
			else
				if (player.burstmobj and player.burstmobj.valid)
					P_RemoveMobj(player.burstmobj)
				end
			end
		end
	end
end)

addHook("TouchSpecial", function(special, toucher)
	local player = toucher.player
	P_SpawnMobj(special.x, special.y, special.z, MT_LASERPLACEHOLDER)
	if (player.wisp1 ~= "laser")
	and not (player.emeralds or player.powers[pw_super])
		if not (player.wisptimer)
			player.wisp1 = "laser"
			P_KillMobj(special)
			S_StartSound(player.mo, sfx_boxopn)
		else
			player.wisp2 = "laser"
		end
	end
	if ((player.wisptimer) and (player.wisp1 ~= "laser"))
	or (player.emeralds or player.powers[pw_super])
		player.wisp2 = "laser"
	end
	if ((player.wisptimer) and (player.wisp1 == "laser"))
		player.wisptimer = 14*TICRATE
	end
	S_StartSound(player.mo, sfx_boxopn)
	player.wispgetdelay = 36
	if (special and special.valid) then P_RemoveMobj(special) end
	return true
end, MT_LASERBOX)

addHook("TouchSpecial", function(special, toucher)
	local player = toucher.player
	P_SpawnMobj(special.x, special.y, special.z, MT_ASTEROIDPLACEHOLDER)
	if (player.wisp1 ~= "asteroid")
	and not (player.emeralds or player.powers[pw_super])
		if not (player.wisptimer)
			player.wisp1 = "asteroid"
			P_KillMobj(special)
			S_StartSound(player.mo, sfx_boxopn)
		else
			player.wisp2 = "asteroid"
		end
	end
	if ((player.wisptimer) and (player.wisp1 ~= "asteroid"))
	or (player.emeralds or player.powers[pw_super])
		player.wisp2 = "asteroid"
	end
	if ((player.wisptimer) and (player.wisp1 == "asteroid"))
		player.wisptimer = 14*TICRATE
	end
	S_StartSound(player.mo, sfx_boxopn)
	player.wispgetdelay = 36
	if (special and special.valid) then P_RemoveMobj(special) end return true
end, MT_ASTEROIDBOX)

addHook("TouchSpecial", function(special, toucher)
	local player = toucher.player
	P_SpawnMobj(special.x, special.y, special.z, MT_BOOSTPLACEHOLDER)
	player.boostmeter = $1 + 20
	if player.powers[pw_super]
	and (toucher.skin == "sonic")
		P_GivePlayerRings(player, 5)
	end
	player.wispgetdelay = 36
	S_StartSound(player.mo, sfx_boxopn)
	if (special and special.valid) then P_RemoveMobj(special) end
	return true
end, MT_BOOSTBOX)



addHook("TouchSpecial", function(special, toucher)
	local player = toucher.player
	P_SpawnMobj(special.x, special.y, special.z, MT_DRILLPLACEHOLDER)
	if (player.wisp1 ~= "drill")
	and not (player.emeralds or player.powers[pw_super])
		if not (player.wisptimer)
			player.wisp1 = "drill"
			P_KillMobj(special)
			S_StartSound(player.mo, sfx_boxopn)
		else
			player.wisp2 = "drill"
		end
	end
	if ((player.wisptimer) and (player.wisp1 ~= "drill"))
	or (player.emeralds or player.powers[pw_super])
		player.wisp2 = "drill"
	end
	if ((player.wisptimer) and (player.wisp1 == "drill"))
		player.wisptimer = 14*TICRATE
	end
	S_StartSound(player.mo, sfx_boxopn)
	player.wispgetdelay = 36
	if (special and special.valid) then P_RemoveMobj(special) end
	return true
end, MT_DRILLBOX)

addHook("TouchSpecial", function(special, toucher)
	local player = toucher.player
	P_SpawnMobj(special.x, special.y, special.z, MT_ROCKETPLACEHOLDER)
	if (player.wisp1 ~= "rocket")
	and not (player.emeralds or player.powers[pw_super])
		if not (player.wisptimer)
			player.wisp1 = "rocket"
			P_KillMobj(special)
			S_StartSound(player.mo, sfx_boxopn)
		else
			player.wisp2 = "rocket"
		end
	end
	if ((player.wisptimer) and (player.wisp1 ~= "rocket"))
	or (player.emeralds or player.powers[pw_super])
		player.wisp2 = "rocket"
	end
	if ((player.wisptimer) and (player.wisp1 == "rocket"))
		player.wisptimer = 3*TICRATE
	end
	S_StartSound(player.mo, sfx_boxopn)
	player.wispgetdelay = 36
	if (special and special.valid) then P_RemoveMobj(special) end
	return true
end, MT_ROCKETBOX)

addHook("TouchSpecial", function(special, toucher)
	local player = toucher.player
	P_SpawnMobj(special.x, special.y, special.z, MT_BURSTPLACEHOLDER)
	if (player.wisp1 ~= "burst")
	and not (player.emeralds or player.powers[pw_super])
		if not (player.wisptimer)
			player.wisp1 = "burst"
			P_KillMobj(special)
			S_StartSound(player.mo, sfx_boxopn)
		else
			player.wisp2 = "burst"
		end
	end
	if ((player.wisptimer) and (player.wisp1 ~= "burst"))
	or (player.emeralds or player.powers[pw_super])
		player.wisp2 = "burst"
	end
	if ((player.wisptimer) and (player.wisp1 == "burst"))
		player.wisptimer = 14*TICRATE
	end
	S_StartSound(player.mo, sfx_boxopn)
	player.wispgetdelay = 36
	if (special and special.valid) then P_RemoveMobj(special) end
	return true
end, MT_BURSTBOX)

addHook("ThinkFrame", do
	for player in players.iterate
		if (player.drilling)
			player.pflags = $1 & ~(PF_JUMPED|PF_SPINNING)
			P_InstaThrust(player.mo, player.mo.angle, FixedMul(30*FRACUNIT, player.mo.scale))
			if (player.pflags & PF_JUMPDOWN)
			and not (player.pflags & PF_SPINDOWN)
				P_SetObjectMomZ(player.mo, 15*FRACUNIT, false)
			end
			if (player.pflags & PF_SPINDOWN)
			and not (player.pflags & PF_JUMPDOWN)
				P_SetObjectMomZ(player.mo, -15*FRACUNIT, false)
			end
			if (player.pflags & PF_SPINDOWN)
			and (player.pflags & PF_JUMPDOWN)
				player.mo.momz = 0
				player.mo.state = S_PLAY_FALL
			end
			if not (player.pflags & PF_JUMPDOWN)
			and not (player.pflags & PF_SPINDOWN)
				player.mo.momz = 0
				player.mo.state = S_PLAY_FALL
			end
			if (player.pflags & PF_JUMPDOWN)
			and not (player.pflags & PF_SPINDOWN)
				player.mo.state = S_PLAY_SPRING
			end
			if (player.pflags & PF_SPINDOWN)
			and not (player.pflags & PF_JUMPDOWN)
				player.mo.state = S_PLAY_FALL
			end
		end
	end
end)

//Debuging functions
/*hud.add(function(v, player)
	if player.asteroidactive
		v.drawString(160, 16, "Value of player.mo.debrisangle:"..player.mo.debrisangle/FRACUNIT, V_MONOSPACE|V_ALLOWLOWERCASE, "center")
	end
end, "game")*/

addHook("ThinkFrame", do
	for player in players.iterate
		if (player.endless == nil) then player.endless = false end
		if not player.mo then continue end
		if player.endless
			if (player.rings > 0)
				player.rings = 0
			end
			player.boostmeter = 105
		end
	end
end)

COM_AddCommand("wisp_endlessboost", function(player)
	if not player.endless
		player.endless = true
		CONS_Printf(player, "Endless Boost enabled")
	else
		player.endless = false
		CONS_Printf(player, "Endless Boost disabled")
	end
end)

COM_AddCommand("wisp_ringdrain", function(player)
	if not (player == server)
	and not IsPlayerAdmin(player)
		CONS_Printf(player, "You must be a server or game administrator to use this!")
		return
	end
	if not wisps.ringdrain
		wisps.ringdrain = true
		print(player.name.." enabled the Ring Drain from boosting whilst in super form")
	else
		wisps.ringdrain = false
		print(player.name.." disabled the Ring Drain from boosting whilst in super form")
	end
end)

COM_AddCommand("wisp_allowsuperfly", function(player)
	if not (player == server)
	and not IsPlayerAdmin(player)
		CONS_Printf(player, "You must be a server or game administrator to use this!")
		return
	end
	if not wisps.allowsuperfly
		wisps.allowsuperfly = true
		print(player.name.." enabled Super Sonic's Super Fly ability")
	else
		wisps.allowsuperfly = false
		for everyone in players.iterate
			everyone.superboost = false
		end
		print(player.name.." disabled Super Sonic's Super Fly ability")
	end
end)

COM_AddCommand("wisp_cheats", function(player)
	if not (player == server)
	and not IsPlayerAdmin(player)
		CONS_Printf(player, "You must be a server or game administrator to use this!")
		return
	end
	if not wisps.cheats
		wisps.cheats = true
		print(player.name.." turned on Cheat mode. Now you can use cheats!")
	else
		wisps.cheats = false
		print(player.name.." turned off Cheat mode. You can no longer use cheats")
	end
end)
local wisplist = {
laser = {
	wisp = "laser",
	timer = 14*TICRATE
	},
drill = {
	wisp = "drill",
	timer = 14*TICRATE
	},
rocket = {
	wisp = "rocket",
	timer = 3*TICRATE
	},
burst = {
	wisp = "burst",
	timer = 14*TICRATE
	},
asteroid = {
	wisp = "asteroid",
	timer = 14*TICRATE
	}
}
COM_AddCommand("wisp_grantwisp", function(player, arg1)
	if not (wisps.cheats)
		CONS_Printf(player, "Cheats must be enabled to use this!")
		return
	end
	if not arg1 then
		CONS_Printf(player, "wisps_grantwisp <wisp>: give yourself a wisp. Calling this on a wisp that you're currently transformed as refills the timer.")
		CONS_Printf(player, "Valid wisps: laser, drill, rocket, burst, asteroid")
		return
	end
	if not wisplist[arg1]
		CONS_Printf(player, "That wisp can't be found. Try again")
		return
	end
	if (player.wisp1 ~= wisplist[arg1].wisp)
	and not (player.emeralds or player.powers[pw_super])
		if not (player.wisptimer)
			player.wisp1 = wisplist[arg1].wisp
			S_StartSound(player.mo, sfx_wspget)
			CONS_Printf(player, "Granted wisp: "..wisplist[arg1].wisp..", Slot: 1")
		else
			player.wisp2 = wisplist[arg1].wisp
			S_StartSound(player.mo, sfx_wspget)
			CONS_Printf(player, "Granted wisp: "..wisplist[arg1].wisp..", Slot: 2")
		end
		return
	elseif (player.wisp1 == wisplist[arg1].wisp)
	and not (player.emeralds or player.powers[pw_super])
	and not player.wisptimer
		S_StartSound(player.mo, sfx_wspget)
		CONS_Printf(player, "Granted wisp: "..wisplist[arg1].wisp..", Slot: 1")
		return
	end
	if ((player.wisptimer) and (player.wisp1 ~= wisplist[arg1].wisp))
	or (player.emeralds or player.powers[pw_super])
		player.wisp2 = wisplist[arg1].wisp
		S_StartSound(player.mo, sfx_wspget)
		CONS_Printf(player, "Granted wisp: "..wisplist[arg1].wisp..", Slot: 2")
		return
	end
	if ((player.wisptimer) and (player.wisp1 == wisplist[arg1].wisp))
		player.wisptimer = wisplist[arg1].timer
		S_StartSound(player.mo, sfx_wspget)
		CONS_Printf(player, "Granted wisp: "..wisplist[arg1].wisp..", Timer refilled")
		return
	end
end)

COM_AddCommand("wisp_nonights", function(player)
	if not (wisps.cheats)
		CONS_Printf(player, "Cheats must be enabled to use this!")
		return
	end
	
	if not player.mo
		CONS_Printf(player, "You lack a player object!")
		return
	end
	
	if (player.powers[pw_carry] == CR_NIGHTSMODE)
		player.powers[pw_carry] = CR_NONE
		player.mo.flags = $ & ~(MF_NOGRAVITY)
		CONS_Printf(player, "Disabled NIGHTS mode. To enable it, touch the starting point again.")
	else
		CONS_Printf(player, "You're not in NIGHTS mode, so nothing happened.")
	end
end)

COM_AddCommand("wisp_help", function(player)
	CONS_Printf(player, "You need help? Here's the command list:")
	CONS_Printf(player, "wisp_allowsuperfly: [SERVER/GAME ADMIN ONLY] toggles Super Sonic's Super Fly on or off. Affects all players.")
	CONS_Printf(player, "wisp_ringdrain: [SERVER/GAME ADMIN ONLY] toggles the mass ring drain when boosting as Super Sonic. Affects all players.")
	CONS_Printf(player, "wisp_cheats: [SERVER/GAME ADMIN ONLY] toggles cheat mode on and off. When on, you can use some commands that require this to be on")
	CONS_Printf(player, "wisp_endlessboost: toggles endless boost mode. Affects you only.")
	CONS_Printf(player, "wisp_spinability: Type it in without no arguments for more details")
	CONS_Printf(player, "wisp_grantwisp <wisp>: [CHEATS MUST BE ENABLED TO USE THIS] Input this command without any arguments for more details.")
	CONS_Printf(player, "wisp_nonights: [CHEATS MUST BE ENABLED TO USE THIS] If you're in nights mode and want to get out, use this command. When nights mode is off, you can use wisps and your abilities like normal!")
end)
		

//Laser wisp aiming reticle (uses the RETT sprites)
addHook("ThinkFrame", do
	for player in players.iterate
		local ret = player.laserreticle
		if player.mo and player.mo.valid
		and player.laserphase1
		and not player.spectator
			local mo = player.mo
			local flip = P_MobjFlip(mo)
			local dist = FixedMul((player.laserspeed*3)*FRACUNIT, mo.scale)
			local rx = mo.x + FixedMul(dist, cos(mo.angle))
			local ry = mo.y + FixedMul(dist, sin(mo.angle))
			local rz = mo.z + FixedMul((player.dz*3)*FRACUNIT*flip, mo.scale)
			if not (ret and ret.valid)
				ret = P_SpawnMobj(rx, ry, rz, MT_LASERRETICLE)
				player.laserreticle = ret
			end
			if ret and ret.valid
				ret.scale = mo.scale
				ret.flags2 = $1 & ~MF2_OBJECTFLIP
				if flip == -1
					ret.flags2 = $1|MF2_OBJECTFLIP
				end
				P_SetOrigin(ret, rx, ry, rz)
			end
		elseif ret and ret.valid
			P_RemoveMobj(ret)
			player.laserreticle = nil
		end
	end
end)

-- ============================================================
-- BOOST POR ENEMIGOS: derrotar enemigos rellena el medidor de boost
-- (ya no hace falta depender de la caja/wisp de boost)
-- ============================================================
local ENEMY_BOOST_GAIN = 15 -- boost que da cada enemigo derrotado
local BOSS_BOOST_GAIN = 10  -- boost que da cada golpe a un jefe
local BOOST_MAX = 105

local function WispMoPlayer(mo)
	if mo and mo.valid and mo.player and mo.player.valid then
		return mo.player
	end
	return nil
end

local function WispGetBoostPlayer(source, inflictor)
	local player = WispMoPlayer(source)
	if player then return player end
	player = WispMoPlayer(inflictor)
	if player then return player end
	if inflictor and inflictor.valid then
		return WispMoPlayer(inflictor.target)
	end
	return nil
end

local function WispAddBoost(player, amount)
	if player.boostmeter == nil then player.boostmeter = BOOST_MAX end
	player.boostmeter = min(player.boostmeter + amount, BOOST_MAX)
end

addHook("MobjDeath", function(target, inflictor, source)
	if not (target and target.valid) then return end
	if not (target.flags & (MF_ENEMY|MF_BOSS)) then return end
	local player = WispGetBoostPlayer(source, inflictor)
	if player then
		WispAddBoost(player, ENEMY_BOOST_GAIN)
	end
end)

addHook("MobjDamage", function(target, inflictor, source)
	if not (target and target.valid) then return end
	if not (target.flags & MF_BOSS) then return end
	local player = WispGetBoostPlayer(source, inflictor)
	if player then
		WispAddBoost(player, BOSS_BOOST_GAIN)
	end
end)
