Skip to content

Commit

Permalink
Created group behaviors
Browse files Browse the repository at this point in the history
  • Loading branch information
Drekken committed Apr 23, 2024
1 parent 12892b6 commit 0cc1648
Showing 1 changed file with 60 additions and 6 deletions.
66 changes: 60 additions & 6 deletions bot/main.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,80 @@
from typing import Optional

from ares import AresBot
from ares.consts import UnitRole, ALL_STRUCTURES
from ares.behaviors.combat import CombatManeuver
from ares.behaviors.combat.group import AMoveGroup

from sc2.ids.unit_typeid import UnitTypeId
from sc2.unit import Unit
from sc2.units import Units
from sc2.position import Point2

class MyBot(AresBot):
class AnglerBot(AresBot):
def __init__(self, game_step_override: Optional[int] = None):
"""Initiate custom bot
Parameters
----------
game_step_override :
If provided, set the game_step to this value regardless of how it was
specified elsewhere
If proied elsewherevided, set the game_step to this value regardless of how it was
specif
"""
super().__init__(game_step_override)

self._assigned_main_army: bool = False

async def on_step(self, iteration: int) -> None:
await super(MyBot, self).on_step(iteration)
await super(AnglerBot, self).on_step(iteration)
#retrieve all attacking units
attackers: Units = self.mediator.get_units_by_role(UnitRole.ATTACKING)

#retrieve main army if one has been assigned
main_army: Units = self.mediator.get_units_by_role(role=UnitRole.MAIN_ARMY, unit_type=UnitTypeId.ZEALOT)
print("Main Army assigned")

self.control_main_army(
main_army=main_army,
target=self.enemy_start_locations[0]
)

# at 10 seconds assign all zealots to MAIN_ARMY role
# This will remove them from the ATTACKING automatically
if not self._assigned_main_army and self.time > 2:
self._assigned_main_army = True
zealots: list[Unit] = [
u for u in attackers if u.type_id == UnitTypeId.ZEALOT
]
for zealot in zealots:
self.mediator.assign_role(tag=zealot.tag, role=UnitRole.MAIN_ARMY
)
def control_main_army(self, main_army: Units, target: Point2) -> None:
#declare a new group maneuver
group_maneuver: CombatManeuver = CombatManeuver()
#add group behaviors
group_maneuver.add(
AMoveGroup(
group= main_army,
group_tags={r.tag for r in main_army},
target=target
)
)
self.register_behavior(group_maneuver)

# step logic here ...
pass
async def on_start(self, unit: Unit) -> None:
#When a unit is created,
#assign it to ATTACKING role
await super(AnglerBot, self).on_start(unit)
type_id: UnitTypeId = unit.type_id
# don't assign structures
if type_id in ALL_STRUCTURES:
return

#assign all other units to ATTACKING role by default
self.mediator.assign_role(tag=unit.tag, role=UnitRole.ATTACKING)
print("Unit assigned to ATTACKING role")


"""
Can use `python-sc2` hooks as usual, but make a call the inherited method in the superclass
Examples:
Expand Down

0 comments on commit 0cc1648

Please sign in to comment.