Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix __eq__ not logical, check if a UnitTagID exists in a unit group #99

Open
wants to merge 2 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 25 additions & 1 deletion examples/distributed_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,15 @@
from sc2 import run_game, maps, Race, Difficulty
from sc2.player import Bot, Computer
from sc2.constants import *
import logging

logger = logging.getLogger(__name__)

class TerranBot(sc2.BotAI):
async def on_step(self, iteration):
await self.distribute_workers()
await self.build_supply()
await self.build_geyser()
await self.build_workers()
await self.expand()

Expand All @@ -28,8 +31,29 @@ async def build_supply(self):
if self.can_afford(UnitTypeId.SUPPLYDEPOT):
await self.build(UnitTypeId.SUPPLYDEPOT, near=cc.position.towards(self.game_info.map_center, 5))

async def build_geyser(self):
for CC in self.units(UnitTypeId.COMMANDCENTER):
# get the number of nearby refineries
nearby_refineries = []
for refinery in self.units(UnitTypeId.REFINERY):
logging.warn(CC.distance_to(refinery))
if CC.distance_to(refinery) < 10:
nearby_refineries.append(refinery)
refineries_to_build = 2 - len(nearby_refineries)
if refineries_to_build <= 0:
return

run_game(maps.get("Abyssal Reef LE"), [
# get a worker from the CC
scv = self.workers.closest_to(CC)

# get the closest geyser
target = self.state.vespene_geyser.closest_to(scv.position)
if scv.position.distance_to(target) < 25 and self.can_afford(UnitTypeId.REFINERY) and not self.already_pending(UnitTypeId.SUPPLYDEPOT):
err = await self.do(scv.build(UnitTypeId.REFINERY, target))



run_game(maps.get("AbyssalReefLE"), [
Bot(Race.Terran, TerranBot()),
Computer(Race.Protoss, Difficulty.Medium)
], realtime=False)
91 changes: 60 additions & 31 deletions sc2/bot_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,31 +137,24 @@ def is_near_to_expansion(t):

return closest

async def distribute_workers(self):
"""
Distributes workers across all the bases taken.
WARNING: This is quite slow when there are lots of workers or multiple bases.
"""

# TODO:
# OPTIMIZE: Assign idle workers smarter
# OPTIMIZE: Never use same worker mutltiple times

expansion_locations = self.expansion_locations
owned_expansions = self.owned_expansions
worker_pool = []
for idle_worker in self.workers.idle:
mf = self.state.mineral_field.closest_to(idle_worker)
await self.do(idle_worker.gather(mf))
@staticmethod
def getNearestWorker(location, workers: list):
return sorted(workers, key=lambda worker: worker.distance_to(location))[0]

for location, townhall in owned_expansions.items():
# ensure that each townhall has the right amount of workers
def check_townhalls(self, worker_pool):
for location, townhall in self.owned_expansions.items():
workers = self.workers.closer_than(20, location)
actual = townhall.assigned_harvesters
ideal = townhall.ideal_harvesters
excess = actual - ideal
if actual > ideal:
worker_pool.extend(workers.random_group_of(min(excess, len(workers))))
continue

# ensure that each geyser has the right amount of workers
def check_geysers(self, worker_pool):
for g in self.geysers:
workers = self.workers.closer_than(5, g)
actual = g.assigned_harvesters
Expand All @@ -171,35 +164,71 @@ async def distribute_workers(self):
worker_pool.extend(workers.random_group_of(min(excess, len(workers))))
continue

async def assign_geysers(self, worker_pool):
for g in self.geysers:
actual = g.assigned_harvesters
ideal = g.ideal_harvesters
deficit = ideal - actual

for x in range(0, deficit):
if worker_pool:
w = worker_pool.pop()
if len(w.orders) == 1 and w.orders[0].ability.id in [AbilityId.HARVEST_RETURN]:
await self.do(w.move(g))
await self.do(w.return_resource(queue=True))
worker = None
if self.workers.idle:
worker = self.getNearestWorker(g, self.workers.idle)
self.workers.idle.remove(worker)
elif worker_pool:
worker = self.getNearestWorker(g, worker_pool)
worker_pool.remove(worker)
if worker:
if len(worker.orders) == 1 and worker.orders[0].ability.id in [AbilityId.HARVEST_RETURN]:
await self.do(worker.move(g))
await self.do(worker.return_resource(queue=True))
else:
await self.do(w.gather(g))
await self.do(worker.gather(g))

for location, townhall in owned_expansions.items():
async def assign_townhalls(self, worker_pool):
for location, townhall in self.owned_expansions.items():
actual = townhall.assigned_harvesters
ideal = townhall.ideal_harvesters

deficit = ideal - actual
for x in range(0, deficit):
if worker_pool:
w = worker_pool.pop()
for _ in range(0, deficit):
worker = None
if self.workers.idle:
worker = self.getNearestWorker(location, self.workers.idle)
self.workers.idle.remove(worker)
elif worker_pool:
worker = self.getNearestWorker(location, worker_pool)
worker_pool.remove(worker)
if worker:
mf = self.state.mineral_field.closest_to(townhall)
if len(w.orders) == 1 and w.orders[0].ability.id in [AbilityId.HARVEST_RETURN]:
await self.do(w.move(townhall))
await self.do(w.return_resource(queue=True))
await self.do(w.gather(mf, queue=True))
if len(worker.orders) == 1 and worker.orders[0].ability.id in [AbilityId.HARVEST_RETURN]:
await self.do(worker.move(townhall))
await self.do(worker.return_resource(queue=True))
await self.do(worker.gather(mf, queue=True))
else:
await self.do(w.gather(mf))
await self.do(worker.gather(mf))

async def distribute_workers(self, geyser_first: bool = False):
"""
Distributes workers across all the bases taken.
WARNING: This is quite slow when there are lots of workers or multiple bases.
"""

# TODO:
# OPTIMIZE: Assign idle workers smarter
# OPTIMIZE: Never use same worker multiple times

worker_pool = []

self.check_townhalls(worker_pool)
self.check_geysers(worker_pool)
if geyser_first:
await self.assign_geysers(worker_pool)
await self.assign_townhalls(worker_pool)
else:
await self.assign_townhalls(worker_pool)
await self.assign_geysers(worker_pool)


@property
def owned_expansions(self):
Expand Down
3 changes: 3 additions & 0 deletions sc2/unit.py
Original file line number Diff line number Diff line change
Expand Up @@ -464,6 +464,9 @@ def __call__(self, ability, *args, **kwargs):
def __repr__(self):
return f"Unit(name={self.name !r}, tag={self.tag})"

def __eq__(self, ID):
return ID == self.type_id

class UnitOrder(object):
@classmethod
def from_proto(cls, proto, game_data):
Expand Down
6 changes: 6 additions & 0 deletions sc2/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ def __sub__(self, other: "Units") -> "Units":
units = [unit for unit in self if unit.tag not in tags]
return Units(units, self.game_data)

def __contains__(self, TypeID):
for tag in self.tags:
if TypeID == self.find_by_tag(tag):
return True
return False

@property
def amount(self) -> int:
return len(self)
Expand Down
6 changes: 6 additions & 0 deletions test/test_units.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,5 +141,11 @@ def test_owned(self):
def test_enemy(self):
self.assertEqual(self.marines.enemy, self.emptyUnitsGroup)

def test_contains(self):
self.assertTrue(UnitTypeId.MARINE in self.marines)

def test_not_contains(self):
self.assertFalse(UnitTypeId.ADEPT in self.marines)

if __name__ == "__main__":
unittest.main()