-----------------------------------------------
-- Target Computer script written by Rolleol --
-----------------------------------------------
--This script works with the gatling gun 
--
--
--
--


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