Ejemplo n.º 1
0
 def act(dc):
     tech = self._tech_tree.getUnitData(type_id)
     pos = self._placer.get_building_position(type_id, dc)
     if pos == None: return []
     extractor_tags = set(
         u.tag
         for u in dc.units_of_type(UNIT_TYPE.ZERG_EXTRACTOR.value))
     builders = dc.units_of_types(tech.whatBuilds)
     prefered_builders = [
         u for u in builders
         if (u.unit_type != UNIT_TYPE.ZERG_DRONE.value
             or len(u.orders) == 0 or
             (u.orders[0].ability_id == ABILITY.HARVEST_GATHER_DRONE.
              value and u.orders[0].target_tag not in extractor_tags))
     ]
     if len(prefered_builders) > 0:
         builder = utils.closest_unit(pos, prefered_builders)
     else:
         if len(builders) == 0: return []
         builder = utils.closest_unit(pos, builders)
     action = sc_pb.Action()
     action.action_raw.unit_command.unit_tags.append(builder.tag)
     action.action_raw.unit_command.ability_id = tech.buildAbility
     if isinstance(pos, Unit):
         action.action_raw.unit_command.target_unit_tag = pos.tag
     else:
         action.action_raw.unit_command.target_world_space_pos.x = pos[
             0]
         action.action_raw.unit_command.target_world_space_pos.y = pos[
             1]
     return [action]
Ejemplo n.º 2
0
 def prioritized_attack(unit, target_units):
     assert len(target_units) > 0
     prioritized_target_units = [
         u for u in target_units if u.unit_type in PRIORITIZED_ATTACK
     ]
     if len(prioritized_target_units) > 0:
         closest_target = utils.closest_unit(unit,
                                             prioritized_target_units)
     else:
         closest_target = utils.closest_unit(unit, target_units)
     target_pos = (closest_target.float_attr.pos_x,
                   closest_target.float_attr.pos_y)
     return self._unit_attack(unit, target_pos, dc)
Ejemplo n.º 3
0
 def _all_idle_workers_gather_minerals(self, dc):
     idle_workers = dc.idle_units_of_type(UNIT_TYPE.ZERG_DRONE.value)
     actions = []
     for worker in idle_workers:
         mineral = utils.closest_unit(worker, dc.minerals)
         action = sc_pb.Action()
         action.action_raw.unit_command.unit_tags.append(worker.tag)
         action.action_raw.unit_command.ability_id = \
             ABILITY.HARVEST_GATHER_DRONE.value
         action.action_raw.unit_command.target_unit_tag = mineral.tag
         actions.append(action)
     return actions
Ejemplo n.º 4
0
 def _next_base_place(self, dc):
     unexploited_minerals = dc.unexploited_minerals
     if len(unexploited_minerals) == 0: return None
     mineral_to_exploit = utils.closest_unit(dc.init_base_pos,
                                             unexploited_minerals)
     resources_nearby = utils.units_nearby(mineral_to_exploit,
                                           dc.minerals + dc.gas,
                                           max_distance=14)
     x_list = [u.float_attr.pos_x for u in resources_nearby]
     y_list = [u.float_attr.pos_y for u in resources_nearby]
     x_mean = sum(x_list) / len(x_list)
     y_mean = sum(y_list) / len(y_list)
     left = int(math.floor(min(x_list)))
     right = int(math.ceil(max(x_list)))
     bottom = int(math.floor(min(y_list)))
     top = int(math.ceil(max(y_list)))
     width = right - left + 1
     height = top - bottom + 1
     x_offset, y_offset = 0, 0
     if height - width >= 5:
         left_mid = (left, (bottom + top) / 2)
         right_mid = (right, (bottom + top) / 2)
         if utils.closest_distance(left_mid, resources_nearby) > \
             utils.closest_distance(right_mid, resources_nearby):
             x_offset = width - height + 1
         width = height - 1
     elif height - width <= -5:
         top_mid = ((left + right) / 2, top)
         bottom_mid = ((left + right) / 2, bottom)
         if utils.closest_distance(top_mid, resources_nearby) < \
             utils.closest_distance(bottom_mid, resources_nearby):
             y_offset = height - width + 1
         height = width - 1
     region = [left + x_offset, bottom + y_offset, width, height]
     place = self._search_place(region, dc, margin=5.5, shrink_mineral=True)
     return utils.closest_unit((x_mean, y_mean), place) \
         if len(place) > 0 else None
Ejemplo n.º 5
0
 def flee_or_fight(unit, target_units):
     assert len(target_units) > 0
     closest_target = utils.closest_unit(unit, target_units)
     closest_dist = utils.closest_distance(unit, enemy_units)
     strongest_health = utils.strongest_health(combat_units)
     if (closest_dist < 5.0 and
             unit.float_attr.health / unit.float_attr.health_max < 0.3
             and strongest_health > 0.9):
         x = unit.float_attr.pos_x + (unit.float_attr.pos_x - \
             closest_target.float_attr.pos_x) * 0.2
         y = unit.float_attr.pos_y + (unit.float_attr.pos_y - \
             closest_target.float_attr.pos_y) * 0.2
         target_pos = (x, y)
         return self._unit_move(unit, target_pos, dc)
     else:
         target_pos = (closest_target.float_attr.pos_x,
                       closest_target.float_attr.pos_y)
         return self._unit_attack(unit, target_pos, dc)
Ejemplo n.º 6
0
 def _assign_workers_gather_minerals(self, dc):
     extractor_tags = set(
         u.tag for u in dc.units_of_type(UNIT_TYPE.ZERG_EXTRACTOR.value))
     workers = [
         u for u in dc.units_of_type(UNIT_TYPE.ZERG_DRONE.value)
         if (len(u.orders) == 0 or (
             u.orders[0].ability_id == ABILITY.HARVEST_GATHER_DRONE.value
             and u.orders[0].target_tag in extractor_tags))
     ]
     actions = []
     for worker in random.sample(workers, min(3, len(workers))):
         target_mineral = utils.closest_unit(worker, dc.minerals)
         action = sc_pb.Action()
         action.action_raw.unit_command.unit_tags.append(worker.tag)
         action.action_raw.unit_command.ability_id = \
             ABILITY.HARVEST_GATHER_DRONE.value
         action.action_raw.unit_command.target_unit_tag = target_mineral.tag
         actions.append(action)
     return actions
Ejemplo n.º 7
0
 def _all_idle_queens_inject_larva(self, dc):
     injectable_queens = [
         # TODO: -->idle_units_of_type
         u for u in dc.units_of_type(UNIT_TYPE.ZERG_QUEEN.value)
         if u.float_attr.energy >= 25
     ]
     bases = dc.mature_units_of_types([
         UNIT_TYPE.ZERG_HATCHERY.value, UNIT_TYPE.ZERG_LAIR.value,
         UNIT_TYPE.ZERG_HIVE.value
     ])
     actions = []
     for queen in injectable_queens:
         action = sc_pb.Action()
         action.action_raw.unit_command.unit_tags.append(queen.tag)
         action.action_raw.unit_command.ability_id = \
             ABILITY.EFFECT_INJECTLARVA.value
         base = utils.closest_unit(queen, bases)
         action.action_raw.unit_command.target_unit_tag = base.tag
         actions.append(action)
     return actions