def get_gauss(self): print(self.map.width) # grid = [self.map.height()][self.map.width()] grid = [[0 for x in range(self.map.width)] for y in range(self.map.height)] # print(grid) for x in range(0, self.map.height): for y in range(0, self.map.width): b = self.gc.starting_map(bc.Planet(0)) grid[x][y] = b.initial_karbonite_at( bc.MapLocation(bc.Planet(0), y, x)) gauss = [[0 for x in range(self.map.width)] for y in range(self.map.height)] for r in range(1, len(grid) - 1): for c in range(1, len(grid[r]) - 1): avg = int( (grid[r][c] + grid[r - 1][c] + grid[r + 1][c] + grid[r - 1][c - 1] + grid[r + 1][c + 1] + grid[r][c - 1] + grid[r][c + 1] + grid[r - 1][c + 1] + grid[r + 1][c - 1]) / 9) gauss[r][c] = avg # for i in gauss: # print(i) # print() grid = gauss for r in range(1, len(grid) - 1): for c in range(1, len(grid[r]) - 1): avg = int( (grid[r][c] + grid[r - 1][c] + grid[r + 1][c] + grid[r - 1][c - 1] + grid[r + 1][c + 1] + grid[r][c - 1] + grid[r][c + 1] + grid[r - 1][c + 1] + grid[r + 1][c - 1]) / 9) gauss[r][c] = avg grid = gauss for r in range(1, len(grid) - 1): for c in range(1, len(grid[r]) - 1): avg = int( (grid[r][c] + grid[r - 1][c] + grid[r + 1][c] + grid[r - 1][c - 1] + grid[r + 1][c + 1] + grid[r][c - 1] + grid[r][c + 1] + grid[r - 1][c + 1] + grid[r + 1][c - 1]) / 9) gauss[r][c] = avg grid = gauss for r in range(1, len(grid) - 1): for c in range(1, len(grid[r]) - 1): avg = int( (grid[r][c] + grid[r - 1][c] + grid[r + 1][c] + grid[r - 1][c - 1] + grid[r + 1][c + 1] + grid[r][c - 1] + grid[r][c + 1] + grid[r - 1][c + 1] + grid[r + 1][c - 1]) / 9) gauss[r][c] = avg for i in gauss: print(i) print() return gauss
def get_initial_karbonite_locations(gc): deposit_locations = {} start_map = gc.starting_map(bc.Planet(0)) for x in range(start_map.width): for y in range(start_map.height): map_location = bc.MapLocation(bc.Planet(0), x, y) karbonite_at = start_map.initial_karbonite_at(map_location) if karbonite_at > 0: deposit_locations[(x, y)] = karbonite_at return deposit_locations
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 build(gc, my_unit, building_assignment, current_roles, workers_per_building): my_location = my_unit.location.map_location() start_map = gc.starting_map(bc.Planet(0)) #print("building_assignment",building_assignment) my_nearby_units = gc.my_units() unit_was_not_assigned = True assigned_building = None #print("unit",my_unit.id,"is building") # loop through building assignments and look for my_unit.id if it is assigned for building_id in building_assignment: if my_unit.id in building_assignment[ building_id] and building_id in list_of_unit_ids(gc): assigned_building = gc.unit(building_id) #print("assigned_building",assigned_building.location.map_location()) if assigned_building.structure_is_built(): #print(my_unit.id,"assigned_building was already built") del building_assignment[building_id] #current_roles["builder"].remove(my_unit.id) #return assigned_building = assign_unit_to_build( gc, my_unit, building_assignment, workers_per_building) unit_was_not_assigned = False break else: unit_was_not_assigned = False if unit_was_not_assigned: #print("unit wasn't assigned building prior") assigned_building = assign_unit_to_build(gc, my_unit, building_assignment, workers_per_building) if assigned_building is None: #print(my_unit.id, "there are no blueprints around") current_roles["builder"].remove(my_unit.id) return #print("unit has been assigned to build at",assigned_building.location.map_location()) assigned_location = assigned_building.location.map_location() if my_location.is_adjacent_to(assigned_location): if gc.can_build(my_unit.id, assigned_building.id): print(my_unit.id, "is building factory at ", assigned_location) gc.build(my_unit.id, assigned_building.id) if assigned_building.structure_is_built(): current_roles["builder"].remove(my_unit.id) del building_assignment[building_id] return # if not adjacent move toward it else: #print(unit.id, "is trying to move toward factory at ",assigned_site) direction_to_blueprint = my_location.direction_to(assigned_location) movement.try_move(gc, my_unit, direction_to_blueprint) # if no nearby blueprints around.. """
def update_deposit_info(gc, unit, karbonite_locations): position = unit.location.map_location() planet = bc.Planet(0) karbonite_locations_keys = list(karbonite_locations.keys())[:] for x, y in karbonite_locations_keys: map_location = bc.MapLocation(planet, x, y) # we can only update info about deposits we can see with our units if not position.is_within_range(unit.vision_range, map_location): continue current_karbonite = gc.karbonite_at(map_location) if current_karbonite == 0: del karbonite_locations[(x, y)] elif karbonite_locations[(x, y)] != current_karbonite: karbonite_locations[(x, y)] = current_karbonite
def get_closest_deposit(gc, unit, karbonite_locations): update_deposit_info(gc, unit, karbonite_locations) planet = bc.Planet(0) position = unit.location.map_location() current_distance = float('inf') closest_deposit = bc.MapLocation(planet, -1, -1) for x, y in karbonite_locations.keys(): map_location = bc.MapLocation(planet, x, y) distance_to_deposit = position.distance_squared_to(map_location) #keep updating current closest deposit to unit if distance_to_deposit < current_distance: current_distance = distance_to_deposit closest_deposit = map_location return closest_deposit
def get_role(gc, my_unit, blueprinting_queue, current_roles, karbonite_locations): my_location = my_unit.location start_map = gc.starting_map(bc.Planet(0)) nearby = gc.sense_nearby_units(my_location.map_location(), my_unit.vision_range) all_factory_count = 0 please_move = False for unit in gc.my_units(): if unit.unit_type == bc.UnitType.Factory: # count ALL factories if my_location.map_location().is_adjacent_to( unit.location.map_location()): please_move = True all_factory_count += 1 for role in current_roles.keys(): if my_unit.id in current_roles[role]: if role == "miner" and please_move: print(my_unit.id, "NEEDS TO MOVE") return "idle" else: return role num_miners = len(current_roles["miner"]) num_blueprinters = len(current_roles["blueprinter"]) num_builders = len(current_roles["builder"]) max_num_blueprinters = 1 #len(blueprinting_queue)*2 + 1 # at least 1 blueprinter, 2 blueprinters per cluster max_num_factories = get_cluster_limit(gc) * 4 num_miners_per_deposit = 2 #approximate, just to cap miner count as deposit number decreases # early game miner production if gc.karbonite() < 100 and num_miners < 2: new_role = "miner" # become blueprinter elif num_blueprinters < max_num_blueprinters and all_factory_count < max_num_factories: new_role = "blueprinter" # default to becoming miner mid game elif num_miners_per_deposit * len(karbonite_locations) > num_miners: new_role = "miner" else: return "idle" current_roles[new_role].append(my_unit.id) return new_role
def mine(gc, unit, karbonite_locations, current_roles): my_location = unit.location position = my_location.map_location() directions = list(bc.Direction) 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 movement.try_move(gc, unit, direction_to_deposit) else: current_roles["miner"].remove(unit.id)
def build(gc, unit, building_assignment, current_roles): my_location = unit.location start_map = gc.starting_map(bc.Planet(0)) assigned_site = building_assignment[unit.id].map_location blueprint_at_site = gc.sense_unit_at_location(assigned_site) assert blueprint_at_site.unit_type == bc.UnitType.Factory or blueprint_at_site.unit_type == bc.UnitType.Rocket if blueprint_at_site.structure_is_built(): print(unit.id, "has finished building a structure at ", assigned_site) current_roles["builder"].remove(unit.id) del building_assignment[unit.id] else: if my_location.map_location().is_adjacent_to(assigned_site): if gc.can_build(unit.id, blueprint_at_site.id): print(unit.id, "is building factory at ", assigned_site) gc.build(unit.id, blueprint_at_site.id) return # if not adjacent move toward it else: #print(unit.id, "is trying to move toward factory at ",assigned_site) direction_to_blueprint = my_location.map_location().direction_to( blueprint_at_site.location.map_location()) movement.try_move(gc, unit, direction_to_blueprint)
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) """
def get_cluster_limit(gc): start_map = gc.starting_map(bc.Planet(0)) return 2 return start_map.width * start_map.height / 100
print(other.unit_type) if(other.unit_type == bc.UnitType.Rocket): rocket_id = other.id # move onto the next unit if other.team != my_team and unit.unit_type == bc.UnitType.Knight and gc.is_attack_ready(unit.id): if gc.can_attack(unit.id, other.id): print('attacked a thing!') gc.attack(unit.id, other.id) if(rocket_id != -1): if(other.unit_type == bc.UnitType.Worker and gc.can_load(rocket_id,other.id)): unit_rocket+=1 gc.load(rocket_id,other.id) if((gc.round() > 700 or unit_rocket > 3) and unit.unit_type == bc.UnitType.Rocket): mapp = gc.starting_map(bc.Planet(1)) for x in range(0,self.mapp.height): for y in range(0,self.map.width): if(gc.can_launch_rocket(unit.id,bc.MapLocation(bc.Planet(1),y,x))): gc.launch_rocket(unit.id,bc.MapLocation(bc.Planet(1),y,x)) #elif gc.is_move_ready(unit.id) and gc.can_move(unit.id, .direction_to(other.location.map_location())): # print('moving unit') # gc.move_robot(unit.id. d) # gc.move_robot(unit.id, gc.direction_to(other.location.map_location()))
def blueprinting_queue_limit(gc): start_map = gc.starting_map(bc.Planet(0)) return 2 return start_map.width * start_map.height / 100
def __init__(self, gc): self.gc = gc p = bc.Planet self.map = gc.starting_map(bc.Planet(0))
# It's a good idea to try to keep your bots deterministic, to make debugging easier. # determinism isn't required, but it means that the same things will happen in every thing you run, # aside from turns taking slightly different amounts of time due to noise. random.seed(6137) # let's start off with some research! # we can queue as much as we want. research.research_step(gc) ##SHARED TEAM INFORMATION## # GENERAL my_team = gc.team() 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)) print('NEXT TO TERRAIN', locs_next_to_terrain) # WORKER blueprinting_queue = [] building_assignment = {} current_worker_roles = {"miner": [], "builder": [], "blueprinter": []} # KNIGHT knight_clusters = list() seen_knights_ids = set() knight_to_cluster = {} ## Remove knights not in cluster KNIGHT_CLUSTER_MIN = 2 # RANGER ranger_roles = {"fighter": [], "sniper": []} ranger_to_cluster = {}
# It's a good idea to try to keep your bots deterministic, to make debugging easier. # determinism isn't required, but it means that the same things will happen in every thing you run, # aside from turns taking slightly different amounts of time due to noise. random.seed(6137) # 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 = {}