def getAnyAIMissionTypes(self): "return types of mission" if AIMissionType.FLEET_MISSION == self.getAIMissionType(): return EnumsAI.getAIFleetMissionTypes() elif AIMissionType.EMPIRE_WAR_MISSION == self.getAIMissionType(): return EnumsAI.getAIEmpireWarMissionTypes() return NotImplemented
def __cleanExplorableSystems(self, startSystemID): "cleanup of all explorable systems" universe = fo.getUniverse() systemIDs = universe.systemIDs empireID = fo.empireID() empire = fo.getEmpire() for systemID in systemIDs: system = universe.getSystem(systemID) if not system: continue #print "system with id: " + str(systemID) if (empire.hasExploredSystem(systemID)): self.addExplorableSystem(AIExplorableSystemType.EXPLORABLE_SYSTEM_EXPLORED, systemID) self.removeExplorableSystem(AIExplorableSystemType.EXPLORABLE_SYSTEM_UNEXPLORED, systemID) #print " has been explored" continue if (startSystemID == -1 or not universe.systemsConnected(systemID, startSystemID, empireID)): for explorableSystemsType in EnumsAI.getAIExplorableSystemTypes(): self.removeExplorableSystem(explorableSystemsType, systemID) #print " is not connected to system with id: " + str(startSystemID) continue explorableSystemsType = self.getExplorableSystem(systemID) if (explorableSystemsType == AIExplorableSystemType.EXPLORABLE_SYSTEM_VISIBLE): #print " is already explored system target" continue #print " is now an unexplored system" self.addExplorableSystem(AIExplorableSystemType.EXPLORABLE_SYSTEM_UNEXPLORED, systemID)
def __init__(self): "constructor" self.__missionsByType = {} for missionType in EnumsAI.getAIFleetMissionTypes(): self.__missionsByType[missionType] = {} self.__aiMissionsByFleetID = {} self.__shipRoleByDesignID = {} self.__fleetRoleByID = {} self.__priorityByType = {} self.__explorableSystemByType = {} for explorableSystemsType in EnumsAI.getAIExplorableSystemTypes(): self.__explorableSystemByType[explorableSystemsType] = {}
def addFleetRole(self, fleetID, missionType): "adds a fleet ID/role pair" if not EnumsAI.checkValidity(missionType): return if fleetID in self.__fleetRoleByID: #print "Fleet ID " + str(fleetID) + " already exists." return else: self.__fleetRoleByID[fleetID] = missionType
def addShipRole(self, shipDesignID, shipRole): "adds a ship designID/role pair" if not (shipRole in EnumsAI.getAIShipRolesTypes()): print "Invalid shipRole: " + str(shipRole) return elif shipDesignID in self.__shipRoleByDesignID: return else: self.__shipRoleByDesignID[shipDesignID] = shipRole
def addExplorableSystem(self, explorableSystemsType, systemID): "add explorable system ID with type" if not (explorableSystemsType in EnumsAI.getAIExplorableSystemTypes()): return systems = self.__explorableSystemByType[explorableSystemsType] if systemID in systems: return systems[systemID] = systemID
def getExplorableSystem(self, systemID): "determines system type from ID and returns it" for explorableSystemsType in EnumsAI.getAIExplorableSystemTypes(): systems = self.getExplorableSystems(explorableSystemsType) if systemID in systems: return explorableSystemsType # print "SystemID " + str(systemID) + " not found." return AIExplorableSystemType.EXPLORABLE_SYSTEM_INVALID
def valid(self): """Returns if this object is valid.""" if self.target_id is None or self.target_type is None or \ not EnumsAI.check_validity(self.target_id): return False if TargetType.TARGET_EMPIRE == self.target_type: return self.target_id in fo.AllEmpireIDs() else: return self.target_obj is not None
def addShipRole(self, shipDesignID, shipRole): "adds a ship name/role pair" if not (shipRole in EnumsAI.getAIShipRolesTypes()): print "Invalid shipRole: " + str(shipRole) return if shipDesignID in self.__shipRoleByDesignID: # print shipDesignID + " already exists." return self.__shipRoleByDesignID[shipDesignID] = shipRole
def valid(self): "returns if this object is valid" if self.target_id == None or self.target_type == None or \ EnumsAI.checkValidity(self.target_id) == False: return False if AITargetType.TARGET_EMPIRE == self.target_type: return self.target_id in fo.AllEmpireIDs() else: return None != self.target_obj return False
def calculateTopProductionQueuePriority(): """calculates the top production queue priority""" productionQueuePriorities = {} for priorityType in EnumsAI.get_priority_production_types(): productionQueuePriorities[priorityType] = foAI.foAIstate.get_priority(priorityType) sortedPriorities = productionQueuePriorities.items() sortedPriorities.sort(lambda x, y: cmp(x[1], y[1]), reverse=True) topProductionQueuePriority = -1 for evaluationPair in sortedPriorities: if topProductionQueuePriority < 0: topProductionQueuePriority = evaluationPair[0] return topProductionQueuePriority
def calculateTopProductionQueuePriority(): """calculates the top production queue priority""" productionQueuePriorities = {} for priorityType in EnumsAI.get_priority_production_types(): productionQueuePriorities[priorityType] = foAI.foAIstate.get_priority(priorityType) sortedPriorities = productionQueuePriorities.items() sortedPriorities.sort(lambda x,y: cmp(x[1], y[1]), reverse=True) topProductionQueuePriority = -1 for evaluationPair in sortedPriorities: if topProductionQueuePriority < 0: topProductionQueuePriority = evaluationPair[0] return topProductionQueuePriority
def assessFleetRole(fleetID): "counts the number of ShipRoles in a fleet and returns a corresponding fleetRole" print ("assessing role of fleet with id " + str(fleetID)) # TODO: one colony ship in fleet should mean it's a colony fleet universe = fo.getUniverse() shipRoles = {} for shipRole in EnumsAI.getAIShipRolesTypes(): shipRoles[shipRole] = 0 fleet = universe.getFleet(fleetID) if not fleet: print "couldn't get fleet with id " + str(fleetID) return AIShipRoleType.SHIP_ROLE_INVALID # count shipRoles for shipID in fleet.shipIDs: ship = universe.getShip(shipID) if ship.design: role = foAI.foAIstate.getShipRole(ship.design.id) else: role = AIShipRoleType.SHIP_ROLE_INVALID if role != AIShipRoleType.SHIP_ROLE_INVALID: shipRoles[role] = shipRoles[role] + 1 # determine most common shipRole favouriteRole = AIShipRoleType.SHIP_ROLE_INVALID for shipRole in shipRoles: if shipRoles[shipRole] == max(shipRoles.values()): favouriteRole = shipRole # assign fleet role if favouriteRole == AIShipRoleType.SHIP_ROLE_CIVILIAN_EXPLORATION: return AIFleetMissionType.FLEET_MISSION_EXPLORATION if favouriteRole == AIShipRoleType.SHIP_ROLE_CIVILIAN_COLONISATION: return AIFleetMissionType.FLEET_MISSION_COLONISATION if favouriteRole == AIShipRoleType.SHIP_ROLE_CIVILIAN_OUTPOST: return AIFleetMissionType.FLEET_MISSION_OUTPOST if favouriteRole == AIShipRoleType.SHIP_ROLE_MILITARY_INVASION: return AIFleetMissionType.FLEET_MISSION_INVASION if favouriteRole == AIShipRoleType.SHIP_ROLE_MILITARY_ATTACK: return AIFleetMissionType.FLEET_MISSION_ATTACK if favouriteRole == AIShipRoleType.SHIP_ROLE_MILITARY: return AIFleetMissionType.FLEET_MISSION_MILITARY return AIShipRoleType.SHIP_ROLE_INVALID
def isValid(self): "returns if this object is valid" if self.getTargetID() == None or self.getAITargetType() == None or EnumsAI.checkValidity(self.getTargetID()) == False: return False universe = fo.getUniverse() if AITargetType.TARGET_FLEET == self.getAITargetType(): fleet = universe.getFleet(self.getTargetID()) if fleet == None: return False return True elif AITargetType.TARGET_SYSTEM == self.getAITargetType(): system = universe.getSystem(self.getTargetID()) if system == None: return False return True elif AITargetType.TARGET_PLANET == self.getAITargetType(): planet = universe.getPlanet(self.getTargetID()) if planet == None: return False return True elif AITargetType.TARGET_BUILDING == self.getAITargetType(): building = universe.getBuilding(self.getTargetID()) if building == None: return False return True elif AITargetType.TARGET_EMPIRE == self.getAITargetType(): empireIDs = fo.AllEmpireIDs() if (empireIDs == None) or (not self.getTargetID() in empireIDs): return False return True return False
def __getAIFleetOrderFromAITarget(self, aiFleetMissionType, aiTarget): fleetAITarget = AITarget.AITarget(EnumsAI.AITargetType.TARGET_FLEET, self.getAITargetID()) orderType = EnumsAI.getFleetOrderTypeForMission(aiFleetMissionType, option=None) result = AIFleetOrder.AIFleetOrder(orderType, fleetAITarget, aiTarget) return result