-- // HMS Master Script
-- // Mostly created by Wolfy
-- // This script contains HMS' main hook, which handles his stats/overpowering and technical stuff like transformation.
-- // Ported to SRB2 v2.2 by SteelT

freeslot("S_HSPK1", "S_HSPK2", "S_HSPK3", "S_HSPK4", "S_HSPK5", "MT_HYPERSPARK", "SPR2_HTNS", "S_HMSTRANS1", "S_HMSTRANS2")

states[S_HSPK1] = {SPR_SSPK, 0, 1, A_SparkFollow, 0, 0, S_HSPK2}
states[S_HSPK2] = {SPR_SSPK, 1, 1, A_SparkFollow, 0, 0, S_HSPK3}
states[S_HSPK3] = {SPR_SSPK, 2, 1, A_SparkFollow, 0, 0, S_HSPK4}
states[S_HSPK4] = {SPR_SSPK, 1, 1, A_SparkFollow, 0, 0, S_HSPK5}
states[S_HSPK5] = {SPR_SSPK, 0, 1, A_SparkFollow, 0, 0, S_HSPK1}
states[S_HMSTRANS1] = {SPR_PLAY, SPR2_HTNS|FF_SPR2SUPER|FF_FULLBRIGHT|FF_ANIMATE, 2, nil, 0, 0, S_HMSTRANS2}
states[S_HMSTRANS2] = {SPR_PLAY, SPR2_HTNS|FF_SPR2SUPER|FF_FULLBRIGHT|FF_ANIMATE, 14, A_FadeOverlay, 0, 0, S_PLAY_FALL}

mobjinfo[MT_HYPERSPARK] = {
	doomednum = -1,
	spawnstate = S_HSPK1,
	spawnhealth = 1,
	seestate = S_NULL,
	seesound = sfx_None,
	reactiontime = 8,
	attacksound = sfx_None,
	painstate = S_NULL,
	painchance = 0,
	painsound = sfx_None,
	meleestate = S_NULL,
	missilestate = S_NULL,
	deathstate = S_NULL,
	xdeathstate = S_NULL,
	deathsound = sfx_None,
	speed = 40*FRACUNIT,
	radius = 8*FRACUNIT,
	height = 8*FRACUNIT,
	mass = 100,
	damage = 20,
	activesound = sfx_None,
	flags = MF_NOBLOCKMAP|MF_NOCLIP|MF_NOGRAVITY|MF_SCENERY,
	raisestate = S_NULL
}

local function SetStats(player)
	if (player.mo.skin == "hms123311") then
		player.powers[pw_underwater] = 1073741824
		player.powers[pw_spacetime] = 1073741824
		player.powers[pw_shield] = SH_ATTRACT
		player.powers[pw_emeralds] = 127
		//player.powers[pw_invulnerability] = TICRATE // Commented out due to music bug with extra life.
		player.mo.supershield = 1
		player.normalspeed = 60*FRACUNIT
		player.accelstart = 200
		player.acceleration = 60
		player.thrustfactor = 5
		player.jumpfactor = 2*FRACUNIT
		player.mindash = 198*FRACUNIT
		player.maxdash = 200*FRACUNIT
	else
		player.powers[pw_underwater] = 0
		player.powers[pw_spacetime] = 0
		player.powers[pw_emeralds] = 0
		//player.powers[pw_invulnerability] = 0
		player.normalspeed = skins[player.mo.skin].normalspeed
		player.accelstart = skins[player.mo.skin].accelstart
		player.acceleration = skins[player.mo.skin].acceleration
		player.thrustfactor = skins[player.mo.skin].thrustfactor
		player.jumpfactor = skins[player.mo.skin].jumpfactor
		player.mindash = skins[player.mo.skin].mindash
		player.maxdash = skins[player.mo.skin].maxdash
		player.powers[pw_super] = 0
		player.powers[pw_emeralds] = 0
	end
end

addHook("PlayerThink", function(player)
	local sparkle	
	if (player.mo and player.mo.skin == "hms123311") then -- // recolors-only!		
		if ((player.powers[pw_carry] == CR_NIGHTSMODE) or (maptol & TOL_NIGHTS)) then return end -- don't mess with nights, man
		if (G_RingSlingerGametype()) then 
			local rand = P_RandomByte()
			if (rand <= 85) then
				R_SetPlayerSkin(player, "sonic")
			elseif (rand >= 170) then
				R_SetPlayerSkin(player, "knuckles")
			else
				R_SetPlayerSkin(player, "tails")
			end
			chatprintf(player, "Sorry, HMS is banned in Ringslinger gamemodes. You should know better.")
		end
			
		if not (All7Emeralds(emeralds)) and not G_GametypeHasSpectators()
			for i = MT_EMERALD1,MT_EMERALD7 do
				P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, i)
			end
		end
			
		if (player.exiting or player.playerstate ~= PST_LIVE) then
			player.powers[pw_shield] = SH_NONE
			//player.powers[pw_invulnerability] = 0
			player.powers[pw_emeralds] = 0
			P_RestoreMusic(player)
		end
		if (player.playerstate == PST_DEAD) then
			player.mo.state = S_PLAY_DEAD
		end
		
		// Extended transformation frames
		if (player.mo.state == S_PLAY_SUPER_TRANS5 and (leveltime % 3 == 0))
			player.mo.state = S_HMSTRANS1
		end
				
		if (player.mo.state >= S_HMSTRANS1 and player.mo.state <= S_HMSTRANS2)
			player.pflags = $|PF_FULLSTASIS
		end
		
		if not player.exiting then
			if (player.mo.issuper == nil) then
				P_DoSuperTransformation(player, true)
				player.mo.issuper = 1
			end
			player.mo.frame = $1|FF_FULLBRIGHT
			player.hmsreset = nil
			SetStats(player)
			if not (player.mo.sparkcount) and not (player.mo.state >= S_PLAY_SUPER_TRANS1) then
				local spark
				for i = 0, 8, 1 do
					spark = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z + player.mo.height, MT_HYPERSPARK)
					spark.target = player.mo
					spark.angle = i*ANGLE_45
				end
				player.mo.sparkcount = 1
			end 
			if ((leveltime % TICRATE == 0) and not (player.mo.state >= S_PLAY_SUPER_TRANS1) and not (player.playerstate ~= PST_LIVE)) then
				player.rings = $1 + 1
			end
			if ((player.pflags & PF_SPINNING) and not (player.pflags & PF_STARTDASH)) then
				P_ElementalFire(player)
			end
					
			// Spawn sparkles, due to not using pw_invulnerability
			if (leveltime % (TICRATE/7) == 0)
				sparkle = P_SpawnMobj(player.mo.x, player.mo.y, player.mo.z, MT_IVSP)
				sparkle.destscale = player.mo.scale
				P_SetScale(sparkle, player.mo.scale)
			end
		end
	else
		if not player.hmsreset then
			SetStats(player)
			--//player.mo.color = skins[player.mo.skin].prefcolor
			player.mo.color = player.skincolor
			P_RestoreMusic(player)
			player.mo.issuper = nil 
			player.mo.sparkcount = nil
			if player.mo.supershield == 1 then
				player.powers[pw_shield] = SH_NONE
				player.mo.supershield = 0
			end
			player.hmsreset = 1
		end
	end
end)

addHook("MobjThinker", function(spark)
	if (spark.target and spark.target.valid) then 
		if (spark.target.eflags & MFE_VERTICALFLIP) then
			spark.eflags = $1|MFE_VERTICALFLIP
		else
			spark.eflags = $1 & ~MFE_VERTICALFLIP
		end
		spark.scale = spark.target.scale
	end
end, MT_HYPERSPARK)

COM_AddCommand("footcancer", function(player)
	print("\x84"..player.name.." \x00has been kicked. \x84Reason:\x00 Nice try, but it'll take more lels then that.")
	COM_BufInsertText(player, "quit")
end)		