Skip to content

Commit

Permalink
Update dependencies and paths
Browse files Browse the repository at this point in the history
  • Loading branch information
Drekken committed Dec 20, 2023
1 parent 723070a commit 01f0889
Show file tree
Hide file tree
Showing 53 changed files with 17,975 additions and 3 deletions.
3 changes: 3 additions & 0 deletions bot/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
from typing import Optional
from ares.consts import ALL_STRUCTURES


from ares import AresBot
from ares.behaviors.combat_manager import CombatManuever
from ares.behaviors.combat.individual import (
Expand Down
84 changes: 82 additions & 2 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ ares-sc2 = { path = "ares-sc2", develop = false }
python = ">=3.11, <3.12"
black = "^23.1.0"
isort = "^5.12.0"
sc2 = "^0.11.2"

[build-system]
requires = ["poetry-core"]
Expand Down
1 change: 0 additions & 1 deletion python-sc2
Submodule python-sc2 deleted from f175e8
12 changes: 12 additions & 0 deletions sc2/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from pathlib import Path


def is_submodule(path):
if path.is_file():
return path.suffix == ".py" and path.stem != "__init__"
if path.is_dir():
return (path / "__init__.py").exists()
return False


__all__ = [p.stem for p in Path(__file__).parent.iterdir() if is_submodule(p)]
88 changes: 88 additions & 0 deletions sc2/action.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from __future__ import annotations

from itertools import groupby
from typing import TYPE_CHECKING, Union

from s2clientprotocol import raw_pb2 as raw_pb

from sc2.position import Point2
from sc2.unit import Unit

if TYPE_CHECKING:
from sc2.ids.ability_id import AbilityId
from sc2.unit_command import UnitCommand


# pylint: disable=R0912
def combine_actions(action_iter):
"""
Example input:
[
# Each entry in the list is a unit command, with an ability, unit, target, and queue=boolean
UnitCommand(AbilityId.TRAINQUEEN_QUEEN, Unit(name='Hive', tag=4353687554), None, False),
UnitCommand(AbilityId.TRAINQUEEN_QUEEN, Unit(name='Lair', tag=4359979012), None, False),
UnitCommand(AbilityId.TRAINQUEEN_QUEEN, Unit(name='Hatchery', tag=4359454723), None, False),
]
"""
for key, items in groupby(action_iter, key=lambda a: a.combining_tuple):
ability: AbilityId
target: Union[None, Point2, Unit]
queue: bool
# See constants.py for combineable abilities
combineable: bool
ability, target, queue, combineable = key

if combineable:
# Combine actions with no target, e.g. lift, burrowup, burrowdown, siege, unsiege, uproot spines
cmd = raw_pb.ActionRawUnitCommand(
ability_id=ability.value, unit_tags={u.unit.tag
for u in items}, queue_command=queue
)
# Combine actions with target point, e.g. attack_move or move commands on a position
if isinstance(target, Point2):
cmd.target_world_space_pos.x = target.x
cmd.target_world_space_pos.y = target.y
# Combine actions with target unit, e.g. attack commands directly on a unit
elif isinstance(target, Unit):
cmd.target_unit_tag = target.tag
elif target is not None:
raise RuntimeError(f"Must target a unit, point or None, found '{target !r}'")

yield raw_pb.ActionRaw(unit_command=cmd)

else:
"""
Return one action for each unit; this is required for certain commands that would otherwise be grouped, and only executed once
Examples:
Select 3 hatcheries, build a queen with each hatch - the grouping function would group these unit tags and only issue one train command once to all 3 unit tags - resulting in one total train command
I imagine the same thing would happen to certain other abilities: Battlecruiser yamato on same target, queen transfuse on same target, ghost snipe on same target, all build commands with the same unit type and also all morphs (zergling to banelings)
However, other abilities can and should be grouped, see constants.py 'COMBINEABLE_ABILITIES'
"""
u: UnitCommand
if target is None:
for u in items:
cmd = raw_pb.ActionRawUnitCommand(
ability_id=ability.value, unit_tags={u.unit.tag}, queue_command=queue
)
yield raw_pb.ActionRaw(unit_command=cmd)
elif isinstance(target, Point2):
for u in items:
cmd = raw_pb.ActionRawUnitCommand(
ability_id=ability.value,
unit_tags={u.unit.tag},
queue_command=queue,
target_world_space_pos=target.as_Point2D,
)
yield raw_pb.ActionRaw(unit_command=cmd)

elif isinstance(target, Unit):
for u in items:
cmd = raw_pb.ActionRawUnitCommand(
ability_id=ability.value,
unit_tags={u.unit.tag},
queue_command=queue,
target_unit_tag=target.tag,
)
yield raw_pb.ActionRaw(unit_command=cmd)
else:
raise RuntimeError(f"Must target a unit, point or None, found '{target !r}'")
Loading

0 comments on commit 01f0889

Please sign in to comment.