def move_and_shrink(unit): # makes the robot move to the least crowded square # hoping that this is expanding the crowd moves = [] location = unit.location for temp in range(9): dir = bc.Direction(temp) # center is direction 8 so it will always be last try: new_location = (location.map_location()).add(dir) nearby_units = gc.sense_nearby_units_by_team( new_location, 2, my_team) moves.append((-1 * len(nearby_units), temp)) except Exception as e: print('Error:', e) # use this to show where the error was traceback.print_exc() moves.sort() for tup in moves: if gc.is_move_ready(unit.id) and gc.can_move( unit.id, bc.Direction(tup[1])): gc.move_robot(unit.id, bc.Direction(tup[1])) #print('Moved successfully!') continue
def try_nearby_directions(goal, skip_exact=False): if not skip_exact: yield goal pos = True p = (goal.value + 1) & 7 n = (goal.value - 1) & 7 while not p == n: if pos: yield bc.Direction(p) p = (p + 1) & 7 pos = False else: yield bc.Direction(n) n = (n - 1) & 7 pos = True yield bc.Direction(p)
def is_clear(state, ml): for i in range(9): direction = bc.Direction(i) new = ml.add(direction) if not state.gc.can_sense_location(new): return False if not state.gc.planet_map.is_passable_terrain_at(new): return False unit = state.gc.sense_unit_at_location(new) if unit is not None and unit.unit_type in [bc.UnitType.Factory, bc.UnitType.Rocket]: return False return True
def move_and_expand(unit): best_dir = bc.Direction(0) less_units = 10 location = unit.location for dir in bc.Direction: new_location = location.add(dir) if new_location.is_on_map(): nearby_units = gc.sense_nearby_units_by_team(new_location.map_location(), 2, my_team) current_units = gc.sense_nearby_units(new_location.map_location(), 1) if nearby_units < less_units and gc.is_passable_terrain: less_units = nearby_units best_dir = dir
def killStatues(state, brawl): enemyStatues = list( state.get_entities(entity_type='statue', team=state.other_team)) for enemyStatue in enemyStatues: enemyX = enemyStatue.location.x enemyY = enemyStatue.location.y moved = False throwed = False tempLock = set() d = [-1, 0, 1] random.shuffle(d) for dx in d: if moved: break for dy in d: breakK = 2 if moved: break if dx == 0 and dy == 0: continue #special handle for k = 1 pickedLocation = battlecode.Location(enemyX + dx, enemyY + dy) throwLocation = battlecode.Location(enemyX + dx * 2, enemyY + dy * 2) if state.map.location_on_map( pickedLocation) and state.map.location_on_map( throwLocation): if brawl: pickedEntity = list( state.get_entities(location=pickedLocation)) else: pickedEntity = list( state.get_entities(location=pickedLocation, team=state.other_team)) if pickedEntity: if pickedEntity[0].can_be_picked: pickedEntity = pickedEntity[0] throwEntity = list( state.get_entities(location=throwLocation)) if throwEntity: throwEntity = throwEntity[0] if throwEntity.team != state.my_team or not throwEntity.is_thrower: break if throwEntity.can_pickup(pickedEntity): throwEntity.queue_pickup(pickedEntity) tempLock.add(throwEntity.id) moved = True if throwEntity.can_throw( battlecode.Direction(-dx, -dy)): throwEntity.queue_throw( battlecode.Direction(-dx, -dy)) throwed = True break else: break for k in range(2, 8): #special check for picking up the entity in line checkLocation = battlecode.Location( enemyX + dx * k, enemyY + dy * k) if state.map.location_on_map(checkLocation): ownEntity = list( state.get_entities(location=checkLocation)) if not ownEntity: continue ownEntity = ownEntity[0] if ownEntity.team != state.my_team or not ownEntity.is_thrower: break if ownEntity.is_holding: if ownEntity.can_throw( battlecode.Direction(-dx, -dy)): ownEntity.queue_throw( battlecode.Direction(-dx, -dy)) moved = True throwed = True break pickupCandidates = ownEntity.entities_within_euclidean_distance( distance=1.9) for pickup in pickupCandidates: if not brawl: if pickup.team == state.my_team: continue if ownEntity.can_pickup(pickup): ownEntity.queue_pickup(pickup) moved = True if ownEntity.can_throw( battlecode.Direction(-dx, -dy)): ownEntity.queue_throw( battlecode.Direction(-dx, -dy)) throwed = True tempLock.add(ownEntity.id) break if moved: break if moved and not throwed: entityList = list( enemyStatue.entities_within_adjacent_distance(breakK * 1.5)) tempLock.add(ent.id for ent in entityList) lockedTargets.union(tempLock)