def find_dimensions(current_planet): low = 19 high = 49 ansx = 19 ansy = 19 planet_map = gc.starting_map(current_planet) while (low <= high): med = (low + high)//2 temp_location = bc.MapLocation(current_planet, med, 0) if planet_map.on_map(temp_location): if ansx < med: ansx = med low = med+1 else: high = med-1 low = 19 high = 49 while (low <= high): med = (low + high)//2 temp_location = bc.MapLocation(current_planet, 0, med) if planet_map.on_map(temp_location): if ansy < med: ansy = med low = med+1 else: high = med-1 return (ansx, ansy)
def neighbors(current, gamemap, gc): nearby = [] for i in range(current.x - 1, current.x + 1): for j in range(current.y - 1, current.y + 1): if not i == current.x and not j == current.y and gamemap.on_map(bc.MapLocation(gc.planet(), i, j)): nearby.append(bc.MapLocation(gc.planet(), i, j)) return nearby
def set_enemy_dir(gc, home_loc): #get map_dimesions height, width = gc.starting_map(bc.Planet.Earth).height, gc.starting_map( bc.Planet.Earth).width #set corners ul_corner, ur_corner, bl_corner, br_corner = bc.MapLocation( bc.Planet.Earth, 1, height), bc.MapLocation(bc.Planet.Earth, width, height), bc.MapLocation( bc.Planet.Earth, 1, 1), bc.MapLocation(bc.Planet.Earth, width, 1) best_corner = ul_corner for corner in [ul_corner, ur_corner, bl_corner, br_corner]: if home_loc.distance_squared_to( best_corner) > home_loc.distance_squared_to(corner): best_corner = corner if best_corner == ul_corner: enemy_corner = br_corner elif best_corner == ur_corner: enemy_corner = bl_corner elif best_corner == bl_corner: enemy_corner = ur_corner elif best_corner == br_corner: enemy_corner = ul_corner enemy_dir = home_loc.direction_to(enemy_corner) return enemy_dir
def followWall(self, loc, dLoc, unit): branches = [] nBranches = [] temp = [] walls = self.checkWalls(loc, unit) if len(walls) == 0: print("No walls. How") elif len(walls) == 1: b = Branch(walls[0], loc) nBranches = nBranches + b.extend() elif len(walls) == 2: #print("Welp: " + str(walls) + " | " + str(unit.id)) #That should be the corner piece between the two walls wall = int((walls[0] + walls[1]) / (2)) b = Branch(wall, loc) nBranches = nBranches + b.extend() elif len(walls) == 3: bWalls = [0, 2, 4, 6] for w in walls: if w == 0: del bWalls[0] elif w == 2: del bWalls[1] elif w == 4: del bWalls[2] else: del bWalls[3] wallDir = bWalls[0] - 2 if wallDir < 0: wallDir = wallDir + 8 b = Branch(wallDir, loc) nBranches = nBranches + b.extend() #print("I dunno") while True: #print("BLEH") temp = [] #If a unit has moved to the destination location exit if gc.has_unit_at_location(dLoc): #print("Um") return [bc.MapLocation(bc.Planet.Earth, -1, -1)] for i in range(0, len(nBranches), 2): #Every other value is the direction in which to extend the branch dest = self.canMoveToDestination( [], nBranches[i].getStartingLocation(), dLoc, unit, nBranches[i], nBranches[i + 1]) #print("Destination: " + str(dLoc) + " | " + str(i)) if i > 400: return [bc.MapLocation(bc.Planet.Earth, -1, -1)] if dest[1] == False: #print("RIP") temp = temp + dest[2] else: keyLocations = nBranches[i].getPath([]) + [dLoc] #print("KeyLocations: " + str(keyLocations) + " | " + str(unit.id)) return keyLocations nBranches = temp branches.append(temp)
def mapDimensions(planet): minCoor = 19 maxCoor = 49 coorX = 19 coorY = 19 planetMap = gc.starting_map(planet) while(minCoor <= maxCoor): median = (minCoor + maxCoor)//2 checkSpot = bc.MapLocation(planet, median, 0) if planetMap.on_map(checkSpot): if coorX < median: coorX = median minCoor = median + 1 else: maxCoor = median - 1 minCoor = 19 maxCoor = 49 while(minCoor <= maxCoor): median = (minCoor + maxCoor)//2 checkSpot = bc.MapLocation(planet, 0, median) if planetMap.on_map(checkSpot): if coorY < median: coorY = median minCoor = median + 1 else: maxCoor = median - 1 return(coorX,coorY)
def __init__(self, wallDirection, startingLoc): self.wallDirection = wallDirection self.startingLoc = startingLoc self.direction = self.tDirection(wallDirection) self.nodes = [] self.branches = [] if self.startingLoc != bc.MapLocation(bc.Planet.Earth, -1, -1): self.pBranch = Branch(-1, bc.MapLocation(bc.Planet.Earth, -1, -1))
def get_karbonite_deposits(): try: current_karbonite_earth = {} for x in range(earth_map.width): for y in range(earth_map.height): if gc.can_sense_location(bc.MapLocation(bc.Planet.Earth, x, y)): current_karbonite_earth[(x,y)] = gc.karbonite_at(bc.MapLocation(bc.Planet.Earth,x,y)) return current_karbonite_earth except Exception as e: print('Error:', e) traceback.print_exc()
def move(self, loc, dir): test1 = bc.MapLocation(bc.Planet.Earth, loc.x + dir[0], loc.y + dir[1]) test2 = bc.MapLocation(bc.Planet.Earth, loc.x + dir[0], loc.y) test3 = bc.MapLocation(bc.Planet.Earth, loc.x, loc.y + dir[1]) if EarthMap.is_passable_terrain_at( test1) and EarthMap.is_passable_terrain_at( test2) and EarthMap.is_passable_terrain_at( test3) and not self.checkUnit(dir, loc): return test1 else: return bc.MapLocation(bc.Planet.Earth, -1, -1)
def tDirection(self, dir): if self.startingLoc == bc.MapLocation(bc.Planet.Earth, -1, -1): return None directions = [] if self.wallDirection % 2 == 0: dir = dir - 2 if dir < 0: dir = dir + 8 directions.append(dir) dir = directions[0] - 4 if dir < 0: dir = dir + 8 directions.append(dir) return directions else: #This means it's a corner piece. Only give when colliding with two walls or just a corner wall dir = self.convertDirection(self.wallDirection - 1) testLocation = bc.MapLocation(bc.Planet.Earth, self.startingLoc.x + dir[0], self.startingLoc.y + dir[1]) #Passible terrain at this spot means it's only a corner piece if EarthMap.is_passable_terrain_at( testLocation ) and not gc.has_unit_at_location(testLocation): dir = self.wallDirection - 1 if dir < 0: dir = dir + 8 directions.append(dir) dir = directions[0] + 1 if dir > 7: dir = dir - 8 directions.append(dir) else: #This means it must be in a corner with two walls to it dir = self.wallDirection - 3 if dir < 0: dir = dir + 8 directions.append(dir) dir = self.wallDirection + 3 if dir > 7: dir = dir - 8 directions.append(dir) #print("AHHHH: " + str(directions)) return directions
def launch(rocket): launched = 0 while launched == 0: landing_site = random.choice(lst_of_passable_mars) #print("LANDING SITE: {}".format(landing_site)) if gc.can_launch_rocket( rocket.id, bc.MapLocation(mars, landing_site[0], landing_site[1])): gc.launch_rocket( rocket.id, bc.MapLocation(mars, landing_site[0], landing_site[1])) launched = 1 return
def adjacentLocation(loc): placesOnMap = [] newx = loc.x newy = loc.y North = bc.MapLocation(bc.Planet.Earth,newx, newy + 1) South = bc.MapLocation(bc.Planet.Earth,newx, newy - 1) East = bc.MapLocation(bc.Planet.Earth,newx + 1, newy) West = bc.MapLocation(bc.Planet.Earth,newx - 1, newy) NorthEast = bc.MapLocation(bc.Planet.Earth,newx + 1, newy + 1) NorthWest = bc.MapLocation(bc.Planet.Earth,newx - 1, newy + 1) SouthEast = bc.MapLocation(bc.Planet.Earth,newx + 1, newy - 1) SouthWest = bc.MapLocation(bc.Planet.Earth,newx - 1, newy - 1) placesOnMap.append(North) placesOnMap.append(South) placesOnMap.append(East) placesOnMap.append(West) placesOnMap.append(NorthEast) placesOnMap.append(NorthWest) placesOnMap.append(SouthEast) placesOnMap.append(SouthWest) return placesOnMap
def checkUnit(self, dir, pLoc): if dir[0] != 0: loc = bc.MapLocation(bc.Planet.Earth, pLoc.x + dir[0], pLoc.y) if gc.has_unit_at_location(loc): return True if dir[1] != 0: loc = bc.MapLocation(bc.Planet.Earth, pLoc.x, pLoc.y + dir[1]) if gc.has_unit_at_location(loc): return True if dir[0] != 0 and dir[1] != 0: loc = bc.MapLocation(bc.Planet.Earth, pLoc.x + dir[0], pLoc.y + dir[1]) if gc.has_unit_at_location(loc): return True return False
def initiate_maps(): for x in range(earth_width): for y in range(earth_height): coords = (x, y) if x == -1 or y == -1 or x == earth_map.width or y == earth_map.height: passable_locations_earth[coords] = False elif earth_map.is_passable_terrain_at(bc.MapLocation(earth, x, y)): passable_locations_earth[coords] = True else: passable_locations_earth[coords] = False lst_of_passable_earth = [ loc for loc in passable_locations_earth if passable_locations_earth[loc] ] lst_of_impassable_earth = [ loc for loc in passable_locations_earth if not passable_locations_earth[loc] ] #print("EARTH: {}".format(lst_of_impassable_earth)) mars_map = gc_file.gc.starting_map(bc.Planet.Mars) mars_width = mars_map.width mars_height = mars_map.height passable_locations_mars = {} for x in range(mars_width): for y in range(mars_height): coords = (x, y) if x == -1 or y == -1 or x == mars_map.width or y == mars_map.height: passable_locations_mars[coords] = False elif mars_map.is_passable_terrain_at(bc.MapLocation(mars, x, y)): passable_locations_mars[coords] = True else: passable_locations_mars[coords] = False #print(passable_locations_mars) #print(passable_locations_earth) for loc in passable_locations_mars: if passable_locations_mars[loc] == True: lst_of_passable_mars.append(loc) for loc in passable_locations_mars: if passable_locations_mars[loc] == False: lst_of_impassable_mars.append(loc)
def __create_new_combat_mission__(self): if True: chance = random.randint(1, 100) if chance < 0: new_mission = Mission() new_mission.action = Missions.RandomMovement return new_mission elif chance > 0: print("Creating combat mission") new_mission = Mission() new_mission.action = Missions.DestroyTarget new_mission.info = MissionInfo new_mission.map_location = self.map_controller.enemy_team_start[ 0] elif chance > 25: new_mission = Mission() new_mission.action = Missions.Patrol new_mission.info = MissionInfo() map_location = bc.MapLocation(self.game_controller.planet(), 0, 0) #TODO better patrol location map_location.x = random.randint(0, 20) map_location.y = random.randint(0, 20) new_mission.info.map_location = map_location return new_mission else: new_mission = Mission() new_mission.action = Missions.Idle return new_mission
def rocketWork(rocket, c, gc, marsx, marsy): if rocket.location.map_location().planet == bc.Planet.Earth and ( len(rocket.structure_garrison()) == rocket.structure_max_capacity() or gc.round == 749): mars_map = gc.starting_map(bc.Planet.Mars) xlist = list(range(marsx)) shuffle(xlist) for x in xlist: ylist = list(range(marsy)) shuffle(ylist) for y in ylist: target = bc.MapLocation(bc.Planet.Mars, x, y) if mars_map.on_map(target): if gc.can_launch_rocket(rocket.id, target): if gc.has_unit_at_location(target): if gc.sense_unit_at_location( target).team == rocket.team: continue gc.launch_rocket(rocket.id, target) print("rocket launched") elif rocket.location.map_location().planet == bc.Planet.Mars: if len(rocket.structure_garrison()) > 0: for d in list(bc.Direction): if gc.can_unload(rocket.id, d): gc.unload(rocket.id, d)
def InitializeMarsMap(self): try: print("Initializing Mars Map") self.marsMap = self.gameController.starting_map(bc.Planet.Mars) self.mars_map = [] print(self.marsMap.width) for mapX in range(self.marsMap.width): self.mars_map.append([]) for mapY in range(self.marsMap.height): mapLoc = bc.MapLocation(bc.Planet.Mars, mapX, mapY) self.mars_map[mapX].append({ "x": mapX, "y": mapY, "hash": self.hashCoordinates(mapX, mapY), "isPassable": self.marsMap.is_passable_terrain_at(mapLoc), "karboniteCount": self.marsMap.initial_karbonite_at(mapLoc) }) #print(self.marsMap.planet) except Exception as e: print('Error:', e) # use this to show where the error was traceback.print_exc()
def on_turn_one(gc): earth_map = gc.starting_map(bc.Planet.Earth) x, y = earth_map.width, earth_map.height started_with_karbonite = [] for x in range(x): for y in range(y): test_location = bc.MapLocation(bc.Planet.Earth, x, y) if earth_map.initial_karbonite_at(test_location) > 0: started_with_karbonite.append(test_location) attack_dir = None breaker = 0 #set workers needed and factories needed workers_needed = 4 factories_needed = 3 #home is location of our worker initially my_units = gc.my_units() if len(my_units) != 0: new_loc = my_units[0].location.map_location() #find enemy direction enemy_dir = set_enemy_dir(gc, new_loc) #then set spaced out home home_loc = find_home_loc(gc, new_loc, enemy_dir) queue_all_research(gc) #set enemy direction print("enemy_dir", enemy_dir) print() print("home_loc", home_loc) return started_with_karbonite, attack_dir, breaker, workers_needed, factories_needed, enemy_dir, home_loc
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 generate_factory_locations(start_map, center, locs_next_to_terrain): x = center.x y = center.y planet = center.planet biggest_cluster = [] relative_block_locations = [(0, 1), (1, 1), (1, 0), (1, -1), (0, -1), (-1, -1), (-1, 0), (-1, 1)] if not is_valid_blueprint_location(start_map, center, locs_next_to_terrain): return [] for i in range(4): cluster = [center] for j in range(3): d = relative_block_locations[(2 * i + j) % 8] block_location = bc.MapLocation(planet, x + d[0], y + d[1]) if is_valid_blueprint_location(start_map, block_location, locs_next_to_terrain): cluster.append(block_location) if len(cluster) > len(biggest_cluster): biggest_cluster = cluster #print("center: ",center) #print("BIGGEST CLUSTER",biggest_cluster) return biggest_cluster
def move_close_to(self, worker: bc.Unit, p_loc: bc.MapLocation) -> None: worker_loc = worker.location.map_location() loc_near = find_empty_loc_near(self.gc, self.gc.starting_map(bc.Planet.Earth), p_loc) if loc_near is not None: # print(f'Try to move worker from {worker.location.map_location()} to {loc_near}') path_to_loc = a_star_search(self.gc, self.gc.starting_map(bc.Planet.Earth), worker_loc, loc_near) if path_to_loc is not None: path_to_loc = list(path_to_loc) else: print( f'Path not found {worker.location.map_location()} to {loc_near}' ) if path_to_loc is not None and len(path_to_loc) > 1: # print(f'Path found {path_to_loc}') next_node = path_to_loc[1] next_dir = worker_loc.direction_to( bc.MapLocation(self.gc.planet(), next_node[0], next_node[1])) if self.gc.can_move(worker.id, next_dir) and self.gc.is_move_ready( worker.id): # print('Worker move') self.gc.move_robot(worker.id, next_dir)
def update_enemies(self, gc): ## Reset self.enemies = set() if variables.curr_planet == bc.Planet.Earth: max_width = variables.earth_start_map.width max_height = variables.earth_start_map.height else: max_width = variables.mars_start_map.width max_height = variables.mars_start_map.height ## Find enemies in quadrant # If enemy in location that can't be sensed don't erase it yet for i in range(self.quadrant_size): x = self.bottom_left[0] + i for j in range(self.quadrant_size): y = self.bottom_left[1] + j loc = (x,y) if loc[0] < max_width and loc[1] < max_height: map_loc = bc.MapLocation(variables.curr_planet, x, y) if gc.can_sense_location(map_loc): if gc.has_unit_at_location(map_loc): unit = gc.sense_unit_at_location(map_loc) if unit.team == variables.enemy_team and unit.unit_type != bc.UnitType.Worker: self.enemy_locs[loc] = unit self.enemies.add(unit.id) elif loc in self.enemy_locs: del self.enemy_locs[loc] elif loc in self.enemy_locs: self.enemies.add(self.enemy_locs[loc].id)
def blink_attack_earth(unit): if not gc.is_blink_ready(unit.id): return if bc.ResearchInfo.get_level(bc.UnitType.Mage) < 4: return location = unit.location possible_targets = sense_nearby_units_by_team(location.map_location(), 2, enemy_team) if len(possible_targets) > 2: return for guess in range(NUMBER_OF_GUESSES): i = random.randint(0, earthHeight-1) j = random.randint(0, earthWidth-1) try: temp_location = bc.MapLocation(bc.Planet.Earth, i, j) if gc.can_blink(unit.id, temp_location): gc.blink(unit.id, temp_location) return except Exception as e: print('Error:', e) # use this to show where the error was traceback.print_exc()
def make_location(gc,x,y): """ makes a map location on earth """ map_location = bc.MapLocation(bc.Planet.Earth,x,y) return map_location
def rocketLogic(): if unit.location.is_on_planet(bc.Planet.Mars): myDirections = pathFinding.whereShouldIGo( myMap, unit.location.map_location().x, unit.location.map_location().y) for d in myDirections: if gc.can_unload(unit.id, d): gc.unload(unit.id, d) elif unit.location.is_on_planet(bc.Planet.Earth): #TODO:wait until has someone in before launch garrison = len(unit.structure_garrison()) #print("waitin on friends") if garrison > 0: if len(landingZones) > 0: myx = landingZones[0][0] myy = landingZones[0][1] print("im going where im told") else: myx = unit.location.map_location().x myy = unit.location.map_location().y print("we lazy") destination = bc.MapLocation(bc.Planet.Mars, myx, myy) print("we takin off boys") #TODO:make sure destination is a valid landing zone, currently keeps x,y from earth if gc.can_launch_rocket(unit.id, destination): del landingZones[0] gc.launch_rocket(unit.id, destination) return
def update_quadrants(): gc = variables.gc battle_quadrants = variables.quadrant_battle_locs battle_locs = variables.last_turn_battle_locs if variables.curr_planet == bc.Planet.Earth: quadrant_size = variables.earth_quadrant_size else: quadrant_size = variables.mars_quadrant_size already_included_quadrants = set() for q_loc in battle_locs: map_loc = battle_locs[q_loc][0] my_quadrant = (int(map_loc.x/quadrant_size), int(map_loc.y/quadrant_size)) already_included_quadrants.add(my_quadrant) for quadrant in battle_quadrants: q_info = battle_quadrants[quadrant] q_info.reset_num_died() new_battle_locs, ranged = q_info.update_enemies(gc) variables.ranged_enemies += ranged if quadrant not in already_included_quadrants and len(new_battle_locs) > 0: loc = new_battle_locs[0] quadrant_loc = (int(loc[0]/5),int(loc[1]/5)) battle_locs[quadrant_loc] = (bc.MapLocation(variables.curr_planet,loc[0],loc[1]), 1) if variables.update_quadrant_healer_loc: for quadrant in battle_quadrants: q_info = battle_quadrants[quadrant] q_info.update_healer_locs()
def get_random_map_location( planet, planet_width, planet_height): #Planet, int, int => MapLocation x = random.randint(0, planet_width - 1) y = random.randint(0, planet_height - 1) return bc.MapLocation(planet, x, y)
def rocket_logic(unit): if unit.location.is_on_planet(bc.Planet.Earth): if len(unit.structure_garrison()) > (750 - gc.round()) / 150: marsLocation = bc.MapLocation(bc.Planet.Mars, random.randint(0, 20), random.randint(0, 20)) if gc.can_launch_rocket(unit.id, marsLocation): gc.launch_rocket(unit.id, marsLocation) print( 'MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM rocket launched!' ) else: for robot in gc.my_units(): if gc.can_load(unit.id, robot.id): gc.load(unit.id, robot.id) print( 'MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM robot loaded to a rocket!' ) else: for direction in directions: if gc.can_unload(unit.id, direction): gc.unload(unit.id, direction) print( 'MMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMMM unloaded a rocket!' ) break
def InitializeEarthMap(self): try: print("Initializing Earth map") self.map = self.gameController.starting_map(bc.Planet.Earth) #print(self.map.width) #print(self.map.height) for unit in self.map.initial_units: if unit.team == self.gameController.team(): if len(self.my_team_start) == 0: self.my_team_start.append(unit.location.map_location()) else: if len(self.enemy_team_start) == 0: self.enemy_team_start.append(unit.location.map_location()) print(self.my_team_start) self.earth_Map = [] for mapX in range(self.map.width): self.earth_map.append([]) for mapY in range(self.map.height): mapLoc = bc.MapLocation(bc.Planet.Earth,mapX, mapY) self.earth_map[mapX].append({"x": mapX, "y": mapY, "hash": self.hashCoordinates(mapX, mapY), "isPassable": self.map.is_passable_terrain_at(mapLoc), "karboniteCount": self.map.initial_karbonite_at(mapLoc)}) #print(self.map.planet) except Exception as e: print('Error:', e) # use this to show where the error was traceback.print_exc()
def get_best_target_in_quadrant(gc, unit, loc_coords, priority_order): """ Returns best_target, best_loc to move towards. """ enemy_team = variables.enemy_team location = bc.MapLocation(variables.curr_planet, loc_coords[0], loc_coords[1]) vuln_enemies = gc.sense_nearby_units_by_team(location, unit.attack_range(), enemy_team) # If there is a vuln enemy but can't attack then don't move if len(vuln_enemies) > 0: if not gc.is_attack_ready(unit.id): return (None, None) else: best_target = max(vuln_enemies, key=lambda x: attack.coefficient_computation( gc, unit, x, location, priority_order)) return (best_target, None) # If there are no vuln enemies then look at vision range and choose to move towards nearby enemies else: vuln_enemies = gc.sense_nearby_units_by_team(location, unit.vision_range, enemy_team) if len(vuln_enemies) > 0: best_target = max( vuln_enemies, key=lambda x: attack.coefficient_computation( gc, unit, x, location, priority_order, far=True)) best_loc = (best_target.location.map_location().x, best_target.location.map_location().y) return (None, best_loc) return (None, None)
def navigate_unit_to(gc: bc.GameController, unit: bc.Unit, target_location: bc.MapLocation) -> bool: # print(f'navigating to {target_location}') unit_location = unit.location.map_location() if unit_location.is_adjacent_to(target_location): # print('Unit is adjacent to target location.') return True else: # Worker is too far, move it closer # print('Unit is too far, move it closer') loc_near = find_empty_loc_near(gc, gc.starting_map(bc.Planet.Earth), target_location) if loc_near is not None: # print(f'Try to move unit from {unit.location.map_location()} to {loc_near}') a_star_result = a_star_search( gc, gc.starting_map(bc.Planet.Earth), unit_location, target_location ) if a_star_result is None: path_to_loc = None else: path_to_loc = list(a_star_result) if path_to_loc is not None and len(path_to_loc) > 1: # print(f'Path found {path_to_loc}') next_node = path_to_loc[1] next_dir = unit_location.direction_to(bc.MapLocation(gc.planet(), next_node[0], next_node[1])) if gc.can_move(unit.id, next_dir) and gc.is_move_ready(unit.id): # print(f'Unit {unit.id} move to {next_node}') gc.move_robot(unit.id, next_dir) return False