def send_invasion_fleets(fleet_ids, evaluated_planets, mission_type): """sends a list of invasion fleets to a list of planet_value_pairs""" universe = fo.getUniverse() invasion_fleet_pool = set(fleet_ids) if not invasion_fleet_pool: return for planet_id, pscore, ptroops in evaluated_planets: planet = universe.getPlanet(planet_id) if not planet: continue sys_id = planet.systemID found_fleets = [] found_stats = {} min_stats = {'rating': 0, 'troopCapacity': ptroops} target_stats = {'rating': 10, 'troopCapacity': ptroops+1} these_fleets = FleetUtilsAI.get_fleets_for_mission(1, target_stats, min_stats, found_stats, "", systems_to_check=[sys_id], systems_checked=[], fleet_pool_set=invasion_fleet_pool, fleet_list=found_fleets, verbose=False) if not these_fleets: if not FleetUtilsAI.stats_meet_reqs(found_stats, min_stats): print "Insufficient invasion troop allocation for system %d ( %s ) -- requested %s , found %s" % ( sys_id, universe.getSystem(sys_id).name, min_stats, found_stats) invasion_fleet_pool.update(found_fleets) continue else: these_fleets = found_fleets target = AITarget.AITarget(EnumsAI.TargetType.TARGET_PLANET, planet_id) print "assigning invasion fleets %s to target %s" % (these_fleets, target) for fleetID in these_fleets: fleet_mission = foAI.foAIstate.get_fleet_mission(fleetID) fleet_mission.clear_fleet_orders() fleet_mission.clear_targets((fleet_mission.get_mission_types() + [-1])[0]) fleet_mission.add_target(mission_type, target)
def send_invasion_fleets(fleet_ids, evaluated_planets, mission_type): """sends a list of invasion fleets to a list of planet_value_pairs""" if not fleet_ids: return universe = fo.getUniverse() invasion_fleet_pool = set(fleet_ids) for planet_id, pscore, ptroops in evaluated_planets: planet = universe.getPlanet(planet_id) if not planet: continue sys_id = planet.systemID found_fleets = [] found_stats = {} min_stats = {'rating': 0, 'troopCapacity': ptroops} target_stats = {'rating': 10, 'troopCapacity': ptroops + _TROOPS_SAFETY_MARGIN} these_fleets = FleetUtilsAI.get_fleets_for_mission(target_stats, min_stats, found_stats, starting_system=sys_id, fleet_pool_set=invasion_fleet_pool, fleet_list=found_fleets) if not these_fleets: if not FleetUtilsAI.stats_meet_reqs(found_stats, min_stats): print "Insufficient invasion troop allocation for system %d ( %s ) -- requested %s , found %s" % ( sys_id, universe.getSystem(sys_id).name, min_stats, found_stats) invasion_fleet_pool.update(found_fleets) continue else: these_fleets = found_fleets target = universe_object.Planet(planet_id) print "assigning invasion fleets %s to target %s" % (these_fleets, target) for fleetID in these_fleets: fleet_mission = foAI.foAIstate.get_fleet_mission(fleetID) fleet_mission.clear_fleet_orders() fleet_mission.clear_target() fleet_mission.set_target(mission_type, target)
def send_invasion_fleets(fleet_ids, evaluated_planets, mission_type): """sends a list of invasion fleets to a list of planet_value_pairs""" universe = fo.getUniverse() invasion_fleet_pool = set(fleet_ids) if not invasion_fleet_pool: return for planet_id, pscore, ptroops in evaluated_planets: planet = universe.getPlanet(planet_id) if not planet: continue sys_id = planet.systemID found_fleets = [] found_stats = {} min_stats = {'rating': 0, 'troopCapacity': ptroops} target_stats = {'rating': 10, 'troopCapacity': ptroops+1} these_fleets = FleetUtilsAI.get_fleets_for_mission(target_stats, min_stats, found_stats, starting_system=sys_id, fleet_pool_set=invasion_fleet_pool, fleet_list=found_fleets) if not these_fleets: if not FleetUtilsAI.stats_meet_reqs(found_stats, min_stats): print "Insufficient invasion troop allocation for system %d ( %s ) -- requested %s , found %s" % ( sys_id, universe.getSystem(sys_id).name, min_stats, found_stats) invasion_fleet_pool.update(found_fleets) continue else: these_fleets = found_fleets target = universe_object.Planet(planet_id) print "assigning invasion fleets %s to target %s" % (these_fleets, target) for fleetID in these_fleets: fleet_mission = foAI.foAIstate.get_fleet_mission(fleetID) fleet_mission.clear_fleet_orders() fleet_mission.clear_target() fleet_mission.set_target(mission_type, target)
def send_invasion_fleets(fleet_ids, evaluated_planets, mission_type): """sends a list of invasion fleets to a list of planet_value_pairs""" if not fleet_ids: return universe = fo.getUniverse() invasion_fleet_pool = set(fleet_ids) for planet_id, pscore, ptroops in evaluated_planets: if pscore < MIN_INVASION_SCORE: continue planet = universe.getPlanet(planet_id) if not planet: continue sys_id = planet.systemID found_fleets = [] found_stats = {} min_stats = {"troopCapacity": ptroops} target_stats = { "troopCapacity": ptroops + _TROOPS_SAFETY_MARGIN, "target_system": TargetSystem(sys_id), } these_fleets = FleetUtilsAI.get_fleets_for_mission( target_stats, min_stats, found_stats, starting_system=sys_id, fleet_pool_set=invasion_fleet_pool, fleet_list=found_fleets, ) if not these_fleets: if not FleetUtilsAI.stats_meet_reqs(found_stats, min_stats): debug( "Insufficient invasion troop allocation for system %d ( %s ) -- requested %s , found %s" % (sys_id, universe.getSystem(sys_id).name, min_stats, found_stats)) invasion_fleet_pool.update(found_fleets) continue else: these_fleets = found_fleets target = TargetPlanet(planet_id) debug("assigning invasion fleets %s to target %s" % (these_fleets, target)) if target.get_object().currentMeterValue( fo.meterType.maxShield) > 0.0 and not secure_system( sys_id, True): continue aistate = get_aistate() for fleetID in these_fleets: fleet_mission = aistate.get_fleet_mission(fleetID) fleet_mission.clear_fleet_orders() fleet_mission.clear_target() fleet_mission.set_target(mission_type, target) # cannot wait for next turn, else the secure mission may be abandoned if these_fleets and sys_id not in AIstate.invasionTargetedSystemIDs: AIstate.invasionTargetedSystemIDs.append(sys_id)
def send_invasion_fleets(fleet_ids, evaluated_planets, mission_type): """sends a list of invasion fleets to a list of planet_value_pairs""" if not fleet_ids: return universe = fo.getUniverse() invasion_fleet_pool = set(fleet_ids) for planet_id, pscore, ptroops in evaluated_planets: if pscore < MIN_INVASION_SCORE: continue planet = universe.getPlanet(planet_id) if not planet: continue sys_id = planet.systemID found_fleets = [] found_stats = {} min_stats = {"rating": 0, "troopCapacity": ptroops} target_stats = { "rating": 10, "troopCapacity": ptroops + _TROOPS_SAFETY_MARGIN, "target_system": TargetSystem(sys_id), } these_fleets = FleetUtilsAI.get_fleets_for_mission( target_stats, min_stats, found_stats, starting_system=sys_id, fleet_pool_set=invasion_fleet_pool, fleet_list=found_fleets, ) if not these_fleets: if not FleetUtilsAI.stats_meet_reqs(found_stats, min_stats): debug( "Insufficient invasion troop allocation for system %d ( %s ) -- requested %s , found %s" % (sys_id, universe.getSystem(sys_id).name, min_stats, found_stats)) invasion_fleet_pool.update(found_fleets) continue else: these_fleets = found_fleets target = TargetPlanet(planet_id) debug("assigning invasion fleets %s to target %s" % (these_fleets, target)) aistate = get_aistate() for fleetID in these_fleets: fleet_mission = aistate.get_fleet_mission(fleetID) fleet_mission.clear_fleet_orders() fleet_mission.clear_target() fleet_mission.set_target(mission_type, target)
def send_invasion_fleets(fleet_ids, evaluated_planets, mission_type): """sends a list of invasion fleets to a list of planet_value_pairs""" if not fleet_ids: return universe = fo.getUniverse() invasion_fleet_pool = set(fleet_ids) for planet_id, pscore, ptroops in evaluated_planets: if pscore < MIN_INVASION_SCORE: continue planet = universe.getPlanet(planet_id) if not planet: continue sys_id = planet.systemID found_fleets = [] found_stats = {} min_stats = {'rating': 0, 'troopCapacity': ptroops} target_stats = {'rating': 10, 'troopCapacity': ptroops + _TROOPS_SAFETY_MARGIN, 'target_system': TargetSystem(sys_id)} these_fleets = FleetUtilsAI.get_fleets_for_mission(target_stats, min_stats, found_stats, starting_system=sys_id, fleet_pool_set=invasion_fleet_pool, fleet_list=found_fleets) if not these_fleets: if not FleetUtilsAI.stats_meet_reqs(found_stats, min_stats): debug("Insufficient invasion troop allocation for system %d ( %s ) -- requested %s , found %s" % ( sys_id, universe.getSystem(sys_id).name, min_stats, found_stats)) invasion_fleet_pool.update(found_fleets) continue else: these_fleets = found_fleets target = TargetPlanet(planet_id) debug("assigning invasion fleets %s to target %s" % (these_fleets, target)) aistate = get_aistate() for fleetID in these_fleets: fleet_mission = aistate.get_fleet_mission(fleetID) fleet_mission.clear_fleet_orders() fleet_mission.clear_target() fleet_mission.set_target(mission_type, target)
def send_invasion_fleets(invasionFleetIDs, evaluatedPlanets, missionType): """sends a list of invasion fleets to a list of planet_value_pairs""" universe=fo.getUniverse() invasionPool = invasionFleetIDs[:] #need to make a copy bestShip, bestDesign, buildChoices = ProductionAI.getBestShipInfo( EnumsAI.AIPriorityType.PRIORITY_PRODUCTION_INVASION) if bestDesign: troopsPerBestShip = 2*( list(bestDesign.parts).count("GT_TROOP_POD") ) else: troopsPerBestShip=5 #may actually not have any troopers available, but this num will do for now #sortedTargets=sorted( [ ( pscore-ptroops/2 , pID, pscore, ptroops) for pID, pscore, ptroops in evaluatedPlanets ] , reverse=True) invasionPool=set(invasionPool) for pID, pscore, ptroops in evaluatedPlanets: # if not invasionPool: return planet=universe.getPlanet(pID) if not planet: continue sysID = planet.systemID foundFleets = [] podsNeeded= math.ceil( (ptroops+0.05)/2.0) foundStats={} minStats= {'rating':0, 'troopPods':podsNeeded} targetStats={'rating':10,'troopPods':podsNeeded+1} theseFleets = FleetUtilsAI.get_fleets_for_mission(1, targetStats , minStats, foundStats, "", systems_to_check=[sysID], systems_checked=[], fleet_pool_set=invasionPool, fleet_list=foundFleets, verbose=False) if not theseFleets: if not FleetUtilsAI.stats_meet_reqs(foundStats, minStats): print "Insufficient invasion troop allocation for system %d ( %s ) -- requested %s , found %s"%(sysID, universe.getSystem(sysID).name, minStats, foundStats) invasionPool.update( foundFleets ) continue else: theseFleets = foundFleets aiTarget = AITarget.AITarget(EnumsAI.AITargetType.TARGET_PLANET, pID) print "assigning invasion fleets %s to target %s"%(theseFleets, aiTarget) for fleetID in theseFleets: fleet=universe.getFleet(fleetID) aiFleetMission = foAI.foAIstate.get_fleet_mission(fleetID) aiFleetMission.clear_fleet_orders() aiFleetMission.clear_targets( (aiFleetMission.get_mission_types() + [-1])[0] ) aiFleetMission.add_target(missionType, aiTarget)
def send_invasion_fleets(invasionFleetIDs, evaluatedPlanets, missionType): """sends a list of invasion fleets to a list of planet_value_pairs""" universe=fo.getUniverse() invasionPool = invasionFleetIDs[:] #need to make a copy bestShip, bestDesign, buildChoices = ProductionAI.getBestShipInfo( EnumsAI.AIPriorityType.PRIORITY_PRODUCTION_INVASION) if bestDesign: troopsPerBestShip = 2*( list(bestDesign.parts).count("GT_TROOP_POD") ) else: troopsPerBestShip=5 #may actually not have any troopers available, but this num will do for now #sortedTargets=sorted( [ ( pscore-ptroops/2 , pID, pscore, ptroops) for pID, pscore, ptroops in evaluatedPlanets ] , reverse=True) invasionPool=set(invasionPool) for pID, pscore, ptroops in evaluatedPlanets: # if not invasionPool: return planet=universe.getPlanet(pID) if not planet: continue sysID = planet.systemID foundFleets = [] podsNeeded= math.ceil( (ptroops+0.05)/2.0) foundStats={} minStats= {'rating':0, 'troopPods':podsNeeded} targetStats={'rating':10,'troopPods':podsNeeded+1} theseFleets = FleetUtilsAI.get_fleets_for_mission(1, targetStats , minStats, foundStats, "", systems_to_check=[sysID], systems_checked=[], fleet_pool_set=invasionPool, fleet_list=foundFleets, verbose=False) if not theseFleets: if not FleetUtilsAI.stats_meet_reqs(foundStats, minStats): print "Insufficient invasion troop allocation for system %d ( %s ) -- requested %s , found %s"%(sysID, universe.getSystem(sysID).name, minStats, foundStats) invasionPool.update( foundFleets ) continue else: theseFleets = foundFleets aiTarget = AITarget.AITarget(EnumsAI.TargetType.TARGET_PLANET, pID) print "assigning invasion fleets %s to target %s"%(theseFleets, aiTarget) for fleetID in theseFleets: fleet=universe.getFleet(fleetID) aiFleetMission = foAI.foAIstate.get_fleet_mission(fleetID) aiFleetMission.clear_fleet_orders() aiFleetMission.clear_targets( (aiFleetMission.get_mission_types() + [-1])[0] ) aiFleetMission.add_target(missionType, aiTarget)
def assign_military_fleets_to_systems(use_fleet_id_list=None, allocations=None, round=1): # assign military fleets to military theater systems global _military_allocations universe = fo.getUniverse() if allocations is None: allocations = [] doing_main = (use_fleet_id_list is None) if doing_main: foAI.foAIstate.misc['ReassignedFleetMissions'] = [] base_defense_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(MissionType.ORBITAL_DEFENSE) unassigned_base_defense_ids = FleetUtilsAI.extract_fleet_ids_without_mission_types(base_defense_ids) for fleet_id in unassigned_base_defense_ids: fleet = universe.getFleet(fleet_id) if not fleet: continue sys_id = fleet.systemID target = universe_object.System(sys_id) fleet_mission = foAI.foAIstate.get_fleet_mission(fleet_id) fleet_mission.clear_fleet_orders() fleet_mission.clear_target() mission_type = MissionType.ORBITAL_DEFENSE fleet_mission.set_target(mission_type, target) all_military_fleet_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(MissionType.MILITARY) if not all_military_fleet_ids: _military_allocations = [] return avail_mil_fleet_ids = list(FleetUtilsAI.extract_fleet_ids_without_mission_types(all_military_fleet_ids)) mil_needing_repair_ids, avail_mil_fleet_ids = avail_mil_needing_repair(avail_mil_fleet_ids) these_allocations = _military_allocations print "==================================================" print "Assigning military fleets" print "---------------------------------" else: avail_mil_fleet_ids = list(use_fleet_id_list) mil_needing_repair_ids, avail_mil_fleet_ids = avail_mil_needing_repair(avail_mil_fleet_ids) these_allocations = allocations # send_for_repair(mil_needing_repair_ids) #currently, let get taken care of by AIFleetMission.generate_fleet_orders() # get systems to defend avail_mil_fleet_ids = set(avail_mil_fleet_ids) for sys_id, alloc, minalloc, rvp, takeAny in these_allocations: if not doing_main and not avail_mil_fleet_ids: break found_fleets = [] found_stats = {} these_fleets = FleetUtilsAI.get_fleets_for_mission({'rating': alloc, 'ratingVsPlanets': rvp}, {'rating': minalloc, 'ratingVsPlanets': rvp}, found_stats, starting_system=sys_id, fleet_pool_set=avail_mil_fleet_ids, fleet_list=found_fleets) if not these_fleets: if not found_fleets or not (FleetUtilsAI.stats_meet_reqs(found_stats, {'rating': minalloc}) or takeAny): if doing_main: if _verbose_mil_reporting: print "NO available/suitable military allocation for system %d ( %s ) -- requested allocation %8d, found available rating %8d in fleets %s" % (sys_id, universe.getSystem(sys_id).name, minalloc, found_stats.get('rating', 0), found_fleets) avail_mil_fleet_ids.update(found_fleets) continue else: these_fleets = found_fleets elif doing_main and _verbose_mil_reporting: print "FULL+ military allocation for system %d ( %s ) -- requested allocation %8d, got %8d with fleets %s" % (sys_id, universe.getSystem(sys_id).name, alloc, found_stats.get('rating', 0), these_fleets) target = universe_object.System(sys_id) for fleet_id in these_fleets: fo.issueAggressionOrder(fleet_id, True) fleet_mission = foAI.foAIstate.get_fleet_mission(fleet_id) fleet_mission.clear_fleet_orders() fleet_mission.clear_target() if sys_id in set(AIstate.colonyTargetedSystemIDs + AIstate.outpostTargetedSystemIDs + AIstate.invasionTargetedSystemIDs): mission_type = MissionType.SECURE else: mission_type = MissionType.MILITARY fleet_mission.set_target(mission_type, target) fleet_mission.generate_fleet_orders() if not doing_main: foAI.foAIstate.misc.setdefault('ReassignedFleetMissions', []).append(fleet_mission) if doing_main: print "---------------------------------" last_round = 3 last_round_name = "LastRound" if round <= last_round: # check if any fleets remain unassigned all_military_fleet_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(MissionType.MILITARY) avail_mil_fleet_ids = list(FleetUtilsAI.extract_fleet_ids_without_mission_types(all_military_fleet_ids)) allocations = [] round += 1 thisround = "Extras Remaining Round %d" % round if round < last_round else last_round_name if avail_mil_fleet_ids: print "Still have available military fleets: %s" % avail_mil_fleet_ids allocations = get_military_fleets(mil_fleets_ids=avail_mil_fleet_ids, try_reset=False, thisround=thisround) if allocations: assign_military_fleets_to_systems(use_fleet_id_list=avail_mil_fleet_ids, allocations=allocations, round=round)
def assign_military_fleets_to_systems(use_fleet_id_list=None, allocations=None, round=1): # assign military fleets to military theater systems global military_allocations universe = fo.getUniverse() if allocations is None: allocations = [] doing_main = (use_fleet_id_list is None) if doing_main: foAI.foAIstate.misc['ReassignedFleetMissions'] = [] base_defense_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(MissionType.ORBITAL_DEFENSE) unassigned_base_defense_ids = FleetUtilsAI.extract_fleet_ids_without_mission_types(base_defense_ids) for fleet_id in unassigned_base_defense_ids: fleet = universe.getFleet(fleet_id) if not fleet: continue sys_id = fleet.systemID target = universe_object.System(sys_id) fleet_mission = foAI.foAIstate.get_fleet_mission(fleet_id) fleet_mission.clear_fleet_orders() fleet_mission.clear_target() mission_type = MissionType.ORBITAL_DEFENSE fleet_mission.add_target(mission_type, target) all_military_fleet_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(MissionType.MILITARY) if not all_military_fleet_ids: military_allocations = [] return avail_mil_fleet_ids = list(FleetUtilsAI.extract_fleet_ids_without_mission_types(all_military_fleet_ids)) mil_needing_repair_ids, avail_mil_fleet_ids = avail_mil_needing_repair(avail_mil_fleet_ids) these_allocations = military_allocations print "==================================================" print "Assigning military fleets" print "---------------------------------" else: avail_mil_fleet_ids = list(use_fleet_id_list) mil_needing_repair_ids, avail_mil_fleet_ids = avail_mil_needing_repair(avail_mil_fleet_ids) these_allocations = allocations # send_for_repair(mil_needing_repair_ids) #currently, let get taken care of by AIFleetMission.generate_fleet_orders() # get systems to defend avail_mil_fleet_ids = set(avail_mil_fleet_ids) for sys_id, alloc, minalloc, takeAny in these_allocations: if not doing_main and not avail_mil_fleet_ids: break found_fleets = [] found_stats = {} try: these_fleets = FleetUtilsAI.get_fleets_for_mission(1, {'rating': alloc}, {'rating': minalloc}, found_stats, "", systems_to_check=[sys_id], systems_checked=[], fleet_pool_set=avail_mil_fleet_ids, fleet_list=found_fleets, verbose=False) except: continue if not these_fleets: if not found_fleets or not (FleetUtilsAI.stats_meet_reqs(found_stats, {'rating': minalloc}) or takeAny): if doing_main: if verbose_mil_reporting: print "NO available/suitable military allocation for system %d ( %s ) -- requested allocation %8d, found available rating %8d in fleets %s" % (sys_id, universe.getSystem(sys_id).name, minalloc, found_stats.get('rating', 0), found_fleets) avail_mil_fleet_ids.update(found_fleets) continue else: these_fleets = found_fleets # rating = sum( map(lambda x: foAI.foAIstate.rate_fleet(x), foundFleets ) ) ratings = map(foAI.foAIstate.get_rating, found_fleets) rating = sum([fr.get('attack', 0) for fr in ratings]) * sum([fr.get('health', 0) for fr in ratings]) if doing_main and verbose_mil_reporting: if rating < minMilAllocations.get(sys_id, 0): print "PARTIAL military allocation for system %d ( %s ) -- requested allocation %8d -- got %8d with fleets %s" % (sys_id, universe.getSystem(sys_id).name, minalloc, rating, these_fleets) else: print "FULL MIN military allocation for system %d ( %s ) -- requested allocation %8d -- got %8d with fleets %s" % (sys_id, universe.getSystem(sys_id).name, minMilAllocations.get(sys_id, 0), rating, these_fleets) elif doing_main and verbose_mil_reporting: print "FULL+ military allocation for system %d ( %s ) -- requested allocation %8d, got %8d with fleets %s" % (sys_id, universe.getSystem(sys_id).name, alloc, found_stats.get('rating', 0), these_fleets) target = universe_object.System(sys_id) for fleet_id in these_fleets: fo.issueAggressionOrder(fleet_id, True) fleet_mission = foAI.foAIstate.get_fleet_mission(fleet_id) fleet_mission.clear_fleet_orders() fleet_mission.clear_target() if sys_id in list(set(AIstate.colonyTargetedSystemIDs + AIstate.outpostTargetedSystemIDs + AIstate.invasionTargetedSystemIDs + AIstate.blockadeTargetedSystemIDs)): mission_type = MissionType.SECURE else: mission_type = MissionType.MILITARY fleet_mission.add_target(mission_type, target) fleet_mission.generate_fleet_orders() if not doing_main: foAI.foAIstate.misc.setdefault('ReassignedFleetMissions', []).append(fleet_mission) if doing_main: print "---------------------------------" last_round = 3 last_round_name = "LastRound" if round <= last_round: # check if any fleets remain unassigned all_military_fleet_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(MissionType.MILITARY) avail_mil_fleet_ids = list(FleetUtilsAI.extract_fleet_ids_without_mission_types(all_military_fleet_ids)) allocations = [] round += 1 thisround = "Extras Remaining Round %d" % round if round < last_round else last_round_name if avail_mil_fleet_ids: print "Still have available military fleets: %s" % avail_mil_fleet_ids allocations = get_military_fleets(mil_fleets_ids=avail_mil_fleet_ids, try_reset=False, thisround=thisround) if allocations: assign_military_fleets_to_systems(use_fleet_id_list=avail_mil_fleet_ids, allocations=allocations, round=round)
def assign_military_fleets_to_systems(useFleetIDList=None, allocations=None): # assign military fleets to military theater systems global MilitaryAllocations universe = fo.getUniverse() if allocations is None: allocations = [] doingMain = (useFleetIDList is None) if doingMain: baseDefenseIDs = FleetUtilsAI.get_empire_fleet_ids_by_role(AIFleetMissionType.FLEET_MISSION_ORBITAL_DEFENSE) unassignedBaseDefenseIDs = FleetUtilsAI.extract_fleet_ids_without_mission_types(baseDefenseIDs) for fleetID in unassignedBaseDefenseIDs: fleet = universe.getFleet(fleetID) if not fleet: continue sysID = fleet.systemID aiTarget = AITarget.AITarget(AITargetType.TARGET_SYSTEM, sysID) aiFleetMission = foAI.foAIstate.get_fleet_mission(fleetID) aiFleetMission.clear_fleet_orders() aiFleetMission.clear_targets( (aiFleetMission.get_mission_types() + [-1])[0] ) missionType = AIFleetMissionType.FLEET_MISSION_ORBITAL_DEFENSE aiFleetMission.add_target( missionType , aiTarget) allMilitaryFleetIDs = FleetUtilsAI.get_empire_fleet_ids_by_role(AIFleetMissionType.FLEET_MISSION_MILITARY) AIstate.militaryFleetIDs = allMilitaryFleetIDs if not allMilitaryFleetIDs: MilitaryAllocations = [] return availMilFleetIDs = list( FleetUtilsAI.extract_fleet_ids_without_mission_types(allMilitaryFleetIDs) ) mil_needing_repair_ids, availMilFleetIDs = avail_mil_needing_repair(availMilFleetIDs) availMilRating = sum( map(lambda x: foAI.foAIstate.get_rating(x).get('overall', 0), availMilFleetIDs ) ) under_repair_mil_rating = sum( map(lambda x: foAI.foAIstate.get_rating(x).get('overall', 0), mil_needing_repair_ids ) ) theseAllocations = MilitaryAllocations print "==================================================" print "assigning military fleets" print "---------------------------------" else: availMilFleetIDs = list( useFleetIDList ) mil_needing_repair_ids, availMilFleetIDs = avail_mil_needing_repair(availMilFleetIDs) availMilRating = sum( map(lambda x: foAI.foAIstate.get_rating(x).get('overall', 0), availMilFleetIDs ) ) theseAllocations = allocations remainingMilRating = availMilRating #send_for_repair(mil_needing_repair_ids) #currently, let get taken care of by AIFleetMission.generate_fleet_orders() # get systems to defend availMilFleetIDs = set(availMilFleetIDs) for sysID, alloc, minalloc, takeAny in theseAllocations: if not doingMain and len(availMilFleetIDs)==0: break foundFleets = [] foundStats={} try: theseFleets = FleetUtilsAI.get_fleets_for_mission(1, {'rating':alloc}, {'rating':minalloc}, foundStats, "", systems_to_check=[sysID], systems_checked=[], fleet_pool_set=availMilFleetIDs, fleet_list=foundFleets, verbose=False) except: continue if not theseFleets: if foundFleets==[] or not ( FleetUtilsAI.stats_meet_reqs( foundStats, {'rating':minalloc}) or takeAny): if doingMain: if verboseMilReporting: print "NO available/suitable military allocation for system %d ( %s ) -- requested allocation %8d, found available rating %8d in fleets %s"%(sysID, universe.getSystem(sysID).name, minalloc, foundStats.get('rating', 0), foundFleets) availMilFleetIDs.update(foundFleets) continue else: theseFleets = foundFleets #rating = sum( map(lambda x: foAI.foAIstate.rate_fleet(x), foundFleets ) ) ratings = map(foAI.foAIstate.get_rating, foundFleets ) rating = sum([fr.get('attack', 0) for fr in ratings]) * sum([fr.get('health', 0) for fr in ratings]) if doingMain and verboseMilReporting: if rating < minMilAllocations.get(sysID, 0): print "PARTIAL military allocation for system %d ( %s ) -- requested allocation %8d -- got %8d with fleets %s"%(sysID, universe.getSystem(sysID).name, minalloc, rating, theseFleets) else: print "FULL MIN military allocation for system %d ( %s ) -- requested allocation %8d -- got %8d with fleets %s "%(sysID, universe.getSystem(sysID).name, minMilAllocations.get(sysID, 0) , rating, theseFleets) elif doingMain and verboseMilReporting: print "FULL+ military allocation for system %d ( %s ) -- requested allocation %8d, got %8d with fleets %s"%(sysID, universe.getSystem(sysID).name, alloc, foundStats.get('rating', 0), theseFleets) aiTarget = AITarget.AITarget(AITargetType.TARGET_SYSTEM, sysID) for fleetID in theseFleets: fleet=universe.getFleet(fleetID) fo.issueAggressionOrder(fleetID, True) aiFleetMission = foAI.foAIstate.get_fleet_mission(fleetID) aiFleetMission.clear_fleet_orders() aiFleetMission.clear_targets( (aiFleetMission.get_mission_types() + [-1])[0] ) if sysID in list(set(AIstate.colonyTargetedSystemIDs + AIstate.outpostTargetedSystemIDs + AIstate.invasionTargetedSystemIDs + AIstate.blockadeTargetedSystemIDs)): missionType = AIFleetMissionType.FLEET_MISSION_SECURE else: missionType = AIFleetMissionType.FLEET_MISSION_MILITARY aiFleetMission.add_target( missionType , aiTarget) aiFleetMission.generate_fleet_orders() if not doingMain: foAI.foAIstate.misc.setdefault('ReassignedFleetMissions', []).append(aiFleetMission) if doingMain: print "---------------------------------"
def assign_military_fleets_to_systems(use_fleet_id_list=None, allocations=None, round=1): # assign military fleets to military theater systems global _military_allocations universe = fo.getUniverse() if allocations is None: allocations = [] doing_main = (use_fleet_id_list is None) aistate = get_aistate() if doing_main: aistate.misc['ReassignedFleetMissions'] = [] base_defense_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(MissionType.ORBITAL_DEFENSE) unassigned_base_defense_ids = FleetUtilsAI.extract_fleet_ids_without_mission_types(base_defense_ids) for fleet_id in unassigned_base_defense_ids: fleet = universe.getFleet(fleet_id) if not fleet: continue sys_id = fleet.systemID target = TargetSystem(sys_id) fleet_mission = aistate.get_fleet_mission(fleet_id) fleet_mission.clear_fleet_orders() fleet_mission.clear_target() mission_type = MissionType.ORBITAL_DEFENSE fleet_mission.set_target(mission_type, target) all_military_fleet_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(MissionType.MILITARY) if not all_military_fleet_ids: _military_allocations = [] return avail_mil_fleet_ids = list(FleetUtilsAI.extract_fleet_ids_without_mission_types(all_military_fleet_ids)) mil_needing_repair_ids, avail_mil_fleet_ids = avail_mil_needing_repair(avail_mil_fleet_ids) these_allocations = _military_allocations debug("==================================================") debug("Assigning military fleets") debug("---------------------------------") else: avail_mil_fleet_ids = list(use_fleet_id_list) mil_needing_repair_ids, avail_mil_fleet_ids = avail_mil_needing_repair(avail_mil_fleet_ids) these_allocations = allocations # send_for_repair(mil_needing_repair_ids) #currently, let get taken care of by AIFleetMission.generate_fleet_orders() # get systems to defend avail_mil_fleet_ids = set(avail_mil_fleet_ids) for sys_id, alloc, minalloc, rvp, takeAny in these_allocations: if not doing_main and not avail_mil_fleet_ids: break debug("Allocating for: %s", TargetSystem(sys_id)) found_fleets = [] found_stats = {} ensure_return = sys_id not in set(AIstate.colonyTargetedSystemIDs + AIstate.outpostTargetedSystemIDs + AIstate.invasionTargetedSystemIDs) these_fleets = FleetUtilsAI.get_fleets_for_mission( target_stats={'rating': alloc, 'ratingVsPlanets': rvp, 'target_system': TargetSystem(sys_id)}, min_stats={'rating': minalloc, 'ratingVsPlanets': rvp, 'target_system': TargetSystem(sys_id)}, cur_stats=found_stats, starting_system=sys_id, fleet_pool_set=avail_mil_fleet_ids, fleet_list=found_fleets, ensure_return=ensure_return) if not these_fleets: debug("Could not allocate any fleets.") if not found_fleets or not (FleetUtilsAI.stats_meet_reqs(found_stats, {'rating': minalloc}) or takeAny): if doing_main: if _verbose_mil_reporting: debug("NO available/suitable military allocation for system %d ( %s ) " "-- requested allocation %8d, found available rating %8d in fleets %s" % (sys_id, universe.getSystem(sys_id).name, minalloc, found_stats.get('rating', 0), found_fleets)) avail_mil_fleet_ids.update(found_fleets) continue else: these_fleets = found_fleets else: debug("Assigning fleets %s to target %s", these_fleets, TargetSystem(sys_id)) if doing_main and _verbose_mil_reporting: debug("FULL+ military allocation for system %d ( %s )" " -- requested allocation %8d, got %8d with fleets %s" % (sys_id, universe.getSystem(sys_id).name, alloc, found_stats.get('rating', 0), these_fleets)) target = TargetSystem(sys_id) for fleet_id in these_fleets: fo.issueAggressionOrder(fleet_id, True) fleet_mission = aistate.get_fleet_mission(fleet_id) fleet_mission.clear_fleet_orders() fleet_mission.clear_target() if sys_id in set(AIstate.colonyTargetedSystemIDs + AIstate.outpostTargetedSystemIDs + AIstate.invasionTargetedSystemIDs): mission_type = MissionType.SECURE elif state.get_empire_planets_by_system(sys_id): mission_type = MissionType.PROTECT_REGION else: mission_type = MissionType.MILITARY fleet_mission.set_target(mission_type, target) fleet_mission.generate_fleet_orders() if not doing_main: aistate.misc.setdefault('ReassignedFleetMissions', []).append(fleet_mission) if doing_main: debug("---------------------------------") last_round = 3 last_round_name = "LastRound" if round <= last_round: # check if any fleets remain unassigned all_military_fleet_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(MissionType.MILITARY) avail_mil_fleet_ids = list(FleetUtilsAI.extract_fleet_ids_without_mission_types(all_military_fleet_ids)) allocations = [] round += 1 thisround = "Extras Remaining Round %d" % round if round < last_round else last_round_name if avail_mil_fleet_ids: debug("Round %s - still have available military fleets: %s", thisround, avail_mil_fleet_ids) allocations = get_military_fleets(mil_fleets_ids=avail_mil_fleet_ids, try_reset=False, thisround=thisround) if allocations: assign_military_fleets_to_systems(use_fleet_id_list=avail_mil_fleet_ids, allocations=allocations, round=round) else: # assign remaining fleets to nearest systems to protect. all_military_fleet_ids = FleetUtilsAI.get_empire_fleet_ids_by_role(MissionType.MILITARY) avail_mil_fleet_ids = list(FleetUtilsAI.extract_fleet_ids_without_mission_types(all_military_fleet_ids)) def system_score(_fid, _sys_id): """Helper function to rank systems by priority""" jump_distance = universe.jumpDistance(_fid, _sys_id) if get_system_local_threat(_sys_id): weight = 10 elif get_system_neighbor_threat(_sys_id): weight = 3 elif get_system_jump2_threat(_sys_id): weight = 1 else: weight = 1 / max(.5, float(state.get_distance_to_enemy_supply(_sys_id)))**1.25 return float(weight) / (jump_distance+1) for fid in avail_mil_fleet_ids: fleet = universe.getFleet(fid) FleetUtilsAI.get_fleet_system(fleet) systems = state.get_empire_planets_by_system().keys() if not systems: continue sys_id = max(systems, key=lambda x: system_score(fid, x)) debug("Assigning leftover %s to system %d " "- nothing better to do.", fleet, sys_id) fleet_mission = aistate.get_fleet_mission(fid) fleet_mission.clear_fleet_orders() target_system = TargetSystem(sys_id) fleet_mission.set_target(MissionType.PROTECT_REGION, target_system) fleet_mission.generate_fleet_orders()
def assign_military_fleets_to_systems(useFleetIDList=None, allocations=None): # assign military fleets to military theater systems global military_allocations universe = fo.getUniverse() if allocations is None: allocations = [] doing_main = (useFleetIDList is None) if doing_main: base_defense_ids = FleetUtilsAI.get_empire_fleet_ids_by_role( AIFleetMissionType.FLEET_MISSION_ORBITAL_DEFENSE) unassigned_base_defense_ids = FleetUtilsAI.extract_fleet_ids_without_mission_types( base_defense_ids) for fleet_id in unassigned_base_defense_ids: fleet = universe.getFleet(fleet_id) if not fleet: continue sys_id = fleet.systemID target = AITarget.AITarget(TargetType.TARGET_SYSTEM, sys_id) fleet_mission = foAI.foAIstate.get_fleet_mission(fleet_id) fleet_mission.clear_fleet_orders() fleet_mission.clear_targets( (fleet_mission.get_mission_types() + [-1])[0]) mission_type = AIFleetMissionType.FLEET_MISSION_ORBITAL_DEFENSE fleet_mission.add_target(mission_type, target) all_military_fleet_ids = FleetUtilsAI.get_empire_fleet_ids_by_role( AIFleetMissionType.FLEET_MISSION_MILITARY) AIstate.militaryFleetIDs = all_military_fleet_ids if not all_military_fleet_ids: military_allocations = [] return avail_mil_fleet_ids = list( FleetUtilsAI.extract_fleet_ids_without_mission_types( all_military_fleet_ids)) mil_needing_repair_ids, avail_mil_fleet_ids = avail_mil_needing_repair( avail_mil_fleet_ids) these_allocations = military_allocations print "==================================================" print "assigning military fleets" print "---------------------------------" else: avail_mil_fleet_ids = list(useFleetIDList) mil_needing_repair_ids, avail_mil_fleet_ids = avail_mil_needing_repair( avail_mil_fleet_ids) these_allocations = allocations # send_for_repair(mil_needing_repair_ids) #currently, let get taken care of by AIFleetMission.generate_fleet_orders() # get systems to defend avail_mil_fleet_ids = set(avail_mil_fleet_ids) for sys_id, alloc, minalloc, takeAny in these_allocations: if not doing_main and not avail_mil_fleet_ids: break found_fleets = [] found_stats = {} try: these_fleets = FleetUtilsAI.get_fleets_for_mission( 1, {'rating': alloc}, {'rating': minalloc}, found_stats, "", systems_to_check=[sys_id], systems_checked=[], fleet_pool_set=avail_mil_fleet_ids, fleet_list=found_fleets, verbose=False) except: continue if not these_fleets: if not found_fleets or not (FleetUtilsAI.stats_meet_reqs( found_stats, {'rating': minalloc}) or takeAny): if doing_main: if verbose_mil_reporting: print "NO available/suitable military allocation for system %d ( %s ) -- requested allocation %8d, found available rating %8d in fleets %s" % ( sys_id, universe.getSystem(sys_id).name, minalloc, found_stats.get('rating', 0), found_fleets) avail_mil_fleet_ids.update(found_fleets) continue else: these_fleets = found_fleets # rating = sum( map(lambda x: foAI.foAIstate.rate_fleet(x), foundFleets ) ) ratings = map(foAI.foAIstate.get_rating, found_fleets) rating = sum([fr.get('attack', 0) for fr in ratings]) * sum( [fr.get('health', 0) for fr in ratings]) if doing_main and verbose_mil_reporting: if rating < minMilAllocations.get(sys_id, 0): print "PARTIAL military allocation for system %d ( %s ) -- requested allocation %8d -- got %8d with fleets %s" % ( sys_id, universe.getSystem(sys_id).name, minalloc, rating, these_fleets) else: print "FULL MIN military allocation for system %d ( %s ) -- requested allocation %8d -- got %8d with fleets %s" % ( sys_id, universe.getSystem(sys_id).name, minMilAllocations.get(sys_id, 0), rating, these_fleets) elif doing_main and verbose_mil_reporting: print "FULL+ military allocation for system %d ( %s ) -- requested allocation %8d, got %8d with fleets %s" % ( sys_id, universe.getSystem(sys_id).name, alloc, found_stats.get('rating', 0), these_fleets) target = AITarget.AITarget(TargetType.TARGET_SYSTEM, sys_id) for fleet_id in these_fleets: fo.issueAggressionOrder(fleet_id, True) fleet_mission = foAI.foAIstate.get_fleet_mission(fleet_id) fleet_mission.clear_fleet_orders() fleet_mission.clear_targets( (fleet_mission.get_mission_types() + [-1])[0]) if sys_id in list( set(AIstate.colonyTargetedSystemIDs + AIstate.outpostTargetedSystemIDs + AIstate.invasionTargetedSystemIDs + AIstate.blockadeTargetedSystemIDs)): mission_type = AIFleetMissionType.FLEET_MISSION_SECURE else: mission_type = AIFleetMissionType.FLEET_MISSION_MILITARY fleet_mission.add_target(mission_type, target) fleet_mission.generate_fleet_orders() if not doing_main: foAI.foAIstate.misc.setdefault('ReassignedFleetMissions', []).append(fleet_mission) if doing_main: print "---------------------------------"