def sendColonyShips(colonyFleetIDs, evaluatedPlanets, missionType): "sends a list of colony ships to a list of planet_value_pairs" fleetPool = colonyFleetIDs[:] potentialTargets = [ (pid, (score, specName) ) for (pid, (score, specName) ) in evaluatedPlanets if score > 30 ] print "colony/outpost ship matching -- fleets %s to planets %s"%( fleetPool, evaluatedPlanets) #for planetID_value_pair in evaluatedPlanets: fleetPool=set(fleetPool) universe=fo.getUniverse() while (len(fleetPool) > 0 ) and ( len(potentialTargets) >0): thisTarget = potentialTargets.pop(0) thisPlanetID=thisTarget[0] thisSysID = universe.getPlanet(thisPlanetID).systemID if (foAI.foAIstate.systemStatus.setdefault(thisSysID, {}).setdefault('monsterThreat', 0) > 2000) or (fo.currentTurn() <20 and foAI.foAIstate.systemStatus[thisSysID]['monsterThreat'] > 200): print "Skipping colonization of system %s due to Big Monster, threat %d"%(PlanetUtilsAI.sysNameIDs([thisSysID]), foAI.foAIstate.systemStatus[thisSysID]['monsterThreat']) continue thisSpec=thisTarget[1][1] foundFleets=[] thisFleetList = FleetUtilsAI.getFleetsForMission(nships=1, targetStats={}, minStats={}, curStats={}, species=thisSpec, systemsToCheck=[thisSysID], systemsChecked=[], fleetPoolSet = fleetPool, fleetList=foundFleets, verbose=False) if thisFleetList==[]: fleetPool.update(foundFleets)#just to be safe continue #must have no compatible colony/outpost ships fleetID = thisFleetList[0] aiTarget = AITarget.AITarget(AITargetType.TARGET_PLANET, thisPlanetID) aiFleetMission = foAI.foAIstate.getAIFleetMission(fleetID) aiFleetMission.addAITarget(missionType, aiTarget)
def assignScoutsToExploreSystems(): # TODO: use Graph Theory to explore closest systems universe = fo.getUniverse() capitalSysID = PlanetUtilsAI.getCapitalSysID() # order fleets to explore #explorableSystemIDs = foAI.foAIstate.getExplorableSystems(AIExplorableSystemType.EXPLORABLE_SYSTEM_UNEXPLORED) explorableSystemIDs = list(borderUnexploredSystemIDs) if not explorableSystemIDs or (capitalSysID == -1): return expSystemsByDist = sorted( map( lambda x: ( universe.linearDistance(capitalSysID, x), x) , explorableSystemIDs ) ) print "Exploration system considering following system-distance pairs:\n %s"%("[ "+ ", ".join(["%3d : %5.1f"%(sys, dist) for dist, sys in expSystemsByDist]) +" ]") exploreList = [sysID for dist, sysID in expSystemsByDist ] alreadyCovered, availableScouts = getCurrentExplorationInfo() print "explorable sys IDs: %s"%exploreList print "already targeted: %s"%alreadyCovered if 'needsEmergencyExploration' not in dir(foAI.foAIstate): foAI.foAIstate.needsEmergencyExploration=[] needsCoverage= foAI.foAIstate.needsEmergencyExploration + [sysID for sysID in exploreList if sysID not in alreadyCovered ] # emergency coverage cane be due to invasion detection trouble, etc. print "needs coverage: %s"%needsCoverage print "available scouts & AIstate locs: %s"%(map( lambda x: (x, foAI.foAIstate.fleetStatus.get(x, {}).get('sysID', -1)), availableScouts) ) print "available scouts & universe locs: %s"%(map( lambda x: (x, universe.getFleet(x).systemID), availableScouts) ) if not needsCoverage or not availableScouts: return availableScouts = set(availableScouts) sentList=[] while (len(availableScouts) > 0 ) and ( len(needsCoverage) >0): thisSysID = needsCoverage.pop(0) if (foAI.foAIstate.systemStatus.setdefault(thisSysID, {}).setdefault('monsterThreat', 0) > 2000*(foAI.foAIstate.aggression) ) or (fo.currentTurn() <20 and foAI.foAIstate.systemStatus[thisSysID]['monsterThreat'] > 200): print "Skipping exploration of system %d due to Big Monster, threat %d"%(thisSysID, foAI.foAIstate.systemStatus[thisSysID]['monsterThreat']) continue foundFleets=[] thisFleetList = FleetUtilsAI.getFleetsForMission(nships=1, targetStats={}, minStats={}, curStats={}, species="", systemsToCheck=[thisSysID], systemsChecked=[], fleetPoolSet = availableScouts, fleetList=foundFleets, verbose=False) if thisFleetList==[]: print "seem to have run out of scouts while trying to cover sysID %d"%thisSysID break #must have ran out of scouts fleetID = thisFleetList[0] aiFleetMission = foAI.foAIstate.getAIFleetMission( fleetID ) aiTarget = AITarget.AITarget(AITargetType.TARGET_SYSTEM, thisSysID ) if len(MoveUtilsAI.canTravelToSystemAndReturnToResupply(fleetID, aiFleetMission.getLocationAITarget(), aiTarget, fo.empireID())) > 0: aiFleetMission.addAITarget(AIFleetMissionType.FLEET_MISSION_EXPLORATION, aiTarget) sentList.append(thisSysID) else: #system too far out, skip it, but can add scout back to available pool print "sysID %d too far out for fleet ( ID %d ) to readch"%(thisSysID, fleetID) availableScouts.update(thisFleetList) print "sent scouting fleets to sysIDs : %s"%sentList return # pylint: disable=pointless-string-statement """
def sendInvasionFleets(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.getFleetsForMission(1, targetStats , minStats, foundStats, "", systemsToCheck=[sysID], systemsChecked=[], fleetPoolSet=invasionPool, fleetList=foundFleets, verbose=False) if theseFleets == []: if not FleetUtilsAI.statsMeetReqs(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.getAIFleetMission(fleetID) aiFleetMission.clearAIFleetOrders() aiFleetMission.clearAITargets( (aiFleetMission.getAIMissionTypes() + [-1])[0] ) aiFleetMission.addAITarget(missionType, aiTarget)
def sendColonyShips(colonyFleetIDs, evaluatedPlanets, missionType): "sends a list of colony ships to a list of planet_value_pairs" fleetPool = colonyFleetIDs[:] if (missionType == AIFleetMissionType.FLEET_MISSION_OUTPOST ): cost = 20+AIDependencies.outpostPodCost * ( 1 + len(AIstate.popCtrIDs)*AIDependencies.colonyPodUpkeep ) else: cost = 20+AIDependencies.colonyPodCost * ( 1 + len(AIstate.popCtrIDs)*AIDependencies.colonyPodUpkeep ) if fo.currentTurn() < 30: cost *= 0.4 #will be making fast tech progress so value is underestimated elif fo.currentTurn() < 60: cost *= 0.8 #will be making fast-ish tech progress so value is underestimated potentialTargets = [ (pid, (score, specName) ) for (pid, (score, specName) ) in evaluatedPlanets if score > (0.8 * cost) ] print "colony/outpost ship matching -- fleets %s to planets %s"%( fleetPool, evaluatedPlanets) #for planetID_value_pair in evaluatedPlanets: fleetPool=set(fleetPool) universe=fo.getUniverse() while (len(fleetPool) > 0 ) and ( len(potentialTargets) >0): thisTarget = potentialTargets.pop(0) thisScore=thisTarget[1][0] thisPlanetID=thisTarget[0] thisSysID = universe.getPlanet(thisPlanetID).systemID if (foAI.foAIstate.systemStatus.setdefault(thisSysID, {}).setdefault('monsterThreat', 0) > 2000) or (fo.currentTurn() <20 and foAI.foAIstate.systemStatus[thisSysID]['monsterThreat'] > 200): print "Skipping colonization of system %s due to Big Monster, threat %d"%(PlanetUtilsAI.sysNameIDs([thisSysID]), foAI.foAIstate.systemStatus[thisSysID]['monsterThreat']) continue thisSpec=thisTarget[1][1] foundFleets=[] thisFleetList = FleetUtilsAI.getFleetsForMission(nships=1, targetStats={}, minStats={}, curStats={}, species=thisSpec, systemsToCheck=[thisSysID], systemsChecked=[], fleetPoolSet = fleetPool, fleetList=foundFleets, verbose=False) if thisFleetList==[]: fleetPool.update(foundFleets)#just to be safe continue #must have no compatible colony/outpost ships fleetID = thisFleetList[0] aiTarget = AITarget.AITarget(AITargetType.TARGET_PLANET, thisPlanetID) aiFleetMission = foAI.foAIstate.getAIFleetMission(fleetID) aiFleetMission.addAITarget(missionType, aiTarget)
def assignMilitaryFleetsToSystems(): # assign military fleets to military theater systems global MilitaryAllocations universe = fo.getUniverse() baseDefenseIDs = FleetUtilsAI.getEmpireFleetIDsByRole(AIFleetMissionType.FLEET_MISSION_ORBITAL_DEFENSE) unassignedBaseDefenseIDs = FleetUtilsAI.extractFleetIDsWithoutMissionTypes(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.getAIFleetMission(fleetID) aiFleetMission.clearAIFleetOrders() aiFleetMission.clearAITargets( (aiFleetMission.getAIMissionTypes() + [-1])[0] ) missionType = AIFleetMissionType.FLEET_MISSION_ORBITAL_DEFENSE aiFleetMission.addAITarget( missionType , aiTarget) allMilitaryFleetIDs = FleetUtilsAI.getEmpireFleetIDsByRole(AIFleetMissionType.FLEET_MISSION_MILITARY) if allMilitaryFleetIDs == []: MilitaryAllocations = [] return #TODO: keep some continuity of missions AIstate.militaryFleetIDs = FleetUtilsAI.extractFleetIDsWithoutMissionTypes(allMilitaryFleetIDs) availMilFleetIDs = list( AIstate.militaryFleetIDs ) #availMilFleetIDs = list( allMilitaryFleetIDs) totMilRating = sum( map(lambda x: foAI.foAIstate.getRating(x).get('overall', 0), allMilitaryFleetIDs ) ) availMilRating = sum( map(lambda x: foAI.foAIstate.getRating(x).get('overall', 0), availMilFleetIDs ) ) print "==================================================" print "assigning military fleets" print "---------------------------------" remainingMilRating = availMilRating # get systems to defend availMilFleetIDs = set(availMilFleetIDs) for sysID, alloc, minalloc, takeAny in MilitaryAllocations: foundFleets = [] foundStats={} theseFleets = FleetUtilsAI.getFleetsForMission(1, {'rating':alloc}, {'rating':minalloc}, foundStats, "", systemsToCheck=[sysID], systemsChecked=[], fleetPoolSet=availMilFleetIDs, fleetList=foundFleets, verbose=False) if theseFleets == []: if foundFleets==[] or not ( FleetUtilsAI.statsMeetReqs( foundStats, {'rating':minalloc}) or takeAny): 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.rateFleet(x), foundFleets ) ) ratings = map(lambda x: foAI.foAIstate.rateFleet(x), foundFleets ) rating = sum([fr.get('attack', 0) for fr in ratings]) * sum([fr.get('health', 0) for fr in ratings]) 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) else: 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.getAIFleetMission(fleetID) aiFleetMission.clearAIFleetOrders() aiFleetMission.clearAITargets( (aiFleetMission.getAIMissionTypes() + [-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.addAITarget( missionType , aiTarget) print "---------------------------------"
def sendColonyShips(colonyFleetIDs, evaluatedPlanets, missionType): "sends a list of colony ships to a list of planet_value_pairs" fleetPool = colonyFleetIDs[:] if (missionType == AIFleetMissionType.FLEET_MISSION_OUTPOST ): cost = 20+AIDependencies.outpostPodCost * ( 1 + len(AIstate.popCtrIDs)*AIDependencies.colonyPodUpkeep ) else: cost = 20+AIDependencies.colonyPodCost * ( 1 + len(AIstate.popCtrIDs)*AIDependencies.colonyPodUpkeep ) if fo.currentTurn() < 30: cost *= 0.4 #will be making fast tech progress so value is underestimated elif fo.currentTurn() < 60: cost *= 0.8 #will be making fast-ish tech progress so value is underestimated potentialTargets = [ (pid, (score, specName) ) for (pid, (score, specName) ) in evaluatedPlanets if score > (0.8 * cost) ] print "colony/outpost ship matching -- fleets %s to planets %s"%( fleetPool, evaluatedPlanets) #adding a lot of checking here because have been getting mysterious exception, after too many recursions to get info fleetPool=set(fleetPool) universe=fo.getUniverse() empireID=fo.empireID() destroyedObjIDs = universe.destroyedObjectIDs(empireID) for fid in fleetPool: fleet = universe.getFleet(fid) if not fleet or fleet.empty: print "Error: bad fleet ( ID %d ) given to colonization routine; will be skipped"%fid fleetPool.remove(fid) continue reportStr="Fleet ID (%d): %d ships; species: "%(fid, fleet.numShips) for sid in fleet.shipIDs: ship = universe.getShip(sid) if not ship: reportStr += "NoShip, " else: reportStr += "%s, "%ship.speciesName print reportStr print #for planetID_value_pair in evaluatedPlanets: while (len(fleetPool) > 0 ) and ( len(potentialTargets) >0): thisTarget = potentialTargets.pop(0) thisScore=thisTarget[1][0] thisPlanetID=thisTarget[0] thisPlanet = universe.getPlanet(thisPlanetID) print "checking pool %s against target %s current owner %s targetSpec %s"%(fleetPool, thisPlanet.name, thisPlanet.owner, thisTarget) thisSysID = thisPlanet.systemID if (foAI.foAIstate.systemStatus.setdefault(thisSysID, {}).setdefault('monsterThreat', 0) > 2000) or (fo.currentTurn() <20 and foAI.foAIstate.systemStatus[thisSysID]['monsterThreat'] > 200): print "Skipping colonization of system %s due to Big Monster, threat %d"%(PlanetUtilsAI.sysNameIDs([thisSysID]), foAI.foAIstate.systemStatus[thisSysID]['monsterThreat']) continue thisSpec=thisTarget[1][1] foundFleets=[] try: thisFleetList = FleetUtilsAI.getFleetsForMission(nships=1, targetStats={}, minStats={}, curStats={}, species=thisSpec, systemsToCheck=[thisSysID], systemsChecked=[], fleetPoolSet = fleetPool, fleetList=foundFleets, triedFleets=set([]), verbose=False) except: continue if thisFleetList==[]: fleetPool.update(foundFleets)#just to be safe continue #must have no compatible colony/outpost ships fleetID = thisFleetList[0] aiTarget = AITarget.AITarget(AITargetType.TARGET_PLANET, thisPlanetID) aiFleetMission = foAI.foAIstate.getAIFleetMission(fleetID) aiFleetMission.addAITarget(missionType, aiTarget)
def assignInvasionFleetsToInvade(): # assign fleet targets to invadable planets universe = fo.getUniverse() empire = fo.getEmpire() empireID = empire.empireID fleetSupplyableSystemIDs = empire.fleetSupplyableSystemIDs fleetSupplyablePlanetIDs = PlanetUtilsAI.getPlanetsInSystemsIDs(fleetSupplyableSystemIDs) allTroopBaseFleetIDs = FleetUtilsAI.getEmpireFleetIDsByRole(EnumsAI.AIFleetMissionType.FLEET_MISSION_ORBITAL_INVASION) availTroopBaseFleetIDs = set(FleetUtilsAI.extractFleetIDsWithoutMissionTypes(allTroopBaseFleetIDs)) for fid in list(availTroopBaseFleetIDs): if fid not in availTroopBaseFleetIDs: continue fleet = universe.getFleet(fid) if not fleet: continue sysID = fleet.systemID system = universe.getSystem(sysID) availPlanets = set(system.planetIDs).intersection(set( foAI.foAIstate.qualifyingTroopBaseTargets.keys())) print "Considering Base Troopers in %s, found planets %s and regtistered targets %s with status %s"%(system.name, list(system.planetIDs), availPlanets, [(pid, foAI.foAIstate.qualifyingTroopBaseTargets[pid]) for pid in availPlanets]) targets = [pid for pid in availPlanets if foAI.foAIstate.qualifyingTroopBaseTargets[pid][1] != -1 ] if not targets: print "Error found no valid target for troop base in system %s (%d)"%(system.name, sysID) continue status=foAI.foAIstate.systemStatus.get( sysID, {} ) local_base_troops = set(status.get('myfleets', [])).intersection(availTroopBaseFleetIDs) troop_pod_tally = 0 for fid2 in local_base_troops: troop_pod_tally += FleetUtilsAI.countPartsFleetwide(fid2, ["GT_TROOP_POD"]) targetID=-1 bestScore=-1 target_troops = 0 # for pid, rating in assignInvasionValues(targets, EnumsAI.AIFleetMissionType.FLEET_MISSION_INVASION, fleetSupplyablePlanetIDs, empire).items(): p_score, p_troops = rating if p_score>bestScore: if p_troops >= 2*troop_pod_tally: pass #continue bestScore = p_score targetID = pid target_troops = p_troops if targetID != -1: local_base_troops.discard(fid) foundFleets = [] podsNeeded= math.ceil( (target_troops - 2*(FleetUtilsAI.countPartsFleetwide(fid, ["GT_TROOP_POD"]))+0.05)/2.0) foundStats={} minStats= {'rating':0, 'troopPods':podsNeeded} targetStats={'rating':10,'troopPods':podsNeeded} theseFleets = FleetUtilsAI.getFleetsForMission(1, targetStats , minStats, foundStats, "", systemsToCheck=[sysID], systemsChecked=[], fleetPoolSet=local_base_troops, fleetList=foundFleets, verbose=False) for fid2 in foundFleets: FleetUtilsAI.mergeFleetAintoB(fid2, fid) availTroopBaseFleetIDs.discard(fid2) availTroopBaseFleetIDs.discard(fid) foAI.foAIstate.qualifyingTroopBaseTargets[targetID][1] = -1 #TODO: should probably delete aiTarget = AITarget.AITarget(EnumsAI.AITargetType.TARGET_PLANET, targetID) aiFleetMission = foAI.foAIstate.getAIFleetMission(fid) aiFleetMission.addAITarget(EnumsAI.AIFleetMissionType.FLEET_MISSION_ORBITAL_INVASION, aiTarget) invasionFleetIDs = AIstate.invasionFleetIDs sendInvasionFleets(invasionFleetIDs, AIstate.invasionTargets, EnumsAI.AIFleetMissionType.FLEET_MISSION_INVASION) allInvasionFleetIDs = FleetUtilsAI.getEmpireFleetIDsByRole(EnumsAI.AIFleetMissionType.FLEET_MISSION_INVASION) for fid in FleetUtilsAI.extractFleetIDsWithoutMissionTypes(allInvasionFleetIDs): thisMission = foAI.foAIstate.getAIFleetMission(fid) thisMission.checkMergers(context="Post-send consolidation of unassigned troops")
def check_retarget_invasion(self): "checks if an invasion mission should be retargeted" universe=fo.getUniverse() empire = fo.getEmpire() empire_id = fo.empireID() fleet_id = self.target_id fleet=universe.getFleet(fleet_id) if fleet.systemID == -1: #next_loc=fleet.nextSystemID return #TODO: still check system = universe.getSystem(fleet.systemID) if not system: return orders=self.getAIFleetOrders() last_sys_target = -1 if orders: last_sys_target = orders[-1].getTargetAITarget().target_id if last_sys_target == fleet.systemID: return #TODO: check for best local target open_targets = [] already_targeted = InvasionAI.getInvasionTargetedPlanetIDs(system.planetIDs, EnumsAI.AIFleetMissionType.FLEET_MISSION_INVASION, empire_id) for pid in system.planetIDs: if pid in already_targeted or (pid in foAI.foAIstate.qualifyingTroopBaseTargets): continue planet = universe.getPlanet(pid) if planet.unowned or (planet.owner == empire_id): continue if (planet.currentMeterValue(fo.meterType.shield)) <= 0: open_targets.append(pid) if not open_targets: return troop_pod_tally = FleetUtilsAI.countPartsFleetwide(fleet_id, ["GT_TROOP_POD"]) targetID=-1 bestScore=-1 target_troops = 0 # fleetSupplyableSystemIDs = empire.fleetSupplyableSystemIDs fleetSupplyablePlanetIDs = PlanetUtilsAI.getPlanetsInSystemsIDs(fleetSupplyableSystemIDs) for pid, rating in InvasionAI.assignInvasionValues(open_targets, EnumsAI.AIFleetMissionType.FLEET_MISSION_INVASION, fleetSupplyablePlanetIDs, empire).items(): p_score, p_troops = rating if p_score>bestScore: if p_troops >= 2*troop_pod_tally: continue bestScore = p_score targetID = pid target_troops = p_troops if targetID == -1: return newFleets=FleetUtilsAI.splitFleet(fleet_id) self.clearAITargets(-1)#TODO: clear from foAIstate self.clearAIFleetOrders() podsNeeded= max(0, math.ceil( (target_troops - 2*(FleetUtilsAI.countPartsFleetwide(fleet_id, ["GT_TROOP_POD"]))+0.05)/2.0) ) foundStats={} minStats= {'rating':0, 'troopPods':podsNeeded} targetStats={'rating':10,'troopPods':podsNeeded} foundFleets = [] theseFleets = FleetUtilsAI.getFleetsForMission(1, targetStats , minStats, foundStats, "", systemsToCheck=[fleet.systemID], systemsChecked=[], fleetPoolSet=set(newFleets), fleetList=foundFleets, verbose=False) for fid2 in foundFleets: FleetUtilsAI.mergeFleetAintoB(fid2, fleet_id) aiTarget = AITarget.AITarget(EnumsAI.AITargetType.TARGET_PLANET, targetID) self.addAITarget(EnumsAI.AIFleetMissionType.FLEET_MISSION_INVASION, aiTarget) self.generateAIFleetOrders()
def assignMilitaryFleetsToSystems(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.getEmpireFleetIDsByRole(AIFleetMissionType.FLEET_MISSION_ORBITAL_DEFENSE) unassignedBaseDefenseIDs = FleetUtilsAI.extractFleetIDsWithoutMissionTypes(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.getAIFleetMission(fleetID) aiFleetMission.clearAIFleetOrders() aiFleetMission.clearAITargets( (aiFleetMission.getAIMissionTypes() + [-1])[0] ) missionType = AIFleetMissionType.FLEET_MISSION_ORBITAL_DEFENSE aiFleetMission.addAITarget( missionType , aiTarget) allMilitaryFleetIDs = FleetUtilsAI.getEmpireFleetIDsByRole(AIFleetMissionType.FLEET_MISSION_MILITARY) AIstate.militaryFleetIDs = allMilitaryFleetIDs if allMilitaryFleetIDs == []: MilitaryAllocations = [] return availMilFleetIDs = list( FleetUtilsAI.extractFleetIDsWithoutMissionTypes(allMilitaryFleetIDs) ) mil_needing_repair_ids, availMilFleetIDs = avail_mil_needing_repair(availMilFleetIDs) availMilRating = sum( map(lambda x: foAI.foAIstate.getRating(x).get('overall', 0), availMilFleetIDs ) ) under_repair_mil_rating = sum( map(lambda x: foAI.foAIstate.getRating(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.getRating(x).get('overall', 0), availMilFleetIDs ) ) theseAllocations = allocations remainingMilRating = availMilRating #send_for_repair(mil_needing_repair_ids) #currently, let get taken care of by AIFleetMission.generateAIFleetOrders() # 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.getFleetsForMission(1, {'rating':alloc}, {'rating':minalloc}, foundStats, "", systemsToCheck=[sysID], systemsChecked=[], fleetPoolSet=availMilFleetIDs, fleetList=foundFleets, verbose=False) except: continue if theseFleets == []: if foundFleets==[] or not ( FleetUtilsAI.statsMeetReqs( 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.rateFleet(x), foundFleets ) ) ratings = map(foAI.foAIstate.getRating, 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.getAIFleetMission(fleetID) aiFleetMission.clearAIFleetOrders() aiFleetMission.clearAITargets( (aiFleetMission.getAIMissionTypes() + [-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.addAITarget( missionType , aiTarget) aiFleetMission.generateAIFleetOrders() if not doingMain: foAI.foAIstate.misc.setdefault('ReassignedFleetMissions', []).append(aiFleetMission) if doingMain: print "---------------------------------"