Skip to content

Commit

Permalink
[FEATURE] Path Navigator
Browse files Browse the repository at this point in the history
Adds a navigator that walks along specified points.
  • Loading branch information
binarydepartment committed Jul 28, 2016
1 parent 0a9444b commit f45b8f2
Show file tree
Hide file tree
Showing 4 changed files with 98 additions and 2 deletions.
12 changes: 12 additions & 0 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,17 @@ def init_config():
type=int,
default=50
)

add_config(
parser,
load,
short_flag="-n",
long_flag="--navigator",
help="Set the navigator to be used(DEFAULT spiral)",
type=str,
default='spiral'
)

add_config(
parser,
load,
Expand Down Expand Up @@ -309,6 +320,7 @@ def init_config():
config.catch = load.get('catch', {})
config.release = load.get('release', {})
config.item_filter = load.get('item_filter', {})
config.path_navigator = load.get('path_navigator', {})
config.action_wait_max = load.get('action_wait_max', 4)
config.action_wait_min = load.get('action_wait_min', 1)

Expand Down
8 changes: 7 additions & 1 deletion pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
from item_list import Item
from metrics import Metrics
from spiral_navigator import SpiralNavigator
from path_navigator import PathNavigator
from worker_result import WorkerResult


Expand All @@ -41,7 +42,12 @@ def __init__(self, config):
def start(self):
self._setup_logging()
self._setup_api()
self.navigator = SpiralNavigator(self)

if self.config.navigator == 'spiral':
self.navigator = SpiralNavigator(self)
elif self.config.navigator == 'path':
self.navigator = PathNavigator(self)

random.seed()

def tick(self):
Expand Down
8 changes: 7 additions & 1 deletion pokemongo_bot/cell_workers/spin_nearest_fort_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,15 @@ def work(self):
nearest_fort = self.get_nearest_fort()

if nearest_fort:
dist = distance(self.position[0], self.position[1], nearest_fort['latitude'], nearest_fort['longitude'])

# Move to and spin the nearest stop.
if MoveToFortWorker(nearest_fort, self.bot).work() == WorkerResult.RUNNING:
if self.config.navigator != 'path' and MoveToFortWorker(nearest_fort, self.bot).work() == WorkerResult.RUNNING:
return WorkerResult.RUNNING

if (dist > 10):
return WorkerResult.SUCCESS

if SeenFortWorker(nearest_fort, self.bot).work() == WorkerResult.RUNNING:
return WorkerResult.RUNNING

Expand Down
72 changes: 72 additions & 0 deletions pokemongo_bot/path_navigator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# -*- coding: utf-8 -*-
import logger
from cell_workers.utils import distance, i2f, format_dist
from human_behaviour import sleep
from step_walker import StepWalker
from pgoapi.utilities import f2i


class PathNavigator(object):
def __init__(self, bot):
self.bot = bot
self.api = bot.api
self.config = bot.config
self.ptr = 0
self.mode = self.get_mode()

if not self.config.path_navigator:
raise RuntimeError('path_navigator config missing')

self.points = self.get_points()

def get_points(self):
if not self.config.path_navigator.has_key('points') or len(self.config.path_navigator['points']) < 2:
raise RuntimeError('You need to specify at least two points in the path_navigator config')

points = [point.split(',') for point in self.config.path_navigator['points']]
return points

def get_mode(self):
mode = 'linear'
if self.config.path_navigator.has_key('mode'):
if self.config.path_navigator['mode'] == 'loop':
mode = 'loop'

return mode

def take_step(self):
point = self.points[self.ptr]
lat = float(point[0])
lng = float(point[1])

if self.config.walk > 0:
step_walker = StepWalker(
self.bot,
self.config.walk,
lat,
lng
)

is_at_destination = False
if step_walker.step():
is_at_destination = True

else:
self.api.set_position(point['lat'], point['lng'])

dist = distance(
self.api._position_lat,
self.api._position_lng,
lat,
lng
)

if dist <= 1 or (self.config.walk > 0 and is_at_destination):
if (self.ptr + 1) == len(self.points):
self.ptr = 0
if self.mode == 'linear':
self.points = list(reversed(self.points))
else:
self.ptr += 1

return [lat, lng]

0 comments on commit f45b8f2

Please sign in to comment.