def random_target(self, obs): minimap_width = len(obs.observation[MINIMAP][0]) minimap_height = len(obs.observation[MINIMAP][0][0]) loc = Point() loc.x = np.random.randint(int(self.width(obs) / 2), minimap_width - int(self.width(obs) / 2)) loc.y = np.random.randint(int(self.height(obs) / 2), minimap_height - int(self.height(obs) / 2)) return loc
def get_closest_enemy(obs, shared, loc): player_relative = obs.observation[MINIMAP][MINI_PLAYER_RELATIVE] hostile_y, hostile_x = (player_relative == PLAYER_HOSTILE).nonzero() closest, min_dist = None, None for h in zip(hostile_x, hostile_y): dist = loc.distance(Point(h[0], h[1])) if not closest or dist < min_dist: closest = Point(h[0], h[1]) min_dist = dist return closest
def _get_new_candidates_from_minimap(self, obs, shared): friendly_fixed_points = [] for x in range(shared['minimap'].width(obs)): for y in range(shared['minimap'].height(obs)): if self._blacklist_map[x, y] != 0 or self._idle_units_map[x, y] != 0: continue is_idle = True for i in range(IdleTracker._MINIMAP_IDLE_STEPS): if not self._last_obs[i]: return [] if self._last_obs[i].observation[MINIMAP][ MINI_PLAYER_RELATIVE][y, x] != PLAYER_SELF: is_idle = False break if is_idle: friendly_fixed_points.append( Unit(Location(minimap_loc=Point(x, y)))) return friendly_fixed_points
def get_safe_screen_location(obs, shared, unit_point, influence_map): safe_x, safe_y = [], [] safe_coeff = 0 while len(safe_x) == 0: safe_x, safe_y = (influence_map == safe_coeff).nonzero() safe_coeff += 1 best_loc, min_dist = None, None for loc in zip(safe_x, safe_y): dist = unit_point.distance(Point(loc[0], loc[1])) if not best_loc or dist < min_dist: best_loc = Point(loc[0], loc[1]) min_dist = dist return best_loc
def location(self, obs, shared): if not self._location_timestamp or self._location_timestamp != shared[ 'env'].timestamp: mini_camera = obs.observation[MINIMAP][MINI_CAMERA] camera_y, camera_x = (mini_camera == 1).nonzero() self._location = Point(round(sum(camera_x) / len(camera_x)), round(sum(camera_y) / len(camera_y))) self._location_timestamp = shared['env'].timestamp return self._location
def get_random_enemy_location(obs): player_relative = obs.observation[MINIMAP][MINI_PLAYER_RELATIVE] hostile_y, hostile_x = (player_relative == PLAYER_HOSTILE).nonzero() if len(hostile_x) == 0: return None else: hostile = list(zip(hostile_x, hostile_y)) selected = random.choice(hostile) return Point(selected[0], selected[1])
def _update_idle_units_map(self, obs, shared): cur_minimap = obs.observation[MINIMAP][MINI_PLAYER_RELATIVE] for x in range(shared['minimap'].width(obs)): for y in range(shared['minimap'].height(obs)): if cur_minimap[y, x] != PLAYER_SELF \ and self._idle_units_map[x, y] != 0: self._idle_units_map[x, y] = 0 for u in self._idle_units: if u.location.minimap.equals(Point(x, y)): self._idle_units.remove(u)
def iterate(self, obs, camera_loc=None): """ Generates camera pixels location """ mini_camera = obs.observation[MINIMAP][MINI_CAMERA] if not camera_loc: camera_y, camera_x = (mini_camera == 1).nonzero() min_x, max_x = min(camera_x), max(camera_x) min_y, max_y = min(camera_y), max(camera_y) for y in range(min_y, max_y + 1): for x in range(min_x, max_x + 1): yield (Point(x, y)) else: width = self.width(obs) height = self.height(obs) for y in range(int(camera_loc.y - height / 2), int(camera_loc.y + height / 2) + 1): for x in range(int(camera_loc.x - width / 2), int(camera_loc.x + width / 2) + 1): yield (Point(x, y))
def get_micro_management_location(obs, shared): _MICRO_DISTANCE = 6 player_relative = obs.observation[MINIMAP][MINI_PLAYER_RELATIVE] locations = [] for y in range(shared['minimap'].height(obs)): for x in range(shared['minimap'].width(obs)): if player_relative[y, x] == PLAYER_SELF: p = Point(x, y) if _is_close_to_enemy(obs, shared, p, _MICRO_DISTANCE): locations.append(p) return locations
def get_enemy_influence_map(obs, shared): _ENEMY_DISTANCE_DANGER = 25 influence_map = np.zeros( (shared['screen'].width(obs), shared['screen'].height(obs))) enemies = shared['screen'].scan_units(obs, shared, list(TERRAN_UNITS), PLAYER_HOSTILE) for y in range(shared['screen'].height(obs)): for x in range(shared['screen'].width(obs)): for e in enemies: if Point(x, y).distance( e.location.screen) <= _ENEMY_DISTANCE_DANGER: influence_map[x, y] += 1 return influence_map
def random_point(obs): loc = Point() loc.x = np.random.randint(0, Minimap.width(obs)) loc.y = np.random.randint(0, Minimap.height(obs)) return loc
def __init__(self): self.camera = Point()
def scan(self, obs, shared): """ Returns a list of the units on screen """ if not self._scanned_units_timestamp or self._scanned_units_timestamp != shared[ 'env'].timestamp: def _explore_contiguous(x, y, unit_id, player_relative, scanned): """ Explores contiguous pixels corresponding to the same unit and returns approximate center of the unit. """ def _try_explore(x, y, queue, scanned): if x < screen_width and y < screen_height \ and scanned[x, y] == 0 \ and obs.observation[SCREEN][SCREEN_PLAYER_RELATIVE][x, y] == player_relative \ and obs.observation[SCREEN][SCREEN_UNIT_TYPE][x, y] == unit_id: queue.append((x, y)) scanned[x, y] = 1 return True else: return False sum_x, sum_y, nb_scanned = x, y, 1 queue = [] queue.append((x, y)) while len(queue) > 0: cur = queue.pop() if _try_explore(cur[0] + 1, cur[1], queue, scanned): sum_x += cur[0] + 1 sum_y += cur[1] nb_scanned += 1 if _try_explore(cur[0], cur[1] + 1, queue, scanned): sum_x += cur[0] sum_y += cur[1] + 1 nb_scanned += 1 if _try_explore(cur[0], cur[1] - 1, queue, scanned): sum_x += cur[0] sum_y += cur[1] - 1 nb_scanned += 1 return round(sum_x / nb_scanned), round(sum_y / nb_scanned) screen_width = Screen.width(obs) screen_height = Screen.height(obs) scanned = np.zeros((screen_width, screen_height)) self._scanned_units = [] for x in range(screen_width): for y in range(screen_height): if scanned[x, y] == 0 and obs.observation[SCREEN][ SCREEN_UNIT_TYPE][x, y] != 0: center_x, center_y = _explore_contiguous( x, y, obs.observation[SCREEN][SCREEN_UNIT_TYPE][x, y], obs.observation[SCREEN][SCREEN_PLAYER_RELATIVE][x, y], scanned) self._scanned_units.append( Unit( Location(screen_loc=Point(center_x, center_y)), obs.observation[SCREEN][SCREEN_UNIT_TYPE][x, y], obs.observation[SCREEN][SCREEN_PLAYER_RELATIVE] [x, y])) self._scanned_units_timestamp = shared['env'].timestamp return self._scanned_units
def random_point(obs, margin=0): loc = Point() loc.x = np.random.randint(margin, Screen.width(obs) - margin - 1) loc.y = np.random.randint(margin, Screen.height(obs) - margin - 1) return loc