예제 #1
0
    private bool CloseCircuit(HashSet <TrackNode> nodes, IntVector3 start, int startRotation)
    {
        // use A* to close track
        // Track ends at (0, 0, 0) rotation 0
        // Start from where cursor left off
        int        endRotation = 0;
        IntVector3 end         = new IntVector3();

        HashSet <SearchNode>    closed = new HashSet <SearchNode>();
        LinkedList <SearchNode> open   = new LinkedList <SearchNode>();

        SearchNode first = new SearchNode(null, startRotation, start);

        first.g = 0;
        first.h = start.dst(ref end);
        open.AddLast(first);

        int countdown = A_STAR_TIMEOUT * length;
        HashSet <IntVector3> currentPathPositions = new HashSet <IntVector3>();

        while (open.Count > 0)
        {
            if (countdown-- < 0)
            {
                return(false);
            }

            SearchNode current = open.First.Value;
            open.RemoveFirst();

            if (closed.Contains(current))
            {
                continue;
            }

            closed.Add(current);

            if (current.pos.Equals(end) && current.rotation == endRotation)
            {
                FinalizeCloseCircuit(nodes, current);
                return(true);
            }

            currentPathPositions.Clear();
            SearchNode tmp = current;
            while (tmp != null)
            {
                currentPathPositions.Add(tmp.pos);
                tmp = tmp.parent;
            }

            List <SearchNode> neighbours = GetNeighboursFor(nodes, current);
            foreach (SearchNode neighbour in neighbours)
            {
                if (closed.Contains(neighbour) || currentPathPositions.Contains(neighbour.pos))
                {
                    continue;
                }

                neighbour.g = current.g; //+ 1;
                neighbour.h = neighbour.pos.dst(ref end);
                AddOrdered(open, neighbour);
            }
        }

        return(false);
    }