Example #1
0
        protected ListCopyable <Node> AstarSearch(Graph graph, ListCopyable <Node> visited, Node startNode, Node endNode, Constraint constraint, int threadIndex)
        {
            var openList = PoolQueue <Node> .Spawn(10);

            startNode.startToCurNodeLen[threadIndex] = 0f;

            openList.Enqueue(startNode);
            startNode.isOpened[threadIndex] = true;

            while (openList.Count > 0)
            {
                var node = openList.Dequeue();
                node.isClosed[threadIndex] = true;

                visited.Add(node);

                if (node.index == endNode.index)
                {
                    PoolQueue <Node> .Recycle(ref openList);

                    return(this.RetracePath(threadIndex, endNode));
                }

                var neighbors = node.GetConnections();
                foreach (var conn in neighbors)
                {
                    if (conn.index < 0)
                    {
                        continue;
                    }

                    var neighbor = graph.nodes[conn.index];
                    if (neighbor.isClosed[threadIndex] == true)
                    {
                        continue;
                    }
                    if (neighbor.IsSuitable(constraint) == false)
                    {
                        continue;
                    }

                    float ng = node.startToCurNodeLen[threadIndex] + conn.cost;
                    if (neighbor.isOpened[threadIndex] == false || ng < neighbor.startToCurNodeLen[threadIndex])
                    {
                        neighbor.startToCurNodeLen[threadIndex] = ng;
                        neighbor.parent[threadIndex]            = node;
                        if (neighbor.isOpened[threadIndex] == false)
                        {
                            openList.Enqueue(neighbor);
                            visited.Add(neighbor);
                            neighbor.isOpened[threadIndex] = true;
                        }
                    }
                }
            }

            PoolQueue <Node> .Recycle(ref openList);

            return(null);
        }
Example #2
0
        protected void FloodFillAreas(Node rootNode, int area)
        {
            var list = PoolQueue <Node> .Spawn(this.nodes.Count);

            list.Enqueue(rootNode);
            while (list.Count > 0)
            {
                var node = list.Dequeue();

                var connections = node.GetConnections();
                for (int j = 0; j < connections.Length; ++j)
                {
                    var connection = connections[j];
                    if (connection.index >= 0)
                    {
                        var nb = this.nodes[connection.index];
                        if (nb.area == 0 && nb.walkable == true)
                        {
                            nb.area = area;
                            //this.FloodFillAreas(nb, area);
                            list.Enqueue(nb);
                        }
                    }
                }
            }

            PoolQueue <Node> .Recycle(ref list);
        }