Distance between two points ?

Please post any questions about developing your plugin here. Please use the search function before posting!
Tuck
Global Moderator
Posts: 205
Joined: Sat Jul 14, 2012 9:35 pm
Location: Copenhagen

Distance between two points ?

Postby Tuck » Tue Aug 14, 2012 4:53 pm

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
-Tuck
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Tue Aug 14, 2012 5:57 pm

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
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Tue Aug 14, 2012 7:51 pm

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
Tuck
Global Moderator
Posts: 205
Joined: Sat Jul 14, 2012 9:35 pm
Location: Copenhagen

Postby Tuck » Tue Aug 14, 2012 8:05 pm

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 :)
-Tuck
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Tue Aug 14, 2012 8:12 pm

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
Tuck
Global Moderator
Posts: 205
Joined: Sat Jul 14, 2012 9:35 pm
Location: Copenhagen

Postby Tuck » Tue Aug 14, 2012 8:16 pm

Ight thanks
-Tuck
User avatar
satoon101
Project Leader
Posts: 2697
Joined: Sat Jul 07, 2012 1:59 am

Postby satoon101 » Wed Aug 15, 2012 2:33 am

Updated script. Sorry, had a silly mistake in there.

Satoon
your-name-here
Developer
Posts: 168
Joined: Sat Jul 07, 2012 1:58 am

Postby your-name-here » Wed Aug 15, 2012 5:15 pm

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 :)

Return to “Plugin Development Support”

Who is online

Users browsing this forum: No registered users and 24 guests