Exemple #1
0
        public IEnumerable <MyPathNode> GetPath(Objects.Location loc, List <Location> BlockingLocs = null)
        {
            lock (lockThis)
            {
                int maxX = 150;
                int maxY = 150;
                //      IsWalking = true;

                StopWalk = false;
                if (!loc.IsInRange(maxX, maxY))
                {
                    client.StatusBar = "Destination is out of range";
                    return(null);
                }


                int playerX, playerY, playerZ, playerId;

                playerX = client.PlayerLocation.X;
                playerY = client.PlayerLocation.Y;
                playerZ = client.PlayerLocation.Z;
                int StartX, StartY, EndX, EndY;
                StartX   = maxX;
                StartY   = maxY;
                EndX     = loc.X - playerX + maxX;
                EndY     = loc.Y - playerY + maxY;
                playerId = client.Player.Id;
                if (client.PlayerLocation.Z != loc.Z)
                {
                    return(null);
                }
                MyPathNode[,] grid = new MyPathNode[maxX * 2, maxY * 2];


                if (StopWalk)
                {
                    return(null);
                }


                this.LoadMapfiles();



                for (int x = 0; x < maxX * 2; x++)
                {
                    for (int y = 0; y < maxX * 2; y++)
                    {
                        int  xdiff        = playerX + x - maxX;
                        int  ydiff        = playerY + y - maxY;
                        int  MovmentSpeed = GetTileCost(xdiff, ydiff);
                        bool isWall       = IsBlocking(xdiff, ydiff);


                        grid[x, y] = new MyPathNode()
                        {
                            IsWall = isWall,
                            X      = x,
                            Y      = y,
                            Cost   = 1,
                        };
                    }
                }


                foreach (Tile t in client.Map.GetTilesSameFloor())
                {
                    bool isWall = false;
                    int  cost = 0;
                    int  cx, cy;
                    cx = t.Location.X - playerX + 300 / 2;
                    cy = t.Location.Y - playerY + 300 / 2;

                    /*if (t.IsBlocking())
                     * {
                     *   isWall = true;
                     *   cost = 500;
                     *
                     * }
                     */
                    if (t.Ground.ItemData.Blocking || t.Ground.ItemData.BlocksPath || t.Items.Any(i => i.ItemData.Blocking || i.ItemData.BlocksPath && !WalkAbleIds.Contains(i.Id)))
                    {
                        isWall = true;
                        cost   = 500;
                    }
                    foreach (TileObject o in t.Objects)
                    {
                        if (o.Id == 99)
                        {
                            //has creature
                            if (o.Data == Core.client.Memory.ReadInt32(client.Addresses.Player.Id))
                            {
                                break;
                            }
                            Creature cr = Core.client.Battlelist.GetCreatures().FirstOrDefault(c => c.Id == o.Data);
                            if (cr != null)
                            {
                                Objects.Bot.Target target = Core.Global.TargetList.FirstOrDefault(j => j.Name.ToLower() == cr.Name.ToLower());
                                if (target == null)
                                {
                                    //it means it is a monster that we dont want.
                                    isWall = true;
                                    cost   = 500;
                                }
                            }
                        }
                    }
                    grid[cx, cy] = new MyPathNode()
                    {
                        IsWall = isWall,
                        // X = t.Location.X - playerX,
                        // Y = t.Location.Y - playerY,
                        X    = cx,
                        Y    = cy,
                        Cost = 1,
                    };
                }
                if (BlockingLocs != null)
                {
                    foreach (Location l in BlockingLocs)
                    {
                        int cx, cy;
                        cx           = l.X - playerX + 300 / 2;
                        cy           = l.Y - playerY + 300 / 2;
                        grid[cx, cy] = new MyPathNode()
                        {
                            IsWall = false,
                            X      = cx,
                            Y      = cy,
                            Cost   = 2000,
                        };
                    }
                }

                grid[EndX, EndY] = new MyPathNode()
                {
                    IsWall = false,
                    X      = EndX,
                    Y      = EndY,
                    Cost   = 0,
                };



                MySolver <MyPathNode, Object> aStar = new MySolver <MyPathNode, Object>(grid);
                IEnumerable <MyPathNode>      path  = aStar.Search(new Point(StartX, StartY), new Point(EndX, EndY), null);
                return(path);
            }
        }