Repository: PokemonGoF/PokemonGo-Bot

Issue:
Trying to catch same pokemon over and over again
### Expected Behavior

Skip the pokemon. Continue moving to fort.
### Actual Behavior

Trying to catch the pokemon over and over again.

```
[22:32:06] A wild Nidoran M appeared! [CP 496] [Potential 0.44] [A/D/S 6/3/11]
[22:32:09] No usable pokeballs found!
[22:32:19] Skipping Recycling of Items. 155 space left in bag.
[22:32:19] Something rustles nearby!                                                                                             [22:32:20] A wild Nidoran M appeared! [CP 496] [Potential 0.44] [A/D/S 6/3/11]
[22:32:24] No usable pokeballs found!
[22:32:33] Skipping Recycling of Items. 155 space left in bag.
[22:32:33] Something rustles nearby!                                                                                             [22:32:34] A wild Nidoran M appeared! [CP 496] [Potential 0.44] [A/D/S 6/3/11]
[22:32:37] No usable pokeballs found!
[22:32:48] Skipping Recycling of Items. 155 space left in bag.
[22:32:48] Something rustles nearby!                                                                                             [22:32:50] A wild Nidoran M appeared! [CP 496] [Potential 0.44] [A/D/S 6/3/11]
[22:32:53] No usable pokeballs found!
[22:33:02] Skipping Recycling of Items. 155 space left in bag.
[22:33:02] Something rustles nearby!                                                                                             [22:33:03] A wild Nidoran M appeared! [CP 496] [Potential 0.44] [A/D/S 6/3/11]
[22:33:06] No usable pokeballs found!
[22:33:16] Skipping Recycling of Items. 155 space left in bag.
[22:33:16] Something rustles nearby!                                                                                             [22:33:17] A wild Nidoran M appeared! [CP 496] [Potential 0.44] [A/D/S 6/3/11]
[22:33:20] No usable pokeballs found!
[22:33:29] Skipping Recycling of Items. 155 space left in bag.
[22:33:29] Something rustles nearby!                                                                                             [22:33:31] A wild Nidoran M appeared! [CP 496] [Potential 0.44] [A/D/S 6/3/11]
[22:33:34] No usable pokeballs found!
[22:33:43] Skipping Recycling of Items. 155 space left in bag.
[22:33:43] Something rustles nearby!                                                                                             [22:33:44] A wild Nidoran M appeared! [CP 496] [Potential 0.44] [A/D/S 6/3/11]
[22:33:48] No usable pokeballs found!
[22:33:59] Skipping Recycling of Items. 155 space left in bag.
[22:33:59] Something rustles nearby!                                                                                             [22:34:00] A wild Nidoran M appeared! [CP 496] [Potential 0.44] [A/D/S 6/3/11]
[22:34:03] No usable pokeballs found!
[22:34:12] Skipping Recycling of Items. 155 space left in bag.
[22:34:12] Something rustles nearby!                                                                                             [22:34:18] A wild Nidoran M appeared! [CP 496] [Potential 0.44] [A/D/S 6/3/11]
[22:34:22] No usable pokeballs found!
[22:34:31] Skipping Recycling of Items. 155 space left in bag.
[22:34:31] Something rustles nearby!                                                                                             
[22:34:32] A wild Nidoran M appeared! [CP 496] [Potential 0.44] [A/D/S 6/3/11]
[22:34:35] No usable pokeballs found!
```
### Your config.json (remove your credentials and any other private info)

``` json
{
    ...
    "tasks": [
        {
            "type": "UpdateLiveStats",
            "config": {
                "min_interval": 60,
                "stats": [
                    "uptime",
                    "km_walked",
                    "level_stats",
                    "xp_earned",
                    "xp_per_hour",
                    "stops_visited",
                    "pokemon_stats",
                    "pokeballs_thrown",
                    "stardust_earned",
                    "highest_cp_pokemon",
                    "most_perfect_pokemon"
                ]
            }
        },
        {
            "type": "HandleSoftBan",
            "config": {
                "enabled": true
            }
        },
        {
            "type": "CollectLevelUpReward",
            "config": {
                "enabled": false
            }
        },
        {
            "type": "IncubateEggs",
            "config": {
                "enabled": true,
                "longer_eggs_first": true
            }
        },
        {
            "type": "TransferPokemon",
            "config": {
                "enabled": true
            }
        },
        {
            "type": "NicknamePokemon",
            "config": {
                "enabled": false,
                "nickname_template": "{iv_pct}_{iv_ads}"
            }
        },
        {
            "type": "EvolvePokemon",
            "config": {
                "enabled": false,
                "evolve_all": "none",
                "first_evolve_by": "cp",
                "evolve_above_cp": 500,
                "evolve_above_iv": 0.8,
                "logic": "and",
                "evolve_speed": 20,
                "use_lucky_egg": false
            }
        },
        {
            "type": "RecycleItems",
            "config": {
                "enabled": true,
                "min_empty_space": 10,
                "item_filter": {
                    "Pokeball": {
                        "keep": 150
                    },
                    "Greatball": {
                        "keep": 150
                    },
                    "Ultraball": {
                        "keep": 150
                    },
                    "Masterball": {
                        "keep": 150
                    },
                    "Potion": {
                        "keep": 0
                    },
                    "Super Potion": {
                        "keep": 0
                    },
                    "Hyper Potion": {
                        "keep": 0
                    },
                    "Max Potion": {
                        "keep": 75
                    },
                    "Revive": {
                        "keep": 10
                    },
                    "Max Revive": {
                        "keep": 20
                    },
                    "Razz Berry": {
                        "keep": 100
                    }
                }
            }
        },
        {
            "type": "CatchVisiblePokemon",
            "config": {
                "enabled": true
            }
        },
        {
            "type": "CatchLuredPokemon",
            "config": {
                "enabled": true
            }
        },
        {
            "type": "SpinFort",
            "config": {
                "enabled": true
            }
        },
        {
            "type": "MoveToMapPokemon",
            "config": {
                "enabled": true,
                "address": "http://127.0.0.1:8080",
                "max_distance": 50000,
                "min_time": 60,
                "min_ball": 20,
                "prioritize_vips": true,
                "snipe": true,
                "snipe_high_prio_only": true,
                "snipe_high_prio_threshold": 600,
                "update_map": false,
                "mode": "distance",
                "catch": { ... }
            }
        },
        {
            "type": "MoveToFort",
            "config": {
                "enabled": true,
                "lure_attraction": true,
                "lure_max_distance": 2000
            }
        },
        {
            "type": "FollowSpiral",
            "config": {
                "enabled": true,
                "diameter": 600,
                "step_size": 70
            }
        }
    ],
    "map_object_cache_time": 5,
    "forts": {
        "avoid_circles": true,
        "max_circle_size": 50
    },
    "websocket_server": false,
    "walk": 80,
    "action_wait_min": 1,
    "action_wait_max": 2,
    "debug": false,
    "test": false,
    "health_record": false,
    "location_cache": true,
    "distance_unit": "km",
    "reconnecting_timeout": 10,
    "catch_randomize_reticle_factor": 1,
    "catch_randomize_spin_factor": 1,
    "logging_color": true,
    ...
}
```
### Other Information

OS: Ubuntu  16.04
Git Commit: 138b66413e45efcef1ae015388b80a2725e7a7d8
Python Version: 2.7.12



Discussion:
The PR is in review. #3421 and another one PR I couldn't fine.

Currently, you can fix by add the code below to `pokemon_catch_worker.py`:

```
        pokemon = Pokemon(self.pokemon_list, pokemon_data)

       # --- the code to be added ----
        maximum_ball = ITEM_ULTRABALL
        items_stock = self.bot.current_inventory()
        current_ball = ITEM_POKEBALL
        num_ball = 0
        while current_ball < maximum_ball:
            num_ball += items_stock[current_ball]
            current_ball += 1

        # skip if no available pokeballs
        if num_ball <= 0:
            return WorkerResult.SUCCESS
        # --- end of the code to be added ----

        # skip ignored pokemon
        if not self._should_catch_pokemon(pokemon):
```

and then add what describes in my PR #3421 

Earlier, I already looked at your PR. Why is your PR incomplete? @joaodragao

@nikhil-pandey It was at previous commit. Thus, it's conflicted now. I don't know how to solve it.

@joaodragao's PR [#3421](https://github.com/PokemonGoF/PokemonGo-Bot/pull/3421) has promise to resolve this issue.

I get same bug , set catch iv 999 can't skip this bug

I trace the code , if you set config.json

```
"min_empty_space": 3,
```

will be loop , and I use dev branch , and bug in (line 30 ~ 39)
pokemongo_bot/cell_workers/recycle_items.py 

it check setting first and loop , so remove the min_empty_space settings will be okay

I dont think its anything to do with `min_empty_space`.

```
2016-08-12 09:18:16,704 [PokemonGoBot] [INFO] [bot_start] Starting bot...
2016-08-12 09:18:18,732 [IncubateEggs] [INFO] [next_egg_incubates] Next egg incubates in 0.12 km
2016-08-12 09:18:25,478 [PokemonCatchWorker] [INFO] [pokemon_appeared] A wild Geodude appeared! [CP 486] [Potential 0.42] [A/D/S 12/5/2]
2016-08-12 09:18:28,149 [PokemonCatchWorker] [INFO] [no_pokeballs] No usable pokeballs found!
2016-08-12 09:18:34,152 [CatchLuredPokemon] [INFO] [lured_pokemon_found] Lured pokemon at fort Zoltar on Santa Monica Pier (975805c30dc6403ab07856c607e9e9a9.12)
2016-08-12 09:18:35,091 [CatchLuredPokemon] [INFO] [lured_pokemon_found] Lured pokemon at fort End of Route 66 (246cd78fc6cb4addb7ff69d695eca867.12)
2016-08-12 09:18:36,164 [PokemonCatchWorker] [INFO] [pokemon_appeared] A wild Mankey appeared! [CP 514] [Potential 0.47] [A/D/S 4/15/2]
2016-08-12 09:18:39,376 [PokemonCatchWorker] [INFO] [no_pokeballs] No usable pokeballs found!
2016-08-12 09:18:51,388 [PokemonCatchWorker] [INFO] [pokemon_appeared] A wild Geodude appeared! [CP 486] [Potential 0.42] [A/D/S 12/5/2]
2016-08-12 09:18:55,351 [PokemonCatchWorker] [INFO] [no_pokeballs] No usable pokeballs found!
2016-08-12 09:19:00,379 [CatchLuredPokemon] [INFO] [lured_pokemon_found] Lured pokemon at fort Zoltar on Santa Monica Pier (975805c30dc6403ab07856c607e9e9a9.12)
2016-08-12 09:19:00,382 [CatchLuredPokemon] [INFO] [lured_pokemon_found] Lured pokemon at fort End of Route 66 (246cd78fc6cb4addb7ff69d695eca867.12)
2016-08-12 09:19:01,529 [PokemonCatchWorker] [INFO] [pokemon_appeared] A wild Mankey appeared! [CP 514] [Potential 0.47] [A/D/S 4/15/2]
2016-08-12 09:19:04,247 [PokemonCatchWorker] [INFO] [no_pokeballs] No usable pokeballs found!
2016-08-12 09:19:16,149 [PokemonCatchWorker] [INFO] [pokemon_appeared] A wild Geodude appeared! [CP 486] [Potential 0.42] [A/D/S 12/5/2]
2016-08-12 09:19:20,031 [PokemonCatchWorker] [INFO] [no_pokeballs] No usable pokeballs found!
2016-08-12 09:19:25,058 [CatchLuredPokemon] [INFO] [lured_pokemon_found] Lured pokemon at fort Zoltar on Santa Monica Pier (975805c30dc6403ab07856c607e9e9a9.12)
2016-08-12 09:19:25,060 [CatchLuredPokemon] [INFO] [lured_pokemon_found] Lured pokemon at fort End of Route 66 (246cd78fc6cb4addb7ff69d695eca867.12)
2016-08-12 09:19:26,130 [PokemonCatchWorker] [INFO] [pokemon_appeared] A wild Mankey appeared! [CP 514] [Potential 0.47] [A/D/S 4/15/2]
2016-08-12 09:19:28,782 [PokemonCatchWorker] [INFO] [no_pokeballs] No usable pokeballs found!
```

It seems like lured pokemons are being processed  even if there are no pokeballs.

hmm... anyway I remove `min_empty_space` and set catch max iv will be okay , and after get some ball fix back

my log :

```
[11:20:19] Starting bot...
[11:20:19] Received level up reward: []
[11:20:19] Next egg incubates in 0.94 km
[11:20:20] Skipping Recycling of Items. 143 space left in bag.
[11:20:20] Something rustles nearby!
[11:20:21] Skipping Recycling of Items. 143 space left in bag.
[11:20:21] Something rustles nearby!
[11:20:22] Skipping Recycling of Items. 143 space left in bag.
[11:20:22] Something rustles nearby!
[11:20:23] Skipping Recycling of Items. 143 space left in bag.
[11:20:23] Something rustles nearby!
[11:20:24] Skipping Recycling of Items. 143 space left in bag.
[11:20:24] Something rustles nearby!
[11:20:25] Skipping Recycling of Items. 143 space left in bag.
[11:20:25] Something rustles nearby!
[11:20:26] Skipping Recycling of Items. 143 space left in bag.
[11:20:26] Something rustles nearby!
[11:20:28] Skipping Recycling of Items. 143 space left in bag.
[11:20:28] Something rustles nearby!
[11:20:29] Skipping Recycling of Items. 143 space left in bag.
[11:20:29] Something rustles nearby!
```

If you have the latest dev branch, removing **min_empty_space** will have no effect. Its set to **6** by default.

okay...sorry I trace the master branch code , but I got the bug in dev branch , so look like I fix this in master branch with remove min_empty_space , and switch back to dev : (

@JokerCatz  Judging by your logs, you just removed the catching part by bumping up the iv. Still the pokemon is being encountered. The expected behaviour when there are no pokeballs is to switch to farming mode and get pokeballs.

Bot feature says "Farm mode" if there is no Pokeball available. This needs to be a feature request because this is not working as of now. This issue was bugging me for some days now. The code described in the top is not working. I duplicated some code from "pokemon_catch_worker.py" to "catch_lured_pokemon.py". Exchange the following code in "catch_lured_pokemon.py":

```
        pokemon_to_catch.append(result)

        self.emit_event(
            'lured_pokemon_found',
            formatted='Lured pokemon at fort {fort_name} ({fort_id})',
            data=result
        )
```

with:

```
        maximum_ball = ITEM_ULTRABALL
        items_stock = self.bot.current_inventory()
        current_ball = ITEM_POKEBALL
        num_ball = 0
        while current_ball < maximum_ball:
            num_ball += items_stock[current_ball]
            current_ball += 1
        if num_ball > 0:
            pokemon_to_catch.append(result)

        if num_ball > 0:
            self.emit_event(
                'lured_pokemon_found',
                formatted='Lured pokemon at fort {fort_name} ({fort_id})',
                data=result
            )
        else:
            self.emit_event(
                'lured_pokemon_found',
                formatted='Skip lured pokemon at fort {fort_name} ({fort_id})',
                data=result
            )
```

At least the bot is still working then. Sorry, first time python use on my side but maybe it helps some people.


Resolving patch:
diff --git a/pokemongo_bot/cell_workers/move_to_map_pokemon.py b/pokemongo_bot/cell_workers/move_to_map_pokemon.py
--- a/pokemongo_bot/cell_workers/move_to_map_pokemon.py
+++ b/pokemongo_bot/cell_workers/move_to_map_pokemon.py
@@ -257,9 +257,12 @@ def work(self):
 
         pokemon = pokemon_list[0]
 
-        # if we only have ultraballs and the target is not a vip don't snipe/walk
-        if (pokeballs + superballs) < self.min_ball and not pokemon['is_vip']:
-            return WorkerResult.SUCCESS
+        if pokeballs < 1:
+            if superballs < 1:
+                if ultraballs < 1:
+                    return WorkerResult.SUCCESS
+                if not pokemon['is_vip']:
+                    return WorkerResult.SUCCESS
 
         if self.config['snipe']:
             if self.snipe_high_prio_only:
diff --git a/pokemongo_bot/cell_workers/pokemon_catch_worker.py b/pokemongo_bot/cell_workers/pokemon_catch_worker.py
--- a/pokemongo_bot/cell_workers/pokemon_catch_worker.py
+++ b/pokemongo_bot/cell_workers/pokemon_catch_worker.py
@@ -67,6 +67,10 @@ def __init__(self, pokemon, bot, config):
     ############################################################################
 
     def work(self, response_dict=None):
+        pokeballs = self.bot.item_inventory_count(1)
+        superballs = self.bot.item_inventory_count(2)
+        ultraballs = self.bot.item_inventory_count(3)
+
         response_dict = response_dict or self.create_encounter_api_call()
 
         # validate response
@@ -93,6 +97,14 @@ def work(self, response_dict=None):
         if not self._should_catch_pokemon(pokemon):
             return WorkerResult.SUCCESS
 
+        is_vip = self._is_vip_pokemon(pokemon)
+        if pokeballs < 1:
+            if superballs < 1:
+                if ultraballs < 1:
+                    return WorkerResult.SUCCESS
+                if not is_vip:
+                    return WorkerResult.SUCCESS
+        
         # log encounter
         self.emit_event(
             'pokemon_appeared',
@@ -113,7 +125,6 @@ def work(self, response_dict=None):
         sleep(3)
 
         # check for VIP pokemon
-        is_vip = self._is_vip_pokemon(pokemon)
         if is_vip:
             self.emit_event('vip_pokemon', formatted='This is a VIP pokemon. Catch!!!')
 


Test patch: