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 30, 2016
1 parent 2f277fd commit e76cf01
Show file tree
Hide file tree
Showing 9 changed files with 225 additions and 4 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,8 @@ configs/*
!configs/release_config.json.example
!configs/config.json.pokemons.example
!configs/config.json.pokemon.example
!configs/config.json.path.example
!configs/path.example.json

# Virtualenv folders
bin/
Expand Down
66 changes: 66 additions & 0 deletions configs/config.json.path.example
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
{
"auth_service": "google",
"username": "YOUR_USERNAME",
"password": "YOUR_PASSWORD",
"location": "SOME_LOCATION",
"gmapkey": "GOOGLE_MAPS_API_KEY",
"max_steps": 5,
"catch_pokemon": true,
"forts": {
"spin": true,
"avoid_circles": true,
"max_circle_size": 50
},
"navigator": {
"type": "path",
"move_to_forts": false,
"path_mode": "loop",
"path_file": "configs/path.example.json"
},
"websocket_server": false,
"walk": 4.16,
"action_wait_min": 1,
"action_wait_max": 4,
"debug": false,
"test": false,
"health_record": true,
"location_cache": true,
"distance_unit": "km",
"reconnecting_timeout": 15,
"item_filter": {
"1": { "keep" : 100 },
"101": { "keep" : 10 },
"102": { "keep" : 30 },
"103": { "keep" : 30 },
"201": { "keep" : 30 },
"701": { "keep" : 100 }
},
"evolve_all": "NONE",
"evolve_speed": 20,
"evolve_cp_min": 300,
"use_lucky_egg": false,
"hatch_eggs": true,
"longer_eggs_first": true,
"evolve_captured": "NONE",
"release_pokemon": true,
"catch_randomize_reticle_factor": 1.0,
"catch_randomize_spin_factor": 1.0,
"catch": {
"any": {"catch_above_cp": 0, "catch_above_iv": 0, "logic": "or"},
"// Example of always catching Rattata:": {},
"// Rattata": { "always_catch" : true }
},
"release": {
"any": {"release_below_cp": 0, "release_below_iv": 0, "logic": "or"},
"// Example of always releasing Rattata:": {},
"// Rattata": {"always_release": true},
"// Example of keeping 3 stronger (based on CP) Pidgey:": {},
"// Pidgey": {"keep_best_cp": 3},
"// Example of keeping 2 stronger (based on IV) Zubat:": {},
"// Zubat": {"keep_best_iv": 2},
"// Also, it is working with any": {},
"// any": {"keep_best_iv": 3},
"// Example of keeping the 2 strongest (based on CP) and 3 best (based on IV) Zubat:": {},
"// Zubat": {"keep_best_cp": 2, "keep_best_iv": 3}
}
}
9 changes: 9 additions & 0 deletions configs/path.example.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{"lng":8.043531775474547,"lat":52.27777834853098},
{"lng":8.045151829719543,"lat":52.27796214757581},
{"lng":8.045924305915833,"lat":52.27753218790187},
{"lng":8.045715093612671,"lat":52.27696437216979},
{"lng":8.044642210006714,"lat":52.27692826803333},
{"lng":8.043639063835144,"lat":52.27722694682294},
{"lng":8.043547868728638,"lat":52.27761095945195}
]
41 changes: 41 additions & 0 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,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 (GPX or JSON).",
type=str,
default=None
)

add_config(
parser,
load,
Expand Down
17 changes: 16 additions & 1 deletion pokemongo_bot/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

import cell_workers
import logger
import navigators
from api_wrapper import ApiWrapper
from cell_workers.utils import distance
from event_manager import EventManager
Expand All @@ -23,7 +24,6 @@
from metrics import Metrics
from pokemongo_bot.event_handlers import LoggingHandler, SocketIoHandler
from pokemongo_bot.socketio_server.runner import SocketIoRunner
from spiral_navigator import SpiralNavigator
from worker_result import WorkerResult


Expand Down Expand Up @@ -51,8 +51,17 @@ def __init__(self, config):
def start(self):
self._setup_logging()
self._setup_api()
<<<<<<< 2f277fd7d08ff9a7ad45cf46e102a00db7ba9fdf
self._setup_workers()
self.navigator = SpiralNavigator(self)
=======

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

>>>>>>> [FEATURE] Path Navigator
random.seed()

def _setup_event_system(self):
Expand Down Expand Up @@ -81,7 +90,13 @@ def tick(self):
self.check_session(self.position[0:2])

for worker in self.workers:
<<<<<<< 2f277fd7d08ff9a7ad45cf46e102a00db7ba9fdf
if worker.work() == WorkerResult.RUNNING:
=======
if not self.config.navigator_move_to_forts and isinstance(worker(self), cell_workers.MoveToFortWorker):
continue
if worker(self).work() == WorkerResult.RUNNING:
>>>>>>> [FEATURE] Path Navigator
return

self.navigator.take_step()
Expand Down
2 changes: 2 additions & 0 deletions pokemongo_bot/navigators/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
from spiral_navigator import SpiralNavigator
from path_navigator import PathNavigator
85 changes: 85 additions & 0 deletions pokemongo_bot/navigators/path_navigator.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
# -*- coding: utf-8 -*-

import gpxpy
import gpxpy.gpx
import json
import pokemongo_bot.logger as logger
from pokemongo_bot.cell_workers.utils import distance, i2f, format_dist
from pokemongo_bot.human_behaviour import sleep
from pokemongo_bot.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]
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# -*- coding: utf-8 -*-
import logger
from cell_workers.utils import distance, format_dist
from step_walker import StepWalker
import pokemongo_bot.logger as logger
from pokemongo_bot.cell_workers.utils import distance, format_dist
from pokemongo_bot.step_walker import StepWalker


class SpiralNavigator(object):
Expand Down
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 e76cf01

Please sign in to comment.