-
Notifications
You must be signed in to change notification settings - Fork 159
/
worker_split.py
52 lines (40 loc) · 1.61 KB
/
worker_split.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
"""
This bot is just to demonstrate that you can do worker split
at game start without having to use 'synchronous_do()'.
This is especially important when your bot runs on realtime=True and
you want your bot to be reliable against Human opponents.
"""
import asyncio
from loguru import logger
from sc2 import maps
from sc2.bot_ai import BotAI
from sc2.data import Difficulty, Race
from sc2.main import run_game
from sc2.player import Bot, Computer
from sc2.units import Units
class WorkerSplitBot(BotAI):
async def on_before_start(self):
""" This function is run before the expansion locations and ramps are calculated. These calculations can take up to a second, depending on the CPU. """
mf: Units = self.mineral_field
for w in self.workers:
w.gather(mf.closest_to(w))
await self._do_actions(self.actions)
self.actions.clear()
await asyncio.sleep(3)
async def on_start(self):
""" This function is run after the expansion locations and ramps are calculated. """
async def on_step(self, iteration):
if iteration % 10 == 0:
await asyncio.sleep(3)
# In realtime=False, this should print "8*x" and "x" if
# self.client.game_step is set to 8 (default value)
# But if your bot takes too long, it will skip game loops.
logger.info(f"Bot's game loop is {self.state.game_loop} and iteration {iteration}")
def main():
run_game(
maps.get("AcropolisLE"),
[Bot(Race.Zerg, WorkerSplitBot()), Computer(Race.Terran, Difficulty.Medium)],
realtime=True,
)
if __name__ == "__main__":
main()