示例#1
0
    def analyzeMap(self):
#        AIPlayer.AIAStar.map = gameLogic.mapp(gameLogic.aStarNode,mapName=gameState.getMapName(),ignoreCities=True)
        for row in gameState.getGameMode().map.nodes:
            for node in row:
                if node.playerStartValue != 0:
                    if gameState.getPlayers()[node.playerStartValue-1].team != self.team:
                        self.enemyStartingNodes.append(node)
                    if self.playerNumber == node.playerStartValue - 1:
                        self.startingNode = node
        #calculate distance to nearest cities and rank them:
        for city in gameState.getGameMode().cities:
            if(not(city.node.yPos == self.startingNode.yPos and city.node.xPos == self.startingNode.xPos)):
                self.rankedCities.append((AIPlayer.findDistance(city.node,self.startingNode),city,))
            else:
                self.rankedCities.append((0,city,))
        self.rankedCities.sort(lambda x,y:x[0]-y[0])
        #find nearest enemy starting node
        for startingNode in self.enemyStartingNodes:
            distance = AIPlayer.findDistance(startingNode,self.startingNode)
            if(self.nearestEnemy == None or distance < self.nearestEnemy[0]):
                self.nearestEnemy = (distance,startingNode,)
                self.nearestEnemyStartingNode = startingNode
        #calculate distance to nearest enemy's nearest cities and rank them:
        for city in gameState.getGameMode().cities:
            if(not(city.node.yPos == self.nearestEnemy[1].yPos and city.node.xPos == self.nearestEnemy[1].xPos)):
                self.enemyRankedCities.append((AIPlayer.findDistance(city.node,self.nearestEnemy[1]),city,))
            else:
                self.enemyRankedCities.append((0,city,))
        self.enemyRankedCities.sort(lambda x,y:x[0]-y[0])
示例#2
0
 def startGame():
     gameState.setGameMode(gameModes.playMode)
     gameState.getGameMode().startGame()
     gameState.rendererUpdateQueue.put(rendererUpdates.playSound(cDefines.defines["DARBUKA_HIT_INDEX"]))
     for player in gameState.getPlayers():
         if player != None and player.isAI:
             player.analyzeMap()
示例#3
0
def shutdownServer():
    with serverLock:
        global server
        if (hasattr(gameState.getGameMode(), "playerMissing")):
            gameState.getGameMode().playerMissing = True
        if (server != None):
            server.stopAcceptingConnections()
        serverStarted = False
示例#4
0
 def findNextNodeTowardNode(self,node):
     if(node == gameState.getGameMode().nextUnit.node):#just a little precaution for now, hopefully the finished AI would never run into this situation
         self.moveNextUnitToRandomNode()
     else:
         path = AIPlayer.findPath(node,gameState.getGameMode().nextUnit.node)
         nextNode = gameState.getGameMode().map.nodes[path[0][1]][path[0][0]]
         if(nextNode.unit == None):
             return nextNode
         else:#needed in case the unit is completely surrounded by friendly units.
             return None
示例#5
0
 def moveNextUnitToRandomNode(self):
     rngState = random.getstate()
     eligibleMoveNodes = []
     for neighb in gameState.getGameMode().nextUnit.node.neighbors:
         if(neighb.unit == None):
             if(neighb.tileValue != cDefines.defines['MOUNTAIN_TILE_INDEX'] or (gameState.getGameMode().nextUnit.unitType.canFly)):
                 eligibleMoveNodes.append(neighb)
     if(len(eligibleMoveNodes) > 0):
         moveToNode = random.choice(eligibleMoveNodes)
         gameState.getClient().sendCommand("moveTo",str(moveToNode.xPos) + " " + str(moveToNode.yPos))
     else:
         gameState.getClient().sendCommand("skip")
     random.setstate(rngState)
示例#6
0
 def addPlayer(args):
     tokens = args.split(":")
     playerNumber = int(tokens[0])
     userName = tokens[1]
     gameState.addPlayer(playerNumber=playerNumber,userName=userName,requestHandler=None)
     for player in gameState.getPlayers():
         if(player != None):
             if(gameState.getPlayerNumber() == player.playerNumber):
                 player.isOwnPlayer = True
                 gameState.setPlayerNumber(player.playerNumber)
             else:
                 player.isOwnPlayer = False
     if(hasattr(gameState.getGameMode(),"redrawTeams")):
         gameState.getGameMode().redrawTeams()
示例#7
0
 def changePlayerNumber(args):
     tokens = args.split(":")
     oldNumber = int(tokens[0])
     newNumber = int(tokens[1])
     gameState.movePlayer(oldNumber,newNumber)
     for player in gameState.getPlayers():
         if(player != None):
             if(gameState.getPlayerNumber() == player.playerNumber):
                 player.isOwnPlayer = True
                 gameState.setPlayerNumber(player.playerNumber)
             else:
                 player.isOwnPlayer = False
     if(hasattr(gameState.getGameMode(),"redrawTeams")):
         gameState.getGameMode().redrawTeams()
示例#8
0
 def cancelQueuedThing(args):
     tokens = args.split(" ")
     node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
     index = int(tokens[2])
     if(len(node.unit.buildQueue) > 0):
         cancelledQueuedThing = node.unit.buildQueue.pop(index-1)
         node.unit.cancelledUnits.append((cancelledQueuedThing,index,))
         if(hasattr(cancelledQueuedThing,"unitType")):#unit
             gameState.getGameMode().players[node.unit.player].redWood = gameState.getGameMode().players[node.unit.player].redWood + cancelledQueuedThing.unitType.costRed
             gameState.getGameMode().players[node.unit.player].blueWood = gameState.getGameMode().players[node.unit.player].blueWood + cancelledQueuedThing.unitType.costBlue
         else:#unittype
             gameState.getGameMode().players[node.unit.player].redWood = gameState.getGameMode().players[node.unit.player].redWood + cancelledQueuedThing.researchCostRed
             gameState.getGameMode().players[node.unit.player].blueWood = gameState.getGameMode().players[node.unit.player].blueWood + cancelledQueuedThing.researchCostBlue
     if(gameState.getGameMode().selectedNode == node and hasattr(uiElements.viewer.theViewer,"isSummonerViewer")):
         uiElements.viewer.theViewer.destroy()
         uiElements.viewer.theViewer = uiElements.summonerViewer(node)
示例#9
0
 def findNearestTile(self,tileComparitor):
     searchDistance = 1
     retNode = None
     while(True):
         neighbs = gameState.getGameMode().nextUnit.node.getNeighbors(searchDistance)
         for node in neighbs:
             if(tileComparitor(node) and node.unit == None):
                 return node
         searchDistance += 1
示例#10
0
 def startResearch(args):
     tokens = args.split(" ",2)
     node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
     unitType = gameState.theUnitTypes[tokens[2]]
     node.unit.queueResearch(unitType)
     gameState.getGameMode().players[node.unit.player].redWood = gameState.getGameMode().players[node.unit.player].redWood - unitType.researchCostRed
     gameState.getGameMode().players[node.unit.player].blueWood = gameState.getGameMode().players[node.unit.player].blueWood - unitType.researchCostBlue
     node.unit.isMeditating = True
     if(gameState.getGameMode().selectedNode == node and hasattr(uiElements.viewer.theViewer,"isSummonerViewer")):
         uiElements.viewer.theViewer.destroy()
         uiElements.viewer.theViewer = uiElements.summonerViewer(node)
示例#11
0
 def startSummoning(args):
     tokens = args.split(" ",2)
     node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
     unitType = gameState.theUnitTypes[tokens[2]]
     gameState.getGameMode().players[node.unit.player].redWood = gameState.getGameMode().players[node.unit.player].redWood - (gameState.researchProgress[node.unit.player][unitType][0]*unitType.costRed)
     
     gameState.getGameMode().players[node.unit.player].blueWood = gameState.getGameMode().players[node.unit.player].blueWood - (gameState.researchProgress[node.unit.player][unitType][0]*unitType.costBlue)
     node.unit.queueUnit(gameLogic.unit(unitType,node.unit.player,node))
     node.unit.isMeditating = True
     if(gameState.getGameMode().selectedNode == node and hasattr(uiElements.viewer.theViewer,"isSummonerViewer")):
         uiElements.viewer.theViewer.destroy()
         uiElements.viewer.theViewer = uiElements.summonerViewer(node)
示例#12
0
 def showChat(args):
     gameState.getGameMode().chatDisplay.addText(args)
示例#13
0
 def testConnectSuccess(args):
     #        gameState.resetNetworkPlayers()
     gameState.getGameMode().modal.destroy()
示例#14
0
 def removeRoom(args):
     gameState.getGameMode().roomSelector.removeRoom(args)
示例#15
0
 def addRoom(args):
     gameState.getGameMode().roomSelector.addRoom(args)
示例#16
0
 def roomCount(args):
     tokens = args.split("*")
     if (hasattr(gameState.getGameMode(), "roomCountUpdate")):
         gameState.getGameMode().roomCountUpdate(tokens[0], tokens[1],
                                                 tokens[2])
示例#17
0
 def chat(args):
     if(hasattr(gameState.getGameMode(),"chatDisplay")):
         gameState.getGameMode().chatDisplay.addText(args)
示例#18
0
    def takeTurn(self):
        nextUnit = gameState.getGameMode().nextUnit
        if(nextUnit.aiData == None):
            nextUnit.aiData = AIUnitData()
        if(not nextUnit.aiData.counted):
            nextUnit.aiData.counted = True
            if(nextUnit.unitType.name == "summoner"):
                pass
            elif(nextUnit.unitType.name == "gatherer"):
                self.workerCount = self.workerCount + 1
            else:
                self.nonWorkerCount = self.nonWorkerCount + 1
        if(nextUnit.unitType.name == "summoner"):
            print 'summoners turn'
            if(nextUnit.node.city != None):
                self.moveNextUnitToRandomNode()
            else:
                if(self.nonWorkerCount > self.workerCount):
                    buildUnitType = gameState.theUnitTypes["gatherer"]
                else:
                    buildUnitType = self.chooseRandomBuildableUnitType()                
                print 'want to build ' + buildUnitType.name
                print self.redWood
                print self.blueWood
                gameState.researchProgress[self.playerNumber]
                if(self.redWood >= (gameState.researchProgress[self.playerNumber][buildUnitType][0]*buildUnitType.costRed) and self.blueWood >= (gameState.researchProgress[self.playerNumber][buildUnitType][0]*buildUnitType.costBlue)):
                    print 'building'
                    gameState.getClient().sendCommand("startSummoning",str(nextUnit.node.xPos) + " " + str(nextUnit.node.yPos) + " " + buildUnitType.name)
                else:
                    print 'skipping build, not enough resources'
                    #Should the summoner move somewhere? Where?
                    gameState.getClient().sendCommand("skip")

        elif(nextUnit.unitType.name == "gatherer"):
            print 'gatherers turn'
            if(nextUnit.aiData.mode == None):
                if(self.workerCount%10 == 0):
                    nextUnit.aiData.mode = MODES.RESEARCH_MODE
                elif(self.workerCount%5 == 0):
                    nextUnit.aiData.mode = MODES.GATHER_BLUE_MODE
                else:
                    nextUnit.aiData.mode = MODES.GATHER_MODE
            if(nextUnit.aiData.mode == MODES.GATHER_MODE):
                comparitorFunc = lambda node:node.tileValue == cDefines.defines['RED_FOREST_TILE_INDEX']
            elif(nextUnit.aiData.mode == MODES.GATHER_BLUE_MODE):
                comparitorFunc = lambda node:node.tileValue == cDefines.defines['BLUE_FOREST_TILE_INDEX']
            else:
                comparitorFunc = lambda node:node.city != None
            print comparitorFunc(nextUnit.node)
            if(comparitorFunc(nextUnit.node)):
                gameState.getClient().sendCommand("startMeditating",str(nextUnit.node.xPos) + " " + str(nextUnit.node.yPos))
            else:
                if(nextUnit.aiData.gotoNode == None or nextUnit.aiData.gotoNode.unit != None):
                    nextUnit.aiData.gotoNode = self.findNearestTile(comparitorFunc)
                self.moveNextUnitTowardNode(nextUnit.aiData.gotoNode)
#             if(nextUnit.aiData.mode == MODES.GATHER_MODE):
#                 if(nextUnit.node.tileValue == cDefines.defines['RED_FOREST_TILE_INDEX']):
#                     gameState.getClient().sendCommand("startMeditating",str(nextUnit.node.xPos) + " " + str(nextUnit.node.yPos))
#                 else:
#                     if(nextUnit.aiData.gotoNode == None or nextUnit.aiData.gotoNode.unit != None):
#                         nextUnit.aiData.gotoNode = self.findNearestRedForest()
#                     self.moveNextUnitTowardNode(nextUnit.aiData.gotoNode)
#             else:#MODES.RESEARCH_MODE
#                 print 'unimplemented!!!'
#                 if(nextUnit.node.city != None):
#                     gameState.getClient().sendCommand("startMeditating",str(nextUnit.node.xPos) + " " + str(nextUnit.node.yPos))
#                 else:
#                     if(nextUnit.aiData.gotoNode == None or (nextUnit.aiData.gotoNode.unit != None and nextUnit.aiData.gotoNode.unit.isMeditating)):
#                         nextUnit.aiData.gotoNode = self.findNearestCity()
#                     self.moveNextUnitTowardNode(nextUnit.aiData.gotoNode)
        else:
            print 'units turn'
            theGroup = self.getGroup(nextUnit)
#            print theGroup
#            nextNode = self.findNextNodeTowardNode(self.nearestEnemyStartingNode)
            confidence = self.getGroupConfidence(theGroup,HOSTILITY_RANGE)
            print "conf: " + str(confidence)
            if(confidence > 0.0):
                print 'attack!'
                attackableUnit = None
                for node in nextUnit.node.getNeighbors(nextUnit.unitType.range):
                    if(node.unit != None and node.unit.team != self.team):
                        if(attackableUnit == None):
                            attackableUnit = node.unit
                        elif(attackableUnit.health > node.unit):
                            attackableUnit = node.unit
                if(attackableUnit == None):                    
                    for ranj in range(0,HOSTILITY_RANGE):
                        for node in nextUnit.node.getNeighbors(ranj):
                            if(node.unit != None and node.unit.team != self.team):
                                if(attackableUnit == None):
                                    attackableUnit = node.unit
                                elif(attackableUnit.health > node.unit):
                                    attackableUnit = node.unit
                if(attackableUnit == None):
                    print "no attackable unit found, moving forward"
                    self.moveNextUnitTowardNode(self.nearestEnemyStartingNode)
                else:
                    print 'attackable unit found'
                    if(nextUnit.node.findDistance(attackableUnit.node,gameState.getGameMode().map.polarity) <= nextUnit.unitType.range):
                        gameState.getClient().sendCommand("attackTo",str(attackableUnit.node.xPos) + " " + str(attackableUnit.node.yPos))
                    else: 
                        print "no unit in range"
                        nextNode = self.findNextNodeTowardNode(node)
                        if(isNodeDangerous(nextNode,group)):
                            print "node is dangerous, skipping"
                            gameState.getClient().sendCommand("skip")
                        else:
                            print "moving forward"
                            self.moveNextUnitTowardNode(attackableUnit.node)
            else:
                print "run away!"
                eligibleRunToNodes = []
                for node in nextUnit.node.neighbors:
                    nodeIsEligible = True
                    if(node.unit != None):
                         nodeIsEligible = False
                    else:
                        for neighb in node.neighbors:
                            if neighb.unit != None and neighb.unit.team != self.team:
                                nodeIsEligible = False
                                break
                    if(nodeIsEligible):
                        eligibleRunToNodes.append(node)
                if(len(eligibleRunToNodes) > 0):
                    moveToNode = random.choice(eligibleRunToNodes)
                    gameState.getClient().sendCommand("moveTo",str(moveToNode.xPos) + " " + str(moveToNode.yPos))
                else:
                    #no where to run to, hit something
                    for node in nextUnit.node.getNeighbors(nextUnit.unitType.range):
                        if(node.unit != None and node.unit.team != self.team):
                            if(attackableUnit == None):
                                attackableUnit = node.unit
                            elif(attackableUnit.health > node.unit):
                                attackableUnit = node.unit
                    gameState.getClient().sendCommand("attackTo",str(attackableUnit.node.xPos) + " " + str(attackableUnit.node.yPos))                    
#                self.moveNextUnitToRandomNode()
        gameState.getClient().sendCommand("chooseNextUnit")
        print 'turn done'
示例#19
0
 def startMeditatingRedo(args):
     tokens = args.split(" ")
     node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
     node.unit.isMeditating = True
示例#20
0
 def disconnectedPlayer(playerNumber):
     if(hasattr(gameState.getGameMode(),"playerMissing")):
         gameState.getGameMode().playerMissing = True
         gameState.getGameMode().missingPlayers.append(int(playerNumber))
         uiElements.playerDisconnectedModal()
示例#21
0
 def removePlayer(playerNumber):
     gameState.removePlayer(int(playerNumber))
     if(hasattr(gameState.getGameMode(),"redrawTeams")):
         gameState.getGameMode().redrawTeams()
示例#22
0
 def setTeamSize(teamSize):
     gameState.setTeamSize(int(teamSize))
     if(hasattr(gameState.getGameMode(),"redrawTeams")):
         gameState.getGameMode().redrawTeams()
示例#23
0
 def changeUserName(args):
     tokens = args.split(":")
     oldUserName = gameState.getPlayers()[int(tokens[0])].userName
     gameState.changeUserName(int(tokens[0]),tokens[1])
     if(hasattr(gameState.getGameMode(),"redrawTeams")):
         gameState.getGameMode().redrawTeams()
示例#24
0
    def setOwnUserName(userName):
        if(gameState.getOwnUserName() == None):#"Player X" from server, prefer real username, which would already be set
            gameState.setOwnUserName(userName)
#        
        if(hasattr(gameState.getGameMode(),"redrawTeams")):
            gameState.getGameMode().redrawTeams()
示例#25
0
 def cancelQueuedThingUndo(args):
     tokens = args.split(" ")
     index = int(tokens[2])
     node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
     cancelledThing,index = node.unit.cancelledUnits.pop()
     node.unit.buildQueue.insert(index-1,cancelledThing)
示例#26
0
 def startResearchRedo(args):
     tokens = args.split(" ",2)
     node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
     unitType = gameState.theUnitTypes[tokens[2]]
     node.unit.queueResearch(unitType)
示例#27
0
 def startSummoningRedo(args):
     tokens = args.split(" ",2)
     node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
     unitType = gameState.theUnitTypes[tokens[2]]
     if(node.unit != None and node.unit.unitType.name == "summoner"):
         node.unit.queueUnit(gameLogic.unit(unitType,node.unit.player,node))
示例#28
0
 def removePlayer(args):
     if (hasattr(gameState.getGameMode(), "removePlayer")):
         gameState.getGameMode().removePlayer(args)
示例#29
0
    def setMap(mapName):
#        gameState.setMapName(mapName)
        if(hasattr(gameState.getGameMode(),"setMap")):
            gameState.getGameMode().setMap(mapName)
        else:
            gameState.setMapName(mapName)
示例#30
0
 def cancelQueuedThingRedo(args):
     tokens = args.split(" ")
     index = int(tokens[2])
     node = gameState.getGameMode().map.nodes[int(tokens[1])][int(tokens[0])]
     if(len(node.unit.buildQueue) > 0):
         node.unit.buildQueue.pop(index-1)