Add back old navigation method

Brian [2016-08-20 10:32:50]
Add back old navigation method
Filename
PokemonGo.RocketBot.Logic/Navigation.cs
PokemonGo.RocketBot.Window/Forms/MainForm.cs
diff --git a/PokemonGo.RocketBot.Logic/Navigation.cs b/PokemonGo.RocketBot.Logic/Navigation.cs
index e002987..7b7b661 100644
--- a/PokemonGo.RocketBot.Logic/Navigation.cs
+++ b/PokemonGo.RocketBot.Logic/Navigation.cs
@@ -11,10 +11,10 @@ using System.Threading.Tasks;
 using GeoCoordinatePortable;
 using Newtonsoft.Json.Linq;
 using PokemonGo.RocketAPI;
-using PokemonGo.RocketBot.Logic.Utils;
-using POGOProtos.Networking.Responses;
 using PokemonGo.RocketBot.Logic.Event;
 using PokemonGo.RocketBot.Logic.State;
+using PokemonGo.RocketBot.Logic.Utils;
+using POGOProtos.Networking.Responses;

 #endregion

@@ -24,13 +24,12 @@ namespace PokemonGo.RocketBot.Logic

     public class Navigation
     {
+        private const double SpeedDownTo = 10/3.6;
+        private readonly Client _client;
+        private readonly Random _randWalking = new Random();
         private double _currentWalkingSpeed;
         private DateTime _lastMajorVariantWalkingSpeed;
         private DateTime _nextMajorVariantWalkingSpeed;
-        private readonly Random _randWalking = new Random();
-
-        private const double SpeedDownTo = 10 / 3.6;
-        private readonly Client _client;

         public Navigation(Client client)
         {
@@ -41,7 +40,7 @@ namespace PokemonGo.RocketBot.Logic
         {
             if (_lastMajorVariantWalkingSpeed == DateTime.MinValue && _nextMajorVariantWalkingSpeed == DateTime.MinValue)
             {
-                var minutes = _randWalking.NextDouble() * (2 - 6) + 2;
+                var minutes = _randWalking.NextDouble()*(2 - 6) + 2;
                 _lastMajorVariantWalkingSpeed = DateTime.Now;
                 _nextMajorVariantWalkingSpeed = _lastMajorVariantWalkingSpeed.AddMinutes(minutes);
                 _currentWalkingSpeed = session.LogicSettings.WalkingSpeedInKilometerPerHour;
@@ -50,11 +49,13 @@ namespace PokemonGo.RocketBot.Logic
             {
                 var oldWalkingSpeed = _currentWalkingSpeed;

-                var randomMin = session.LogicSettings.WalkingSpeedInKilometerPerHour - session.LogicSettings.WalkingSpeedVariant;
-                var randomMax = session.LogicSettings.WalkingSpeedInKilometerPerHour + session.LogicSettings.WalkingSpeedVariant;
-                _currentWalkingSpeed = _randWalking.NextDouble() * (randomMax - randomMin) + randomMin;
+                var randomMin = session.LogicSettings.WalkingSpeedInKilometerPerHour -
+                                session.LogicSettings.WalkingSpeedVariant;
+                var randomMax = session.LogicSettings.WalkingSpeedInKilometerPerHour +
+                                session.LogicSettings.WalkingSpeedVariant;
+                _currentWalkingSpeed = _randWalking.NextDouble()*(randomMax - randomMin) + randomMin;

-                var minutes = _randWalking.NextDouble() * (2 - 6) + 2;
+                var minutes = _randWalking.NextDouble()*(2 - 6) + 2;
                 _lastMajorVariantWalkingSpeed = DateTime.Now;
                 _nextMajorVariantWalkingSpeed = _lastMajorVariantWalkingSpeed.AddMinutes(minutes);

@@ -65,7 +66,7 @@ namespace PokemonGo.RocketBot.Logic
                 });
             }

-            return _currentWalkingSpeed / 3.6;
+            return _currentWalkingSpeed/3.6;
         }

         private double MinorWalkingSpeedVariant(ISession session)
@@ -76,17 +77,19 @@ namespace PokemonGo.RocketBot.Logic

                 if (_randWalking.Next(1, 10) > 5) //Random change upper or lower variant speed
                 {
-                    var randomMax = session.LogicSettings.WalkingSpeedInKilometerPerHour + session.LogicSettings.WalkingSpeedVariant + 0.5;
+                    var randomMax = session.LogicSettings.WalkingSpeedInKilometerPerHour +
+                                    session.LogicSettings.WalkingSpeedVariant + 0.5;

-                    _currentWalkingSpeed += _randWalking.NextDouble() * (0.01 - 0.09) + 0.01;
+                    _currentWalkingSpeed += _randWalking.NextDouble()*(0.01 - 0.09) + 0.01;
                     if (_currentWalkingSpeed > randomMax)
                         _currentWalkingSpeed = randomMax;
                 }
                 else
                 {
-                    var randomMin = session.LogicSettings.WalkingSpeedInKilometerPerHour - session.LogicSettings.WalkingSpeedVariant - 0.5;
+                    var randomMin = session.LogicSettings.WalkingSpeedInKilometerPerHour -
+                                    session.LogicSettings.WalkingSpeedVariant - 0.5;

-                    _currentWalkingSpeed -= _randWalking.NextDouble() * (0.01 - 0.09) + 0.01;
+                    _currentWalkingSpeed -= _randWalking.NextDouble()*(0.01 - 0.09) + 0.01;
                     if (_currentWalkingSpeed < randomMin)
                         _currentWalkingSpeed = randomMin;
                 }
@@ -101,31 +104,31 @@ namespace PokemonGo.RocketBot.Logic
                 }
             }

-            return _currentWalkingSpeed / 3.6;
+            return _currentWalkingSpeed/3.6;
         }

         private List<List<double>> Route(ISession session, GeoCoordinate start, GeoCoordinate dest)
         {
-            List<List<double>> result = new List<List<double>>();
+            var result = new List<List<double>>();

             try
             {
-                WebRequest web = WebRequest.Create(
+                var web = WebRequest.Create(
                     $"https://maps.googleapis.com/maps/api/directions/json?origin={start.Latitude},{start.Longitude}&destination={dest.Latitude},{dest.Longitude}&mode=walking&units=metric&key={session.LogicSettings.GoogleAPIKey}");
                 web.Credentials = CredentialCache.DefaultCredentials;

                 string strResponse;
-                using (WebResponse response = web.GetResponse())
+                using (var response = web.GetResponse())
                 {
-                    using (Stream stream = response.GetResponseStream())
+                    using (var stream = response.GetResponseStream())
                     {
                         Debug.Assert(stream != null, "stream != null");
-                        using (StreamReader reader = new StreamReader(stream))
+                        using (var reader = new StreamReader(stream))
                             strResponse = reader.ReadToEnd();
                     }
                 }

-                JObject parseObject = JObject.Parse(strResponse);
+                var parseObject = JObject.Parse(strResponse);
                 result = Points(parseObject["routes"][0]["overview_polyline"]["points"].ToString(), 1e5);
             }
             catch (WebException e)
@@ -151,15 +154,15 @@ namespace PokemonGo.RocketBot.Logic
             if (string.IsNullOrEmpty(overview))
                 throw new ArgumentNullException("Points");

-            bool polyline = false;
+            var polyline = false;
             int index = 0, lat = 0, lng = 0;
-            char[] polylineChars = overview.ToCharArray();
-            List<List<double>> result = new List<List<double>>();
+            var polylineChars = overview.ToCharArray();
+            var result = new List<List<double>>();

             while (index < polylineChars.Length)
             {
                 int sum = 0, shifter = 0, nextBits;
-                List<double> coordinates = new List<double>();
+                var coordinates = new List<double>();

                 do
                 {
@@ -172,12 +175,12 @@ namespace PokemonGo.RocketBot.Logic
                     break;

                 if (!polyline)
-                    lat += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
+                    lat += (sum & 1) == 1 ? ~(sum >> 1) : sum >> 1;
                 else
                 {
-                    lng += (sum & 1) == 1 ? ~(sum >> 1) : (sum >> 1);
-                    coordinates.Add(lng / precision);
-                    coordinates.Add(lat / precision);
+                    lng += (sum & 1) == 1 ? ~(sum >> 1) : sum >> 1;
+                    coordinates.Add(lng/precision);
+                    coordinates.Add(lat/precision);
                     result.Add(coordinates);
                 }

@@ -187,7 +190,8 @@ namespace PokemonGo.RocketBot.Logic
             return result;
         }

-        public async Task<PlayerUpdateResponse> Move(GeoCoordinate targetLocation, Func<Task<bool>> functionExecutedWhileWalking,
+        public async Task<PlayerUpdateResponse> Move(GeoCoordinate targetLocation,
+            Func<Task<bool>> functionExecutedWhileWalking,
             ISession session,
             CancellationToken cancellationToken)
         {
@@ -195,7 +199,7 @@ namespace PokemonGo.RocketBot.Logic
             if (!session.LogicSettings.DisableHumanWalking)
             {
                 PlayerUpdateResponse result = null;
-                List<GeoCoordinate> points = new List<GeoCoordinate>();
+                var points = new List<GeoCoordinate>();
                 var route = Route(session,
                     new GeoCoordinate(
                         _client.CurrentLatitude,
@@ -206,15 +210,17 @@ namespace PokemonGo.RocketBot.Logic
                 foreach (var item in route)
                     points.Add(new GeoCoordinate(item.ToArray()[1], item.ToArray()[0]));

-                for (int i = 0; i < points.Count; i++)
+                for (var i = 0; i < points.Count; i++)
                 {
-                    var speedInMetersPerSecond = session.LogicSettings.UseWalkingSpeedVariant ? MajorWalkingSpeedVariant(session) :
-                    session.LogicSettings.WalkingSpeedInKilometerPerHour / 3.6;
+                    var speedInMetersPerSecond = session.LogicSettings.UseWalkingSpeedVariant
+                        ? MajorWalkingSpeedVariant(session)
+                        : session.LogicSettings.WalkingSpeedInKilometerPerHour/3.6;
                     var sourceLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);

                     var nextWaypointBearing = LocationUtils.DegreeBearing(sourceLocation, points.ToArray()[i]);
                     var nextWaypointDistance = speedInMetersPerSecond;
-                    var waypoint = LocationUtils.CreateWaypoint(sourceLocation, nextWaypointDistance, nextWaypointBearing);
+                    var waypoint = LocationUtils.CreateWaypoint(sourceLocation, nextWaypointDistance,
+                        nextWaypointBearing);

                     var requestSendDateTime = DateTime.Now;
                     result =
@@ -236,9 +242,11 @@ namespace PokemonGo.RocketBot.Logic
                             (DateTime.Now - requestSendDateTime).TotalMilliseconds;

                         sourceLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);
-                        var currentDistanceToTarget = LocationUtils.CalculateDistanceInMeters(sourceLocation, points.ToArray()[i]);
+                        var currentDistanceToTarget = LocationUtils.CalculateDistanceInMeters(sourceLocation,
+                            points.ToArray()[i]);

-                        var realDistanceToTargetSpeedDown = LocationUtils.CalculateDistanceInMeters(sourceLocation, targetLocation);
+                        var realDistanceToTargetSpeedDown = LocationUtils.CalculateDistanceInMeters(sourceLocation,
+                            targetLocation);
                         if (realDistanceToTargetSpeedDown < 40)
                             if (speedInMetersPerSecond > SpeedDownTo)
                                 speedInMetersPerSecond = SpeedDownTo;
@@ -247,9 +255,10 @@ namespace PokemonGo.RocketBot.Logic
                             speedInMetersPerSecond = MinorWalkingSpeedVariant(session);

                         nextWaypointDistance = Math.Min(currentDistanceToTarget,
-                            millisecondsUntilGetUpdatePlayerLocationResponse / 1000 * speedInMetersPerSecond);
+                            millisecondsUntilGetUpdatePlayerLocationResponse/1000*speedInMetersPerSecond);
                         nextWaypointBearing = LocationUtils.DegreeBearing(sourceLocation, points.ToArray()[i]);
-                        waypoint = LocationUtils.CreateWaypoint(sourceLocation, nextWaypointDistance, nextWaypointBearing);
+                        waypoint = LocationUtils.CreateWaypoint(sourceLocation, nextWaypointDistance,
+                            nextWaypointBearing);

                         requestSendDateTime = DateTime.Now;
                         result =
@@ -268,36 +277,56 @@ namespace PokemonGo.RocketBot.Logic

                 return result;
             }
-
-            var curLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);
-            var dist = LocationUtils.CalculateDistanceInMeters(curLocation, targetLocation);
-            if (dist >= 100)
+            else
             {
-                var nextWaypointDistance = dist * 70 / 100;
-                var nextWaypointBearing = LocationUtils.DegreeBearing(curLocation, targetLocation);
+                var sourceLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);

-                var waypoint = LocationUtils.CreateWaypoint(curLocation, nextWaypointDistance, nextWaypointBearing);
-                var sentTime = DateTime.Now;
+                var nextWaypointBearing = LocationUtils.DegreeBearing(sourceLocation, targetLocation);
+
+                var nextWaypointDistance = session.LogicSettings.UseWalkingSpeedVariant
+                    ? MajorWalkingSpeedVariant(session)
+                    : session.LogicSettings.WalkingSpeedInKilometerPerHour/3.6;
+                ;
+                ;
+                var waypoint = LocationUtils.CreateWaypoint(sourceLocation, nextWaypointDistance, nextWaypointBearing);
+
+                //Initial walking
+                var requestSendDateTime = DateTime.Now;
+                var result =
+                    await
+                        _client.Player.UpdatePlayerLocation(waypoint.Latitude, waypoint.Longitude,
+                            waypoint.Altitude);

-                PlayerUpdateResponse result;
                 UpdatePositionEvent?.Invoke(waypoint.Latitude, waypoint.Longitude);

                 do
                 {
+                    var speedInMetersPerSecond = session.LogicSettings.UseWalkingSpeedVariant
+                        ? MajorWalkingSpeedVariant(session)
+                        : session.LogicSettings.WalkingSpeedInKilometerPerHour/3.6;
                     cancellationToken.ThrowIfCancellationRequested();

-                    curLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);
-                    LocationUtils.CalculateDistanceInMeters(curLocation, targetLocation);
+                    var millisecondsUntilGetUpdatePlayerLocationResponse =
+                        (DateTime.Now - requestSendDateTime).TotalMilliseconds;

-                    dist = LocationUtils.CalculateDistanceInMeters(curLocation, targetLocation);
+                    sourceLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);
+                    var currentDistanceToTarget = LocationUtils.CalculateDistanceInMeters(sourceLocation, targetLocation);

-                    if (dist >= 100)
-                        nextWaypointDistance = dist * 70 / 100;
-                    else
-                        nextWaypointDistance = dist;
+                    if (currentDistanceToTarget < 40)
+                    {
+                        if (speedInMetersPerSecond > SpeedDownTo)
+                        {
+                            //Logger.Write("We are within 40 meters of the target. Speeding down to 10 km/h to not pass the target.", LogLevel.Info);
+                            speedInMetersPerSecond = SpeedDownTo;
+                        }
+                    }
+
+                    nextWaypointDistance = Math.Min(currentDistanceToTarget,
+                        millisecondsUntilGetUpdatePlayerLocationResponse/1000*speedInMetersPerSecond);
+                    nextWaypointBearing = LocationUtils.DegreeBearing(sourceLocation, targetLocation);
+                    waypoint = LocationUtils.CreateWaypoint(sourceLocation, nextWaypointDistance, nextWaypointBearing);

-                    nextWaypointBearing = LocationUtils.DegreeBearing(curLocation, targetLocation);
-                    waypoint = LocationUtils.CreateWaypoint(curLocation, nextWaypointDistance, nextWaypointBearing);
+                    requestSendDateTime = DateTime.Now;
                     result =
                         await
                             _client.Player.UpdatePlayerLocation(waypoint.Latitude, waypoint.Longitude,
@@ -308,18 +337,8 @@ namespace PokemonGo.RocketBot.Logic

                     if (functionExecutedWhileWalking != null)
                         await functionExecutedWhileWalking(); // look for pokemon
-                } while (LocationUtils.CalculateDistanceInMeters(curLocation, targetLocation) >= 10);
-                return result;
-            }
-            else
-            {
-                var result =
-                    await
-                        _client.Player.UpdatePlayerLocation(targetLocation.Latitude, targetLocation.Longitude,
-                            LocationUtils.getElevation(targetLocation.Latitude, targetLocation.Longitude));
-                UpdatePositionEvent?.Invoke(targetLocation.Latitude, targetLocation.Longitude);
-                if (functionExecutedWhileWalking != null)
-                    await functionExecutedWhileWalking(); // look for pokemon
+                } while (LocationUtils.CalculateDistanceInMeters(sourceLocation, targetLocation) >= 30);
+
                 return result;
             }
         }
@@ -335,8 +354,9 @@ namespace PokemonGo.RocketBot.Logic

             var targetLocation = new GeoCoordinate(Convert.ToDouble(trk.Lat, CultureInfo.InvariantCulture),
                 Convert.ToDouble(trk.Lon, CultureInfo.InvariantCulture));
-            var speedInMetersPerSecond = session.LogicSettings.UseWalkingSpeedVariant ? MajorWalkingSpeedVariant(session) :
-                    session.LogicSettings.WalkingSpeedInKilometerPerHour / 3.6;
+            var speedInMetersPerSecond = session.LogicSettings.UseWalkingSpeedVariant
+                ? MajorWalkingSpeedVariant(session)
+                : session.LogicSettings.WalkingSpeedInKilometerPerHour/3.6;
             var sourceLocation = new GeoCoordinate(_client.CurrentLatitude, _client.CurrentLongitude);
             LocationUtils.CalculateDistanceInMeters(sourceLocation, targetLocation);
             var nextWaypointBearing = LocationUtils.DegreeBearing(sourceLocation, targetLocation);
@@ -373,7 +393,7 @@ namespace PokemonGo.RocketBot.Logic
                     speedInMetersPerSecond = MinorWalkingSpeedVariant(session);

                 nextWaypointDistance = Math.Min(currentDistanceToTarget,
-                    millisecondsUntilGetUpdatePlayerLocationResponse / 1000 * speedInMetersPerSecond);
+                    millisecondsUntilGetUpdatePlayerLocationResponse/1000*speedInMetersPerSecond);
                 nextWaypointBearing = LocationUtils.DegreeBearing(sourceLocation, targetLocation);
                 waypoint = LocationUtils.CreateWaypoint(sourceLocation, nextWaypointDistance, nextWaypointBearing);

diff --git a/PokemonGo.RocketBot.Window/Forms/MainForm.cs b/PokemonGo.RocketBot.Window/Forms/MainForm.cs
index d57676a..e90bb4d 100644
--- a/PokemonGo.RocketBot.Window/Forms/MainForm.cs
+++ b/PokemonGo.RocketBot.Window/Forms/MainForm.cs
@@ -251,17 +251,19 @@ namespace PokemonGo.RocketBot.Window.Forms
                 _playerOverlay.Markers.Clear();
                 _playerOverlay.Routes.Clear();
                 _playerLocations.Clear();
-                /*var routePoint =
+                var routePoint =
                     (from pokeStop in pokeStops
                         where pokeStop != null
                         select new PointLatLng(pokeStop.Latitude, pokeStop.Longitude)).ToList();
+                foreach(var pks in pokeStops)
+                    Logger.Write(pks.Latitude + ", " + pks.Longitude);

                 // Temporary removed it since the route is calculated on the fly with gmap api's
                 var route = new GMapRoute(routePoint, "Walking Path")
                 {
                     Stroke = new Pen(Color.FromArgb(128, 0, 179, 253), 4)
                 };
-                _pokestopsOverlay.Routes.Add(route);*/
+                _pokestopsOverlay.Routes.Add(route);

                 foreach (var pokeStop in pokeStops)
                 {
You may download the files in Public Git.