Skip to content

Commit

Permalink
Fix state parameters being passed
Browse files Browse the repository at this point in the history
  • Loading branch information
Reynald Pader committed Feb 1, 2018
1 parent 1620f16 commit 9c60adc
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 39 deletions.
6 changes: 3 additions & 3 deletions examples/terran_marine_rush_build_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sc2.state_conditions.conditions import all_of, supply_at_least, minerals_at_least, unit_count


def first_barracks(bot, state):
def first_barracks(bot):
return bot.units(UnitTypeId.BARRACKS).first


Expand Down Expand Up @@ -38,9 +38,9 @@ def __init__(self):
self.attack = False
self.build_order = BuildOrder(self, build_order, worker_count=16)

async def on_step(self, state, iteration):
async def on_step(self, iteration):
await self.distribute_workers()
await self.build_order.execute_build(state)
await self.build_order.execute_build()

if self.units(UnitTypeId.MARINE).amount >= 15 or self.attack:
self.attack = True
Expand Down
2 changes: 1 addition & 1 deletion examples/threebase_voidray.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from sc2.player import Bot, Computer

class ThreebaseVoidrayBot(sc2.BotAI):
def select_target(self, state):
def select_target(self):
if self.known_enemy_structures.exists:
return random.choice(self.known_enemy_structures)

Expand Down
6 changes: 3 additions & 3 deletions examples/zerg_rush_build_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@


def research(building, upgrade):
async def research_spec(bot,state):
async def research_spec(bot):
sp = bot.units(building).ready
if sp.exists and bot.can_afford(upgrade) and not bot.already_pending(upgrade):
await bot.do(sp.first(upgrade))
Expand Down Expand Up @@ -37,7 +37,7 @@ def __init__(self):

self.build_order = BuildOrder(self, build_order, worker_count=35)

async def on_step(self, state, iteration):
async def on_step(self, iteration):
await self.distribute_workers()

if self.vespene >= 100:
Expand All @@ -46,7 +46,7 @@ async def on_step(self, state, iteration):
await self.do(sp.first(AbilityId.ZERGLINGMOVEMENTSPEED))
self.mboost_started = True

await self.build_order.execute_build(state)
await self.build_order.execute_build()

for queen in self.units(UnitTypeId.QUEEN).idle:
if queen.energy >= 25: # Hard coded, since this is not (yet) available
Expand Down
2 changes: 1 addition & 1 deletion sc2/bot_ai.py
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def already_pending(self, unit_type):
return sum([o.ability == ability for w in self.workers for o in w.orders])
elif any(egg.orders[0].ability == ability for egg in self.units(EGG)):
return sum([egg.orders[0].ability == ability for egg in self.units(EGG)])
return
return 0

async def build(self, building, near, max_distance=20, unit=None, random_alternative=True, placement_step=2):
if isinstance(near, Unit):
Expand Down
14 changes: 7 additions & 7 deletions sc2/build_orders/build_order.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,17 @@ def __init__(self, bot, build, worker_count=0, auto_add_supply=True):
self.worker_count = worker_count
self.auto_add_supply = auto_add_supply

async def execute_build(self, state):
async def execute_build(self):
bot = self.bot
if bot.supply_left <= ((bot.supply_cap+50) / 50) and not bot.already_pending(bot.supply_type) \
and self.auto_add_supply:
return await add_supply().execute(bot, state)
return await add_supply().execute(bot)

for index, item in enumerate(self.build):
condition, command = item
condition = item[0] if item[0] else always_true
if condition(bot, state) and not command.is_done:
e = await command.execute(bot, state)
if condition(bot) and not command.is_done:
e = await command.execute(bot)
if command.is_done:
return e
else:
Expand All @@ -30,12 +30,12 @@ async def execute_build(self, state):

if e == ActionResult.NotEnoughFood and self.auto_add_supply \
and not bot.already_pending(bot.supply_type):
return await add_supply().execute(bot, state)
return await add_supply().execute(bot)
continue

if bot.workers.amount < self.worker_count:
if bot.race == Race.Zerg:
return await morph(race_worker[Race.Zerg]).execute(bot, state)
return await morph(race_worker[Race.Zerg]).execute(bot)
else:
return await train_unit(race_worker[bot.race], race_townhalls[self.bot.race]).execute(bot, state)
return await train_unit(race_worker[bot.race], race_townhalls[self.bot.race]).execute(bot)
return None
22 changes: 11 additions & 11 deletions sc2/build_orders/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ def __init__(self, action, repeatable=False, priority=False):
self.is_repeatable = repeatable
self.is_priority = priority

async def execute(self, bot, state):
e = await self.action(bot, state)
async def execute(self, bot):
e = await self.action(bot)
if not e and not self.is_repeatable:
self.is_done = True

Expand All @@ -23,7 +23,7 @@ def allow_repeat(self):


def expand(prioritize=False, repeatable=True):
async def do_expand(bot, state):
async def do_expand(bot):
building = bot.basic_townhall_type
can_afford = bot.can_afford(building)
if can_afford:
Expand All @@ -35,7 +35,7 @@ async def do_expand(bot, state):


def train_unit(unit, on_building, prioritize=False, repeatable=False):
async def do_train(bot, state):
async def do_train(bot):
buildings = bot.units(on_building).ready.noqueue
if buildings.exists:
selected = buildings.first
Expand All @@ -52,7 +52,7 @@ async def do_train(bot, state):


def morph(unit, prioritize=False, repeatable=False):
async def do_morph(bot, state):
async def do_morph(bot):
larvae = bot.units(UnitTypeId.LARVA)
if larvae.exists:
selected = larvae.first
Expand All @@ -69,7 +69,7 @@ async def do_morph(bot, state):


def construct(building, placement=None, prioritize=True, repeatable=False):
async def do_build(bot, state):
async def do_build(bot):

if not placement:
location = bot.townhalls.first.position.towards(bot.game_info.map_center, 5)
Expand All @@ -87,28 +87,28 @@ async def do_build(bot, state):


def add_supply(prioritize=True, repeatable=False):
async def supply_spec(bot, state):
async def supply_spec(bot):
can_afford = bot.can_afford(bot.supply_type)
if can_afford:
if bot.race == Race.Zerg:
return await morph(bot.supply_type).execute(bot, state)
return await morph(bot.supply_type).execute(bot)
else:
return await construct(bot.supply_type).execute(bot, state)
return await construct(bot.supply_type).execute(bot)
else:
return can_afford.action_result

return Command(supply_spec, priority=prioritize, repeatable=repeatable)


def add_gas(prioritize=True, repeatable=False):
async def do_add_gas(bot, state):
async def do_add_gas(bot):
can_afford = bot.can_afford(bot.geyser_type)
if not can_afford:
return can_afford.action_result

owned_expansions = bot.owned_expansions
for location, th in owned_expansions.items():
vgs = state.vespene_geyser.closer_than(20.0, th)
vgs = bot.state.vespene_geyser.closer_than(20.0, th)
for vg in vgs:
worker = bot.select_build_worker(vg.position)
if worker is None:
Expand Down
26 changes: 13 additions & 13 deletions sc2/state_conditions/conditions.py
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
def all_of(*args):
def condition(bot, state):
return all(map(lambda a: a(bot, state), args))
def condition(bot):
return all(map(lambda a: a(bot), args))

return condition


def any_of(*args):
def condition(bot, state):
return all(any(lambda a: a(bot, state), args))
def condition(bot):
return all(any(lambda a: a(bot), args))

return condition


def always_true(bot, state):
def always_true(bot):
return True


def supply_at_least(s):
def condition(bot, state):
def condition(bot):
return bot.supply_used >= s

return condition


def gas_at_least(s):
def condition(bot, state):
def condition(bot):
return bot.vespene >= s

return condition


def gas_less_than(s):
def condition(bot, state):
def condition(bot):
return bot.vespene < s

return condition


def minerals_at_least(s):
def condition(bot, state):
def condition(bot):
return bot.minerals >= s

return condition


def minerals_less_than(s):
def condition(bot, state):
def condition(bot):
return bot.minerals < s

return condition


def unit_count(unit, n, include_pending=False):
def condition(bot, state):
def condition(bot):
actual_amount = bot.units(unit).amount
if include_pending:
actual_amount += bot.already_pending(unit)
Expand All @@ -62,7 +62,7 @@ def condition(bot, state):


def unit_count_at_least(unit, n, include_pending=False):
def condition(bot, state):
def condition(bot):
actual_amount = bot.units(unit).amount
if include_pending:
actual_amount += bot.already_pending(unit)
Expand All @@ -72,7 +72,7 @@ def condition(bot, state):


def unit_count_less_than(unit, n, include_pending=False):
def condition(bot, state):
def condition(bot):
actual_amount = bot.units(unit).amount
if include_pending:
actual_amount += bot.already_pending(unit)
Expand Down

0 comments on commit 9c60adc

Please sign in to comment.