Exemplo n.º 1
0
        /// <summary>
        /// Upon completion of pathfinding, this method builds the path that autopilot will have to follow.
        /// </summary>
        /// <param name="forwardHash">Position hash of the forward node that was reached.</param>
        /// <param name="backward">The backward set that was reached.</param>
        /// <param name="backwardHash">Position hash of the backward node that was reached.</param>
        private void BuildPath(long forwardHash, PathNodeSet backward = null, long backwardHash = 0L)
        {
            m_path.Clear();
#if SHOW_PATH
            PurgeTempGPS();
#endif

            Log.DebugLog("Obstruction: " + m_obstructingEntity.Entity.nameWithId() + ", match position: " + m_obstructingEntity.MatchPosition + ", position: " + m_obstructingEntity.GetPosition() + ", actual position: " + m_obstructingEntity.Entity.PositionComp.GetPosition());

            PathNode node;
            if (!m_forward.TryGetReached(forwardHash, out node))
            {
                Log.AlwaysLog("Parent hash " + forwardHash + " not found in forward set, failed to build path", Logger.severity.ERROR);
                if (backward.HasReached(forwardHash))
                {
                    Log.AlwaysLog("Backward set does contains hash", Logger.severity.DEBUG);
                }
                PathfindingFailed();
                return;
            }
            while (node.DistToCur != 0f)
            {
#if SHOW_PATH
                ShowPosition(node, "Path");
#endif
                m_path.AddFront(ref node.Position);
                Log.DebugLog("Forward: " + ReportRelativePosition(node.Position));
                if (!m_forward.TryGetReached(node.ParentKey, out node))
                {
                    Log.AlwaysLog("Child hash " + forwardHash + " not found in forward set, failed to build path", Logger.severity.ERROR);
                    if (backward.HasReached(forwardHash))
                    {
                        Log.AlwaysLog("Backward set does contains hash", Logger.severity.DEBUG);
                    }
                    PathfindingFailed();
                    return;
                }
            }
            m_path.AddFront(ref node.Position);
            Log.DebugLog("Forward: " + ReportRelativePosition(node.Position));

            if (backwardHash != 0L)
            {
                if (!backward.TryGetReached(backwardHash, out node))
                {
                    Log.AlwaysLog("Parent hash " + backwardHash + " not found in backward set, failed to build path", Logger.severity.ERROR);
                    if (m_forward.HasReached(forwardHash))
                    {
                        Log.AlwaysLog("Forward set does contains hash", Logger.severity.DEBUG);
                    }
                    PathfindingFailed();
                    return;
                }

                if (forwardHash == backwardHash && node.ParentKey != 0L)
                {
                    if (!backward.TryGetReached(node.ParentKey, out node))
                    {
                        Log.AlwaysLog("First child hash " + backwardHash + " not found in backward set, failed to build path", Logger.severity.ERROR);
                        if (m_forward.HasReached(forwardHash))
                        {
                            Log.AlwaysLog("Forward set does contains hash", Logger.severity.DEBUG);
                        }
                        PathfindingFailed();
                        return;
                    }
                }
                while (node.DistToCur != 0f)
                {
#if SHOW_PATH
                    ShowPosition(node, "Path");
#endif
                    m_path.AddBack(ref node.Position);
                    Log.DebugLog("Backward: " + ReportRelativePosition(node.Position));
                    if (!backward.TryGetReached(node.ParentKey, out node))
                    {
                        Log.AlwaysLog("Child hash " + backwardHash + " not found in backward set, failed to build path", Logger.severity.ERROR);
                        if (m_forward.HasReached(forwardHash))
                        {
                            Log.AlwaysLog("Forward set does contains hash", Logger.severity.DEBUG);
                        }
                        PathfindingFailed();
                        return;
                    }
                }
                m_path.AddBack(ref node.Position);
                Log.DebugLog("Backward: " + ReportRelativePosition(node.Position));
            }

            //#if PROFILE
            //			LogStats();
            //#endif

            foreach (Vector3D position in m_path.m_positions)
            {
                Log.DebugLog("Path: " + ReportRelativePosition(position));
            }

            if (backward != null)
            {
                for (int i = 0; i < m_backwardList.Length; i++)
                {
                    if (m_backwardList[i] == backward)
                    {
                        m_pickedDestination = m_destinations[i];
                        Log.DebugLog("Picked destination: " + m_pickedDestination);
                        break;
                    }
                }
            }

            CurrentState = State.FollowingPath;
            Log.DebugLog("Built path", Logger.severity.INFO);
            //Logger.DebugNotify("Finished Pathfinding", level: Logger.severity.INFO);
            SetNextPathTarget();
        }