//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 player.mo and player.mo.valid and player.mo.skin == "rafael"
		if srb2p return end
		if player.chilltoggle == 0 return end
		if player.mo and player.mo.valid and (player.mo.milnejoin and player.mo.milnejoin.valid) and player.chilltoggle == 3 return end
		
		//Detect when we press the chill key
		if not ((player.cmd.buttons & BT_TOSSFLAG and player.chilltoggle == 1) or (player.cmd.buttons & BT_CUSTOM1 and player.chilltoggle == 2) or (player.cmd.buttons & BT_CUSTOM3 and player.chilltoggle == 3))
			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 the squat key
		if not ((player.cmd.buttons & BT_TOSSFLAG and player.squattoggle == 1) or (player.cmd.buttons & BT_CUSTOM1 and player.squattoggle == 2) or (player.cmd.buttons & BT_CUSTOM3 and player.squattoggle == 3))
		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)
//secret-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 remadeshoes = CV_RegisterVar({"rafa_shoes", "Off", CV_NETVAR, CV_OnOff})

addHook("PlayerThink", function(player)
	if (player.mo.skin ~= "rafael")
	or not (remadeshoes.value)
		return
	end
	if player.powers[pw_sneakers] then
		S_ChangeMusic("RSHOES", true, player)
	else
		P_RestoreMusic(player)
	end
end)

-- Dancing keys
addHook("PlayerThink", function(player)
	if player.chilltoggle == nil
		player.chilltoggle = 1
		player.squattoggle = 2
	end
end)

COM_AddCommand("rafa_chill", function(player, arg)
	if not (player.mo and player.mo.valid) then CONS_Printf(player, "You must be in a level to use this.") return end
	if arg
		if arg == "tossflag"
			player.chilltoggle = 1
			CONS_Printf(player, "Tossflag is now set as the chill button.")
		end
		if arg == "button1" or arg == "custom1"
			player.chilltoggle = 2
			CONS_Printf(player, "Custom button 1 is now set as the chill button.")
		end		
		if arg == "button3" or arg == "custom3" 
			player.chilltoggle = 3
			CONS_Printf(player, "Custom button 3 is now set as the chill button.")
		end
		if arg == "off"
			player.chilltoggle = 0
			CONS_Printf(player, "Rafael can no longer chill.") //sad :(
		end
	else
		CONS_Printf(player,"rafa_chill <button>: Change which button will make you chill as Rafael. [custom1, custom3, tossflag, off]")
		if player.chilltoggle == 0
			CONS_Printf(player,"You currently have chilling toggled off.")
		elseif player.chilltoggle == 1
			CONS_Printf(player,"Your chill button is currently set to Tossflag.")
		elseif player.chilltoggle == 2
			CONS_Printf(player,"Your chill button is currently set to Custom Button 1.")
		elseif player.chilltoggle == 3
			CONS_Printf(player,"Your chill button is currently set to Custom Button 3.")
		end
	end
end)

COM_AddCommand("rafa_squat", function(player, arg)
	if not (player.mo and player.mo.valid) then CONS_Printf(player, "You must be in a level to use this.") return end
	if arg
		if arg == "tossflag"
			player.squattoggle = 1
			CONS_Printf(player, "Tossflag is now set as the squat button.")
		end
		if arg == "button1" or arg == "custom1"
			player.squattoggle = 2
			CONS_Printf(player, "Custom button 1 is now set as the squat button.")
		end		
		if arg == "button3" or arg == "custom3" 
			player.squattoggle = 3
			CONS_Printf(player, "Custom button 3 is now set as the squat button.")
		end
		if arg == "off"
			player.squattoggle = 0
			CONS_Printf(player, "Rafael can no longer squat.") //sad :(
		end
	else
		CONS_Printf(player,"rafa_squat <button>: Change which button will make you squat as Rafael. [custom1, custom3, tossflag, off]")
		if player.squattoggle == 0
			CONS_Printf(player,"You currently have squating toggled off.")
		elseif player.squattoggle == 1
			CONS_Printf(player,"Your squat button is currently set to Tossflag.")
		elseif player.squattoggle == 2
			CONS_Printf(player,"Your squat button is currently set to Custom Button 1.")
		elseif player.squattoggle == 3
			CONS_Printf(player,"Your squat button is currently set to Custom Button 3.")
		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
}

//Cosmetics Support
addHook("PlayerThink", function(player)
	if Cosmetics
		Cosmetics.SkinOffsets["rafael"] = 4
	end
end)

//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 = 1,
		special = CBW_Battle.Action.BombThrow,
		guard_frame = 1
	}
end)