Skip to content

Commit

Permalink
create order -> action methods
Browse files Browse the repository at this point in the history
  • Loading branch information
cameronangliss committed Jan 4, 2025
1 parent f6f541e commit 751645b
Showing 1 changed file with 106 additions and 0 deletions.
106 changes: 106 additions & 0 deletions src/poke_env/player/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
from poke_env.environment.battle import Battle
from poke_env.environment.double_battle import DoubleBattle
from poke_env.environment.move import Move
from poke_env.environment.pokemon import Pokemon
from poke_env.player.battle_order import (
BattleOrder,
DefaultBattleOrder,
Expand Down Expand Up @@ -540,6 +541,111 @@ def _doubles_action_to_order_individual(
), "invalid pick"
return order

Check warning on line 542 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L542

Added line #L542 was not covered by tests

###################################################################################
# Order -> Action methods

@staticmethod
def order_to_action(order: BattleOrder, battle: AbstractBattle) -> ActionType:
if isinstance(battle, Battle):
assert not isinstance(order, DoubleBattleOrder)
return PokeEnv._singles_order_to_action(order, battle) # type: ignore
elif isinstance(battle, DoubleBattle):
assert isinstance(order, DoubleBattleOrder)
return PokeEnv._doubles_order_to_action(order, battle) # type: ignore

Check warning on line 554 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L549-L554

Added lines #L549 - L554 were not covered by tests
else:
raise TypeError()

Check warning on line 556 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L556

Added line #L556 was not covered by tests

@staticmethod
def _singles_order_to_action(order: BattleOrder, battle: Battle) -> int:
assert order.order is not None
if isinstance(order, ForfeitBattleOrder):
return -1
elif isinstance(order.order, Pokemon):
action = [p.base_species for p in battle.team.values()].index(

Check warning on line 564 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L560-L564

Added lines #L560 - L564 were not covered by tests
order.order.base_species
)
else:
assert not battle.force_switch, "invalid pick"
active_mon = battle.active_pokemon
assert active_mon is not None
mvs = (

Check warning on line 571 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L568-L571

Added lines #L568 - L571 were not covered by tests
battle.available_moves
if len(battle.available_moves) == 1
and battle.available_moves[0].id in ["struggle", "recharge"]
else list(active_mon.moves.values())
)
action = mvs.index(order.order)
if order.mega:
gimmick = 1
elif order.z_move:
gimmick = 2
elif order.dynamax:
gimmick = 3
elif order.terastallize:
gimmick = 4

Check warning on line 585 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L577-L585

Added lines #L577 - L585 were not covered by tests
else:
gimmick = 0
action = 6 + action + 4 * gimmick
assert order.order in battle.available_moves, "invalid pick"
assert not order.mega or battle.can_mega_evolve, "invalid pick"
assert not order.z_move or (

Check warning on line 591 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L587-L591

Added lines #L587 - L591 were not covered by tests
battle.can_z_move and order.order in active_mon.available_z_moves
), "invalid pick"
assert not order.dynamax or battle.can_dynamax, "invalid pick"
assert not order.terastallize or battle.can_tera is not None, "invalid pick"
return action

Check warning on line 596 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L594-L596

Added lines #L594 - L596 were not covered by tests

@staticmethod
def _doubles_order_to_action(
order: DoubleBattleOrder, battle: DoubleBattle
) -> List[int]:
action1 = PokeEnv._doubles_order_to_action_individual(

Check warning on line 602 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L602

Added line #L602 was not covered by tests
order.first_order, battle, 0
)
action2 = PokeEnv._doubles_order_to_action_individual(

Check warning on line 605 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L605

Added line #L605 was not covered by tests
order.second_order, battle, 1
)
return [action1, action2]

Check warning on line 608 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L608

Added line #L608 was not covered by tests

@staticmethod
def _doubles_order_to_action_individual(
order: Optional[BattleOrder], battle: DoubleBattle, pos: int
) -> int:
if order is None:
return 0
assert order.order is not None
if isinstance(order, ForfeitBattleOrder):
return -1
elif isinstance(order.order, Pokemon):
action = [p.base_species for p in battle.team.values()].index(

Check warning on line 620 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L614-L620

Added lines #L614 - L620 were not covered by tests
order.order.base_species
) + 1
else:
assert not battle.force_switch[pos], "invalid pick"
active_mon = battle.active_pokemon[pos]
assert active_mon is not None, "invalid pick"
mvs = (

Check warning on line 627 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L624-L627

Added lines #L624 - L627 were not covered by tests
battle.available_moves[pos]
if len(battle.available_moves[pos]) == 1
and battle.available_moves[pos][0].id in ["struggle", "recharge"]
else list(active_mon.moves.values())
)
action = mvs.index(order.order)
target = order.move_target + 2
if order.terastallize:
gimmick = 1

Check warning on line 636 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L633-L636

Added lines #L633 - L636 were not covered by tests
else:
gimmick = 0
action = 6 + action + 4 * target + 20 * gimmick
assert order.order in battle.available_moves[pos], "invalid pick"
assert (

Check warning on line 641 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L638-L641

Added lines #L638 - L641 were not covered by tests
not order.terastallize or battle.can_tera[pos] is not False
), "invalid pick"
assert order.move_target in battle.get_possible_showdown_targets(

Check warning on line 644 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L644

Added line #L644 was not covered by tests
order.order, active_mon
), "invalid pick"
return action

Check warning on line 647 in src/poke_env/player/env.py

View check run for this annotation

Codecov / codecov/patch

src/poke_env/player/env.py#L647

Added line #L647 was not covered by tests

###################################################################################
# Helper methods

Expand Down

0 comments on commit 751645b

Please sign in to comment.