Exemplo n.º 1
0
        private Node GetNodeAt(_Point p)
        {
            Node n = new Node();

            n.X = p.X;
            n.Y = p.Y;
            n.G = (Math.Abs(p.X - _start.X) + Math.Abs(p.Y - _start.Y)) / 4;
            n.H = (Math.Abs(p.X - _end.X) + Math.Abs(p.Y - _end.Y)) / 4;
            return(n);
        }
Exemplo n.º 2
0
        internal static TimeSpan LastSearch = new TimeSpan(); //TODO rm

        public List <Point> FindPath(Point start, Point end)
        {
            WasPathFound = false;
            LastSearch   = new TimeSpan(DateTime.Now.Ticks);

            long StartTicks = Main.Ticks;

            open.Clear();
            close.Clear();
            _end   = new _Point(end);
            _start = new _Point(start);
            _Point cur = new _Point(start);
            Node   curn, tpn;
            Node   tpn2i;

            curn   = GetNodeAt(cur);
            curn.F = curn.H;
            close.Add(curn);

            if (InputEngine.curKeyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.S))
            {
            }

            while (true)
            {
                for (int i = 0; i < directions.Length; i++)
                {
                    tpn    = GetNodeAt(cur + directions[i]);
                    tpn.PX = cur.X;
                    tpn.PY = cur.Y;
                    if (tpn.X == _end.X && tpn.Y == _end.Y)
                    {
                        curn = tpn;
                        goto EndFound;
                    }
                    if (Components.ComponentsManager.VisibilityMap.GetAStarValue(tpn.X, tpn.Y) == 0)
                    {
                        continue;
                    }
                    //tpn.H = Components.ComponentsManager.MapVisibility.GetAStarValue(tpn.X, tpn.Y) * 2;
                    //if (tpn.G == curn.G)//VisMap is occupied
                    //    continue;
                    if (tpn.X - tpn.PX != curn.X - curn.PX || tpn.Y - tpn.PY != curn.Y - curn.PY)
                    {
                        tpn.H += 2;
                    }
                    tpn.F = tpn.G + tpn.H;
                    //punish change direction
                    if ((tpn2i = NodeExistsOpenIndex(tpn.X, tpn.Y)) != null)
                    {
                        if (tpn.F < tpn2i.F)
                        {
                            open.Remove(tpn2i);
                        }
                        else
                        {
                            continue;
                        }
                    }
                    //open.Add(tpn);
                    InsertOpenNode(tpn);
                }
                if (open.Count > MAX_OPEN || open.Count == 0 ||
                    (close.Count % 100 == 0 && Main.Ticks - StartTicks > 3))//calculations cannot be longer then 60ms
                {
                    return(null);
                }
                //quicksortOpen();
                curn  = open[0];
                cur.X = curn.X;
                cur.Y = curn.Y;
                open.RemoveAt(0);
                close.Add(curn);
            }
EndFound:
            List <Point> path = new List <Point>();

            path.Add(end);
            while (curn.X != start.X || curn.Y != start.Y)
            {
                path.Add(new Point(curn.X, curn.Y));
                curn = GetClosedParent(ref curn);
            }
            path.Add(start);
            WasPathFound = true;
            if (InputEngine.curKeyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.A))
            {
                SaveDebug();
            }
            return(path);
        }
Exemplo n.º 3
0
        public List<Point> FindPath(Point start, Point end)
        {
            WasPathFound = false;
            LastSearch = new TimeSpan(DateTime.Now.Ticks);

            long StartTicks = Main.Ticks;

            open.Clear();
            close.Clear();
            _end = new _Point(end);
            _start = new _Point(start);
            _Point cur = new _Point(start);
            Node curn, tpn;
            Node tpn2i;

            curn = GetNodeAt(cur);
            curn.F = curn.H;
            close.Add(curn);

            if (InputEngine.curKeyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.S))
            {
            }

            while (true)
            {
                for (int i = 0; i < directions.Length; i++)
                {
                    tpn = GetNodeAt(cur + directions[i]);
                    tpn.PX = cur.X;
                    tpn.PY = cur.Y;
                    if (tpn.X == _end.X && tpn.Y == _end.Y)
                    {
                        curn = tpn;
                        goto EndFound;
                    }
                    if (Components.ComponentsManager.VisibilityMap.GetAStarValue(tpn.X, tpn.Y) == 0)
                        continue;
                    //tpn.H = Components.ComponentsManager.MapVisibility.GetAStarValue(tpn.X, tpn.Y) * 2;
                    //if (tpn.G == curn.G)//VisMap is occupied
                    //    continue;
                    if (tpn.X - tpn.PX != curn.X - curn.PX || tpn.Y - tpn.PY != curn.Y - curn.PY)
                        tpn.H += 2;
                    tpn.F = tpn.G + tpn.H;
                    //punish change direction
                    if ((tpn2i = NodeExistsOpenIndex(tpn.X, tpn.Y)) != null)
                        if (tpn.F < tpn2i.F)
                        {
                            open.Remove(tpn2i);
                        }
                        else
                            continue;
                    //open.Add(tpn);
                    InsertOpenNode(tpn);
                }
                if (open.Count > MAX_OPEN || open.Count == 0 ||
                    (close.Count % 100 == 0 && Main.Ticks - StartTicks > 3))//calculations cannot be longer then 60ms
                        return null;
                //quicksortOpen();
                curn = open[0];
                cur.X = curn.X;
                cur.Y = curn.Y;
                open.RemoveAt(0);
                close.Add(curn);
            }
            EndFound:
            List<Point> path = new List<Point>();
            path.Add(end);
            while (curn.X != start.X || curn.Y != start.Y)
            {
                path.Add(new Point(curn.X, curn.Y));
                curn = GetClosedParent(ref curn);
            }
            path.Add(start);
            WasPathFound = true;
            if (InputEngine.curKeyboard.IsKeyDown(Microsoft.Xna.Framework.Input.Keys.A))
                SaveDebug();
            return path;
        }
Exemplo n.º 4
0
 private Node GetNodeAt(_Point p)
 {
     Node n = new Node();
     n.X = p.X;
     n.Y = p.Y;
     n.G = (Math.Abs(p.X - _start.X) + Math.Abs(p.Y - _start.Y)) / 4;
     n.H = (Math.Abs(p.X - _end.X) + Math.Abs(p.Y - _end.Y)) / 4;
     return n;
 }