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)
def get_attack(gc, unit, location): vuln_enemies = gc.sense_nearby_units_by_team(location, unit.attack_range(), sense_util.enemy_team(gc)) if len(vuln_enemies) == 0: return None return max(vuln_enemies, key=lambda x: coefficient_computation(gc, unit, x, location))
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
def update_target(self, gc, unit): """ Updates enemy if there is another enemy closer to cluster """ try: unit_loc = unit.location.map_location() enemies = gc.sense_nearby_units_by_team(unit_loc, unit.vision_range, sense_util.enemy_team(gc)) enemies = sorted(enemies, key=lambda x: x.location.map_location(). distance_squared_to(unit_loc)) if len(enemies) > 0: if enemies[0].id != self.target_unit_id: self.target_unit_id = enemies[0].id self.target_loc = enemies[0].location.map_location() return True except: return False
def knight_sense(gc, unit, knight_to_cluster, KNIGHT_CLUSTER_MIN): """ This function chooses the direction the knight should move in. If it senses enemies nearby then will return direction that it can move to. If it can attack it will say if it is in range for regular attack and/or javelin. Otherwise, will attempt to group with other allied knights and return a direction to clump the knights up. Returns: New desired direction. """ new_direction = None try: unit_loc = unit.location.map_location() except: return try: enemies = gc.sense_nearby_units_by_team(unit_loc, unit.vision_range, sense_util.enemy_team(gc)) except: print('KNIGHTS ARE FUCKERS') if len(enemies) > 0: enemies = sorted(enemies, key=lambda x: x.location.map_location(). distance_squared_to(unit_loc)) ## Remove knights if their clusters are too small unavailable_knight_ids = set() for u_knight_id in knight_to_cluster: cluster = knight_to_cluster[u_knight_id] if len(cluster.cluster_units()) >= KNIGHT_CLUSTER_MIN: unavailable_knight_ids.add(u_knight_id) ## Create cluster! new_cluster = clusters.create_knight_cluster(gc, unit, enemies[0], unavailable_knight_ids) cluster_unit_ids = new_cluster.cluster_units() for unit_id in cluster_unit_ids: knight_to_cluster[unit_id] = new_cluster
def snipe_sense(gc, unit, battle_locs, queued_paths, location, direction_to_coord, precomputed_bfs): signals = {} dir = None attack = None snipe = 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 not unit.ranger_is_sniping(): if len(enemies) > 0: visible_enemies = True attack = get_attack(gc, unit, location) if len(enemies) > 0 or check_radius_squares_factories( gc, unit, 2) or not gc.is_begin_snipe_ready( unit.id): #or how_many_adjacent(gc, unit)>5 dir = move_away(gc, unit, battle_locs, location) else: try: best_unit = None best_priority = -float("inf") for poss_enemy in gc.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 rockets if best_priority == 5: break snipe = best_unit except: pass return dir, attack, snipe, move_then_attack, visible_enemies, signals
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
import sys import traceback import Units.map_info as map_info import Units.sense_util as sense_util import Units.explore as explore import time import numpy as np from scipy.sparse import dok_matrix from scipy.sparse import csgraph gc = bc.GameController() ## CONSTANTS ## my_team = gc.team() enemy_team = sense_util.enemy_team(gc) directions = list(bc.Direction) all_but_center_dir = directions[:-1] earth = bc.Planet.Earth mars = bc.Planet.Mars unit_types = { "worker": bc.UnitType.Worker, "knight": bc.UnitType.Knight, "mage": bc.UnitType.Mage, "healer": bc.UnitType.Healer, "ranger": bc.UnitType.Ranger, "factory": bc.UnitType.Factory, "rocket": bc.UnitType.Rocket
# let's start off with some research! # we can queue as much as we want. research.research_step(gc) ##SHARED TEAM INFORMATION## # GENERAL queued_paths = {} karbonite_locations = map_info.get_initial_karbonite_locations(gc) locs_next_to_terrain = map_info.get_locations_next_to_terrain(gc, bc.Planet(0)) start_map = gc.starting_map(bc.Planet(0)) # print('NEXT TO TERRAIN',locs_next_to_terrain) constants = c.Constants(list(bc.Direction), gc.team(), sense_util.enemy_team(gc), start_map, locs_next_to_terrain, karbonite_locations) #ROCKETS rocket_launch_times = {} rocket_landing_sites = {} # WORKER blueprinting_queue = [] building_assignment = {} blueprinting_assignment = {} current_worker_roles = { "miner": [], "builder": [], "blueprinter": [],