public void checkAtTargets()
        {
            if (m_scriptListens_atTarget || m_scriptListens_notAtTarget)
            {
                if (m_targets.Count > 0)
                {
                    bool at_target = false;
                    Dictionary<uint, scriptPosTarget> atTargets = new Dictionary<uint, scriptPosTarget>();
                    lock (m_targets)
                    {
                        foreach (uint idx in m_targets.Keys)
                        {
                            scriptPosTarget target = m_targets[idx];
                            if (Util.GetDistanceTo(target.targetPos, m_rootPart.GroupPosition) <= target.tolerance)
                            {
                                // trigger at_target
                                if (m_scriptListens_atTarget)
                                {
                                    at_target = true;
                                    scriptPosTarget att = new scriptPosTarget
                                                              {
                                                                  targetPos = target.targetPos,
                                                                  tolerance = target.tolerance,
                                                                  handle = target.handle
                                                              };
                                    atTargets.Add(idx, att);
                                }
                            }
                        }
                    }

                    if (atTargets.Count > 0)
                    {
                        uint[] localids = new uint[0];
                        localids = new uint[m_parts.Count];
                        int cntr = 0;
                        foreach (SceneObjectPart part in m_partsList)
                        {
                            localids[cntr] = part.LocalId;
                            cntr++;
                        }

                        foreach (uint t in localids)
                        {
                            foreach (scriptPosTarget att in atTargets.Values)
                            {
                                m_scene.EventManager.TriggerAtTargetEvent(
                                    t, att.handle, att.targetPos, m_rootPart.GroupPosition);
                            }
                        }

                        return;
                    }

                    if (m_scriptListens_notAtTarget && !at_target)
                    {
                        //trigger not_at_target
                        uint[] localids = new uint[0];
                        localids = new uint[m_parts.Count];
                        int cntr = 0;
                        foreach (SceneObjectPart part in m_partsList)
                        {
                            localids[cntr] = part.LocalId;
                            cntr++;
                        }

                        foreach (uint t in localids)
                        {
                            m_scene.EventManager.TriggerNotAtTargetEvent(t);
                        }
                    }
                }
            }
            if (m_scriptListens_atRotTarget || m_scriptListens_notAtRotTarget)
            {
                if (m_rotTargets.Count > 0)
                {
                    bool at_Rottarget = false;
                    Dictionary<uint, scriptRotTarget> atRotTargets = new Dictionary<uint, scriptRotTarget>();
                    lock (m_rotTargets)
                    {
                        foreach (uint idx in m_rotTargets.Keys)
                        {
                            scriptRotTarget target = m_rotTargets[idx];
                            double angle =
                                Math.Acos(target.targetRot.X*m_rootPart.GetRotationOffset().X +
                                          target.targetRot.Y*m_rootPart.GetRotationOffset().Y +
                                          target.targetRot.Z*m_rootPart.GetRotationOffset().Z +
                                          target.targetRot.W*m_rootPart.GetRotationOffset().W)*2;
                            if (angle < 0) angle = -angle;
                            if (angle > Math.PI) angle = (Math.PI*2 - angle);
                            if (angle <= target.tolerance)
                            {
                                // trigger at_rot_target
                                if (m_scriptListens_atRotTarget)
                                {
                                    at_Rottarget = true;
                                    scriptRotTarget att = new scriptRotTarget
                                                              {
                                                                  targetRot = target.targetRot,
                                                                  tolerance = target.tolerance,
                                                                  handle = target.handle
                                                              };
                                    atRotTargets.Add(idx, att);
                                }
                            }
                        }
                    }

                    if (atRotTargets.Count > 0)
                    {
                        uint[] localids = new uint[0];
                        localids = new uint[m_parts.Count];
                        int cntr = 0;
                        foreach (SceneObjectPart part in m_partsList)
                        {
                            localids[cntr] = part.LocalId;
                            cntr++;
                        }

                        foreach (uint t in localids)
                        {
                            foreach (scriptRotTarget att in atRotTargets.Values)
                            {
                                m_scene.EventManager.TriggerAtRotTargetEvent(
                                    t, att.handle, att.targetRot, m_rootPart.GetRotationOffset());
                            }
                        }

                        return;
                    }

                    if (m_scriptListens_notAtRotTarget && !at_Rottarget)
                    {
                        //trigger not_at_target
                        uint[] localids = new uint[0];
                        localids = new uint[m_parts.Count];
                        int cntr = 0;
                        foreach (SceneObjectPart part in m_partsList)
                        {
                            localids[cntr] = part.LocalId;
                            cntr++;
                        }

                        foreach (uint t in localids)
                        {
                            m_scene.EventManager.TriggerNotAtRotTargetEvent(t);
                        }
                    }
                }
            }
        }
 public int registerTargetWaypoint(Vector3 target, float tolerance)
 {
     scriptPosTarget waypoint = new scriptPosTarget {targetPos = target, tolerance = tolerance};
     uint handle = m_scene.SceneGraph.AllocateLocalId();
     waypoint.handle = handle;
     lock (m_targets)
     {
         m_targets.Add(handle, waypoint);
     }
     AddGroupTarget(this);
     return (int) handle;
 }