
-- -- Flybot -- --
freeslot(
"MT_FLYBOT",
"S_FLYINIT", "S_FLYLOOK", "S_FLYMOVE",
"SPR_FLYB","sfx_flybo")

local fly_position_test = true

sfxinfo[sfx_flybo] = {
	singular = true,
	priority = 64,
	flags = SF_X4AWAYSOUND
}

mobjinfo[MT_FLYBOT] = {
	--$Category Broadside Cliffs
	--$Name Black Shooting Fly
	--$Sprite FLYBA1
	doomednum = 2300,
	radius = 24*FRACUNIT,
	height = 40*FRACUNIT,
	speed = 4*FRACUNIT,
	reactiontime = 2,
	spawnstate = S_FLYINIT,
	seestate = S_FLYMOVE,
	seesound = sfx_flybo,
	deathstate = S_XPLD_FLICKY,
	deathsound = sfx_pop,
	raisestate = MT_JETTBULLET,
	painchance = 1000,
	attacksound = sfx_trfire,
	mass = 100,
	flags = MF_SLIDEME|MF_ENEMY|MF_SPECIAL|MF_SHOOTABLE|MF_NOGRAVITY
}

states[S_FLYINIT] = {
	sprite = SPR_FLYB, frame = A, tics = 1, nextstate = S_FLYLOOK}
	
states[S_FLYLOOK] = {
	sprite = SPR_FLYB, frame = A, tics = 4, action = A_Look, var1 = 0+5000<<16, var2 = 1, nextstate = S_FLYLOOK}

states[S_FLYMOVE] = {
	sprite = SPR_FLYB, frame = A, tics = 6, action = none, nextstate = S_FLYMOVE}
	
addHook("MobjThinker", function(mob)
	if (mob.state == S_FLYINIT) then
		mob.distance_x = 9999
		mob.distance_y = 9999
		mob.distance_z = 9999
		mob.shoot_timer = 0
		mob.fly_position_test_long = true
	else
		if abs(mob.momz) > 1*FRACUNIT then
			mob.momz = mob.momz/2
		end
		
		if (abs(mob.momx) > 2*FRACUNIT or abs(mob.momy) > 2*FRACUNIT) then
			fly_position_test = P_CheckPosition(mob,mob.x+(mob.momx*4),mob.y+(mob.momy*4),mob.z)
			mob.fly_position_test_long = P_CheckPosition(mob,mob.x+(mob.momx*64),mob.y+(mob.momy*64),mob.z)
		end
	end
	
	if mob.shoot_timer > 300 then
		mob.shoot_timer = 0
		A_ChangeAngleRelative(mob,180,180)
	end
	
	if mob.state == S_FLYMOVE and not(mob.target == nil) then
		mob.distance_x = abs(mob.target.x-mob.x)/FRACUNIT
		mob.distance_y = abs(mob.target.y-mob.y)/FRACUNIT
		mob.distance_z = abs(mob.target.z-mob.z)/FRACUNIT
		
		--manual animation handling (to reduce state count and state changes)--
		if mob.tics >= 6 then
			mob.frame = A
		elseif mob.tics < 6 and mob.tics >= 4 then
			mob.frame = B
		elseif mob.tics < 4 and mob.tics >= 2 then
			mob.frame = C
		elseif mob.tics < 2 and mob.tics >= 0 then
			mob.frame = B
		end
		
		if (mob.shoot_timer < 70 and (mob.distance_x < 2500 or mob.shoot_timer < 2500)) then
			if (mob.shoot_timer == 0 and mob.momx < 10 and mob.momy < 10) then
				A_FaceTarget(mob)
			end
				
			if (mob.distance_x > (256+mob.distance_z) or mob.distance_y > (256+mob.distance_z)) then
				A_PlaySeeSound(mob)
				A_Thrust(mob,20,1)
			else
				A_ForceStop(mob,0)
				mob.shoot_timer = mob.shoot_timer + 1
				if mob.shoot_timer == 15 then
					A_ShootBullet(mob)
				end
			end
		else
			mob.shoot_timer = mob.shoot_timer + 1
			
			if (mob.distance_x < 2000 and mob.distance_y < 2000 and fly_position_test == true) then
				A_Thrust(mob,20,1)
			else
				if mob.fly_position_test_long == false then
					mob.state = S_FLYLOOK
					//print('stopped')
				end
			end
		end
	end
	
	if mob.state == S_FLYLOOK and mob.shoot_timer > 0 then
		A_ForceStop(mob,0)
		mob.shoot_timer = mob.shoot_timer + 1
		
		--manual animation handling (to reduce state count and state changes)--
		if mob.tics >= 2 then
			mob.frame = A
		else
			mob.frame = C
		end
	end
end
,MT_FLYBOT)