
freeslot("MT_JETTRUNNER","MT_JETTARGET","MT_JETTLASER","MT_JETTSEEKER","MT_JETTWARNING")

// create the object to aim at
function A_Runner1Aim(actor, var1, var2) // (actor, var1, var2)
	local tar = actor.target
	local locvar1 = var1
	local angle = angle_t
	local point = mobj_t
	local dur = tic_t
	local i = 0
	local tarDist = 0
	local projectile = var1
	
	actor.aim = P_SpawnMobj(actor.x+FixedMul(200*FRACUNIT, cos(actor.angle)),  
	actor.y+FixedMul(200*FRACUNIT, sin(actor.angle)), actor.z+(1*FRACUNIT), MT_JETTARGET)
	actor.aim.creator = actor
	//end

end

// create the laser 
function A_Runner1Laser(actor, var1, var2) // (actor, var1, var2)
	local tar = actor.target
	local locvar1 = var1
	local angle = angle_t
	local point = mobj_t
	local dur = tic_t
	local i = 0
	local tarDist = 0
	local projectile = var1
	
	if (tar) and (tar.valid) then // Only bother checking if target exists
	actor.test = P_SpawnMobj(actor.x+FixedMul(41*FRACUNIT, cos(actor.angle)),  
	actor.y+FixedMul(41*FRACUNIT, sin(actor.angle)), actor.z, MT_JETTLASER)
	actor.test.angle = actor.angle
	actor.test.z = actor.z
	//(actor.test, actor.x + 41 * cos(actor.angle), actor.y + 41 * sin(actor.angle), actor.z)
		if actor.aim and actor.aim.valid then
		actor.test.target = actor.aim
		actor.test.creator = actor
		end
		
	else
	actor.state = S_JETR_JUMP0
		if actor.eflags&MFE_UNDERWATER then
		P_SetObjectMomZ(actor, (4*FRACUNIT), false)
		else
		P_SetObjectMomZ(actor, (10*FRACUNIT), false)
		end
	S_StartSound(actor, 197) //jump sound
	actor.movecount = 0 //bug fix
	end
	
	
end

addHook("ShouldDamage", function(mo, moinflictor, mosource, damage, damagetype)
	
	if mosource.type == MT_JETTLASER  or  mosource.type == MT_EGGMOBILE_FIRE then // no killing each other
	return false
	end 
	
	if mo.state == S_JETR_PAIN0 or mo.state == S_JETR_PAIN1 or mo.state == S_JETR_PAIN2 or mo.state == S_JETR_PAIN3 then
	return false
	end
	
	//mo.flags2 = $&~(MF2_FRET)
	
	if mo.aim and mo.aim.valid then
		P_RemoveMobj(mo.aim)
	end
	if mo.test and mo.test.valid then
		P_RemoveMobj(mo.test)
	end
	if mo.platformseeker and mo.platformseeker.valid then
		P_RemoveMobj(mo.platformseeker)
	end
	//P_SetObjectMomZ(mo, 1*FRACUNIT, true)

end, MT_JETTRUNNER)

addHook("MobjDamage", function(mo, moinflictor, mosource, damage, damagetype)

	if (mosource.type == MT_JETTLASER  or  mosource.type == MT_EGGMOBILE_FIRE) and mo.state != S_JETR_PAIN0  then // no killing each other
	return true
	end 
	
	//mo.flags2 = $&~(MF2_FRET)
	
	if mo.aim and mo.aim.valid then
		P_RemoveMobj(mo.aim)
	end
	if mo.test and mo.test.valid then
		P_RemoveMobj(mo.test)
	end
	if mo.platformseeker and mo.platformseeker.valid then
		P_RemoveMobj(mo.platformseeker)
	end
	//P_SetObjectMomZ(mo, 1*FRACUNIT, true)
	
end, MT_JETTRUNNER)

addHook("MobjThinker", function(mo)  //AMBUSH makes AI focus more on serprission attacks while no AMBUSH is more about chasing
local tar = mo.target
local randomangle = P_RandomRange(1,8)
local angle = angle_t

	if mo.movejump == nill
		mo.movejump = false
		mo.laserwarning = 0
		mo.postionwarning = (36*FRACUNIT)
		mo.postionwarning2 = (36*FRACUNIT)
		mo.aimpostion = (200*FRACUNIT)
		mo.platformseeker = 0
		mo.formationattack = false
		mo.chasejump = false
		mo.waterjump = 1
	end
	
	if mo.eflags&MFE_UNDERWATER then
	mo.waterjump = 1/2
	else
	mo.waterjump = 1
	end
	
    if mo.test and mo.test.valid then
	   if mo.state == S_JETR_PAIN0 or mo.state == S_JETR_DIE1 then 
		P_RemoveMobj(mo.test)
		else
		P_TeleportMove(mo.test, mo.x + 40 * cos(mo.angle-(ANG10/2)), mo.y + 40 * sin(mo.angle-(ANG10/2)), mo.z +4*FRACUNIT)
		end
	elseif mo.state == S_JETR_ATK4 then
	mo.state = S_JETR_JUMP0 // if thers no laser give up trying to shoot
		if mo.eflags&MFE_UNDERWATER then
		P_SetObjectMomZ(mo, (4*FRACUNIT), false)
		else
		P_SetObjectMomZ(mo, (10*FRACUNIT), false)
		end
	S_StartSound(mo, 197) //jump sound
	mo.movecount = 0 //bug fix
	end
		
	if (tar) and (tar.valid) then
	mo.postion = P_AproxDistance(tar.x - mo.x, tar.y - mo.y)
	mo.postionheight = (tar.z - mo.z)
	 if mo.state == S_JETR_STND then // keep attacking if hit into a pit
		mo.state = S_JETR_ATK1
	 end
	end
		
	//----------------trying to prevent friendly fire---------------------------------//	
	// dont shhot if an ally is right in frount of you
	if (mo.aim) and (mo.aim.valid) and (mo.aim.tracer) and (mo.aim.tracer.valid) then 
	mo.aimpostion = P_AproxDistance(mo.aim.tracer.x - mo.aim.x, mo.aim.tracer.y - mo.aim.y)
		if (mo.aim) and (mo.aim.valid) and mo.state == S_JETR_ATK2 and mo.aimpostion <= (100*FRACUNIT) and (mo.valid) then
			mo.angle = $+ANGLE_180
			mo.state = S_JETR_RUN5
			mo.movecount = 0 //bug fix
			P_RemoveMobj(mo.aim)
		end
	//else
	
	end
	
	if mo.flags2&MF2_AMBUSH and (mo.tracer) and (mo.tracer.valid) and mo.tracer != mo.test then // jump if an ally starts an attack
		mo.allypostion = P_AproxDistance(mo.tracer.x - mo.x, mo.tracer.y - mo.y)
		//mo.allyangle = R_PointToAngle2(mo.x, mo.y, mo.tracer.x, mo.tracer.y)
		if mo.allypostion <= (195*FRACUNIT) and mo.tracer != mo.test and
		(mo.state == S_JETR_ATK2 or mo.state == S_JETR_RUN8 or mo.state == S_JETR_RUN7 or
		mo.state == S_JETR_RUN6 or mo.state == S_JETR_RUN5 or mo.state == S_JETR_RUN4 or
		mo.state == S_JETR_RUN3 or mo.state == S_JETR_RUN2 or mo.state == S_JETR_RUN1) then
			mo.angle = $+ANGLE_180
			mo.state = S_JETR_JUMP1
				if mo.eflags&MFE_UNDERWATER then
				P_SetObjectMomZ(mo, (4*FRACUNIT), false)
				else
				P_SetObjectMomZ(mo, (10*FRACUNIT), false)
				end
			S_StartSound(mo, 197) //jump sound
			mo.formationattack = true // attack after jump
			mo.movecount = 0 //bug fix
		end
	end

	//------------------ create an object just so that way jetty syn runner does't run into lasers as much--//
	if not (mo.warning) then
	mo.warning = P_SpawnMobj(mo.x+FixedMul(70*FRACUNIT, cos(mo.angle)),  
	mo.y+FixedMul(70*FRACUNIT, sin(mo.angle)), mo.z, MT_JETTWARNING)
	mo.warning.creator = mo
	mo.postionwarning = (36*FRACUNIT)
	mo.postionwarning2 = (36*FRACUNIT)
	elseif (mo.warning) and (mo.warning.valid) then
	P_TeleportMove(mo.warning, mo.x + 70 * cos(mo.angle), mo.y + 70 * sin(mo.angle), mo.z)	
		if (mo.warning.target) and  (mo.warning.target.valid)then
		mo.postionwarning2 = P_AproxDistance(mo.warning.target.x - mo.warning.x, mo.warning.target.y - mo.warning.y)
		//---------- jump over warnings's Target MT_EGGMOBILE_TARGET--//
			if mo.flags2&MF2_AMBUSH and mo.postionwarning2 <= (95*FRACUNIT) and
			(mo.state == S_JETR_ATK2 or mo.state == S_JETR_RUN8 or mo.state == S_JETR_RUN7 or
			mo.state == S_JETR_RUN6 or mo.state == S_JETR_RUN5 or mo.state == S_JETR_RUN4 or
			mo.state == S_JETR_RUN3 or mo.state == S_JETR_RUN2 or mo.state == S_JETR_RUN1) then
				mo.state = S_JETR_JUMP0 //
					if mo.eflags&MFE_UNDERWATER then
					P_SetObjectMomZ(mo, (4*FRACUNIT), false)
					else
					P_SetObjectMomZ(mo, (10*FRACUNIT), false)
					end
				mo.formationattack = true // attack after jump
				S_StartSound(mo, 197) //jump sound
				mo.movecount = 0 //bug fix
			end
		end
		if (mo.warning.tracer) and  (mo.warning.tracer.valid)then
		mo.postionwarning = P_AproxDistance(mo.warning.tracer.x - mo.warning.x, mo.warning.tracer.y - mo.warning.y)
		//---------- jump over warnings's tracer  MT_EGGMOBILE_FIRE--//
			if mo.flags2&MF2_AMBUSH and mo.postionwarning2 <= (25*FRACUNIT) and
			(mo.state == S_JETR_ATK2 or mo.state == S_JETR_RUN8 or mo.state == S_JETR_RUN7 or
			mo.state == S_JETR_RUN6 or mo.state == S_JETR_RUN5 or mo.state == S_JETR_RUN4 or
			mo.state == S_JETR_RUN3 or mo.state == S_JETR_RUN2 or mo.state == S_JETR_RUN1) then
				//mo.target = mo.warning.tracer
				mo.target = mo.warning.target
				mo.state = S_JETR_JUMP0 //
					if mo.eflags&MFE_UNDERWATER then
					P_SetObjectMomZ(mo, (4*FRACUNIT), false)
					else
					P_SetObjectMomZ(mo, (10*FRACUNIT), false)
					end
				mo.formationattack = true // attack after jump
				S_StartSound(mo, 197) //jump sound
				mo.movecount = 0 //bug fix
			end
		end
	end
	if mo.formationattack == true and mo.state == S_JETR_RUN4 and mo.flags2&MF2_AMBUSH then
	mo.formationattack = false
	mo.state = S_JETR_ATK1 
	end
	
	//------------------------------------------------------------------------------//
	
	//---------------jump conditions------------------------//
	if mo.flags2&MF2_AMBUSH or (mo.platformseeker != 0) then
	//????
	elseif tar and tar.valid and mo.state == S_JETR_ATK2 then
		if (tar.z -(20*FRACUNIT)) > mo.z then
		mo.chasejump = true
			if mo.aim and mo.aim.valid then
				P_RemoveMobj(mo.aim)
			end
		end
	end
	
	// when target is on top of a platform jump to reach them also check if too heigh. movecount check is used to prevent a bug
	if (mo.state == S_JETR_RUN8 and (tar.z -(20*FRACUNIT)) > mo.z and tar.state != S_PLAY_SPINDASH and tar.state != S_PLAY_JUMP and tar.state != S_PLAY_PAIN 
	and tar.floorz and mo.postion <= (250*FRACUNIT) and mo.postionheight < (180*FRACUNIT)) or mo.movecount < -10 then
		mo.state = S_JETR_JUMP0
			if mo.eflags&MFE_UNDERWATER then
			P_SetObjectMomZ(mo, (4*FRACUNIT), false)
			else
			P_SetObjectMomZ(mo, (10*FRACUNIT), false)
			end
		S_StartSound(mo, 197) //jump sound
		mo.movecount = 0 //bug fix
	elseif mo.state == S_JETR_RUN3 and tar.z < mo.z and mo.flags2&MF2_AMBUSH and //if too close or too far jump down
	(mo.postion > (1890*FRACUNIT) or mo.postion <= (90*FRACUNIT)) then
		mo.state = S_JETR_JUMP0
			if mo.eflags&MFE_UNDERWATER then
			P_SetObjectMomZ(mo, (4*FRACUNIT), false)
			else
			P_SetObjectMomZ(mo, (10*FRACUNIT), false)
			end
		S_StartSound(mo, 197) //jump sound
		mo.movecount = 0 //bug fix
	elseif mo.state == S_JETR_RUN4 and mo.postion <= (190*FRACUNIT) and tar.z <= mo.z then //if close attack early
		mo.state = S_JETR_ATK1
	end
	
	// if stuck jumping close to a wall gain some speed so you can get over it
	if mo.momx <= (2*FRACUNIT) and mo.momy <= (2*FRACUNIT) and 
	mo.momx >= (-2*FRACUNIT) and mo.momy >= (-2*FRACUNIT) and mo.state == S_JETR_JUMP2 then
	mo.movejump = true
	end
	if mo.movejump == true and mo.state == S_JETR_JUMP2 then
	P_Thrust(mo, mo.angle, FixedMul((1*(FRACUNIT)/4), mo.scale))
	else
	mo.movejump = false
	end
		
	//---seek the heigh ground---//----------------------------------//
	if (mo.state == S_JETR_RUN3 or mo.state == S_JETR_RUN4) and not (mo.platformseeker) then 
	// spawn an object that can check for platforms
	mo.platformseeker = P_SpawnMobj(mo.x, mo.y, mo.z+(104*FRACUNIT), MT_JETTSEEKER)
	P_TeleportMove(mo.platformseeker, mo.platformseeker.x , mo.platformseeker.y , mo.z +(104*FRACUNIT))
	mo.platformseeker.creator = mo

		if (randomangle == 1) then // I dont think lua can do switch statements
		mo.platformseeker.angle = mo.angle
		elseif (randomangle == 2) then
		mo.platformseeker.angle = mo.angle-(mo.angle/4)
		elseif (randomangle == 3) then
		mo.platformseeker.angle = mo.angle-(mo.angle/3)
		elseif (randomangle == 4)then
		mo.platformseeker.angle = mo.angle-(mo.angle/2)
		elseif (randomangle == 5)then
		mo.platformseeker.angle = (mo.angle*-1)
		elseif (randomangle == 6)then
		mo.platformseeker.angle = (mo.angle*-1)-(mo.angle/2)
		elseif (randomangle == 7)then
		mo.platformseeker.angle = (mo.angle*-1)-(mo.angle/3)
		elseif (randomangle == 8)then
		mo.platformseeker.angle = (mo.angle*-1)-(mo.angle/4)
		end

	end
	
	if (mo.aim) and mo.aim.valid then
	//??? else
	elseif (mo.state == S_JETR_ATK4 or mo.state == S_JETR_ATK3) and mo.chasejump == true then
		if mo.test and mo.test.valid  and mo.health > 0 then
			mo.test.state = S_NULL
			if mo.postion <= (140*FRACUNIT) then // jump
			mo.state = S_JETR_JUMP0
				if mo.eflags&MFE_UNDERWATER then
				P_SetObjectMomZ(mo, (4*FRACUNIT), false)
				else
				P_SetObjectMomZ(mo, (10*FRACUNIT), false)
				end
			S_StartSound(mo, 197) //jump sound
			mo.movecount = 0 //bug fix
			else
			mo.state = S_JETR_RUN1
			end
		//mo.chasejump = false
		end
	end
	
end, MT_JETTRUNNER)//-------------------------------------------------------------------------------//

// when laser is removed set creators state
addHook("MobjRemoved", function(mo)
	if mo.creator and mo.creator.valid then
		if (mo.creator.state == S_JETR_ATK5 or mo.creator.state == S_JETR_ATK4 or mo.creator.state == S_JETR_ATK3) then
			mo.creator.state = S_JETR_RUN1
			if mo.creator.aim and mo.creator.aim.valid then
			P_RemoveMobj(mo.creator.aim)
			end
		end
	end
end, MT_JETTLASER)

//------------- platform seeker think ----------------------------//
addHook("MobjThinker", function(mo)
// if on a platform and heigher then creator make them jump
	if ( P_IsObjectOnGround(mo) and mo.creator and
	((mo.floorz) or (mo.eflags & MFE_ONGROUND)) ) then
		if mo.z-10 > mo.creator.z and (mo.creator.state == S_JETR_RUN8 or mo.creator.state == S_JETR_RUN7 or
		mo.creator.state == S_JETR_RUN6 or mo.creator.state == S_JETR_RUN5 or mo.creator.state == S_JETR_RUN4 or
		mo.creator.state == S_JETR_RUN3 or mo.creator.state == S_JETR_RUN2 or mo.creator.state == S_JETR_RUN1) then
			if mo.state != S_JETL_SEK4 then
				P_RemoveMobj(mo)
			else
			mo.oldtar = mo.creator.target
			mo.creator.target = mo
			mo.creator.state = S_JETR_JUMP0
				if mo.creator.eflags&MFE_UNDERWATER then
				P_SetObjectMomZ(mo.creator, (4*FRACUNIT), false)
				else
				P_SetObjectMomZ(mo.creator, (10*FRACUNIT), false)
				end
			mo.creator.chasejump = false 
			S_StartSound(mo.creator, 197) //jump sound
			end
		end
	end
	
end, MT_JETTSEEKER)

//if touching a spin/spindashing player remove self--------------//
/*addHook("MobjCollide", function(touchobj, mo) 
	if (touchobj.state == S_PLAY_SPINDASH or touchobj.state == S_PLAY_ROLL) and mo.type == MT_JETTSEEKER then
		//P_RemoveMobj(mo)
	end
end)*/

// when platform seeker is removed tell creator its gone 
addHook("MobjRemoved", function(mo)
	if mo.creator and mo.creator.valid then
		mo.creator.platformseeker = 0
		if (mo.oldtar) then //remind creator of its orignal target
		mo.creator.target = mo.oldtar
		end
		
	end
end, MT_JETTSEEKER)
//--------------------when jeety syn runner is removed remove all it child objects--------//
addHook("MobjRemoved", function(mo)

	if mo.aim and mo.aim.valid then
		P_RemoveMobj(mo.aim)
	end
	if mo.test and mo.test.valid then
		mo.test.state = S_NULL
	end
	if mo.platformseeker and mo.platformseeker.valid then
		P_RemoveMobj(mo.platformseeker)
	end
	if mo.warning and mo.warning.valid then
		P_RemoveMobj(mo.warning)
	end
end, MT_JETTRUNNER)

//-------------//

addHook("MobjDeath", function(mo, inflictor, damagetype) // prevent 800 points bug? solved! truns out I needed 2 damage hooks

	mo.state = S_JETR_DIE1

	//return true
end, MT_JETTRUNNER)

