From fd760f615c0d2d4afe9b5335cf6da17aac5c6c9f Mon Sep 17 00:00:00 2001 From: kanemasa1987 Date: Sat, 20 Aug 2016 23:41:22 +0900 Subject: [PATCH] Polyline, Altitude bugfix and Polyline support for MoveToFort (#4350) * bugfix for polyline * leftovers * changed default value of pokecli config aswell * added fix for #4250 * fixed spiral aswell * original current and last are wrong... * seems it was ok, reverted partly... * upside down * changed message * cleanup & polyline support for move_to_fort * fixed default conf to StepWalker --- configs/config.json.example | 5 +- pokecli.py | 4 +- pokemongo_bot/cell_workers/follow_path.py | 9 ++- pokemongo_bot/cell_workers/follow_spiral.py | 14 ++--- pokemongo_bot/cell_workers/move_to_fort.py | 5 +- .../cell_workers/move_to_map_pokemon.py | 3 +- pokemongo_bot/walkers/polyline_generator.py | 57 ++++++++----------- pokemongo_bot/walkers/polyline_walker.py | 13 +++-- pokemongo_bot/walkers/walker_factory.py | 2 +- 9 files changed, 52 insertions(+), 60 deletions(-) diff --git a/configs/config.json.example b/configs/config.json.example index 8c41649005..f67999a99e 100644 --- a/configs/config.json.example +++ b/configs/config.json.example @@ -152,7 +152,8 @@ "type": "MoveToFort", "config": { "lure_attraction": true, - "lure_max_distance": 2000 + "lure_max_distance": 2000, + "walker": "StepWalker" } }, { @@ -186,7 +187,7 @@ "gps_default_altitude": 8.0, "replicate_gps_xy_noise": false, "replicate_gps_z_noise": false, - "gps_xy_noise_range": 0.00025, + "gps_xy_noise_range": 0.000125, "gps_z_noise_range": 12.5, "debug": false, "test": false, diff --git a/pokecli.py b/pokecli.py index b3fea40727..05ac6b610f 100644 --- a/pokecli.py +++ b/pokecli.py @@ -529,9 +529,9 @@ def _json_loader(filename): parser, load, long_flag="--gps_xy_noise_range", - help="Intensity of gps noise (unit is lat and lng,) high values may cause issues (default=0.00025)", + help="Intensity of gps noise (unit is lat and lng,) high values may cause issues (default=0.000125)", type=float, - default=0.00025 + default=0.000125 ) add_config( parser, diff --git a/pokemongo_bot/cell_workers/follow_path.py b/pokemongo_bot/cell_workers/follow_path.py index e5dccdedb5..d295b72320 100644 --- a/pokemongo_bot/cell_workers/follow_path.py +++ b/pokemongo_bot/cell_workers/follow_path.py @@ -99,8 +99,7 @@ def find_closest_point_idx(self, points): return return_idx def work(self): - last_lat = self.bot.api._position_lat - last_lng = self.bot.api._position_lng + last_lat, last_lng, last_alt = self.bot.api.get_position() point = self.points[self.ptr] lat = float(point['lat']) @@ -138,10 +137,10 @@ def work(self): self.emit_event( 'position_update', - formatted="Walk to {last_position} now at {current_position}, distance left: ({distance} {distance_unit}) ..", + formatted="Walking from {last_position} to {current_position}, distance left: ({distance} {distance_unit}) ..", data={ - 'last_position': (lat, lng, 0), - 'current_position': (last_lat, last_lng, 0), + 'last_position': (last_lat, last_lng, last_alt), + 'current_position': (lat, lng, alt), 'distance': dist, 'distance_unit': 'm' } diff --git a/pokemongo_bot/cell_workers/follow_spiral.py b/pokemongo_bot/cell_workers/follow_spiral.py index 4acd16038f..16e6406810 100644 --- a/pokemongo_bot/cell_workers/follow_spiral.py +++ b/pokemongo_bot/cell_workers/follow_spiral.py @@ -67,8 +67,7 @@ def _generate_spiral(starting_lat, starting_lng, step_size, step_limit): return coords def work(self): - last_lat = self.bot.api._position_lat - last_lng = self.bot.api._position_lng + last_lat, last_lng, last_alt = self.bot.api.get_position() point = self.points[self.ptr] self.cnt += 1 @@ -80,6 +79,7 @@ def work(self): point['lng'] ) + alt = uniform(self.bot.config.alt_min, self.bot.config.alt_max) if self.bot.config.walk_max > 0: step_walker = StepWalker( self.bot, @@ -92,8 +92,8 @@ def work(self): 'position_update', formatted="Walking from {last_position} to {current_position} ({distance} {distance_unit})", data={ - 'last_position': (last_lat, last_lng, 0), - 'current_position': (point['lat'], point['lng'], 0), + 'last_position': (last_lat, last_lng, last_alt), + 'current_position': (point['lat'], point['lng'], alt), 'distance': dist, 'distance_unit': 'm' } @@ -102,15 +102,13 @@ def work(self): if step_walker.step(): step_walker = None else: - alt = uniform(self.bot.config.alt_min, self.bot.config.alt_max) self.bot.api.set_position(point['lat'], point['lng'], alt) - self.emit_event( 'position_update', formatted="Teleported from {last_position} to {current_position} ({distance} {distance_unit})", data={ - 'last_position': (last_lat, last_lng, 0), - 'current_position': (point['lat'], point['lng'], 0), + 'last_position': (last_lat, last_lng, last_alt), + 'current_position': (point['lat'], point['lng'], alt), 'distance': dist, 'distance_unit': 'm' } diff --git a/pokemongo_bot/cell_workers/move_to_fort.py b/pokemongo_bot/cell_workers/move_to_fort.py index f1f2589145..6b83d78ffe 100644 --- a/pokemongo_bot/cell_workers/move_to_fort.py +++ b/pokemongo_bot/cell_workers/move_to_fort.py @@ -3,7 +3,7 @@ from pokemongo_bot import inventory from pokemongo_bot.constants import Constants -from pokemongo_bot.walkers.step_walker import StepWalker +from pokemongo_bot.walkers.walker_factory import walker_factory from pokemongo_bot.worker_result import WorkerResult from pokemongo_bot.base_task import BaseTask from utils import distance, format_dist, fort_details @@ -17,6 +17,7 @@ def initialize(self): self.lure_attraction = self.config.get("lure_attraction", True) self.lure_max_distance = self.config.get("lure_max_distance", 2000) self.ignore_item_count = self.config.get("ignore_item_count", False) + self.walker = self.config.get('walker', 'StepWalker') def should_run(self): has_space_for_loot = inventory.Items.has_space_for_loot() @@ -74,7 +75,7 @@ def work(self): data=fort_event_data ) - step_walker = StepWalker( + step_walker = walker_factory(self.walker, self.bot, lat, lng diff --git a/pokemongo_bot/cell_workers/move_to_map_pokemon.py b/pokemongo_bot/cell_workers/move_to_map_pokemon.py index 26cd7603bd..19d61e5402 100644 --- a/pokemongo_bot/cell_workers/move_to_map_pokemon.py +++ b/pokemongo_bot/cell_workers/move_to_map_pokemon.py @@ -382,6 +382,5 @@ def _move_to(self, pokemon): return walker_factory(self.walker, self.bot, pokemon['latitude'], - pokemon['longitude'], - **{'parent': MoveToMapPokemon} + pokemon['longitude'] ) diff --git a/pokemongo_bot/walkers/polyline_generator.py b/pokemongo_bot/walkers/polyline_generator.py index 28dac8c6ac..1e6033ad28 100644 --- a/pokemongo_bot/walkers/polyline_generator.py +++ b/pokemongo_bot/walkers/polyline_generator.py @@ -11,50 +11,36 @@ class PolylineObjectHandler: Does this need to be a class? More like a namespace... ''' - _cache = {} - _kill_these = set() + _cache = None @staticmethod - def cached_polyline(bot, origin, destination, speed, parent_cls): + def cached_polyline(origin, destination, speed): ''' Google API has limits, so we can't generate new Polyline at every tick... ''' - for key in list(PolylineObjectHandler._kill_these): - PolylineObjectHandler._cache.pop(key) - PolylineObjectHandler._kill_these.remove(key) - - key = parent_cls - if key not in PolylineObjectHandler._cache: - polyline = Polyline(origin, destination, speed) - polyline.key = key - polyline.destination = destination - - PolylineObjectHandler._cache[key] = polyline - elif key in PolylineObjectHandler._cache and PolylineObjectHandler._cache[key].destination != destination: - # The case bot changes mind to catch Mew instead of following Doduo... - # Merge with upper code? Without comment, it would be quite puzzling... - polyline = Polyline(origin, destination, speed) - polyline.key = key - polyline.destination = destination - - PolylineObjectHandler._cache[key] = polyline + + # _cache might be None... + is_old_cache = lambda : tuple(origin) != PolylineObjectHandler._cache.get_last_pos() + new_dest_set = lambda : tuple(destination) != PolylineObjectHandler._cache.destination + + if None == PolylineObjectHandler._cache or is_old_cache() or new_dest_set(): + PolylineObjectHandler._cache = Polyline(origin, destination, speed) else: - polyline = PolylineObjectHandler._cache[key] - return polyline + # valid cache found + pass - @staticmethod - def delete_cache(polyline): - PolylineObjectHandler._cache.pop(polyline.key) + return PolylineObjectHandler._cache class Polyline(object): def __init__(self, origin, destination, speed): self.DISTANCE_API_URL='https://maps.googleapis.com/maps/api/directions/json?mode=walking' self.origin = origin - self.destination = destination + self.destination = tuple(destination) self.URL = '{}&origin={}&destination={}'.format(self.DISTANCE_API_URL, - '{},{}'.format(*self.origin), - '{},{}'.format(*self.destination)) + '{},{}'.format(*self.origin), + '{},{}'.format(*self.destination)) + self.request_responce = requests.get(self.URL).json() try: # Polyline walker starts teleporting after reaching api query limit. @@ -73,6 +59,7 @@ def __init__(self, origin, destination, speed): self.is_paused = False self._last_paused_timestamp = None self._paused_total = 0.0 + self._last_pos = (None, None) def reset_timestamps(self): self._timestamp = time.time() @@ -132,10 +119,16 @@ def get_pos(self): percentage_walked = (time_passed_distance - (walked_end_step - step_distance)) / step_distance else: percentage_walked = 1.0 - return self.calculate_coord(percentage_walked, *steps_dict[walked_end_step]) + result = self.calculate_coord(percentage_walked, *steps_dict[walked_end_step]) + self._last_pos = tuple(result[0]) + return self._last_pos else: # otherwise return the destination https://github.com/th3w4y/PokemonGo-Bot/issues/27 - return [self.points[-1]] + self._last_pos = tuple(self.points[-1]) + return self._last_pos + + def get_last_pos(self): + return self._last_pos def calculate_coord(self, percentage, o, d): # If this is the destination then returning as such diff --git a/pokemongo_bot/walkers/polyline_walker.py b/pokemongo_bot/walkers/polyline_walker.py index 00c896e569..21b9b2c89a 100644 --- a/pokemongo_bot/walkers/polyline_walker.py +++ b/pokemongo_bot/walkers/polyline_walker.py @@ -9,13 +9,14 @@ class PolylineWalker(StepWalker): ''' - Heavy multi-botting can cause issue, since the directions API has limits. + Heavy multi-botting can cause issues, since the directions API has limits. + StepWalker is generated by the factory in the case. ''' - def __init__(self, bot, dest_lat, dest_lng, parent): + def __init__(self, bot, dest_lat, dest_lng): super(PolylineWalker, self).__init__(bot, dest_lat, dest_lng) - self.polyline_walker = PolylineObjectHandler.cached_polyline(bot, (self.api._position_lat, self.api._position_lng), - (self.destLat, self.destLng), self.speed, parent) + self.polyline_walker = PolylineObjectHandler.cached_polyline(tuple(self.api.get_position()[:2]), + (self.destLat, self.destLng), self.speed) self.dist = distance( self.bot.position[0], @@ -28,13 +29,13 @@ def step(self): cLat, cLng = self.api._position_lat, self.api._position_lng if self.dist < 10: # 10m, add config? set it at constants? - PolylineObjectHandler.delete_cache(self.polyline_walker) return True self.polyline_walker.unpause() sleep(1) self.polyline_walker.pause() - cLat, cLng = self.polyline_walker.get_pos()[0] + + cLat, cLng = self.polyline_walker.get_pos() alt = uniform(self.bot.config.alt_min, self.bot.config.alt_max) self.api.set_position(cLat, cLng, alt) self.bot.heartbeat() diff --git a/pokemongo_bot/walkers/walker_factory.py b/pokemongo_bot/walkers/walker_factory.py index cda32ed36f..652c8f04c2 100644 --- a/pokemongo_bot/walkers/walker_factory.py +++ b/pokemongo_bot/walkers/walker_factory.py @@ -9,7 +9,7 @@ def walker_factory(name, bot, dest_lat, dest_lng, *args, **kwargs): ret = StepWalker(bot, dest_lat, dest_lng) elif 'PolylineWalker' == name: try: - ret = PolylineWalker(bot, dest_lat, dest_lng, *args, **kwargs) + ret = PolylineWalker(bot, dest_lat, dest_lng) except: ret = StepWalker(bot, dest_lat, dest_lng) return ret