internal static void Search(GridGraph graph, GridSearch search, int x, int y, float[,] weights, IComparer <GridEdge> comparer)
        {
            search.Clear();
            int width  = graph.Width;
            int height = graph.Height;

            search.IsVisited[x, y] = true;
            search.Order.Add(new Vector2i(x, y));
            search.Parent[x, y] = new Vector2i(x, y);

            PriorityQueue <GridEdge> queue = new PriorityQueue <GridEdge>(comparer);

            List <GridEdge> edges = new List <GridEdge>(8);

            graph.GetEdges(x, y, edges, weights);

            if (edges.Count != 0)
            {
                foreach (GridEdge edge in edges)
                {
                    queue.Push(edge);
                }

                edges.Clear();
            }

            while (queue.Count != 0)
            {
                GridEdge edge = queue.Pop();

                Vector2i v = edge.To;
                if (search.IsVisited[v.x, v.y])
                {
                    continue;
                }

                search.Order.Add(v);
                search.IsVisited[v.x, v.y] = true;
                search.Parent[v.x, v.y]    = edge.From;

                if (graph.Edges[v.x, v.y] == 0)
                {
                    continue;
                }

                graph.GetEdges(v.x, v.y, edges, weights);

                foreach (GridEdge e in edges)
                {
                    if (search.IsVisited[e.To.x, e.To.y])
                    {
                        continue;
                    }
                    queue.Push(e);
                }

                edges.Clear();
            }
        }
Beispiel #2
0
        internal static void Search(GridGraph graph, GridSearch search, int x, int y)
        {
            search.Clear();
            int width  = graph.Width;
            int height = graph.Height;

            Queue <Vector2i> queue = new Queue <Vector2i>();

            queue.Enqueue(new Vector2i(x, y));

            search.Parent[x, y]    = new Vector2i(x, y);
            search.IsVisited[x, y] = true;

            while (queue.Count != 0)
            {
                Vector2i u = queue.Dequeue();
                search.Order.Add(u);

                int edge = graph.Edges[u.x, u.y];

                for (int i = 0; i < 8; i++)
                {
                    int xi = u.x + D8.OFFSETS[i, 0];
                    int yi = u.y + D8.OFFSETS[i, 1];

                    if (xi < 0 || xi > width - 1)
                    {
                        continue;
                    }
                    if (yi < 0 || yi > height - 1)
                    {
                        continue;
                    }

                    if ((edge & 1 << i) == 0)
                    {
                        continue;
                    }
                    if (search.IsVisited[xi, yi])
                    {
                        continue;
                    }

                    queue.Enqueue(new Vector2i(xi, yi));
                    search.IsVisited[xi, yi] = true;
                    search.Parent[xi, yi]    = u;
                }
            }
        }
Beispiel #3
0
        internal static void Search(GridGraph graph, GridSearch search, Vector2i start, Vector2i target)
        {
            search.Clear();
            int width  = graph.Width;
            int height = graph.Height;

            search.Parent[start.x, start.y] = start;

            var open = new List <Node>();

            open.Add(new Node(start.x, start.y));

            int g = 0;

            while (open.Count > 0)
            {
                var lowest = open.Min(n => n.f);
                var u      = open.First(n => n.f == lowest);

                open.Remove(u);
                search.IsVisited[u.x, u.y] = true;

                if (search.IsVisited[target.x, target.y])
                {
                    break;
                }

                int edge = graph.Edges[u.x, u.y];
                g++;

                for (int i = 0; i < 8; i++)
                {
                    int xi = u.x + D8.OFFSETS[i, 0];
                    int yi = u.y + D8.OFFSETS[i, 1];

                    if (xi < 0 || xi > width - 1)
                    {
                        continue;
                    }
                    if (yi < 0 || yi > height - 1)
                    {
                        continue;
                    }

                    if ((edge & 1 << i) == 0)
                    {
                        continue;
                    }
                    if (search.IsVisited[xi, yi])
                    {
                        continue;
                    }

                    int idx = Contains(xi, yi, open);
                    if (idx == -1)
                    {
                        var n = new Node(xi, yi);
                        n.g = g;
                        n.h = Distance(target.x, target.y, xi, yi);
                        n.f = n.g + n.h;

                        search.Parent[n.x, n.y] = new Vector2i(u.x, u.y);
                        open.Add(n);
                    }
                    else
                    {
                        var n = open[idx];
                        if (g + n.h < n.f)
                        {
                            n.g = g;
                            n.f = n.g + n.h;
                            search.Parent[n.x, n.y] = new Vector2i(u.x, u.y);
                            open[idx] = n;
                        }
                    }
                }
            }
        }