コード例 #1
0
        private List <Vector2> Reverse(Unvisited last)
        {
            List <Vector2> inReverseOrder = new List <Vector2>();

            inReverseOrder.Add(End);
            while (last != null)
            {
                inReverseOrder.Add(last.Location);
                last = last.Parent;
            }

            inReverseOrder.Reverse();
            return(inReverseOrder);
        }
コード例 #2
0
ファイル: Spider.cs プロジェクト: jonfast565/Spidr
        public void Start()
        {
            // ignore ssl errors
            ServicePointManager.ServerCertificateValidationCallback = (obj, certificate, chain, errors) => (true);

            // start
            var starter = UrlObject.FromString(Frontier);

            if (!Unvisited.Any())
            {
                Unvisited.Add(starter.GetFullPath(false), starter);
            }

            // while still pages unprocessed
            while (Unvisited.Any() && Visited.Count < MaxAllowedPages)
            {
                Parallel.ForEach(Unvisited, (urlPair) =>
                {
                    try
                    {
                        try
                        {
                            var p = PageFromUrl(urlPair.Value);
                            ProcessNewPaths(p, urlPair.Value);
                        }
                        catch (ArgumentOutOfRangeException) { }

                        var unprocessed = Visited.Where(x => x.Value.Processed == false);
                        foreach (var page in unprocessed)
                        {
                            if (this.JobType == SpiderJobType.PAGE_ONLY)
                            {
                                page.Value.LinkTags = new List <LinkTag>();
                            }
                            PersistenceInserter.PersistData(page.Value);
                            page.Value.Processed = true;
                        }
                    }
                    catch (ArgumentException) { }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                    }
                });
            }
        }
コード例 #3
0
ファイル: Spider.cs プロジェクト: jonfast565/Spidr
        public void ProcessNewPaths(Page p, UrlObject domainObject)
        {
            if (p != null && domainObject != null)
            {
                Console.WriteLine("Visited: " + p.Link.GetFullPath(false));

                Unvisited.Remove(p.Link.GetFullPath(false));
                if (!Visited.ContainsKey(p.Link.GetFullPath(false)))
                {
                    Visited.Add(p.Link.GetFullPath(false), p);
                }

                foreach (LinkTag l in p.LinkTags)
                {
                    var toBeVisited = false;
                    var visited     = false;
                    try
                    {
                        var key = Unvisited[l.Url.GetFullPath(false)];
                        toBeVisited = true;
                    }
                    catch (KeyNotFoundException /* knfe */) { }

                    try
                    {
                        var key = Visited[l.Url.GetFullPath(false)];
                        visited = true;
                    }
                    catch (KeyNotFoundException /* knfe */) { }

                    if (toBeVisited != true
                        & visited != true)
                    {
                        if (l.Url.GetDomain() == domainObject.GetDomain())
                        {
                            Unvisited.Add(l.Url.GetFullPath(false), l.Url);
                        }
                    }
                }
            }
        }
コード例 #4
0
        /// <summary>
        /// Calculates the path that the unit should follow to get to end from start. If no path was found, then
        /// this returns none
        /// </summary>
        /// <returns>the list of points to go to null if no path found</returns>
        public List <Vector2> CalculatePath()
        {
            List <T> collidables = Map.TraceExhaust(Bounds, Start, End, ExcludeIDs, ExcludeFlags);

            if (collidables.Count == 0)
            {
                return(ToList(End));
            }

            if (!Map.Trace(ToList(Bounds), End, ExcludeIDs, ExcludeFlags))
            {
                return(null);
            }

            HashSet <Tuple <int, int, int> > closed = new HashSet <Tuple <int, int, int> >();
            FastPriorityQueue <Unvisited>    open   = new FastPriorityQueue <Unvisited>(256);

            var uvStart = new Unvisited()
            {
                Parent                 = null,
                Location               = Start,
                DistanceStartToHere    = 0,
                HeurDistanceHereToDest = (End - Start).Length()
            };

            QueueCollidables(uvStart, collidables, open, closed);

            while (open.Count > 0)
            {
                Unvisited next = open.Dequeue();
                collidables = Map.TraceExhaust(Bounds, next.Location, End, ExcludeIDs, ExcludeFlags);
                if (collidables.Count == 0)
                {
                    return(Reverse(next));
                }
                QueueCollidables(next, collidables, open, closed);
            }

            return(null);
        }
コード例 #5
0
        private void QueueCollidables(Unvisited from, List <T> cols, FastPriorityQueue <Unvisited> open, HashSet <Tuple <int, int, int> > closed)
        {
            float aabbW = Bounds.AABB.Width;
            float aabbH = Bounds.AABB.Height;

            HashSet <Tuple <int, int, int> > myClosed = null;

            for (int colsInd = 0, colsLen = cols.Count; colsInd < colsLen; colsInd++)
            {
                AACollidable collidable = cols[colsInd];
                Vector2      cent       = collidable.Bounds.Center;
                for (int vertsInd = 0, vertsLen = collidable.Bounds.Vertices.Length; vertsInd < vertsLen; vertsInd++)
                {
                    Vector2 vert = collidable.Bounds.Vertices[vertsInd];
                    for (int myVertInd = 0, myVertsLen = Bounds.Vertices.Length; myVertInd < myVertsLen; myVertInd++)
                    {
                        var tup = Tuple.Create(collidable.ID, vertsInd, myVertInd);
                        if (closed.Contains(tup))
                        {
                            continue;
                        }
                        if (myClosed != null && myClosed.Contains(tup))
                        {
                            continue;
                        }
                        Vector2 myVert = Bounds.Vertices[myVertInd];

                        Vector2 point = new Vector2(vert.X - myVert.X, vert.Y - myVert.Y);
                        point += collidable.Position;

                        if (Math2.Approximately(point, from.Location))
                        {
                            closed.Add(tup);
                            continue;
                        }

                        if (!Map.Contains(Bounds, point))
                        {
                            closed.Add(tup);
                            continue;
                        }

                        var inters = Map.TraceExhaust(ToList(Bounds), point, ExcludeIDs, ExcludeFlags);
                        if (inters.Count != 0)
                        {
                            if (myClosed == null)
                            {
                                myClosed = new HashSet <Tuple <int, int, int> >();
                            }
                            myClosed.Add(tup);

                            for (int i = 0; i < inters.Count; i++)
                            {
                                if (inters[i] != collidable)
                                {
                                    cols.Add(inters[i]);
                                    colsLen++;
                                }
                            }
                            continue;
                        }

                        List <T> tmp = Map.TraceExhaust(Bounds, from.Location, point, ExcludeIDs, ExcludeFlags);
                        if (tmp.Count == 0)
                        {
                            closed.Add(tup);
                            var unv = new Unvisited()
                            {
                                Parent                 = from,
                                Location               = point,
                                DistanceStartToHere    = from.DistanceStartToHere + (point - from.Location).Length(),
                                HeurDistanceHereToDest = (End - point).Length()
                            };
                            open.Enqueue(unv, unv.CorrectPrio);
                        }
                        else
                        {
                            if (myClosed == null)
                            {
                                myClosed = new HashSet <Tuple <int, int, int> >();
                            }
                            myClosed.Add(tup);

                            for (int i = 0; i < tmp.Count; i++)
                            {
                                if (tmp[i] != collidable)
                                {
                                    cols.Add(tmp[i]);
                                    colsLen++;
                                }
                            }
                        }
                    }
                }
            }
        }