poke-env icon indicating copy to clipboard operation
poke-env copied to clipboard

Bug with battle.force_switch

Open akashsara opened this issue 2 years ago • 6 comments

So there's actually two bugs. The first is what I mentioned here. Specifically, in the scenario where battle.force_switch is True and there are no Pokemon left on the bench, both battle.available_moves and battle.available_switches are empty.

I fixed this while defining a custom _action_to_move() function but encountered a second bug. Specifically:

battle.won: None
battle.lost: False
battle.finished: False
battle.turn: 25
battle.force_switch: True

battle.team:
{
'p1: Latias': latias (pokemon object) [Active: False, Status: FNT], 
'p1: Zoroark': zoroark (pokemon object) [Active: False, Status: FNT], 
'p1: Nidoqueen': nidoqueen (pokemon object) [Active: False, Status: FNT], 
'p1: Shiftry': shiftry (pokemon object) [Active: False, Status: FNT], 
'p1: Kartana': kartana (pokemon object) [Active: True, Status: FNT], 
'p1: Corsola': corsolagalar (pokemon object) [Active: False, Status: FNT]
}

battle.available_switches:
[]

battle.available_moves:
[]

So this is really weird because the battle should be over here. All 6 Pokemon on the user's side have fainted so the fact that we have no actions is correct, but we shouldn't be receiving this state at all right?

akashsara avatar Jun 14 '22 05:06 akashsara

This might be related to #246

akashsara avatar Jun 14 '22 20:06 akashsara

Another weird scenario:

> print(battle.won, battle.lost, battle.finished, battle.turn)
None False False 8

> print(battle.force_switch, force_switch, battle.trapped)
False False True

> print(battle.team)
{'p1: Scyther': scyther (pokemon object) [Active: False, Status: None], 
'p1: Zoroark': zoroark (pokemon object) [Active: False, Status: None], 
'p1: Hitmonchan': hitmonchan (pokemon object) [Active: False, Status: None], 
'p1: Dialga': dialga (pokemon object) [Active: False, Status: FNT], 
'p1: Wobbuffet': wobbuffet (pokemon object) [Active: False, Status: FNT], 
'p1: Doublade': doublade (pokemon object) [Active: True, Status: None]}

> print(battle.available_switches)
[hitmonchan (pokemon object) [Active: False, Status: None], scyther (pokemon object) [Active: False, Status: None], 
doublade (pokemon object) [Active: True, Status: None]]

> print(battle.available_moves)
[flamethrower (Move object), darkpulse (Move object), nastyplot (Move object), sludgebomb (Move object)]

> print(battle.active_pokemon.moves)
{'ironhead': ironhead (Move object), 'shadowsneak': shadowsneak (Move object), 
'swordsdance': swordsdance (Move object), 'closecombat': closecombat (Move object)}

battle.available_moves and battle.moves have completely different values.

akashsara avatar Jun 15 '22 23:06 akashsara

That's a good find. Do you have an idea of where the parsing goes wrong?

hsahovic avatar Jun 16 '22 08:06 hsahovic

Unfortunately not. This was the only error that popped up while training and I haven't been able to reproduce it since then. I've had the other two scenarios I mentioned happen multiple times though. At the moment I'm just printing these different values whenever I encounter a scenario with no legal actions so I haven't been able to investigate further.

akashsara avatar Jun 17 '22 07:06 akashsara

I have one potential idea for this problem's cause - I'll test it today. It's related to requests message resulting in an action before some messages arriving at the same time have been parsed, and should be addressable by deferring the choice.

hsahovic avatar Jun 17 '22 08:06 hsahovic

I experimented with my hypothesis - It was wrong. However, in doing so, I did identify the root cause of every example I looked at: Zoroark and illusion. poke-env sometimes wrongly sets the pokemon that Zoroark was impersonating as fainted when it faints, and doesn't:

  1. Remove the fainted state when a request with contradictory information arrives
  2. Correctly interacts illusion related messages to status updates

There is a clear direction forward on both of these fronts. I'll take a crack at it over the weekend.

hsahovic avatar Jun 17 '22 23:06 hsahovic