private void Close()
 {
     CustomTools.Console.DebugLog("Client::Close() Close client");
     CloseWebSocket();
     if (SendFromThread)
     {
         sendingQueue.Clear();
     }
     ConnectionClosed.SafeInvoke(this);
 }
 private void Dispose(bool disposing)
 {
     if (disposing)
     {
         CloseWebSocket();
         uri = null;
         if (!callbackManager.IsNull())
         {
             callbackManager.Dispose();
             callbackManager = null;
         }
         if (!sendingQueue.IsNull())
         {
             sendingQueue.Clear();
             sendingQueue = null;
         }
         pinging = false;
         connectionOpenEvent.Reset();
         connectionOpenEvent.Close();
         if (!receivedQueue.IsNull())
         {
             receivedQueue.Clear();
             receivedQueue = null;
         }
     }
 }
Beispiel #3
0
        /// <summary>
        /// Test a path between current position and destination.
        /// </summary>
        private void TestPath(Vector3D destination, MyEntity ignoreEntity, byte runId, bool isAlternate, bool tryAlternates, bool slowDown = false)
        {
            m_logger.debugLog("m_navBlock == null", Logger.severity.FATAL, condition: m_navBlock == null);

            if (runId != m_runId)
            {
                m_logger.debugLog("destination changed, abort", Logger.severity.DEBUG);
                return;
            }

            if (!lock_testPath.TryAcquireExclusive())
            {
                m_logger.debugLog("Already running, requeue (destination:" + destination + ", ignoreEntity: " + ignoreEntity.getBestName() + ", runId :" + runId
                                  + ", isAlternate: " + isAlternate + ", tryAlternates: " + tryAlternates + ", slowDown: " + slowDown + ")");
                LockedQueue <Action> queue = isAlternate ? m_pathLow : m_pathHigh;
                queue.Enqueue(() => TestPath(destination, ignoreEntity, runId, isAlternate, tryAlternates));
                return;
            }
            try
            {
                if (m_grid != m_navBlock.Grid)
                {
                    m_logger.debugLog("Grid has changed, from " + m_grid.getBestName() + " to " + m_navBlock.Grid.getBestName() + ", nav block: " + m_navBlock.Block.getBestName(), Logger.severity.WARNING);
                    return;
                }
                m_logger.debugLog("Running, (destination:" + destination + ", ignoreEntity: " + ignoreEntity.getBestName() + ", runId :" + runId
                                  + ", isAlternate: " + isAlternate + ", tryAlternates: " + tryAlternates + ", slowDown: " + slowDown + ")");

                MyEntity obstructing;
                Vector3? pointOfObstruction;

                if (!isAlternate && !m_ignoreAsteroid)
                {
                    if (slowDown)
                    {
                        if ((m_planetCheckSpeed.CurrentState & PlanetChecker.State.Blocked) != 0)
                        {
                            float speed = Vector3.Distance(m_planetCheckSpeed.ObstructionPoint, m_navBlock.WorldPosition) * 0.1f;
                            if (speed < 1f)
                            {
                                speed = 1f;
                            }
                            m_navSet.Settings_Task_NavWay.SpeedTarget = speed;
                            m_logger.debugLog("Path blocked by planet, set SpeedTarget to " + speed + ", obstructed by planet", Logger.severity.TRACE);
                            return;
                        }
                    }
                    else
                    {
                        if ((m_planetCheckDest.CurrentState & PlanetChecker.State.Blocked) != 0 &&
                            // planet checker is using an older displacement so verify that obstruction is closer than destination
                            Vector3D.DistanceSquared(m_navBlock.WorldPosition, m_planetCheckDest.ObstructionPoint) < Vector3D.DistanceSquared(m_navBlock.WorldPosition, destination))
                        {
                            m_logger.debugLog("path blocked by planet, to destination: " + (destination - m_navBlock.WorldPosition) + ", to obstruction: " + (m_planetCheckDest.ObstructionPoint - m_navBlock.WorldPosition));

                            if (m_pathState < PathState.Searching)
                            {
                                m_pathState = PathState.Searching;
                            }
                            obstructing = m_planetCheckDest.gravcomp;

                            Vector3 direction = Vector3.Normalize(m_navBlock.WorldPosition - obstructing.GetCentre());
                            pointOfObstruction = m_planetCheckDest.ObstructionPoint + direction * 1e3f;

                            float distance = Vector3.Distance(m_navBlock.WorldPosition, pointOfObstruction.Value);

                            MoveObstruction = obstructing;
                            m_pathHigh.Clear();
                            ClearAltPath();
                            if ((m_planetCheckDest.CurrentState & PlanetChecker.State.BlockedPath) != 0)
                            {
                                FindAlternate_AroundObstruction(pointOfObstruction.Value - m_navBlock.WorldPosition, new Vector3[] { direction }, 1e4f, runId);
                            }
                            else                             // blocked by gravity
                            {
                                FindAlternate_AroundObstruction(pointOfObstruction.Value - m_navBlock.WorldPosition, new Vector3[] { direction }, 1e6f, runId);
                            }
                            m_pathLow.Enqueue(() => {
                                if (m_altPath_AlternatesFound != 0)
                                {
                                    SetWaypoint();
                                }
                                RunItem();
                            });
                            m_pathLow.Enqueue(() => m_pathState = PathState.Path_Blocked);

                            return;
                        }
                    }
                }

                // for alternates, check that it can be better than current value
                if (isAlternate)
                {
                    float distToWaypointSquared = (float)Vector3D.DistanceSquared(m_navBlock.WorldPosition, destination);
                    if (distToWaypointSquared * WaypointDistanceBias * WaypointDistanceBias > m_altPath_PathValue * m_altPath_PathValue)
                    {
                        m_logger.debugLog("no point in checking alternate path, bias is too high", Logger.severity.TRACE);
                        m_logger.debugLog("no alternate, yet path value is set", Logger.severity.ERROR, condition: m_altPath_AlternatesFound == 0);
                        IncrementAlternatesFound();
                        return;
                    }
                }

                if (m_pathChecker.TestFast(m_navBlock, destination, m_ignoreAsteroid, ignoreEntity, m_landing))
                {
                    m_logger.debugLog("path is clear (fast)", Logger.severity.TRACE);
                    PathClear(ref destination, runId, isAlternate, slowDown);
                    return;
                }

                if (m_pathChecker.TestSlow(out obstructing, out pointOfObstruction))
                {
                    m_logger.debugLog("path is clear (slow)", Logger.severity.TRACE);
                    PathClear(ref destination, runId, isAlternate, slowDown);
                    return;
                }

                if (runId != m_runId)
                {
                    m_logger.debugLog("destination changed, abort", Logger.severity.DEBUG);
                    return;
                }

                if (slowDown)
                {
                    float speed = Vector3.Distance(pointOfObstruction.Value, m_navBlock.WorldPosition) * 0.1f;
                    if (speed < 1f)
                    {
                        speed = 1f;
                    }
                    IMyEntity destEntity = m_navSet.Settings_Current.DestinationEntity;
                    if (destEntity != null)
                    {
                        destEntity = destEntity.GetTopMostParent();
                    }
                    if (obstructing.GetTopMostParent() == destEntity)
                    {
                        m_navSet.Settings_Task_NavWay.SpeedMaxRelative = speed;
                        m_logger.debugLog("Set SpeedMaxRelative to " + speed + ", obstructing: " + obstructing.getBestName() + ", DestinationEntity: " + m_navSet.Settings_Current.DestinationEntity.getBestName(), Logger.severity.TRACE);
                    }
                    else
                    {
                        m_navSet.Settings_Task_NavWay.SpeedTarget = speed;
                        m_logger.debugLog("Set SpeedTarget to " + speed + ", obstructing: " + obstructing.getBestName() + ", DestinationEntity: " + m_navSet.Settings_Current.DestinationEntity.getBestName(), Logger.severity.TRACE);
                    }
                    return;
                }

                if (m_pathState < PathState.Searching)
                {
                    m_pathState = PathState.Searching;
                }

                m_logger.debugLog("path is blocked by " + obstructing.getBestName() + " at " + pointOfObstruction + ", ignoreEntity: " + ignoreEntity.getBestName(), isAlternate ? Logger.severity.TRACE : Logger.severity.DEBUG);
                m_logger.debugLog("grid: " + obstructing.GetTopMostParent().DisplayName, isAlternate ? Logger.severity.TRACE : Logger.severity.DEBUG, condition: obstructing is IMyCubeBlock);

                if (isAlternate && m_altPath_AlternatesFound != 0)
                {
                    IncrementAlternatesFound();
                }

                if (tryAlternates)
                {
                    //float angle = m_navSet.Settings_Current.DistanceAngle;
                    //if (angle > 0.1f && CanRotate)
                    //{
                    //	m_logger.debugLog("wait for rotation", "TestPath()");
                    //	return;
                    //}

                    if (m_navSet.Settings_Task_NavEngage.NavigatorMover != m_navSet.Settings_Current.NavigatorMover)
                    {
                        m_logger.debugLog("obstructed while flying to a waypoint, throwing it out and starting over", Logger.severity.DEBUG);
                        m_navSet.OnTaskComplete_NavWay();
                        return;
                    }

                    ClearAltPath();
                    MoveObstruction = obstructing;
                    TryAlternates(runId, pointOfObstruction.Value, obstructing);
                }
            }
            finally
            {
                lock_testPath.ReleaseExclusive();
                RunItem();
            }
        }
Beispiel #4
0
        public void TestPath(Vector3D destination, bool landing)
        {
            if (m_navSet.Settings_Current.DestinationChanged || m_prevMover != m_navSet.Settings_Current.NavigatorMover)
            {
                m_logger.debugLog("new destination: " + destination, Logger.severity.INFO);
                m_navSet.Settings_Task_NavWay.DestinationChanged = false;
                m_prevMover = m_navSet.Settings_Current.NavigatorMover;
                m_runId++;
                m_pathLow.Clear();
                ClearAltPath();
                m_pathState = PathState.Not_Running;
                m_planetCheckDest.Stop();
                m_planetCheckSpeed.Stop();
            }
            //else
            //	m_logger.debugLog("destination unchanged", "TestPath()");

            if (Globals.UpdateCount < m_nextRunPath)
            {
                return;
            }
            m_nextRunPath = Globals.UpdateCount + 10ul;

            if (m_pathLow.Count != 0)
            {
                m_logger.debugLog("path low is running");
                return;
            }

            m_navBlock        = m_navSet.Settings_Current.NavigationBlock;
            m_destination     = destination;
            m_ignoreAsteroid  = m_navSet.Settings_Current.IgnoreAsteroid;
            m_landing         = landing;
            m_canChangeCourse = m_navSet.Settings_Current.PathfinderCanChangeCourse;
            MyEntity destEntity = m_navSet.Settings_Current.DestinationEntity as MyEntity;

            m_logger.debugLog("DestinationEntity: " + m_navSet.Settings_Current.DestinationEntity.getBestName());
            byte runId = m_runId;

            const float minimumDistance = 100f;
            const float minDistSquared  = minimumDistance * minimumDistance;
            const float seconds         = 10f;
            const float distOverSeconds = minimumDistance / seconds;

            Vector3 displacement    = destination - m_navBlock.WorldPosition;
            float   distanceSquared = displacement.LengthSquared();
            float   testDistance;
            Vector3 move_direction = m_grid.Physics.LinearVelocity;
            float   speedSquared   = move_direction.LengthSquared();

            if (distanceSquared > minDistSquared)
            {
                // only look ahead 10 s / 100 m
                testDistance = speedSquared < distOverSeconds ? minimumDistance : (float)Math.Sqrt(speedSquared) * seconds;
                if (testDistance * testDistance < distanceSquared)
                {
                    Vector3 direction = displacement / (float)Math.Sqrt(distanceSquared);
                    destination = m_navBlock.WorldPosition + testDistance * direction;
                    m_logger.debugLog("moved destination: " + destination + ", distance: " + testDistance + ", direction: " + direction);
                }
            }
            else
            {
                m_logger.debugLog("using actual destination: " + destination);
            }

            m_pathHigh.Enqueue(() => TestPath(destination, destEntity, runId, isAlternate: false, tryAlternates: true));
            if (m_ignoreAsteroid)
            {
                m_planetCheckDest.Stop();
            }
            else
            {
                m_planetCheckDest.Start(destination - m_navBlock.WorldPosition);
            }

            // given velocity and distance, calculate destination
            if (speedSquared > 1f)
            {
                Vector3D moveDest = m_navBlock.WorldPosition + move_direction * LookAheadSpeed_Seconds;
                m_pathHigh.Enqueue(() => TestPath(moveDest, null, runId, isAlternate: false, tryAlternates: false, slowDown: true));
                if (m_ignoreAsteroid)
                {
                    m_planetCheckSpeed.Stop();
                }
                else
                {
                    m_planetCheckSpeed.Start(moveDest - m_navBlock.WorldPosition);
                }
            }
            else
            {
                m_navSet.Settings_Task_NavWay.SpeedMaxRelative = float.MaxValue;
                m_navSet.Settings_Task_NavWay.SpeedTarget      = float.MaxValue;
            }

            RunItem();
        }