Matuiss2

Roach rush AI- v2.3 (24- 0 against version 1)

Jul 30th, 2018
108
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 9.68 KB | None | 0 0
  1. import sc2
  2. from sc2 import run_game, maps, Race, Difficulty
  3. from sc2.constants import HATCHERY, ZERGLING, QUEEN, LARVA, EFFECT_INJECTLARVA, \
  4. SPAWNINGPOOL, RESEARCH_ZERGLINGMETABOLICBOOST, OVERLORD, EXTRACTOR, DRONE, ROACHWARREN, ROACH, \
  5. QUEENSPAWNLARVATIMER, EVOLUTIONCHAMBER, RESEARCH_ZERGMISSILEWEAPONSLEVEL1, AbilityId, \
  6. RESEARCH_ZERGGROUNDARMORLEVEL1, HYDRALISKDEN, HYDRALISK, LAIR, RESEARCH_ZERGMISSILEWEAPONSLEVEL2,\
  7. RESEARCH_ZERGGROUNDARMORLEVEL2, PROBE, SCV,  SPINECRAWLER, UPGRADETOLAIR_LAIR, CANCEL_MORPHLAIR
  8. from sc2.player import Bot, Computer
  9.  
  10. # todo for v3 release
  11. # todo update comments
  12. # todo investigate why its behaving strange vs building when attacking
  13. # todo investigate why no hydras are being made
  14. # todo fix the expansion bug(not on me)
  15. # todo zerg_rush defense
  16. # todo its over collecting gas investigate why
  17. # todo optimize worker rush defense()
  18.  
  19.  
  20.  
  21. class ZergRoachv2(sc2.BotAI):
  22.     def __init__(self):
  23.         self.flag1 = False
  24.     async def on_step(self, iteration):
  25.         global larvas, roaches, drones, base_amount, hatcherys, \
  26.             sp, enemy_units, roach, rw, lair, base, abilitys, suplyl
  27.         larvas = self.units(LARVA)
  28.         roach = self.units(ROACH)
  29.         roaches = roach.amount
  30.         drones = self.units(DRONE).amount
  31.         hatcherys = self.townhalls
  32.         base_amount = hatcherys.amount
  33.         sp = self.units(SPAWNINGPOOL)
  34.         base = self.units(HATCHERY)
  35.         enemy_units = self.known_enemy_units.not_structure
  36.         rw = self.units(ROACHWARREN)
  37.         lair = self.units(LAIR)
  38.         abilitys = self.get_available_abilities
  39.         suplyl = self.supply_left
  40.         await self.defend_workerrush()
  41.         await self.distribute_workers()
  42.         await self.build_workers()
  43.         await self.build_overlords()
  44.         await self.build_hatchery()
  45.         await self.build_spawning_pool()
  46.         await self.upgrade_lair()
  47.         await self.build_queens_inject_larva()
  48.         await self.build_roach_warren()
  49.         await self.build_extrator()
  50.         await self.build_evochamber()
  51.         await self.build_roaches()
  52.         await self.defend_attack()
  53.         await self.armor_attack()
  54.         await self.build_hydraden()
  55.         await self.build_hydras()
  56.  
  57.     '''def seconds(self):
  58.        return self.state.game_loop * 0.725 * (1 / 16)'''
  59.     async def build_workers(self):        
  60.         if drones < 18.5 * base_amount \
  61.                 and self.can_afford(DRONE) and larvas.exists\
  62.                 and suplyl > 0 and drones < 66:
  63.             if base_amount == 3:
  64.                 if roaches > 7:
  65.                     await self.do(larvas.random.train(DRONE))
  66.             else:
  67.                 await self.do(larvas.random.train(DRONE))
  68.  
  69.     async def build_overlords(self):        
  70.         if base_amount == 1:
  71.             if suplyl < 3 and self.can_afford(OVERLORD) and larvas.exists \
  72.                     and not self.already_pending(OVERLORD):
  73.                 await self.do(larvas.random.train(OVERLORD))
  74.         elif suplyl < 8 and self.can_afford(OVERLORD) and larvas.exists:
  75.             await self.do(larvas.random.train(OVERLORD))
  76.  
  77.     async def build_hatchery(self):      
  78.         if self.can_afford(HATCHERY) and base_amount < 2\
  79.                 and not self.already_pending(HATCHERY):
  80.             if base_amount == 1:
  81.                 await self.expand_now()
  82.             '''    
  83.            elif base_amount * 8.5 < roaches:
  84.                await self.expand_now()'''
  85.  
  86.     async def build_spawning_pool(self):        
  87.         if self.can_afford(SPAWNINGPOOL) and not sp.exists and base_amount == 2:
  88.             await self.build(SPAWNINGPOOL, near=hatcherys.first.position.towards(self._game_info.map_center, 6))
  89.  
  90.     async def build_queens_inject_larva(self):        
  91.         hatcherys_random = hatcherys.random
  92.         if sp.exists:
  93.             if self.units(QUEEN).amount < base_amount and hatcherys_random.is_ready\
  94.                     and not self.already_pending(QUEEN) and hatcherys_random.noqueue\
  95.                     and sp.first.is_ready:
  96.                 if self.can_afford(QUEEN):
  97.                     await self.do(hatcherys_random.train(QUEEN))
  98.         for queen in self.units(QUEEN).idle:
  99.             selected = hatcherys.closest_to(queen.position)
  100.             if hatcherys_random.is_ready:
  101.                 if queen.energy >= 25 and not selected.has_buff(QUEENSPAWNLARVATIMER):
  102.                     await self.do(queen(EFFECT_INJECTLARVA, selected))
  103.  
  104.     async def build_roach_warren(self):        
  105.         if self.can_afford(ROACHWARREN)and not rw.exists\
  106.                 and not self.already_pending(ROACHWARREN) and sp.ready:
  107.             await self.build(ROACHWARREN, near=sp.first)
  108.  
  109.     async def build_extrator(self):        
  110.         gas = self.units(EXTRACTOR)
  111.         vgs = self.state.vespene_geyser.closer_than(10, hatcherys.ready.random)
  112.         for vg in vgs:
  113.             drone = self.select_build_worker(vg.position)
  114.             if drone is not None and self.can_afford(EXTRACTOR) and await self.can_place(EXTRACTOR, vg.position):
  115.                 if gas.amount < 5 and not self.already_pending(EXTRACTOR) \
  116.                         and base_amount >= 2 and self.can_afford(EXTRACTOR):
  117.                     if not gas.exists:
  118.                         err = await self.do(drone.build(EXTRACTOR, vg))
  119.                         if not err:
  120.                             break
  121.                     elif gas.amount <= 2 and self.units(EVOLUTIONCHAMBER).ready.exists:
  122.                         err = await self.do(drone.build(EXTRACTOR, vg))
  123.                         if not err:
  124.                             break
  125.                     elif gas.amount == 3 and roaches > 5:
  126.                         err = await self.do(drone.build(EXTRACTOR, vg))
  127.                         if not err:
  128.                             break
  129.                     elif gas.amount >= 4 and roaches > 12 and base_amount > 2:
  130.                         err = await self.do(drone.build(EXTRACTOR, vg))
  131.                         if not err:
  132.                             break
  133.  
  134.     async def build_evochamber(self):        
  135.         if self.can_afford(EVOLUTIONCHAMBER)and rw.exists \
  136.                 and self.units(EVOLUTIONCHAMBER).amount < 2\
  137.                 and not self.already_pending(EVOLUTIONCHAMBER):
  138.             await self.build(EVOLUTIONCHAMBER, near=rw.first)
  139.  
  140.     async def build_roaches(self):        
  141.         if roaches > 15:
  142.             if self.flag1 and suplyl > 1\
  143.                     and self.can_afford(ROACH) and larvas.exists\
  144.                     and rw.ready.exists:
  145.                 await self.do(larvas.random.train(ROACH))
  146.         elif suplyl > 1 and self.can_afford(ROACH) and larvas.exists\
  147.                 and rw.ready.exists:
  148.             await self.do(larvas.random.train(ROACH))
  149.  
  150.     async def armor_attack(self):        
  151.         evochambers = self.units(EVOLUTIONCHAMBER)
  152.         if evochambers.ready.idle.exists:
  153.             for evo in evochambers.ready.idle:
  154.                 abilities = await abilitys(evo)
  155.                 abilities_list = [RESEARCH_ZERGMISSILEWEAPONSLEVEL1,
  156.                                    RESEARCH_ZERGMISSILEWEAPONSLEVEL2,
  157.                                    RESEARCH_ZERGGROUNDARMORLEVEL1,
  158.                                    RESEARCH_ZERGGROUNDARMORLEVEL2]
  159.                 for ability in abilities_list:
  160.                     if ability in abilities:
  161.                         if self.can_afford(ability):
  162.                             err = await self.do(evo(ability))
  163.                             if not err:
  164.                                 break
  165.  
  166.     async def defend_attack(self):        
  167.         enemy_structures = self.known_enemy_structures
  168.         if roaches > 14:
  169.             for r in roach:
  170.                 if not enemy_structures.exists:
  171.                     err = await self.do(r.attack(self.enemy_start_locations[0]))
  172.                     if not err:
  173.                         break
  174.                 elif enemy_units.exists:
  175.                     target = enemy_units.not_flying
  176.                     if target.exists:
  177.                         err = await self.do(r.attack(target.closest_to(r.position)))
  178.                         if not err:
  179.                             break
  180.                 else:
  181.                     err = await self.do(r.attack(enemy_structures.closest_to(r.position)))
  182.                     if not err:
  183.                         break
  184.  
  185.     async def upgrade_lair(self):
  186.         if base.idle.ready.exists and sp.ready.exists and self.can_afford(
  187.                 UPGRADETOLAIR_LAIR) and not self.flag1 and roaches > 13:
  188.             self.flag1 = True
  189.             await self.do(base.ready.idle.random(UPGRADETOLAIR_LAIR))
  190.  
  191.     async def build_hydraden(self):
  192.         if self.can_afford(HYDRALISKDEN) and not self.units(HYDRALISKDEN).exists\
  193.                 and not self.already_pending(HYDRALISKDEN) and rw.ready\
  194.                 and lair.ready:
  195.             if roaches > 16:
  196.                 await self.build(HYDRALISKDEN, near=rw.first)
  197.  
  198.     async def build_hydras(self):
  199.         if suplyl > 1 and self.can_afford(HYDRALISK) and larvas.exists\
  200.                 and self.units(HYDRALISKDEN).ready.exists and \
  201.                 roaches * 2 > self.units(HYDRALISK).amount:
  202.             await self.do(larvas.random.train(HYDRALISK))
  203.  
  204.     async def defend_workerrush(self):
  205.         if enemy_units.exists:
  206.             enemy_units_close = enemy_units.closer_than(5, hatcherys.first)
  207.             if enemy_units_close.exists:
  208.                 workers = enemy_units_close.of_type([PROBE, DRONE, SCV])
  209.                 if workers.exists and base_amount < 2:
  210.                         for worker in self.workers:
  211.                             await self.do(worker.attack(enemy_units.closest_to(worker.position)))
Advertisement
Add Comment
Please, Sign In to add comment