//original by glaber
//Glaber's Badnik pack carried this tbh

freeslot(
    "MT_UNIDASU_BALL",
	"MT_UNIDASU",
	"S_UNDS_IDLE",
	"S_UNDS_RUN",
	"S_UNDS_COLOR",
	"S_UNDS_STND",
	"S_UNDS_COLOR2",
	"S_UNDS_CHASE",
	"S_UNDS_BALL1",
	"S_UNDS_BALL2",
	"S_UNDS_BALL3",
	"S_UNDS_BALLCOLOR",
	"S_UNDS_DUAL1",
	"S_UNDS_DUAL2",
	"S_UNDS_DUAL3",
	"S_UNDS_DUAL4",
	"S_UNDS_DUAL5",
	"S_UNDS_DUAL6",
	"MT_UNIUNI_BALL",
	"MT_UNIUNI",
	"S_UNI_IDLE",
	"S_UNI_BALL1",
	"S_UNI_BALL2",
	"S_UNI_BALL3",
	"S_UNI_BALLCOLOR",
	"S_UNI_DUAL1",
	"S_UNI_DUAL2",
	"S_UNI_DUAL3",
	"S_UNI_DUAL4",
	"S_UNI_DUAL5",
	"S_UNI_DUAL6",
	"SPR_UNDS"
)

local function CustomOrbieSpawn(mobj, mapthing)
    local offset
    
    if mapthing.z != 0
        offset = mapthing.z
    else
        offset = 12*FRACUNIT
    end
    
    if (mapthing.options & MTF_OBJECTFLIP)
        if offset != 0
            mobj.z = mobj.z - offset
        else
            mobj.z = mobj.ceilingz
        end
    else
        if offset != 0
            mobj.z = mobj.z + offset
        else
            mobj.z = mobj.floorz
        end
    end
    
end

addHook("MapThingSpawn", CustomOrbieSpawn, MT_UNIDASU)
addHook("MapThingSpawn", CustomOrbieSpawn, MT_UNIUNI)

//UNIDUS ball spawn code
--Code translated by glaber with help from chaoloveicemdboy, CEO of le sus ?, and Monster Iystyn
addHook("MobjSpawn", function(mobj)
	local ball
	// Spawn "damage" number of "painchance" spikeball mobjs
	// threshold is the distance they should keep from the MT_OUNIDUS (touching radius + ball painchance)
	for i = 0,mobj.info.damage
		
		ball = P_SpawnMobj(mobj.x, mobj.y, mobj.z, mobj.info.painchance)
		ball.destscale = mobj.scale
		P_SetScale(ball, mobj.scale)
		ball.target = mobj
		ball.movedir = FixedAngle(FixedMul(FixedDiv(i<<FRACBITS, mobj.info.damage<<FRACBITS), 360<<FRACBITS))
		ball.threshold = ball.radius + mobj.radius + FixedMul(ball.info.painchance, ball.scale)

		local var1, var2 = states[ball.state].var1, states[ball.state].var2
		states[ball.state].action(ball, var1, var2)
	end
end, MT_UNIDASU)

addHook("MobjSpawn", function(mobj)
	local ball
	// Spawn "damage" number of "painchance" spikeball mobjs
	// threshold is the distance they should keep from the MT_OUNIDUS (touching radius + ball painchance)
	for i = 0,mobj.info.damage
		
		ball = P_SpawnMobj(mobj.x, mobj.y, mobj.z, mobj.info.painchance)
		ball.destscale = mobj.scale
		P_SetScale(ball, mobj.scale)
		ball.target = mobj
		ball.movedir = FixedAngle(FixedMul(FixedDiv(i<<FRACBITS, mobj.info.damage<<FRACBITS), 360<<FRACBITS))
		ball.threshold = ball.radius + mobj.radius + FixedMul(ball.info.painchance, ball.scale)

		local var1, var2 = states[ball.state].var1, states[ball.state].var2
		states[ball.state].action(ball, var1, var2)
	end
end, MT_UNIUNI)

//original by Glaber and MIDIman
local UNIDUS_TIMER = 104

addHook("MobjSpawn", function(mo)
	if not (mo and mo.valid) then return end
	mo.timer = UNIDUS_TIMER
end, MT_UNIUNI)

addHook("MobjThinker", function(mo)
	if not (mo and mo.valid) then return end
	if mo.state ~= S_UNI_IDLE then return end
	P_InstaThrust(mo, mo.angle, 2*mo.scale)
	if mo.timer
		mo.timer = $-1
	else
		mo.timer = UNIDUS_TIMER
		mo.angle = $ + ANGLE_180
	end
end, MT_UNIUNI)

//Unidasu
mobjinfo[MT_UNIDASU] = {
		//$Name "Unidasu"
		//$Sprite UNDSA1
		//$Category Retro Enemies
        doomednum = 4092,
        spawnstate = S_UNDS_IDLE,
        spawnhealth = 1,
        seestate = S_UNDS_RUN,
        seesound = sfx_None,
        reactiontime = 32,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = MT_UNIDASU_BALL,
        painsound = sfx_None,
		missilestate = S_NULL,
        deathstate = S_XPLD_FLICKY,
        xdeathstate = S_NULL,
        deathsound = sfx_pop,
        speed = 2,
        radius = 18*FRACUNIT,
        height = 36*FRACUNIT,
        dispoffset = 0,
        mass = 4*FRACUNIT,
        damage = 5,
        activesound = sfx_None,
		raisestate = S_NULL,
        flags = MF_ENEMY|MF_SPECIAL|MF_SHOOTABLE|MF_NOGRAVITY
}

//44 for green
states[S_UNDS_IDLE] = {SPR_UNDS, A, 1, A_DualAction, S_UNDS_COLOR, S_UNDS_STND, S_UNDS_IDLE}
states[S_UNDS_COLOR] = {SPR_UNDS, A, 1, A_ChangeColorAbsolute, 0, 54, S_UNDS_STND}
states[S_UNDS_STND] = {SPR_UNDS, A, 1, A_Look, (768<<16)+0, 0, S_UNDS_COLOR}
states[S_UNDS_RUN] = {SPR_UNDS, B, 1, A_DualAction, S_UNDS_CHASE, S_UNDS_COLOR2, S_UNDS_RUN}
states[S_UNDS_COLOR2] = {SPR_UNDS, B, 1, A_ChangeColorAbsolute, 0, 23, S_UNDS_CHASE}
states[S_UNDS_CHASE] = {SPR_UNDS, B, 1, A_Chase, 0, 0, S_UNDS_CHASE}

//UNIDASU SPIKEBALL
mobjinfo[MT_UNIDASU_BALL] = {
	doomednum = -1,
	spawnstate = S_UNDS_DUAL1,
	spawnhealth = 1,
	seesound = sfx_None,
	reactiontime = 1,
	painchance = 20*FRACUNIT,
	speed = 2*FRACUNIT,
	radius = 13*FRACUNIT,
	height = 26*FRACUNIT,
	damage = 8*FRACUNIT,
	mass = DMG_SPIKE,
	flags = MF_PAIN|MF_NOGRAVITY|MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOCLIPTHING
}

states[S_UNDS_DUAL1] = {SPR_UNDS, C, 1, A_DualAction, S_UNDS_BALL1, S_UNDS_BALLCOLOR, S_UNDS_DUAL2}
states[S_UNDS_DUAL2] = {SPR_UNDS, C, 1, A_DualAction, S_UNDS_BALL1, S_UNDS_BALLCOLOR, S_UNDS_DUAL3}
states[S_UNDS_DUAL3] = {SPR_UNDS, D, 1, A_DualAction, S_UNDS_BALL2, S_UNDS_BALLCOLOR, S_UNDS_DUAL4}
states[S_UNDS_DUAL4] = {SPR_UNDS, D, 1, A_DualAction, S_UNDS_BALL2, S_UNDS_BALLCOLOR, S_UNDS_DUAL5}
states[S_UNDS_DUAL5] = {SPR_UNDS, E, 1, A_DualAction, S_UNDS_BALL3, S_UNDS_BALLCOLOR, S_UNDS_DUAL6}
states[S_UNDS_DUAL6] = {SPR_UNDS, E, 1, A_DualAction, S_UNDS_BALL3, S_UNDS_BALLCOLOR, S_UNDS_DUAL1}
states[S_UNDS_BALL1] = {SPR_UNDS, C, 1, A_UnidusBall, 1, 0, S_UNDS_BALL2}
states[S_UNDS_BALL2] = {SPR_UNDS, D, 1, A_UnidusBall, 1, 0, S_UNDS_BALL3}
states[S_UNDS_BALL3] = {SPR_UNDS, E, 1, A_UnidusBall, 1, 0, S_UNDS_BALL1}

states[S_UNDS_BALLCOLOR] = {SPR_UNDS, C, 1, A_ChangeColorAbsolute, 0, 24, S_UNDS_BALLCOLOR}

//UNIUNI
mobjinfo[MT_UNIUNI] = {
		//$Name "Uni-Uni"
		//$Sprite UNDSA1
		//$Category Retro Enemies
        doomednum = 4094,
        spawnstate = S_UNI_IDLE,
        spawnhealth = 1,
        seestate = S_NULL,
        seesound = sfx_None,
        reactiontime = 32,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = MT_UNIUNI_BALL,
        painsound = sfx_None,
		missilestate = S_NULL,
        deathstate = S_XPLD_FLICKY,
        xdeathstate = S_NULL,
        deathsound = sfx_pop,
        speed = 2,
        radius = 18*FRACUNIT,
        height = 36*FRACUNIT,
        dispoffset = 0,
        mass = 4*FRACUNIT,
        damage = 5,
        activesound = sfx_None,
		raisestate = S_NULL,
        flags = MF_ENEMY|MF_SPECIAL|MF_SHOOTABLE|MF_NOGRAVITY
}

states[S_UNI_IDLE] = {SPR_UNDS, A, 1, A_ChangeColorAbsolute, 0, 43, S_UNI_IDLE}

//UNIUNI SPIKEBALL because it shouldnt throw
mobjinfo[MT_UNIUNI_BALL] = {
	doomednum = -1,
	spawnstate = S_UNI_DUAL1,
	spawnhealth = 1,
	seesound = sfx_None,
	reactiontime = 1,
	painchance = 20*FRACUNIT,
	speed = 6*FRACUNIT,
	radius = 13*FRACUNIT,
	height = 26*FRACUNIT,
	damage = 8*FRACUNIT,
	mass = DMG_SPIKE,
	flags = MF_PAIN|MF_NOGRAVITY
}

states[S_UNI_DUAL1] = {SPR_UNDS, C, 1, A_DualAction, S_UNI_BALL1, S_UNI_BALLCOLOR, S_UNI_DUAL2}
states[S_UNI_DUAL2] = {SPR_UNDS, C, 1, A_DualAction, S_UNI_BALL1, S_UNI_BALLCOLOR, S_UNI_DUAL3}
states[S_UNI_DUAL3] = {SPR_UNDS, D, 1, A_DualAction, S_UNI_BALL2, S_UNI_BALLCOLOR, S_UNI_DUAL4}
states[S_UNI_DUAL4] = {SPR_UNDS, D, 1, A_DualAction, S_UNI_BALL2, S_UNI_BALLCOLOR, S_UNI_DUAL5}
states[S_UNI_DUAL5] = {SPR_UNDS, E, 1, A_DualAction, S_UNI_BALL3, S_UNI_BALLCOLOR, S_UNI_DUAL6}
states[S_UNI_DUAL6] = {SPR_UNDS, E, 1, A_DualAction, S_UNI_BALL3, S_UNI_BALLCOLOR, S_UNI_DUAL1}
states[S_UNI_BALL1] = {SPR_UNDS, C, 1, A_UnidusBall, 0, 0, S_UNI_BALL2}
states[S_UNI_BALL2] = {SPR_UNDS, D, 1, A_UnidusBall, 0, 0, S_UNI_BALL3}
states[S_UNI_BALL3] = {SPR_UNDS, E, 1, A_UnidusBall, 0, 0, S_UNI_BALL1}

states[S_UNI_BALLCOLOR] = {SPR_UNI, C, 1, A_ChangeColorAbsolute, 0, 36, S_UNI_BALLCOLOR}