redlight.update(squares)

    moves = set()
    destinations = defaultdict(int)
    originations = defaultdict(list)
    interior_strengths = [
        square.strength for square in game_map if square.owner == myID
        and min(game_map.neighbors(square),
                key=lambda x: degrade_potential(*pf_map[x])).owner == myID
    ]
    interior_strengths.sort(reverse=True)
    percentile = (1 - len(interior_strengths) /
                  (50 * 50)) * (args.int_max - args.int_min) + args.int_min
    strength_hurdle = interior_strengths[int(
        len(interior_strengths) * percentile)] if interior_strengths else 0
    for square in sorted(
        (square for square in game_map
         if square.owner == myID and square.strength > 0),
            key=lambda x: (x.strength, -pf_map[x][1]),
            reverse=True):  #when tied strength, move closest first
        move = assign_move(square)
        moves.add(move)
        target = game_map.get_target(square, move.direction)
        destinations[target] += square.strength + (
            square.production if move.direction == STILL else 0)
        originations[target].append(
            (hlt.opposite_cardinal(move.direction), square))
    hlt.send_frame(moves)
    logging.debug(
        str(turn) + ' :: ' + str(int(1000 * (time.time() - start_time))))
    game_map.get_frame()
    frontier = [(initial_potential(square), random.random(), initial_potential(square), 0, square) for square in game_map if square.owner != myID]
    pf_map = dict()
    heapq.heapify(frontier)
    while len(pf_map) < game_map.width * game_map.height:
        _, _, square_potential, friendly_distance, square = heapq.heappop(frontier)
        if square not in pf_map:
            pf_map[square] = (square_potential, friendly_distance)
            for neighbor in game_map.neighbors(square):
                if neighbor.owner != myID:
                    neighbor_potential  = (1 - args.alpha) * square_potential + args.alpha * neighbor.strength / neighbor.production if neighbor.production and neighbor.owner == 0 else float('inf')
                    heapq.heappush(frontier, (neighbor_potential, random.random(), neighbor_potential, friendly_distance, neighbor))
                else:
                    neighbor_potential  = degrade_potential(square_potential, friendly_distance + 1)
                    heapq.heappush(frontier, (neighbor_potential, random.random(), square_potential, friendly_distance + 1, neighbor))
    moves = set()
    destinations = defaultdict(int)
    originations = defaultdict(list)
    interior_strengths = [square.strength for square in game_map if square.owner == myID and min(game_map.neighbors(square), key=lambda x: degrade_potential(*pf_map[x])).owner == myID]
    interior_strengths.sort(reverse=True)
    percentile = (1 - len(interior_strengths) / (50 * 50)) * (args.int_max - args.int_min) + args.int_min
    strength_hurdle = interior_strengths[int(len(interior_strengths) * percentile)] if interior_strengths else 0
    for square in sorted((square for square in game_map if square.owner == myID and square.strength > 0), key=lambda x: (x.strength, -pf_map[square][1]), reverse=True):  #when tied strength, move closest first
        move = assign_move(square)
        moves.add(move)
        target = game_map.get_target(square, move.direction)
        destinations[target] += square.strength
        originations[target].append((hlt.opposite_cardinal(move.direction), square))
    hlt.send_frame(moves)
    logging.debug(str(turn) + ' :: ' + str(int(1000 * (time.time() - start_time))))
Beispiel #3
0
def assign_move(square):
    #Full strength squares should head to nearest border
    if square.strength == 255:
        if fne(square) == 12:
            return fnb(square)
        else:
            return fne(square)

    total = 0
    #Go through all neighbours
    for direction, neighbor in enumerate(game_map.neighbors(square)):

        c = True
        total += neighbor.strength
        if neighbor.owner != myID:
            #The square is not surrounded by its own team
            c = False

        #If it is next to a friendly neighbour and they are both relatively weak, try merge
        if neighbor.owner == myID and (
                square.strength < 50 and square.strength > 10) and (
                    neighbor.strength < 50
                    and neighbor.strength > square.production):
            #remain still if the production is higher
            #make sure they do not waste too much:
            if neighbor.strength + square.strength >= 280:
                pass

            else:
                if square.production < neighbor.production:
                    return direction
                elif square.production == neighbor.production:
                    if random.randint(0, 1) == 1:
                        return direction
                    else:
                        return STILL
                else:
                    return STILL

    #If low on territory and high strength and at a border, move towards areas of high production
    if territory < 40 and square.strength > square.production * 4 and c == False:
        return fhwpd(square)

    #If low on territory and has medium strength, just go to nearest border (assuming it can take it)
    if territory < 40 and square.strength > square.production * 5:
        if dt(square) != True:
            return fwe(square)
        else:
            return fnb(square)

    #Move away from stronger enemies, and move towards weaker enemies.
    if fne(square) != 12 and ne(square) > 3:
        #If weak, remain still
        if square.strength < square.production * 5:
            return STILL
        if nfs(square) < nes(square):
            return hlt.opposite_cardinal(fne(square))
        else:
            return fne(square)

    #If it is being attacked, i.e. has many enemies nearby, just attack
    if ne(square) > 4 and square.strength > square.production * 5:
        if fne(square) == 12:
            return fnb(square)
        else:
            return fne(square)

    #Otherwise if the square is in its territory and quite strong, move towards nearest enemy preferably, or near border
    if c == True and square.strength > 40 and territory > 30:
        if fne(square) == 12:
            return fnb(square)
        else:
            return fne(square)

    #If it is surrounded by weak neighbors of its own type, then stay still
    if c == True and total < square.production * 12 and square.strength < 200:
        return STILL

    #If it is next to an enemy, take the weakest enemy
    if c == False:
        return fwe(square)

    #Default just stay still
    return STILL
Beispiel #4
0
                     if dists[n] != 101 and dists[site] > dists[n]):
                 new_val = 255 - (site.strength +
                                  known_strengths[neighbor.y][neighbor.x])
                 if new_val >= best_val: best_dir, best_val = dir, new_val
             make_move(site, best_dir)
 new_perim = sorted(perimeter, key=get_val)
 while len(new_perim) > 0:
     target = new_perim.pop()
     route = [{}]
     required = 1 + target.strength
     can_move = True
     for d, n in sorted([(d, n)
                         for d, n in enumerate(game_map.neighbors(target))
                         if n.owner == myID and n not in moves],
                        key=lambda p: 1 / (1 + p[1].strength)):
         route[0][n] = opposite_cardinal(d)
         required -= n.strength
         if required <= 0:
             break
     while required > 0 and len(route[-1]) > 0:
         for stage in route:
             required -= sum([site.production for site in stage])
         if required <= 0:
             can_move = False
             break
         new_sites = {}
         for site in route[-1]:
             new_sites.update({
                 n: opposite_cardinal(d)
                 for d, n in enumerate(game_map.neighbors(site))
                 if n.owner == myID and n.dist <= site.dist and n not in