Exemple #1
0
        //---------------------------------------------------------------------------------------------

        public void RemoveDiagonalCorners(PathNode parent, PathBuilder builder)
        {
            int count = this.Count;

            for (int i = count - 1; i >= 0; i--)
            {
                PathNode current = this[i];
                if (current.DiagonalToParent)
                {
                    PathNodeList currentAdje = builder.FindAdjacentNodes(current);
                    currentAdje.MergePossible(this);
                    if (currentAdje.Count == 1 || currentAdje.Count == 0)
                    {
                        this.RemoveAt(i);
                    }
                }
            }
        }
Exemple #2
0
        //---------------------------------------------------------------------------------------------

        private bool TryGoTo(ushort x, ushort y, int fromDistance, int tries, PathNodeList localList)
        {
            IUOPosition destination = this.CreatePositionInstance(x, y, 0);

            if (localList == null)
            {
                localList = new PathNodeList();
            }
            if (EnableLog)
            {
                Game.PrintMessage("TryGoTo : " + x + ", " + y + " - " + tries);
            }

            if (this.ActualPosition.Equals(destination) || Robot.GetRelativeVectorLength(this.ActualPosition, destination) <= fromDistance)
            {
                if (EnableLog)
                {
                    Game.PrintMessage("TryGoTo OK - 1: " + x + ", " + y);
                }

                return(true);
            }

            PathNode start = new PathNode()
            {
                X = World.Player.X, Y = World.Player.Y
            };
            PathNode end = new PathNode()
            {
                X = x, Y = y
            };


            if (tries > 0)
            {
                using (PathBuilder builder = new PathBuilder(UseCachedPathList ? this.PossibleNodes : localList))
                {
                    if (builder.ComputePath(start, end, fromDistance))
                    {
                        PathNodeList computedPath = new PathNodeList();
                        computedPath.AddRange(builder.ComputedPathNodes);
                        computedPath.Reverse();

                        if (EnableLog)
                        {
                            Game.PrintMessage("TryGoTo Found: " + x + ", " + y + " / " + builder.Searchs);
                        }

                        int step = 0;
                        foreach (PathNode node in computedPath)
                        {
                            step++;
                            IUOPosition  pos  = this.CreatePositionInstance(node.X, node.Y, x);
                            GotoStepArgs args = new GotoStepArgs(pos, tries, fromDistance);
                            if (this.BeforeMove != null)
                            {
                                this.BeforeMove(this, args);
                            }

                            bool moveFail = false;

                            if (args.Abort)
                            {
                                Game.PrintMessage("TryGoTo Abort " + destination);
                                return(false);
                            }

                            if (args.IvalidDestination)
                            {
                                if (EnableLog)
                                {
                                    Game.PrintMessage("Path IvalidDestination: " + this.ActualPosition.ToString() + " to " + node.ToString());
                                }
                                moveFail = true;
                            }
                            else
                            {
                                if (!this.Move(GetMovementDirection(this.GetAngle(pos))))
                                {
                                    if (!this.Move(GetMovementDirection(this.GetAngle(pos))))
                                    {
                                        moveFail = true;
                                    }
                                }
                            }

                            if (moveFail)
                            {
                                if (EnableLog)
                                {
                                    Game.PrintMessage("PathFail: " + this.ActualPosition.ToString() + " to " + node.ToString());
                                }

                                PathNode findNode = builder.PossibleNodes.FindNode(node.X, node.Y);
                                if (findNode != null && !findNode.Walkable.HasValue)
                                {
                                    findNode.Walkable = false;
                                }

                                return(TryGoTo(x, y, fromDistance, --tries, localList));
                            }

                            if (this.AfterMoveSuccess != null)
                            {
                                this.AfterMoveSuccess(this, new GotoStepArgs(pos, tries, fromDistance));
                            }

                            if (args.Abort)
                            {
                                return(false);
                            }
                        }

                        //            09:44 Phoenix: TryGoTo: 1380, 2706 - 459
                        //09:44 Phoenix: Compute Path FAIL: 1380,2707 to 1380,2706
                        //09:44 Phoenix: TryGoTo END: 1380, 2706

                        if (this.ActualPosition.Equals(destination) || Robot.GetRelativeVectorLength(this.ActualPosition, destination) <= fromDistance)
                        {
                            if (EnableLog)
                            {
                                Game.PrintMessage("TryGoTo OK - 2: " + x + ", " + y);
                            }

                            this.OnGoToSuccess(this, EventArgs.Empty);
                            return(true);
                        }
                        else
                        {
                            return(TryGoTo(x, y, fromDistance, --tries, localList));
                        }
                    }
                    else
                    {
                        if (EnableLog)
                        {
                            Game.PrintMessage("Compute Path FAIL - Restart: " + start.ToString() + " to " + end.ToString());
                        }

                        return(TryGoTo(x, y, fromDistance, --tries, null));
                    }
                }
            }


            if (EnableLog)
            {
                Game.PrintMessage("TryGoTo END: " + x + ", " + y);
            }

            return(false);
        }