def checkSocket(self): try:#this gives errors all the time... receivedData = self.socket.recv(1024) except: receivedData = '' for command in receivedData.split("|"): if(len(command) > 0): # print command tokens = command.split(" ",2) if(tokens[0] == "chooseNextUnit"): with commandLock: if(len(self.commandLog) > 0): for command in self.commandLog: doCommand(command[0]+"Undo",command[1]) doCommand("chooseNextUnit") if(len(self.commandLog) > 0): for command in self.commandLog: doCommand(command[0]+"Redo",command[1]) self.commandLog = [] else: if(gameState.getPlayerNumber() == SERVER or int(tokens[1]) != gameState.getPlayerNumber() or tokens[0] == "changePlayerNumber"):#skip our own commands, they were executed immediately if(len(tokens) > 2): with commandLock: doCommand(tokens[0],args=tokens[2]) else: with commandLock: doCommand(tokens[0]) else: self.commandLog = self.commandLog[:-1:] #print "commandLog: " + str(self.commandLog) if(gameState.getOwnUserName() != None and gameState.getPlayers()[gameState.getPlayerNumber()] != None and gameState.getPlayers()[gameState.getPlayerNumber()].userName != gameState.getOwnUserName()): gameState.getClient().sendCommand("changeUserName",str(gameState.getPlayerNumber()) + ":" + gameState.getOwnUserName())
def stopClient(): try: gameState.getClient().socket.shutdown(socket.SHUT_RD) except socket.error as e: print e.errno gameState.getClient().socket.close() gameState.setClient(None) gameState.resetPlayers() gameState.resetAIs() gameState.setOwnUserName(None)
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)
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'
def moveNextUnitTowardNode(self,node): nextNode = self.findNextNodeTowardNode(node) if(nextNode != None): gameState.getClient().sendCommand("moveTo",str(nextNode.xPos) + " " + str(nextNode.yPos)) else: gameState.getClient().sendCommand("skip")