|
| 1 | +import random |
| 2 | + |
| 3 | +import sc2 |
| 4 | +from sc2 import Race, Difficulty |
| 5 | +from sc2.constants import * |
| 6 | +from sc2.player import Bot, Computer |
| 7 | +from sc2.player import Human |
| 8 | + |
| 9 | +class ProxyRaxBot(sc2.BotAI): |
| 10 | + def select_target(self): |
| 11 | + target = self.known_enemy_structures |
| 12 | + if target.exists: |
| 13 | + return target.random.position |
| 14 | + |
| 15 | + target = self.known_enemy_units |
| 16 | + if target.exists: |
| 17 | + return target.random.position |
| 18 | + |
| 19 | + if min([u.position.distance_to(self.enemy_start_locations[0]) for u in self.units]) < 5: |
| 20 | + return self.enemy_start_locations[0].position |
| 21 | + |
| 22 | + return self.state.mineral_field.random.position |
| 23 | + |
| 24 | + async def on_step(self, iteration): |
| 25 | + cc = (self.units(COMMANDCENTER) | self.units(ORBITALCOMMAND)) |
| 26 | + if not cc.exists: |
| 27 | + target = self.known_enemy_structures.random_or(self.enemy_start_locations[0]).position |
| 28 | + for unit in self.workers | self.units(BATTLECRUISER): |
| 29 | + await self.do(unit.attack(target)) |
| 30 | + return |
| 31 | + else: |
| 32 | + cc = cc.first |
| 33 | + |
| 34 | + |
| 35 | + if iteration % 50 == 0 and self.units(BATTLECRUISER).amount > 2: |
| 36 | + target = self.select_target() |
| 37 | + forces = self.units(BATTLECRUISER) |
| 38 | + if (iteration//50) % 10 == 0: |
| 39 | + for unit in forces: |
| 40 | + await self.do(unit.attack(target)) |
| 41 | + else: |
| 42 | + for unit in forces.idle: |
| 43 | + await self.do(unit.attack(target)) |
| 44 | + |
| 45 | + if self.can_afford(SCV) and self.workers.amount < 22 and cc.noqueue: |
| 46 | + await self.do(cc.train(SCV)) |
| 47 | + |
| 48 | + if self.units(FUSIONCORE).exists and self.can_afford(BATTLECRUISER): |
| 49 | + for sp in self.units(STARPORT): |
| 50 | + if sp.has_add_on and sp.noqueue: |
| 51 | + if not self.can_afford(BATTLECRUISER): |
| 52 | + break |
| 53 | + await self.do(sp.train(BATTLECRUISER)) |
| 54 | + |
| 55 | + elif self.supply_left < 3: |
| 56 | + if self.can_afford(SUPPLYDEPOT): |
| 57 | + await self.build(SUPPLYDEPOT, near=cc.position.towards(self.game_info.map_center, 8)) |
| 58 | + |
| 59 | + if self.units(SUPPLYDEPOT).exists: |
| 60 | + if not self.units(BARRACKS).exists: |
| 61 | + if self.can_afford(BARRACKS): |
| 62 | + await self.build(BARRACKS, near=cc.position.towards(self.game_info.map_center, 8)) |
| 63 | + |
| 64 | + elif self.units(BARRACKS).exists and self.units(REFINERY).amount < 2: |
| 65 | + if self.can_afford(REFINERY): |
| 66 | + vgs = self.state.vespene_geyser.closer_than(20.0, cc) |
| 67 | + for vg in vgs: |
| 68 | + if self.units(REFINERY).closer_than(1.0, vg).exists: |
| 69 | + break |
| 70 | + |
| 71 | + worker = self.select_build_worker(vg.position) |
| 72 | + if worker is None: |
| 73 | + break |
| 74 | + |
| 75 | + await self.do(worker.build(REFINERY, vg)) |
| 76 | + break |
| 77 | + |
| 78 | + if self.units(BARRACKS).ready.exists: |
| 79 | + f = self.units(FACTORY) |
| 80 | + if not f.exists: |
| 81 | + if self.can_afford(FACTORY): |
| 82 | + await self.build(FACTORY, near=cc.position.towards(self.game_info.map_center, 8)) |
| 83 | + elif f.ready.exists and self.units(STARPORT).amount < 2: |
| 84 | + if self.can_afford(STARPORT): |
| 85 | + await self.build(STARPORT, near=cc.position.towards(self.game_info.map_center, 30).random_on_distance(8)) |
| 86 | + |
| 87 | + for sp in self.units(STARPORT).ready: |
| 88 | + if sp.add_on_tag == 0: |
| 89 | + await self.do(sp.build(STARPORTTECHLAB)) |
| 90 | + |
| 91 | + if self.units(STARPORT).ready.exists: |
| 92 | + if self.can_afford(FUSIONCORE) and not self.units(FUSIONCORE).exists: |
| 93 | + await self.build(FUSIONCORE, near=cc.position.towards(self.game_info.map_center, 8)) |
| 94 | + |
| 95 | + for a in self.units(REFINERY): |
| 96 | + if a.assigned_harvesters < a.ideal_harvesters: |
| 97 | + w = self.workers.closer_than(20, a) |
| 98 | + if w.exists: |
| 99 | + await self.do(w.random.gather(a)) |
| 100 | + |
| 101 | + for scv in self.units(SCV).idle: |
| 102 | + await self.do(scv.gather(self.state.mineral_field.closest_to(cc))) |
| 103 | + |
| 104 | +def main(): |
| 105 | + sc2.run_game(sc2.maps.get("Sequencer LE"), [ |
| 106 | + # Human(Race.Terran), |
| 107 | + Bot(Race.Terran, ProxyRaxBot()), |
| 108 | + Computer(Race.Zerg, Difficulty.Hard) |
| 109 | + ], realtime=False) |
| 110 | + |
| 111 | +if __name__ == '__main__': |
| 112 | + main() |
0 commit comments