Page 1 of 1

Distance between two points ?

Posted: Tue Aug 14, 2012 4:53 pm
by Tuck
I would like to calculect the distance between 2 players on player_death event to register how far the attacker was from his victim, would this be possible with the current revision ?

In ES i did as bellow:

Syntax: Select all

from vecmath import vector, distance
distance(vector(es.getplayerlocation(VICTIM)), vector(es.getplayerlocation(ATTACKER))) * 0.0254

Posted: Tue Aug 14, 2012 5:57 pm
by satoon101
Not currently. We still need to expose the Vector methods to be able to get the xyz coords. Once that is done, this will be perfectly possible, and possibly look something like:

Syntax: Select all

from Source import Player
from events.decorator import Event

@Event
def player_death(GameEvent):
# Get the victim and attacker userids
userid = GameEvent.GetInt('userid')
attacker = GameEvent.GetInt('attacker')

# Was this a suicide?
if attacker in (userid, 0):

# If so, do nothing
return

# Get the victim and attacker playerinfo
victim = get_player_info(userid)
killer = get_player_info(attacker)

# Were either of the 2 playerinfo instances returned as None?
if victim is None or killer is None:

# If so, do nothing
return

# Was this a team kill?
if victim.GetTeamIndex() == killer.GetTeamIndex():

# If so, do nothing
return

# Get the player's position
victim_origin = victim.GetAbsOrigin()

# Get the attacker's position
killer_origin = killer.GetAbsOrigin()

distance = victim_origin.DistTo(killer_origin)

def get_player_info(userid):
'''Returns an IPlayerInfo instance for the given userid'''

# Loop through all players on the server
for player in Player.Players():

# Is the current player the one we are looking for?
if player.GetUserID() == userid:

# Return the player's IPlayerInfo instance
return player

# If no player was found, return None
return None
Satoon

Posted: Tue Aug 14, 2012 7:51 pm
by satoon101
If you build with the newest version (just pushed some changes a few minutes ago), you can use the script above. Take note that I changed that script to use a better method.

Satoon

Posted: Tue Aug 14, 2012 8:05 pm
by Tuck
satoon101 wrote:If you build with the newest version (just pushed some changes a few minutes ago), you can use the script above. Take note that I changed that script to use a better method.

Satoon


Thanks :) , is there any references for Source.Player ?
i would love to see what kinda information i could get via it :)

Posted: Tue Aug 14, 2012 8:12 pm
by satoon101
For now, until the wiki gets updated, you can look at the available methods to the IPlayerInfo instance in the core:
http://code.google.com/p/source-python/source/browse/src/core/modules/export_player.cpp

To iterate over all players on the server (with their IPlayerInfo instances), you can use for player in Player.Players():

Satoon

Posted: Tue Aug 14, 2012 8:16 pm
by Tuck
Ight thanks

Posted: Wed Aug 15, 2012 2:33 am
by satoon101
Updated script. Sorry, had a silly mistake in there.

Satoon

Posted: Wed Aug 15, 2012 5:15 pm
by your-name-here
As an FYI, there are MANY more vector operation functions we haven't wrapped yet. Once we get this implemented, it will virtually eliminate the need for a vecmath library. In addition, they will be orders of magnitude faster because a lot of the vector code uses SSE and SSE2 instruction sets in order to perform floating point operations quickly :)