Skip to content

Commit

Permalink
Merge pull request #113 from AresSC2/feat/remove-unit-from-squads
Browse files Browse the repository at this point in the history
feat: request to remove unit from squads
  • Loading branch information
raspersc2 authored Feb 27, 2024
2 parents ca2ec53 + 732e462 commit 4d4b6c6
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 2 deletions.
13 changes: 13 additions & 0 deletions docs/tutorials/tips_and_tricks.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,16 @@ defending_workers: Units = self.mediator.get_units_from_role(
role=UnitRole.DEFENDING, unit_type=UnitTypeId.SCV
)
```

## Selecting a unit already assigned to a `UnitSquad`
If you are using `ares-sc2` [unit squad system](../api_reference/manager_mediator.md#ares.managers.manager_mediator.ManagerMediator.get_squads),
and you want to select a unit already assigned to a squad then you should take care to remove the unit to ensure
accurate squad calculations. You can do so by making the following mediator request:

```python
from sc2.unit import Unit

# pretend this unit is already assigned to a unit squad
unit: Unit = self.units[0]
self.mediator.remove_tag_from_squads(tag=unit.tag)
```
5 changes: 3 additions & 2 deletions src/ares/build_runner/build_order_runner.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,9 +194,10 @@ async def do_step(self, step: BuildOrderStep) -> None:
force_close=True,
select_persistent_builder=command != UnitID.REFINERY,
only_select_persistent_builder=self.persistent_worker
and command in {UnitID.BARRACKS, UnitID.COMMANDCENTER}
and command
in {UnitID.BARRACKS, UnitID.FACTORY, UnitID.COMMANDCENTER}
and not self.ai.already_pending(UnitID.BARRACKS)
and self.ai.time < 90.0,
and self.ai.time < 125.0,
):
if next_building_position := await self.get_position(
step.command, step.target
Expand Down
2 changes: 2 additions & 0 deletions src/ares/consts.py
Original file line number Diff line number Diff line change
Expand Up @@ -264,6 +264,7 @@ class ManagerRequestType(str, Enum):
# SquadManager
GET_POSITION_OF_MAIN_SQUAD = "GET_POSITION_OF_MAIN_SQUAD"
GET_SQUADS = "GET_SQUADS"
REMOVE_TAG_FROM_SQUADS = "REMOVE_TAG_FROM_SQUADS"

# TerrainManager
BUILDING_POSITION_BLOCKED_BY_BURROWED_UNIT = (
Expand Down Expand Up @@ -363,6 +364,7 @@ class UnitRole(str, Enum):
HARASSING = "HARASSING" # units that are harassing
IDLE = "IDLE" # not doing anything
MAP_CONTROL = "MAP_CONTROL" # units controlling the map (lings/hellions?)
OFFENSIVE_REPAIR = "OFFENSIVE_REPAIR" # with the main force
OVERLORD_HUNTER = "OVERLORD_HUNTER" # units looking for overlords
PERSISTENT_BUILDER = "PERSISTENT_BUILDER" # does not get reassigned automatically
PROXY_WORKER = "PROXY_WORKER"
Expand Down
12 changes: 12 additions & 0 deletions src/ares/managers/manager_mediator.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,18 @@ def get_squads(self, **kwargs) -> list["UnitSquad"]:
**kwargs,
)

def remove_tag_from_squads(self, **kwargs) -> None:
"""
Squad Manager
Keyword args:
tag: int
"""
return self.manager_request(
ManagerName.SQUAD_MANAGER,
ManagerRequestType.REMOVE_TAG_FROM_SQUADS,
**kwargs,
)

"""
TerrainManager
"""
Expand Down
18 changes: 18 additions & 0 deletions src/ares/managers/squad_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,9 @@ def __init__(
self._get_position_of_main_squad(**kwargs)
),
ManagerRequestType.GET_SQUADS: lambda kwargs: (self._get_squads(**kwargs)),
ManagerRequestType.REMOVE_TAG_FROM_SQUADS: lambda kwargs: self.remove_tag(
kwargs["tag"]
),
}

self._assigned_unit_tags: set[int] = set()
Expand Down Expand Up @@ -168,6 +171,21 @@ async def update(self, _iteration: int) -> None:
squad_info["squad_object"].squad_position, f"{squad_id}"
)

def remove_tag(self, tag: int) -> None:
if tag in self._assigned_unit_tags:
found_squad: bool = False
squad_id_to_remove_from = ""
role: UnitRole = UnitRole.ATTACKING
for _role, squad_dict in self._squads_dict.items():
for squad_id, squad_info in squad_dict.items():
if tag in squad_info[self.TAGS]:
squad_id_to_remove_from = squad_id
found_squad = True
role = _role
break
if found_squad:
self._remove_unit_tag(tag, role, squad_id_to_remove_from)

def _get_squads(
self,
role: UnitRole,
Expand Down

0 comments on commit 4d4b6c6

Please sign in to comment.