Skip to content
This repository has been archived by the owner on Jul 22, 2019. It is now read-only.

Commit

Permalink
Refactored walk, now single function is used (#863)
Browse files Browse the repository at this point in the history
    Refactored walk, now single function is used
  • Loading branch information
bg-git-hub authored and jabbink committed Aug 9, 2016
1 parent c42592f commit 5e7dbfa
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 170 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class LootOneNearbyPokestop(val sortedPokestops: List<Pokestop>, val lootTimeout
private var cooldownPeriod = 5

override fun run(bot: Bot, ctx: Context, settings: Settings) {
ctx.api.setLocation(ctx.lat.get(), ctx.lng.get(), 0.0)
val nearbyPokestops = sortedPokestops.filter {
it.canLoot(lootTimeouts = lootTimeouts, api = ctx.api)
}
Expand All @@ -35,7 +36,6 @@ class LootOneNearbyPokestop(val sortedPokestops: List<Pokestop>, val lootTimeout
if (settings.displayPokestopName)
pokestopID = "\"${closest.details.name}\""
Log.normal("Looting nearby pokestop $pokestopID")
ctx.api.setLocation(ctx.lat.get(), ctx.lng.get(), 0.0)
val result = closest.loot()

if (result?.itemsAwarded != null) {
Expand Down Expand Up @@ -68,10 +68,7 @@ class LootOneNearbyPokestop(val sortedPokestops: List<Pokestop>, val lootTimeout
lootTimeouts.put(closest.id, closest.cooldownCompleteTimestampMs)
}
Result.OUT_OF_RANGE -> {
val location = S2LatLng.fromDegrees(closest.latitude, closest.longitude)
val self = S2LatLng.fromDegrees(ctx.lat.get(), ctx.lng.get())
val distance = self.getEarthDistance(location)
Log.red("Pokestop out of range; distance: $distance")
Log.red("Pokestop out of range; distance: ${closest.distance}")
}
Result.IN_COOLDOWN_PERIOD -> {
lootTimeouts.put(closest.id, ctx.api.currentTimeMillis() + cooldownPeriod * 60 * 1000)
Expand Down
228 changes: 63 additions & 165 deletions src/main/kotlin/ink/abb/pogo/scraper/tasks/Walk.kt
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,7 @@ class Walk(val sortedPokestops: List<Pokestop>, val lootTimeouts: Map<String, Lo
ctx.server.coordinatesToGoTo.removeAt(0)
Log.normal("Walking to ${coordinates.latRadians()}, ${coordinates.lngRadians()}")

if (settings.followStreets) {
walkRoute(bot, ctx, settings, S2LatLng.fromDegrees(coordinates.latRadians(), coordinates.lngRadians()), settings.speed, true)
} else {
walk(bot, ctx, settings, S2LatLng.fromDegrees(coordinates.latRadians(), coordinates.lngRadians()), settings.speed, true)
}
walk(bot, ctx, settings, S2LatLng.fromDegrees(coordinates.latRadians(), coordinates.lngRadians()), settings.speed, true)
} else {
val nearestUnused: List<Pokestop> = sortedPokestops.filter {
val canLoot = it.canLoot(ignoreDistance = true, lootTimeouts = lootTimeouts, api = ctx.api)
Expand All @@ -57,39 +53,78 @@ class Walk(val sortedPokestops: List<Pokestop>, val lootTimeouts: Map<String, Lo
if (settings.displayPokestopName)
Log.normal("Walking to pokestop \"${chosenPokestop.details.name}\"")

if (settings.followStreets) {
walkRoute(bot, ctx, settings, S2LatLng.fromDegrees(chosenPokestop.latitude, chosenPokestop.longitude), settings.speed, false)
} else {
walk(bot, ctx, settings, S2LatLng.fromDegrees(chosenPokestop.latitude, chosenPokestop.longitude), settings.speed, false)
}
walk(bot, ctx, settings, S2LatLng.fromDegrees(chosenPokestop.latitude, chosenPokestop.longitude), settings.speed, false)
}
}
}

fun walk(bot: Bot, ctx: Context, settings: Settings, end: S2LatLng, speed: Double, sendDone: Boolean) {
val start = S2LatLng.fromDegrees(ctx.lat.get(), ctx.lng.get())
val diff = end.sub(start)
val distance = start.getEarthDistance(end)
val timeout = 200L
// prevent division by 0
private fun walk(bot: Bot, ctx: Context, settings: Settings, end: S2LatLng, speed: Double, sendDone: Boolean) {
if (settings.followStreets) {
walkRoute(bot, ctx, settings, end, speed, sendDone)
} else {
walkDirectly(bot, ctx, settings, end, speed, sendDone)
}
}

private fun walkDirectly(bot: Bot, ctx: Context, settings: Settings, end: S2LatLng, speed: Double, sendDone: Boolean) {
walkPath(bot, ctx, settings, mutableListOf(end), speed, sendDone)
}

private fun walkRoute(bot: Bot, ctx: Context, settings: Settings, end: S2LatLng, speed: Double, sendDone: Boolean) {
val coordinatesList = getRouteCoordinates(S2LatLng.fromDegrees(ctx.lat.get(), ctx.lng.get()), end)
if (coordinatesList.size >= 0) {
walkPath(bot, ctx, settings, coordinatesList, speed, sendDone)
} else {
walkDirectly(bot, ctx, settings, end, speed, sendDone)
}
}

//all walk functions should call this one
private fun walkPath(bot: Bot, ctx: Context, settings: Settings, path: MutableList<S2LatLng>, speed: Double, sendDone: Boolean) {
if (speed.equals(0)) {
return
}
val timeRequired = distance / speed
val stepsRequired = timeRequired / (timeout.toDouble() / 1000.toDouble())
// prevent division by 0
if (stepsRequired.equals(0)) {
if(path.isEmpty()) {
return
}
val deltaLat = diff.latDegrees() / stepsRequired
val deltaLng = diff.lngDegrees() / stepsRequired

Log.normal("Walking to ${end.toStringDegrees()} in $stepsRequired steps.")
var remainingSteps = stepsRequired
val timeout = 200L

var remainingSteps = 0.0
var deltaLat = 0.0
var deltaLng = 0.0

val pauseWalk: AtomicBoolean = AtomicBoolean(false)
var pauseCounter = 2

bot.runLoop(timeout, "WalkingLoop") { cancel ->

if(remainingSteps <= 0) {
if (path.isEmpty()) {
Log.normal("Destination reached.")
if (sendDone) {
ctx.server.sendGotoDone()
}
ctx.walking.set(false)
cancel()
} else {
//calculate delta lat/long for next step
val start = S2LatLng.fromDegrees(ctx.lat.get(), ctx.lng.get())
val nextPoint = path.first()
path.removeAt(0)
val diff = nextPoint.sub(start)
val distance = start.getEarthDistance(nextPoint)
val timeRequired = distance / speed
val stepsRequired = timeRequired / (timeout.toDouble() / 1000.toDouble())

deltaLat = diff.latDegrees() / stepsRequired
deltaLng = diff.lngDegrees() / stepsRequired

Log.normal("Walking to ${nextPoint.toStringDegrees()} in $stepsRequired steps.")
remainingSteps = stepsRequired
}
}

if (pauseWalk.get()) {
Thread.sleep(timeout * 2)
pauseCounter--
Expand Down Expand Up @@ -121,150 +156,13 @@ class Walk(val sortedPokestops: List<Pokestop>, val lootTimeouts: Map<String, Lo
ctx.server.setLocation(lat, lng)

remainingSteps--
if (remainingSteps <= 0) {
Log.normal("Destination reached.")

if (sendDone) {
ctx.server.sendGotoDone()
}

ctx.walking.set(false)
cancel()
}
}

}

fun walkRoute(bot: Bot, ctx: Context, settings: Settings, end: S2LatLng, speed: Double, sendDone: Boolean) {
if (speed.equals(0)) {
return
}
val timeout = 200L
val coordinatesList = getRouteCoordinates(S2LatLng.fromDegrees(ctx.lat.get(), ctx.lng.get()), end)
if (coordinatesList.size <= 0) {
walk(bot, ctx, settings, end, speed, sendDone)
} else {
val pauseWalk: AtomicBoolean = AtomicBoolean(false)
var pauseCounter = 2
bot.runLoop(timeout, "WalkingLoop") { cancel ->
if (pauseWalk.get()) {
Thread.sleep(timeout * 2)
pauseCounter--
if (!(ctx.api.inventories.itemBag.hasPokeballs() && bot.api.map.getCatchablePokemon(ctx.blacklistedEncounters).size > 0 && settings.catchPokemon)) {
// api break free
pauseWalk.set(false)
pauseCounter = 0
}
// fixed tries break free
if (pauseCounter > 0) {
return@runLoop
} else {
pauseWalk.set(false)
}
}
// don't run away when there are still Pokemon around
if (pauseCounter > 0 && ctx.api.inventories.itemBag.hasPokeballs() && bot.api.map.getCatchablePokemon(ctx.blacklistedEncounters).size > 0 && settings.catchPokemon) {
// Stop walking
Log.normal("Pausing to catch pokemon...")
pauseCounter = 2
pauseWalk.set(true)
return@runLoop
}

val start = S2LatLng.fromDegrees(ctx.lat.get(), ctx.lng.get())
val step = coordinatesList.first()
coordinatesList.removeAt(0)
val diff = step.sub(start)
val distance = start.getEarthDistance(step)
val timeRequired = distance / speed
val stepsRequired = timeRequired / (timeout.toDouble() / 1000.toDouble())
if (stepsRequired.equals(0)) {
cancel()
}
val deltaLat = diff.latDegrees() / stepsRequired
val deltaLng = diff.lngDegrees() / stepsRequired
var remainingSteps = stepsRequired
while (remainingSteps > 0) {
ctx.lat.addAndGet(deltaLat)
ctx.lng.addAndGet(deltaLng)
ctx.server.setLocation(ctx.lat.get(), ctx.lng.get())
remainingSteps--
Thread.sleep(timeout)
}

if (coordinatesList.size <= 0) {
Log.normal("Destination reached.")
if (sendDone) {
ctx.server.sendGotoDone()
}
ctx.walking.set(false)
cancel()

}
}
}
}

fun walkAndComeBack(bot: Bot, ctx: Context, settings: Settings, end: S2LatLng, speed: Double, sendDone: Boolean) {
// TODO not used anymore? remove?
private fun walkAndComeBack(bot: Bot, ctx: Context, settings: Settings, end: S2LatLng, speed: Double, sendDone: Boolean) {
val start = S2LatLng.fromDegrees(ctx.lat.get(), ctx.lng.get())
val diff = end.sub(start)
val distance = start.getEarthDistance(end)
val timeout = 200L
// prevent division by 0
if (speed.equals(0)) {
return
}
val timeRequired = distance / speed
val stepsRequired = timeRequired / (timeout.toDouble() / 1000.toDouble())
// prevent division by 0
if (stepsRequired.equals(0)) {
return
}
val deltaLat = diff.latDegrees() / stepsRequired
val deltaLng = diff.lngDegrees() / stepsRequired
val deltaLat2 = -deltaLat
val deltaLng2 = -deltaLng

Log.normal("Walking to ${end.toStringDegrees()} in $stepsRequired steps.")
var remainingStepsGoing = stepsRequired
var remainingStepsComing = stepsRequired
bot.runLoop(timeout, "WalkingLoop") { cancel ->
// don't run away when there are still Pokemon around
if (remainingStepsGoing.toInt().mod(20) == 0 || remainingStepsComing.toInt().mod(20) == 0)
if (ctx.api.inventories.itemBag.hasPokeballs() && bot.api.map.getCatchablePokemon(ctx.blacklistedEncounters).size > 0 && settings.catchPokemon) {
// Stop walking
Log.normal("Pausing to catch pokemon...")
// Try to catch once, then wait for next walk loop
bot.task(CatchOneNearbyPokemon())
return@runLoop
}

if (remainingStepsGoing > 0) {
ctx.lat.addAndGet(deltaLat)
ctx.lng.addAndGet(deltaLng)

ctx.server.setLocation(ctx.lat.get(), ctx.lng.get())
remainingStepsGoing--
} else if (remainingStepsGoing <= 0) {
ctx.lat.addAndGet(deltaLat2)
ctx.lng.addAndGet(deltaLng2)

ctx.server.setLocation(ctx.lat.get(), ctx.lng.get())

remainingStepsComing--
}

if (remainingStepsComing <= 0) {
Log.normal("Destination reached.")

if (sendDone) {
ctx.server.sendGotoDone()
}

ctx.walking.set(false)
cancel()
}
}
walkPath(bot, ctx, settings, mutableListOf(end, start), speed, sendDone)
}

private fun selectRandom(pokestops: List<Pokestop>, ctx: Context): Pokestop {
Expand Down Expand Up @@ -299,4 +197,4 @@ class Walk(val sortedPokestops: List<Pokestop>, val lootTimeouts: Map<String, Lo
// should not happen
return pokestops.first()
}
}
}

0 comments on commit 5e7dbfa

Please sign in to comment.