//Extra Life from Sonic the Comic
//Sonic the Comic by Fleetway Editions
//Original Triple Dash ability from the Sonic 3 A.I.R. mod by Vsgdv

//I'd like to give my biggest thanks to NARbluebear for helping me with this.
//I would've been stuck with no dash limit like a total idiot otherwise.
//(just so you know, most of the code was still done by me)
local function ExtraLifeTripleDash(player)
    if not (player.mo and player.mo.valid
    and player.mo.skin == "extralife")
        then return
    end
	
    if player.tripledash == nil
        player.tripledash = 0
    end
	
	if player.infinitedash == nil 
        player.infinitedash = 0 //this is important later on
    end
    
    if player.tdashcount == nil
    or P_IsObjectOnGround(player.mo)
        player.tdashcount = 0
    end
    
    if player.exiting
    or P_PlayerInPain(player)
    or player.tdashcount > 2 // counting starts at 0
	    then return
    end
	
	if not (P_IsObjectOnGround(player.mo))
    and player.pflags & PF_SPINDOWN
        player.tripledash = $ + 1
    else
        player.tripledash = 0
    end

    if not (P_IsObjectOnGround(player.mo))
    and player.pflags & PF_JUMPED
    //The dash
    and player.tripledash <= 1
    and player.pflags & PF_SPINDOWN
        player.mo.state = S_PLAY_SPINDASH
		S_StartSound(player.mo, sfx_spndsh)
    end
	if player.mo.state == S_PLAY_SPINDASH
	    P_InstaThrust(player.mo, player.mo.angle, 0*FRACUNIT)
	    P_SetObjectMomZ(player.mo, 0, false)
    end
	
    //Release
    if not (P_IsObjectOnGround(player.mo))
    and player.tripledash <= 1
    and player.mo.state == S_PLAY_SPINDASH
    and not (player.pflags & PF_SPINDOWN)
        P_SetObjectMomZ(player.mo, 12*FRACUNIT, false)
        P_InstaThrust(player.mo, player.mo.angle, 30*FRACUNIT)
        S_StartSound(player.mo, sfx_zoom)
        player.mo.state = S_PLAY_JUMP
		if (player.infinitedash == 0)
        player.tdashcount = $ + 1
		elseif (player.infinitedash == 1)
		player.tdashcount = 0
	  end
   end
end



addHook("PlayerThink", ExtraLifeTripleDash)

//Thanks for your suggestion, A'Dilla!
addHook("ShieldSpecial", function(player)
   if player.tripledash <= 1
      and player.tdashcount < 2
	     then return true
	  elseif player.tdashcount > 2
	     then return false
    end
end)

//Jelly Tails & Extra Life's Quick Swap.
local function JTELQuickSwap(player)
    if not (player.mo and player.mo.valid)
	and (player.mo.skin == "jellytails"
	or player.mo.skin == "extralife")
	   then return
	end
    
	if not (players[1] and players[1].bot and players[1].valid)
	and (players[1].skin == "extralife"
	or players[1].skin == "jellytails")
	   then return
	end
	
	if player.swapdelay == nil
	   player.swapdelay = 0
	end
	
    if player.exiting
	   then return
	end
	
	//Switch to Extra Life
	if (player.cmd.buttons & BT_CUSTOM2)
	and (player.mo.skin == "jellytails")
	and (player.swapdelay > 34)
	S_StartSound(player.mo, sfx_kc65)
	A_OldRingExplode(player.mo, MT_PARTICLE, 0)
	R_SetPlayerSkin(player, "extralife")
	R_SetPlayerSkin(players[1], "jellytails")
	player.swapdelay = 0
	//Switch to Jelly Tails
	elseif (player.cmd.buttons & BT_CUSTOM2)
	and (player.mo.skin == "extralife")
	and (player.swapdelay > 34)
	S_StartSound(player.mo, sfx_kc65)
	A_OldRingExplode(player.mo, MT_PARTICLE, 0)
	R_SetPlayerSkin(player, "jellytails")
	R_SetPlayerSkin(players[1], "extralife")
	player.swapdelay = 0
	end
	player.swapdelay = $ + 1
end

addHook("PlayerThink", JTELQuickSwap)


//This, ladies and gentlemen, is the Infinite Dash.
//It is the result of me being unable to make a limit for the Triple Dash
//without NARbluebear's help.
//I decided I would bring it back just for the kicks.

//Admin only command as to prevent total cheating.
local function InfiniteDashCheat(player, arg)
    if player.mo and player.mo.valid
	   and player.mo.skin == "extralife"
	   string = "off"
	      if arg
		      arg = arg:lower()
			  if arg == "off"
			    string = "off"
			    player.infinitedash = 0
				CONS_Printf(player, "Extra Life now uses the Triple Dash as intended.")
			  elseif arg == "on"
			    string = "on"
			    player.infinitedash = 1
				CONS_Printf(player, "Extra Life can now use the Triple Dash indefinetly.")
			  end
	     else
			 CONS_Printf(player, "Allows you to use the Triple Dash indefinetly. Currently " + string)
		  end
	 else
		 CONS_Printf(player, "You must be in a level to use this command!")
	 end
end

COM_AddCommand("infdash", function(player, arg)
    InfiniteDashCheat(player, arg)
end, COM_ADMIN)
   