
checkenemy_timeout = 5;
checkpoint_timeout = 5;
distance = 5;
point = {0, 0, 0};

function CheckEnemy()
	log("CheckEnemy");
	local enemy = unit.GetClosestEnemy(0, object.desc.VisibilityRange);
	if enemy then
		point = GetPosition(enemy);
		unit.MoveTo(GetTerrainPoint(point));
		AIparts:RemovePeriodic("CheckEnemy");
		AIparts:AddPeriodic("CheckPoint", checkpoint_timeout, CheckPoint);
	end;
end;

function CheckPoint()
	log("CheckPoint");
	if CalcDistance(object.GetPosition(), point) < distance then
		--unit.UnloadAll();
		AIparts:RemovePeriodic("CheckPoint");
		AIparts:AddPeriodic("CheckEnemy", checkenemy_timeout, CheckEnemy);
	end;
end;

AIparts:AddPeriodic("CheckEnemy", checkenemy_timeout, CheckEnemy);