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 29, 2016
1 parent dfa4f78 commit 2102682
Show file tree
Hide file tree
Showing 5 changed files with 142 additions and 3 deletions.
42 changes: 42 additions & 0 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,6 +216,47 @@ def init_config():
type=int,
default=50
)

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

add_config(
parser,
load,
short_flag="-mtf",
long_flag="--navigator.move_to_forts",
help="Set whether the navigator should move towards nearby forts (DEFAULT True)",
type=bool,
default=True
)

add_config(
parser,
load,
short_flag="-pm",
long_flag="--navigator.path_mode",
help="Set the mode for the path navigator (DEFAULT loop)",
type=str,
default="loop"
)

add_config(
parser,
load,
short_flag="-pf",
long_flag="--navigator.path_file",
help="Set the file containing the path for the path navigator",
type=str,
default=None
)

add_config(
parser,
load,
Expand Down Expand Up @@ -335,6 +376,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
14 changes: 11 additions & 3 deletions pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from pokemongo_bot.event_handlers import SocketIoHandler
from pokemongo_bot.socketio_server.runner import SocketIoRunner
from spiral_navigator import SpiralNavigator
from path_navigator import PathNavigator
from worker_result import WorkerResult
from event_manager import EventManager
from api_wrapper import ApiWrapper
Expand All @@ -36,8 +37,7 @@ class PokemonGoBot(object):
cell_workers.EvolveAllWorker,
cell_workers.RecycleItemsWorker,
cell_workers.CatchVisiblePokemonWorker,
cell_workers.SeenFortWorker,
cell_workers.MoveToFortWorker
cell_workers.SeenFortWorker
]

@property
Expand All @@ -58,10 +58,18 @@ def __init__(self, config):
# Make our own copy of the workers for this instance
self.workers = list(self.WORKERS)

if self.config.navigator_move_to_forts:
self.workers.append(cell_workers.MoveToFortWorker)

def start(self):
self._setup_logging()
self._setup_api()
self.navigator = SpiralNavigator(self)

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

random.seed()

def _setup_event_system(self):
Expand Down
4 changes: 4 additions & 0 deletions pokemongo_bot/cell_workers/catch_visible_pokemon_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,10 @@ def work(self):

def get_lured_pokemon(self):
forts = self.bot.get_forts(order_by_distance=True)

if len(forts) == 0:
return False

fort = forts[0]

self.api.fort_details(fort_id=fort['id'],
Expand Down
84 changes: 84 additions & 0 deletions pokemongo_bot/path_navigator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
# -*- coding: utf-8 -*-
import logger
import gpxpy
import gpxpy.gpx
import json
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.points = self.load_path()


def load_path(self):
if self.config.navigator_path_file == None:
raise RuntimeError('You need to specify a path file (json or gpx)')

if self.config.navigator_path_file.endswith('.json'):
return self.load_json(self.config.navigator_path_file)
elif self.config.navigator_path_file.endswith('.gpx'):
return self.load_gpx(self.config.navigator_path_file)

def load_json(self, file):
with open(file) as data_file:
return json.load(data_file)

def load_gpx(self, file):
gpx_file = open(file, 'r')
gpx = gpxpy.parse(gpx_file)

if len(gpx.tracks) == 0:
raise RuntimeError('GPX file does not cotain a track')

points = []
track = gpx.tracks[0]
for segment in track.segments:
for point in segment.points:
points.append({"lat": point.latitude, "lng": point.longitude})

return points;

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

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(lat, 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.config.navigator_path_mode == 'linear':
self.points = list(reversed(self.points))
else:
self.ptr += 1

return [lat, lng]
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@ flask==0.11.1
socketIO_client==0.7.0
eventlet==0.19.0
universal-analytics-python==0.2.4
gpxpy==1.1.1

0 comments on commit 2102682

Please sign in to comment.