//instashield v7
//by hazepastel

if (VERSION == 202) and (SUBVERSION < 13)
    local function blackout(v)
        v.drawFill()
        v.drawString(160, 100, ("this mod requires version 2.2.13 or higher"),V_REDMAP, "center")
    end
    hud.add(blackout, "title")
    hud.add(blackout, "game")
    error("Your copy of 2.2 is obsolete and not compatible with this mod. Please update to version 2.2.13 or higher.", 0)
end

local function SafeFreeslot(...)
    for _, item in ipairs({...})
        if rawget(_G, item) == nil
            freeslot(item)
        end
    end
end

SafeFreeslot(
	"MT_SWAPINSTASHIELD",
	"S_SWAPINSTASHIELD",
	"S_SWAPINSTASHIELD1A",
	"S_SWAPINSTASHIELD1B",
	"S_SWAPINSTASHIELD2A",
	"S_SWAPINSTASHIELD2B",
	"S_SWAPINSTASHIELD3A",
	"S_SWAPINSTASHIELD3B",
	"S_SWAPINSTASHIELD4A",
	"S_SWAPINSTASHIELD4B",
	"S_SWAPINSTASHIELD5A",
	"S_SWAPINSTASHIELD5B",
	"S_SWAPINSTASHIELD6A",
	"S_SWAPINSTASHIELD6B",
	"SPR_SWTW"
)

mobjinfo[MT_SWAPINSTASHIELD] = {
	doomednum = -1,
	spawnhealth = 1,
	spawnstate = S_SWAPINSTASHIELD,
	radius = 48*FRACUNIT,
	height = 48*FRACUNIT,
	flags = MF_NOGRAVITY|MF_NOBLOCKMAP|MF_NOCLIPHEIGHT
}

states[S_SWAPINSTASHIELD] = {SPR_NULL, 0, 1, A_CapeChase, 0, 0, S_SWAPINSTASHIELD1A}
states[S_SWAPINSTASHIELD1A] = {SPR_SWTW, 0|FF_FULLBRIGHT, 1, A_CapeChase, 0, 0, S_SWAPINSTASHIELD1B}
states[S_SWAPINSTASHIELD1B] = {SPR_NULL, 0, 1, A_CapeChase, 0, 0, S_SWAPINSTASHIELD2A}
states[S_SWAPINSTASHIELD2A] = {SPR_SWTW, 1|FF_FULLBRIGHT, 1, A_CapeChase, 0, 0, S_SWAPINSTASHIELD2B}
states[S_SWAPINSTASHIELD2B] = {SPR_NULL, 0, 1, A_CapeChase, 0, 0, S_SWAPINSTASHIELD3A}
states[S_SWAPINSTASHIELD3A] = {SPR_SWTW, 2|FF_FULLBRIGHT, 1, A_CapeChase, 0, 0, S_SWAPINSTASHIELD3B}
states[S_SWAPINSTASHIELD3B] = {SPR_NULL, 0, 1, A_CapeChase, 0, 0, S_SWAPINSTASHIELD4A}
states[S_SWAPINSTASHIELD4A] = {SPR_SWTW, 3|FF_FULLBRIGHT, 1, A_CapeChase, 0, 0, S_SWAPINSTASHIELD4B}
states[S_SWAPINSTASHIELD4B] = {SPR_NULL, 0, 1, A_CapeChase, 0, 0, S_SWAPINSTASHIELD5A}
states[S_SWAPINSTASHIELD5A] = {SPR_SWTW, 4|FF_FULLBRIGHT, 1, A_CapeChase, 0, 0, S_SWAPINSTASHIELD5B}
states[S_SWAPINSTASHIELD5B] = {SPR_NULL, 0, 1, A_CapeChase, 0, 0, S_SWAPINSTASHIELD6A}
states[S_SWAPINSTASHIELD6A] = {SPR_SWTW, 5|FF_FULLBRIGHT, 1, A_CapeChase, 0, 0, S_SWAPINSTASHIELD6B}
states[S_SWAPINSTASHIELD6B] = {SPR_NULL, 0, 1, A_CapeChase, 0, 0, S_NULL}

addHook("JumpSpinSpecial", function(player)
	if (player.mo.skin == "kaydensbag") and not (player.glidetime) and not (player.pflags & (PF_SHIELDABILITY|PF_SPINDOWN)) and not player.powers[pw_super]
		S_StartSound(player.mo, sfx_s3k42)
		local instashield = P_SpawnMobjFromMobj(player.mo, 0, 0, 0, MT_SWAPINSTASHIELD)
		instashield.target = player.mo
		instashield.spritexscale = $*3/2
		instashield.spriteyscale = $*3/2
		if not (player.mo.colorized)
			instashield.flash = true
		end
		player.mo.tracer = instashield
		player.pflags = $ & ~PF_SPINNING
		player.glidetime = 5
		player.powers[pw_strong] = $|STR_GUARD
		player.shieldscale = 0
		return true
	end
end)

addHook("MobjThinker", function(insta)
	if (insta.target)
		if (insta.target.state != S_PLAY_JUMP)
			if insta.flash
				insta.target.colorized = false
			end
			pcall(P_RemoveMobj, insta)
			return
		end
		if insta.flash
			if (insta.sprite == SPR_NULL)
				insta.target.colorized = false
			else
				insta.target.colorized = true
				insta.target.frame = ($ & FF_FRAMEMASK)|FF_FULLBRIGHT
			end
		end
	end
end, MT_SWAPINSTASHIELD)

addHook("MobjRemoved", function(insta)
	if not (insta.target) or not (insta.target.player) return nil end
	insta.target.player.shieldscale = skins[insta.target.skin].shieldscale
	insta.target.player.powers[pw_strong] = $ & ~STR_GUARD
end, MT_SWAPINSTASHIELD)

addHook("MobjMoveCollide", function(insta, mobj)
	if not (insta.z-FixedMul(24*FRACUNIT, insta.scale) > mobj.z+mobj.height) and not(mobj.z-FixedMul(24*FRACUNIT, insta.scale) > insta.z+insta.height)
		if (mobj.flags & MF_SPECIAL) and mobj.valid and not (mobj.flags & MF_PAIN)
			P_TouchSpecialThing(mobj, insta.target, false)
			return false
		end
		if (mobj.flags & MF_MONITOR) and (mobj.health)
			if (P_MobjFlip(insta.target)*insta.target.momz) < 0
				insta.target.momz = -$
			end
			P_KillMobj(mobj, insta, insta.target)
			return false
		end
		if (mobj.flags & MF_PUSHABLE) and (mobj.flags & MF_SHOOTABLE)
			P_KillMobj(mobj, insta, insta.target)
			return false
		end
	end
end, MT_SWAPINSTASHIELD)
