Page 1 of 1

Making python script for teamkill score

Posted: 2017-05-19 01:47
by MaxP
I'm using the python scoring file from aix2 to give to the players some bonuses for destroying enemy vehicles, that defined in separate file.

I'm trying to create the one that will count friendly vehicles teamkills too (vehicle_tkbonus.conf file)

Here is the changed lines from the scoringCommon.py:

Code: Select all

VEHICLE_BONUS_CONF_FILE = "mods/aix2ex_mm/python/game/vehicle_bonus.conf"
g_vehicle_bonus = []

[color=DarkRed]VEHICLE_TKBONUS_CONF_FILE = "mods/aix2ex_mm/python/game/vehicle_tkbonus.conf"
g_vehicle_tkbonus = [] [/color]

def init():

	global g_vehicle_bonus

	fread = open(VEHICLE_BONUS_CONF_FILE)
	readlines = fread.readlines()
	fread.close()
	
	for line in readlines:
		list = line.split(',')
		g_vehicle_bonus.append(list)

	[color=DarkRed]global g_vehicle_tkbonus

	fread = open(VEHICLE_TKBONUS_CONF_FILE)
	readlines = fread.readlines()
	fread.close()

	
	for line in readlines:
		list = line.split(',')
		g_vehicle_tkbonus.append(list)[/color]
And

Code: Select all

def onPlayerKilled(victim, attacker, weapon, assists, object):	

	killedByEmptyVehicle = False
	countAssists = False
	
	# killed by unknown, no score
	if attacker == None:
		
		# check if killed by vehicle in motion
		if weapon == None and object != None:
			if hasattr(object, 'lastDrivingPlayerIndex'):
				attacker = bf2.playerManager.getPlayerByIndex(object.lastDrivingPlayerIndex)
				killedByEmptyVehicle = True


		if attacker == None:
			if g_debug: print "No attacker found"
			pass

			victimVehicle = victim.getVehicle()
	# killed by remote controlled vehicle, no score awarded in this game
	if object and object.isPlayerControlObject and object.getIsRemoteControlled():		
		pass
		
	# no attacker, killed by object
	elif attacker == None:
		pass
		
	# killed by self
	elif attacker == victim:

		# no suicides from own wreck
		if killedByEmptyVehicle and object.getIsWreck():
			return

		attacker.score.suicides += 1
		if not attacker.isAIPlayer():
			addScore(attacker, SCORE_SUICIDE, RPL)
		
	# killed by own team
	elif attacker.getTeam() == victim.getTeam():

		# no teamkills from wrecks
		if object != None and object.getIsWreck():
			return
			
		# no teamkills from artillery
		if weapon:
			attackerVehicle = bf2.objectManager.getRootParent(weapon)
			if attackerVehicle.isPlayerControlObject and attackerVehicle.getIsRemoteControlled():
				return

#HF Start
	if weapon == None and object != None:
			victimVehicle = victim.getVehicle()
			victimRootVehicle = bf2.objectManager.getRootParent(victimVehicle)
			victimVehicleType = getVehicleType(victimRootVehicle.templateName)
			attackerVehicle = attacker.getVehicle()
			attackerRootVehicle = bf2.objectManager.getRootParent(attackerVehicle)
			attackerVehicleType = getVehicleType(attackerRootVehicle.templateName)
			[color=DarkRed]VehicleTKName = victimRootVehicle.templateName[/color]
			if attackerVehicleType == VEHICLE_TYPE_AVIATOR and victimVehicleType == VEHICLE_TYPE_SOLDIER:
				return


#HF Stop
		
		[color=DarkRed]for vehicle_tkbonus in g_vehicle_tkbonus:
			
			if vehicle_tkbonus[0].lower() == str(VehicleTKName).lower():
				addScore(attacker, int(vehicle_tkbonus[1]), RPL)
				if not attacker.isAIPlayer():
					host.rcon_invoke("game.sayall \"" + attacker.getName() + " destroyed " + "friendly vehicle " + str(SCORE_TEAMKILL + int(vehicle_tkbonus[1])) + "]" + "\"")				
			countAssists = True[/color]

	# killed by enemy
	else:
		attacker.score.kills += 1
		addScore(attacker, SCORE_KILL, SKILL)
		vehicle = victim.getVehicle()
		victimVehicle = bf2.objectManager.getRootParent( vehicle )
		VehicleName = victimVehicle.templateName

		for vehicle_bonus in g_vehicle_bonus:




			if vehicle_bonus[0].lower() == str(VehicleName).lower():
				addScore(attacker, int(vehicle_bonus[1]), SKILL)
				if not attacker.isAIPlayer():
					host.rcon_invoke("game.sayall \"" + attacker.getName() + " destroyed " + "an enemy vehicle" + " [+" + str(SCORE_KILL + int(vehicle_bonus[1])) + "]" + "\"")

		countAssists = True

				# headshot/range/speed scoring + message
		data = createdata(victim, attacker, weapon)
		bf2.Timer(delayedplayerkilled, 0.1, 1, data)

	# kill assist
	if countAssists and victim:

		for a in assists:
			assister = a[0]
			assistType = a[1]
			
			if assister.getTeam() != victim.getTeam():
			
				# passenger
				if assistType == 0:
					assister.score.passengerAssists += 1
					addScore(assister, SCORE_KILLASSIST_PASSENGER, RPL)
				# targeter
				elif assistType == 1:
					assister.score.targetAssists += 1
					addScore(assister, SCORE_KILLASSIST_TARGETER, RPL)
				# damage
				elif assistType == 2:
					assister.score.damageAssists += 1
					addScore(assister, SCORE_KILLASSIST_DAMAGE, RPL)
				# driver passenger
				elif assistType == 3:
					assister.score.driverAssists += 1
					addScore(assister, SCORE_KILLASSIST_DRIVER, RPL)
				else:
					# unknown kill type
					pass
I've changed the lines marked with red, but I've no experience, and got strange result: the TK "bonus" works only if the friendly vehicle explodes after taking the critical damage inflicted by me.

Can anybody help me to create the correct script?

Re: Making python script for teamkill score

Posted: 2017-05-19 13:36
by AlonTavor
Don't have time right now to go over it, ill check it through later.

Re: Making python script for teamkill score

Posted: 2017-05-20 12:29
by MaxP
Thanks. I think I've done it by intuitive way, with remembering my institute programming course ))

Code: Select all

def onPlayerKilled(victim, attacker, weapon, assists, object):	

	killedByEmptyVehicle = False
	countAssists = False
	
	# killed by unknown, no score
	if attacker == None:
		
		# check if killed by vehicle in motion
		if weapon == None and object != None:
			if hasattr(object, 'lastDrivingPlayerIndex'):
				attacker = bf2.playerManager.getPlayerByIndex(object.lastDrivingPlayerIndex)
				killedByEmptyVehicle = True


		if attacker == None:
			if g_debug: print "No attacker found"
			pass

			victimVehicle = victim.getVehicle()
	# killed by remote controlled vehicle, no score awarded in this game
	if object and object.isPlayerControlObject and object.getIsRemoteControlled():		
		pass
		
	# no attacker, killed by object
	elif attacker == None:
		pass
		
	# killed by self
	elif attacker == victim:

		# no suicides from own wreck
		if killedByEmptyVehicle and object.getIsWreck():
			return

		attacker.score.suicides += 1
		if not attacker.isAIPlayer():
			addScore(attacker, SCORE_SUICIDE, RPL)
		
	# killed by own team
[color=DarkRed]	elif attacker.getTeam() == victim.getTeam():
		for vehicle_tkbonus in g_vehicle_tkbonus:
			vehicle = victim.getVehicle()
			victimVehicle = bf2.objectManager.getRootParent( vehicle )
			VehicleTKName = victimVehicle.templateName
			if vehicle_tkbonus[0].lower() == str(VehicleTKName).lower():
				addScore(attacker, int(vehicle_tkbonus[1]), RPL)
				if not attacker.isAIPlayer():
					host.rcon_invoke("game.sayall \"" + attacker.getName() + " destroyed " + "friendly vehicle " + str(SCORE_TEAMKILL + int(vehicle_tkbonus[1])) + "]" + "\"")	[/color]			

		# no teamkills from wrecks
		if object != None and object.getIsWreck():
			return
			
		# no teamkills from artillery
		if weapon:
			attackerVehicle = bf2.objectManager.getRootParent(weapon)
			if attackerVehicle.isPlayerControlObject and attackerVehicle.getIsRemoteControlled():
				return

#HF Start
		if weapon == None and object != None:

			victimVehicle = victim.getVehicle()
			victimRootVehicle = bf2.objectManager.getRootParent(victimVehicle)
			victimVehicleType = getVehicleType(victimRootVehicle.templateName)
			attackerVehicle = attacker.getVehicle()
			attackerRootVehicle = bf2.objectManager.getRootParent(attackerVehicle)
			attackerVehicleType = getVehicleType(attackerRootVehicle.templateName)
			if attackerVehicleType == VEHICLE_TYPE_AVIATOR and victimVehicleType == VEHICLE_TYPE_SOLDIER:
				return
#HF Stop
		attacker.score.TKs += 1
		addScore(attacker, SCORE_TEAMKILL, RPL)
				
		countAssists = True
But now the game count -6 for teamkill, instead of -2.
Maybe I forgot the "return" operator somewhere?

And another idea. I want the victim to get command points when being teamkilled.
As I understand, the script must go into the same section -

Code: Select all

attacker.getTeam() == victim.getTeam()
How to recreate this?

Re: Making python script for teamkill score

Posted: 2017-05-20 12:31
by AlonTavor
Shouldn't everything from lines 54 to 74 be one tab to the right?
nvm posted before i saw your new post




Well you should return after the addScore in lines 47 if you don't want it to remove score as "normal teamkill" as well (there's addscore in 74)

Re: Making python script for teamkill score

Posted: 2017-05-20 12:37
by AlonTavor
MaxP wrote: And another idea. I want the victim to get command points when being teamkilled.
As I understand, the script must go into the same section -

Code: Select all

attacker.getTeam() == victim.getTeam()
How to recreate this?
https://projects.uturista.pt/bf2tech/in ... layerScore

victim.score.rplScore
or through your "addScore", Can't see what it does though.

Re: Making python script for teamkill score

Posted: 2017-05-20 21:29
by MaxP
Thank, I've done it.
Here is my implemetation for coop game for those who interested (for stock BF2 mods only!).

The vehicle_tkbonus.conf and vehicle_bonus.conf has the following synthax for each string:
[vehicle template name],[score bonus],
I.e.
ger_jet_tornadogr4_mw1,-4, (for tk)
or
ger_jet_tornadogr4_mw1,4, (for score)

The victim gets vehicle bonus score + 4 when being teamkilled in vehicle, and +4 when being teamkilled on-foot.
Just some compensation ))
I've found it useful.