def move(state, robot): """ Moves a robot to a nearby goal sector that avoiding hedges """ x = robot.location.x y = robot.location.y sector_to_move_to = state.map.sector_at(robot.location) H = state.map.height W = state.map.width for dx in [-5, 0 ,5]: for dy in [-5, 0, 5]: if(not (0 <= x +dx < W and 0<= y + dy <H)): continue point_to_check = battlecode.Location(x + dy, y+dy) that_sector = state.map.sector_at(point_to_check) if(that_sector.team.id == state.my_team_id): continue center = get_centroid(sector_to_move_to) ents = state.get_entities(location = battlecode.Location((x + dx/5, y+dy/5))) if(len(ents) > 0): #other ents in location to move? continue dir_to_move = robot.location.direction_to(center) robot.queue_move(dir_to_move) return
def moveIntoEnemyVicinity(state, self, deltaX, deltaY): """ check if walking into enemy vicinity :param state: :param self: :param deltaX: :param deltaY: :return: """ currentX, currentY = self.location.x, self.location.y targetX = currentX + deltaX targetY = currentY + deltaY for dx in [-1, 0, 1]: for dy in [-1, 0, 1]: testLocation = battlecode.Location(int(targetX + dx), int(targetY + dy)) if not state.map.location_on_map(testLocation): continue if list( state.get_entities(self, entity_type='thrower', location=testLocation, team=state.other_team)): return True return False
def move(state, robot): """ Moves a robot to a nearby goal sector that avoiding hedges """ x = robot.location.x y = robot.location.y sector_to_move_to = state.map.sector_at(robot.location) H = state.map.height W = state.map.width for dx in [-5, 0, 5]: for dy in [-5, 0, 5]: if (not (0 <= x + dx < W and 0 <= y + dy < H)): continue point_to_check = battlecode.Location(x + dx, y + dy) that_sector = state.map.sector_at(point_to_check) if (that_sector.team.id == state.my_team_id): continue sector_to_move_to = that_sector center = get_centroid(sector_to_move_to) if (robot.location == center): return dir_to_move = robot.location.direction_to(center) if not robot.can_move(dir_to_move): continue robot.queue_move(dir_to_move) return
def is_throw_path_valid(state, source, target): # Checks if the path between source and target is clear of hedges and target is on a throwable trajectory direction = source.location.direction_to(target.location) dx = target.location.x - source.location.x dy = target.location.y - source.location.y if (abs(dx) != abs(dy) and dx != 0 and dy != 0): return False initial = source.location for i in range(max(abs(dx), abs(dy))): interm_loc = battlecode.Location(initial.x + i * direction.dx, initial.y + i * direction.dy) on_map = state.map.location_on_map(interm_loc) if (not on_map): return False is_occupied = len( list( state.get_entities(location=interm_loc, entity_type=battlecode.Entity.HEDGE))) > 0 if is_occupied: return False return True
def get_dirt_tiles(startLoc, tiles): """ Returns a list of sorted Location objects in terms of startLoc. Far away first, closer last (so reverse order). """ dirt_tiles = [] for j in range(len(tiles)): row = tiles[j] for i in range(len(tiles[0])): elem = row[i] if elem == 'D': dirt_tiles.append(battlecode.Location(i,j)) dirt_tiles = sorted(dirt_tiles, key=lambda x: startLoc.distance_to(x), reverse=True) return dirt_tiles # @staticmethod # def throw(thrower, targetLoc): # """ # This function performs the art of throwing an ally to a target # location targetLoc. # """ # thrower_loc = thrower.location # throw_dir = thrower_loc.direction_to(targetLoc) # ## Has unit on its back, no cooldowns to throw # if thrower.can_throw(throw_dir): # queue_throw(throw_dir) # return True # else: # return False
def find_closest_goals(state, robot, all_sectors): """ :param state: :param robot: :param all_sectors: :return: Location object """ min_loc = (0, 0) min_dist = 500000 for s in all_sectors: if (robot.location.distance_to( battlecode.Location(s.top_left[0] + 2, s.top_left[1] - 2)) < min_dist): min_loc = (s.top_left[0] + 2, s.top_left[1] - 2) min_dist = robot.location.distance_to( battlecode.Location(s.top_left[0] + 2, s.top_left[1] - 2)) return battlecode.Location(min_loc)
def is_throw_path_valid(state, source, target): # Checks if the path between source and target is clear of hedges and target is on a throwable trajectory dx = target.location.x - source.location.x dy = target.location.y - source.location.y if not (abs(dx) == abs(dy) or dx == 0 or dy == 0): return False if abs(dx) == abs(dy): # aligned along diagonal for i in range(dx): obstacles = list( state.get_entities( location=battlecode.Location(source.location.x + i + 1, source.location.y + i + 1))) if len(obstacles ) > 0 and obstacles[0].type == battlecode.Entity.HEDGE: return False return True if dx == 0: # aligned along diagonal for i in range(dx): obstacles = list( state.get_entities(location=battlecode.Location( source.location.x, source.location.y + i + 1))) if len(obstacles ) > 0 and obstacles[0].type == battlecode.Entity.HEDGE: return False return True if dy == 0: # aligned along diagonal for i in range(dx): obstacles = list( state.get_entities( location=battlecode.Location(source.location.x + i + 1, source.location.y))) if len(obstacles ) > 0 and obstacles[0].type == battlecode.Entity.HEDGE: return False return True
def get_goal_sectors(state): """ faskf """ all_sectors = [] for x in range(0, state.map.width, 5): for y in range(0, state.map.height, 5): this_sector = state.map.sector_at(battlecode.Location(x, y)) if (not state.my_team.id == this_sector.team.id): all_sectors.append(this_sector) #all sectors has non allied sectors return all_sectors
def get_fraction_sectors(state, team): num_sectors = 0 total_sectors = 0 map_height = state.map.height map_width = state.map.width for x in range(int(map_width / 5)): for y in range(int(map_height / 5)): sector = state.map.sector_at(battlecode.Location(5 * x, 5 * y)) total_sectors += 1 if sector.team == team: num_sectors += 1 return float(num_sectors) / float(total_sectors)
def get_closest_cell(map, my_location, sector): min_x = sector.top_left.x max_x = min_x + map.sector_size - 1 min_y = sector.top_left.y max_y = min_y + map.sector_size - 1 if my_location.x <= min_x: loc_x = min_x elif my_location.x >= max_x: loc_x = max_x else: loc_x = my_location.x if my_location.y <= min_y: loc_y = min_y elif my_location.y >= max_y: loc_y = max_y else: loc_y = my_location.y return battlecode.Location(loc_x, loc_y)
statue_list = [] for enemy in state.get_entities(team=state.other_team): if enemy.is_statue: statue_list.append(enemy) # Your Code will run within this loop for entity in state.get_entities(entity_type='thrower', team=state.my_team): # This line gets all the bots on your team robot_class = our_bots[entity.id] if our_bots[entity.id].return_role()[0] == None: print("henloooo") target = battlecode.Location(10, 10) if our_bots[entity.id].cooldown_time == 0: our_bots[entity.id].update_role('build', target) # our_bots[entity.id].cooldown_time = 10 robot_class.brain() # if(state.turn % 10 == 0): # for direction in battlecode.Direction.directions(): # if entity.can_build(direction): # entity.queue_build(direction) # # my_location = entity.location # near_entites = entity.entities_within_euclidean_distance(1.9) # near_entites = list(filter(lambda x: x.can_be_picked, near_entites)) #
## Run it down mid mode elif frac_my_sectors > 0.3: role = 'offense' else: role = "idle" #default # CARRY OUT ROLES if role == "offense": cum_x = 0 cum_y = 0 for enemy in enemies: enemy_location = enemy.location cum_x += enemy_location.x cum_y += enemy_location.y enemy_length = len(enemies) average_location = battlecode.Location(int(cum_x/enemy_length),int(cum_y/enemy_length)) if average_location != my_location: average_direction = my_location.direction_to(average_location) if entity.can_move(average_direction): entity.queue_move(average_direction) role = "idle" if role == "defend": carrying = prep_stance(entity, 'defend', state) if carrying >= 0: defended = stance(entity, 'defend', state, enemies) role = "idle" if role == "explore":# If thrower, tries to attack for direction in battlecode.Direction.directions():
def killStatues(state, brawl): enemyStatues = list( state.get_entities(entity_type='statue', team=state.other_team)) for enemyStatue in enemyStatues: enemyX = enemyStatue.location.x enemyY = enemyStatue.location.y moved = False throwed = False tempLock = set() d = [-1, 0, 1] random.shuffle(d) for dx in d: if moved: break for dy in d: breakK = 2 if moved: break if dx == 0 and dy == 0: continue #special handle for k = 1 pickedLocation = battlecode.Location(enemyX + dx, enemyY + dy) throwLocation = battlecode.Location(enemyX + dx * 2, enemyY + dy * 2) if state.map.location_on_map( pickedLocation) and state.map.location_on_map( throwLocation): if brawl: pickedEntity = list( state.get_entities(location=pickedLocation)) else: pickedEntity = list( state.get_entities(location=pickedLocation, team=state.other_team)) if pickedEntity: if pickedEntity[0].can_be_picked: pickedEntity = pickedEntity[0] throwEntity = list( state.get_entities(location=throwLocation)) if throwEntity: throwEntity = throwEntity[0] if throwEntity.team != state.my_team or not throwEntity.is_thrower: break if throwEntity.can_pickup(pickedEntity): throwEntity.queue_pickup(pickedEntity) tempLock.add(throwEntity.id) moved = True if throwEntity.can_throw( battlecode.Direction(-dx, -dy)): throwEntity.queue_throw( battlecode.Direction(-dx, -dy)) throwed = True break else: break for k in range(2, 8): #special check for picking up the entity in line checkLocation = battlecode.Location( enemyX + dx * k, enemyY + dy * k) if state.map.location_on_map(checkLocation): ownEntity = list( state.get_entities(location=checkLocation)) if not ownEntity: continue ownEntity = ownEntity[0] if ownEntity.team != state.my_team or not ownEntity.is_thrower: break if ownEntity.is_holding: if ownEntity.can_throw( battlecode.Direction(-dx, -dy)): ownEntity.queue_throw( battlecode.Direction(-dx, -dy)) moved = True throwed = True break pickupCandidates = ownEntity.entities_within_euclidean_distance( distance=1.9) for pickup in pickupCandidates: if not brawl: if pickup.team == state.my_team: continue if ownEntity.can_pickup(pickup): ownEntity.queue_pickup(pickup) moved = True if ownEntity.can_throw( battlecode.Direction(-dx, -dy)): ownEntity.queue_throw( battlecode.Direction(-dx, -dy)) throwed = True tempLock.add(ownEntity.id) break if moved: break if moved and not throwed: entityList = list( enemyStatue.entities_within_adjacent_distance(breakK * 1.5)) tempLock.add(ent.id for ent in entityList) lockedTargets.union(tempLock)
def get_centroid(sector): """given a sector returns its centroid as a location""" tl = sector.top_left return battlecode.Location(tl[0] + 2, tl[1] - 2)
# Your Code will run within this loop for entity in state.get_entities(entity_type='thrower', team=state.my_team): # This line gets all the bots on your team robot_class = our_bots[entity.id] if our_bots[entity.id].return_role()[0] == None: #TODO: ELaine's random location generator part 2, switch with McCoy's location generator target_coord = (random.randrange(1, max_x - 1), random.randrange(1, max_y - 1)) if target_coord not in hedges_loc and target_coord != our_bots[ entity.id].loc: target = battlecode.Location(target_coord) if our_bots[entity.id].cooldown_time == 0: our_bots[entity.id].update_role('build', target) # our_bots[entity.id].cooldown_time = 10 robot_class.brain() # if(state.turn % 10 == 0): # for direction in battlecode.Direction.directions(): # if entity.can_build(direction): # entity.queue_build(direction) # # my_location = entity.location # near_entites = entity.entities_within_euclidean_distance(1.9) # near_entites = list(filter(lambda x: x.can_be_picked, near_entites)) #
# If not building this turn if not is_building: unowned_sectors = [] for top_left in state.map._sectors: sector = state.map.sector_at(top_left) if sector.team != state.my_team: unowned_sectors.append(sector) # Initialize max tracking variables closest_center = None closest_sector_index = -1 dist_closest_sector = np.inf for i in range(len(unoccupied_sectors)): s = unoccupied_sectors[i] center = battlecode.Location(s.top_left.x + 2, s.top_left.y + 2) is_closer = my_location.distance_to( center) < dist_closest_sector is_not_capped = sector_assignments[i] <= cap_assignments if is_closer and is_not_capped: closest_center = center closest_sector_index = i dist_closest_sector = my_location.distance_to(center) if closest_center is not None: opt_direction = my_location.direction_to(closest_center) if entity.can_move(opt_direction): sector_assignments[closest_sector_index] += 1 entity.queue_move(opt_direction) continue