Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sc2
- from sc2 import run_game, maps, Race, Difficulty
- from sc2.constants import HATCHERY, ZERGLING, QUEEN, LARVA, EFFECT_INJECTLARVA, \
- SPAWNINGPOOL, RESEARCH_ZERGLINGMETABOLICBOOST, OVERLORD, EXTRACTOR, DRONE, ROACHWARREN, ROACH, \
- QUEENSPAWNLARVATIMER, EVOLUTIONCHAMBER, RESEARCH_ZERGMISSILEWEAPONSLEVEL1, AbilityId, \
- RESEARCH_ZERGGROUNDARMORLEVEL1, HYDRALISKDEN, HYDRALISK, LAIR, RESEARCH_ZERGMISSILEWEAPONSLEVEL2,\
- RESEARCH_ZERGGROUNDARMORLEVEL2, PROBE, SCV, SPINECRAWLER, UPGRADETOLAIR_LAIR, CANCEL_MORPHLAIR
- from sc2.player import Bot, Computer
- # todo for v3 release
- # todo update comments
- # todo investigate why its behaving strange vs building when attacking
- # todo investigate why no hydras are being made
- # todo fix the expansion bug(not on me)
- # todo zerg_rush defense
- # todo its over collecting gas investigate why
- # todo optimize worker rush defense()
- class ZergRoachv2(sc2.BotAI):
- def __init__(self):
- self.flag1 = False
- async def on_step(self, iteration):
- global larvas, roaches, drones, base_amount, hatcherys, \
- sp, enemy_units, roach, rw, lair, base, abilitys, suplyl
- larvas = self.units(LARVA)
- roach = self.units(ROACH)
- roaches = roach.amount
- drones = self.units(DRONE).amount
- hatcherys = self.townhalls
- base_amount = hatcherys.amount
- sp = self.units(SPAWNINGPOOL)
- base = self.units(HATCHERY)
- enemy_units = self.known_enemy_units.not_structure
- rw = self.units(ROACHWARREN)
- lair = self.units(LAIR)
- abilitys = self.get_available_abilities
- suplyl = self.supply_left
- await self.defend_workerrush()
- await self.distribute_workers()
- await self.build_workers()
- await self.build_overlords()
- await self.build_hatchery()
- await self.build_spawning_pool()
- await self.upgrade_lair()
- await self.build_queens_inject_larva()
- await self.build_roach_warren()
- await self.build_extrator()
- await self.build_evochamber()
- await self.build_roaches()
- await self.defend_attack()
- await self.armor_attack()
- await self.build_hydraden()
- await self.build_hydras()
- '''def seconds(self):
- return self.state.game_loop * 0.725 * (1 / 16)'''
- async def build_workers(self):
- if drones < 18.5 * base_amount \
- and self.can_afford(DRONE) and larvas.exists\
- and suplyl > 0 and drones < 66:
- if base_amount == 3:
- if roaches > 7:
- await self.do(larvas.random.train(DRONE))
- else:
- await self.do(larvas.random.train(DRONE))
- async def build_overlords(self):
- if base_amount == 1:
- if suplyl < 3 and self.can_afford(OVERLORD) and larvas.exists \
- and not self.already_pending(OVERLORD):
- await self.do(larvas.random.train(OVERLORD))
- elif suplyl < 8 and self.can_afford(OVERLORD) and larvas.exists:
- await self.do(larvas.random.train(OVERLORD))
- async def build_hatchery(self):
- if self.can_afford(HATCHERY) and base_amount < 2\
- and not self.already_pending(HATCHERY):
- if base_amount == 1:
- await self.expand_now()
- '''
- elif base_amount * 8.5 < roaches:
- await self.expand_now()'''
- async def build_spawning_pool(self):
- if self.can_afford(SPAWNINGPOOL) and not sp.exists and base_amount == 2:
- await self.build(SPAWNINGPOOL, near=hatcherys.first.position.towards(self._game_info.map_center, 6))
- async def build_queens_inject_larva(self):
- hatcherys_random = hatcherys.random
- if sp.exists:
- if self.units(QUEEN).amount < base_amount and hatcherys_random.is_ready\
- and not self.already_pending(QUEEN) and hatcherys_random.noqueue\
- and sp.first.is_ready:
- if self.can_afford(QUEEN):
- await self.do(hatcherys_random.train(QUEEN))
- for queen in self.units(QUEEN).idle:
- selected = hatcherys.closest_to(queen.position)
- if hatcherys_random.is_ready:
- if queen.energy >= 25 and not selected.has_buff(QUEENSPAWNLARVATIMER):
- await self.do(queen(EFFECT_INJECTLARVA, selected))
- async def build_roach_warren(self):
- if self.can_afford(ROACHWARREN)and not rw.exists\
- and not self.already_pending(ROACHWARREN) and sp.ready:
- await self.build(ROACHWARREN, near=sp.first)
- async def build_extrator(self):
- gas = self.units(EXTRACTOR)
- vgs = self.state.vespene_geyser.closer_than(10, hatcherys.ready.random)
- for vg in vgs:
- drone = self.select_build_worker(vg.position)
- if drone is not None and self.can_afford(EXTRACTOR) and await self.can_place(EXTRACTOR, vg.position):
- if gas.amount < 5 and not self.already_pending(EXTRACTOR) \
- and base_amount >= 2 and self.can_afford(EXTRACTOR):
- if not gas.exists:
- err = await self.do(drone.build(EXTRACTOR, vg))
- if not err:
- break
- elif gas.amount <= 2 and self.units(EVOLUTIONCHAMBER).ready.exists:
- err = await self.do(drone.build(EXTRACTOR, vg))
- if not err:
- break
- elif gas.amount == 3 and roaches > 5:
- err = await self.do(drone.build(EXTRACTOR, vg))
- if not err:
- break
- elif gas.amount >= 4 and roaches > 12 and base_amount > 2:
- err = await self.do(drone.build(EXTRACTOR, vg))
- if not err:
- break
- async def build_evochamber(self):
- if self.can_afford(EVOLUTIONCHAMBER)and rw.exists \
- and self.units(EVOLUTIONCHAMBER).amount < 2\
- and not self.already_pending(EVOLUTIONCHAMBER):
- await self.build(EVOLUTIONCHAMBER, near=rw.first)
- async def build_roaches(self):
- if roaches > 15:
- if self.flag1 and suplyl > 1\
- and self.can_afford(ROACH) and larvas.exists\
- and rw.ready.exists:
- await self.do(larvas.random.train(ROACH))
- elif suplyl > 1 and self.can_afford(ROACH) and larvas.exists\
- and rw.ready.exists:
- await self.do(larvas.random.train(ROACH))
- async def armor_attack(self):
- evochambers = self.units(EVOLUTIONCHAMBER)
- if evochambers.ready.idle.exists:
- for evo in evochambers.ready.idle:
- abilities = await abilitys(evo)
- abilities_list = [RESEARCH_ZERGMISSILEWEAPONSLEVEL1,
- RESEARCH_ZERGMISSILEWEAPONSLEVEL2,
- RESEARCH_ZERGGROUNDARMORLEVEL1,
- RESEARCH_ZERGGROUNDARMORLEVEL2]
- for ability in abilities_list:
- if ability in abilities:
- if self.can_afford(ability):
- err = await self.do(evo(ability))
- if not err:
- break
- async def defend_attack(self):
- enemy_structures = self.known_enemy_structures
- if roaches > 14:
- for r in roach:
- if not enemy_structures.exists:
- err = await self.do(r.attack(self.enemy_start_locations[0]))
- if not err:
- break
- elif enemy_units.exists:
- target = enemy_units.not_flying
- if target.exists:
- err = await self.do(r.attack(target.closest_to(r.position)))
- if not err:
- break
- else:
- err = await self.do(r.attack(enemy_structures.closest_to(r.position)))
- if not err:
- break
- async def upgrade_lair(self):
- if base.idle.ready.exists and sp.ready.exists and self.can_afford(
- UPGRADETOLAIR_LAIR) and not self.flag1 and roaches > 13:
- self.flag1 = True
- await self.do(base.ready.idle.random(UPGRADETOLAIR_LAIR))
- async def build_hydraden(self):
- if self.can_afford(HYDRALISKDEN) and not self.units(HYDRALISKDEN).exists\
- and not self.already_pending(HYDRALISKDEN) and rw.ready\
- and lair.ready:
- if roaches > 16:
- await self.build(HYDRALISKDEN, near=rw.first)
- async def build_hydras(self):
- if suplyl > 1 and self.can_afford(HYDRALISK) and larvas.exists\
- and self.units(HYDRALISKDEN).ready.exists and \
- roaches * 2 > self.units(HYDRALISK).amount:
- await self.do(larvas.random.train(HYDRALISK))
- async def defend_workerrush(self):
- if enemy_units.exists:
- enemy_units_close = enemy_units.closer_than(5, hatcherys.first)
- if enemy_units_close.exists:
- workers = enemy_units_close.of_type([PROBE, DRONE, SCV])
- if workers.exists and base_amount < 2:
- for worker in self.workers:
- await self.do(worker.attack(enemy_units.closest_to(worker.position)))
Advertisement
Add Comment
Please, Sign In to add comment