//Rafael Extra Stuff

local cv_extra = CV_RegisterVar({"rafa_rings", "On", CV_NETVAR, CV_OnOff})

freeslot("S_PLAY_RAFXTRA", "MT_RAFAXTRA","S_PLAY_RAFXTRB", "MT_RAFAXTRB", "S_PLAY_RAFXTRC", "MT_RAFAXTRC")

states[S_PLAY_RAFXTRA] = {SPR_PLAY, SPR2_CLMB, 5, nil, 0, 0, S_PLAY_RAFXTRA}
states[S_PLAY_RAFXTRB] = {SPR_PLAY, SPR2_LAND, 5, nil, 0, 0, S_PLAY_RAFXTRB}
states[S_PLAY_RAFXTRC] = {SPR_PLAY, SPR2_CLNG, 5, nil, 0, 0, S_PLAY_RAFXTRC}

addHook("MobjThinker", function(mobj)
	if (mobj.valid) and (mobj.health)
		P_SpawnGhostMobj(mobj)
		P_KillMobj(mobj)
	end
end, MT_RAFAXTRA)

addHook("MobjThinker", function(mobj)
	if (mobj.valid) and (mobj.health)
		P_SpawnGhostMobj(mobj)
		P_KillMobj(mobj)
	end
end, MT_RAFAXTRB)

addHook("MobjThinker", function(mobj)
	if (mobj.valid) and (mobj.health)
		P_SpawnGhostMobj(mobj)
		P_KillMobj(mobj)
	end
end, MT_RAFAXTRC)

//Emotes
addHook("PlayerThink", function(player)
	if srb2p return end
	if player.mo and player.mo.valid and player.mo.skin == "rafael"
		//Detect when we press Toss Flag
		if not (player.cmd.buttons & BT_TOSSFLAG)
			player.rafaeltosstapready = true
			player.rafaeltosstapping = false
		elseif player.rafaeltosstapready 
		and not (player.hugging) then
			player.rafaeltosstapping = true
			player.rafaeltosstapready = false
		else
			player.rafaeltosstapping = false
		end
		if player.panim == PA_PAIN
		or player.panim == PA_DEAD
		or player.mo.state == S_PLAY_DEAD
			player.rafaeltosstapready = false
			player.rafaeltosstapping = false
		end
		
		//Detect if we're tapping Custom 1
		if not (player.cmd.buttons & BT_CUSTOM1) then
			player.rafaelcustomtapready = true
			player.rafaelcustomtapping = false
		elseif player.rafaelcustomtapready then
			player.rafaelcustomtapping = true
			player.rafaelcustomtapready = false
		else
			player.rafaelcustomtapping = false
		end
		if player.panim == PA_PAIN
		or player.panim == PA_DEAD
		or player.mo.state == S_PLAY_DEAD
			player.rafaelcustomtapready = false
			player.rafaelcustomtapping = false
		end
		
		//Detect if we're tapping Fire Normal?
		if not (player.cmd.buttons & BT_FIRENORMAL) then
			player.rafaelweaponready = true
			player.rafaelweaponing = false
		elseif player.rafaelweaponready then
			player.rafaelweaponing = true
			player.rafaelweaponready = false
		else
			player.rafaelweaponing = false
		end
		if player.panim == PA_PAIN
		or player.panim == PA_DEAD
		or player.mo.state == S_PLAY_DEAD
			player.rafaelweaponready = false
			player.rafaelweaponing = false
		end
		
		//Chill
        if player.powers[pw_carry] == CA_NONE
            if player.rafxtra
            and not (player.hugging)
                player.panim = PA_DASH
				player.rafaelcustomtapping = false
				player.rafaelcustomtapready = false
				player.rafaelweaponready = false
				player.rafaelweaponing = false
				if player.mo.state == S_PLAY_RAFXTRA
					if player.playerstate != PST_LIVE
						player.mo.state = S_PLAY_DEAD
					end
				end
				if G_RingSlingerGametype()
					player.rafxtra = true
				end
                if player.rafaeltosstapping
                    if player.mo.state == S_PLAY_RAFXTRA
                        player.mo.state = S_PLAY_STND
                    end
                    player.rafxtra = false
                    P_RestoreMusic(player)
                else
                    if player.mo.state != S_PLAY_RAFXTRA
                        player.mo.state = S_PLAY_RAFXTRA
                    end
                    player.pflags = $|PF_FULLSTASIS
                end
            elseif player.rafaeltosstapping
                player.rafxtra = true
				if (player.powers[pw_super])
                P_PlayJingleMusic(player, "SCHILL", nil, true, JT_OTHER)
			elseif not player.rafxtrb or not player.rafxtrc
				P_PlayJingleMusic(player, "_CHILL", nil, true, JT_OTHER)
            end
        end
		if player.rafxtrb or player.rafxtrc
			player.rafxtra = false
		end
		
		//Dancing (Which is bassicaly a copy of the chill script but diferent :P)
		if player.powers[pw_carry] == CA_NONE
            if player.rafxtrb
            and not (player.hugging) and not (player.eggsuperflying)
                player.panim = PA_DASH
				player.rafaeltosstapping = false
				player.rafaeltosstapready = false
				player.rafaelweaponready = false
				player.rafaelweaponing = false
				if player.mo.state == S_PLAY_RAFXTRB
					if player.playerstate != PST_LIVE
						player.mo.state = S_PLAY_DEAD
					end
				end
				if G_RingSlingerGametype()
					player.rafxtrb = true
				end
                if player.rafaelcustomtapping
                    if player.mo.state == S_PLAY_RAFXTRB
                        player.mo.state = S_PLAY_STND
                    end
                    player.rafxtrb = false
                    P_RestoreMusic(player)
                else
                    if player.mo.state != S_PLAY_RAFXTRB
                        player.mo.state = S_PLAY_RAFXTRB
                    end
                    player.pflags = $|PF_FULLSTASIS
                end
            elseif player.rafaelcustomtapping and not (player.eggsuperflying)
                player.rafxtrb = true
				if not player.eggsuperflying or not player.rafxtra or not player.rafxtrc
					P_PlayJingleMusic(player, "_ODSZ1", nil, true, JT_OTHER)
				end
            end
        end
		if player.eggsuperflying
			player.rafxtrb = false
		end
		
		//O_O
		if player.powers[pw_carry] == CA_NONE
			if player.rafxtrc
			and not (player.hugging) and not player.eggsuperflying
				player.panim = PA_DASH
				player.rafaeltosstapping = false
				player.rafaeltosstapready = false
				player.rafaelcustomtapping = false
				player.rafaelcustomtapready = false
				if player.mo.state == S_PLAY_RAFXTRC
					if player.playerstate != PST_LIVE
						player.mo.state = S_PLAY_DEAD
					end
				end
				if player.rafaelweaponing or G_RingSlingerGametype()
					if player.mo.state == S_PLAY_RAFXTRC
						player.mo.state = S_PLAY_STND
					end
					player.rafxtrc = false
					P_RestoreMusic(player)
				else
					if player.mo.state != S_PLAY_RAFXTRC
						player.mo.state = S_PLAY_RAFXTRC
					end
					player.pflags = $|PF_FULLSTASIS
				end
			elseif player.rafaelweaponing and not (gametyperules & GTR_RINGSLINGER) and not (player.eggsuperflying)
				player.rafxtrc = true
				if not player.eggsuperflying or not player.rafxtra or not player.rafxtrb or not G_RingSlingerGametype()
					P_PlayJingleMusic(player, "SECRET", nil, true, JT_OTHER)
				end
			end
		end
		if player.eggsuperflying
		player.rafxtrc = false
		end
	end
end
end)

//Makes it soo that the level music continues
addHook("ShouldJingleContinue", function(player, musname)
	if true return end
	if musname != "RAFXTRA" return end
	if player and player.mo and player.mo.skin == "rafael" and player.rafxtra
		return true
	else
		return false
	end
end)

addHook("ShouldJingleContinue", function(player, musname)
	if true return end
	if musname != "RAFXTRB" return end
	if player and player.mo and player.mo.skin == "rafael" and player.rafxtrb
		return true
	else
		return false
	end
end)

addHook("ShouldJingleContinue", function(player, musname)
	if true return end
	if musname != "RAFXTRC" return end
	if player and player.mo and player.mo.skin == "rafael" and player.rafxtrc
		return true
	else
		return false
	end
end)

//Prevents him for doing the emote in Nights
addHook("PlayerSpawn", function(player)
    if player.mo and player.mo.valid and player.mo.skin == "rafael"
        if player.powers[pw_carry] != CR_NIGHTSMODE then player.mo.state = S_PLAY_STND end
        player.rafxtra = false
		player.rafxtrb = false
		player.rafxtrc = false
    end
end)

//Mini Rings
addHook("PlayerThink", function(player)
  if (player.mo.skin ~= "rafael")
  or not (cv_extra.value)
    return
  end
	if not player.rafxtra
	return
  end
	local enemy = P_LookForEnemies(player)
	if not (enemy) return end
	if (enemy.valid)
	and (enemy.flags & MF_ENEMY)
		P_KillMobj(enemy)
		local explosion = P_SpawnMobj(enemy.x, enemy.y, enemy.z, MT_RING)
		explosion.scale = enemy.scale/3
		explosion.state = S_RING
		explosion.target = player.mo
		explosion.nodmg = true
   end
end)

//Command
COM_AddCommand("rafa_jump", function(player, arg1)
    if arg1 == "1" or arg1 == "on" or arg1 == "yes"
	    and player.mo.skin == "rafael"
		player.charflags = $1|SF_NOJUMPSPIN|SF_NOJUMPDAMAGE
	else
		player.charflags = $1 & ~SF_NOJUMPSPIN|SF_NOJUMPDAMAGE
    end
end)

//Winning Animation
addHook("PlayerThink", function(player)
	if not (player.mo and player.mo.valid and player.mo.skin == "rafael") return end
	if player.exiting and player.mo.state == S_PLAY_STND
		if player.mo.state != S_PLAY_MELEE
			player.mo.state = S_PLAY_MELEE
		end
		player.mo.momx = 0 player.mo.momy = 0
	end
end)

//Kirby Support

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

kirbyabilitytable["rafael"] = 4
kirbyabilitytable["l.rafael"] = -9

//Battle!
local BattleFuncSet = false

addHook("ThinkFrame",function()
	if (BattleFuncSet) or not(CBW_Battle and CBW_Battle.SkinVars) then return end
	BattleFuncSet = true

	CBW_Battle.SkinVars["rafael"] = {
		flags = SKINVARS_GUARD|SKINVARS_GUNSLINGER,
		weight = 150,
		shields = 3,
		special = CBW_Battle.Action.BombThrow,
		guard_frame = 1
	}
end)