#WaW Rally point code V2 by peppervieze inspired on the early rally point version. #Original version largely based on the 21CW code. #Some random stuff kept in comments for easy acces: # # print '[wawRallyPointV2] : ' # from game.gameplayPlugin import base from spawners import createSpawner from bf2.stats.constants import * import game.utilities import bf2 import bf2.PlayerManager import host import math import spawners import bf2.Timer #These vars tweak the rally point system. minDistanceFromCP = 50 #min distance from default: 50... [35M on caumont camp 21] minPlayersClose = 1 #min number of squad members close default: 2 minplayersInSquad = 1 #min number of players in squad default: 3 maxDistanceFromSL = 25 #max distance of the squad members from the SL when creating a rallypoint. default: 25 maxSpawnDistanceRP = 8 #Change this if undesired warp effects are encountered. default: 8 rallyInterval = 10 #Time after which a new rally can be created in seconds default: 90 creationDelay = 2 #Timer delay, so that the object can be placed in the world. #constants GAME_PLAYING = 1 GAME_END = 2 GAME_PRE_GAME = 3 G_DEBUG = 1 #The 2 dimentional array carries in its first index position the team, and in the second one the specific point(e.g. rallypoint etc). g_rp = [[None]*9,[None]*9] #temporay filler for the rallypoint array g_mainbases = [[],[]] #carries all the mainbases. mainbase condition equals team not zero and uncapturable g_cp = [] #Carries all the control points def init(): #Register all the event handlers here #host.registerGameStatusHandler(onGameStatusChanged) #host.registerHandler('RemoteCommand', onRemoteCommand) #host.registerHandler('PlayerSpawn', onPlayerSpawn) #host.registerHandler('PlayerChangedSquad', onSquadChange) #host.registerHandler('VehicleDestroyed', onVehicleDestroyed) print '[wawRallyPointV2] registering event handlers' def onGameStatusChanged(status): #If the game isnt playing dont gather anything yet, to avoid queezy bugs. if status == GAME_END: return if status != GAME_PLAYING: return #gather all the flags and the main bases allControlPoints = bf2.objectManager.getObjectsOfType('dice.hfe.world.ObjectTemplate.ControlPoint') #Team 0 = grey, 1 = axis, 2 = allies for cp in allControlPoints: cpTeam = cp.cp_getParam('team') if cp.cp_getParam('unableToChangeTeam'): if cpTeam != 0: g_mainbases[cpTeam-1].append(cp) else: g_cp.append(cp) #Note that clearing a array[] by doing so: array = [] whilst its already defined outside the function will make it #local instead of document wide. I need to find a good way of clearing this. #Debug stuf if G_DEBUG: print '[wawRallyPointV2] g_cp : ', g_cp print '[wawRallyPointV2] g_rp : ', g_rp print '[wawRallyPointV2] g_mainbases : ', g_mainbases print '[wawRallyPointV2] for each g_cp:' for cp in g_cp: print 'CP Name: ', cp.templateName print '[wawRallyPointV2] for each g_mainbases:' for i in range(2): for mb in g_mainbases[i-1]: print 'MB Name: ', mb.templateName def createRallyPoint(player): #first get all the data needed playerTeamId = player.getTeam() playerTeam = makeStringTeamName(playerTeamId) playerSquadId = player.getSquadId() position = player.getVehicle().getPosition() templateName = 'rallypoint_' + playerTeam + '_' + str(playerSquadId) deleteRallyPoint(templateName, playerTeamId) createSpawner( templateName + '_spawner', (position[0], position[1]-1.0, position[2]), None, True, {'team': playerTeamId } ) #This timer makes sure we dont get the object to early!! tempTimer = bf2.Timer(tempTimeFire, creationDelay, 1, (templateName, playerTeamId, playerSquadId) ) if G_DEBUG: print '[wawRallyPointV2] done adding stuff to g_rp', g_rp print '[wawRallyPointV2] g_rp[team-1][playerSquadId-1]: ', g_rp[playerTeamId-1][playerSquadId-1] print '[wawRallyPointV2] g_rp[team-1][playerSquadId-1].creationTime: ', g_rp[playerTeamId-1][playerSquadId-1].creationTime def tempTimeFire(data): rallyPoints = bf2.objectManager.getObjectsOfTemplate(data[0]) for rally in rallyPoints: if rally.getPosition() != (0,0,0): try: g_rp[data[1]-1][data[2]-1] = rally g_rp[data[1]-1][data[2]-1].position = tuple( rally.getPosition() ) g_rp[data[1]-1][data[2]-1].creationTime = int( currentTime()-creationDelay ) break except Exception, e: print 'Failed to add to array: ',e pass def deleteRallyPoint(templateName, playerTeam): #deleting of the rallypoint. There are octually 3 objects that needs to be deleted. objs = host.rcon_invoke('object.listObjectsOfTemplate ' + templateName) objs = objs.split() for i in range( (len(objs)/10) ): id = (i * 10) + 3 host.rcon_invoke('object.delete id' + str(objs[id]) ) try: for rp in g_rp[playerTeam-1]: if rp is not None: if rp.templateName == templateName: rp.position = None rp = None except Exception, e: print '[wawRallyPoint V2] failed to clear g_rp: ', e pass def onVehicleDestroyed(vehicleObject, attackerObject): #DEBUG BIT msg = str( 'Destroyed: ' + str(vehicleObject.templateName) + ' by ' + str(attackerObject) ) sendMessage(1, msg) sendMessage(2, msg) #END DEBUG BIT attackingTeam = attackerObject.getTeam() destroyedVehicleName = vehicleObject.templateName.lower() if isRallyPoint(destroyedVehicleName, attackingTeam): try: index = int( destroyedVehicleName[( len(destroyedVehicleName)-1 )] ) g_rp[attackingTeam-1][index-1].position = None g_rp[attackingTeam-1][index-1] = None deleteRallyPoint(destroyedVehicleName ,attackingTeam) except Exception, e: print 'Error line 155: ', e pass def isRallyPoint(objectName, team): #for rallypoint in rallypoints(team for rp in g_rp[team-1]: if rp.templateName == objectName: return True def onPlayerSpawn(playerObject, soldierObject): playerTeamId = playerObject.getTeam() playerTeam = makeStringTeamName(playerTeamId) playerSquad = playerObject.getSquadId() #Player squad is zero, means no squad playerRp = str('rallypoint_' + playerTeam + '_' + str(playerSquad)) playerPosition = playerObject.getDefaultVehicle().getPosition() #loop through all the RP's of own team for rp in g_rp[playerTeamId-1]: if rp is not None: distance = math.fabs( getSquadSquareHorizontalDistance( (rp.position), (playerPosition) ) ) #if its outside the possible spawn distance go on to the next RP(its not the one we spawned on) if distance > maxSpawnDistanceRP: continue #if it isnt our RP this we means we spawned on the wrong one. First attempt warp to RP.Then to Base,then kill.. if rp.templateName != playerRp: try: playerObject.getDefaultVehicle().setPosition(g_rp[playerTeamId-1][playerSquad-1].position) except Exception, e: try: playerObject.getDefaultVehicle().setPosition(g_mainbases[playerTeamId-1][0].getPosition()) break except Exception, e: playerObject.getVehicle().setDamage(0) pass break pass break #checks on squad change def onSquadChange(player, oldSquadId, newSquadId): #If the players oldsquad was nothing we dont need to check if oldSquadId == 0: return playerTeam = player.getTeam() playerTeamStr = makeStringTeamName(playerTeam) oldSquad = getPlayersInSquad(playerTeam, oldSquadId) #If a players old squad is below the minium delete the RP. if len(oldSquad) < minplayersInSquad: templateName = 'rallypoint_' + playerTeamStr + '_' + str(oldSquadId) deleteRallyPoint(templateName, playerTeam) def canSpawnRally(player): #very naught in my eyes, for not being a SL. works playerTeam = player.getTeam() playerSquadId = player.getSquadId() if not player.isSquadLeader(): sendMessage(playerTeam, 'player is not SL, cannot spawn rallypoint') return False #He should be alive! works if not player.isAlive(): sendMessage(playerTeam, 'cannot create rallypoint. Player is not ALIVE!') return False #check to see if the SL is in a vehicle works rootStart = player.getDefaultVehicle() rootNow = getRootParent(player.getVehicle()) if rootStart.templateName is not rootNow.templateName: sendMessage(playerTeam, ('squad ' + str(playerSquadId) + ' cannot create a RP from a vehicle ') ) return False #gather stuff once to save processing time playersInSquad = getPlayersInSquad(playerTeam, playerSquadId) playerPosition = player.getVehicle().getPosition() if len(playersInSquad) < minplayersInSquad: sendMessage(playerTeam, ('squad ' + str(playerSquadId) + ' does not have enough players in squad to create rallypoint') ) return False #checks to see if enough players are close near the SL #TODO: check to see if this works! Works if not playersInSquadClose(playerPosition, playersInSquad): sendMessage(playerTeam, ('squad ' + str(playerSquadId) + ' does not have enough players close to SL to create RP')) return False #Check to see last creation time of cp. works #TODO: make a property on the rallypoint that stores the creation time. DONE! ^_^ try: if g_rp[playerTeam-1][playerSquadId-1].creationTime is not None: if (currentTime() - g_rp[playerTeam-1][playerSquadId-1].creationTime) < rallyInterval: sendMessage(playerTeam, ('squad ' + str(playerSquadId) + ' cannot create a rallypoint yet: ' + str(currentTime() - g_rp[playerTeam-1][playerSquadId-1].creationTime) + ' seconds have passed since last RP') ) return False except Exception, e: pass #check distance from cp's #TODO: works for cp in g_cp: try: squareDist = getSquadSquareHorizontalDistance(playerPosition, cp.getPosition()) if squareDist < minDistanceFromCP: sendMessage(playerTeam, ('squad ' + str(playerSquadId) + ' to close to a control point to spawn rallypoint') ) return False except Exception, e: pass #All checks out. lets make a rally point! return True #gather all players in squad def getPlayersInSquad(team, squadId): squad = [] for p in bf2.playerManager.getPlayers(): if p.getTeam() == team and p.getSquadId() == squadId: squad.append(p) return squad #check the players in he squad within the SL's range def playersInSquadClose(playerPosition, players): try: close = [] for p in players: if p.isAlive(): if p.isSquadLeader(): close.append(p) else: pPosition = p.getDefaultVehicle().getPosition() squareDist = getSquadSquareHorizontalDistance(playerPosition, pPosition) if squareDist < maxDistanceFromSL: close.append(p) if len(close) < minPlayersClose: return False else: return True except Exception, e: print 'error on checking playersInSquadClose: ', e pass #Receiving the remoteCommand that fires the control system. def onRemoteCommand(playerId, cmd): if cmd != 'rally': return #playerId = -1 on lan. if playerId == -1: playerId = 255 #check rallypoint conditions player = bf2.playerManager.getPlayerByIndex(playerId) #new remote command code. Should be player based, although this will require some testing. try: if int(currentTime()) - player.RemoteCommandSend <= 2: return except Exception, e: pass player.RemoteCommandSend = int(currentTime()) if not canSpawnRally(player): return else: #if allowed create rallypoint createRallyPoint(player) #format teamId into a actual human readable name def makeStringTeamName(teamId): if teamId == 1: playerTeamName = 'axis' return str(playerTeamName) if teamId == 2: playerTeamName = 'allies' return str(playerTeamName) #sends a teamwide message in the ticker at the top of the game window def sendMessage(team, msg): host.rcon_invoke('game.sayTeam ' + str(team) + ' "' + msg + '"') #get the square horizontal distance TODO: Might need to get the abs(absolute( >0)) value for this to work. def getSquadSquareHorizontalDistance(obj1, obj2): #print 'Square dist: ', obj1, ' - ',obj2 dx = obj1[0] - obj2[0] dz = obj1[2] - obj2[2] returnVal = (dx * dx) + (dz * dz) returnVal = math.sqrt(returnVal) print 'DISTANCE SQUARE ', returnVal return returnVal #gets the current time and attaches it to the rallypoint variable. def currentTime(): return int( host.timer_getWallTime()) class wawRallyPoint(base): init() def __init__(self, rally_battle = 0): return def round_start(self, hooker): print 'Round start thingy' ''' -------------------- COPY AND PASTE AREA Vehicle has been destroyed Destroyed: rallypoint_allies_1 by [testScript] received message: Destroyed: rallypoint_allies_1 by from Id 255 in ServerTeamMessage -- flags 1 [DynamicHeader.py] received textMessage: Destroyed: rallypoint_allies_1 by placeholder [testScript] received message: Destroyed: rallypoint_allies_1 by from Id 255 in ServerTeamMessage -- flags 2 [DynamicHeader.py] received textMessage: Destroyed: rallypoint_allies_1 by placeholder objectName rallypoint_allies_1 team 2 rp: RP has been destroyed: rallypoint_allies_1 A rallypoint is destroyed: rallypoint_allies_1 DH ---------------- OLD CODE OLD CODE OLD CODE OLD CODE OLD CODE OLD CODE OLD CODE OLD CODE playerTeamId = playerObject.getTeam() playerTeam = makeStringTeamName(playerTeamId) playerSquadId = playerObject.getSquadId() templateName = 'rallypoint_' + playerTeam + '_' + str(playerSquadId) playerRPs = bf2.objectManager.getObjectsOfTemplate(templateName) playerRP = None for newRp in playerRPs: if newRp.getPosition() != (0,0,0): playerRP = newRp for rp in g_rp[playerTeamId-1]: if rp is not None: print '[line152]check RP ', rp.templateNamesa try: if getSquadSquareHorizontalDistance(rp.position, playerObject.getVehicle().getPosition()) < maxDistanceFromSL: print '[line154]within distance' if rp.templateName != str('rallypoint_' + playerTeam + '_' + str(playerSquadId)): # CHANGE TO != DEBUGGG print '[line156]not his own RP' try: if playerRP is not None: playerObject.getDefaultVehicle().setPosition(playerRP.getPosition()) else: playerObject.getDefaultVehicle().setPosition(g_mainbases[playerTeamId-1][0].getPosition() ) except Exception, e: print '[line163] error: ',e try: playerObject.getDefaultVehicle().setPosition(g_mainbases[playerTeamId-1][0].getPosition() ) except Exception, e: print '[line 167] error: ', e playerObject.getVehicle().setDamage(0) pass else: print '[line171] players own RP' return except Exception, e: print 'somethign is wrong: ', e pass try: if playerRP is not None: playerObject.getDefaultVehicle().setPosition(playerRP.getPosition()) else: playerObject.getDefaultVehicle().setPosition(g_mainbases[playerTeamId-1][0].getPosition() ) except Exception, e: print 'Killing the bastard ' playerObject.getVehicle().setDamage(0) pass ''' ''' CURR SPAWNSYSTEM 22-06-2010 playerTeamId = playerObject.getTeam() playerTeam = makeStringTeamName(playerTeamId) playerSquadId = playerObject.getSquadId() playerPosition = playerObject.getDefaultVehicle().getPosition() correctTemplateName = str('rallypoint_' + str(playerTeam) + '_' + str(playerSquadId) ) for i in range(10): for rallyPoint in bf2.objectManager.getObjectsOfTemplate('rallypoint_' + playerTeam + '_' + str(i)): try: if not rallyPoint.isValid() or rallyPoint.getPosition() == (0,0,0): continue except: continue # Check to see if it's a valid rally point if getSquadSquareHorizontalDistance(rallyPoint.getPosition(), playerPosition) < maxSpawnDistanceRP: templateName = rallyPoint.templateName if correctTemplateName == templateName or 'rallypoint_' + playerTeam + '_0' == templateName: print "Spawned at correct rally point" returnw else: print "Spawned at incorrect rally point" wrongRally = True if not wrongRally: return try: playerObject.getDefaultVehicle().setPosition( g_mainbases[playerTeamId-1][0].getPosition() ) except Exception, e: playerObject.getVehicle().setDamage(0) '''