def avail_mil_needing_repair(mil_fleet_ids, split_ships=False, on_mission=False, repair_limit=0.70): """Returns tuple of lists: (ids_needing_repair, ids_not).""" fleet_buckets = [[], []] universe = fo.getUniverse() cutoff = [repair_limit, 0.25][on_mission] for fleet_id in mil_fleet_ids: fleet = universe.getFleet(fleet_id) ship_buckets = [[], []] ships_cur_health = [0, 0] ships_max_health = [0, 0] for ship_id in fleet.shipIDs: this_ship = universe.getShip(ship_id) cur_struc = this_ship.currentMeterValue(fo.meterType.structure) max_struc = this_ship.currentMeterValue(fo.meterType.maxStructure) ship_ok = cur_struc >= cutoff * max_struc ship_buckets[ship_ok].append(ship_id) ships_cur_health[ship_ok] += cur_struc ships_max_health[ship_ok] += max_struc this_sys_id = (fleet.nextSystemID != INVALID_ID and fleet.nextSystemID) or fleet.systemID fleet_ok = (sum(ships_cur_health) >= cutoff * sum(ships_max_health)) local_status = foAI.foAIstate.systemStatus.get(this_sys_id, {}) my_local_rating = combine_ratings( local_status.get('mydefenses', {}).get('overall', 0), local_status.get('myFleetRating', 0)) my_local_rating_vs_planets = local_status.get('myFleetRatingVsPlanets', 0) needed_here = local_status.get( 'totalThreat', 0) > 0 # TODO: assess if remaining other forces are sufficient safely_needed = needed_here and my_local_rating > local_status.get( 'totalThreat', 0) and my_local_rating_vs_planets > local_status.get( 'planetThreat', 0) # TODO: improve both assessment prongs if not fleet_ok: if safely_needed: print "Fleet %d at %s needs repair but deemed safely needed to remain for defense" % ( fleet_id, ppstring(PlanetUtilsAI.sys_name_ids([fleet.systemID]))) else: if needed_here: print "Fleet %d at %s needed present for combat, but is damaged and deemed unsafe to remain." % ( fleet_id, ppstring(PlanetUtilsAI.sys_name_ids([fleet.systemID]))) print "\t my_local_rating: %.1f ; threat: %.1f" % ( my_local_rating, local_status.get('totalThreat', 0)) print "Selecting fleet %d at %s for repair" % ( fleet_id, ppstring(PlanetUtilsAI.sys_name_ids([fleet.systemID]))) fleet_buckets[fleet_ok or safely_needed].append(fleet_id) return fleet_buckets
def avail_mil_needing_repair(mil_fleet_ids, split_ships=False, on_mission=False): """returns tuple of lists-- ( ids_needing_repair, ids_not )""" fleet_buckets = [[], []] universe = fo.getUniverse() cutoff = [0.70, 0.25][on_mission] for fleet_id in mil_fleet_ids: fleet = universe.getFleet(fleet_id) ship_buckets = [[], []] ships_cur_health = [0, 0] ships_max_health = [0, 0] safely_needed = False for ship_id in fleet.shipIDs: this_ship = universe.getShip(ship_id) cur_struc = this_ship.currentMeterValue(fo.meterType.structure) max_struc = this_ship.currentMeterValue(fo.meterType.maxStructure) ship_ok = cur_struc >= cutoff * max_struc ship_buckets[ship_ok].append(ship_id) ships_cur_health[ship_ok] += cur_struc ships_max_health[ship_ok] += max_struc thisSysID = (fleet.nextSystemID != -1 and fleet.nextSystemID) or fleet.systemID fleet_ok = sum(ships_cur_health) >= cutoff * sum(ships_max_health) local_status = foAI.foAIstate.systemStatus.get(thisSysID, {}) my_local_rating = local_status.get("mydefenses", {}).get("overall", 0) + local_status.get("myFleetRating", 0) needed_here = local_status.get("totalThreat", 0) > 0 # TODO: assess if remaining other forces are sufficient safely_needed = needed_here and my_local_rating > local_status.get( "totalThreat", 0 ) # TODO: improve both assessment prongs if not fleet_ok: if safely_needed: print "Fleet %d at %s needs repair but deemed safely needed to remain for defense" % ( fleet_id, ppstring(PlanetUtilsAI.sys_name_ids([fleet.systemID])), ) else: if needed_here: print "Fleet %d at %s needed present for combat, but is damaged and deemed unsafe to remain." % ( fleet_id, ppstring(PlanetUtilsAI.sys_name_ids([fleet.systemID])), ) print "\t my_local_rating: %.1f ; threat: %.1f" % ( my_local_rating, local_status.get("totalThreat", 0), ) print "Selecting fleet %d at %s for repair" % ( fleet_id, ppstring(PlanetUtilsAI.sys_name_ids([fleet.systemID])), ) fleet_buckets[fleet_ok or safely_needed].append(fleet_id) return fleet_buckets
def get_safe_path_leg_to_dest(fleet_id, start_id, dest_id): start_targ = universe_object.System(start_id) dest_targ = universe_object.System(dest_id) #TODO actually get a safe path this_path = can_travel_to_system(fleet_id, start_targ, dest_targ, ensure_return=False) path_ids = [targ.id for targ in this_path if targ.id != start_id] + [start_id] start_info = PlanetUtilsAI.sys_name_ids([start_id]) dest_info = PlanetUtilsAI.sys_name_ids([dest_id]) path_info = [PlanetUtilsAI.sys_name_ids([sys_id]) for sys_id in path_ids] print "Fleet %d requested safe path leg from %s to %s, found path %s" % ( fleet_id, ppstring(start_info), ppstring(dest_info), ppstring(path_info)) return path_ids[0]
def get_repair_fleet_order(fleet_target, current_sys_id): """ Returns repair AIFleetOrder to [nearest safe] drydock.""" # find nearest supplied system drydock_sys_id = get_nearest_drydock_system_id(current_sys_id) drydock_system_target = AITarget.AITarget(TargetType.TARGET_SYSTEM, drydock_sys_id) print "ordering fleet %d to %s for repair" % (fleet_target.target_id, ppstring(PlanetUtilsAI.sys_name_ids([drydock_sys_id]))) # create resupply AIFleetOrder return AIFleetOrder.AIFleetOrder(AIFleetOrderType.ORDER_REPAIR, fleet_target, drydock_system_target)
def sys_name_ids(sys_ids: Iterable[int]) -> str: """ Get a string representation of a list with system_ids. The returned string is of the form "[S_id<name>, ...]" :return: string representation of the systems in the list """ universe = fo.getUniverse() return ppstring([str(universe.getSystem(sys_id)) for sys_id in sys_ids])
def planet_string(planet_ids: Union[PlanetId, List[PlanetId]]) -> str: """ Get a string representation of the passed planets. """ def _safe_planet_name(planet_id): planet = fo.getUniverse().getPlanet(planet_id) return fo.to_str("P", planet_id, (planet and planet.name) or "?") if isinstance(planet_ids, int): return _safe_planet_name(planet_ids) return ppstring([_safe_planet_name(pid) for pid in planet_ids])
def sys_name_ids(sys_ids): """ Get a string representation of a list with system_ids. The returned string is of the form "[S_id<name>, ...]" :param sys_ids: list of system ids :rtype: string :return: string representation of the systems in the list """ universe = fo.getUniverse() return ppstring([str(universe.getSystem(sys_id)) for sys_id in sys_ids])
def planet_string(planet_ids): """ Get a string representation of the passed planets :param planet_ids: list of planet ids or single id :rtype: str """ def _safe_planet_name(planet_id): planet = fo.getUniverse().getPlanet(planet_id) return fo.to_str('P', planet_id, (planet and planet.name) or "?") if isinstance(planet_ids, int): return _safe_planet_name(planet_ids) return ppstring([_safe_planet_name(pid) for pid in planet_ids])
def get_repair_fleet_order(fleet_target, current_sys_id): """ Return fleet_orders.OrderRepair for fleet to proceed system with drydock. :param fleet_target: fleet that need to be repaired :type fleet_target: universe_object.Fleet # TODO check if we can remove this id, because fleet already have it. :param current_sys_id: current system id :type current_sys_id: int :return: order to repair :rtype fleet_orders.OrderRepair """ # find nearest supplied system drydock_sys_id = get_nearest_drydock_system_id(current_sys_id) print "ordering fleet %d to %s for repair" % (fleet_target.id, ppstring(PlanetUtilsAI.sys_name_ids([drydock_sys_id]))) # create resupply AIFleetOrder return fleet_orders.OrderRepair(fleet_target, universe_object.System(drydock_sys_id))
def get_repair_fleet_order(fleet, current_system_id): """Return fleet_orders.OrderRepair for fleet to proceed to system with drydock. :param fleet: fleet that need to be repaired :type fleet: universe_object.Fleet # TODO check if we can remove this id, because fleet already have it. :param current_system_id: current location of the fleet, next system if currently on starlane. :type current_system_id: int :return: order to repair :rtype fleet_orders.OrderRepair """ # TODO Cover new mechanics where happiness increases repair rate - don't always use nearest system! # find nearest drydock system drydock_sys_id = get_best_drydock_system_id(current_system_id, fleet.id) if drydock_sys_id is None: return None print "Ordering fleet %s to %s for repair" % (fleet, ppstring(PlanetUtilsAI.sys_name_ids([drydock_sys_id]))) return fleet_orders.OrderRepair(fleet, universe_object.System(drydock_sys_id))
def get_repair_fleet_order(fleet_target, current_sys_id): """ Return fleet_orders.OrderRepair for fleet to proceed system with drydock. :param fleet_target: fleet that need to be repaired :type fleet_target: universe_object.Fleet # TODO check if we can remove this id, because fleet already have it. :param current_sys_id: current system id :type current_sys_id: int :return: order to repair :rtype fleet_orders.OrderRepair """ # TODO Cover new mechanics where happiness increases repair rate - don't always use nearest system! # find nearest supplied system drydock_sys_id = get_best_drydock_system_id(current_sys_id, fleet_target.id) if drydock_sys_id is None: return None print "ordering fleet %d to %s for repair" % (fleet_target.id, ppstring(PlanetUtilsAI.sys_name_ids([drydock_sys_id]))) # create resupply AIFleetOrder return fleet_orders.OrderRepair(fleet_target, universe_object.System(drydock_sys_id))
def get_safe_path_leg_to_dest(fleet_id, start_id, dest_id): start_targ = universe_object.System(start_id) dest_targ = universe_object.System(dest_id) # TODO actually get a safe path this_path = can_travel_to_system(fleet_id, start_targ, dest_targ, ensure_return=False) path_ids = [targ.id for targ in this_path if targ.id != start_id] + [start_id] start_info = PlanetUtilsAI.sys_name_ids([start_id]) dest_info = PlanetUtilsAI.sys_name_ids([dest_id]) path_info = [PlanetUtilsAI.sys_name_ids([sys_id]) for sys_id in path_ids] print "Fleet %d requested safe path leg from %s to %s, found path %s" % (fleet_id, ppstring(start_info), ppstring(dest_info), ppstring(path_info)) return path_ids[0]