Matuiss2

Greddy_bot v1(Beats roach roach v2 68 - 32)

Aug 4th, 2018
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 16.74 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.     ADEPTPHASESHIFT, DISRUPTORPHASED, RESEARCH_TUNNELINGCLAWS, RESEARCH_GLIALREGENERATION, \
  9.     BURROWDOWN_ROACH, BURROWUP_ROACH, EGG
  10. from sc2.player import Bot, Computer
  11.  
  12. # todo for v4 release
  13. # todo zerg rush defense
  14. # todo work defense have much better performance now but still not perfect usage wise
  15. # todo general micro
  16. # todo detection, high ground spotter
  17. # todo figure out a way to handle base loss
  18. # todo make the attacks periodic
  19. # todo make burrow micro work
  20. # todo attacking logic just find close buildings
  21.  
  22.  
  23. class ZergRoachv3(sc2.BotAI):
  24.     def __init__(self):
  25.         self.flag1 = False
  26.         self.flag2 = False
  27.  
  28.     async def on_step(self, iteration):
  29.         """globals so I dont repeat the same variable all the functions"""
  30.         global larvas, roaches, drones, base_amount, hatcherys, sp, enemy_units, \
  31.         roach, rw, lair, base, abilitys, suplyl, hydraden, hydra, base_exist, larva_exist, \
  32.         evochambers
  33.         larvas = self.units(LARVA)
  34.         larva_exist = larvas.exists
  35.         roach = self.units(ROACH)
  36.         roaches = roach.amount
  37.         drones = self.units(DRONE).amount
  38.         hatcherys = self.townhalls
  39.         base_exist = hatcherys.exists
  40.         hydraden = self.units(HYDRALISKDEN)
  41.         hydra = self.units(HYDRALISK)
  42.         base_amount = hatcherys.amount
  43.         sp = self.units(SPAWNINGPOOL)
  44.         base = self.units(HATCHERY)
  45.         enemy_units = self.known_enemy_units.not_structure
  46.         rw = self.units(ROACHWARREN)
  47.         lair = self.units(LAIR)
  48.         evochambers = self.units(EVOLUTIONCHAMBER)
  49.         abilitys = self.get_available_abilities
  50.         suplyl = self.supply_left
  51.         await self.defend_workerrush()
  52.         if iteration % 7 or not self.flag2:
  53.             await self.distribute_workers()
  54.         await self.build_workers()
  55.         await self.build_overlords()
  56.         await self.build_hatchery()
  57.         await self.build_spawning_pool()
  58.         await self.upgrade_lair()
  59.         await self.build_queens_inject_larva()
  60.         await self.build_roach_warren()
  61.         await self.build_extrator()
  62.         await self.build_evochamber()
  63.         await self.build_roaches()
  64.         await self.defend_attack()
  65.         await self.armor_attack()
  66.         await self.build_hydraden()
  67.         await self.build_hydras()
  68.         await self.rw_upgrades()
  69.  
  70.     '''def seconds(self):
  71.        return self.state.game_loop * 0.725 * (1 / 16)
  72.        '''
  73.  
  74.     async def build_workers(self):
  75.         """this logic works for now, maybe later I ll have to change(when I
  76.        focus on the transition)"""
  77.         if drones < 18 * base_amount \
  78.                 and self.can_afford(DRONE) and larva_exist \
  79.                 and suplyl > 0 and drones < 81:
  80.             if base_amount > 3:
  81.                 if roaches > 7:
  82.                     await self.do(larvas.random.train(DRONE))
  83.             else:
  84.                 await self.do(larvas.random.train(DRONE))
  85.  
  86.     async def build_overlords(self):
  87.         """this logic can be improved, sometimes I get supply blocked for a
  88.        little bit, nothing major"""
  89.         if base_amount == 1:
  90.             if suplyl < 3 and self.can_afford(OVERLORD) and larva_exist \
  91.                     and not self.already_pending(OVERLORD):
  92.                 await self.do(larvas.random.train(OVERLORD))
  93.         elif suplyl < 11 and self.can_afford(OVERLORD) and larva_exist\
  94.                 and self.supply_cap != 200:
  95.             await self.do(larvas.random.train(OVERLORD))
  96.  
  97.     async def build_hatchery(self):
  98.         """this logic works for now, Ill have to change it when the expand_now()
  99.        bug gets fixed"""
  100.         try:
  101.             if self.can_afford(HATCHERY) and base_amount < 7 \
  102.                     and not self.already_pending(HATCHERY):
  103.                 if base_amount <= 2:
  104.                     await self.expand_now()
  105.                 elif base_amount == 3 and roaches > 15:
  106.                     await self.expand_now()
  107.                 elif base_amount * 6.5 < roaches + hydra.amount:
  108.                     await self.expand_now()
  109.         except AssertionError:
  110.             print("damn it")
  111.  
  112.     async def build_spawning_pool(self):
  113.         """good logic, no way to improve, final version"""
  114.         if self.can_afford(SPAWNINGPOOL) and not sp.exists and base_amount == 2:
  115.             await self.build(SPAWNINGPOOL, near=hatcherys.first.position.towards(self._game_info.map_center, 6))
  116.  
  117.     async def build_queens_inject_larva(self):
  118.         """this logic works for now, even tho it works for now it will be ideal
  119.         if the queens get created once for each hatchery so it doesnt have to
  120.          travel for the injection"""
  121.         queens = self.units(QUEEN)
  122.         if base_exist:
  123.             hatcherys_random = hatcherys.random
  124.             if sp.ready.exists:
  125.                 if queens.amount < hatcherys.ready.amount and hatcherys_random.is_ready \
  126.                         and not self.already_pending(QUEEN) and hatcherys_random.noqueue and suplyl > 1:
  127.                     if self.can_afford(QUEEN) and not queens.closer_than(8, hatcherys_random):
  128.                         await self.do(hatcherys_random.train(QUEEN))
  129.             for queen in queens.idle:
  130.                 selected = hatcherys.closest_to(queen.position)
  131.                 if queen.energy >= 25 and not selected.has_buff(QUEENSPAWNLARVATIMER):
  132.                     await self.do(queen(EFFECT_INJECTLARVA, selected))
  133.  
  134.     async def build_roach_warren(self):
  135.         """good logic, no way to improve, final version"""
  136.         if self.can_afford(ROACHWARREN) and not rw.exists \
  137.                 and not self.already_pending(ROACHWARREN) and sp.ready and base_amount == 3:
  138.             await self.build(ROACHWARREN, near=sp.first)
  139.  
  140.     async def build_extrator(self):
  141.         """logic can be improved, its over collecting gas, when the amount of
  142.        bases raise, this will have to be changed even more"""
  143.         gas = self.units(EXTRACTOR)
  144.         gaisers = gas.amount
  145.         if sp.exists:
  146.             if hatcherys.ready.exists:
  147.                 vgs = self.state.vespene_geyser.closer_than(10, hatcherys.ready.random)
  148.                 for vg in vgs:
  149.                     drone = self.select_build_worker(vg.position)
  150.                     if drone is not None and self.can_afford(EXTRACTOR)\
  151.                             and await self.can_place(EXTRACTOR, vg.position):
  152.                         if gaisers < 9 and not self.already_pending(EXTRACTOR) \
  153.                                 and base_amount >= 2 and self.can_afford(EXTRACTOR):
  154.                             if not gas.exists:
  155.                                 err = await self.do(drone.build(EXTRACTOR, vg))
  156.                                 if not err:
  157.                                     break
  158.                             elif gaisers == 1 and evochambers.ready.exists:
  159.                                 err = await self.do(drone.build(EXTRACTOR, vg))
  160.                                 if not err:
  161.                                     break
  162.                             elif gaisers <= 4 and rw.ready.exists and roach.ready.exists:
  163.                                 err = await self.do(drone.build(EXTRACTOR, vg))
  164.                                 if not err:
  165.                                     break
  166.                             elif roaches > gaisers * 1.75 and hatcherys.ready.amount >= gaisers / 2:
  167.                                 err = await self.do(drone.build(EXTRACTOR, vg))
  168.                                 if not err:
  169.                                     break
  170.  
  171.     async def build_evochamber(self):
  172.         """this works for now, maybe will have to change the logic, since its
  173.        way to greedy to make 2 evochambers before the third base, but for now
  174.        the change is not needed"""
  175.         if self.can_afford(EVOLUTIONCHAMBER) and rw.exists \
  176.                 and not self.already_pending(EVOLUTIONCHAMBER):
  177.             if not evochambers.exists:
  178.                 await self.build(EVOLUTIONCHAMBER, near=rw.first)
  179.             elif base_amount == 3 and evochambers.amount < 2:
  180.                 await self.build(EVOLUTIONCHAMBER, near=rw.first)
  181.  
  182.     async def build_roaches(self):
  183.         """this works for now but can be improved a lot, the hydra-roach ratio
  184.        needs observation and also needs to be smarter"""
  185.         if roaches > 16:
  186.             if hydraden.ready.exists:
  187.                 if self.flag1 and suplyl > 1 \
  188.                         and self.can_afford(ROACH) and larva_exist \
  189.                         and rw.ready.exists \
  190.                         and (hydra.amount * 2) - roaches > 0:
  191.                     await self.do(larvas.random.train(ROACH))
  192.         elif suplyl > 1 and self.can_afford(ROACH) and larva_exist \
  193.                 and rw.ready.exists:
  194.             await self.do(larvas.random.train(ROACH))
  195.  
  196.     async def armor_attack(self):
  197.         """good logic for now all Ill have to do is add 3/ 3 when I focus on
  198.        the late game later"""
  199.         if evochambers.ready.idle.exists:
  200.             for evo in evochambers.ready.idle:
  201.                 abilities = await abilitys(evo)
  202.                 abilities_list = [RESEARCH_ZERGMISSILEWEAPONSLEVEL1,
  203.                                   RESEARCH_ZERGMISSILEWEAPONSLEVEL2,
  204.                                   RESEARCH_ZERGGROUNDARMORLEVEL1,
  205.                                   RESEARCH_ZERGGROUNDARMORLEVEL2]
  206.                 for ability in abilities_list:
  207.                     if ability in abilities:
  208.                         if self.can_afford(ability):
  209.                             err = await self.do(evo(ability))
  210.                             if not err:
  211.                                 break
  212.  
  213.     async def rw_upgrades(self):
  214.         if rw.ready.idle.exists and lair.exists and hatcherys.ready.amount >= 3:
  215.             abilities = await abilitys(rw.first)
  216.             abilities_list = [RESEARCH_TUNNELINGCLAWS, RESEARCH_GLIALREGENERATION]
  217.             for ability in abilities_list:
  218.                 if ability in abilities:
  219.                     if self.can_afford(ability):
  220.                         err = await self.do(rw.first(ability))
  221.                         if not err:
  222.                             break
  223.  
  224.     async def defend_attack(self):
  225.         """this is the logic the needs changes the most, its much better than
  226.            before, but the units are sent in a line instead of being in a group,
  227.            there is a fail in that too that prevents it from targeting buildings(
  228.            unless its on sight), also its not smart at all it just a_moves and
  229.            target the closest enemy, needs a lot of improvements, its the soul of
  230.            the engine"""
  231.         enemy_structures = self.known_enemy_structures
  232.         hydra_roach = self.units.of_type([HYDRALISK, ROACH])
  233.         filtered_enemies = enemy_units.exclude_type([ADEPTPHASESHIFT, DISRUPTORPHASED, EGG])
  234.         if roaches > 14:
  235.             for r in hydra_roach.prefer_idle:
  236.                 if r.health < 60 and BURROWDOWN_ROACH in await abilitys(roach)\
  237.                         and self.can_afford(BURROWDOWN_ROACH):
  238.                     await self.do(r(BURROWDOWN_ROACH))
  239.                 elif r.health > 120 and BURROWUP_ROACH in await abilitys(roach) \
  240.                         and self.can_afford(BURROWUP_ROACH):
  241.                     await self.do(r(BURROWUP_ROACH))
  242.                 else:
  243.                     if filtered_enemies.exists:
  244.                         if r.type_id == HYDRALISK:
  245.                             err = await self.do(r.attack(filtered_enemies.closest_to(r.position)))
  246.                             if not err:
  247.                                 break
  248.                         else:
  249.                             target = filtered_enemies.not_flying
  250.                             if target.exists:
  251.                                 err = await self.do(r.attack(target.closest_to(r.position)))
  252.                                 if not err:
  253.                                     break
  254.                     if enemy_structures.exists:
  255.                         err = await self.do(r.attack(enemy_structures.closest_to(r.position)))
  256.                         if not err:
  257.                             break
  258.                     elif r.position.distance_to(self.enemy_start_locations[0]) > 5:
  259.                         err = await self.do(r.attack(self.enemy_start_locations[0]))
  260.                         if not err:
  261.                             break
  262.         elif roaches + hydra.amount > 4:
  263.             for r in hydra_roach.prefer_idle:
  264.                 if filtered_enemies.exists:
  265.                     if r.type_id == HYDRALISK:
  266.                         err = await self.do(r.attack(filtered_enemies.closest_to(r.position)))
  267.                         if not err:
  268.                             break
  269.                     else:
  270.                         target = filtered_enemies.not_flying
  271.                         if target.exists:
  272.                             err = await self.do(r.attack(target.closest_to(r.position)))
  273.                             if not err:
  274.                                 break
  275.  
  276.     async def upgrade_lair(self):
  277.         """Good for now, but just like the evochambers, its way too greedy,
  278.        maybe Ill have to change it but for now is good, carefull to not
  279.        surpass the number of roaches of the first condition in the build
  280.        roaches function"""
  281.         if base.ready.exists and sp.ready.exists and self.can_afford(
  282.                 UPGRADETOLAIR_LAIR) and not self.flag1:
  283.             self.flag1 = True
  284.             await self.do(base.ready.idle.random(UPGRADETOLAIR_LAIR))
  285.  
  286.     async def build_hydraden(self):
  287.         """This starts very late, maybe ill have to change it but its not
  288.        priority, I want to build it smartly(like only build it vs flying
  289.        focused army)"""
  290.         if self.can_afford(HYDRALISKDEN) and not hydraden.exists \
  291.                 and not self.already_pending(HYDRALISKDEN) and rw.ready \
  292.                 and lair.exists:
  293.             if roaches > 13:
  294.                 await self.build(HYDRALISKDEN, near=rw.first)
  295.  
  296.     async def build_hydras(self):
  297.         """good enough for now, no need for more condition since the program
  298.        already prioritizes roaches so I put the logic there, maybe later when
  299.         I add more units Ill have to change this"""
  300.         if suplyl > 1 and self.can_afford(HYDRALISK) and larva_exist \
  301.                 and hydraden.ready.exists:
  302.             await self.do(larvas.random.train(HYDRALISK))
  303.  
  304.     async def defend_workerrush(self):
  305.         """it always works vs worker rushes, but its way too abroad it attacks
  306.        scouting workers, also the logic can be optimized, its super slow when
  307.        its called"""
  308.         if enemy_units.exists and base_exist:
  309.             enemy_units_close = enemy_units.closer_than(5, hatcherys.first)
  310.             if enemy_units_close.exists:
  311.                 workers = enemy_units_close.of_type([PROBE, DRONE, SCV])
  312.                 if workers.exists and base_amount < 2:
  313.                     self.flag2 = True
  314.                     for worker in self.workers:
  315.                         err = await self.do(worker.attack(enemy_units.closest_to(worker.position)))
  316.                         if not err:
  317.                             break
  318.         else:
  319.             self.flag2 = False
  320.  
  321. '''
  322.    async def defend_12pool(self):
  323.        if self.seconds() < 210:
  324.            my_zerglings = self.units(ZERGLING).idle
  325.            zerglings = enemy_units.of_type([ZERGLING])
  326.            if zerglings.amount > 4:
  327.                for z in my_zerglings:
  328.                    if zerglings.exists and my_zerglings.amount > 8:
  329.                        await self.do(z.attack(zerglings.closest_to(z.position)))
  330.                if sp.ready.exists:
  331.                    if self.can_afford(ZERGLING)and larva_exist:
  332.                        await self.do(larvas.random.train(ZERGLING))
  333.                    if self.can_afford(SPINECRAWLER):
  334.                        await self.build(SPINECRAWLER, near=sp.first)
  335. '''
  336.  
  337. class WorkerRushBot(sc2.BotAI):
  338.     async def on_step(self, iteration):
  339.         if iteration == 0:
  340.             for worker in self.workers:
  341.                 await self.do(worker.attack(self.enemy_start_locations[0]))
  342.  
  343. run_game(maps.get("AbyssalReefLE"), [
  344.     Bot(Race.Zerg, ZergRoachv3()),
  345.     Computer(Race.Zerg, Difficulty.VeryHard)
  346. ], realtime=False)
  347. # CheatVision
Advertisement
Add Comment
Please, Sign In to add comment