//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)
freeslot("S_PLAY_TAUNT", "SPR_TUNT", "S_PLAY_TPOSE", "SPR_TPOS", "sfx_vnboom")

//A small adjustment to make sure the Triple Dash works properly.
addHook("AbilitySpecial", function(player)
   if player.mo.skin == "extralife" and player.charability == CA_NONE
   if not (player.pflags & PF_THOKKED)
	player.pflags = $1|PF_THOKKED
  end
 end
end)

//Finally changed the ability key to Jump.
//This required a slight rewrite but hey,
//whatever pleases the people, right?
addHook("PlayerThink", function(player)
   if 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.tdashtimer == nil
    or P_IsObjectOnGround(player.mo)
	   player.tdashtimer = 0
    end
	
   if player.exiting
    or P_PlayerInPain(player)
    or player.tdashcount > 2 // counting starts at 0
	    then return
    end
	
	if (player.pflags & PF_THOKKED)
	and (player.pflags & PF_JUMPED)
    and (player.pflags & PF_JUMPDOWN)
        player.tripledash = $ + 1
    else
        player.tripledash = 0
    end
	
    //Here's the ability itself.
	if (player.pflags & PF_THOKKED)
	and (player.pflags & PF_JUMPED)
	and (player.pflags & PF_JUMPDOWN)
	and (player.tripledash <= 1)
	and not (player.tdashtimer >= 69)
	  player.pflags = $ & ~PF_STARTJUMP -- Patch for the Low Dash. ~ARJr
	  player.mo.state = S_PLAY_SPINDASH
	  S_StartSound(player.mo, sfx_spndsh)
	elseif (player.tdashtimer >= 69)
	   player.tdashcount = 3
	   S_StartSound(player.mo, sfx_s3k51)
	   player.mo.state = S_PLAY_FALL
	   player.pflags = $|PF_NOJUMPDAMAGE
	end
	if (player.pflags & PF_THOKKED)
	and (player.pflags & PF_JUMPED)
	and (player.pflags & PF_JUMPDOWN)
	and not (player.tdashtimer >= 69)
	and not (player.infinitedash == 1)
	 player.tdashtimer = $ + 1
	end
	if (player.mo.state == S_PLAY_SPINDASH)
	and not (player.tdashtimer >= 69)
	    P_InstaThrust(player.mo, player.mo.angle, 0*FRACUNIT)
	    P_SetObjectMomZ(player.mo, 0*FRACUNIT, false)
	end
	if (player.pflags & PF_THOKKED) 
	and player.mo.state == S_PLAY_SPINDASH
	and (player.pflags & PF_JUMPED)
	  and not (player.pflags & PF_JUMPDOWN)
	  and not (player.tdashtimer >= 69)
        P_InstaThrust(player.mo, player.mo.angle, 30*FRACUNIT)
        S_StartSound(player.mo, sfx_zoom)
		player.tdashtimer = 0
        player.mo.state = S_PLAY_JUMP
	 if not (player.cmd.buttons & BT_SPIN) -- You can now hold Spin to go low! ~ARJr
	 if (player.powers[pw_underwater])
	 P_SetObjectMomZ(player.mo, 3*FRACUNIT, false)
	 else
	 P_SetObjectMomZ(player.mo, 12*FRACUNIT, false)
	 end
	   if (player.infinitedash == 0)
         player.tdashcount = $ + 1
	   elseif (player.infinitedash == 1)
	     player.tdashcount = 0
		 end
	  end
   end
end)

//The frame-perfect jump bug is now obsolete!
//I am a mad genius!
local function TripleDashReset(player)
   if player.mo and player.mo.skin == "extralife"
      and not P_IsObjectOnGround(player.mo)
	  
	  if player.mo.eflags & MFE_JUSTHITFLOOR
	     player.tdashcount = 0
	  end
   end
end

addHook("PlayerThink", TripleDashReset)

//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)

//Taunting!
//I've received requests for it, and now Ruby's delivering!

sfxinfo[sfx_vnboom].caption = "WHAT? EXTRA LIFE T-POSIN!?"

//Credit to Icezer for a portion of the code from Alt Sonic.
local function ELTauntThinker(player)
    if player.mo.skin ~= "extralife"
	   then return
	end
	
	if P_IsObjectOnGround(player.mo)
	 and player.speed == 0
	  if player.cmd.buttons & BT_CUSTOM3
	   if player.c3down == false
	      if P_RandomChance(FRACUNIT/100)
	        S_StartSound(player.mo, sfx_vnboom)
	        player.mo.state = S_PLAY_TPOSE
	    else
	        P_SpawnMobjFromMobj(player.mo, 0, 0, 0, MT_EXPLODE)
	        S_StartSound(player.mo, sfx_pop)
	        player.mo.state = S_PLAY_ELTAUNT
	     end
		 player.c3down = true
      end
      player.pflags = $1|PF_FULLSTASIS
	  player.swapdelay = 0
	  else
	      player.c3down = false
		  if player.mo.state == S_PLAY_ELTAUNT
		  P_SpawnMobjFromMobj(player.mo, 0, 0, 0, MT_EXPLODE)
	      S_StartSound(player.mo, sfx_pop)
		  player.mo.state = S_PLAY_STND
		  elseif player.mo.state == S_PLAY_TPOSE
		  player.mo.state = S_PLAY_STND
	   end
	 end
   else
	   if player.mo.state == S_PLAY_ELTAUNT
	   P_SpawnMobjFromMobj(player.mo, 0, 0, 0, MT_EXPLODE)
	   S_StartSound(player.mo, sfx_pop)
	   player.mo.state = S_PLAY_STND
	   elseif player.mo.state == S_PLAY_TPOSE
	   player.mo.state = S_PLAY_STND
      end
   end
end

addHook("PlayerThink", ELTauntThinker)
	   
//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)

//Kirby compatibility
if not(kirbyabilitytable)
    rawset(_G, "kirbyabilitytable", {})
end

kirbyabilitytable["extralife"] = 6 //Needle
   