From: <the...@us...> - 2008-06-21 17:13:10
|
Revision: 2596 http://freeorion.svn.sourceforge.net/freeorion/revision/?rev=2596&view=rev Author: thesilentone Date: 2008-06-21 10:13:15 -0700 (Sat, 21 Jun 2008) Log Message: ----------- AI update: -added priorityAI module that determines which resources the AI empire needs and what the AI should build/research -module currently determines demand for scouts, colony ships, food, minerals and production -updated other modules accordingly -several minor tweaks Modified Paths: -------------- trunk/FreeOrion/default/AI/AIstate.py trunk/FreeOrion/default/AI/ColonisationAI.py trunk/FreeOrion/default/AI/ExplorationAI.py trunk/FreeOrion/default/AI/FleetUtils.py trunk/FreeOrion/default/AI/FreeOrionAI.py Modified: trunk/FreeOrion/default/AI/AIstate.py =================================================================== --- trunk/FreeOrion/default/AI/AIstate.py 2008-06-21 03:17:40 UTC (rev 2595) +++ trunk/FreeOrion/default/AI/AIstate.py 2008-06-21 17:13:15 UTC (rev 2596) @@ -2,26 +2,28 @@ import freeOrionAIInterface as fo # TO DO: -# hide gs_etc functions +# hide gs_etc functions +# move enumerators in their own module # AI enumerators missionTypes = ["MT_NONE", "MT_EXPLORATION", "MT_COLONISATION", "MT_MOVE", "MT_MERGE", "MT_ATTACK", "MT_GUARD"] -# fleetRoles = ... <> conversion to/from missionType? - +# fleetRoles = ... <> conversion to/from missionType? + +# ship Roles # MOVE militaryRoles = ["SR_ATTACK", "SR_LONGRANGE", "SR_MISSILES", "SR_POINTDEFENSE"] civilianRoles = ["SR_EXPLORATION", "SR_COLONISATION"] -shipRoles = ["SR_NONE"] + militaryRoles + civilianRoles +shipRoles = ["SR_NONE"] + militaryRoles + civilianRoles + + +# global variables +foodStockpileSize = 1 # food stored per population +minimalColoniseValue = 4 # minimal value for a planet to be colonised, now a size 2 terran world -priorityResources = ["PR_FOOD", "PR_MINERALS", "PR_PRODUCTION", "PR_RESEARCH", "PR_TRADE"] -priorityProduction = ["PR_EXPLORATION", "PR_COLONISATION", "PR_MILITARY", "PR_BUILDINGS"] -priorityResearch = ["PR_LEARNING", "PR_GROWTH", "PR_PRODUCTION", "PR_CONSTRUCTION", "PR_ECONOMICS", "PR_SHIPS"] -priorityTypes = ["PR_NONE"] + priorityResources + priorityProduction + priorityResearch - # AIstate class class AIstate: - "stores, saves and loads AI gamestate" + "stores AI gamestate" # def startTurn (cleans missions, fleetroles, calls colonisable planets + priorities) # def endTurn @@ -59,14 +61,17 @@ def removeFleetRole(self, fleetID): gs_removeFleetRole(self, fleetID) def cleanFleetRoles(self): gs_cleanFleetRoles(self) - # colonisable planets (should be set at start of turn) - # (hide?) setColonisablePlanets + # common variables + # def explorableSystems +# def colonisablePlanets (should be set at start of turn) + # def # getColonisablePlanets (deepcopy!) # demands (should be set at start of turn) def setPriority(self, priorityType, value): gs_setPriority(self, priorityType, value) def getPriority(self, priorityType): return gs_getPriority(self, priorityType) - def getAllPriorities(self): return gs_getAllPriorities(self) + def getAllPriorities(self): return gs_getAllPriorities(self) + def printPriorities(self): gs_printPriorities(self) # constructor / destructor def __init__(self): @@ -324,7 +329,7 @@ "returns the priority value of the specified type" if priorityType in self.__priorityByType__: - return self.__priorityByType__[priorityType] + return copy.deepcopy(self.__priorityByType__[priorityType]) return 0 # todo: check for valid types @@ -334,3 +339,10 @@ "returns a dictionary with all priority values" return copy.deepcopy(self.__priorityByType__) + +def gs_printPriorities(self): + "prints all priorities" + + for priority in self.__priorityByType__: + print priority + ": " + str(self.__priorityByType__[priority]) + Modified: trunk/FreeOrion/default/AI/ColonisationAI.py =================================================================== --- trunk/FreeOrion/default/AI/ColonisationAI.py 2008-06-21 03:17:40 UTC (rev 2595) +++ trunk/FreeOrion/default/AI/ColonisationAI.py 2008-06-21 17:13:15 UTC (rev 2596) @@ -1,17 +1,16 @@ import freeOrionAIInterface as fo import FreeOrionAI as foAI +import AIstate import FleetUtils # Evaluation uses only size and hospitality score # later: specials, exploration, distance +# TODO: do NOT pass universe, empire etc. -# constants -minimalColoniseValue = 4 # minimal value a planet must have to be colonised; right now a size 2 terran planet - # globals -colonisablePlanets = [] # !!! move into AIstate +colonisablePlanetIDs = [] # !!! move into AIstate # main function @@ -21,22 +20,22 @@ empireID = fo.empireID() universe = fo.getUniverse() - # get colonyships - allColonyFleetIDs = FleetUtils.getEmpireFleetIDsByRole(empireID, "MT_COLONISATION") + # get colony fleets + allColonyFleetIDs = FleetUtils.getEmpireFleetIDsByRole("MT_COLONISATION") colonyFleetIDs = FleetUtils.extractFleetIDsWithoutMission(allColonyFleetIDs) - removeInvalidMissions(universe) + removeInvalidMissions() print "Colony Fleets: " + str(allColonyFleetIDs) print "Current colonise missions: " + str(foAI.foAIstate.getMissions("MT_COLONISATION")) # get planets - systemIDs = getExploredSystemIDs(empire, universe) - planetIDs = getPlanetsInSystemsIDs(systemIDs, universe) + systemIDs = getExploredSystemIDs() + planetIDs = getPlanetsInSystemsIDs(systemIDs) - removeAlreadyOwnedPlanetIDs(planetIDs, empireID, universe) + removeAlreadyOwnedPlanetIDs(planetIDs) - evaluatedPlanets = assignColonisationValues(planetIDs, universe) + evaluatedPlanets = assignColonisationValues(planetIDs) removeLowValuePlanets(evaluatedPlanets) sortedPlanets = evaluatedPlanets.items() @@ -46,21 +45,23 @@ for evaluationPair in sortedPlanets: print "ID|Score: " + str(evaluationPair) # export planets for other AI modules - global colonisablePlanets - colonisablePlanets = sortedPlanets # !!! move into AIstate? + global colonisablePlanetIDs + colonisablePlanetIDs = sortedPlanets # !!! move into AIstate? # send Colony Ships, colonise if at target system - sendColonyShips(colonyFleetIDs, sortedPlanets, universe) - coloniseTargetPlanets(universe) + sendColonyShips(colonyFleetIDs, sortedPlanets) + coloniseTargetPlanets() -def removeInvalidMissions(universe): +def removeInvalidMissions(): "deletes invalid colonisation missions" print "Removing invalid colonisation missions:" + universe = fo.getUniverse() + missions = foAI.foAIstate.getMissions("MT_COLONISATION") for fleetID in missions: @@ -84,9 +85,12 @@ foAI.foAIstate.removeMission("MT_COLONISATION", fleetID) -def getExploredSystemIDs(empire, universe): +def getExploredSystemIDs(): "retrieves all systems explored by the empire" + empire = fo.getEmpire() + universe = fo.getUniverse() + systemIDs = [] objectIDs = universe.allObjectIDs @@ -99,9 +103,11 @@ return systemIDs -def getPlanetsInSystemsIDs(systemIDs, universe): +def getPlanetsInSystemsIDs(systemIDs): "creates a list with all planets known to the empire" + universe = fo.getUniverse() + planetIDs = [] objectIDs = universe.allObjectIDs @@ -115,9 +121,12 @@ return planetIDs -def removeAlreadyOwnedPlanetIDs(planetIDs, empireID, universe): +def removeAlreadyOwnedPlanetIDs(planetIDs): "removes planets that already are being colonised or owned" + empireID = fo.empireID() + universe = fo.getUniverse() + coloniseMissions = foAI.foAIstate.getMissions("MT_COLONISATION") deletePlanets = [] @@ -139,30 +148,36 @@ # print "removed planet " + str(ID) -def assignColonisationValues(planetIDs, universe): +def assignColonisationValues(planetIDs): "creates a dictionary that takes planetIDs as key and their colonisation score as value" + universe = fo.getUniverse() + planetValues = {} for planetID in planetIDs: - planetValues[planetID] = evaluatePlanet(planetID, universe) + planetValues[planetID] = evaluatePlanet(planetID) return planetValues -def evaluatePlanet(planetID, universe): +def evaluatePlanet(planetID): "returns the colonisation value of a planet" + universe = fo.getUniverse() + planet = universe.getPlanet(planetID) if (planet == None): return 0 - return getPlanetHospitality(planetID, universe) * planet.size + return getPlanetHospitality(planetID) * planet.size # planet size ranges from 1-5 -def getPlanetHospitality(planetID, universe): +def getPlanetHospitality(planetID): "returns a value depending on the planet type" + universe = fo.getUniverse() + planet = universe.getPlanet(planetID) if planet == None: return 0 @@ -182,15 +197,17 @@ removeIDs = [] for planetID in evaluatedPlanets.iterkeys(): - if (evaluatedPlanets[planetID] < minimalColoniseValue): + if (evaluatedPlanets[planetID] < AIstate.minimalColoniseValue): removeIDs.append(planetID) for ID in removeIDs: del evaluatedPlanets[ID] -def sendColonyShips(colonyFleetIDs, evaluatedPlanets, universe): +def sendColonyShips(colonyFleetIDs, evaluatedPlanets): "sends a list of colony ships to a list of planet_value_pairs" + universe = fo.getUniverse() + i = 0 for planetID_value_pair in evaluatedPlanets: @@ -212,9 +229,11 @@ i=i+1 -def coloniseTargetPlanets(universe): +def coloniseTargetPlanets(): "checks if a colonyship has arrived at its destination; colonises planet and removes mission" + universe = fo.getUniverse() + coloniseMissions = foAI.foAIstate.getMissions("MT_COLONISATION") for fleetID in coloniseMissions: Modified: trunk/FreeOrion/default/AI/ExplorationAI.py =================================================================== --- trunk/FreeOrion/default/AI/ExplorationAI.py 2008-06-21 03:17:40 UTC (rev 2595) +++ trunk/FreeOrion/default/AI/ExplorationAI.py 2008-06-21 17:13:15 UTC (rev 2596) @@ -2,19 +2,25 @@ import FreeOrionAI as foAI import FleetUtils +# to do: assign scout fleets to closest planets (put in FleetUtils) +# do NOT pass empire from main function + +# globals +explorableSystemIDs = [] # move out to AIstate + + def generateExplorationOrders(): - # retreive objects from freeOrionAIInterface - empire = fo.getEmpire() empireID = fo.empireID() # get explorable systems and scouting fleets - systemIDs = getExplorableSystemIDs(getHomeSystemID(empire), empireID) + global explorableSystemIDs + explorableSystemIDs = getExplorableSystemIDs(getHomeSystemID()) - removeInvalidExploreMissions(empire) + removeInvalidExploreMissions() - mFleetIDs = FleetUtils.getEmpireFleetIDsByRole(empireID, "MT_EXPLORATION") - fleetIDs = FleetUtils.extractFleetIDsWithoutMission(mFleetIDs) + mFleetIDs = FleetUtils.getEmpireFleetIDsByRole("MT_EXPLORATION") + fleetIDs = FleetUtils.extractFleetIDsWithoutMission(mFleetIDs) # line can probably be removed # order fleets to explore for fleetID in fleetIDs: @@ -23,7 +29,7 @@ if foAI.foAIstate.hasMission("MT_EXPLORATION", fleetID): continue # else send fleet to a system - for systemID in systemIDs: + for systemID in explorableSystemIDs: # if system is already being explored, continue if foAI.foAIstate.hasTarget("MT_EXPLORATION", systemID): continue @@ -33,16 +39,16 @@ foAI.foAIstate.addMission("MT_EXPLORATION", [fleetID, systemID]) break - print "Scouts: " + str(FleetUtils.getEmpireFleetIDsByRole(empireID, "MT_EXPLORATION")) + print "Scouts: " + str(FleetUtils.getEmpireFleetIDsByRole("MT_EXPLORATION")) print "Systems being explored (fleet|system): " + str(foAI.foAIstate.getMissions("MT_EXPLORATION")) - -def getHomeSystemID(empire): +def getHomeSystemID(): "returns the systemID of the home world" + empire = fo.getEmpire() universe = fo.getUniverse() homeworld = universe.getPlanet(empire.homeworldID) @@ -51,12 +57,13 @@ # returns list of systems ids known of by but not explored by empireID, # that a ship located in startSystemID could reach via starlanes -def getExplorableSystemIDs(startSystemID, empireID): +def getExplorableSystemIDs(startSystemID): "returns explorable systems" universe = fo.getUniverse() objectIDs = universe.allObjectIDs - empire = fo.getEmpire(empireID) + empireID = fo.empireID() + empire = fo.getEmpire() systemIDs = [] @@ -70,11 +77,12 @@ return systemIDs -def removeInvalidExploreMissions(empire): +def removeInvalidExploreMissions(): "removes missions if fleet is destroyed or system already explored" print "Removing invalid exploration missions:" + empire = fo.getEmpire() universe = fo.getUniverse() exploreMissions = foAI.foAIstate.getMissions("MT_EXPLORATION") Modified: trunk/FreeOrion/default/AI/FleetUtils.py =================================================================== --- trunk/FreeOrion/default/AI/FleetUtils.py 2008-06-21 03:17:40 UTC (rev 2595) +++ trunk/FreeOrion/default/AI/FleetUtils.py 2008-06-21 17:13:15 UTC (rev 2596) @@ -67,10 +67,12 @@ return shipID -def getEmpireFleetIDs(empireID): +def getEmpireFleetIDs(): "returns all fleetIDs of an empire" - universe = fo.getUniverse() + universe = fo.getUniverse() + empireID = fo.empireID() + fleetIDs = universe.fleetIDs empireFleetIDs = [] @@ -86,10 +88,12 @@ return empireFleetIDs -def getEmpireFleetIDsByRole(empireID, fleetRole): +def getEmpireFleetIDsByRole(fleetRole): "returns a list with fleetIDs that have the specified role" + + empireID = fo.empireID() - fleetIDs = getEmpireFleetIDs(empireID) + fleetIDs = getEmpireFleetIDs() fleetIDsWithRole = [] for fleetID in fleetIDs: Modified: trunk/FreeOrion/default/AI/FreeOrionAI.py =================================================================== --- trunk/FreeOrion/default/AI/FreeOrionAI.py 2008-06-21 03:17:40 UTC (rev 2595) +++ trunk/FreeOrion/default/AI/FreeOrionAI.py 2008-06-21 17:13:15 UTC (rev 2596) @@ -4,6 +4,8 @@ import FleetUtils import ExplorationAI import ColonisationAI +# import TacticsAI +import PriorityAI # import ProductionAI # AIstate @@ -55,7 +57,7 @@ # called when client receives a load game message def resumeLoadedGame(savedStateString): print "Resuming loaded game" - try: + try: foAIstate = AIstate.AIstate() # foAIstate = pickle.loads(savedStateString) except: @@ -89,11 +91,15 @@ # pre-turn cleanup foAIstate.cleanFleetRoles() - # ...missions + # ...missions + # ...demands/priorities - # call AI modules + # call AI modules + PriorityAI.calculatePriorities() + ExplorationAI.generateExplorationOrders() ColonisationAI.generateColonisationOrders() - # ProductionAI.generateProductionOrders() +# ProductionAI.generateProductionOrders() +# TacticsAI.generateTacticOrders() fo.doneTurn() |