local d=script.Parent local x=script.Parent.Parent.Base local z=nil local q=Vector3.new(0,0,0)local N=Vector3.new(0,0,0)local f=Vector3.new(0,0,0)local H=Vector3.new(0,0,0)local G=Vector3.new(0,0,0)local c=Vector3.new(0,0,0)local U=Vector3.new(0,0,0)local D=Vector3.new(0,0,0)local J=Vector3.new(0,0,0)local A=Vector3.new(0,0,0)local I=Vector3.new(0,0,0)local y=Vector3.new(0,0,0)local s=Vector3.new(0,0,0)local g=Vector3.new(0,0,0)local k=Vector3.new(0,0,0)local E=Vector3.new(0,0,0)local F=Vector3.new(0,0,0)local o=0 local l=1 while true do z=script.Parent.Target.Value c=G G=H H=f f=N N=q q=d.TargetPosition.Value if N==Vector3.new(0,0,0)then c=q G=q H=q f=q N=q end y=I I=A A=J J=D D=U U=d.TargetVelocity.Value s=(((((q+N)+f)+H)+G)+c)/6 g=(((((U+D)+J)+A)+I)+y)/6 k=((((q+(N-q))+(f-N))+(H-f))+(G-H))+(c-G)/6 E=((((U+(D-U))+(J-D))+(A-J))+(I-A))+(y-I)/6 E=E*l if o<1 then o=10 l=math.random(75,125)/100 else o=o-1 end F=k+E*(1+(d.Position-F).Magnitude/3000)d.TargetPredictedPosition.Value=F d.TargetingError.Value=F-(d.TargetPosition.Value+d.TargetVelocity.Value)wait(.1)end

local switch = script.Parent
local base = script.Parent.Parent.Base
local target = nil
local targetposition1 = Vector3.new(0,0,0) -- Stores target's position, you can add more if you like
local targetposition2 = Vector3.new(0,0,0)
local targetposition3 = Vector3.new(0,0,0)
local targetposition4 = Vector3.new(0,0,0)
local targetposition5 = Vector3.new(0,0,0)
local targetposition6 = Vector3.new(0,0,0)

local targetvelocity1 = Vector3.new(0,0,0) -- Stores target's velocity, you can add more if you like
local targetvelocity2 = Vector3.new(0,0,0)
local targetvelocity3 = Vector3.new(0,0,0)
local targetvelocity4 = Vector3.new(0,0,0)
local targetvelocity5 = Vector3.new(0,0,0)
local targetvelocity6 = Vector3.new(0,0,0)

local targetavgposition = Vector3.new(0,0,0) -- Stores target's average position and velocity, might be useful
local targetavgvelocity = Vector3.new(0,0,0)

local targetnextposition = Vector3.new(0,0,0) -- This is what the script thinks the target's next position/velocity will be based on
local targetnextvelocity = Vector3.new(0,0,0) -- the values above

local targetpredictedposition = Vector3.new(0,0,0) -- This is what it returns to the other scripts and used for aiming and fuses

local timeuntilrerollvelocitymultiplier = 0 -- used for rerolling velocity multiplier
local velocitymultiplier = 1 -- literal multiplier, 1x is normal

while true do
	target = script.Parent.Target.Value
	
	targetposition6=targetposition5
	targetposition5=targetposition4
	targetposition4=targetposition3
	targetposition3=targetposition2
	targetposition2=targetposition1
	targetposition1=switch.TargetPosition.Value -- current value
	if targetposition2==Vector3.new(0,0,0) then -- if target position 2 is still empty, make all others copy current position
		targetposition6=targetposition1
		targetposition5=targetposition1
		targetposition4=targetposition1
		targetposition3=targetposition1
		targetposition2=targetposition1
	end
	
	targetvelocity6=targetvelocity5
	targetvelocity5=targetvelocity4
	targetvelocity4=targetvelocity3
	targetvelocity3=targetvelocity2
	targetvelocity2=targetvelocity1
	targetvelocity1=switch.TargetVelocity.Value -- current value
	
	targetavgposition=(targetposition1+targetposition2+targetposition3+targetposition4+targetposition5+targetposition6)/6
	targetavgvelocity=(targetvelocity1+targetvelocity2+targetvelocity3+targetvelocity4+targetvelocity5+targetvelocity6)/6
	
	-- Simplest equation, but lacks any sense of tracking
	--targetnextposition=targetposition1 
	--targetnextvelocity=targetvelocity1
	
	-- Worst equation, easily fooled by constantly turning
	--targetnextposition=(targetposition1+targetavgposition)/2 -- Worst equation, easily fooled by constantly turning
	--targetnextvelocity=(targetvelocity1+targetavgvelocity)/2
	
	-- This one is kinda meh, it can only reliably engage targets at 400 velocity
	--targetnextposition=targetavgposition 
	--targetnextvelocity=targetavgvelocity
	
	-- Okay equation, can reliably shoot at targets up to 750+ velocity
	--targetnextposition=targetposition1+(targetposition2-targetposition1)
	--targetnextvelocity=targetvelocity1+(targetvelocity2-targetvelocity1)
	
	-- Very good equation, can reliably get direct hits on targets up to 150 velocity and with fuses can shoot at targets up to 800+ velocity
	targetnextposition=(targetposition1+(targetposition2-targetposition1)+(targetposition3-targetposition2)+(targetposition4-targetposition3)+(targetposition5-targetposition4)+(targetposition6-targetposition5)/6)
	targetnextvelocity=(targetvelocity1+(targetvelocity2-targetvelocity1)+(targetvelocity3-targetvelocity2)+(targetvelocity4-targetvelocity3)+(targetvelocity5-targetvelocity4)+(targetvelocity6-targetvelocity5)/6)
	
	targetnextvelocity=targetnextvelocity*velocitymultiplier -- place random multiplier on velocity so it isn't too predictable
	if timeuntilrerollvelocitymultiplier < 1 then
		timeuntilrerollvelocitymultiplier=10 -- 1 second between rerolls
		velocitymultiplier=(math.random(75,125)/100)
	else
		timeuntilrerollvelocitymultiplier=timeuntilrerollvelocitymultiplier-1
	end
	
	targetpredictedposition=targetnextposition+(targetnextvelocity*(1+((switch.Position-targetpredictedposition).Magnitude)/3000))
	switch.TargetPredictedPosition.Value=targetpredictedposition -- Sets the TargetPredictedPosition so that other scripts can use it
	
	switch.TargetingError.Value=targetpredictedposition-(switch.TargetPosition.Value+switch.TargetVelocity.Value) -- just for debug purposes
	wait(0.1) -- I would advise not setting this too high or too low because then it either becomes unresponsive to
	   		   -- evasive maneuvers or won't have enough data to predict where the target is going
			   -- I think a value of (Delay between shots/2) secs works well enough
end
