//weegee

freeslot(//"MT_SLWG",
		 "SPR_SLWG",
		 //"S_SLWG",
		 "sfx_ssxtra",
		 "sfx_ssxtr2")

//mobjinfo[MT_SLWG] = {
//	doomednum = 4090,
//	spawnstate = S_SLWG,
//	radius = 16*FRACUNIT,
//	height = 32*FRACUNIT,
//	flags = MF_NOCLIP,
//	--$Name lolzzzz
//	--$Sprite SLWGA0
//}

//states[S_SLWG] = {
//	sprite = SPR_SLWG,
//	frame = A,
//	tics = -1,
//	nextstate = S_SLWG
//}

sfxinfo[sfx_ssxtra] = {
        singular = false,
        priority = 64,
        flags = SF_X4AWAYSOUND
}
sfxinfo[sfx_ssxtra].caption = "SOMEONE JUST GOT WEEGEE'D!"

sfxinfo[sfx_ssxtr2] = {
        singular = false,
        priority = 64,
        flags = SF_X4AWAYSOUND
}
sfxinfo[sfx_ssxtr2].caption = "SOMEONE JUST GOT DEWEEGEE'D!"

local WEEGEETRANSFORM_SFX = sfx_ssxtra -- transform sound
local UNWEEGEE_SFX = sfx_ssxtr2 -- untransform sound
local SPRITE = SPR_SLWG -- sprite

///// Previous functions /////
local function WeegeeifyPlayer(mo)
    if mo.sflori then return end
	if mo.weegee then return end
	if mo.kubik then return end
	if mo.goomb then return end
	if mo.goomb2 then return end
	if mo.sanic then return end
	if mo.roark then return end 
	if mo.bear5 then return end
	if mo.flora then return end 
	if mo.fredd then return end -- We already weegee'd this player, why do it again?

    S_StartSound(mo, WEEGEETRANSFORM_SFX)
    mo.weegee = true
    mo.alpha = 0

    if not (mo.overlay and mo.overlay.valid) then
        mo.overlay = P_SpawnMobjFromMobj(mo, mo.x, mo.y, mo.z, MT_THOK)
        mo.overlay.sprite = SPRITE
        mo.overlay.frame = A
        mo.overlay.tics = -1
        mo.overlay.flags = MF_NOCLIPHEIGHT|MF_NOBLOCKMAP|MF_NOGRAVITY|MF_NOCLIP
        P_SetOrigin(mo.overlay, mo.x, mo.y, mo.z)
    end
end

local function Unweegeeify(mo)
    if not mo.weegee then return end -- We already removed weegee from this player, why do it again?

    S_StartSound(mo, UNWEEGEE_SFX)
    mo.weegee = false
    mo.alpha = FU

    if mo.overlay and mo.overlay.valid then
        P_RemoveMobj(mo.overlay)
    end
end

local function ZCollide(mo1, mo2) --Z Collision checks
	if mo1.z > mo2.height+mo2.z then return false end
	if mo2.z > mo1.height+mo1.z then return false end
	return true
end

//// Linedef Execute ////

addHook("LinedefExecute", function(line, mo) --Transform via linedef execute
    if mo.valid and mo.health and mo.player and not mo.weegee then
        WeegeeifyPlayer(mo)
    end
end, "SSXTRAWG")

addHook("LinedefExecute", function(line, mo) --Untransform via linedef execute
    if mo and mo.valid and mo.health and mo.player and mo.weegee then
        Unweegeeify(mo)
    end
end, "UNWEEGEE")

addHook("PostThinkFrame", function() --Weeegeeeeeee! Using PostThinkFrame to have exact consistency with the player.
    for p in players.iterate() do
        local mo = p.mo

		if not mo then continue end
		if not mo.valid then continue end
        if not mo.weegee then continue end

        -- Ugh since some custom characters gets fancy when setting a sprite
        -- we're using a separated object instead.
        if mo.health then
            if mo.overlay and mo.overlay.valid then
                P_MoveOrigin(mo.overlay, mo.x, mo.y, mo.z)
                mo.overlay.scale = mo.scale
                mo.overlay.spritexscale = mo.spritexscale
                mo.overlay.spriteyscale = mo.spriteyscale
                mo.overlay.color = mo.color
            end
        elseif mo.overlay then
            Unweegeeify(mo)
        end
    end
end)

addHook("TeamSwitch", function(player, team)
	if team then return end
	if not player.mo then return end
	if not player.mo.valid then return end
	if not player.mo.weegee then return end

	Unweegeeify(player.mo)
end)