--Timers!

addHook("PlayerThink",  function(p)
    if not (p.stt and (p.mo and p.mo.valid and p.mo.skin == "surge"))
	return
	end 
	local s = p.mo
	local yu = p.stt
	
	--TheVolt Dash! Powerful electric super thok.
	
	if yu.voltdash
		p.charflags = $|SF_CANBUSTWALLS
		SurgeVoltSpawn(p, s, yu, H, G, nil, true)
		local ghs = P_SpawnGhostMobj(s)
			if leveltime % 2 == 0
				ghs.color = SKINCOLOR_ICY
			else
				ghs.color = SKINCOLOR_SKY
			end
		ghs.blendmode = AST_ADD
		ghs.rollangle = ANG30*yu.voltdash
		if yu.voltjump == 20
			s.momz = $/3
		end	
		if yu.voltdash == 25
		ghs = P_SpawnMobjFromMobj(s, 0, 0, 0, MT_STTAUTOVOLT)
		ghs.sttspeed = 2
		ghs.tics = 14
		ghs.frame = T|FF_FULLBRIGHT
		ghs.destscale = $*99
		end
			
	yu.voltdash = $-1
	
		if not (p.pflags & PF_JUMPED)
		or P_PlayerInPain(p)
		yu.voltdash = 0
		end
	else
		p.charflags = $ & ~SF_CANBUSTWALLS
	end
	--Stretchy effect when double-jumping or thokking
	if yu.doublejumpy
	yu.doublejumpy = $-1
	s.spriteyscale = FRACUNIT+(yu.doublejumpy*5000)
	s.spritexscale = FRACUNIT-(yu.doublejumpy*2000)
		if not yu.doublejumpy
		s.spriteyscale = FRACUNIT
		s.spritexscale = FRACUNIT
		end
	end
	--Improved air control
	if (yu.voltjump or yu.controltime)
	and not P_IsObjectOnGround(s)
		if (p.speed < 18*s.scale) and (p.cmd.sidemove or p.cmd.forwardmove)
		local ang = (p.cmd.angleturn<<16) + (R_PointToAngle2(0, 0, p.cmd.forwardmove<<16, -(p.cmd.sidemove<<16)))
			if not (s.eflags & MFE_UNDERWATER)
				P_Thrust(s, ang, FRACUNIT) 
			else
				P_Thrust(s, ang, 17000)
			end			
		end
	end
	if yu.nohb --Time for NOT laying down hitboxes.
		if not p.homing
		yu.nohb = $-1
			if yu.homingspeed != nil
			and yu.homingangle != nil
			and not P_IsObjectOnGround(s) and (p.pflags & PF_JUMPED)
			P_InstaThrust(s, yu.homingangle, (yu.homingspeed*6)/9)
			yu.homingspeed = nil
			yu.homingangle = nil
			end
		end
	end
	if yu.controltime --Time for air control
	yu.controltime = $-1
	end
	if s.state == S_PLAY_SPRING
		yu.notfalling = 2
	elseif yu.notfalling and s.state == S_PLAY_FALL and not (yu.voltjump > 20)
		s.sprite2 = SPR2_TIRE
		s.frame = A
		yu.notfalling = $-1
	end
	--The super double jump!
	
	if yu.voltjump
	yu.voltjump = $-1
		SurgeAuraSpawn(p, s, yu, A, E)
		
		local ghs = P_SpawnGhostMobj(s)
		ghs.color = P_RandomRange(SKINCOLOR_SUPERSKY1, SKINCOLOR_SUPERSKY5)
		ghs.blendmode = AST_ADD
		ghs.z = $-(s.momz*2)
		ghs.destscale = ghs.scalespeed
		
		if (yu.voltjump > 5)
		
			p.drawangle = (yu.voltjump*(ANG1*45))
			s.sprite2 = SPR2_SPNG
			s.frame = A	
			if (yu.voltjump > 15)
			local offset = 5*FRACUNIT
			local frame = Z+5
			if (leveltime % 2 == 0)
			offset = $*-1
			frame = Z+4
			end
			--Upwards bolt of lightning!
			local ghs = P_SpawnMobjFromMobj(s, offset*-1, offset, -35*FRACUNIT-(yu.voltjump*FRACUNIT), MT_STTAUTOVOLT) 
			ghs.sprite = SPR_STAU
			ghs.frame = frame|FF_FULLBRIGHT
			ghs.scale = s.scale+(800*yu.voltjump)
			ghs.sttspeed = 1
			ghs.tics = 4
			ghs.rollangle = ANGLE_90
			ghs.blendmode = AST_ADD
		
			if yu.voltjump == 21
				s.momz = $/6
			elseif (yu.voltjump > 21)
				P_RadiusAttack(s, s, 150*s.scale)
				p.pflags = $ & ~PF_JUMPED & ~PF_STARTJUMP
				s.sprite2 = SPR2_SPNG
				s.frame = A
			end
			end
		elseif yu.voltjump == 5
			p.pflags = $|PF_JUMPED & ~PF_THOKKED
			if s.state != S_PLAY_ROLL
			s.state = S_PLAY_ROLL
			end
		end
		if P_PlayerInPain(p)
		yu.voltjump = 0
		end
	end		
end)

--Automatically advances frames.
addHook("MobjThinker",  function(s)
	if s.valid and (leveltime % s.sttspeed == 0)
		s.frame = $+1
		if s.valid and (s.tics < 5) and s.sttfade
			if not (s.frame >= TR_TRANS90)
			s.frame = $+FRACUNIT
			end
		s.scalespeed = $*2
		end
	end
end, MT_STTAUTOVOLT)

--Automatically advances frames.
addHook("MobjThinker",  function(s)
	if s.fuse < 7
		s.blendmode = $ & ~AST_ADD
		s.frame = $|(TR_TRANS90-(s.fuse*FRACUNIT))
		s.destscale = $*99
		s.flags = $|MF_NOCLIPTHING|MF_NOCLIPHEIGHT|MF_NOCLIP & ~MF_MISSILE
		return
	else
		s.blendmode = AST_ADD
		s.frame = $ & ~FF_FULLBRIGHT & ~TR_TRANS40 --Laziness ahoy.
		if s.frame > S
			s.frame = P
		else
			s.frame = $+1
		end
		s.frame = $|FF_FULLBRIGHT|TR_TRANS40
	end
end, MT_STTTRAIL)

--Bashed something? Gain electricity! All the incentive to be an aggressive little jerk!
addHook("MobjDamage", function(s, i, so)
	if i and i.valid and (i.skin == "surge") and i.player // and i.player.stt.homer
	and s.valid and ((s.flags & MF_ENEMY) or (s.flags & MF_MONITOR) or (s.flags & MF_BOSS))
	local yu = i.player.stt
	local p = i.player
		yu.nohb = 2
		yu.homer = false
		yu.fullcharge = true
		yu.curltime = true
		if (p.pflags & PF_JUMPED) or (p.pflags & PF_NOJUMPDAMAGE)
			if not (yu.voltdash > 20)
			and not (yu.voltjump > 20)
			p.pflags = $ & ~PF_THOKKED
			end
		end
	end
end)

addHook("ShouldDamage", function(s, i, so, damage, dmgtype)
	if dmgtype and dmgtype == DMG_ELECTRIC and (s.skin == "surge") and s.player
	local p = s.player
		if not p.stt.fullcharge
		and not (p.cmd.buttons & BT_CUSTOM1)
		and not p.gotflag
		p.stt.fullcharge = true --free zoom zoom
		S_StartSound(s, sfx_cdfm55)
		end
		if so and so.valid
		and ((so.type == MT_CYBRAKDEMON) or (so.type == MT_CYBRAKDEMON_ELECTRIC_BARRIER)) --Feel free to add any other objects here.
			if (s.state != S_PLAY_PAIN)
				S_StartSound(s, sfx_s3kae)
				P_SetObjectMomZ(s, 8*FRACUNIT)
				P_InstaThrust(s, R_PointToAngle2(s.x, s.y, so.x, so.y), -30*FRACUNIT)
				s.state = S_PLAY_PAIN
				p.pflags = $ & ~PF_JUMPED & ~PF_SPINNING & ~PF_STARTDASH & ~PF_THOKKED
				SurgeQuake(p,30*FRACUNIT, 4)
			end
		end
		return false
	end
end, MT_PLAYER)

--Literally just copy-paste.

addHook("MobjThinker", function(s)
	if not s.valid 
	return end
	if displayplayer and displayplayer.amstt
	or secondarydisplayplayer and secondarydisplayplayer.amstt
		s.flags2 = $|MF2_DONTDRAW
		return true
	end
end, MT_LOCKON)

--Kill spikes, thx.
local function DieSpike(s, v)
	if not (v.player and v.player.stt.voltdash and v.skin == "surge")
	return
	end
	if not (v.z > s.z+s.height)
	and not (s.z > v.z+v.height)
		S_StartSound(v, s.info.deathsound)
		P_KillMobj(s, v, v)
		SurgeQuake(v.player, 45*s.scale, 3)
		return false 
	end
end
addHook("MobjCollide", DieSpike, MT_SPIKE)
addHook("MobjCollide", DieSpike, MT_WALLSPIKE)

//This is a strategic collaboration with Modern Sonic and NOT a laziness thing!
--Touch Kirby's spark thingy? Get charge.
local function Touch2Charge(s, v)
	if not (v.player and v.skin == "surge" and v.player.stt and not v.player.stt.fullcharge)
	return
	end
	if not (v.z > s.z+s.height)
	and not (s.z > v.z+v.height)
		SurgeMaxAura(v.player, v, v.player.stt)
		return false 
	end
end
addHook("MobjCollide", Touch2Charge, MT_SPARKEFF)
addHook("MobjMoveCollide", Touch2Charge, MT_SPARKEFF)
addHook("MobjCollide", Touch2Charge, MT_SPARKHURT)
addHook("MobjMoveCollide", Touch2Charge, MT_SPARKHURT)
//This is a strategic collaboration with Modern Sonic and NOT a laziness thing!

addHook("MobjMoveCollide", function(eb, item)
	if eb.valid and eb.target and eb.target.valid and eb.target.player and P_CanPickupItem(eb.target.player) and not P_PlayerInPain(eb.target.player) and item and item.valid and eb.z <= item.z + item.height*10 and eb.z + eb.height >= item.z
	and not item.soncattract and (item.type == MT_RING or item.type == MT_COIN or eb.target.player.ctfteam == 1 and item.type == MT_REDTEAMRING or eb.target.player.ctfteam == 2 and item.type == MT_BLUETEAMRING)
		item.soncattract = true
		item.target = eb.target
		item.ringchasetimer = 6*TICRATE
		if not eb.target.player.stt.ringattract then eb.target.player.stt.ringattract = {} end
		table.insert(eb.target.player.stt.ringattract,item)
	end
end, MT_STTRINGRANGE)

--Stop hurting my friends! I mean...wait, who?
local function DontTouch(s, v)
	if not (v and v.valid)
	or (v.type == MT_STTTRAIL)
	return false
	end
	if v.player
		if (gametype == GT_COOP) or (gametype == GT_RACE) or (gametype == GT_COMPETITION) or G_TagGametype()
			if v.skin == "surge" and s.target and s.target.valid and s.target.player and (s.target != v)
				SurgeMaxAura(v.player, v, v.player.stt) --Free zooms
			end
			return false
		elseif s.valid and G_GametypeHasTeams() and s.target and s.target.valid and s.target.player
		and (v.player.ctfteam == s.target.player.ctfteam)
			if v.skin == "surge" and s.target and s.target.valid and s.target.player and (s.target != v)
				SurgeMaxAura(v.player, v, v.player.stt) --Free zoom from friends
			end
			return false
	//	elseif G_GametypeHasSpectators() or G_GametypeHasTeams()		
		end
	end
end
addHook("MobjCollide", DontTouch, MT_STTTRAIL)
addHook("MobjMoveCollide", DontTouch, MT_STTTRAIL)

--Let's try MFE_SPRUNG first.
--Reset attributes when hitting springs.
/*addHook("MobjMoveCollide", function(s, v)
if not ((s.player and s.skin == "surge" and s.player.stt) or (v and v.valid))
return end
local p = s.player
local yu = p.stt
	if (v.flags & MF_SPRING)
		if not (s.z > v.z+v.height) and not (v.z > s.z+s.height)
			yu.homingspeed = 0
			yu.homingangle = 0
			yu.voltjump = 0
			yu.voltdash = 0
			return
		end
	end
end, MT_PLAYER)*/