--[[
guard.lua:
INTERFACE
guard_timeout = 50;
check_timeout = 50;
id = 1;
delta = 15;
END
--]]

i = 1;
distance = 5;
points = {{}};

function Guard()
--	log("Guard");
	local i = 0;
--[[
	local enemies_list = unit.GetAllyList(0, object.desc.VisibilityRange);
	for index,enemy in pairs(enemies_list) do
		i = 1 + i;
	end
	log("i = "..i);
--]]
	local point = GetPosition(id);
	points = {
		{point[1] + delta, point[2], point[3]};
		{point[1], point[2], point[3] + delta};
		{point[1] - delta, point[2], point[3]};
		{point[1], point[2], point[3] - delta};
	}
end

function CheckPoint()
--	ScreenLog("CheckPoint "..k1);
	local pred = i;
	if CalcDistance(object.GetPosition(), points[i]) < distance then
--		log("Move");
		i = i + 1;
		if i == 5 then
--			log("Move2");
			i = 1;
		end
		unit.MoveTo(GetTerrainPoint(points[i]));
	elseif unit.IsStopped() then
--		log("Pred");
		unit.MoveTo(GetTerrainPoint(points[i]));
	end
end

Guard();
unit.MoveTo(GetTerrainPoint(points[1]));
AIparts:AddPeriodic("CheckPoint", check_timeout, CheckPoint);
AIparts:AddPeriodic("Guard", guard_timeout, Guard);
