Skip to content

Commit

Permalink
Polyline, Altitude bugfix and Polyline support for MoveToFort (#4350)
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
kanemasa1987 authored and solderzzc committed Aug 20, 2016
1 parent 72e111c commit fd760f6
Show file tree
Hide file tree
Showing 9 changed files with 52 additions and 60 deletions.
5 changes: 3 additions & 2 deletions configs/config.json.example
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,8 @@
"type": "MoveToFort",
"config": {
"lure_attraction": true,
"lure_max_distance": 2000
"lure_max_distance": 2000,
"walker": "StepWalker"
}
},
{
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions pokecli.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
9 changes: 4 additions & 5 deletions pokemongo_bot/cell_workers/follow_path.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])
Expand Down Expand Up @@ -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'
}
Expand Down
14 changes: 6 additions & 8 deletions pokemongo_bot/cell_workers/follow_spiral.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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,
Expand All @@ -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'
}
Expand All @@ -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'
}
Expand Down
5 changes: 3 additions & 2 deletions pokemongo_bot/cell_workers/move_to_fort.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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()
Expand Down Expand Up @@ -74,7 +75,7 @@ def work(self):
data=fort_event_data
)

step_walker = StepWalker(
step_walker = walker_factory(self.walker,
self.bot,
lat,
lng
Expand Down
3 changes: 1 addition & 2 deletions pokemongo_bot/cell_workers/move_to_map_pokemon.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,6 +382,5 @@ def _move_to(self, pokemon):
return walker_factory(self.walker,
self.bot,
pokemon['latitude'],
pokemon['longitude'],
**{'parent': MoveToMapPokemon}
pokemon['longitude']
)
57 changes: 25 additions & 32 deletions pokemongo_bot/walkers/polyline_generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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()
Expand Down Expand Up @@ -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
Expand Down
13 changes: 7 additions & 6 deletions pokemongo_bot/walkers/polyline_walker.py
Original file line number Diff line number Diff line change
Expand Up @@ -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],
Expand All @@ -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()
Expand Down
2 changes: 1 addition & 1 deletion pokemongo_bot/walkers/walker_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit fd760f6

Please sign in to comment.