Skip to content

Commit

Permalink
Merge pull request #5 from Dentosal/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
tweakimp authored Feb 27, 2019
2 parents 5bc6d95 + 04de144 commit e7f39c9
Show file tree
Hide file tree
Showing 15 changed files with 47 additions and 36 deletions.
2 changes: 1 addition & 1 deletion examples/distributed_workers.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ async def on_step(self, iteration):
await self.expand()

async def build_workers(self):
for cc in self.units(UnitTypeId.COMMANDCENTER).ready.noqueue:
for cc in self.units(UnitTypeId.COMMANDCENTER).ready.idle:
if self.can_afford(UnitTypeId.SCV):
await self.do(cc.train(UnitTypeId.SCV))

Expand Down
2 changes: 1 addition & 1 deletion examples/protoss/cannon_rush.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def on_step(self, iteration):
else:
nexus = self.units(NEXUS).first

if self.workers.amount < 16 and nexus.noqueue:
if self.workers.amount < 16 and nexus.is_idle:
if self.can_afford(PROBE):
await self.do(nexus.train(PROBE))

Expand Down
4 changes: 2 additions & 2 deletions examples/protoss/threebase_voidray.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ async def on_step(self, iteration):
await self.build(PYLON, near=nexus)
return

if self.workers.amount < self.units(NEXUS).amount*15 and nexus.noqueue:
if self.workers.amount < self.units(NEXUS).amount*15 and nexus.is_idle:
if self.can_afford(PROBE):
await self.do(nexus.train(PROBE))

Expand Down Expand Up @@ -89,7 +89,7 @@ async def on_step(self, iteration):
if self.can_afford(STARGATE):
await self.build(STARGATE, near=pylon)

for sg in self.units(STARGATE).ready.noqueue:
for sg in self.units(STARGATE).ready.idle:
if self.can_afford(VOIDRAY):
await self.do(sg.train(VOIDRAY))

Expand Down
2 changes: 1 addition & 1 deletion examples/protoss/warpgate_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ async def on_step(self, iteration):
await self.build(PYLON, near=nexus)
return

if self.workers.amount < self.units(NEXUS).amount*22 and nexus.noqueue:
if self.workers.amount < self.units(NEXUS).amount*22 and nexus.is_idle:
if self.can_afford(PROBE):
await self.do(nexus.train(PROBE))

Expand Down
4 changes: 2 additions & 2 deletions examples/terran/cyclone_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ async def on_step(self, iteration):
for unit in forces.idle:
await self.do(unit.attack(target))

if self.can_afford(SCV) and self.workers.amount < 22 and cc.noqueue:
if self.can_afford(SCV) and self.workers.amount < 22 and cc.is_idle:
await self.do(cc.train(SCV))

elif self.supply_left < 3:
Expand Down Expand Up @@ -74,7 +74,7 @@ async def on_step(self, iteration):
p = cc.position.towards_with_random_angle(self.game_info.map_center, 16)
await self.build(FACTORY, near=p)

for factory in self.units(FACTORY).ready.noqueue:
for factory in self.units(FACTORY).ready.idle:
# Reactor allows us to build two at a time
if self.can_afford(CYCLONE):
await self.do(factory.train(CYCLONE))
Expand Down
4 changes: 2 additions & 2 deletions examples/terran/onebase_battlecruiser.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ async def on_step(self, iteration):
for unit in forces.idle:
await self.do(unit.attack(target))

if self.can_afford(SCV) and self.workers.amount < 22 and cc.noqueue:
if self.can_afford(SCV) and self.workers.amount < 22 and cc.is_idle:
await self.do(cc.train(SCV))

if self.units(FUSIONCORE).exists and self.can_afford(BATTLECRUISER):
for sp in self.units(STARPORT):
if sp.has_add_on and sp.noqueue:
if sp.has_add_on and sp.is_idle:
if not self.can_afford(BATTLECRUISER):
break
await self.do(sp.train(BATTLECRUISER))
Expand Down
4 changes: 2 additions & 2 deletions examples/terran/proxy_rax.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ async def on_step(self, iteration):
cg = ControlGroup(self.units(MARINE).idle)
self.attack_groups.add(cg)

if self.can_afford(SCV) and self.workers.amount < 16 and cc.noqueue:
if self.can_afford(SCV) and self.workers.amount < 16 and cc.is_idle:
await self.do(cc.train(SCV))

elif self.supply_left < (2 if self.units(BARRACKS).amount < 3 else 4):
Expand All @@ -36,7 +36,7 @@ async def on_step(self, iteration):
p = self.game_info.map_center.towards(self.enemy_start_locations[0], 25)
await self.build(BARRACKS, near=p)

for rax in self.units(BARRACKS).ready.noqueue:
for rax in self.units(BARRACKS).ready.idle:
if not self.can_afford(MARINE):
break
await self.do(rax.train(MARINE))
Expand Down
2 changes: 1 addition & 1 deletion examples/terran/ramp_wall.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ async def on_step(self, iteration):
else:
cc = cc.first

if self.can_afford(SCV) and self.workers.amount < 16 and cc.noqueue:
if self.can_afford(SCV) and self.workers.amount < 16 and cc.is_idle:
await self.do(cc.train(SCV))


Expand Down
4 changes: 2 additions & 2 deletions examples/zerg/hydralisk_push.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ async def on_step(self, iteration):
await self.build(SPAWNINGPOOL, near=hq)

if self.units(SPAWNINGPOOL).ready.exists:
if not self.units(LAIR).exists and hq.noqueue:
if not self.units(LAIR).exists and hq.is_idle:
if self.can_afford(LAIR):
await self.do(hq.build(LAIR))

Expand Down Expand Up @@ -80,7 +80,7 @@ async def on_step(self, iteration):
await self.do(w.random.gather(a))

if self.units(SPAWNINGPOOL).ready.exists:
if not self.units(QUEEN).exists and hq.is_ready and hq.noqueue:
if not self.units(QUEEN).exists and hq.is_ready and hq.is_idle:
if self.can_afford(QUEEN):
await self.do(hq.train(QUEEN))

Expand Down
8 changes: 4 additions & 4 deletions examples/zerg/onebase_broodlord.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ async def on_step(self, iteration):
await self.build(SPAWNINGPOOL, near=hq)

if self.units(SPAWNINGPOOL).ready.exists:
if not self.units(LAIR).exists and not self.units(HIVE).exists and hq.noqueue:
if not self.units(LAIR).exists and not self.units(HIVE).exists and hq.is_idle:
if self.can_afford(LAIR):
await self.do(hq.build(LAIR))

Expand All @@ -70,15 +70,15 @@ async def on_step(self, iteration):
if self.can_afford(SPIRE):
await self.build(SPIRE, near=hq)

if self.units(INFESTATIONPIT).ready.exists and not self.units(HIVE).exists and hq.noqueue:
if self.units(INFESTATIONPIT).ready.exists and not self.units(HIVE).exists and hq.is_idle:
if self.can_afford(HIVE):
await self.do(hq.build(HIVE))

if self.units(HIVE).ready.exists:
spires = self.units(SPIRE).ready
if spires.exists:
spire = spires.random
if self.can_afford(GREATERSPIRE) and spire.noqueue:
if self.can_afford(GREATERSPIRE) and spire.is_idle:
await self.do(spire.build(GREATERSPIRE))

if self.units(EXTRACTOR).amount < 2 and not self.already_pending(EXTRACTOR):
Expand All @@ -100,7 +100,7 @@ async def on_step(self, iteration):
await self.do(w.random.gather(a))

if self.units(SPAWNINGPOOL).ready.exists:
if not self.units(QUEEN).exists and hq.is_ready and hq.noqueue:
if not self.units(QUEEN).exists and hq.is_ready and hq.is_idle:
if self.can_afford(QUEEN):
await self.do(hq.train(QUEEN))

Expand Down
23 changes: 10 additions & 13 deletions sc2/bot_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -385,19 +385,16 @@ async def can_cast(
return True
return False

def select_build_worker(self, pos: Union[Unit, Point2, Point3], force: bool = False) -> Optional[Unit]:
"""Select a worker to build a bulding with."""

workers = self.workers.closer_than(20, pos) or self.workers
for worker in workers.prefer_close_to(pos).prefer_idle:
if (
not worker.orders
or len(worker.orders) == 1
and worker.orders[0].ability.id in {AbilityId.MOVE, AbilityId.HARVEST_GATHER, AbilityId.HARVEST_RETURN}
):
return worker

return workers.random if force else None
def select_build_worker(self, pos: Union[Unit, Point2, Point3], force: bool=False) -> Optional[Unit]:
"""Select a worker to build a building with."""
workers = self.workers.filter(lambda w: (w.is_gathering or w.is_idle) and w.distance_to(pos) < 20) or self.workers
if workers:
for worker in workers.sorted_by_distance_to(pos).prefer_idle:
if not worker.orders or len(worker.orders) == 1 and worker.orders[0].ability.id in {AbilityId.MOVE,
AbilityId.HARVEST_GATHER}:
return worker

return workers.random if force else None

async def can_place(self, building: Union[AbilityData, AbilityId, UnitTypeId], position: Point2) -> bool:
"""Tests if a building can be placed in the given location."""
Expand Down
6 changes: 6 additions & 0 deletions sc2/game_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,12 @@ def cost(self) -> "Cost":

class UnitTypeData:
def __init__(self, game_data, proto):
# The ability_id for lurkers is
# LURKERASPECTMPFROMHYDRALISKBURROWED_LURKERMPFROMHYDRALISKBURROWED
# instead of the correct MORPH_LURKER.
if proto.unit_id == UnitTypeId.LURKERMP.value:
proto.ability_id = AbilityId.MORPH_LURKER.value

self._game_data = game_data
self._proto = proto

Expand Down
7 changes: 5 additions & 2 deletions sc2/unit.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Any, Dict, List, Optional, Set, Tuple, Union # mypy type checking
import warnings

from . import unit_command
from .cache import property_immutable_cache, property_mutable_cache
Expand All @@ -8,6 +9,8 @@
from .ids.unit_typeid import UnitTypeId
from .position import Point2, Point3

warnings.simplefilter('once')


class UnitGameData:
""" Populated by sc2/main.py on game launch.
Expand Down Expand Up @@ -500,8 +503,8 @@ def order_target(self) -> Optional[Union[int, Point2]]:
@property_immutable_cache
def noqueue(self) -> bool:
""" Checks if the unit is idle. """
# TODO deprecate in favor of .is_idle?
return not self.orders
warnings.warn("noqueue will be removed soon, please use is_idle instead", DeprecationWarning, stacklevel=2)
return self.is_idle

@property_immutable_cache
def is_idle(self) -> bool:
Expand Down
9 changes: 7 additions & 2 deletions sc2/units.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import logging
import random
import warnings
from .unit import Unit
from itertools import chain
from typing import Any, Dict, Iterable, List, Optional, Set, Tuple, Union

Expand All @@ -9,6 +11,8 @@

logger = logging.getLogger(__name__)

warnings.simplefilter('once')


class Units(list):
"""A collection of Unit objects. Makes it easy to select units by selectors."""
Expand Down Expand Up @@ -284,7 +288,8 @@ def not_ready(self) -> "Units":

@property
def noqueue(self) -> "Units":
return self.filter(lambda unit: unit.noqueue)
warnings.warn("noqueue will be removed soon, please use idle instead", DeprecationWarning, stacklevel=2)
return self.idle

@property
def idle(self) -> "Units":
Expand Down Expand Up @@ -343,7 +348,7 @@ def prefer_idle(self) -> "Units":
return self.sorted(lambda unit: unit.is_idle, reverse=True)

def prefer_close_to(self, p: Union[Unit, Point2, Point3]) -> "Units":
# TODO redundant?
warnings.warn("prefer_close_to will be removed soon, please use sorted_by_distance_to instead", DeprecationWarning, stacklevel=2)
return self.sorted_by_distance_to(p)


Expand Down
2 changes: 1 addition & 1 deletion test/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
# For live version
FROM burnysc2/python-sc2-docker
FROM burnysc2/python-sc2-docker:1.1

# Copy files from the current commit (the python-sc2 folder) to root
ADD . /root/python-sc2
Expand Down

0 comments on commit e7f39c9

Please sign in to comment.