Beispiel #1
0
def mine(gc, unit, karbonite_locations, current_roles, building_assignment):
    my_location = unit.location
    position = my_location.map_location()
    closest_deposit = get_closest_deposit(gc, unit, karbonite_locations)
    start_map = gc.starting_map(bc.Planet(0))

    #check to see if there even are deposits
    if start_map.on_map(closest_deposit):
        direction_to_deposit = position.direction_to(closest_deposit)
        #print(unit.id, "is trying to mine at", direction_to_deposit)
        if position.is_adjacent_to(
                closest_deposit) or position == closest_deposit:
            # mine if adjacent to deposit
            if gc.can_harvest(unit.id, direction_to_deposit):
                gc.harvest(unit.id, direction_to_deposit)
                current_roles["miner"].remove(unit.id)
                #print(unit.id," just harvested!")
        else:
            # move toward deposit
            enemies = gc.sense_nearby_units_by_team(position,
                                                    unit.vision_range,
                                                    sense_util.enemy_team(gc))
            if len(enemies) > 0:
                dir = sense_util.best_available_direction(gc, unit, enemies)
                movement.try_move(gc, unit, dir)
                #current_roles["miner"].remove(unit.id)
                #current_roles["builder"].append(unit.id)
                #building_assignment[unit.id] = pick_closest_building_assignment(gc, unit, building_assignment)
            else:
                movement.try_move(gc, unit, direction_to_deposit)
    else:
        current_roles["miner"].remove(unit.id)
Beispiel #2
0
def mage_sense(gc, unit, battle_locs, queued_paths):
    dir = None
    attack = None
    blink = None
    move_then_attack = False
    visible_enemies = False
    location = unit.location.map_location()
    enemies = gc.sense_nearby_units_by_team(location, unit.vision_range,
                                            sense_util.enemy_team(gc))
    if len(enemies) > 0:
        if unit.id in queued_paths:
            del queued_paths[unit.id]
        visible_enemies = True
        sorted_enemies = sorted(enemies,
                                key=lambda x: x.location.map_location().
                                distance_squared_to(location))
        closest_enemy = ranger.closest_among_ungarrisoned(sorted_enemies)
        print('closest enemy:', closest_enemy)
        attack = ranger.get_attack(gc, unit, location)
        print(attack)
        if attack is not None:
            print('found it here')
            if closest_enemy is not None:
                if (ranger.exists_bad_enemy(enemies) and
                    (closest_enemy.location.map_location().distance_squared_to(
                        location))**(0.5) + 2 < unit.attack_range()**
                    (0.5)) or not gc.can_attack(unit.id, attack.id):
                    dir = sense_util.best_available_direction(
                        gc, unit, enemies)

        else:
            if gc.is_move_ready(unit.id):
                if closest_enemy is not None:
                    dir = ranger.optimal_direction_towards(
                        gc, unit, location,
                        closest_enemy.location.map_location())
                    next_turn_loc = location.add(dir)
                    attack = ranger.get_attack(gc, unit, next_turn_loc)
                    if attack is not None:
                        move_then_attack = True
                else:
                    dir = ranger.get_explore_dir(gc, unit)

    else:
        if unit.id in queued_paths:
            if location != queued_paths[unit.id]:
                dir = ranger.optimal_direction_towards(gc, unit, location,
                                                       queued_paths[unit.id])
                return dir, attack, blink, move_then_attack, visible_enemies
            else:
                del queued_paths[unit.id]
        if len(battle_locs) > 0:
            dir, target = ranger.go_to_battle(gc, unit, battle_locs)
            queued_paths[unit.id] = target
        else:
            dir = ranger.get_explore_dir(gc, unit)

    return dir, attack, blink, move_then_attack, visible_enemies
Beispiel #3
0
def move_away(gc, unit, battle_locs, map_loc):
    lst = []
    for nearby_unit in gc.sense_nearby_units(map_loc, 10):
        if nearby_unit.location.is_on_map():
            nearby_loc = nearby_unit.location.map_location()
            if gc.can_sense_location(nearby_loc) and gc.has_unit_at_location(nearby_loc) and gc.sense_unit_at_location(nearby_loc).unit_type == bc.UnitType.Factory:
                lst.append(nearby_unit)

    return sense_util.best_available_direction(gc, unit, lst)
Beispiel #4
0
def timestep(gc, unit, info, karbonite_locations, locs_next_to_terrain,
             blueprinting_queue, building_assignment, current_roles):

    # last check to make sure the right unit type is running this
    if unit.unit_type != bc.UnitType.Worker:
        # prob should return some kind of error
        return
    my_team = gc.team()

    my_location = unit.location
    # make sure unit can actually perform actions ie. not in garrison
    if not my_location.is_on_map():
        return
    print()
    print("ON UNIT #", unit.id, "position: ", unit.location.map_location())
    role = get_role(gc, unit, blueprinting_queue, current_roles,
                    karbonite_locations)

    print("KARBONITE: ", gc.karbonite())
    if gc.team() == bc.Team(0):
        pass
        #print("current_roles",current_roles)
        #print("blueprinting_queue",blueprinting_queue)
        #print("building_assignment",building_assignment)

    current_num_workers = info[0]
    max_num_workers = get_replication_cap(gc, karbonite_locations)
    worker_spacing = 10

    #print("REPLICATION CAP: ",max_num_workers)
    # replicates if unit is able to (cooldowns, available directions etc.)
    if current_num_workers < max_num_workers:
        replicate(gc, unit)
        return

    # runs this block every turn if unit is miner
    if role == "miner":
        mine(gc, unit, karbonite_locations, current_roles)
    # if unit is builder
    elif role == "builder":
        build(gc, unit, building_assignment, current_roles)
    # if unit is blueprinter
    elif role == "blueprinter":
        blueprint(gc, unit, blueprinting_queue, building_assignment,
                  current_roles, locs_next_to_terrain)
    # if unit is idle
    elif role == "idle":
        nearby = gc.sense_nearby_units(my_location.map_location(),
                                       worker_spacing)
        away_from_units = sense_util.best_available_direction(gc, unit, nearby)
        print(unit.id, "at", unit.location.map_location(),
              "is trying to move to", away_from_units)
        movement.try_move(gc, unit, away_from_units)
Beispiel #5
0
def timestep(gc, unit, composition, knight_to_cluster, seen_knights_ids, KNIGHT_CLUSTER_MIN, constants):

    # last check to make sure the right unit type is running this
    if unit.unit_type != bc.UnitType.Knight:
        # prob should return some kind of error
        return

    my_team = gc.team()
    direction = None
    location = unit.location

    if location.is_on_map() and unit.id not in seen_knights_ids: 
        ## Clustering: if has a goal location keep moving there
        if unit.id in knight_to_cluster: 
            try:
                c = knight_to_cluster[unit.id]
                # print('cluster units: ', c.cluster_units())
                valid_cluster = clusters.knight_cluster_sense(gc, unit, c)
            except: 
                # print('KNIGHT clustering sense didnt run')
                pass

            try:
                if not valid_cluster: 
                    clusters.remove_cluster(c, knight_to_cluster)
                    # print('removed cluster')
                else: 
                    seen_knights_ids.update(c.cluster_units())
            except: 
                # print('cannot remove cluster OR add units to seen')
                pass

        else: 
            try: 
                enemy = knight_sense(gc, unit, knight_to_cluster, KNIGHT_CLUSTER_MIN)
            except:
                # print('KNIGHT sense didnt run')
                pass

        ## Movement
        try: 
            ## Knight movement away from allies
            if direction == None:  
                nearby = gc.sense_nearby_units(unit.location.map_location(),8)
                direction = sense_util.best_available_direction(gc,unit,nearby)

            if gc.is_move_ready(unit.id) and gc.can_move(unit.id, direction):
                gc.move_robot(unit.id, direction)
                # print('moved no cluster')

        except:
            # print('KNIGHT movement didnt go through')
            pass
Beispiel #6
0
def timestep(gc, unit, composition, last_turn_battle_locs):

    # last check to make sure the right unit type is running this
    if unit.unit_type != bc.UnitType.Healer:
        return

    my_team = gc.team()
    direction = None
    location = unit.location

    if location.is_on_map():
        unit_loc = location.map_location()
        print('HEALER LOC: ', unit_loc)

        directions, heal_target = healer_sense(gc, unit, last_turn_battle_locs)

        ## Movement
        try:
            if directions is not None:
                if len(directions) > 0 and gc.is_move_ready(unit.id):
                    for d in directions:
                        if gc.can_move(unit.id, d):
                            gc.move_robot(unit.id, d)
                            print('HEALER moved to battle loc!')
                            break

            ## Healer movement away from allies
            else:
                nearby = gc.sense_nearby_units(unit.location.map_location(), 8)
                direction = sense_util.best_available_direction(
                    gc, unit, nearby)

                if gc.is_move_ready(unit.id) and gc.can_move(
                        unit.id, direction):
                    gc.move_robot(unit.id, direction)
                    print('HEALER moved away from allies!')

        except:
            print('HEALER movement didnt go through')

        ## Healing
        try:
            if heal_target is not None:
                if gc.is_heal_ready(unit.id) and gc.can_heal(
                        unit.id, heal_target.id):
                    gc.heal(unit.id, heal_target.id)  # heal it, if possible
                    print('healed ', heal_target.id)
        except:
            print('HEALER healing didnt go through')
Beispiel #7
0
def mine_mars(gc, unit):
    my_location = unit.location.map_location()
    all_locations = gc.all_locations_within(my_location, unit.vision_range)
    planet = bc.Planet.Mars
    start_map = gc.starting_map(planet)
    worker_spacing = 8

    current_distance = float('inf')
    closest_deposit = bc.MapLocation(planet, -1, -1)
    for deposit_location in all_locations:
        if gc.karbonite_at(deposit_location) == 0:
            continue
        distance_to_deposit = my_location.distance_squared_to(deposit_location)
        #keep updating current closest deposit to unit
        if distance_to_deposit < current_distance:
            current_distance = distance_to_deposit
            closest_deposit = deposit_location
        #check to see if there even are deposits

    if start_map.on_map(closest_deposit):
        direction_to_deposit = my_location.direction_to(closest_deposit)
        #print(unit.id, "is trying to mine at", direction_to_deposit)
        if my_location.is_adjacent_to(
                closest_deposit) or my_location == closest_deposit:
            # mine if adjacent to deposit
            if gc.can_harvest(unit.id, direction_to_deposit):
                gc.harvest(unit.id, direction_to_deposit)

                #print(unit.id," just harvested on Mars!")
        else:
            # move toward deposit
            movement.try_move(gc, unit, direction_to_deposit)
    else:
        nearby = gc.sense_nearby_units_by_team(my_location.map_location(),
                                               worker_spacing, gc.team())

        away_from_units = sense_util.best_available_direction(gc, unit, nearby)
        #print(unit.id, "at", unit.location.map_location(), "is trying to move to", away_from_units)
        movement.try_move(gc, unit, away_from_units)
Beispiel #8
0
def timestep(unit):

    # last check to make sure the right unit type is running this
    if unit.unit_type != bc.UnitType.Healer:
        return
    # print('HEALER ID: ', unit.id)
    gc = variables.gc

    composition = variables.info
    direction_to_coord = variables.direction_to_coord
    bfs_array = variables.bfs_array
    enemy_team = variables.enemy_team
    my_team = variables.my_team

    assigned_healers = variables.assigned_healers  ## healer id: (cluster, best_healer_loc)
    assigned_overcharge = variables.assigned_overcharge
    overcharge_targets = variables.overcharge_targets

    quadrant_battles = variables.quadrant_battle_locs
    if variables.curr_planet == bc.Planet.Earth:
        quadrant_size = variables.earth_quadrant_size
    else:
        quadrant_size = variables.mars_quadrant_size

    unit_locations = variables.unit_locations

    best_dir = None
    best_loc = None
    best_heal_target = None
    best_overcharge_target = None
    heal = False
    overcharge = False
    location = unit.location

    if location.is_on_map():
        #print(location.map_location())
        ## Add new ones to unit_locations, else just get the location
        if unit.id not in unit_locations:
            loc = unit.location.map_location()
            unit_locations[unit.id] = (loc.x, loc.y)
            f_f_quad = (int(loc.x / quadrant_size), int(loc.y / quadrant_size))
            quadrant_battles[f_f_quad].add_ally(unit.id, "healer")

        unit_loc = unit_locations[unit.id]

        ## Assign role
        if unit.id not in assigned_overcharge and unit.id not in assigned_healers:
            overcharge_to_total = 1
            total = len(assigned_healers) + len(assigned_overcharge)
            if total > 0:
                overcharge_to_total = len(assigned_overcharge) / total
            # Assign to overcharge if there are targets and ratio is good
            if variables.research.get_level(
                    variables.unit_types["healer"]
            ) == 3 and overcharge_to_total < 0.2 and len(
                    overcharge_targets) > 0:
                best_overcharge_target = gc.unit(overcharge_targets.pop())
                assigned_overcharge[unit.id] = best_overcharge_target
            best_heal_target = get_best_target(gc, unit, unit_loc, my_team)
            if best_heal_target is not None:
                heal = True
            assigned, best_loc = assign_to_quadrant(gc, unit, unit_loc)
            # print('assigned? ', assigned)
            # print('assigned loc: ', best_loc)
            if not assigned:
                nearby = gc.sense_nearby_units_by_team(
                    bc.MapLocation(variables.curr_planet, unit_loc[0],
                                   unit_loc[1]), 8, variables.my_team)
                best_dir = sense_util.best_available_direction(
                    gc, unit, nearby)

        ## Overcharge
        if unit.id in assigned_overcharge:
            ally = assigned_overcharge[unit.id]
            if gc.can_overcharge(unit.id, ally.id):
                overcharge = True
                best_overcharge_target = ally
        if best_heal_target is None:
            best_heal_target = get_best_target(gc, unit, unit_loc, my_team)
            if best_heal_target is not None:
                heal = True

        ## Movement
        # If sees enemies close enough then tries to move away from them
        loc = bc.MapLocation(variables.curr_planet, unit_loc[0], unit_loc[1])
        enemies = gc.sense_nearby_units_by_team(loc, unit.vision_range,
                                                enemy_team)
        if len(enemies) > 0:
            enemies = sorted(enemies,
                             key=lambda x: x.location.map_location().
                             distance_squared_to(loc))
            enemy_loc = enemies[0].location.map_location()
            best_dir = dir_away_from_enemy(gc, unit, loc, enemy_loc)
        elif variables.curr_round > 650:
            best_dir = Ranger.move_to_rocket(gc, unit, loc,
                                             variables.direction_to_coord,
                                             variables.bfs_array)
        # Otherwise, goes to locations in need of healers
        else:
            if unit.id in assigned_overcharge:
                ally = assigned_overcharge[unit.id]
                best_loc_map = ally.location.map_location()
                best_loc = (best_loc_map.x, best_loc_map.y)
            elif unit.id in assigned_healers:
                best_loc = assigned_healers[unit.id][1]

        ## Do shit
        #print(best_dir)
        #print(best_loc)
        if best_overcharge_target is not None:
            if overcharge and gc.is_overcharge_ready(unit.id):
                gc.overcharge(unit.id, best_overcharge_target.id)
                overcharged_unit = gc.unit(best_overcharge_target.id)
                if overcharged_unit.unit_type == variables.unit_types[
                        "ranger"]:
                    Ranger.timestep(overcharged_unit)
        if best_heal_target is not None:
            if heal and gc.is_heal_ready(unit.id) and gc.can_heal(
                    unit.id, best_heal_target.id):
                gc.heal(unit.id, best_heal_target.id)
        if best_dir is not None and gc.is_move_ready(unit.id) and gc.can_move(
                unit.id, best_dir):
            gc.move_robot(unit.id, best_dir)
            add_new_location(unit.id, unit_loc, best_dir)
        elif best_loc is not None and gc.is_move_ready(
                unit.id) and unit_loc != best_loc:
            try_move_smartly(unit, unit_loc, best_loc)
Beispiel #9
0
def ranger_sense(gc, unit, battle_locs, queued_paths, ranger_roles, location,
                 constants, direction_to_coord, precomputed_bfs):
    if unit.id in ranger_roles["sniper"]:
        return snipe_sense(gc, unit, battle_locs, queued_paths, location,
                           direction_to_coord, precomputed_bfs)
    signals = {}
    dir = None
    attack = None
    snipe = None
    closest_enemy = None
    move_then_attack = False
    visible_enemies = False
    enemies = gc.sense_nearby_units_by_team(location, unit.vision_range,
                                            sense_util.enemy_team(gc))
    if len(enemies) > 0:
        if unit.id in queued_paths:
            del queued_paths[unit.id]
        visible_enemies = True
        sorted_enemies = sorted(enemies,
                                key=lambda x: x.location.map_location().
                                distance_squared_to(location))
        closest_enemy = closest_among_ungarrisoned(sorted_enemies)
        attack = get_attack(gc, unit, location)
        if attack is not None:
            if closest_enemy is not None:
                if check_radius_squares_factories(gc, unit):
                    dir = optimal_direction_towards(
                        gc, unit, location,
                        closest_enemy.location.map_location(),
                        constants.directions)
                elif (exists_bad_enemy(enemies)) or not gc.can_attack(
                        unit.id, closest_enemy.id):
                    dir = sense_util.best_available_direction(
                        gc, unit, enemies)
                #and (closest_enemy.location.map_location().distance_squared_to(location)) ** (
                #0.5) + 2 < unit.attack_range() ** (0.5)) or not gc.can_attack(unit.id, attack.id):
        else:
            if gc.is_move_ready(unit.id):

                if closest_enemy is not None:
                    dir = optimal_direction_towards(
                        gc, unit, location,
                        closest_enemy.location.map_location(),
                        constants.directions)

                    next_turn_loc = location.add(dir)
                    attack = get_attack(gc, unit, next_turn_loc)
                    if attack is not None:
                        move_then_attack = True
                else:
                    dir = get_explore_dir(gc, unit, location,
                                          constants.directions)

    else:
        # if there are no enemies in sight, check if there is an ongoing battle.  If so, go there.
        if len(battle_locs) > 0:
            dir, target = go_to_battle(gc, unit, battle_locs,
                                       constants.directions, location,
                                       direction_to_coord, precomputed_bfs)
            #queued_paths[unit.id] = target
        else:
            #dir = move_away(gc, unit, battle_locs)
            dir = get_explore_dir(gc, unit, location, constants.directions)
        """
        elif unit.id in queued_paths:
            if location!=queued_paths[unit.id]:
                dir = optimal_direction_towards(gc, unit, location, queued_paths[unit.id])
                return dir, attack, snipe, move_then_attack, visible_enemies, signals
            else:
                del queued_paths[unit.id]
        """

    return dir, attack, snipe, move_then_attack, visible_enemies, closest_enemy, signals
Beispiel #10
0
def ranger_sense(gc, unit, battle_locs, ranger_roles, location,
                 direction_to_coord, bfs_array, targeting_units, rocket_locs):
    enemies = gc.sense_nearby_units_by_team(location, unit.vision_range,
                                            variables.enemy_team)
    if unit.id in ranger_roles["sniper"]:
        return snipe_sense(gc, unit, battle_locs, location, enemies,
                           direction_to_coord, bfs_array, targeting_units)
    elif unit.id in ranger_roles["go_to_mars"]:
        return go_to_mars_sense(gc, unit, battle_locs, location, enemies,
                                direction_to_coord, bfs_array, targeting_units,
                                rocket_locs)
    signals = {}
    dir = None
    attack = None
    snipe = None
    closest_enemy = None
    move_then_attack = False
    visible_enemies = False
    start_time = time.time()
    # if variables.print_count < 10:
    #    print("Sensing nearby units:", time.time() - start_time)
    if len(enemies) > 0:
        visible_enemies = True
        start_time = time.time()
        closest_enemy = None
        closest_dist = float('inf')
        for enemy in enemies:
            loc = enemy.location
            if loc.is_on_map():
                dist = sense_util.distance_squared_between_maplocs(
                    loc.map_location(), location)
                if dist < closest_dist:
                    closest_dist = dist
                    closest_enemy = enemy
        # if variables.print_count < 10:
        #    print("Getting closest enemy:", time.time() - start_time)
        # sorted_enemies = sorted(enemies, key=lambda x: x.location.map_location().distance_squared_to(location))
        # closest_enemy = closest_among_ungarrisoned(sorted_enemies)
        start_time = time.time()
        attack = get_attack(gc, unit, location, targeting_units)
        # if variables.print_count < 10:
        #    print("Getting attack:", time.time() - start_time)
        if attack is not None:
            if closest_enemy is not None:
                start_time = time.time()
                if check_radius_squares_factories(gc, location):
                    dir = optimal_direction_towards(
                        gc, unit, location,
                        closest_enemy.location.map_location())
                elif (exists_bad_enemy(closest_enemy)) or not gc.can_attack(
                        unit.id, closest_enemy.id):
                    # if variables.print_count < 10:
                    #    print("Checking if condition:", time.time() - start_time)
                    start_time = time.time()
                    dir = sense_util.best_available_direction(
                        gc, unit, [closest_enemy])
                    # if variables.print_count < 10:
                    #    print("Getting best available direction:", time.time() - start_time)

                    # and (closest_enemy.location.map_location().distance_squared_to(location)) ** (
                    # 0.5) + 2 < unit.attack_range() ** (0.5)) or not gc.can_attack(unit.id, attack.id):
        else:
            if gc.is_move_ready(unit.id):

                if closest_enemy is not None:
                    dir = optimal_direction_towards(
                        gc, unit, location,
                        closest_enemy.location.map_location())

                    next_turn_loc = location.add(dir)
                    attack = get_attack(gc, unit, next_turn_loc,
                                        targeting_units)
                    if attack is not None:
                        move_then_attack = True
                else:
                    dir = run_towards_init_loc_new(gc, unit, location,
                                                   direction_to_coord)
                    # if variables.print_count < 10:
                    #    print("Getting direction:", time.time() - start_time)
    else:
        # if there are no enemies in sight, check if there is an ongoing battle.  If so, go there.
        if len(rocket_locs) > 0 and gc.round(
        ) > 600 and variables.curr_planet == bc.Planet.Earth:
            dir = move_to_rocket(gc, unit, location, direction_to_coord,
                                 bfs_array)
            if dir is not None:
                return dir, attack, snipe, move_then_attack, visible_enemies, closest_enemy, signals
        if len(battle_locs) > 0:
            # print('IS GOING TO BATTLE')
            dir = go_to_battle_new(gc, unit, battle_locs, location,
                                   direction_to_coord)
            # queued_paths[unit.id] = target
        else:
            # dir = move_away(gc, unit, battle_locs)
            if variables.curr_planet == bc.Planet.Earth:
                # print('IS RUNNING TOWARDS INIT LOC')
                dir = run_towards_init_loc_new(gc, unit, location,
                                               direction_to_coord)
            else:
                # print('EXPLORING')
                dir = get_explore_dir(gc, unit, location)
        """
        elif unit.id in queued_paths:
            if location!=queued_paths[unit.id]:
                dir = optimal_direction_towards(gc, unit, location, queued_paths[unit.id])
                return dir, attack, snipe, move_then_attack, visible_enemies, signals
            else:
                del queued_paths[unit.id]
        """
        # if variables.print_count < 10:
        #    print("regular movement:", time.time() - start_time)

    return dir, attack, snipe, move_then_attack, visible_enemies, closest_enemy, signals
    """
Beispiel #11
0
def timestep(unit):
    # last check to make sure the right unit type is running this
    if unit.unit_type != bc.UnitType.Knight:
        # prob should return some kind of error
        return

    gc = variables.gc

    assigned_knights = variables.assigned_knights
    quadrant_battles = variables.quadrant_battle_locs
    unit_locations = variables.unit_locations

    info = variables.info

    my_team = variables.my_team
    enemy_team = variables.enemy_team
    directions = variables.directions

    best_loc = None  ## (x,y)
    best_dir = None
    best_target = None
    location = unit.location

    if variables.curr_planet == bc.Planet.Earth:
        quadrant_size = variables.earth_quadrant_size
    else:
        quadrant_size = variables.mars_quadrant_size

    if location.is_on_map():
        # unit_loc = location.map_location()
        if unit.id not in unit_locations:
            loc = unit.location.map_location()
            unit_locations[unit.id] = (loc.x, loc.y)
            f_f_quad = (int(loc.x / quadrant_size), int(loc.y / quadrant_size))
            quadrant_battles[f_f_quad].add_ally(unit.id, "knight")

        unit_loc = unit_locations[unit.id]

        ## Attack
        best_target = get_best_target(gc, unit, unit_loc, knight_unit_priority)

        ## Movement
        # If new knight assign to location
        if unit.id not in assigned_knights:
            assigned, best_loc = assign_to_quadrant(gc, unit, unit_loc)
            if not assigned:
                nearby = gc.sense_nearby_units_by_team(
                    bc.MapLocation(variables.curr_planet, unit_loc[0],
                                   unit_loc[1]), 8, variables.my_team)
                best_dir = sense_util.best_available_direction(
                    gc, unit, nearby)
        else:
            best_loc = assigned_knights[unit.id]

        ## Do shit
        # Attack
        if best_target is not None and gc.can_attack(
                unit.id, best_target.id
        ):  # checked if ready to attack in get best target
            gc.attack(unit.id, best_target.id)

        # Move
        if best_loc is not None and gc.is_move_ready(
                unit.id) and unit_loc != best_loc:
            try_move_smartly(unit, unit_loc, best_loc)
        elif best_dir is not None and gc.is_move_ready(
                unit.id) and gc.can_move(unit.id, best_dir):
            gc.move_robot(unit.id, best_dir)
            add_new_location(unit.id, unit_loc, best_dir)
Beispiel #12
0
def blueprint(gc, unit, blueprinting_queue, building_assignment, current_roles,
              locs_next_to_terrain):
    my_location = unit.location
    start_map = gc.starting_map(bc.Planet(0))
    directions = list(bc.Direction)

    blueprint_spacing = 20
    nearby = gc.sense_nearby_units(my_location.map_location(),
                                   blueprint_spacing)
    is_nearby_factories = False
    is_nearby_potential_factories = False

    # if it finds a nice location for factory cluster, put it in queue
    if len(blueprinting_queue) < get_cluster_limit(gc):
        for other in nearby:
            if other.unit_type == bc.UnitType.Factory:
                is_nearby_factories = True
                break
        for cluster in blueprinting_queue:
            for potential_factory in cluster:
                if my_location.map_location().distance_squared_to(
                        potential_factory.map_location) < blueprint_spacing:
                    is_nearby_potential_factories = True
                    break
        if not (is_nearby_factories or is_nearby_potential_factories):
            future_factory_locations = generate_factory_locations(
                start_map, my_location.map_location(), locs_next_to_terrain)
            if len(future_factory_locations) > 0:
                new_cluster = [
                    BuildSite(building_location, bc.UnitType.Factory)
                    for building_location in future_factory_locations
                ]
                blueprinting_queue.extend([new_cluster])
            #print(unit.id," just added to building queue")

    # assign this unit to build a blueprint, if nothing to build just move away from other factories
    if unit.id not in building_assignment:
        if len(blueprinting_queue) > 0:
            closest_building_site = get_closest_site(gc, unit,
                                                     blueprinting_queue)
            for cluster in blueprinting_queue:
                if closest_building_site in cluster:
                    cluster.remove(closest_building_site)
                    if len(cluster) == 0:
                        blueprinting_queue.remove(cluster)
                    break
            building_assignment[unit.id] = closest_building_site
            #print(unit.id, "has been assigned to this building ",closest_building_site)
        else:
            all_factories = []
            for other in gc.my_units():
                if other.unit_type == bc.UnitType.Factory:
                    all_factories.append(other)
            away_from_factories = sense_util.best_available_direction(
                gc, unit, all_factories)
            # pick other direction if direction is center
            if away_from_factories == directions[8]:
                away_from_factories = directions[1]
            movement.try_move(gc, unit, away_from_factories)
            #print(unit.id, " is exploring the map for build sites")

    # build blueprint in assigned square
    if unit.id in building_assignment:
        assigned_site = building_assignment[unit.id]
        direction_to_site = my_location.map_location().direction_to(
            assigned_site.map_location)
        if my_location.map_location().is_adjacent_to(
                assigned_site.map_location):
            if can_blueprint_factory(gc) and gc.can_blueprint(
                    unit.id, bc.UnitType.Factory, direction_to_site):
                gc.blueprint(unit.id, bc.UnitType.Factory, direction_to_site)
                current_roles["blueprinter"].remove(unit.id)
                current_roles["builder"].append(unit.id)
                print(unit.id, " just created a blueprint!")
            else:
                pass
                #print(unit.id, "can't build but is right next to assigned site")
        elif my_location.map_location() == assigned_site.map_location:
            # when unit is currently on top of the queued building site
            d = random.choice(list(bc.Direction))
            movement.try_move(gc, unit, d)
            #print(unit.id, " is on top of its build site and is moving away")
        else:
            # move toward queued building site
            print(unit.id, "is moving toward building site: ", assigned_site)
            next_direction = my_location.map_location().direction_to(
                assigned_site.map_location)

            movement.try_move(gc, unit, next_direction)
            """
Beispiel #13
0
def ranger_sense(gc, unit, battle_locs, ranger_roles, location,
                 direction_to_coord, bfs_array, targeting_units, rocket_locs):
    signals = {}
    dir = None
    attack = None
    snipe = None
    closest_enemy = None
    move_then_attack = False
    visible_enemies = False
    if not unit.ranger_is_sniping():
        if unit.id in variables.is_sniping:
            del variables.is_sniping[unit.id]
        enemies = gc.sense_nearby_units_by_team(location, unit.vision_range,
                                                variables.enemy_team)
        if unit.id in ranger_roles["go_to_mars"]:
            return go_to_mars_sense(gc, unit, battle_locs, location, enemies,
                                    direction_to_coord, bfs_array,
                                    targeting_units, rocket_locs)
        # if variables.print_count < 10:
        #    print("Sensing nearby units:", time.time() - start_time)
        if len(enemies) > 0:
            visible_enemies = True
            start_time = time.time()
            closest_enemy = None
            closest_dist = float('inf')
            for enemy in enemies:
                loc = enemy.location
                if loc.is_on_map():
                    dist = sense_util.distance_squared_between_maplocs(
                        loc.map_location(), location)
                    if dist < closest_dist:
                        closest_dist = dist
                        closest_enemy = enemy
            # if variables.print_count < 10:
            #    print("Getting closest enemy:", time.time() - start_time)
            # sorted_enemies = sorted(enemies, key=lambda x: x.location.map_location().distance_squared_to(location))
            # closest_enemy = closest_among_ungarrisoned(sorted_enemies)
            attack = get_attack(gc, unit, location, targeting_units)
            # if variables.print_count < 10:
            #    print("Getting attack:", time.time() - start_time)
            if attack is not None:
                #visible_enemies = True
                if closest_enemy is not None:
                    closest_enemy_loc = closest_enemy.location.map_location()
                    if check_radius_squares_factories(
                            gc, location
                    ) and closest_enemy.unit_type != variables.unit_types[
                            "knight"]:
                        dir = optimal_direction_towards(
                            gc, unit, location, closest_enemy_loc)
                    elif closest_enemy.unit_type == variables.unit_types[
                            "knight"] or not gc.can_attack(
                                unit.id, closest_enemy.id):
                        # if variables.print_count < 10:
                        #    print("Checking if condition:", time.time() - start_time)
                        #start_time = time.time()
                        dir = sense_util.best_available_direction(
                            gc, unit, [closest_enemy])
                        # if variables.print_count < 10:
                        #    print("Getting best available direction:", time.time() - start_time)

                        # and (closest_enemy.location.map_location().distance_squared_to(location)) ** (
                        # 0.5) + 2 < unit.attack_range() ** (0.5)) or not gc.can_attack(unit.id, attack.id):

                    elif variables.num_enemies > 0.9 * variables.info[2]:
                        #dir = None
                        dir = sense_util.best_available_direction(
                            gc, unit, [closest_enemy])
                    else:
                        dir = None
                        #dir = sense_util.best_available_direction(gc, unit, [closest_enemy])
                        #dir = try_orthogonal(gc, unit, location, closest_enemy_loc)

            else:
                num_sniping = len(variables.is_sniping)
                if gc.research_info().get_level(
                        variables.unit_types["ranger"]
                ) == 3 and gc.is_begin_snipe_ready(
                        unit.id) and num_sniping < 10:
                    best_unit = None
                    best_priority = -float("inf")
                    for poss_enemy in variables.units:
                        if poss_enemy.location.is_on_map(
                        ) and poss_enemy.team != gc.team() and snipe_priority(
                                poss_enemy) > best_priority:
                            best_unit = poss_enemy
                            best_priority = snipe_priority(poss_enemy)

                        # temporary always target factories
                        if best_priority == 5:
                            break

                    snipe = best_unit

                elif gc.is_move_ready(unit.id):

                    if closest_enemy is not None:
                        dir = go_to_loc(
                            unit, location,
                            closest_enemy.location.map_location()
                        )  #optimal_direction_towards(gc, unit, location, closest_enemy.location.map_location())
                        if dir is None or dir == variables.directions[8]:
                            dir = optimal_direction_towards(
                                gc, unit, location,
                                closest_enemy.location.map_location())
                        next_turn_loc = location.add(dir)
                        attack = get_attack(gc, unit, next_turn_loc,
                                            targeting_units)
                        if attack is not None:
                            move_then_attack = True
                    else:
                        if variables.curr_planet == bc.Planet.Earth:
                            # print('IS RUNNING TOWARDS INIT LOC')
                            dir = run_towards_init_loc_new(
                                gc, unit, location, direction_to_coord)
                        else:
                            # print('EXPLORING')
                            dir = get_explore_dir(gc, unit, location)
                        # if variables.print_count < 10:
                        #    print("Getting direction:", time.time() - start_time)
        else:
            # if there are no enemies in sight, check if there is an ongoing battle.  If so, go there.
            if len(rocket_locs
                   ) > 0 and variables.curr_planet == bc.Planet.Earth and (
                       gc.round() > 600 or
                       (variables.num_enemies < 5 and gc.round() > 275)):
                dir = move_to_rocket(gc, unit, location, direction_to_coord,
                                     bfs_array)
                if dir is not None:
                    return dir, attack, snipe, move_then_attack, visible_enemies, closest_enemy, signals
            if len(battle_locs) > 0:
                # print('IS GOING TO BATTLE')
                dir = go_to_battle_new(gc, unit, battle_locs, location,
                                       direction_to_coord)
                # queued_paths[unit.id] = target
            else:
                # dir = move_away(gc, unit, battle_locs)
                if variables.curr_planet == bc.Planet.Earth:
                    # print('IS RUNNING TOWARDS INIT LOC')
                    dir = run_towards_init_loc_new(gc, unit, location,
                                                   direction_to_coord)
                else:
                    # print('EXPLORING')
                    dir = get_explore_dir(gc, unit, location)
            """
            elif unit.id in queued_paths:
                if location!=queued_paths[unit.id]:
                    dir = optimal_direction_towards(gc, unit, location, queued_paths[unit.id])
                    return dir, attack, snipe, move_then_attack, visible_enemies, signals
                else:
                    del queued_paths[unit.id]
            """
            # if variables.print_count < 10:
            #    print("regular movement:", time.time() - start_time)

    return dir, attack, snipe, move_then_attack, visible_enemies, closest_enemy, signals
    """
Beispiel #14
0
def timestep(gc, unit, info, karbonite_locations, blueprinting_queue,
             blueprinting_assignment, building_assignment, current_roles):
    #print(building_assignment)
    # last check to make sure the right unit type is running this
    if unit.unit_type != bc.UnitType.Worker:
        # prob should return some kind of error
        return
    my_team = gc.team()

    my_location = unit.location

    # make sure unit can actually perform actions ie. not in garrison
    if not my_location.is_on_map():
        return

    if my_location.map_location().planet is bc.Planet.Mars:
        mine_mars(gc, unit)
        return

    my_role = "idle"
    for role in current_roles:
        if unit.id in current_roles[role]:
            my_role = role

    # print()
    # print("on unit #",unit.id, "position: ",unit.location.map_location(), "role: ",my_role)

    #print("KARBONITE: ",gc.karbonite()

    current_num_workers = info[0]
    max_num_workers = get_replication_cap(gc, karbonite_locations)
    worker_spacing = 8
    workers_per_building = 4

    #print("REPLICATION CAP: ",max_num_workers)
    # replicates if unit is able to (cooldowns, available directions etc.)
    if current_num_workers < max_num_workers:
        try_replicate = replicate(gc, unit)
        if try_replicate:
            return

    # runs this block every turn if unit is miner
    if my_role == "miner":
        mine(gc, unit, karbonite_locations, current_roles, building_assignment)
    # if unit is builder
    elif my_role == "builder":
        build(gc, unit, building_assignment, current_roles,
              workers_per_building)
    # if unit is blueprinter
    elif my_role == "blueprinter":
        blueprint(gc, unit, blueprinting_queue, building_assignment,
                  blueprinting_assignment, current_roles)
    # if unit is boarder
    elif my_role == "boarder":
        board(gc, unit, current_roles)
    # if unit is idle
    elif my_role == "repairer":
        repair(gc, unit, current_roles)
    else:
        nearby = gc.sense_nearby_units_by_team(my_location.map_location(),
                                               worker_spacing, gc.team())

        away_from_units = sense_util.best_available_direction(gc, unit, nearby)
        #print(unit.id, "at", unit.location.map_location(), "is trying to move to", away_from_units)
        movement.try_move(gc, unit, away_from_units)