def setGeneralPlanetResourceFocus(): "set resource focus of planets except capitalID, asteroids, and gas giants" universe = fo.getUniverse() empire = fo.getEmpire() empireID = empire.empireID empirePlanetIDs = PlanetUtilsAI.getOwnedPlanetsByEmpire(universe.planetIDs, empireID) capitalID = [empire.capitalID] asteroids = PlanetUtilsAI.getTypePlanetEmpireOwned(fo.planetType.asteroids) gasGiants = PlanetUtilsAI.getTypePlanetEmpireOwned(fo.planetType.gasGiant) generalPlanetIDs = list(set(empirePlanetIDs) - (set(capitalID)|set(asteroids)|set(gasGiants))) topPriority = topResourcePriority() fleetSupplyableSystemIDs = empire.fleetSupplyableSystemIDs fleetSupplyablePlanetIDs = PlanetUtilsAI.getPlanetsInSystemsIDs(fleetSupplyableSystemIDs) if topPriority == AIPriorityType.PRIORITY_RESOURCE_FOOD: newFocus = AIFocusType.FOCUS_FARMING for planetID in generalPlanetIDs: planet = universe.getPlanet(planetID) focus = newFocus if focus in planet.availableFoci: fo.issueChangeFocusOrder(planetID, focus) elif topPriority == AIPriorityType.PRIORITY_RESOURCE_MINERALS: newFocus = AIFocusType.FOCUS_MINING for planetID in generalPlanetIDs: planet = universe.getPlanet(planetID) focus = newFocus if planetID in fleetSupplyablePlanetIDs and focus in planet.availableFoci: fo.issueChangeFocusOrder(planetID, focus) elif topPriority == AIPriorityType.PRIORITY_RESOURCE_PRODUCTION: newFocus = AIFocusType.FOCUS_INDUSTRY for planetID in generalPlanetIDs: planet = universe.getPlanet(planetID) focus = newFocus if planetID in fleetSupplyablePlanetIDs and focus in planet.availableFoci: fo.issueChangeFocusOrder(planetID, focus) elif topPriority == AIPriorityType.PRIORITY_RESOURCE_RESEARCH: newFocus = AIFocusType.FOCUS_RESEARCH for planetID in generalPlanetIDs: planet = universe.getPlanet(planetID) focus = newFocus if planetID in fleetSupplyablePlanetIDs and focus in planet.availableFoci: fo.issueChangeFocusOrder(planetID, focus) else: focus = AIFocusType.FOCUS_FARMING if focus in planet.availableFoci: fo.issueChangeFocusOrder(planetID, focus)
def setGeneralPlanetResourceFocus(): "set resource focus of planets except capitalID, asteroids, and gas giants" global __timerFile universe = fo.getUniverse() empire = fo.getEmpire() empireID = empire.empireID timer= [ time() ] # getPlanets empirePlanetIDs = PlanetUtilsAI.getOwnedPlanetsByEmpire(universe.planetIDs, empireID) timer.append( time() ) #filter capitalIDL = [PlanetUtilsAI.getCapital()] asteroids = PlanetUtilsAI.getTypePlanetEmpireOwned(fo.planetType.asteroids) gasGiants = PlanetUtilsAI.getTypePlanetEmpireOwned(fo.planetType.gasGiant) generalPlanetIDs = list(set(empirePlanetIDs) - (set(capitalIDL)|set(asteroids)|set(gasGiants))) timer.append( time() ) #Priority #fleetSupplyableSystemIDs = empire.fleetSupplyableSystemIDs #fleetSupplyablePlanetIDs = PlanetUtilsAI.getPlanetsInSystemsIDs(fleetSupplyableSystemIDs) ppPrio = foAI.foAIstate.getPriority(AIPriorityType.PRIORITY_RESOURCE_PRODUCTION) rpPrio = foAI.foAIstate.getPriority(AIPriorityType.PRIORITY_RESOURCE_RESEARCH) priorityRatio = float(ppPrio)/(rpPrio+0.0001) timer.append( time() ) # shuffle # not supporting Growth for general planets until also adding code to make sure would actually benefit shuffle(generalPlanetIDs) timer.append( time() ) # targets pp, rp = getResourceTargetTotals() targetRatio = pp/(rp+0.0001) nplanets = len(generalPlanetIDs) nChanges = int( ( nplanets * max( 1.0/(rp+0.001), 2.0/(pp+0.0001)) * ( (pp - rp* priorityRatio)/(priorityRatio +2))) +0.5 ) # weird formula I came up with to estimate desired number of changes print "current target totals -- pp: %.1f rp: %.1f ; ratio %.2f ; desired ratio %.2f will change up to %d foci from total of %d planets"%(pp, rp, targetRatio, priorityRatio, nChanges, nplanets) timer.append( time() ) #loop iChanges = 0 for planetID in generalPlanetIDs: if iChanges >= nChanges: break planet = universe.getPlanet(planetID) oldFocus=planet.focus targetRatio = pp/(rp + 0.0001) ratioRatio = priorityRatio / ( targetRatio + 0.0001) if (AIFocusType.FOCUS_MINING in planet.availableFoci): #and ((priorityRatio > 0.5 ) or ( ratioRatio > 0.9 ) ) : #could be a more complex decision here, if oldFocus != AIFocusType.FOCUS_MINING: iChanges +=1 #even if not necessarily directed towards desired ratio fo.issueChangeFocusOrder(planetID, AIFocusType.FOCUS_MINING) print "Changing planet focus for ID %s : %s from %s to %s "%(planetID, planet.name, oldFocus, AIFocusType.FOCUS_MINING ) continue newFocus=None if ( ratioRatio > 1.1 ): #needs industry if oldFocus == AIFocusType.FOCUS_INDUSTRY: continue else: newFocus = AIFocusType.FOCUS_INDUSTRY elif ( ratioRatio < 0.91 ): if oldFocus == AIFocusType.FOCUS_RESEARCH: continue else: newFocus = AIFocusType.FOCUS_RESEARCH if newFocus: #pp -= planet.currentMeterValue(fo.meterType.targetIndustry) #rp -= planet.currentMeterValue(fo.meterType.targetResearch) fo.issueChangeFocusOrder(planetID, newFocus) iChanges += 1 #pp += planet.currentMeterValue(fo.meterType.targetIndustry) #rp += planet.currentMeterValue(fo.meterType.targetResearch) print "Changing planet focus for ID %s : %s from %s to %s "%(planetID, planet.name, oldFocus, newFocus ) timer.append( time() ) #end if doResourceTiming and __timerEntries==__timerEntries2: times = [timer[i] - timer[i-1] for i in range(1, len(timer) ) ] timeFmt = "%30s: %8d msec " print "ResourcesAI Time Requirements:" for mod, modTime in zip(__timerEntries, times): print timeFmt%((30*' '+mod)[-30:], int(1000*modTime)) if resourceTimerFile: print "len times: %d ; len entries: %d "%(len(times), len(__timerEntries)) resourceTimerFile.write( __timerFileFmt%tuple( [ fo.currentTurn() ]+map(lambda x: int(1000*x), times )) +'\n') resourceTimerFile.flush()