Ejemplo n.º 1
0
def try_move_smartly(unit, map_loc1, map_loc2):
    if variables.gc.is_move_ready(unit.id):
        our_coords = map_loc1
        target_coords = map_loc2
        bfs_array = variables.bfs_array
        our_coords_val = Ranger.get_coord_value(our_coords)
        target_coords_val = Ranger.get_coord_value(target_coords)
        if bfs_array[our_coords_val, target_coords_val] != float('inf'):
            best_dirs = Ranger.use_dist_bfs(our_coords, target_coords,
                                            bfs_array)
            choice_of_dir = random.choice(best_dirs)
            shape = variables.direction_to_coord[choice_of_dir]
            options = sense_util.get_best_option(shape)
            for option in options:
                if variables.gc.can_move(unit.id, option):
                    variables.gc.move_robot(unit.id, option)
                    ## CHANGE LOC IN NEW DATA STRUCTURE
                    add_new_location(unit.id, our_coords, option)
                    break
Ejemplo n.º 2
0
def optimal_unload(gc, unit, directions, building_assignments, battle_locs):
    """
	Tries to find unload direction towards a battle location, otherwise finds another optimal
	direction with previous logic.

	Returns direction or None.
	"""
    unit_loc = unit.location.map_location()
    #build_sites = list(map(lambda site: site.map_location,list(building_assignments.values())))

    ## Find list of best unload towards battle_locs and use best dir that doesn't interfere with building
    if len(battle_locs) > 0:
        weakest = random.choice(list(battle_locs.keys()))
        target_loc = battle_locs[weakest][0]

        shape = [target_loc.x - unit_loc.x, target_loc.y - unit_loc.y]
        unload_dirs = sense_util.get_best_option(
            shape)  ## never returns None or empty list

        for d in unload_dirs:
            if gc.can_unload(unit.id,
                             d):  #and unit_loc.add(d) not in build_sites
                return d

    ## Use previous optimal location
    best = None
    best_val = -float('inf')

    for d in directions:
        if gc.can_unload(
                unit.id, d
        ):  # and unit.location.map_location().add(d) not in build_sites
            locs = gc.all_locations_within(unit.location.map_location(), 9)
            locs_good = []
            for loc in locs:
                if gc.can_sense_location(loc) and gc.has_unit_at_location(loc):
                    locs_good.append(loc)
            num_good = len(locs_good)
            if num_good > best_val:
                best_val = num_good
                best = d

    return best
Ejemplo n.º 3
0
def move_to_rocket(gc, unit, location, direction_to_coord, bfs_array):
    dir = None
    location_coords = (location.x, location.y)
    if unit.id not in variables.which_rocket or variables.which_rocket[unit.id][1] not in variables.rocket_locs:
        for rocket in variables.rocket_locs:
            target_loc = variables.rocket_locs[rocket]
            target_coords = (target_loc.x, target_loc.y)
            start_coords_val = get_coord_value(location_coords)
            target_coords_val = get_coord_value(target_coords)
            if bfs_array[start_coords_val, target_coords_val]!=float('inf'):
                variables.which_rocket[unit.id] = (target_loc, rocket)
                break

    if unit.id not in variables.which_rocket:
        return None

    target_loc = variables.which_rocket[unit.id][0]

    # # rocket was destroyed
    if not gc.has_unit_at_location(target_loc):
        return variables.directions[8]
    #print(unit.id)
    #print('MY LOCATION:', start_coords)
    #print('GOING TO:', target_loc)
    if max(abs(target_loc.x - location_coords[0]), abs(target_loc.y-location_coords[1])) == 1:
        rocket = gc.sense_unit_at_location(target_loc)
        if gc.can_load(rocket.id, unit.id):
            gc.load(rocket.id, unit.id)
    else:
        target_coords = (target_loc.x, target_loc.y)
        start_coords_val = get_coord_value(location_coords)
        target_coords_val = get_coord_value(target_coords)
        #target_coords_thirds = (int(target_loc.x / bfs_fineness), int(target_loc.y / bfs_fineness))
        if bfs_array[start_coords_val, target_coords_val]!=float('inf'):
            best_dirs = use_dist_bfs(location_coords, target_coords, bfs_array)
            choice_of_dir = random.choice(best_dirs)
            shape = direction_to_coord[choice_of_dir]
            options = sense_util.get_best_option(shape)
            for option in options:
                if gc.can_move(unit.id, option):
                    return option
        return variables.directions[8]
Ejemplo n.º 4
0
def go_to_battle(gc, unit, battle_locs, location, direction_to_coord, precomputed_bfs, bfs_fineness):
    #start_time = time.time()
    # send a unit to battle
    weakest = random.choice(list(battle_locs.keys()))
    target = battle_locs[weakest][0]
    start_coords = (location.x, location.y)
    target_coords_thirds = (int(target.x/bfs_fineness), int(target.y/bfs_fineness))
    if (start_coords, target_coords_thirds) not in precomputed_bfs:
        target_coords = pick_from_init_enemy_locs(start_coords)
        if target_coords is None:
            return None
        else:
            target_coords_thirds = (int(target_coords.x / bfs_fineness), int(target_coords.y / bfs_fineness))
    shape = direction_to_coord[precomputed_bfs[(start_coords, target_coords_thirds)]]
    options = sense_util.get_best_option(shape)
    for option in options:
        if gc.can_move(unit.id, option):
            #print(time.time() - start_time)
            return option
    return directions[8]
Ejemplo n.º 5
0
def healer_sense(gc, unit, unit_loc, battle_locs, constants):
    direction = None
    heal_target = None

    ## If healer sees an enemy in vision range, move away from enemy
    enemies = gc.sense_nearby_units_by_team(unit_loc, unit.vision_range,
                                            constants.enemy_team)
    if len(enemies) > 0:
        enemies = sorted(enemies,
                         key=lambda x: x.location.map_location().
                         distance_squared_to(unit_loc))
        dir_to_enemy = unit_loc.direction_to(
            enemies[0].location.map_location())
        new_loc = unit_loc.subtract(dir_to_enemy)
        new_dir = unit_loc.direction_to(new_loc)

        if gc.can_move(unit.id, new_dir):
            direction = new_dir

    ## Otherwise attempt to send healer to battle location
    if direction is None and len(battle_locs) > 0:
        weakest = random.choice(list(battle_locs.keys()))
        target_loc = battle_locs[weakest][0]

        shape = [target_loc.x - unit_loc.x, target_loc.y - unit_loc.y]
        directions = sense_util.get_best_option(shape)

        for d in directions:
            if gc.can_move(unit.id, d):
                direction = d
                break

    ## Attempt to heal nearby units
    nearby = gc.sense_nearby_units_by_team(unit_loc,
                                           unit.ability_range() - 1,
                                           constants.my_team)
    if len(nearby) > 0:
        nearby = sorted(nearby, key=lambda x: x.health / x.max_health)
        heal_target = nearby[0]

    return direction, heal_target
Ejemplo n.º 6
0
def go_to_mars_sense(gc, unit, battle_locs, location, enemies, direction_to_coord, precomputed_bfs, targeting_units, bfs_fineness, rocket_locs):
    #print('GOING TO MARS')
    signals = {}
    dir = None
    attack = None
    snipe = None
    closest_enemy = None
    move_then_attack = False
    visible_enemies = False
    
    if len(enemies) > 0:
        visible_enemies = True
        attack = get_attack(gc, unit, location, targeting_units)
    start_coords = (location.x, location.y)

    # # rocket was launched
    if unit.id not in variables.which_rocket or variables.which_rocket[unit.id][1] not in variables.rocket_locs:
        variables.ranger_roles["go_to_mars"].remove(unit.id)
        return dir, attack, snipe, move_then_attack, visible_enemies, closest_enemy, signals
    target_loc = variables.which_rocket[unit.id][0]

    # # rocket was destroyed
    # if not gc.has_unit_at_location(target_loc):
    #     variables.ranger_roles["go_to_mars"].remove(unit.id)
    #     return dir, attack, snipe, move_then_attack, visible_enemies, closest_enemy, signals
    #print(unit.id)
    #print('MY LOCATION:', start_coords)
    #print('GOING TO:', target_loc)
    if max(abs(target_loc.x - start_coords[0]), abs(target_loc.y-start_coords[1])) == 1:
        rocket = gc.sense_unit_at_location(target_loc)
        if gc.can_load(rocket.id, unit.id):
            gc.load(rocket.id, unit.id)
    elif sense_util.distance_squared_between_maplocs(location, target_loc) < 17:
        #print('REALLY CLOSE')
        poss_dir = location.direction_to(target_loc)
        shape = variables.direction_to_coord[poss_dir]
        options = sense_util.get_best_option(shape)
        for option in options:
            if gc.can_move(unit.id, option):
                # print(time.time() - start_time)
                dir = option
                break
        if dir is None:
            dir = directions[8]
        """
        result = explore.bfs_with_destination((target_loc.x, target_loc.y), start_coords, variables.gc, variables.curr_planet, variables.passable_locations_earth, variables.coord_to_direction)
        if result is None:
            variables.ranger_roles["go_to_mars"].remove(unit.id)
            dir = None
        else:
            dir = result
        """
    else:
        start_coords = (location.x, location.y)
        target_coords_thirds = (int(target_loc.x / bfs_fineness), int(target_loc.y / bfs_fineness))
        if (start_coords, target_coords_thirds) not in precomputed_bfs:
            poss_dir = location.direction_to(target_loc)
            shape = variables.direction_to_coord[poss_dir]
            options = sense_util.get_best_option(shape)
            for option in options:
                if gc.can_move(unit.id, option):
                    # print(time.time() - start_time)
                    dir = option
                    break
            if dir is None:
                dir = directions[8]
        else:
            shape = direction_to_coord[precomputed_bfs[(start_coords, target_coords_thirds)]]
            options = sense_util.get_best_option(shape)
            for option in options:
                if gc.can_move(unit.id, option):
                    # print(time.time() - start_time)
                    dir = option
                    break
            if dir is None:
                dir = directions[8]
        #print(dir)

    return dir, attack, snipe, move_then_attack, visible_enemies, closest_enemy, signals