freeslot("MT_GOHLA", "S_GOHLA_STND", "S_GOHLA_RUN", "MT_GOHLA_BALL", "S_GOHLA_BALL", "S_GOHLA_BALL2", "SPR_GOLA")

local function CustomFlameySpawn(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", CustomFlameySpawn, MT_GOHLA)

//Gohla ball spawn code
--Code translated by glaber with help from chaoloveicemdboy, CEO of le sus ?
addHook("MobjSpawn", function(mobj)
	local ball
	// Spawn "damage" number of "painchance" spikeball mobjs
	// threshold is the distance they should keep from the MT_GOHLA (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_GOHLA)


// Old Gohla
mobjinfo[MT_GOHLA] = {
		//$Name "Gohla"
		//$Sprite GOLAA1
		//$Category Genesis Enemies
        doomednum = 3111,
        spawnstate = S_GOHLA_STND,
        spawnhealth = 1,
        seestate = S_GOHLA_RUN,
        seesound = sfx_None,
        reactiontime = 32,
        attacksound = sfx_None,
        painstate = S_NULL,
        painchance = MT_GOHLA_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_GOHLA_STND] = {SPR_GOLA, A, 4, A_Look, 0, 0, S_GOHLA_STND}
states[S_GOHLA_RUN] = {SPR_GOLA, A, 1, A_Chase, 0, 0, S_GOHLA_RUN}

// GOHLA Fireball, code modified from Midiman's 2.1 eggmobile spike ball code.

mobjinfo[MT_GOHLA_BALL] = {
	doomednum = -1,
	spawnstate = S_GOHLA_BALL,
	spawnhealth = 1,
	seesound = sfx_None,
	reactiontime = 1,
	painchance = 20*FRACUNIT,
	speed = 2*FRACUNIT,
	radius = 13*FRACUNIT,
	height = 26*FRACUNIT,
	damage = 8*FRACUNIT,
	mass = DMG_FIRE,
	flags = MF_PAIN|MF_NOGRAVITY|MF_NOCLIP|MF_NOCLIPHEIGHT|MF_NOCLIPTHING
}

// Replace A_GOHLABall with a 2.1 variant, if it changes in the future
states[S_GOHLA_BALL] = {SPR_GOLA, B, 1, A_UnidusBall, 1, 0, S_GOHLA_BALL2}
states[S_GOHLA_BALL2] = {SPR_GOLA, C, 1, A_UnidusBall, 1, 0, S_GOHLA_BALL}