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 board(gc, my_unit, current_roles): my_location = my_unit.location.map_location() finished_rockets = [] for unit in gc.my_units(): if unit.unit_type == bc.UnitType.Rocket and unit.structure_is_built( ) and len(unit.structure_garrison()) < unit.structure_max_capacity(): finished_rockets.append(unit) minimum_distance = float('inf') closest_rocket = None for rocket in finished_rockets: dist_to_rocket = my_location.distance_squared_to( rocket.location.map_location()) if dist_to_rocket < minimum_distance: minimum_distance = dist_to_rocket closest_rocket = rocket if closest_rocket is None: current_roles["boarder"].remove(my_unit.id) return rocket_location = closest_rocket.location.map_location() if my_location.is_adjacent_to(rocket_location): if gc.can_load(closest_rocket.id, my_unit.id): #print(unit.id, 'loaded') gc.load(closest_rocket.id, my_unit.id) current_roles["boarder"].remove(my_unit.id) else: #print(unit.id, 'moving toward rocket') direction_to_rocket = my_location.direction_to(rocket_location) movement.try_move(gc, my_unit, direction_to_rocket)
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 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)
def repair(gc, unit, current_roles): map_loc = unit.location.map_location() closest = None closest_dist = float('inf') for fact in gc.my_units(): if fact.unit_type == bc.UnitType.Factory: if fact.structure_is_built() and fact.health < fact.max_health: loc = fact.location.map_location() dist = map_loc.distance_squared_to(loc) if dist < closest_dist: closest = fact closest_dist = dist if closest != None: if gc.can_repair(unit.id, closest.id): gc.repair(unit.id, closest.id) else: dir = map_loc.direction_to(closest.location.map_location()) movement.try_move(gc, unit, dir) else: current_roles["repairer"].remove(unit.id)
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)
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 blueprint(gc, my_unit, blueprinting_queue, building_assignment, blueprinting_assignment, current_roles): my_location = my_unit.location.map_location() directions = list(bc.Direction) blueprint_spacing = 10 nearby = gc.sense_nearby_units(my_location, blueprint_spacing) is_nearby_building = False is_nearby_potential_buildings = False # if it finds a nice location for factory cluster, put it in queue if len(blueprinting_queue) < blueprinting_queue_limit(gc): # print("blueprinting_queue",blueprinting_queue) # print(is_valid_blueprint_location(gc,my_location,blueprinting_queue,blueprinting_assignment)) if is_valid_blueprint_location(gc, my_location, blueprinting_queue, blueprinting_assignment): if can_blueprint_rocket(gc, blueprinting_queue): new_site = BuildSite(my_location, bc.UnitType.Rocket) blueprinting_queue.append(new_site) elif can_blueprint_factory(gc, blueprinting_queue): new_site = BuildSite(my_location, bc.UnitType.Factory) blueprinting_queue.append(new_site) print(my_unit.id, " just added to building queue", my_location) # assign this unit to build a blueprint, if nothing to build just move away from other factories if my_unit.id not in blueprinting_assignment: # print(my_unit.id,"currently has no assigned site") current_roles["blueprinter"].remove(my_unit.id) """ all_buildings = [] for other in gc.my_units(): if other.unit_type == bc.UnitType.Factory or other.unit_type == bc.UnitType.Rocket: all_buildings.append(other) away_from_buildings = sense_util.best_available_direction(gc,my_unit,all_buildings) # pick other direction if direction is center if away_from_buildings == bc.Direction.Center: away_from_buildings = bc.Direction.North movement.try_move(gc,my_unit,away_from_buildings) #print(unit.id, " is exploring the map for build sites") """ # build blueprint in assigned square if my_unit.id in blueprinting_assignment: assigned_site = blueprinting_assignment[my_unit.id] # if my_unit.id in blueprinting_assignment: # print("unit",my_unit.id,"blueprinting at",blueprinting_assignment[my_unit.id]) #print(unit.id, "is assigned to building in", assigned_site.map_location) direction_to_site = my_location.direction_to( assigned_site.map_location) if my_location.is_adjacent_to(assigned_site.map_location): if gc.can_blueprint(my_unit.id, assigned_site.building_type, direction_to_site): gc.blueprint(my_unit.id, assigned_site.building_type, direction_to_site) del blueprinting_assignment[my_unit.id] current_roles["blueprinter"].remove(my_unit.id) current_roles["builder"].append(my_unit.id) # print(my_unit.id, " just created a blueprint!") #else: #print(unit.id, "can't build but is right next to assigned site") elif my_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, my_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.direction_to( assigned_site.map_location) movement.try_move(gc, my_unit, next_direction)
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)