//roaring knight

freeslot("SPR_RORK",
		 "S_ROARK1",
		 "S_ROARK2",
		 "S_ROARK3",
		 "S_ROARK4",
		 "S_ROARK5",
		 "S_ROARK6",
		 "S_ROARK7",
		 "S_ROARK8",
		 "sfx_ssxt71",
		 "sfx_ssxt72")

states[S_ROARK1] = {
	sprite = SPR_RORK,
	frame = A,
	tics = 6,
	nextstate = S_ROARK2
}

states[S_ROARK2] = {
	sprite = SPR_RORK,
	frame = B,
	tics = 6,
	nextstate = S_ROARK3
}

states[S_ROARK3] = {
	sprite = SPR_RORK,
	frame = C,
	tics = 6,
	nextstate = S_ROARK4
}

states[S_ROARK4] = {
	sprite = SPR_RORK,
	frame = B,
	tics = 6,
	nextstate = S_ROARK5
}

states[S_ROARK5] = {
	sprite = SPR_RORK,
	frame = A,
	tics = 6,
	nextstate = S_ROARK6
}

states[S_ROARK6] = {
	sprite = SPR_RORK,
	frame = D,
	tics = 6,
	nextstate = S_ROARK7
}

states[S_ROARK7] = {
	sprite = SPR_RORK,
	frame = E,
	tics = 6,
	nextstate = S_ROARK8
}

states[S_ROARK8] = {
	sprite = SPR_RORK,
	frame = D,
	tics = 6,
	nextstate = S_ROARK1
}

local ROARKTRANSFORM_SFX = sfx_ssxt71 -- transform sound
local UNROARK_SFX = sfx_ssxt72 -- untransform sound
local SPRITE = SPR_RORK -- sprite
local STATE = S_ROARK1 -- state

///// Previous functions /////
local function ROARKifyPlayer(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 ROARK'd this player, why do it again?

    S_StartSound(mo, ROARKTRANSFORM_SFX)
    mo.roark = 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.state = STATE
        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 UnROARKify(mo)
    if not mo.roark then return end -- We already removed ROARK from this player, why do it again?

    S_StartSound(mo, UNROARK_SFX)
    mo.roark = 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.roark then
        ROARKifyPlayer(mo)
    end
end, "SSXTRARK")

addHook("LinedefExecute", function(line, mo) --Untransform via linedef execute
    if mo and mo.valid and mo.health and mo.player and mo.roark then
        UnROARKify(mo)
    end
end, "UNROARK")

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.roark 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*3/2
                mo.overlay.spritexscale = mo.spritexscale
                mo.overlay.spriteyscale = mo.spriteyscale
                mo.overlay.color = mo.color
            end
        elseif mo.overlay then
            UnROARKify(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.roark then return end

	UnROARKify(player.mo)
end)