def addAIPlayer(): players = gameState.getPlayers() for i in range(0,8): if(players[i] == None): aiPlayer = gameState.addPlayer(playerClass=AIPlayer,playerNumber=i,requestHandler=None) break gameState.addAIPlayer(aiPlayer) for player in gameState.getPlayers(): if(player != None): player.dispatchCommand("addPlayer -1 " + str(aiPlayer.playerNumber) + ":" + aiPlayer.userName) return aiPlayer
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])
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()
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 startGame(): with serverLock: global server server.stopAcceptingConnections() for player in gameState.getPlayers(): if (player != None): player.dispatchCommand("startGame -1") player.dispatchCommand("chooseNextUnit -1")
def handle(self): while 1: try: data = self.request.recv(1024) if not data: break else: for player in gameState.getPlayers(): if (player != None): player.dispatchCommand(data) except: break
def finish(self): # SocketServer.StreamRequestHandler.finish(self) if (self.client_address[0] == "94.75.235.221"): return #just testing else: deadPlayerNumber = self.player.playerNumber deadUserName = self.player.userName # if(gameState.getPlayers().count(self.player) > 0):#can be removed first by resetPlayers when server goes down # gameState.removePlayer(self.player.playerNumber) if (server.acceptingConnections): for player in gameState.getPlayers(): if (player != None): player.dispatchCommand("removePlayer -1 " + str(deadPlayerNumber)) for index in range(0, 8): if (deadUserName == "Player " + str(index + 1)): gameState.playerUserNames[index] = False else: for player in gameState.getPlayers(): if (player != None): player.dispatchCommand("disconnectedPlayer -1 " + str(deadPlayerNumber)) print str(self.client_address) + " disconnected."
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()
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()
def addNetworkPlayer(requestHandler): players = gameState.getPlayers() for i in range(0, 8): if (players[i] != None and not players[i].isAI and not hasattr(players[i], "dispatchCommand")): #modifies existing players(from a loaded game) to be network players on the server player = gameState.addPlayer(playerClass=gameLogic.NetworkPlayer, playerNumber=i, requestHandler=requestHandler, playerObj=players[i]) break elif (players[i] == None): player = gameState.addPlayer(playerClass=gameLogic.NetworkPlayer, playerNumber=i, requestHandler=requestHandler) break return player
def setup(self): with server.acceptingConnectionsLock: if (server.acceptingConnections): print 'setup new player: ' + str(self.client_address) if (self.client_address[0] == "94.75.235.221"): print 'just testing' #TODO: send a confirmation back to the server else: SocketServer.StreamRequestHandler.setup(self) self.player = addNetworkPlayer(self) self.player.dispatchCommand("setTeamSize -1 " + str(gameState.getTeamSize())) self.player.dispatchCommand("setPlayerNumber -1 " + str(self.player.playerNumber)) userName = "******" for index in range(0, 8): isUsed = gameState.playerUserNames[index] if (not isUsed): gameState.playerUserNames[index] = True userName = "******" + str(index + 1) break if (gameState.getMapName() != None): self.player.dispatchCommand("setMap -1 " + gameState.getMapName()) seed = time.time() * 256 for player in gameState.getPlayers(): if (player != None): #AI don't have dispatchcommand player.dispatchCommand("seedRNG -1 " + str(seed)) self.player.dispatchCommand( "addPlayer -1 " + str(player.playerNumber) + ":" + player.userName) if (player.playerNumber != self.player.playerNumber): player.dispatchCommand( "addPlayer -1 " + str(self.player.playerNumber) + ":" + self.player.userName) self.player.dispatchCommand("setOwnUserName -1 " + userName) print str(self.client_address) + " setup done." else: #TODO: send command to client indicating game has started or host is no longer accepting connections... print 'host is not accepting connections'
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()