--[[
moveto.lua:
INTERFACE
-- 1) Moves a train or ship through the points taken from the table below.
-- 2) Unloads unit when the last point reached

points = {
		{0, 0, 0},
	}
check_timeout = 5;
distance = 5;
unload = false;
END
--]]

function Main()
	AIparts:Run();
end

function TemplateUnloadInitialize(Points, Distance, CheckTimeout, Unload)
	if type(AIparts) ~= "table" then AIparts = utils.CreateSubsystems() end;
	for _,v in ipairs(Points) do
		unit.AppendWaypoint(GetTerrainPoint(v));
	end
	local last_waypoint = Points[#Points];
	local function check()
		if CalcDistance(object.GetPosition(), last_waypoint) < Distance then
			if Unload then
				unit.UnloadCargo();
			end
			AIparts:RemovePeriodic("template.CheckPoint");
		end
	end
	AIparts:AddPeriodic("template.CheckPoint", CheckTimeout, check);
end

TemplateUnloadInitialize(points, distance, check_timeout, unload);
