# 建造农民 asyncdefbuild_workers(self): # 星灵枢纽(NEXUS)无队列建造,可以提高晶体矿的利用率,不至于占用资源 for
nexus in self.units(UnitTypeId.NEXUS).ready.noqueue: # 是否有50晶体矿 if self.can_afford(UnitTypeId.PROBE): await self.do(nexus.train(UnitTypeId.PROBE))
## 建造水晶 asyncdefbuild_pylons(self): ## 供应人口和现有人口之差小于5且水晶不是正在建造 if self.supply_left<5andnot self.already_pending(UnitTypeId.PYLON): nexuses = self.units(UnitTypeId.NEXUS).ready if nexuses.exists: if self.can_afford(UnitTypeId.PYLON): await self.build(UnitTypeId.PYLON,near=nexuses.first)
# 建造农民 asyncdefbuild_workers(self): # 星灵枢纽(NEXUS)无队列建造,可以提高晶体矿的利用率,不至于占用资源 for nexus in self.units(UnitTypeId.NEXUS).ready.noqueue: # 是否有50晶体矿 if self.can_afford(UnitTypeId.PROBE): await self.do(nexus.train(UnitTypeId.PROBE))
## 建造水晶 asyncdefbuild_pylons(self): ## 供应人口和现有人口之差小于5且建筑不是正在建造 if self.supply_left < 5andnot self.already_pending(UnitTypeId.PYLON): nexuses = self.units(UnitTypeId.NEXUS).ready if nexuses.exists: if self.can_afford(UnitTypeId.PYLON): await self.build(UnitTypeId.PYLON, near=nexuses.first)
## 建造吸收厂 asyncdefbuild_assimilators(self): for nexus in self.units(UnitTypeId.NEXUS).ready: # 在瓦斯泉上建造吸收厂 vaspenes = self.state.vespene_geyser.closer_than(15.0,nexus) for vaspene in vaspenes: ifnot self.can_afford(UnitTypeId.ASSIMILATOR): break worker = self.select_build_worker(vaspene.position) if worker isNone: break ifnot self.units(UnitTypeId.ASSIMILATOR).closer_than(1.0,vaspene).exists: await self.do(worker.build(UnitTypeId.ASSIMILATOR,vaspene))
## 开矿 asyncdefexpand(self): if self.units(UnitTypeId.NEXUS).amount<3and self.can_afford(UnitTypeId.NEXUS): await self.expand_now()
# 建造农民 asyncdefbuild_workers(self): # 星灵枢纽(NEXUS)无队列建造,可以提高晶体矿的利用率,不至于占用资源 for nexus in self.units(UnitTypeId.NEXUS).ready.noqueue: # 是否有50晶体矿 if self.can_afford(UnitTypeId.PROBE): await self.do(nexus.train(UnitTypeId.PROBE))
## 建造水晶 asyncdefbuild_pylons(self): ## 供应人口和现有人口之差小于5且建筑不是正在建造 if self.supply_left < 5andnot self.already_pending(UnitTypeId.PYLON): nexuses = self.units(UnitTypeId.NEXUS).ready if nexuses.exists: if self.can_afford(UnitTypeId.PYLON): await self.build(UnitTypeId.PYLON, near=nexuses.first)
## 建造吸收厂 asyncdefbuild_assimilators(self): for nexus in self.units(UnitTypeId.NEXUS).ready: # 在瓦斯泉上建造吸收厂 vaspenes = self.state.vespene_geyser.closer_than(15.0,nexus) for vaspene in vaspenes: ifnot
self.can_afford(UnitTypeId.ASSIMILATOR): break worker = self.select_build_worker(vaspene.position) if worker isNone: break ifnot self.units(UnitTypeId.ASSIMILATOR).closer_than(1.0,vaspene).exists: await self.do(worker.build(UnitTypeId.ASSIMILATOR,vaspene))
## 开矿 asyncdefexpand(self): if self.units(UnitTypeId.NEXUS).amount<2and self.can_afford(UnitTypeId.NEXUS): await self.expand_now()
## 建造进攻性建筑 asyncdefoffensive_force_buildings(self): if self.units(UnitTypeId.PYLON).ready.exists: pylon = self.units(UnitTypeId.PYLON).ready.random if self.units(UnitTypeId.PYLON).ready.exists: # 根据神族建筑科技图,折跃门建造过后才可以建造控制核心 if self.units(UnitTypeId.GATEWAY).ready.exists: ifnot self.units(UnitTypeId.CYBERNETICSCORE): if self.can_afford(UnitTypeId.CYBERNETICSCORE) andnot self.already_pending(UnitTypeId.CYBERNETICSCORE): await self.build(UnitTypeId.CYBERNETICSCORE,near = pylon) # 否则建造折跃门 else: if self.can_afford(UnitTypeId.GATEWAY) andnot self.already_pending(UnitTypeId.GATEWAY): await self.build(UnitTypeId.GATEWAY,near=pylon)
# 造兵 asyncdefbuild_offensive_force(self): # 无队列化建造 for gw in self.units(UnitTypeId.GATEWAY).ready.noqueue: if self.can_afford(UnitTypeId.STALKER) and self.supply_left>0: await self.do(gw.train(UnitTypeId.STALKER))
# 建造农民 asyncdefbuild_workers(self): # 星灵枢纽(NEXUS)无队列建造,可以提高晶体矿的利用率,不至于占用资源 for nexus in self.units(UnitTypeId.NEXUS).ready.noqueue: # 是否有50晶体矿 if self.can_afford(UnitTypeId.PROBE): await self.do(nexus.train(UnitTypeId.PROBE))
## 建造水晶 asyncdefbuild_pylons(self): ## 供应人口和现有人口之差小于5且建筑不是正在建造 if self.supply_left < 5andnot self.already_pending(UnitTypeId.PYLON): nexuses = self.units(UnitTypeId.NEXUS).ready if nexuses.exists: if self.can_afford(UnitTypeId.PYLON): await self.build(UnitTypeId.PYLON, near=nexuses.first)
## 建造吸收厂 asyncdefbuild_assimilators(self): for nexus in self.units(UnitTypeId.NEXUS).ready: # 在瓦斯泉上建造吸收厂 vaspenes = self.state.vespene_geyser.closer_than(15.0,nexus) for vaspene in vaspenes: ifnot self.can_afford(UnitTypeId.ASSIMILATOR): break worker = self.select_build_worker(vaspene.position) if worker isNone: break ifnot self.units(UnitTypeId.ASSIMILATOR).closer_than(1.0,vaspene).exists: await self.do(worker.build(UnitTypeId.ASSIMILATOR,vaspene))
## 开矿 asyncdefexpand(self): if self.units(UnitTypeId.NEXUS).amount<3and self.can_afford(UnitTypeId.NEXUS): await self.expand_now()
## 建造进攻性建筑 asyncdefoffensive_force_buildings(self): if self.units(UnitTypeId.PYLON).ready.exists: pylon = self.units(UnitTypeId.PYLON).ready.random # 根据神族建筑科技图,折跃门建造过后才可以建造控制核心 if self.units(UnitTypeId.GATEWAY).ready.exists andnot self.units(UnitTypeId.CYBERNETICSCORE): if self.can_afford(UnitTypeId.CYBERNETICSCORE) andnot self.already_pending(UnitTypeId.CYBERNETICSCORE): await self.build(UnitTypeId.CYBERNETICSCORE,near = pylon) # 否则建造折跃门 elif len(self.units(UnitTypeId.GATEWAY))<=3: if self.can_afford(UnitTypeId.GATEWAY) andnot self.already_pending(UnitTypeId.GATEWAY): await self.build(UnitTypeId.GATEWAY,near=pylon)
## 造兵 asyncdefbuild_offensive_force(self): # 无队列化建造 for gw in self.units(UnitTypeId.GATEWAY).ready.noqueue: if self.can_afford(UnitTypeId.STALKER) and self.supply_left>0: await self.do(gw.train(UnitTypeId.STALKER))
## 进攻 asyncdefattack(self): # 追猎者数量超过15个开始进攻 if self.units(UnitTypeId.STALKER).amount>15: for s in self.units(UnitTypeId.STALKER).idle: await self.do(s.attack(self.find_target(self.state)))
# 防卫模式:视野范围内存在敌人,开始攻击 if self.units(UnitTypeId.STALKER).amount>5: if len(self.known_enemy_units)>0: for s in self.units(UnitTypeId.STALKER).idle: await self.do(s.attack(random.choice(self.known_enemy_units)))
# 建造农民 asyncdefbuild_workers
(self): # 星灵枢钮*16(一个基地配备16个农民)大于农民数量并且现有农民数量小于MAX_WORKERS if len(self.units(UnitTypeId.NEXUS))*16>len(self.units(UnitTypeId.PROBE)) and len(self.units(UnitTypeId.PROBE))# 星灵枢纽(NEXUS)无队列建造,可以提高晶体矿的利用率,不至于占用资源 for nexus in self.units(UnitTypeId.NEXUS).ready.noqueue: # 是否有50晶体矿建造农民 if self.can_afford(UnitTypeId.PROBE): await self.do(nexus.train(UnitTypeId.PROBE))
## 建造水晶 asyncdefbuild_pylons(self): ## 供应人口和现有人口之差小于5且建筑不是正在建造 if self.supply_left < 5andnot self.already_pending(UnitTypeId.PYLON): nexuses = self.units(UnitTypeId.NEXUS).ready if nexuses.exists: if self.can_afford(UnitTypeId.PYLON): await self.build(UnitTypeId.PYLON, near=nexuses.first)
## 建造吸收厂 asyncdefbuild_assimilators(self): for nexus in self.units(UnitTypeId.NEXUS).ready: # 在瓦斯泉上建造吸收厂 vaspenes = self.state.vespene_geyser.closer_than(15.0,nexus) for vaspene in vaspenes: ifnot self.can_afford(UnitTypeId.ASSIMILATOR): break worker = self.select_build_worker(vaspene.position) if worker isNone: break ifnot self.units(UnitTypeId.ASSIMILATOR).closer_than(1.0,vaspene).exists: await self.do(worker.build(UnitTypeId.ASSIMILATOR,vaspene))
## 建造进攻性建筑 asyncdefoffensive_force_buildings(self): print(self.iteration / self.ITERATIONS_PER_MINUTE) if self.units(UnitTypeId.PYLON).ready.exists: pylon = self.units(UnitTypeId.PYLON).ready.random # 根据神族建筑科技图,折跃门建造过后才可以建造控制核心 if self.units(UnitTypeId.GATEWAY).ready.exists andnot self.units(UnitTypeId.CYBERNETICSCORE): if self.can_afford(UnitTypeId.CYBERNETICSCORE) andnot self.already_pending(UnitTypeId.CYBERNETICSCORE): await self.build(UnitTypeId.CYBERNETICSCORE, near=pylon) # 否则建造折跃门 # (self.iteration / self.ITERATIONS_PER_MINUTE)/2 是一个缓慢递增的值 elif len(self.units(UnitTypeId.GATEWAY)) < ((self.iteration / self.ITERATIONS_PER_MINUTE) / 2): if self.can_afford(UnitTypeId.GATEWAY) andnot self.already_pending(UnitTypeId.GATEWAY): await self.build(UnitTypeId.GATEWAY, near=pylon) # 控制核心存在的情况下建造星门 if self.units(UnitTypeId.CYBERNETICSCORE).ready.exists: if len(self.units(UnitTypeId.STARGATE)) < ((self.iteration / self.ITERATIONS_PER_MINUTE) / 2): if self.can_afford(UnitTypeId.STARGATE) andnot
self.already_pending(UnitTypeId.STARGATE): await self.build(UnitTypeId.STARGATE, near=pylon)
## 造兵 asyncdefbuild_offensive_force(self): # 无队列化建造 for gw in self.units(UnitTypeId.GATEWAY).ready.noqueue: ifnot self.units(UnitTypeId.STALKER).amount > self.units(UnitTypeId.VOIDRAY).amount:
if self.can_afford(UnitTypeId.STALKER) and self.supply_left > 0: await self.do(gw.train(UnitTypeId.STALKER))
for sg in self.units(UnitTypeId.STARGATE).ready.noqueue: if self.can_afford(UnitTypeId.VOIDRAY) and self.supply_left > 0: await self.do(sg.train(UnitTypeId.VOIDRAY))
## 进攻 asyncdefattack(self): # {UNIT: [n to fight, n to defend]} aggressive_units = {UnitTypeId.STALKER: [15, 5], UnitTypeId.VOIDRAY: [8, 3]}
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("AcidPlantLE"), [ Bot(Race.Protoss, SentdeBot()), Computer(Race.Terran, Difficulty.Hard) ], realtime=False)
## 造兵 asyncdefbuild_offensive_force(self): # 无队列化建造 # for gw in self.units(UnitTypeId.GATEWAY).ready.noqueue: # if not self.units(UnitTypeId.STALKER).amount > self.units(UnitTypeId.VOIDRAY).amount: # # if self.can_afford(UnitTypeId.STALKER) and self.supply_left > 0: # await self.do(gw.train(UnitTypeId.STALKER))
for sg in self.units(UnitTypeId.STARGATE).ready.noqueue: if self.can_afford(UnitTypeId.VOIDRAY) and self.supply_left > 0: await self.do(sg.train(UnitTypeId.VOIDRAY))
# 建造农民 asyncdefbuild_workers(self): # 星灵枢钮*16(一个基地配备16个农民)大于农民数量并且现有农民数量小于MAX_WORKERS if len(self.units(UnitTypeId.NEXUS)) * 16 > len(self.units(UnitTypeId.PROBE)) and len( self.units(UnitTypeId.PROBE)) < self.MAX_WORKERS: # 星灵枢纽(NEXUS)无队列建造,可以提高晶体矿的利用率,不至于占用资源 for nexus in self.units(UnitTypeId.NEXUS).ready.noqueue: # 是否有50晶体矿建造农民 if self.can_afford(UnitTypeId.PROBE): await self.do(nexus.train(UnitTypeId.PROBE))
## 建造水晶 asyncdefbuild_pylons(self): ## 供应人口和现有人口之差小于5且建筑不是正在建造 if self.supply_left < 5andnot self.already_pending(UnitTypeId.PYLON): nexuses = self.units(UnitTypeId.NEXUS).ready if nexuses.exists: if self.can_afford(UnitTypeId.PYLON): await self.build(UnitTypeId.PYLON, near=nexuses.first)
## 建造吸收厂 asyncdefbuild_assimilators(self): for nexus in self.units(UnitTypeId.NEXUS).ready: # 在瓦斯泉上建造吸收厂 vaspenes = self.state.vespene_geyser.closer_than(15.0, nexus) for vaspene in vaspenes: ifnot self.can_afford(UnitTypeId.ASSIMILATOR): break worker = self.select_build_worker(vaspene.position) if worker isNone: break ifnot self.units(UnitTypeId.ASSIMILATOR).closer_than(1.0, vaspene).exists: await self.do(worker.build(UnitTypeId.ASSIMILATOR, vaspene))
## 开矿 asyncdefexpand(self): # (self.iteration / self.ITERATIONS_PER_MINUTE)是一个缓慢递增的值,动态开矿 if self.units(UnitTypeId.NEXUS).amount < self.iteration / self.ITERATIONS_PER_MINUTE and self.can_afford( UnitTypeId.NEXUS): await self.expand_now()
## 建造进攻性建筑 asyncdefoffensive_force_buildings(self): print(self.iteration / self.ITERATIONS_PER_MINUTE) if self.units(UnitTypeId.PYLON).ready.exists: pylon = self.units(UnitTypeId.PYLON).ready.random # 根据神族建筑科技图,折跃门建造过后才可以建造控制核心 if self.units(UnitTypeId.GATEWAY).ready.exists andnot self.units(UnitTypeId.CYBERNETICSCORE): if self.can_afford(UnitTypeId.CYBERNETICSCORE) andnot self.already_pending(UnitTypeId.CYBERNETICSCORE): await self.build(UnitTypeId.CYBERNETICSCORE, near=pylon) # 否则建造折跃门 # (self.iteration / self.ITERATIONS_PER_MINUTE)/2 是一个缓慢递增的值 # elif len(self.units(UnitTypeId.GATEWAY)) < ((self.iteration / self.ITERATIONS_PER_MINUTE) / 2): elif len(self.units(UnitTypeId.GATEWAY)) < 1: if self.can_afford(UnitTypeId.GATEWAY) andnot self.already_pending(UnitTypeId.GATEWAY): await self.build(UnitTypeId.GATEWAY, near=pylon) # 控制核心存在的情况下建造星门 if self.units(UnitTypeId.CYBERNETICSCORE).ready.exists: if len(self.units(UnitTypeId.STARGATE)) < ((self.iteration / self.ITERATIONS_PER_MINUTE) / 2): if self.can_afford(UnitTypeId.STARGATE) andnot self.already_pending(UnitTypeId.STARGATE): await self.build(UnitTypeId.STARGATE, near=pylon)
## 造兵 asyncdefbuild_offensive_force(self): # 无队列化建造 for sg in self.units(UnitTypeId.STARGATE).ready.noqueue: if self.can_afford(UnitTypeId.VOIDRAY) and self.supply_left > 0: await self.do(sg.train(UnitTypeId.VOIDRAY))
## 进攻 asyncdefattack(self): # {UNIT: [n to fight, n to defend]} aggressive_units = {UnitTypeId.VOIDRAY: [8, 3]}
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)))
for unit_type in draw_dict: for unit in self.units(unit_type).ready: pos = unit.position cv2.circle(game_data, (int(pos[0]), int(pos[1])), draw_dict[unit_type][0], draw_dict[unit_type][1], -1)
存储三族的主基地名称(星灵枢纽,指挥中心,孵化场),刻画敌方建筑。
# 主基地名称 main_base_names = ["nexus", "supplydepot", "hatchery"] # 记录敌方基地位置 for enemy_building in self.known_enemy_structures: pos = enemy_building.position if enemy_building.name.lower() notin main_base_names: cv2.circle(game_data, (int(pos[0]), int(pos[1])), 5, (200, 50, 212), -1) for enemy_building in self.known_enemy_structures: pos = enemy_building.position if enemy_building.name.lower() in main_base_names: cv2.circle(game_data, (int(pos[0]), int(pos[1])), 15, (0, 0, 255), -1)
刻画敌方单位,如果是农民画得小些,其他单位则画大些。
for enemy_unit in self.known_enemy_units:
ifnot enemy_unit.is_structure: worker_names = ["probe", "scv", "drone"] # if that unit is a PROBE, SCV, or DRONE... it's a worker pos = enemy_unit.position if enemy_unit.name.lower() in worker_names: cv2.circle(game_data, (int(pos[0]), int(pos[1])), 1, (55, 0, 155), -1) else: cv2.circle(game_data, (int(pos[0]), int(pos[1])), 3, (50, 0, 215), -1)
在offensive_force_buildings(self)方法中添加建造机械台
if self.units(CYBERNETICSCORE).ready.exists: if len(self.units(ROBOTICSFACILITY)) < 1: if self.can_afford(ROBOTICSFACILITY) andnot self.already_pending(ROBOTICSFACILITY): await self.build(ROBOTICSFACILITY, near=pylon)
创建scout(),训练Observer
asyncdefscout(self): if len(self.units(OBSERVER)) > 0: scout = self.units(OBSERVER)[0] if scout.is_idle: enemy_location = self.enemy_start_locations[0] move_to = self.random_location_variance(enemy_location) print(move_to) await self.do(scout.move(move_to))
else: for rf in self.units(ROBOTICSFACILITY).ready.noqueue: if self.can_afford(OBSERVER) and self.supply_left > 0: await self.do(rf.train(OBSERVER))
defrandom_location_variance(self, enemy_start_location): x = enemy_start_location[0] y = enemy_start_location[1]
x += ((random.randrange(-20, 20))/100) * enemy_start_location[0] y += ((random.randrange(-20, 20))/100) * enemy_start_location[1]
if x < 0: x = 0 if y < 0: y = 0 if x > self.game_info.map_size[0]: x = self.game_info.map_size[0] if y > self.game_info.map_size[1]: y = self.game_info.map_size[1]
import sc2 from sc2 import run_game, maps, Race, Difficulty, position from sc2.player import Bot, Computer from sc2.constants import * import random import numpy as np import cv2
else: for rf in self.units(UnitTypeId.ROBOTICSFACILITY).ready.noqueue: if self.can_afford(UnitTypeId.OBSERVER) and self.supply_left > 0: await self.do(rf.train(UnitTypeId.OBSERVER))
for unit_type in draw_dict: for unit in self.units(unit_type).ready: pos = unit.position cv2.circle(game_data, (int(pos[0]), int(pos[1])), draw_dict[unit_type][0], draw_dict[unit_type][1], -1)
# 主基地名称 main_base_names = ["nexus", "supplydepot", "hatchery"] # 记录敌方基地位置 for enemy_building in self.known_enemy_structures: pos = enemy_building.position # 不是主基地建筑,画小一些 if enemy_building.name.lower() notin main_base_names: cv2.circle(game_data, (int(pos[0]), int(pos[1])), 5, (200, 50, 212), -1) for enemy_building in self.known_enemy_structures: pos = enemy_building.position if enemy_building.name.lower() in main_base_names: cv2.circle(game_data, (int(pos[0]), int(pos[1])), 15, (0, 0, 255), -1)
for enemy_unit in self.known_enemy_units:
ifnot enemy_unit.is_structure: worker_names = ["probe", "scv", "drone"] # if that unit is a PROBE, SCV, or DRONE... it's a worker pos = enemy_unit.position if enemy_unit.name.lower() in worker_names: cv2.circle(game_data, (int(pos[0]), int(pos[1])), 1, (55, 0, 155), -1) else: cv2.circle(game_data, (int(pos[0]), int(pos[1])), 3, (50, 0, 215), -1)
for obs in self.units(UnitTypeId.OBSERVER).ready: pos = obs.position cv2.circle(game_data, (int(pos[0]), int(pos[1])), 1, (255, 255, 255), -1)
# flip horizontally to make our final fix in visual representation: flipped = cv2.flip(game_data, 0) resized = cv2.resize(flipped, dsize=None, fx=2, fy=2)
cv2.imshow('Intel', resized) cv2.waitKey(1)
defrandom_location_variance(self, enemy_start_location): x = enemy_start_location[0] y = enemy_start_location[1]
x += ((random.randrange(-20, 20)) / 100) * enemy_start_location[0] y += ((random.randrange(-20, 20)) / 100) * enemy_start_location[1]
if x < 0:
x = 0 if y < 0: y = 0 if x > self.game_info.map_size[0]: x = self.game_info.map_size[0] if y > self.game_info.map_size[1]: y = self.game_info.map_size[1]
# 建造农民 asyncdefbuild_workers(self): # 星灵枢钮*16(一个基地配备16个农民)大于农民数量并且现有农民数量小于MAX_WORKERS if len(self.units(UnitTypeId.NEXUS)) * 16 > len(self.units(UnitTypeId.PROBE)) and len( self.units(UnitTypeId.PROBE)) < self.MAX_WORKERS: # 星灵枢纽(NEXUS)无队列建造,可以提高晶体矿的利用率,不至于占用资源 for nexus in self.units(UnitTypeId.NEXUS).ready.noqueue: # 是否有50晶体矿建造农民 if self.can_afford(UnitTypeId.PROBE): await self.do(nexus.train(UnitTypeId.PROBE))
## 建造水晶 asyncdefbuild_pylons(self): ## 供应人口和现有人口之差小于5且建筑不是正在建造 if self.supply_left < 5andnot self.already_pending(UnitTypeId.PYLON): nexuses = self.units(UnitTypeId.NEXUS).ready if nexuses.exists: if self.can_afford(UnitTypeId.PYLON): await self.build(UnitTypeId.PYLON, near=nexuses.first)
## 建造吸收厂 asyncdefbuild_assimilators(self): for nexus in self.units(UnitTypeId.NEXUS).ready: # 在瓦斯泉上建造吸收厂 vaspenes = self.state.vespene_geyser.closer_than(15.0, nexus) for vaspene in vaspenes: ifnot self.can_afford(UnitTypeId.ASSIMILATOR): break worker = self.select_build_worker(vaspene.position) if worker isNone: break ifnot self.units(UnitTypeId.ASSIMILATOR).closer_than(1.0, vaspene).exists: await self.do(worker.build(UnitTypeId.ASSIMILATOR, vaspene))
## 开矿 asyncdefexpand(self): # (self.iteration / self.ITERATIONS_PER_MINUTE)是一个缓慢递增的值,动态开矿 if self.units(UnitTypeId.NEXUS).amount < self.iteration / self.ITERATIONS_PER_MINUTE and self.can_afford( UnitTypeId.NEXUS): await self.expand_now()
## 建造进攻性建筑 asyncdefoffensive_force_buildings(self): print(self.iteration / self.ITERATIONS_PER_MINUTE) if self.units(UnitTypeId.PYLON).ready.exists: pylon = self.units(UnitTypeId.PYLON).ready.random # 根据神族建筑科技图,折跃门建造过后才可以建造控制核心 if self.units(UnitTypeId.GATEWAY).ready.exists andnot self.units(UnitTypeId.CYBERNETICSCORE): if self.can_afford(UnitTypeId.CYBERNETICSCORE) andnot self.already_pending(UnitTypeId.CYBERNETICSCORE): await self.build(UnitTypeId.CYBERNETICSCORE, near=pylon) # 否则建造折跃门
# (self.iteration / self.ITERATIONS_PER_MINUTE)/2 是一个缓慢递增的值 # elif len(self.units(UnitTypeId.GATEWAY)) < ((self.iteration / self.ITERATIONS_PER_MINUTE) / 2): elif len(self.units(UnitTypeId.GATEWAY)) < 1: if self.can_afford(UnitTypeId.GATEWAY) andnot self.already_pending(UnitTypeId.GATEWAY): await self.build(UnitTypeId.GATEWAY, near=pylon) # 控制核心存在的情况下建造机械台 if self.units(UnitTypeId.CYBERNETICSCORE).ready.exists: if len(self.units(UnitTypeId.ROBOTICSFACILITY)) < 1: if self.can_afford(UnitTypeId.ROBOTICSFACILITY) andnot self.already_pending( UnitTypeId.ROBOTICSFACILITY): await self.build(UnitTypeId.ROBOTICSFACILITY, near=pylon)
# 控制核心存在的情况下建造星门 if self.units(UnitTypeId.CYBERNETICSCORE).ready.exists: if len(self.units(UnitTypeId.STARGATE)) < ((self.iteration / self.ITERATIONS_PER_MINUTE) / 2): if self.can_afford(UnitTypeId.STARGATE) andnot self.already_pending(UnitTypeId.STARGATE): await self.build(UnitTypeId.STARGATE, near=pylon)
## 造兵 asyncdefbuild_offensive_force(self): # 无队列化建造 # for gw in self.units(UnitTypeId.GATEWAY).ready.noqueue: # if not self.units(UnitTypeId.STALKER).amount > self.units(UnitTypeId.VOIDRAY).amount: # # if self.can_afford(UnitTypeId.STALKER) and self.supply_left > 0: # await self.do(gw.train(UnitTypeId.STALKER))
for sg in self.units(UnitTypeId.STARGATE).ready.noqueue: if self.can_afford(UnitTypeId.VOIDRAY) and self.supply_left > 0: await self.do(sg.train(UnitTypeId.VOIDRAY))
## 进攻 asyncdefattack(self): # {UNIT: [n to fight, n to defend]} aggressive_units = {UnitTypeId.VOIDRAY: [8, 3]}
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)))