diff --git a/utama_core/strategy/examples/defense_strategy.py b/utama_core/strategy/examples/defense_strategy.py index ca394c5d..51ba98ba 100644 --- a/utama_core/strategy/examples/defense_strategy.py +++ b/utama_core/strategy/examples/defense_strategy.py @@ -5,6 +5,7 @@ from utama_core.config.enums import Role, Tactic from utama_core.entities.data.object import TeamType +from utama_core.entities.data.vector import Vector2D from utama_core.entities.game import Game from utama_core.skills.src.block import block_attacker from utama_core.skills.src.defend_parameter import defend_parameter @@ -13,6 +14,19 @@ from utama_core.strategy.common import AbstractBehaviour, AbstractStrategy +def _assign_roles_dynamic(game: Game) -> dict: + goal_line = game.field.my_goal_line + gx = (goal_line[0][0] + goal_line[1][0]) / 2 + gy = (goal_line[0][1] + goal_line[1][1]) / 2 + goal = Vector2D(gx, gy) + robots = sorted(game.friendly_robots.values(), key=lambda r: r.p.distance_to(goal)) + n = len(robots) + if n == 0: + return {} + roles = [Role.GOALKEEPER] + [Role.DEFENDER] * max(0, n - 2) + [Role.MIDFIELDER] + return {r.id: role for r, role in zip(robots, roles)} + + class FindBlockingTarget(AbstractBehaviour): def __init__(self, wr_blocking_target: str, name: Optional[str] = "FindBlockingTarget2BB"): super().__init__(name=name) @@ -155,11 +169,7 @@ def setup_(self): def update(self) -> py_trees.common.Status: self.blackboard.set( self.role_map_key, - { - 0: Role.MIDFIELDER, - 1: Role.DEFENDER, - 2: Role.GOALKEEPER, - }, + _assign_roles_dynamic(self.blackboard.game), ) return py_trees.common.Status.SUCCESS