@@ -32,11 +32,7 @@ class Walk(val sortedPokestops: List<Pokestop>, val lootTimeouts: Map<String, Lo
3232 ctx.server.coordinatesToGoTo.removeAt(0 )
3333 Log .normal(" Walking to ${coordinates.latRadians()} , ${coordinates.lngRadians()} " )
3434
35- if (settings.followStreets) {
36- walkRoute(bot, ctx, settings, S2LatLng .fromDegrees(coordinates.latRadians(), coordinates.lngRadians()), settings.speed, true )
37- } else {
38- walk(bot, ctx, settings, S2LatLng .fromDegrees(coordinates.latRadians(), coordinates.lngRadians()), settings.speed, true )
39- }
35+ walk(bot, ctx, settings, S2LatLng .fromDegrees(coordinates.latRadians(), coordinates.lngRadians()), settings.speed, true )
4036 } else {
4137 val nearestUnused: List <Pokestop > = sortedPokestops.filter {
4238 val canLoot = it.canLoot(ignoreDistance = true , lootTimeouts = lootTimeouts, api = ctx.api)
@@ -57,39 +53,78 @@ class Walk(val sortedPokestops: List<Pokestop>, val lootTimeouts: Map<String, Lo
5753 if (settings.displayPokestopName)
5854 Log .normal(" Walking to pokestop \" ${chosenPokestop.details.name} \" " )
5955
60- if (settings.followStreets) {
61- walkRoute(bot, ctx, settings, S2LatLng .fromDegrees(chosenPokestop.latitude, chosenPokestop.longitude), settings.speed, false )
62- } else {
63- walk(bot, ctx, settings, S2LatLng .fromDegrees(chosenPokestop.latitude, chosenPokestop.longitude), settings.speed, false )
64- }
56+ walk(bot, ctx, settings, S2LatLng .fromDegrees(chosenPokestop.latitude, chosenPokestop.longitude), settings.speed, false )
6557 }
6658 }
6759 }
6860
69- fun walk (bot : Bot , ctx : Context , settings : Settings , end : S2LatLng , speed : Double , sendDone : Boolean ) {
70- val start = S2LatLng .fromDegrees(ctx.lat.get(), ctx.lng.get())
71- val diff = end.sub(start)
72- val distance = start.getEarthDistance(end)
73- val timeout = 200L
74- // prevent division by 0
61+ private fun walk (bot : Bot , ctx : Context , settings : Settings , end : S2LatLng , speed : Double , sendDone : Boolean ) {
62+ if (settings.followStreets) {
63+ walkRoute(bot, ctx, settings, end, speed, sendDone)
64+ } else {
65+ walkDirectly(bot, ctx, settings, end, speed, sendDone)
66+ }
67+ }
68+
69+ private fun walkDirectly (bot : Bot , ctx : Context , settings : Settings , end : S2LatLng , speed : Double , sendDone : Boolean ) {
70+ walkPath(bot, ctx, settings, mutableListOf (end), speed, sendDone)
71+ }
72+
73+ private fun walkRoute (bot : Bot , ctx : Context , settings : Settings , end : S2LatLng , speed : Double , sendDone : Boolean ) {
74+ val coordinatesList = getRouteCoordinates(S2LatLng .fromDegrees(ctx.lat.get(), ctx.lng.get()), end)
75+ if (coordinatesList.size >= 0 ) {
76+ walkPath(bot, ctx, settings, coordinatesList, speed, sendDone)
77+ } else {
78+ walkDirectly(bot, ctx, settings, end, speed, sendDone)
79+ }
80+ }
81+
82+ // all walk functions should call this one
83+ private fun walkPath (bot : Bot , ctx : Context , settings : Settings , path : MutableList <S2LatLng >, speed : Double , sendDone : Boolean ) {
7584 if (speed.equals(0 )) {
7685 return
7786 }
78- val timeRequired = distance / speed
79- val stepsRequired = timeRequired / (timeout.toDouble() / 1000 .toDouble())
80- // prevent division by 0
81- if (stepsRequired.equals(0 )) {
87+ if (path.isEmpty()) {
8288 return
8389 }
84- val deltaLat = diff.latDegrees() / stepsRequired
85- val deltaLng = diff.lngDegrees() / stepsRequired
8690
87- Log .normal(" Walking to ${end.toStringDegrees()} in $stepsRequired steps." )
88- var remainingSteps = stepsRequired
91+ val timeout = 200L
92+
93+ var remainingSteps = 0.0
94+ var deltaLat = 0.0
95+ var deltaLng = 0.0
8996
9097 val pauseWalk: AtomicBoolean = AtomicBoolean (false )
9198 var pauseCounter = 2
99+
92100 bot.runLoop(timeout, " WalkingLoop" ) { cancel ->
101+
102+ if (remainingSteps <= 0 ) {
103+ if (path.isEmpty()) {
104+ Log .normal(" Destination reached." )
105+ if (sendDone) {
106+ ctx.server.sendGotoDone()
107+ }
108+ ctx.walking.set(false )
109+ cancel()
110+ } else {
111+ // calculate delta lat/long for next step
112+ val start = S2LatLng .fromDegrees(ctx.lat.get(), ctx.lng.get())
113+ val nextPoint = path.first()
114+ path.removeAt(0 )
115+ val diff = nextPoint.sub(start)
116+ val distance = start.getEarthDistance(nextPoint)
117+ val timeRequired = distance / speed
118+ val stepsRequired = timeRequired / (timeout.toDouble() / 1000 .toDouble())
119+
120+ deltaLat = diff.latDegrees() / stepsRequired
121+ deltaLng = diff.lngDegrees() / stepsRequired
122+
123+ Log .normal(" Walking to ${nextPoint.toStringDegrees()} in $stepsRequired steps." )
124+ remainingSteps = stepsRequired
125+ }
126+ }
127+
93128 if (pauseWalk.get()) {
94129 Thread .sleep(timeout * 2 )
95130 pauseCounter--
@@ -121,150 +156,13 @@ class Walk(val sortedPokestops: List<Pokestop>, val lootTimeouts: Map<String, Lo
121156 ctx.server.setLocation(lat, lng)
122157
123158 remainingSteps--
124- if (remainingSteps <= 0 ) {
125- Log .normal(" Destination reached." )
126-
127- if (sendDone) {
128- ctx.server.sendGotoDone()
129- }
130-
131- ctx.walking.set(false )
132- cancel()
133- }
134159 }
135-
136160 }
137161
138- fun walkRoute (bot : Bot , ctx : Context , settings : Settings , end : S2LatLng , speed : Double , sendDone : Boolean ) {
139- if (speed.equals(0 )) {
140- return
141- }
142- val timeout = 200L
143- val coordinatesList = getRouteCoordinates(S2LatLng .fromDegrees(ctx.lat.get(), ctx.lng.get()), end)
144- if (coordinatesList.size <= 0 ) {
145- walk(bot, ctx, settings, end, speed, sendDone)
146- } else {
147- val pauseWalk: AtomicBoolean = AtomicBoolean (false )
148- var pauseCounter = 2
149- bot.runLoop(timeout, " WalkingLoop" ) { cancel ->
150- if (pauseWalk.get()) {
151- Thread .sleep(timeout * 2 )
152- pauseCounter--
153- if (! (ctx.api.inventories.itemBag.hasPokeballs() && bot.api.map.getCatchablePokemon(ctx.blacklistedEncounters).size > 0 && settings.catchPokemon)) {
154- // api break free
155- pauseWalk.set(false )
156- pauseCounter = 0
157- }
158- // fixed tries break free
159- if (pauseCounter > 0 ) {
160- return @runLoop
161- } else {
162- pauseWalk.set(false )
163- }
164- }
165- // don't run away when there are still Pokemon around
166- if (pauseCounter > 0 && ctx.api.inventories.itemBag.hasPokeballs() && bot.api.map.getCatchablePokemon(ctx.blacklistedEncounters).size > 0 && settings.catchPokemon) {
167- // Stop walking
168- Log .normal(" Pausing to catch pokemon..." )
169- pauseCounter = 2
170- pauseWalk.set(true )
171- return @runLoop
172- }
173-
174- val start = S2LatLng .fromDegrees(ctx.lat.get(), ctx.lng.get())
175- val step = coordinatesList.first()
176- coordinatesList.removeAt(0 )
177- val diff = step.sub(start)
178- val distance = start.getEarthDistance(step)
179- val timeRequired = distance / speed
180- val stepsRequired = timeRequired / (timeout.toDouble() / 1000 .toDouble())
181- if (stepsRequired.equals(0 )) {
182- cancel()
183- }
184- val deltaLat = diff.latDegrees() / stepsRequired
185- val deltaLng = diff.lngDegrees() / stepsRequired
186- var remainingSteps = stepsRequired
187- while (remainingSteps > 0 ) {
188- ctx.lat.addAndGet(deltaLat)
189- ctx.lng.addAndGet(deltaLng)
190- ctx.server.setLocation(ctx.lat.get(), ctx.lng.get())
191- remainingSteps--
192- Thread .sleep(timeout)
193- }
194-
195- if (coordinatesList.size <= 0 ) {
196- Log .normal(" Destination reached." )
197- if (sendDone) {
198- ctx.server.sendGotoDone()
199- }
200- ctx.walking.set(false )
201- cancel()
202-
203- }
204- }
205- }
206- }
207-
208- fun walkAndComeBack (bot : Bot , ctx : Context , settings : Settings , end : S2LatLng , speed : Double , sendDone : Boolean ) {
162+ // TODO not used anymore? remove?
163+ private fun walkAndComeBack (bot : Bot , ctx : Context , settings : Settings , end : S2LatLng , speed : Double , sendDone : Boolean ) {
209164 val start = S2LatLng .fromDegrees(ctx.lat.get(), ctx.lng.get())
210- val diff = end.sub(start)
211- val distance = start.getEarthDistance(end)
212- val timeout = 200L
213- // prevent division by 0
214- if (speed.equals(0 )) {
215- return
216- }
217- val timeRequired = distance / speed
218- val stepsRequired = timeRequired / (timeout.toDouble() / 1000 .toDouble())
219- // prevent division by 0
220- if (stepsRequired.equals(0 )) {
221- return
222- }
223- val deltaLat = diff.latDegrees() / stepsRequired
224- val deltaLng = diff.lngDegrees() / stepsRequired
225- val deltaLat2 = - deltaLat
226- val deltaLng2 = - deltaLng
227-
228- Log .normal(" Walking to ${end.toStringDegrees()} in $stepsRequired steps." )
229- var remainingStepsGoing = stepsRequired
230- var remainingStepsComing = stepsRequired
231- bot.runLoop(timeout, " WalkingLoop" ) { cancel ->
232- // don't run away when there are still Pokemon around
233- if (remainingStepsGoing.toInt().mod(20 ) == 0 || remainingStepsComing.toInt().mod(20 ) == 0 )
234- if (ctx.api.inventories.itemBag.hasPokeballs() && bot.api.map.getCatchablePokemon(ctx.blacklistedEncounters).size > 0 && settings.catchPokemon) {
235- // Stop walking
236- Log .normal(" Pausing to catch pokemon..." )
237- // Try to catch once, then wait for next walk loop
238- bot.task(CatchOneNearbyPokemon ())
239- return @runLoop
240- }
241-
242- if (remainingStepsGoing > 0 ) {
243- ctx.lat.addAndGet(deltaLat)
244- ctx.lng.addAndGet(deltaLng)
245-
246- ctx.server.setLocation(ctx.lat.get(), ctx.lng.get())
247- remainingStepsGoing--
248- } else if (remainingStepsGoing <= 0 ) {
249- ctx.lat.addAndGet(deltaLat2)
250- ctx.lng.addAndGet(deltaLng2)
251-
252- ctx.server.setLocation(ctx.lat.get(), ctx.lng.get())
253-
254- remainingStepsComing--
255- }
256-
257- if (remainingStepsComing <= 0 ) {
258- Log .normal(" Destination reached." )
259-
260- if (sendDone) {
261- ctx.server.sendGotoDone()
262- }
263-
264- ctx.walking.set(false )
265- cancel()
266- }
267- }
165+ walkPath(bot, ctx, settings, mutableListOf (end, start), speed, sendDone)
268166 }
269167
270168 private fun selectRandom (pokestops : List <Pokestop >, ctx : Context ): Pokestop {
@@ -299,4 +197,4 @@ class Walk(val sortedPokestops: List<Pokestop>, val lootTimeouts: Map<String, Lo
299197 // should not happen
300198 return pokestops.first()
301199 }
302- }
200+ }
0 commit comments