def timestep(unit): # last check to make sure the right unit type is running this if unit.unit_type != bc.UnitType.Knight: # prob should return some kind of error return gc = variables.gc assigned_knights = variables.assigned_knights quadrant_battles = variables.quadrant_battle_locs unit_locations = variables.unit_locations info = variables.info my_team = variables.my_team enemy_team = variables.enemy_team directions = variables.directions best_loc = None ## (x,y) best_dir = None best_target = None location = unit.location if variables.curr_planet == bc.Planet.Earth: quadrant_size = variables.earth_quadrant_size else: quadrant_size = variables.mars_quadrant_size if location.is_on_map(): if unit.id not in unit_locations: loc = unit.location.map_location() unit_locations[unit.id] = (loc.x, loc.y) f_f_quad = (int(loc.x / quadrant_size), int(loc.y / quadrant_size)) quadrant_battles[f_f_quad].add_ally(unit.id, "knight") variables.knight_attacks[unit.id] = 0 unit_loc = unit_locations[unit.id] ## Movement # If new knight assign to location if unit.id not in assigned_knights: assigned, best_loc = assign_to_quadrant(gc, unit, unit_loc) if not assigned: unit_loc_map = bc.MapLocation(variables.curr_planet, unit_loc[0], unit_loc[1]) best_dir = Ranger.run_towards_init_loc_new( gc, unit, unit_loc_map, variables.direction_to_coord) #nearby = gc.sense_nearby_units_by_team(bc.MapLocation(variables.curr_planet, unit_loc[0], unit_loc[1]), 8, variables.my_team) #best_dir = sense_util.best_available_direction(gc,unit,nearby) else: best_loc = assigned_knights[unit.id] ## Attack based on if in quadrant if best_loc is not None: curr_quadrant = (int(unit_loc[0] / quadrant_size), int(unit_loc[1] / quadrant_size)) best_loc_quadrant = (int(best_loc[0] / quadrant_size), int(best_loc[1] / quadrant_size)) ## If in best quadrant already, then get best_loc towards the target if curr_quadrant == best_loc_quadrant: best_target, best_loc = get_best_target_in_quadrant( gc, unit, unit_loc, knight_unit_priority) else: best_target = get_best_target(gc, unit, unit_loc, knight_unit_priority) ## Do shit # Attack if best_target is not None and gc.can_attack( unit.id, best_target.id ): # checked if ready to attack in get best target gc.attack(unit.id, best_target.id) variables.knight_attacks[unit.id] += 1 # Move if best_loc is not None and gc.is_move_ready( unit.id) and unit_loc != best_loc: try_move_smartly(unit, unit_loc, best_loc) elif best_dir is not None and gc.is_move_ready( unit.id) and gc.can_move(unit.id, best_dir): gc.move_robot(unit.id, best_dir) add_new_location(unit.id, unit_loc, best_dir)
def mage_sense(gc, unit, battle_locs, mage_roles, location, direction_to_coord, bfs_array, targeting_units, rocket_locs): enemies = gc.sense_nearby_units_by_team(location, unit.vision_range, variables.enemy_team) if unit.id in mage_roles["go_to_mars"]: return go_to_mars_sense(gc, unit, battle_locs, location, enemies, direction_to_coord, bfs_array, targeting_units, rocket_locs) signals = {} dir = None attack = None blink = None closest_enemy = None move_then_attack = False visible_enemies = False start_time = time.time() # if variables.print_count < 10: # print("Sensing nearby units:", time.time() - start_time) if len(enemies) > 0: visible_enemies = True start_time = time.time() closest_enemy = None closest_dist = float('inf') for enemy in enemies: loc = enemy.location if loc.is_on_map(): dist = sense_util.distance_squared_between_maplocs( loc.map_location(), location) if dist < closest_dist: closest_dist = dist closest_enemy = enemy # if variables.print_count < 10: # print("Getting closest enemy:", time.time() - start_time) # sorted_enemies = sorted(enemies, key=lambda x: x.location.map_location().distance_squared_to(location)) # closest_enemy = closest_among_ungarrisoned(sorted_enemies) start_time = time.time() attack = Ranger.get_attack(gc, unit, location, targeting_units) # if variables.print_count < 10: # print("Getting attack:", time.time() - start_time) if attack is not None: if closest_enemy is not None: start_time = time.time() if Ranger.check_radius_squares_factories(gc, location): dir = Ranger.optimal_direction_towards( gc, unit, location, closest_enemy.location.map_location()) elif (Ranger.exists_bad_enemy(closest_enemy) ) or not gc.can_attack(unit.id, closest_enemy.id): # if variables.print_count < 10: # print("Checking if condition:", time.time() - start_time) start_time = time.time() dir = sense_util.best_available_direction( gc, unit, [closest_enemy]) # if variables.print_count < 10: # print("Getting best available direction:", time.time() - start_time) # 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 = 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, targeting_units) if attack is not None: move_then_attack = True else: dir = Ranger.run_towards_init_loc_new( gc, unit, location, direction_to_coord) # if variables.print_count < 10: # print("Getting direction:", time.time() - start_time) else: # if there are no enemies in sight, check if there is an ongoing battle. If so, go there. if len(rocket_locs) > 0 and gc.round( ) > 660 and variables.curr_planet == bc.Planet.Earth: dir = Ranger.move_to_rocket(gc, unit, location, direction_to_coord, bfs_array) if dir is not None: return dir, attack, blink, move_then_attack, visible_enemies, closest_enemy, signals if len(battle_locs) > 0: # print('IS GOING TO BATTLE') dir = Ranger.go_to_battle_new(gc, unit, battle_locs, location, direction_to_coord) # queued_paths[unit.id] = target else: # dir = move_away(gc, unit, battle_locs) if variables.curr_planet == bc.Planet.Earth: # print('IS RUNNING TOWARDS INIT LOC') dir = Ranger.run_towards_init_loc_new(gc, unit, location, direction_to_coord) else: # print('EXPLORING') dir = Ranger.get_explore_dir(gc, unit, location) # if variables.print_count < 10: # print("regular movement:", time.time() - start_time) return dir, attack, blink, move_then_attack, visible_enemies, closest_enemy, signals
def knight_sense(gc, unit, battle_locs, knight_roles, location, direction_to_coord, bfs_array, targeting_units, rocket_locs): enemies = gc.sense_nearby_units_by_team(location, unit.vision_range, variables.enemy_team) if unit.id in knight_roles["go_to_mars"]: return go_to_mars_sense(gc, unit, battle_locs, location, enemies, direction_to_coord, bfs_array, targeting_units, rocket_locs) signals = {} dir = None attack = None javelin = None closest_enemy = None move_then_attack = False visible_enemies = False # if variables.print_count < 10: # print("Sensing nearby units:", time.time() - start_time) if len(enemies) > 0: visible_enemies = True closest_enemy = None closest_dist = -float('inf') for enemy in enemies: enemy_loc = enemy.location if enemy_loc.is_on_map(): enemy_map_loc = enemy_loc.map_location() coeff = Ranger.coefficient_computation(gc, unit, enemy, enemy_map_loc, location) # dist = sense_util.distance_squared_between_maplocs(loc.map_location(), location) if coeff > closest_dist: closest_dist = coeff closest_enemy = enemy # if variables.print_count < 10: # print("Getting closest enemy:", time.time() - start_time) # 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, targeting_units, knight_unit_priority) if attack is not None: if closest_enemy is not None: dir = Ranger.go_to_loc(unit, location, closest_enemy.location.map_location()) # optimal_direction_towards(gc, unit, location, closest_enemy.location.map_location()) else: if gc.is_move_ready(unit.id): if closest_enemy is not None: dir = Ranger.go_to_loc(unit, location, closest_enemy.location.map_location()) # optimal_direction_towards(gc, unit, location, closest_enemy.location.map_location()) if dir is None or dir == variables.directions[8]: dir = Ranger.optimal_direction_towards(gc, unit, location, closest_enemy.location.map_location()) next_turn_loc = location.add(dir) attack = get_attack(gc, unit, next_turn_loc, targeting_units, knight_unit_priority) if attack is not None: move_then_attack = True else: if variables.curr_planet == bc.Planet.Earth: # print('IS RUNNING TOWARDS INIT LOC') dir = Ranger.run_towards_init_loc_new(gc, unit, location, direction_to_coord) else: # print('EXPLORING') dir = Ranger.get_explore_dir(gc, unit, location) # if variables.print_count < 10: # print("Getting direction:", time.time() - start_time) else: # if there are no enemies in sight, check if there is an ongoing battle. If so, go there. if len(rocket_locs) > 0 and gc.round() > 660 and variables.curr_planet == bc.Planet.Earth: dir = Ranger.move_to_rocket(gc, unit, location, direction_to_coord, bfs_array) if dir is not None: return dir, attack, javelin, move_then_attack, visible_enemies, closest_enemy, signals if len(battle_locs) > 0: # print('IS GOING TO BATTLE') dir = Ranger.go_to_battle_new(gc, unit, battle_locs, location, direction_to_coord) # queued_paths[unit.id] = target else: # dir = move_away(gc, unit, battle_locs) if variables.curr_planet == bc.Planet.Earth: # print('IS RUNNING TOWARDS INIT LOC') dir = Ranger.run_towards_init_loc_new(gc, unit, location, direction_to_coord) else: # print('EXPLORING') dir = Ranger.get_explore_dir(gc, unit, location) # if variables.print_count < 10: # print("regular movement:", time.time() - start_time) return dir, attack, javelin, move_then_attack, visible_enemies, closest_enemy, signals
def timestep_complex(unit): gc = variables.gc composition = variables.info direction_to_coord = variables.direction_to_coord bfs_array = variables.bfs_array enemy_team = variables.enemy_team my_team = variables.my_team assigned_healers = variables.assigned_healers ## healer id: (cluster, best_healer_loc) assigned_overcharge = variables.assigned_overcharge overcharge_targets = variables.overcharge_targets quadrant_battles = variables.quadrant_battle_locs if variables.curr_planet == bc.Planet.Earth: quadrant_size = variables.earth_quadrant_size else: quadrant_size = variables.mars_quadrant_size unit_locations = variables.unit_locations best_dir = None best_loc = None best_heal_target = None best_overcharge_target = None heal = False overcharge = False location = unit.location if location.is_on_map(): #print(location.map_location()) ## Add new ones to unit_locations, else just get the location if unit.id not in unit_locations: loc = unit.location.map_location() unit_locations[unit.id] = (loc.x,loc.y) f_f_quad = (int(loc.x / quadrant_size), int(loc.y / quadrant_size)) quadrant_battles[f_f_quad].add_ally(unit.id, "healer") unit_loc = unit_locations[unit.id] ## Assign role if unit.id not in assigned_overcharge and unit.id not in assigned_healers: overcharge_to_total = 1 total = len(assigned_healers) + len(assigned_overcharge) if total > 0: overcharge_to_total = len(assigned_overcharge) / total # Assign to overcharge if there are targets and ratio is good if variables.research.get_level(variables.unit_types["healer"]) == 3 and overcharge_to_total < 0.2 and len(overcharge_targets) > 0: best_overcharge_target = gc.unit(overcharge_targets.pop()) assigned_overcharge[unit.id] = best_overcharge_target if gc.is_heal_ready(unit.id): best_heal_target = get_best_target(gc, unit, unit_loc, my_team) if best_heal_target is not None: heal = True assigned, best_loc = assign_to_quadrant(gc, unit, unit_loc) if not assigned: unit_loc_map = bc.MapLocation(variables.curr_planet, unit_loc[0], unit_loc[1]) best_dir = Ranger.run_towards_init_loc_new(gc, unit, unit_loc_map, variables.direction_to_coord) ## Overcharge if unit.id in assigned_overcharge: ally = assigned_overcharge[unit.id] if gc.can_overcharge(unit.id, ally.id): overcharge = True best_overcharge_target = ally if best_heal_target is None and gc.is_heal_ready(unit.id): best_heal_target = get_best_target(gc, unit, unit_loc, my_team) if best_heal_target is not None: heal = True ## Movement # If sees enemies close enough then tries to move away from them loc = bc.MapLocation(variables.curr_planet, unit_loc[0], unit_loc[1]) enemies = gc.sense_nearby_units_by_team(loc, unit.vision_range, enemy_team) if len(enemies) > 0: enemies = sorted(enemies, key=lambda x: x.location.map_location().distance_squared_to(loc)) enemy_loc = enemies[0].location.map_location() battle_quadrant = (int(enemy_loc.x/5), int(enemy_loc.y/5)) if battle_quadrant not in variables.next_turn_battle_locs: variables.next_turn_battle_locs[battle_quadrant] = (enemy_loc, 1) best_dir = dir_away_from_enemy(gc, unit, loc, enemy_loc) elif variables.curr_round > 650: best_dir = Ranger.move_to_rocket(gc, unit, loc, variables.direction_to_coord, variables.bfs_array) # Otherwise, goes to locations in need of healers else: if unit.id in assigned_overcharge: ally = assigned_overcharge[unit.id] best_loc_map = ally.location.map_location() best_loc = (best_loc_map.x, best_loc_map.y) elif unit.id in assigned_healers: best_loc = assigned_healers[unit.id][1] ## Do shit #print(best_dir) #print(best_loc) if best_overcharge_target is not None: if overcharge and gc.is_overcharge_ready(unit.id): gc.overcharge(unit.id, best_overcharge_target.id) overcharged_unit = gc.unit(best_overcharge_target.id) if overcharged_unit.unit_type == variables.unit_types["ranger"]: Ranger.timestep(overcharged_unit) if best_heal_target is not None: if heal and gc.can_heal(unit.id, best_heal_target.id): gc.heal(unit.id, best_heal_target.id) if best_dir is not None and gc.is_move_ready(unit.id) and gc.can_move(unit.id,best_dir): gc.move_robot(unit.id, best_dir) add_new_location(unit.id, unit_loc, best_dir) elif best_loc is not None and gc.is_move_ready(unit.id) and unit_loc != best_loc: try_move_smartly(unit, unit_loc, best_loc)