//Rafael's Extra Stuff
freeslot ("S_PLAY_RAFACHILL", "SPR2_CHLL", "MT_RAFACHILL", "S_PLAY_RAFASQUAT", "SPR2_SQAT", "MT_RAFASQUAT", "sfx_arg1")

states[S_PLAY_RAFACHILL] = {SPR_PLAY, SPR2_CHLL, 5, nil, 0, 0, S_PLAY_RAFACHILL}
states[S_PLAY_RAFASQUAT] = {SPR_PLAY, SPR2_SQAT, 5, nil, 0, 0, S_PLAY_RAFASQUAT}
sfxinfo[sfx_arg1].caption = "Secret Message"

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

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

//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_DEAD
		or player.mo.state == S_PLAY_DEAD
			player.rafaeltosstapready = false
			player.rafaeltosstapping = false
			P_RestoreMusic(player)
		end
		
		//Detect if we're tapping Custom 1
		if not (player.cmd.buttons & BT_CUSTOM1)
		or not (P_IsObjectOnGround(player.mo)) 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_DEAD
		or player.mo.state == S_PLAY_DEAD
			player.rafaelcustomtapready = false
			player.rafaelcustomtapping = false
			player.rafasquat = false
			P_RestoreMusic(player)
		end
		
		//Chill
        if player.powers[pw_carry] == CA_NONE
            if player.rafachill
            and not (player.hugging)
                player.panim = PA_DASH
				player.rafaelcustomtapping = false
				player.rafaelcustomtapready = false
				if player.mo.state == S_PLAY_RAFACHILL
					if player.playerstate != PST_LIVE
						player.mo.state = S_PLAY_DEAD
					end
				end
                if player.rafaeltosstapping
                    if player.mo.state == S_PLAY_RAFACHILL
                        player.mo.state = S_PLAY_FALL
                    end
                    player.rafachill = false
                    P_RestoreMusic(player)
                else
                    if player.mo.state != S_PLAY_RAFACHILL
                        player.mo.state = S_PLAY_RAFACHILL
                    end
                    player.pflags = $|PF_FULLSTASIS
                end
            elseif player.rafaeltosstapping
                player.rafachill = true
				if (player.powers[pw_super])
                P_PlayJingleMusic(player, "SCHILL", nil, true, JT_OTHER)
			elseif not player.rafasquat
				P_PlayJingleMusic(player, "CHILL", nil, true, JT_OTHER)
            end
        end
		if player.rafasquat
			player.rafachill = false
		end
		
		//Squat (Which is bassicaly a copy of the chill script :P)
		if player.powers[pw_carry] == CA_NONE
            if player.rafasquat
            and not (player.hugging)
                player.panim = PA_DASH
				player.rafaeltosstapping = false
				player.rafaeltosstapready = false
				if player.mo.state == S_PLAY_RAFASQUAT
					if player.playerstate != PST_LIVE
						player.mo.state = S_PLAY_DEAD
					end
				end
                if player.rafaelcustomtapping
                    if player.mo.state == S_PLAY_RAFASQUAT
                        player.mo.state = S_PLAY_STND
                    end
                    player.rafasquat = false
                    P_RestoreMusic(player)
                else
                    if player.mo.state != S_PLAY_RAFASQUAT
                        player.mo.state = S_PLAY_RAFASQUAT
                    end
                    player.pflags = $|PF_FULLSTASIS
				end
            elseif player.rafaelcustomtapping
					player.rafasquat = true
					if not player.rafachill
						P_PlayJingleMusic(player, "SQUAT", nil, true, JT_OTHER)
					end
				end
			end
		end
	end
end)

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

addHook("ShouldJingleContinue", function(player, musname)
	if true return end
	if musname != "RAFASQUAT" return end
	if player and player.mo and player.mo.skin == "rafael" and player.rafasquat
		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.rafachill = false
		player.rafasquat = false
    end
end)

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

addHook("PlayerThink", function(player)
	if (player.mo.skin ~= "rafael")
	or not (cv_extra.value)
		return
	end
	if not player.rafachill
		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_FLINGRING)
		explosion.scale = enemy.scale/2,5
		explosion.state = S_RING
		explosion.target = player.mo
		explosion.nodmg = true
	end
end)

//Secret  message :o
print("Hey! It's FinnishGameFan here! If you are reading this, you're a dataminer, or then just checkin' the console! Have fun playing with Rafael!")

//Commands

-- Spin Jump
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)

-- Music Remade Shoes Theme
local cv_extra = CV_RegisterVar({"rafa_shoes", "Off", CV_NETVAR, CV_OnOff})

addHook("ThinkFrame", do
	for player in players.iterate
	if (player.mo.skin ~= "rafael")
	or not (cv_extra.value)
		return
	end
	if (player.mo.valid) and (player.mo.skin == "rafael")
		if player.powers[pw_sneakers] then
			S_ChangeMusic("RSHOES", true, player)
		elseif not player.exiting
			P_RestoreMusic(player)
		end
	end
end
end)

-- Secret Message
local function mvdsecret(player)
	CONS_Printf(player, "[Congratulations, player. this pre-recorded congratulations message has been added to congratulate you for revealing the secret message. You have been awarded w/a particularly added secret release date. Deploying release date:] [Saturday, May 25th, 2022 -- 12:00 AM (UTC)]")
	S_StartSound(player.mo, sfx_arg1)
end

COM_AddCommand("arg", mvdsecret)

//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_WINS
			player.mo.state = S_PLAY_WINS
		end
		player.mo.momx = 0 player.mo.momy = 0
	end
end)

//Footsteps support
if not PlayerAnimInfo
	rawset(_G, "PlayerAnimInfo", {})
end

PlayerAnimInfo["rafael"] = {
		["runFrames"] = {0, 2},
		["dashFrames"] = {-1},
		["walkFrames"] = {3, 7},
		["waitFrames"] = {0},
		["milnekickFrames"] = {0, 4},
		["run"] = true,
		["dash"] = false,
		["walk"] = true,
		["idle"] = true,
		["wait"] = true,
		["milnekick"] = true,
		["superRun"] = false,
		["superDash"] = false,
		["superWalk"] = true,
		["superIdle"] = true,
		["superWait"] = false
}

//Kirby Support

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

kirbyabilitytable["rafaelre"] = 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)