-- Spamton NEO

freeslot(
	"SPR_SPMT",
	"SFX_SPMLOL",
	"MT_SPAMTONNEO",
	"S_SPAMTONNEO_INIT1",
	"S_SPAMTONNEO_INIT2",
	"S_SPAMTONNEO_LOOK",
	"S_SPAMTONNEO_CHASE1",
	"S_SPAMTONNEO_CHASE2",
	"S_SPAMTONNEO_CHASE3",
	"S_SPAMTONNEO_CHASE4",
	"S_SPAMTONNEO_PAIN",
	"S_SPAMTONNEO_OVERTIME1",
	"S_SPAMTONNEO_OVERTIME2"
)

mobjinfo[MT_SPAMTONNEO] = {
	doomednum = 3363,
	spawnhealth = 9999,
	spawnstate = S_SPAMTONNEO_INIT1,
	seestate = S_SPAMTONNEO_CHASE1,
	painstate = S_SPAMTONNEO_PAIN,
	painsound = sfx_behurt,
	deathstate = S_XPLD1,
	deathsound = sfx_pop,
	radius = 32*FRACUNIT,
	height = 42*FRACUNIT,
	speed = 4,
	flags = MF_NOBLOCKMAP|MF_NOCLIPTHING
}

states[S_SPAMTONNEO_INIT1] = {SPR_NULL, A, 500, None, 0, 0, S_SPAMTONNEO_INIT2}
states[S_SPAMTONNEO_INIT2] = {SPR_SPMT, A, 10, None, 0, 0, S_SPAMTONNEO_LOOK}
states[S_SPAMTONNEO_LOOK] = {SPR_SPMT, A, 10, A_Look, 3000*FRACUNIT+1, 0, S_SPAMTONNEO_LOOK}
states[S_SPAMTONNEO_CHASE1] = {SPR_SPMT, A, 2, A_PlaySound, SFX_SMPLOL, 0, S_SPAMTONNEO_CHASE2}
states[S_SPAMTONNEO_CHASE2] = {SPR_SPMT, A, 30, None, 0, 0, S_SPAMTONNEO_CHASE3}
states[S_SPAMTONNEO_CHASE3] = {SPR_SPMT, B, 30, None, 0, 0, S_SPAMTONNEO_CHASE4}
states[S_SPAMTONNEO_CHASE4] = {SPR_SPMT, C, 30, None, 0, 0, S_SPAMTONNEO_CHASE2}
states[S_SPAMTONNEO_PAIN] = {SPR_SPMT, A, 10, A_PlaySound, SFX_SMPLOL, 0, S_SPAMTONNEO_CHASE1}
states[S_SPAMTONNEO_OVERTIME1] = {SPR_SPMT, D, 1, A_PlaySound, sfx_etexpl, 1, S_SPAMTONNEO_OVERTIME2}
states[S_SPAMTONNEO_OVERTIME2] = {SPR_SPMT, D, -1, None, 0, 0, S_SPAMTONNEO_OVERTIME2}

function A_SlideyChase(actor, var1, var2)
	if not (actor.target and actor.target.valid) then return end
	P_Thrust(actor, R_PointToAngle2(actor.x, actor.y, actor.target.x, actor.target.y), FixedMul(actor.info.speed*FRACUNIT/12, actor.scale))
	A_FaceTarget(actor, 0, 0)
end

local chasing
local reorienttimer
local looktimer

addHook("MobjSpawn", function(mo)
	mo.chasing = false
	mo.reorienttimer = 14
	mo.looktimer = 500
end, MT_SPAMTONNEO)

-- maintain'd for 2.4 by toaster
-- the level's mapobjectscale is FRACUNIT*32/100
-- the maximum sight distance was previously, 2000*FRACUNIT
-- for the same functional sight limit, it's mapobjectscale times 200,000 over 32, which is 6250*mapobjectscale
-- no I don't know why I'm putting effort into making this scale agnostic

addHook("MobjThinker", function(mo)
	-- Init
	if mo.state == S_SPAMTONNEO_INIT2 then
		mo.flags = MF_SPECIAL|MF_SHOOTABLE|MF_ENEMY|MF_SLIDEME|MF_PAIN
	elseif mo.state == S_SPAMTONNEO_CHASE1 then
		mo.chasing = true
	-- Keel over dead
	elseif mo.state == S_SPAMTONNEO_OVERTIME1 then
		mo.chasing = false
		mo.flags = MF_NOCLIPTHING|MF_SCENERY
		mo.momx = 0
		mo.momy = 0
	end

	if mo.chasing == true then
		if mo.state == S_SPAMTONNEO_PAIN then
			mo.reorienttimer = 14
		else
			mo.reorienttimer = $ - 1
			mo.looktimer = $ - 1
		end

		if (not (mo.target and mo.target.valid)) or (mo.looktimer <= 0 and mo.state == S_SPAMTONNEO_CHASE4 and R_PointToDist2(mo.x, mo.y, mo.target.x, mo.target.y) >= 6250*mapobjectscale)
				mo.chasing = false
				mo.reorienttimer = 14
				mo.looktimer = 500
				mo.state = S_SPAMTONNEO_LOOK
		end

		if mo.chasing and mo.reorienttimer <= 0 then
			A_SlideyChase(mo,0,0)
			mo.reorienttimer = 14
		else
			P_Thrust(mo, mo.angle, FixedMul(mo.info.speed*FRACUNIT/4, mo.scale))
		end
	end
end, MT_SPAMTONNEO)