freeslot("sfx_taunt","sfx_parry","SPR2_TUNT","S_NOTSBAGTAUNT")
addHook("PlayerThink", function(player) // Make sure that it runs
	if (player and player.mo and player.mo.valid) then
		if (player.mo.skin == "notsbag")
			if player.playerstate == PST_DEAD then
				player.dashmode = 0
			end
		end
	end
end)

//air walk remover, made by Kirb, reusable

addHook("PlayerThink", function(player) //main hook //https://wiki.srb2.org/wiki/Lua/Hooks

if not(player.mo) then return end //do not execute the script if the player isn't in game, because it could produce errors (i don't think that there's a wiki page that explains this, after all it isn't that complex)

if not(player.mo.skin == "notsbag") then return end //do not execute the script if the player isn't sonic, optional

if not(P_IsObjectOnGround(player.mo)) //if the player isn't on ground.. //https://wiki.srb2.org/wiki/Lua/Functions
and (player.mo.state == S_PLAY_RUN) //and he plays the walk or run animation.. //https://wiki.srb2.org/wiki/List_of_states
and not player.powers[pw_carry] //and not on a rollout ball --https://wiki.srb2.org/wiki/A_CustomPower
player.mo.state = S_PLAY_FALL //it plays the fall animation //https://wiki.srb2.org/wiki/List_of_states
end
end)

addHook("PlayerThink", function(player)
    if (player and player.mo and player.mo.valid) and player.mo.skin == "notsbag" then
		if (player.powers[pw_justsprung] or player.mo.eflags & MFE_SPRUNG) and player.mo.state != S_PLAY_SPRING then 
			player.dashmode = DASHMODE_MAX
		end
    end
end)

addHook("PlayerThink", function(player)
	if (player and player.mo and player.mo.valid) and player.mo.skin == "notsbag" then
		if player.dashmode >= 3*TICRATE
			player.powers[pw_strong] = STR_DASH|STR_ANIM|STR_PUNCH|STR_STOMP|STR_WALL|STR_SPIKE
		end
	end
end)

addHook("ThinkFrame", do
	for player in players.iterate
		if (player and player.mo and player.mo.valid)
			if (player.mo.skin == "notsbag")
				if player.dashmode >= 3*TICRATE
				player.nomoredashmodeflash = P_SpawnMobjFromMobj(player.mo,0,0,0,MT_OVERLAY)
				player.nomoredashmodeflash.target = player.mo
				player.nomoredashmodeflash.skin = player.mo.skin
				player.nomoredashmodeflash.sprite = player.mo.sprite
				player.nomoredashmodeflash.spritexscale = player.mo.spritexscale
				player.nomoredashmodeflash.spriteyscale = player.mo.spriteyscale
				player.nomoredashmodeflash.sprite2 = player.mo.sprite2
				player.nomoredashmodeflash.color = player.mo.color
				player.nomoredashmodeflash.angle = player.drawangle
				player.nomoredashmodeflash.info.dispoffset = 1
				player.nomoredashmodeflash.frame = player.mo.frame
				player.nomoredashmodeflash.fuse = 1
				player.nomoredashmodeflash.rollangle = player.mo.rollangle
				player.nomoredashmodeflash.roll = player.mo.roll
				player.nomoredashmodeflash.pitch = player.mo.pitch
				end
			end
		end
	end
end)

local function SoapyCollisions(mo1, mo2) //originally used for the enemy effect hook because it wouldnt show sometimes
	local buffer = 48*FRACUNIT
	local maxdist = mo1.radius + mo2.radius + buffer

	if abs(mo1.x - mo2.x) > maxdist or abs(mo1.y - mo2.y) > maxdist
		return false
	end

	if mo1.z > mo2.z + mo2.height + buffer or mo1.z + mo1.height + buffer < mo2.z
		return false
	end

	return true
end

addHook("MobjCollide", function(door, stupid)
    if not (door and door.valid and stupid and stupid.valid) then return end
	
    if stupid.player and stupid.skin == "notsbag" 
    and (stupid.player.notsoap_mach4ithink or stupid.player.powers[pw_super])
		if SoapyCollisions(door, stupid)
			for i = 1, 10 do
				local effect = P_SpawnMobj(door.x, door.y, door.z + P_RandomRange(5, 55)*FRACUNIT, MT_WOODDEBRIS)
				effect.angle = P_RandomRange(0, 359)*ANG1
				effect.scale = FixedDiv(P_RandomRange(60, 180) * FRACUNIT, 100 * FRACUNIT)

				P_InstaThrust(effect, effect.angle, P_RandomRange(15, 35)*FRACUNIT)
				effect.momz = P_RandomRange(10, 32)*FRACUNIT
			end
			P_KillMobj(door, stupid, stupid)
		end
	end
end, MT_SALOONDOOR)

addHook("MobjCollide", function(door, stupid)
    if not (door and door.valid and stupid and stupid.valid) then return end

    if stupid.player and stupid.skin == "notsbag" 
    and (stupid.player.notsoap_mach4ithink or stupid.player.powers[pw_super])
		if SoapyCollisions(door, stupid)
			S_StartSound(stupid, sfx_bowl)
			S_StartSound(stupid, sfx_brakrx)
			for i = 1, 10 do
				local dust = P_SpawnMobj(door.x, door.y, door.z + 20*FRACUNIT, MT_SPINDUST)
				dust.angle = P_RandomRange(0, 359)*ANG1
				dust.scale = FRACUNIT * 3/2
				P_InstaThrust(dust, dust.angle, P_RandomRange(15, 35)*FRACUNIT)
				dust.momz = P_RandomRange(5, 18)*FRACUNIT
			end
			P_KillMobj(door, stupid, stupid)
		end
	end
end, MT_SALOONDOORCENTER)

local spr2 = SPR2_TUNT -- PLEASE freeslot whatever sprite2 you use here

states[S_NOTSBAGTAUNT] = {
	sprite = SPR_PLAY,
	frame = spr2,
	tics = -1,
	nextstate = S_NOTSBAGTAUNT
}

local function canTaunt(p)
	if P_PlayerInPain(p)
	or p.playerstate == PST_DEAD
	or (p.pflags & PF_SPINNING)
	or p.mo.state == S_PLAY_ROLL
	or p.mo.state == S_PLAY_GLIDE
	or p.mo.state == S_PLAY_GLIDE_LANDING
	or p.mo.state == S_PLAY_CLING
	or p.mo.state == S_PLAY_CLIMB
	or p.mo.state == S_PLAY_NOTSBAG_BLASTTB1
	or p.mo.peelouttimer
	or (p.pflags & PF_SLIDING)
	or (p.powers[pw_carry] == CR_NIGHTSMODE)
	//or p.firehurt
	//or p.electrichurt
	or (p.powers[pw_carry])
		return false
	end
	return true
end

addHook("PlayerSpawn", function(p)
	p.notsbagtaunting = false
end)

addHook("PlayerThink", function(p)
	if not (p.mo and p.mo.valid)
	or not(p.mo.skin == "notsbag")
		if p.notsbagtaunting
			p.notsbagtaunting = false
		end
		return
	end
	
	if (p.cmd.buttons & BT_TOSSFLAG)
		p.notsbagtauntheld = $+1
	else
		p.notsbagtauntheld = 0
	end
	
	if p.notsbagtauntheld == 1
	and not p.notsbagtaunting
	and canTaunt(p)
		p.notsbagtaunt = {
			time = 0,
			pflags = p.pflags,
			momx = p.mo.momx,
			momy = p.mo.momy,
			momz = p.mo.momz,
			dashmode = p.dashmode,
			state = p.mo.state,
			anim_dur = p.mo.anim_duration,
			tics = p.mo.tics,
			frame = p.mo.frame,
			tframe = P_RandomRange(0, skins[p.mo.skin].sprites[spr2].numframes-1)
		}
		p.notsbagtaunting = true
		S_StartSound(p.mo, sfx_taunt) -- replac sfx_thok with sound you want play :)
	end
	
	if p.notsbagtaunting
		if not canTaunt(p)
			p.notsbagtaunting = false
			return
		end
		S_StopSoundByID(p.mo, sfx_kc65)
		local t = p.notsbagtaunt
		p.pflags = $1|PF_FULLSTASIS
		p.mo.momx = 0
		p.mo.momy = 0
		p.mo.momz = 0
		
		if p.mo.state ~= S_NOTSBAGTAUNT
			p.mo.state = S_NOTSBAGTAUNT
			p.mo.frame = t.tframe
		end
		
		t.time = $+1
		if t.time >= TICRATE/2 -- change this value for another time thing i dont know
			p.notsbagtaunting = false
			p.mo.momx = t.momx
			p.mo.momy = t.momy
			p.mo.momz = t.momz
			p.pflags = t.pflags
			p.dashmode = t.dashmode
			P_MovePlayer(p)
			p.mo.state = t.state
			p.mo.anim_duration = t.anim_dur
			p.mo.tics = t.tics
			p.mo.frame = t.frame
			return
		end
	end
end)

// addHook('ShouldDamage', function(target, inflictor, source, _, dmg)
// 	if not target.player then return end
// 	local player = target.player
	
// 	if not(player.mo.skin == "notsbag") then return end
// 	if not (inflictor and inflictor.valid) then return end

// 	if player.notsbagtaunting and (dmg ~= DMG_FIRE or mapheaderinfo[gamemap].bonustype == 1) then
// 		if (inflictor.flags & MF_MISSILE) then
// 			inflictor.target = target
// 			inflictor.momx = -$
// 			inflictor.momy = -$
// 			inflictor.momz = -$
// 			player.parrytime = 0
// 		else
// 			P_DamageMobj(inflictor, target, target)
// 			player.parrytime = 0
// 		end
// 		P_InstaThrust(player.mo, player.drawangle, FixedMul(-8*FU, player.mo.scale))
// 		P_MovePlayer(player)
// 		player.notsbagtaunting = false
// 		player.mo.state = L_Choose(S_PLAY_NOTSBAG_PARRY1, S_PLAY_NOTSBAG_PARRY2, S_PLAY_NOTSBAG_PARRY3)
// 		S_StartSound(player.mo, sfx_parry)
// 		return false
// 	end
// end)

// addHook("PlayerThink", function(player)
// 	if player.mo.state == S_PLAY_NOTSBAG_PARRY1 or player.mo.state == S_PLAY_NOTSBAG_PARRY2 or player.mo.state == S_PLAY_NOTSBAG_PARRY3 then
// 		player.pflags = $|PF_FULLSTASIS
// 		player.parrytime = $+1
// 		if player.parrytime == 30 then
// 			player.pflags = $ & ~PF_FULLSTASIS
// 		end
// 	end
// end)