コード例 #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="destination">Desired location to move</param>
        /// <param name="walkingSpeedMin">Minimal walking speed during the move</param>
        /// <param name="walkingSpeedMax">Maximal walking speed during the move</param>
        /// <param name="functionExecutedWhileWalking">Functions #1 to be exec while walking, like task or smth</param>
        /// <param name="functionExecutedWhileWalking2">Functions #1 to be exec while walking, like task or smth</param>
        /// <param name="cancellationToken">regular session cancelation token</param>
        /// <param name="session">ISession param of the bot, to detect which bot started it</param>
        /// <param name="direct">Directly move to the point, skip routing services</param>
        /// <param name="waypointsToVisit">Waypoints to visit during the move, required to redure Google Directions API usage</param>
        /// <param name="eggWalker"></param>
        /// <returns></returns>
        internal async Task <PlayerUpdateResponse> Move(GeoCoordinate destination, double walkingSpeedMin, double walkingSpeedMax, Func <Task <bool> > functionExecutedWhileWalking, Func <Task <bool> > functionExecutedWhileWalking2,
                                                        CancellationToken cancellationToken, ISession session, bool direct = false, List <GeoCoordinate> waypointsToVisit = null, EggWalker eggWalker = null)
        {
            var currentLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude, _client.CurrentAltitude);
            var result          = new PlayerUpdateResponse();

            if (session.LogicSettings.UseHumanPathing)
            {
                var waypoints = new List <GeoCoordinate>();

                if (!direct)
                {
                    RoutingResponse routingResponse = null;
                    try
                    {
                        switch (session.LogicSettings.RoutingService)
                        {
                        case RoutingService.MobBot:
                            routingResponse = Routing.GetRoute(currentLocation, destination, session);
                            break;

                        case RoutingService.OpenLs:
                            routingResponse = OsmRouting.GetRoute(currentLocation, destination, session);
                            break;

                        case RoutingService.GoogleDirections:
                            routingResponse = GoogleRouting.GetRoute(currentLocation, destination, session,
                                                                     waypointsToVisit);
                            break;

                        case RoutingService.MapzenValhalla:
                            routingResponse = MapzenRouting.GetRoute(currentLocation, destination, session,
                                                                     waypointsToVisit);
                            break;
                        }
                    }
                    catch (NullReferenceException ex)
                    {
                        session.EventDispatcher.Send(new DebugEvent
                        {
                            Message = ex.ToString()
                        });
                        routingResponse = new RoutingResponse();
                    }

                    if (routingResponse?.Coordinates != null)
                    {
                        foreach (var item in routingResponse.Coordinates)
                        {
                            if (item == null)
                            {
                                continue;
                            }
                            //0 = lat, 1 = long (MAYBE NOT THO?)
                            switch (session.LogicSettings.RoutingService)
                            {
                            case RoutingService.MobBot:
                                waypoints.Add(new GeoCoordinate(item[1], item[0]));
                                break;

                            case RoutingService.OpenLs:
                                waypoints.Add(new GeoCoordinate(item.ToArray()[1], item.ToArray()[0], item.ToArray()[2]));
                                break;

                            case RoutingService.GoogleDirections:
                                waypoints.Add(new GeoCoordinate(item[0], item[1]));
                                break;

                            case RoutingService.MapzenValhalla:
                                waypoints.Add(new GeoCoordinate(item[0], item[1]));
                                break;
                            }
                        }
                        if ((session.LogicSettings.RoutingService == RoutingService.GoogleDirections || session.LogicSettings.RoutingService == RoutingService.MobBot || session.LogicSettings.RoutingService == RoutingService.MapzenValhalla) && session.LogicSettings.UseMapzenApiElevation)
                        {
                            waypoints = await session.MapzenApi.FillAltitude(waypoints);
                        }
                    }
                }

                if (waypoints.Count == 0)
                {
                    waypoints.Add(destination);
                }
                else if (waypoints.Count > 1)
                {
                    var nextPath = waypoints.Select(item => Tuple.Create(item.Latitude, item.Longitude)).ToList();
                    session.EventDispatcher.Send(new NextRouteEvent
                    {
                        Coords = nextPath
                    });
                    destination = waypoints.Last();
                }

                var  navi               = new Navigation(_client, UpdatePositionEvent);
                var  waypointsArr       = waypoints.ToArray();
                long nextMaintenceStamp = 0;
                //MILD REWRITE TO USE HUMANPATHWALKING;
                foreach (var t in waypointsArr)
                {
                    if (session.ForceMoveTo != null)
                    {
                        return(await ForceMoveTask.Execute(session, cancellationToken));
                    }
                    // skip waypoints under 5 meters
                    var sourceLocation   = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);
                    var distanceToTarget = LocationUtils.CalculateDistanceInMeters(sourceLocation, t);
                    if (distanceToTarget <= 5)
                    {
                        continue;
                    }

                    var nextSpeed = RandomExtensions.NextInRange(session.Client.rnd, walkingSpeedMin, walkingSpeedMax) * session.Settings.MoveSpeedFactor;
                    session.State = BotState.Walk;
                    result        = await navi.HumanPathWalking(session, t, nextSpeed, functionExecutedWhileWalking, functionExecutedWhileWalking2, cancellationToken);

                    if (session.Runtime.BreakOutOfPathing)
                    {
                        return(result);
                    }
                    UpdatePositionEvent?.Invoke(t.Latitude, t.Longitude, t.Altitude);

                    if (nextMaintenceStamp < DateTime.UtcNow.ToUnixTime())
                    {
                        await MaintenanceTask.Execute(session, cancellationToken);

                        nextMaintenceStamp = DateTime.UtcNow.AddMinutes(3).ToUnixTime();
                    }
                    if (eggWalker != null)
                    {
                        await eggWalker.ApplyDistance(distanceToTarget, cancellationToken);
                    }
                }
                session.State = BotState.Idle;
                var curcoord = new GeoCoordinate(session.Client.CurrentLatitude, session.Client.CurrentLongitude);
                if (LocationUtils.CalculateDistanceInMeters(curcoord, destination) > 40)
                {
                    var nextSpeed = RandomExtensions.NextInRange(session.Client.rnd, walkingSpeedMin, walkingSpeedMax) * session.Settings.MoveSpeedFactor;

                    result = await navi.HumanPathWalking(session, destination, nextSpeed, functionExecutedWhileWalking, functionExecutedWhileWalking2, cancellationToken);
                }
                await MaintenanceTask.Execute(session, cancellationToken);
            }
            else
            {
                if (destination.Latitude.Equals(session.Runtime.lastPokeStopCoordinate.Latitude) && destination.Longitude.Equals(session.Runtime.lastPokeStopCoordinate.Longitude))
                {
                    session.Runtime.BreakOutOfPathing = true;
                }

                if (session.Runtime.BreakOutOfPathing)
                {
                    await MaintenanceTask.Execute(session, cancellationToken);

                    return(result);
                }
                var navi     = new Navigation(_client, UpdatePositionEvent);
                var curcoord = new GeoCoordinate(session.Client.CurrentLatitude, session.Client.CurrentLongitude);
                if (LocationUtils.CalculateDistanceInMeters(curcoord, destination) > 40)
                {
                    var nextSpeed = RandomExtensions.NextInRange(session.Client.rnd, walkingSpeedMin, walkingSpeedMax) * session.Settings.MoveSpeedFactor;

                    result = await navi.HumanPathWalking(session, destination, nextSpeed, functionExecutedWhileWalking, functionExecutedWhileWalking2, cancellationToken);
                }
            }
            session.State = BotState.Idle;
            await MaintenanceTask.Execute(session, cancellationToken);

            return(result);
        }
コード例 #2
0
        private static async Task <List <GeoCoordinate> > CreateHumanRoute(GeoCoordinate destination, CancellationToken cancellationToken, ISession session,
                                                                           bool direct, List <GeoCoordinate> waypointsToVisit, GeoCoordinate currentLocation)
        {
            var waypoints = new List <GeoCoordinate>();

            if (!direct)
            {
                RoutingResponse routingResponse = null;
                try
                {
                    switch (session.LogicSettings.RoutingService)
                    {
                    case RoutingService.OpenLs:
                        routingResponse = OsmRouting.GetRoute(currentLocation, destination, session);
                        break;

                    case RoutingService.GoogleDirections:
                        routingResponse = GoogleRouting.GetRoute(currentLocation, destination, session,
                                                                 waypointsToVisit);
                        break;

                    case RoutingService.MapzenValhalla:
                        routingResponse = MapzenRouting.GetRoute(currentLocation, destination, session,
                                                                 waypointsToVisit);
                        break;
                    }
                }
                catch (NullReferenceException ex)
                {
                    session.EventDispatcher.Send(new DebugEvent
                    {
                        Message = ex.ToString()
                    });
                    routingResponse = new RoutingResponse();
                }

                if (routingResponse?.Coordinates == null)
                {
                    return(waypoints);
                }
                foreach (var item in routingResponse.Coordinates)
                {
                    if (item == null)
                    {
                        continue;
                    }
                    //0 = lat, 1 = long (MAYBE NOT THO?)
                    switch (session.LogicSettings.RoutingService)
                    {
                    case RoutingService.OpenLs:
                        waypoints.Add(new GeoCoordinate(item.ToArray()[1], item.ToArray()[0], item.ToArray()[2]));
                        break;

                    case RoutingService.GoogleDirections:
                        waypoints.Add(new GeoCoordinate(item[0], item[1]));
                        break;

                    case RoutingService.MapzenValhalla:
                        waypoints.Add(new GeoCoordinate(item[0], item[1]));
                        break;
                    }
                }
                if ((session.LogicSettings.RoutingService == RoutingService.GoogleDirections ||
                     session.LogicSettings.RoutingService == RoutingService.MapzenValhalla) &&
                    session.LogicSettings.UseMapzenApiElevation)
                {
                    return(await session.MapzenApi.FillAltitude(waypoints, token : cancellationToken));
                }
                return(waypoints);
            }
            waypoints.Add(destination);
            return(waypoints);
        }
コード例 #3
0
ファイル: Navigation2.cs プロジェクト: ragekwit/Catchem-PoGo
        public async Task <PlayerUpdateResponse> Move(GeoCoordinate destination, double walkingSpeedMin, double walkingSpeedMax, Func <Task <bool> > functionExecutedWhileWalking, Func <Task <bool> > functionExecutedWhileWalking2,
                                                      CancellationToken cancellationToken, ISession session, bool direct = false)
        {
            var currentLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude, _client.CurrentAltitude);
            var result          = new PlayerUpdateResponse();

            if (session.LogicSettings.UseHumanPathing)
            {
                ////initial coordinate generaton

                ////prepare the result object for further manipulation + return


                ////initial time
                //var requestSendDateTime = DateTime.Now;
                //var distanceToDest = LocationUtils.CalculateDistanceInMeters(currentLocation, destination);
                //double metersPerInterval = 0.5; //approximate meters for each interval/waypoint to be spaced from the last.
                //////get distance ofc
                //////create segments
                //var segments = Math.Floor(distanceToDest / metersPerInterval);
                List <GeoCoordinate> waypoints = new List <GeoCoordinate>();
                ////get differences in lat / long
                //var latDiff = Math.Abs(currentLocation.Latitude - destination.Latitude);
                //var lonDiff = Math.Abs(currentLocation.Longitude - destination.Longitude);
                //var latAdd = latDiff / segments;
                //var lonAdd = latDiff / segments;
                //var lastLat = currentLocation.Latitude;
                //var lastLon = currentLocation.Longitude;
                ////generate waypoints old code -goes in straight line basically
                //for (int i = 0; i < segments; i++)
                //{
                //    //TODO: add altitude calculations into everything
                //    lastLat += latAdd;
                //    lastLon += lonAdd;
                //    waypoints.Add(new GeoCoordinate(lastLat, lastLon, currentLocation.Altitude));
                //}

                //TODO: refactor the generation of waypoint code to break the waypoints given to us by the routing information down into segements like above.
                //generate waypoints new code
                if (!direct)
                {
                    //var routingResponse = OsmRouting.GetRoute(currentLocation, destination, session);
                    //waypoints = routingResponse.Coordinates;
                    RoutingResponse routingResponse;
                    try
                    {
                        routingResponse = !session.LogicSettings.UseOpenLsRouting ? Routing.GetRoute(currentLocation, destination) : OsmRouting.GetRoute(currentLocation, destination, session);
                    }
                    catch (NullReferenceException ex)
                    {
                        session.EventDispatcher.Send(new DebugEvent
                        {
                            Message = ex.ToString()
                        });
                        routingResponse = new RoutingResponse();
                    }

                    if (routingResponse?.Coordinates != null)
                    {
                        foreach (var item in routingResponse.Coordinates)
                        {
                            if (item == null)
                            {
                                continue;
                            }
                            //0 = lat, 1 = long (MAYBE NOT THO?)
                            waypoints.Add(!session.LogicSettings.UseOpenLsRouting
                                    ? new GeoCoordinate(item.ToArray()[1], item.ToArray()[0],
                                                        session.LogicSettings.UseMapzenApiElevation ? session.MapzenApi.GetAltitude(item.ToArray()[1], item.ToArray()[0]) : 0)
                                    : new GeoCoordinate(item.ToArray()[1], item.ToArray()[0], item.ToArray()[2]));
                        }
                    }
                }

                if (waypoints.Count == 0)
                {
                    waypoints.Add(destination);
                }
                else if (waypoints.Count > 1)
                {
                    var nextPath = waypoints.Select(item => Tuple.Create(item.Latitude, item.Longitude)).ToList();
                    session.EventDispatcher.Send(new NextRouteEvent
                    {
                        Coords = nextPath
                    });
                }


                //var timeSinceMoveStart = DateTime.Now.Ticks;
                //double curAcceleration = 1.66; //Lets assume we accelerate at 1.66 m/s ish. TODO: Fuzz this a bit
                //double curWalkingSpeed = 0;
                //double maxWalkingSpeed = (session.LogicSettings.WalkingSpeedInKilometerPerHour / 3.6); //Get movement speed in meters

                ////TODO: Maybe update SensorInfo to replicate/mimic movement, or is it fine as is?
                //bool StopWalking = false;
                //double TimeToAccelerate = GetAccelerationTime(curWalkingSpeed, maxWalkingSpeed, curAcceleration);
                ////double InitialMove = getDistanceTraveledAccelerating(TimeToAccelerate, curAcceleration, curWalkingSpeed);


                //double MoveLeft = curWalkingSpeed;
                //bool NeedMoreMove = false;
                //bool StopMove = false;
                //int UpdateInterval = 1; // in seconds - any more than this breaks the calculations for distance and such. It all relys on taking ~1 second to perform the actions needed, pretty much.

                //makes you appear to move slower if you're catching pokemon, hitting stops, etc.
                //This feels like more human behavior. Dunnomateee
                Navigation navi         = new Navigation(_client, UpdatePositionEvent);
                var        waypointsArr = waypoints.ToArray();
                //MILD REWRITE TO USE HUMANPATHWALKING;
                foreach (var t in waypointsArr)
                {
                    if (session.ForceMoveTo != null)
                    {
                        return(await ForceMoveTask.Execute(session, cancellationToken));
                    }
                    // skip waypoints under 5 meters
                    var sourceLocation   = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);
                    var distanceToTarget = LocationUtils.CalculateDistanceInMeters(sourceLocation,
                                                                                   t);
                    if (distanceToTarget <= 5)
                    {
                        continue;
                    }

                    var nextSpeed = session.Client.rnd.NextInRange(walkingSpeedMin, walkingSpeedMax) * session.Settings.MoveSpeedFactor;

                    result = await
                             navi.HumanPathWalking(t, nextSpeed,
                                                   functionExecutedWhileWalking, functionExecutedWhileWalking2, cancellationToken);

                    if (RuntimeSettings.BreakOutOfPathing)
                    {
                        return(result);
                    }
                    UpdatePositionEvent?.Invoke(t.Latitude, t.Longitude, t.Altitude);
                    //Console.WriteLine("Hit waypoint " + x);
                }
                var curcoord = new GeoCoordinate(session.Client.CurrentLatitude, session.Client.CurrentLongitude);
                if (LocationUtils.CalculateDistanceInMeters(curcoord, destination) > 40)
                {
                    var nextSpeed = session.Client.rnd.NextInRange(walkingSpeedMin, walkingSpeedMax) * session.Settings.MoveSpeedFactor;

                    result = await navi.HumanPathWalking(destination, nextSpeed,
                                                         functionExecutedWhileWalking, functionExecutedWhileWalking2, cancellationToken);
                }
            }
            else
            {
                if (destination.Latitude.Equals(RuntimeSettings.lastPokeStopCoordinate.Latitude) &&
                    destination.Longitude.Equals(RuntimeSettings.lastPokeStopCoordinate.Longitude))
                {
                    RuntimeSettings.BreakOutOfPathing = true;
                }

                if (RuntimeSettings.BreakOutOfPathing)
                {
                    await MaintenanceTask.Execute(session, cancellationToken);

                    return(result);
                }
                var navi     = new Navigation(_client, UpdatePositionEvent);
                var curcoord = new GeoCoordinate(session.Client.CurrentLatitude, session.Client.CurrentLongitude);
                if (LocationUtils.CalculateDistanceInMeters(curcoord, destination) > 40)
                {
                    var nextSpeed = session.Client.rnd.NextInRange(walkingSpeedMin, walkingSpeedMax) * session.Settings.MoveSpeedFactor;

                    result = await navi.HumanPathWalking(destination, nextSpeed,
                                                         functionExecutedWhileWalking, functionExecutedWhileWalking2, cancellationToken);
                }
            }
            await MaintenanceTask.Execute(session, cancellationToken);

            return(result);
        }