From 7d696acb486163e4c3f9a160ff5fc1313016aa82 Mon Sep 17 00:00:00 2001 From: Eli White Date: Mon, 25 Jul 2016 01:44:34 -0700 Subject: [PATCH] Fixing teleporting to poke stops --- pokemongo_bot/__init__.py | 14 ++--- .../cell_workers/move_to_fort_worker.py | 17 +++++- pokemongo_bot/spiral_navigator.py | 15 ++++- pokemongo_bot/step_walker.py | 59 ++++++++++++++++--- 4 files changed, 85 insertions(+), 20 deletions(-) diff --git a/pokemongo_bot/__init__.py b/pokemongo_bot/__init__.py index 1b1e53ba9a..92c54b36b1 100644 --- a/pokemongo_bot/__init__.py +++ b/pokemongo_bot/__init__.py @@ -14,7 +14,6 @@ from pgoapi.utilities import f2i, h2f from cell_workers import PokemonCatchWorker, SeenFortWorker, MoveToFortWorker, InitialTransferWorker, EvolveAllWorker from cell_workers.utils import distance, get_cellid, encode -from step_walker import StepWalker from human_behaviour import sleep from spiral_navigator import SpiralNavigator from geopy.geocoders import GoogleV3 @@ -31,7 +30,6 @@ def __init__(self, config): def start(self): self._setup_logging() self._setup_api() - self.step_walker = StepWalker(self) self.navigator = SpiralNavigator(self) random.seed() @@ -73,7 +71,7 @@ def work_on_cell(self, cell, position): if self.config.evolve_all: # Will skip evolving if user wants to use an egg and there is none skip_evolves = False - + # Pop lucky egg before evolving to maximize xp gain use_lucky_egg = self.config.use_lucky_egg lucky_egg_count = self.item_inventory_count(Item.ITEM_LUCKY_EGG.value) @@ -102,7 +100,7 @@ def work_on_cell(self, cell, position): print('[#] Attempting to evolve all pokemons ...') worker = EvolveAllWorker(self) worker.work() - + # Flip the bit. self.config.evolve_all = [] @@ -267,7 +265,7 @@ def drop_item(self, item_id, count): # Example of good request response #{'responses': {'RECYCLE_INVENTORY_ITEM': {'result': 1, 'new_count': 46}}, 'status_code': 1, 'auth_ticket': {'expire_timestamp_ms': 1469306228058L, 'start': '/HycFyfrT4t2yB2Ij+yoi+on778aymMgxY6RQgvrGAfQlNzRuIjpcnDd5dAxmfoTqDQrbz1m2dGqAIhJ+eFapg==', 'end': 'f5NOZ95a843tgzprJo4W7Q=='}, 'request_id': 8145806132888207460L} return inventory_req - + def use_lucky_egg(self): self.api.use_item_xp_boost(item_id=301) inventory_req = self.api.call() @@ -352,9 +350,9 @@ def item_inventory_count(self, id): return item_count def _set_starting_position(self): - + has_position = False - + if self.config.test: # TODO: Add unit tests return @@ -398,7 +396,7 @@ def _set_starting_position(self): '[x] Last in-game location was set as: {}'.format( self.position)) logger.log('') - + has_position = True return except: diff --git a/pokemongo_bot/cell_workers/move_to_fort_worker.py b/pokemongo_bot/cell_workers/move_to_fort_worker.py index ffbdd934ac..19a586af2c 100644 --- a/pokemongo_bot/cell_workers/move_to_fort_worker.py +++ b/pokemongo_bot/cell_workers/move_to_fort_worker.py @@ -1,14 +1,15 @@ from utils import distance, format_dist from pokemongo_bot.human_behaviour import sleep from pokemongo_bot import logger +from pokemongo_bot.step_walker import StepWalker class MoveToFortWorker(object): def __init__(self, fort, bot): + self.bot = bot self.fort = fort self.api = bot.api self.config = bot.config self.navigator = bot.navigator - self.step_walker = bot.step_walker self.position = bot.position def work(self): @@ -28,7 +29,19 @@ def work(self): position = (lat, lng, 0.0) if self.config.walk > 0: - self.step_walker.step(self.config.walk, *position[0:2]) + step_walker = StepWalker( + self.bot, + self.config.walk, + self.api._position_lat, + self.api._position_lng, + position[0], + position[1] + ) + + while True: + if step_walker.step(self.config.walk, *position[0:2]): + break + else: self.api.set_position(*position) diff --git a/pokemongo_bot/spiral_navigator.py b/pokemongo_bot/spiral_navigator.py index 2ef34d2f23..6fb174b0e9 100644 --- a/pokemongo_bot/spiral_navigator.py +++ b/pokemongo_bot/spiral_navigator.py @@ -11,6 +11,7 @@ from human_behaviour import sleep, random_lat_long_delta from cell_workers.utils import distance, i2f, format_time, format_dist +from step_walker import StepWalker from pgoapi.utilities import f2i, h2f import logger @@ -31,6 +32,7 @@ def __init__(self, bot): self.steplimit2 = self.steplimit**2 self.origin_lat = self.bot.position[0] self.origin_lon = self.bot.position[1] + self._step_walker = None def take_step(self): position = (self.origin_lat, self.origin_lon, 0.0) @@ -49,6 +51,15 @@ def take_step(self): position = (self.x * 0.0025 + self.origin_lat, self.y * 0.0025 + self.origin_lon, 0) if self.config.walk > 0: + if not self._step_walker: + self._step_walker = StepWalker( + self.bot, + self.config.walk, + self.api._position_lat, + self.api._position_lng, + position[0], + position[1] + ) dist = distance( i2f(self.api._position_lat), @@ -59,7 +70,9 @@ def take_step(self): logger.log('[#] Walking from ' + str((i2f(self.api._position_lat), i2f( self.api._position_lng))) + " to " + str((str(position[0:2]))) + " " + format_dist(dist, self.config.distance_unit)) - self.bot.step_walker.step(self.config.walk, *position[0:2]) + + if self._step_walker.step(): + self._step_walker = None else: self.api.set_position(*position) if self.x == self.y or self.x < 0 and self.x == -self.y or self.x > 0 and self.x == 1 - self.y: diff --git a/pokemongo_bot/step_walker.py b/pokemongo_bot/step_walker.py index a61e0f914a..e0fb64bf00 100644 --- a/pokemongo_bot/step_walker.py +++ b/pokemongo_bot/step_walker.py @@ -1,25 +1,63 @@ import logger -from cell_workers.utils import distance, i2f, format_time +from cell_workers.utils import distance, i2f, format_time, format_dist from human_behaviour import random_lat_long_delta, sleep -from math import ceil +from math import ceil, sqrt class StepWalker(object): - def __init__(self, bot): + def __init__(self, bot, speed, initLat, initLng, destLat, destLng): self.bot = bot self.api = bot.api - def step(self, speed, lat, lng): - if self.api._position_lat == lat and self.api._position_lng == lng: + dist = distance( + i2f(initLat), + i2f(initLng), + destLat, + destLng + ) + + self.speed = speed + + self.destLat = destLat + self.destLng = destLng + + self.steps = (dist + 0.0) / (speed + 0.0) + + if dist < 0.01 or self.steps < 1: + self.dLat = 0 + self.dLng = 0 + self.magnitude = 0; + else: + self.dLat = (destLat - i2f(initLat)) / self.steps + self.dLng = (destLng - i2f(initLng)) / self.steps + self.magnitude = self._pythagorean(self.dLat, self.dLng) + + def step(self): + dist = distance( + i2f(self.api._position_lat), + i2f(self.api._position_lng), + self.destLat, + self.destLng + ) + # print 'distance' + # print dist + + if (self.dLat == 0 and self.dLng == 0) or dist < 15: return True - dLat = (lat - i2f(self.api._position_lat)) / speed - dLng = (lng - i2f(self.api._position_lng)) / speed + totalDLat = (self.destLat - i2f(self.api._position_lat)) + totalDLng = (self.destLng - i2f(self.api._position_lng)) + magnitude = self._pythagorean(totalDLat, totalDLng) + unitLat = totalDLat / magnitude + unitLng = totalDLng / magnitude - cLat = i2f(self.api._position_lat) + dLat + random_lat_long_delta() - cLng = i2f(self.api._position_lng) + dLng + random_lat_long_delta() + scaledDLat = unitLat * self.magnitude + scaledDLng = unitLng * self.magnitude + + cLat = i2f(self.api._position_lat) + scaledDLat + random_lat_long_delta() + cLng = i2f(self.api._position_lng) + scaledDLng + random_lat_long_delta() self.api.set_position(cLat, cLng, 0) self.bot.heartbeat() @@ -27,3 +65,6 @@ def step(self, speed, lat, lng): # self._work_at_position( # i2f(self.api._position_lat), i2f(self.api._position_lng), # alt, False) + + def _pythagorean(self, lat, lng): + return sqrt((lat ** 2) + (lng ** 2))