chat = {}
local rchat=game:GetService("Chat")
function chat.Chat(ignore,object,text)
	if not object then return end
	rchat:Chat(object,text)
	--print("AUTO:",text) --
end

local function advancedTarget(pS,t,o) -- pS = projectileSpeed, t = targetPart, o = originPart 
	-- So, I understand the maths in this. I also understand that it forms a circle for radius of targetting, also the curve to find it is quadratic and there is sine somewhere too
	-- https://devforum.roblox.com/t/making-a-lead-shot-indicator/325666/9 (figure out the maths here when you're older, i understand it but not confidently)
	local tV = t.Velocity * 1.1 -- target Velocity, *1.5 because of compensation
	local di = t.Position - o.Position -- displacement, origin to target
	local a = pS^2 - tV:Dot(tV) -- a,b,c as in quadratic equation so that ax^2 + bx + c  = 0
	local b = -2 * tV:Dot(di)
	local c = -di:Dot(di)
	local d = b^2 - 4*a*c -- d as an intermediate value for quadratic formula
	if d < 0 then return t.Position end -- failed, let's just hope we hit it anyways by luck
	local t0 = (-b + math.sqrt(d)) / (2*a) -- hello mr. hegarty, i exist
	local t1 = (-b - math.sqrt(d)) / (2*a)
	local ti = 0
	if t0 > 0 then ti = t0 end
	if t1 > 0 then ti = t1 end
	local target = t.CFrame.Position + tV * ti
	return target
end

function travelToLocation(a)
	a.humanoid:MoveTo(a.target.Position)
end

function findPlayer(a)
	local closest
	local distance = 100
	for _,player in pairs(game.Players:GetPlayers()) do
		if player.Character and player.Character:FindFirstChild("Humanoid") then
			local currentDistance = (player.Character.PrimaryPart.Position - a.head.Position).Magnitude
			if currentDistance < distance then distance = currentDistance end
			closest = player.Character.PrimaryPart
		end
	end
	if closest then a.task = travelToLocation a.target = closest end
end

function enterPlane(a)
	--[[local pathfinding = game:GetService("PathfindingService")
	local path = pathfinding:CreatePath({
		AgentCanJump = true,
		AgentHeight = 4,
		AgentWidth = 4
	})	
	chat:Chat(a.head,"computing")
	path:ComputeAsync(a.model.PrimaryPart.Position,a.plane.Position)
	if path.Status ~= Enum.PathStatus.Success then
		chat:Chat(a.head,"no path") 
		a.model:SetPrimaryPartCFrame(a.plane.CFrame)
		a.task = getTask return end
	
	a.plane.Material = Enum.Material.Neon
	a.plane.Transparency = 0.5
	
	chat:Chat(a.head,"walking")
	local waypointIndex = 0
	local waypoints = path:GetWaypoints()
	local function operateWaypoint()
		if not a.operate then return end
		if a.humanoid.SeatPart then chat:Chat(a.head,"piloting") a.task = flyPlane return end 
		waypointIndex = waypointIndex + 1
		if waypointIndex > #waypoints or not waypoints[waypointIndex] then return end
		a.humanoid:MoveTo(waypoints[waypointIndex].Position)
		
		local shouldReturn = false
		local event = a.humanoid.MoveToFinished:Connect(function()
			if waypoints[waypointIndex] and not a.humanoid.SeatPart and waypoints[waypointIndex].Action == Enum.PathWaypointAction.Jump then
				a.humanoid.Jump = true
			end
			shouldReturn = true
			operateWaypoint()
		end)
		wait(1)
		if not a.plane or not a.plane:FindFirstChild("Pilot") then return end
		event:Disconnect()
		if (a.model.PrimaryPart.Position - a.plane.Position).Magnitude < 6 then
			
		end
		if shouldReturn then return end
		operateWaypoint()
	end
	operateWaypoint()]]
	a.model:SetPrimaryPartCFrame(a.plane.CFrame)
	a.task = flyPlaneStraight
	a.v.parent = a.plane.Parent.Parent
	a.target = game.Workspace.BotTarget.Position
end
local flight = require(_G.Modules.ObjectInteraction.Main)
function flyPlaneStraight(a)
	if not a.flyPlaneStraightTick then a.flyPlaneStraightTick = tick() end
	if (tick() - a.flyPlaneStraightTick) > 3 then a.task = flyPlane a.flyPlaneStraightTick = nil end
	a.v.bindings.throttle = 0.8
	a.v.bindings.pitch = 0.3
	a.v.bindings.roll = 0
	a.v.bindings.yaw = 0
	local tar = a.plane.CFrame.LookVector
	tar = Vector3.new(tar.X,0,tar.Z).Unit
	local pos = (a.plane.Position + tar * 1000 + Vector3.new(0,150,0))
	a.v:aimTowards(pos)
end
function stop(a)
end
local trajectoryPart = Instance.new("Part")
for index,item in pairs({
		Color = Color3.new(1,1,1),
		Material = Enum.Material.Neon,
		Transparency = 0.2,
		Size = Vector3.new(6,6,6),
		CanCollide = false,
		Anchored = true,
		Shape = Enum.PartType.Ball,
		Parent = game.Workspace
	}) do
	trajectoryPart[index] = item
end
local targetPart = trajectoryPart:Clone()
targetPart.Parent = game.Workspace
function flyPlane(a)
	a.target = a.model.Target.Value.PrimaryPart.Position
	local trajectory = a.plane.Velocity*2 + a.plane.Position
	trajectoryPart.Position = trajectory
	targetPart.Position = a.target
	print(trajectory)
	if trajectory.Y < -20 then
		a.task = flyPlaneStraight
		print("recover!")
	end
	a.v.bindings.pitch = 0.0
	a.v.bindings.yaw = 0
	a.v:aimTowards(a.target)
	local speed = a.plane.Velocity.Magnitude
	if speed > 350 or speed < 50 or a.plane.Position.Y < -40 then print("stop!",speed) a.task = stop a.v.bindings.throttle = 0 task.delay(1,function() a.humanoid.Health = 0 end) end
end
function getTask(a)
	chat:Chat(a.head,"task")
	wait(0.5)
	if a.humanoid.SeatPart then chat:Chat(a.head,"piloting") a.task = flyPlaneStraight return end 
	a.task = locatePlane
	chat:Chat(a.head,"pilot")
end
function selfDestruct(a)
	chat:Chat(a.head,"destruct")
	a.humanoid.Health = 0
end
function locatePlane(a)
	chat:Chat(a.head,"find plane")
	a.plane = nil
	local distance = 100
	for _,object in pairs(game.Workspace.Auto:GetChildren()) do
		local seat = object:FindFirstChildWhichIsA("VehicleSeat",true)
		if seat and not seat.Occupant then
			local offset = (seat.Position - a.head.Position).Magnitude
			if offset < distance then
				distance = offset
				a.plane = seat
			end
		end
	end
	if a.plane then
		chat:Chat(a.head,"found plane")
		a.task = enterPlane
		return
	end
	chat:Chat(a.head,"no plane")
	a.task = selfDestruct
end
function hibernate(a)
	
end
function init(auto)
	local a = {}
	a.model = auto
	a.head = auto.Head
	if a.head:FindFirstChildWhichIsA("SurfaceGui") then a.head.SurfaceGui:Destroy() end
	a.face = a.head.Face
	a.humanoid = auto.Humanoid
	a.team = auto.Team.Value
	a.operate = true
	wait()
	a.plane = a.humanoid.SeatPart
	a.task = flyPlaneStraight
	a.v = flight.initialise(a.head)
	a.v.parent = a.humanoid.SeatPart.Parent.Parent
	a.v:seatEntered(true,a.humanoid.SeatPart)
	a.target = (a.plane.CFrame * CFrame.new(0,0,1000)).Position
	a.target = Vector3.new(a.target.X,a.plane.CFrame.Position.Y + 100,a.target.Z)
	a.humanoid.Died:Connect(function()
		wait(2)
		a.operate = false
		a.v.operate = false
		a.v:destroy()
		a.model:Destroy()
	end)
	while wait((1/30)*3) and a.operate do
		a.task(a)
	end
end
return getfenv()