forked from Dentosal/python-sc2
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcannon_rush.py
More file actions
60 lines (49 loc) · 2.26 KB
/
cannon_rush.py
File metadata and controls
60 lines (49 loc) · 2.26 KB
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
53
54
55
56
57
58
59
60
import random
import sc2
from sc2 import Race, Difficulty
from sc2.constants import *
from sc2.player import Bot, Computer
class CannonRushBot(sc2.BotAI):
async def on_step(self, iteration):
if iteration == 0:
await self.chat_send("(probe)(pylon)(cannon)(cannon)(gg)")
if not self.units(NEXUS).exists:
for worker in self.workers:
await self.do(worker.attack(self.enemy_start_locations[0]))
return
else:
nexus = self.units(NEXUS).first
if self.workers.amount < 16 and nexus.noqueue:
if self.can_afford(PROBE):
await self.do(nexus.train(PROBE))
elif not self.units(PYLON).exists and not self.already_pending(PYLON):
if self.can_afford(PYLON):
await self.build(PYLON, near=nexus)
elif not self.units(FORGE).exists:
pylon = self.units(PYLON).ready
if pylon.exists:
if self.can_afford(FORGE):
await self.build(FORGE, near=pylon.closest_to(nexus))
elif self.units(PYLON).amount < 2:
if self.can_afford(PYLON):
pos = self.enemy_start_locations[0].towards(self.game_info.map_center, random.randrange(8, 15))
await self.build(PYLON, near=pos)
elif not self.units(PHOTONCANNON).exists:
if self.units(PYLON).ready.amount >= 2 and self.can_afford(PHOTONCANNON):
pylon = self.units(PYLON).closer_than(20, self.enemy_start_locations[0]).random
await self.build(PHOTONCANNON, near=pylon)
else:
if self.can_afford(PYLON) and self.can_afford(PHOTONCANNON): # ensure "fair" decision
for _ in range(20):
pos = self.enemy_start_locations[0].random_on_distance(random.randrange(5, 12))
building = PHOTONCANNON if self.state.psionic_matrix.covers(pos) else PYLON
r = await self.build(building, near=pos)
if not r: # success
break
def main():
sc2.run_game(sc2.maps.get("Abyssal Reef LE"), [
Bot(Race.Protoss, CannonRushBot()),
Computer(Race.Protoss, Difficulty.Medium)
], realtime=False)
if __name__ == '__main__':
main()