-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTerran_Bot.py
131 lines (113 loc) · 5.91 KB
/
Terran_Bot.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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
import time
import math
import random
import sc2
from sc2.bot_ai import BotAI
from sc2.player import Bot, Computer
from sc2.ids.ability_id import AbilityId
from sc2.ids.unit_typeid import UnitTypeId
from sc2.constants import *
class Project_Tychus(BotAI):
async def on_step(self, iteration: int):
self.iteration = iteration
if iteration == 0:
await self.on_first_step()
#pass
if iteration % 30 == 0:
#await self.display_data()
pass
#SCV Stuff
await self.on_every_step()
# Increase Supply
await self.increase_supply()
# Build SCVs
await self.build_workers()
# Collect Gas
await self.collect_gas()
# Not sure if this works | Basic Idle work limit and worker distribution
#await self.distribute_workers()
await self.worker_control()
async def on_first_step(self):
#Worker cap adjustment to calculation
#self.worker_cap = 34
self.worker_cap = (len(self.townhalls(UnitTypeId.COMMANDCENTER))* 16) + (len(self.townhalls(UnitTypeId.COMMANDCENTER)) * 6)
self.refinery_limit = 0
#self.refinery_limit = len(self.townhalls(UnitTypeId.COMMANDCENTER).ready) * 1
self.expansion_locations_keys = list(self.expansion_locations.keys())
await self.chat_send("Project Tychus Bot")
async def on_every_step(self):
#self.zealots = self.units(ZEALOT)
#self.stalkers = self.units(STALKER)
#self.army = self.zealots + self.stalkers
#self.collectedActions = []
#self.ready_commandcenteres = self.units(COMMANDCENTER).ready
self.choose_unit()
self.ready_commandcenteres = self.townhalls(UnitTypeId.COMMANDCENTER).ready
self.ready_ccs = self.units(COMMANDCENTER).ready
self.commandcenters = self.townhalls(UnitTypeId.COMMANDCENTER).ready
hq: Unit = self.townhalls.first
#self.commandcenter = self.townhalls.same_tech(UnitTypeId.COMMANDCENTER)
#self.worker_cap = (len(self.units(COMMANDCENTER))* 16) + (len(self.units(COMMANDCENTER)) * 6)
#self.enemy_units_cost = self.get_food_cost(self.enemy_units) + self.get_food_cost(self.enemy_defense_structures)
#self.enemy_defense_structures = []
async def build_workers(self):
ideal_harvesters = 0
assigned_harvesters = 0
for commandcenter in self.ready_commandcenteres:
ideal_harvesters = ideal_harvesters + commandcenter.ideal_harvesters
assigned_harvesters = assigned_harvesters + commandcenter.assigned_harvesters
for commandcenter in self.ready_commandcenteres:
# build workers when there is a shortage at the commandcenter.
scvs = self.units(SCV)
if len(scvs) <= self.worker_cap:
if ideal_harvesters > assigned_harvesters:
if commandcenter.noqueue:
if self.supply_left <= 1 and self.already_pending(SUPPLYDEPOT):
if self.can_afford(UnitTypeId.SCV):
#await self.do(commandcenter.train(UnitTypeId.SCV))
commandcenter.train(UnitTypeId.SCV)
elif self.supply_left >= 1:
if self.can_afford(UnitTypeId.SCV):
#await self.do(commandcenter.train(UnitTypeId.SCV))
commandcenter.train(UnitTypeId.SCV)
async def increase_supply(self):
if self.can_afford(UnitTypeId.SUPPLYDEPOT):
if not self.already_pending(SUPPLYDEPOT):
if self.supply_left < self.supply_cap * 0.20:
worker_candidates = self.workers.filter(lambda worker: (worker.is_collecting or worker.is_idle) and worker.tag not in self.unit_tags_received_action)
# Worker_candidates can be empty
if worker_candidates:
map_center = self.game_info.map_center
position_towards_map_center = self.start_location.towards(map_center, distance=5)
placement_position = await self.find_placement(UnitTypeId.SUPPLYDEPOT, near=position_towards_map_center, placement_step=1)
# Placement_position can be None
if placement_position:
build_worker = worker_candidates.closest_to(placement_position)
build_worker.build(UnitTypeId.SUPPLYDEPOT, placement_position)
async def collect_gas(self):
self.refinerys = len(self.units(REFINERY)) + self.already_pending(REFINERY)
#self.refinery_limit = len(self.townhalls(UnitTypeId.COMMANDCENTER).ready) * 2
vespene_deposits = []
await self.chat_send("I GOT HERE oneeeeeeeeeeeee")
if self.refinery_limit > self.refinerys:
await self.chat_send("I GOT HERE")
for vg in self.vespene_geyser.closer_than(10, hq):
if self.can_afford(REFINERY):
vespene_deposit = random.choice(vespene_deposits)
scv = self.select_build_worker(vespene_deposit.position)
if scv:
#await self.do(scv.build(UnitTypeId.REFINERY, vespene_deposit))
await self.do(scv.build(REFINERY, vespene_deposit))
async def worker_control(self):
scvs = self.units(SCV)
allowed_idle_workers = 3
if allowed_idle_workers >= len(scvs)-3:
await self.distribute_workers()
def choose_unit(self):
self.refinery_limit = 2
self.worker_cap = 34 + (self.refinery_limit * 3)
sc2.run_game(
sc2.maps.get("AutomatonLE"),
[Bot(sc2.Race.Terran, Project_Tychus()), Computer(sc2.Race.Terran, sc2.Difficulty.Easy)],
realtime=True,
)