def game(): run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, ProtossBot()), Computer(Race.Protoss, Difficulty.Hard) ], realtime=False)
import sc2 from sc2 import run_game, maps, Race, Difficulty from sc2.player import Bot, Computer from sc2.constants import * from sc2.units import Units from sc2.unit import Unit from sc2.position import Point2 from sc2.ids.unit_typeid import UnitTypeId class ClayBot(sc2.BotAI): async def on_step(self, iteration): # what to do every step await self.distribute_workers() # in sc2/bot_ai.py await self.assemble_workers() # TODO: figure out how to multi-manage command centers. async def assemble_workers(self): for command_center in self.units(UnitTypeId.COMMANDCENTER).ready.idle: if self.can_afford(UnitTypeId.SCV) and self.supply_workers < 16: await self.do(command_center.train(UnitTypeId.SCV)) # Ephemeron is default map. run_game(maps.get("EphemeronLE"), [ Bot(Race.Terran, ClayBot()), Computer(Race.Zerg, Difficulty.Easy) ], realtime=False)
for s in self.units(UnitTypeId.STALKER).idle: s.attack(self.find_target(self.state)) elif self.units(UnitTypeId.STALKER).amount > 3: if len(self.enemy_units) > 0: for s in self.units(UnitTypeId.STALKER).idle: s.attack(self.enemy_units.random) def find_target(self, state): if len(self.enemy_units) > 0: return self.enemy_units.random elif len(self.enemy_structures) > 0: return self.enemy_structures.random else: return self.enemy_start_locations[0] def count_harvesters(self): ideal = 0 assigned = 0 for structure in self.structures: ideal += structure.ideal_harvesters assigned += structure.assigned_harvesters return (assigned, ideal) run_game( maps.get("AcropolisLE"), [Bot(Race.Protoss, ProtossBot()), Computer(Race.Zerg, Difficulty.Medium)], realtime=False)
def main(): run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, BinaryBot()), Computer(Race.Terran, Difficulty.Medium) ], realtime=False)
import sc2 from sc2 import run_game, maps, Race, Difficulty from sc2.player import Bot, Computer class WorkerRushBot(sc2.BotAI): async def on_step(self, iteration): if iteration == 0: for worker in self.workers: await self.do(worker.attack(self.enemy_start_locations[0])) run_game(maps.get("Abyssal Reef LE"), [ Bot(Race.Zerg, WorkerRushBot()), Computer(Race.Protoss, Difficulty.Medium) ], realtime=True)
async def build_offensive_force(self): for gw in self.units(GATEWAY).ready.noqueue: if self.units(STALKER).amount < self.units(VOIDRAY).amount: if self.can_afford(STALKER) and self.supply_left > 0: await self.do(gw.train(STALKER)) for sg in self.units(STARGATE).ready.noqueue: if self.can_afford(VOIDRAY) and self.supply_left > 0: await self.do(sg.train(VOIDRAY)) async def attack(self): for aggressive_unit, config in self.aggressive_units.items(): if (self.units(aggressive_unit).amount > config["fight"] and self.units(aggressive_unit).amount > config["defend"]): for s in self.units(STALKER).idle: await self.do( s.attack(random.choice(self.known_enemy_units))) elif self.units(aggressive_unit).amount > config["defend"]: if len(self.known_enemy_units) > 0: for s in self.units(aggressive_unit).idle: await self.do( s.attack(random.choice(self.known_enemy_units))) run_game( maps.get("AbyssalReefLE"), [Bot(Race.Protoss, Cyrus()), Computer(Race.Protoss, Difficulty.Hard)], realtime=False, ) time.sleep(100)
if self.can_afford(PYLON): loc = self.units(NEXUS).random await self.build(PYLON,near=loc) def find_target(self, state): if len(self.known_enemy_units) > 0: return random.choice(self.known_enemy_units) elif len(self.known_enemy_structures) > 0: return random.choice(self.known_enemy_structures) else: return self.enemy_start_locations[0] async def command(self): # {UNIT: [n to fight, n to defend]} aggressive_units = {STALKER: [15, 1], VOIDRAY: [10, 1]} for UNIT in aggressive_units: if self.units(UNIT).amount > aggressive_units[UNIT][0] and self.units(UNIT).amount > aggressive_units[UNIT][1]: for s in self.units(UNIT).idle: await self.do(s.attack(self.find_target(self.state))) elif self.units(UNIT).amount > aggressive_units[UNIT][1]: if len(self.known_enemy_units) > 0: for s in self.units(UNIT).idle: await self.do(s.attack(random.choice(self.known_enemy_units))) run_game(maps.get("(2)DreamcatcherLE"),[ Bot(Race.Protoss,Main()), Computer(Race.Random,Difficulty.VeryHard) ], realtime=False)
wait = random.randrange(20, 165) self.do_something_after = self.iteration + wait elif choice == 1: # 攻击最近的nexus if len(self.known_enemy_units) > 0: target = self.known_enemy_units.closest_to( random.choice(self.units(NEXUS))) elif choice == 2: # 攻击敌方建筑 if len(self.known_enemy_structures) > 0: target = random.choice(self.known_enemy_structures) elif choice == 3: # 攻击大本营(start) target = self.enemy_start_locations[0] if target: for vr in self.units(VOIDRAY).idle: await self.do(vr.attack(target)) y = np.zeros(4) y[choice] = 1 # print(y) self.train_data.append([y, self.flipped]) run_game(maps.get("KingsCoveLE"), [ Bot(Race.Protoss, SentdeBot(use_model=True)), Computer(Race.Terran, Difficulty.Medium) ], realtime=False)
run_ladder_game(bot) else: # Local game print("Starting local game...") parser = argparse.ArgumentParser() parser.add_argument('--ForceRace', type=str, nargs="?", help='Force a specific race') parser.add_argument('--ForceStrategy', type=str, nargs="?", help='Force a specific strategy') args, _ = parser.parse_known_args() bot: Bot if args.ForceRace is None: bot = Bot(Race.Random, Chance()) elif args.ForceStrategy is None: bot = Bot(race_map[args.ForceRace], Chance()) else: bot = Bot(race_map[args.ForceRace], Chance(args.ForceStrategy)) run_game(maps.get("2000AtmospheresAIE"), [ bot, Computer(Race.Random, Difficulty.VeryHard), ], save_replay_as=f'replays/replay.SC2Replay', realtime=False)
async def morphaa_overlordeja(self): liian_vahan_overlordeja = len(self.units.of_type( UnitTypeId.OVERLORD)) < 5 if (self.tarpeeksi_larvoja() and liian_vahan_overlordeja and self.can_afford(UnitTypeId.OVERLORD) and not self.already_pending(UnitTypeId.OVERLORD)): await self.chat_send('Morphataan overlordeja!') larva = self.units.of_type(UnitTypeId.LARVA).random treenaus = larva(AbilityId.LARVATRAIN_OVERLORD) await self.do(treenaus) async def morphaa_droneja(self): liian_vahan_droneja = len(self.units.of_type(UnitTypeId.DRONE)) < 16 if (self.tarpeeksi_larvoja() and liian_vahan_droneja and self.can_afford(UnitTypeId.DRONE) and not self.already_pending(UnitTypeId.DRONE)): await self.chat_send('Morphataan droneja :)!') larva = self.units.of_type(UnitTypeId.LARVA).random treenaus = larva(AbilityId.LARVATRAIN_DRONE) await self.do(treenaus) def tarpeeksi_larvoja(self): return len(self.units.of_type(UnitTypeId.LARVA)) >= 1 run_game( maps.get("Triton LE"), [Bot(Race.Zerg, Tyoskentelijat()), Computer(Race.Zerg, Difficulty.Easy)], realtime=True)
def main(): run_game(maps.get("AutomatonLE"), [ Bot(Race.Protoss, SentdeBot()), Computer(Race.Protoss, Difficulty.Medium) ], realtime=False) # realtime设为False可以加速
await self.do(factory.train(HELLION)) async def attack(self): for marine in self.units(MARINE).idle: if self.units(MARINE).ready.amount > 15: await self.do( marine.attack( self.known_enemy_structures.random_or( self.enemy_start_locations[0]).position)) elif len(self.known_enemy_units) > 0: await self.do( marine.attack(random.choice(self.known_enemy_units))) for hellion in self.units(HELLION).idle: if self.units(HELLION).ready.amount > 5: await self.do( hellion.attack( self.known_enemy_structures.random_or( self.enemy_start_locations[0]).position)) elif len(self.known_enemy_units) > 0: await self.do( hellion.attack(random.choice(self.known_enemy_units))) if __name__ == '__main__': run_game(maps.get('AbyssalReefLE'), [ Bot(Race.Terran, MarineRushBot()), Computer(Race.Terran, Difficulty.Medium) ], realtime=False)
def main(): run_game(maps.get("AcidplantLE"), [ Bot(Race.Zerg, charles()), Computer(Race.Protoss, Difficulty.Hard) ], realtime=False)
def collect() -> None: """ To be able to run this, you need to have the "Empty128" map downloaded and in your SC2/maps folder You can download the map from here ("Melee" link): https://github.com/Blizzard/s2client-proto#map-packs """ run_game(maps.get("Empty128"), [Bot(Race.Zerg, MyBot())], realtime=False)
proxy = self.structures(PYLON).closest_to( self.enemy_start_locations[0]) pylon = self.structures(PYLON).ready.random if self.structures(GATEWAY).ready: # If we have no cyber core, build one if not self.structures(CYBERNETICSCORE): if self.can_afford( CYBERNETICSCORE) and self.already_pending( CYBERNETICSCORE) == 0: await self.build(CYBERNETICSCORE, near=pylon) # Build up to 4 gates if self.can_afford(GATEWAY) and self.structures( WARPGATE).amount + self.structures(GATEWAY).amount < 4: await self.build(GATEWAY, near=pylon) # TODO FIX BUILD_OFFENSIVE_ARMY() # TODO CHANGE WINDOW RESOLUTION() async def build_offensive_army(self): for gw in self.units(GATEWAY).ready.noqueue: if self.can_afford(STALKER) and self.supply_left > 0: await self.do(gw.train(STALKER)) run_game( maps.get("EphemeronLE"), [Bot(Race.Protoss, ClayBot()), Computer(Race.Terran, Difficulty.Easy)], realtime=False)
import sc2 from sc2 import run_game, maps, Race, Difficulty from sc2.player import Bot, Computer, Human class WorkerRushBot(sc2.BotAI): async def on_step(self, iteration): if iteration == 0: for worker in self.workers: await self.do(worker.attack(self.enemy_start_locations[0])) if __name__ == "__main__": map_name = "training_scenario_5_Human" run_game(maps.get(map_name), [Human(Race.Terran), Computer(Race.Terran, Difficulty.Medium)], realtime=True)
if worker is None: break if not self.units(UnitTypeId.REFINERY).closer_than(1.0, gas).exists: await self.do(worker.build(UnitTypeId.REFINERY, gas)) async def expand_base(self): if self.units(UnitTypeId.COMMANDCENTER).amount < 3 and self.can_afford( UnitTypeId.COMMANDCENTER): await self.expand_now() async def offensive_force_buildings(self): commandc = self.units(UnitTypeId.COMMANDCENTER).ready if not self.units(UnitTypeId.BARRACKS).ready.exists: if self.can_afford( UnitTypeId.BARRACKS) and not self.already_pending( UnitTypeId.BARRACKS): await self.build(UnitTypeId.BARRACKS, near=commandc.random) async def build_offensive_force(self): for barracks in self.units(UnitTypeId.BARRACKS).ready.noqueue: if self.can_afford(UnitTypeId.MARINE) and self.supply_left > 0: await self.do(barracks.train(UnitTypeId.MARINE)) run_game( maps.get("KingsCoveLE"), [Bot(Race.Terran, TerranBot()), Computer(Race.Zerg, Difficulty.VeryEasy)], realtime=True)
#!/usr/bin/env python # -*- coding: utf-8 -*- """ Entry point """ from sc2 import Race from sc2 import Difficulty from sc2 import run_game from sc2 import maps as sc2_maps from sc2.player import Computer from sc2.player import Human from config import maps from strategy.terran.my_bot_player import MyBotPlayer if __name__ == '__main__': players = [ MyBotPlayer().build_bot_player(), MyBotPlayer().build_bot_player() # Human(Race.Terran) # Computer(Race.Zerg, Difficulty.VeryEasy) ] run_game( map_settings=sc2_maps.get(maps.MAP_SIMPLE64), players=players, realtime=False )
def main(): # Creates a random number between 0-9 diff = random.randrange(0, 10) # depending on the number selected a difficulty is chosen if diff == 0: run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, BinaryBot()), Computer(Race.Terran, Difficulty.VeryEasy) ], realtime=False) if diff == 1: run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, BinaryBot()), Computer(Race.Terran, Difficulty.Easy) ], realtime=False) if diff == 2: run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, BinaryBot()), Computer(Race.Terran, Difficulty.Medium) ], realtime=False) if diff == 3: run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, BinaryBot()), Computer(Race.Terran, Difficulty.MediumHard) ], realtime=False) if diff == 4: run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, BinaryBot()), Computer(Race.Terran, Difficulty.Hard) ], realtime=False) if diff == 5: run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, BinaryBot()), Computer(Race.Terran, Difficulty.Harder) ], realtime=False) if diff == 6: run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, BinaryBot()), Computer(Race.Terran, Difficulty.VeryHard) ], realtime=False) if diff == 7: run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, BinaryBot()), Computer(Race.Terran, Difficulty.CheatVision) ], realtime=False) if diff == 8: run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, BinaryBot()), Computer(Race.Terran, Difficulty.CheatMoney) ], realtime=False) if diff == 9: run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, BinaryBot()), Computer(Race.Terran, Difficulty.CheatInsane) ], realtime=False)
async def on_step(self, iteration): # SEND INITIAL NOOB if iteration == 0: noob = self.workers[0] await self.do(noob.move(self.nextpoint)) # SET RALLY POINT FOR NOOB CENTER for cc in self.units(COMMANDCENTER): abilities = await self.get_available_abilities(cc) if AbilityId.RALLY_COMMANDCENTER in abilities: await self.do(cc(RALLY_COMMANDCENTER, self.nextpoint)) # WHEN NOOB DIES, SPAWN NEW NOOB for cc in self.units(UnitTypeId.COMMANDCENTER).ready.noqueue: if cc.assigned_harvesters < 12: await self.do(cc.train(SCV)) # MOAR NOOBS if self.supply_left < 2: if self.can_afford( SUPPLYDEPOT) and self.already_pending(SUPPLYDEPOT) < 2: await self.build(SUPPLYDEPOT, near=cc.position.towards( self.game_info.map_center, 5)) #start the game run_game(maps.get("Simple64"), [Bot(Race.Terran, NoobNoob()), Bot(Race.Terran, BoomBot())], realtime=False)
#{UNIT: [n to fight, n to defend]} aggressive_units = {'m': [50, 5], 'hn': [15, 3]} # print(aggressive_units['m']) total = self.units(MARINE).amount + self.units(HELLION).amount if self.units(MARINE).amount >= aggressive_units['m'][ 0] and self.units(HELLION).amount >= aggressive_units['hn'][0]: for s in self.units(MARINE).idle: # if self.units(MARINE).amount == aggressive_units['MARINE'][0] and self.unitsHELLION).amount == aggressive_units['HELLION'][0]: await self.do(s.attack(self.enemy_start_locations[0])) for m in self.units(HELLION).idle: # if self.units(MARINE).amount == aggressive_units['MARINE'][0] and self.unitsHELLION).amount == aggressive_units['HELLION'][0]: await self.do(m.attack(self.enemy_start_locations[0])) if self.units(HELLION).amount > aggressive_units['hn'][ 1] and self.units(MARINE).amount > aggressive_units['m'][1]: if len(self.known_enemy_units) > 0: for s in self.units(HELLION).idle: await self.do( s.attack(random.choice(self.known_enemy_units))) if len(self.known_enemy_units) > 0: for m in self.units(MARINE).idle: await self.do( m.attack(random.choice(self.known_enemy_units))) run_game(maps.get("CyberForestLE"), [Bot(Race.Terran, Hestia()), Computer(Race.Zerg, Difficulty.Medium)], realtime=False)
async def expand(self): try: if self.units(NEXUS).amount < self.nexus_limit and self.can_afford(NEXUS): await self.expand_now() except Exception as e: print(str(e)) if __name__ == "__main__": """ parser = argparse.ArgumentParser(description = "The Squinn Bot") parser.add_argument("--race", choices = ["protoss", "zerg", "terran", "random"], default = "random", help = "Specify the race [DEFAULT: random]") parser.add_argument("--seed", type = int, default = 42, help = "Random seed [DEFAULT: 42].") parser.add_argument("--headless", action = "store_true") args = vars(parser.parse_args()) np.random.seed(args['seed']) races = {"protoss": Race.Protoss, "terran": Race.Terran, "zerg": Race.Zerg} if args['race'] == "random": race = np.random.choice(list(races.values())) else: race = races[args['race']] """ run_game(maps.get("AbyssalReefLE"), [ Bot(Race.Protoss, SquinnBot()), Computer(Race.Terran, Difficulty.Easy) ], realtime = True)
if (x, y) != (0, 0) } # filter positions outside map size if exclude_out_of_bounds: positions = { p for p in positions if 0 <= p[0] < self._game_info.pathing_grid.width and 0 <= p[1] < self._game_info.pathing_grid.height } return positions async def expand(self): if self.units(COMMANDCENTER).amount + self.units( ORBITALCOMMAND).amount < ( self.iteration / self.ITERATIONS_PER_MINUTE ) * 4 and self.can_afford( COMMANDCENTER) and not self.already_pending(COMMANDCENTER): await self.expand_now() if self.minerals > 700 and self.can_afford( COMMANDCENTER) and not self.already_pending(COMMANDCENTER): await self.expand() run_game( maps.get("ThunderbirdLE"), [Bot(Race.Terran, fivebb()), Computer(Race.Zerg, Difficulty.CheatInsane)], realtime=False, save_replay_as='Example.SC2Replay')