def timestep(unit): # last check to make sure the right unit type is running this if unit.unit_type != bc.UnitType.Healer: return # print('HEALER ID: ', unit.id) 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 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) # print('assigned? ', assigned) # print('assigned loc: ', best_loc) if not assigned: 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) ## 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: 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() 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.is_heal_ready(unit.id) 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)
info = [ num_workers, num_knights, num_rangers, num_mages, num_healers, num_factory, num_rocket ] for unit in gc.my_units(): # resepective unit types execute their own AI if unit.unit_type == bc.UnitType.Worker: worker.timestep(gc, unit, info, karbonite_locations, locs_next_to_terrain, blueprinting_queue, building_assignment, current_worker_roles) elif unit.unit_type == bc.UnitType.Knight: knight.timestep(gc, unit, info, knight_to_cluster, seen_knights_ids, KNIGHT_CLUSTER_MIN) elif unit.unit_type == bc.UnitType.Ranger: ranger.timestep(gc, unit, info, last_turn_battle_locs, next_turn_battle_locs, queued_paths, ranger_roles) elif unit.unit_type == bc.UnitType.Mage: mage.timestep(gc, unit, info, last_turn_battle_locs, next_turn_battle_locs, queued_paths) elif unit.unit_type == bc.UnitType.Healer: healer.timestep(gc, unit, info, last_turn_battle_locs) elif unit.unit_type == bc.UnitType.Factory: factory.timestep(gc, unit, info, building_assignment, mining_rate=3 * len(current_worker_roles["miner"])) elif unit.unit_type == bc.UnitType.Rocket: rocket.timestep(gc, unit, info)
start_time = time.time() worker.timestep(unit) time_workers += (time.time() - start_time) except Exception as e: print('Error:', e) # use this to show where the error was traceback.print_exc() elif unit.unit_type == unit_types["knight"]: #start_time = time.time() knight.timestep(unit) #time_knights+=(time.time()-start_time) elif unit.unit_type == unit_types["ranger"]: try: start_time = time.time() ranger.timestep(unit) time_rangers += (time.time() - start_time) except Exception as e: #print('RANGER ERROR.') if ranger in variables.ranger_roles["go_to_mars"]: variables.ranger_roles["go_to_mars"].remove(ranger) elif ranger in variables.ranger_roles["fighter"]: variables.ranger_roles["fighter"].remove(ranger) elif ranger in variables.ranger_roles["sniper"]: variables.ranger_roles["sniper"].remove(ranger) traceback.print_exc() elif unit.unit_type == unit_types["mage"]: mage.timestep(unit) elif unit.unit_type == unit_types["healer"]: start_time = time.time()
try: worker.timestep(gc, unit, info, karbonite_locations, blueprinting_queue, blueprinting_assignment, building_assignment, current_worker_roles) except Exception as e: print('Error:', e) # use this to show where the error was traceback.print_exc() elif unit.unit_type == bc.UnitType.Knight: knight.timestep(gc, unit, info, knight_to_cluster, seen_knights_ids, KNIGHT_CLUSTER_MIN, constants) elif unit.unit_type == bc.UnitType.Ranger: ranger.timestep(gc, unit, info, last_turn_battle_locs, next_turn_battle_locs, queued_paths, ranger_roles, constants, direction_to_coord, precomputed_bfs) elif unit.unit_type == bc.UnitType.Mage: mage.timestep(gc, unit, info, last_turn_battle_locs, next_turn_battle_locs, queued_paths) elif unit.unit_type == bc.UnitType.Healer: healer.timestep(gc, unit, info, last_turn_battle_locs, constants) elif unit.unit_type == bc.UnitType.Factory: factory.timestep(gc, unit, info, building_assignment, last_turn_battle_locs, constants, mining_rate=3 *