Jump to content

Home

Getting a Clients Location?


TJ01

Recommended Posts

Hi.

 

Keeping it short and sweet :thmbup1:; how do i obtain a Client's Location in co-ordinates?

 

I looked up how the viewpos command gets the co-ords but that's a client-side command and the cg.refdef.vieworg doesn't work on a server side mod.

 

Surely there must be a struct somewhere or something that holds the client's location...

 

Nearly all mod's out there let you tele to a player so i know it can be done; i just don't know how :confused:

 

Thanks for your help :)

Link to comment
Share on other sites

Heh, mmk...

 

Here's a bit of code I used to teleport you roughly where you're looking at.

I'm sure you could tweak it around a bit for what you want =P

 

static void teleport( gentity_t *ent ) {
trace_t	tr;
vec3_t	fPos, telepos;
int i;

//We want to obtain where they're looking at - This does it for us =]
AngleVectors(ent->client->ps.viewangles, fPos, NULL, NULL);
for (i=0; i < 3; i++)
	fPos[i] = ent->client->ps.origin[i] + fPos[i]*Q3_INFINITE;

//Perform the trace, the tr.endpos will hold the vec3_t of where it collided with something
trap_Trace(&tr, ent->client->ps.origin, 0, 0, fPos, ent->s.number, MASK_OPAQUE|CONTENTS_SOLID|CONTENTS_BODY|CONTENTS_ITEM|CONTENTS_CORPSE );

//If we teleported directly to tr.endpos, we'd end up in a wall, so do a bit of vector math and we're safe
VectorMA(tr.endpos, 32, tr.plane.normal, telepos);

//Beam me up, scotty!
TeleportPlayer( ent, telepos, ent->client->ps.viewangles );
}

Link to comment
Share on other sites

The command worked a treat; thanks razor.

 

But I'm still unable to create a command which prints the clients location.

I want the clients location in co-ordinates to be viewable to admins.

 

Any idea where i'd find the clients location/origin ?

 

Also i'm unsure how i would make a cmd which teleports any other client to co-ords; could anyone help me here?

Link to comment
Share on other sites

No problem :)

 

As you might have noticed, TeleportPlayer works the real magic.

It takes in a pointer to the gentity_t of the player you wish to teleport, then a vector (3d coordinates), and the angle you wish to face them in (as another vector).

 

VectorMA(tr.endpos, 32, tr.plane.normal, telepos) is a macro for some funky vector math (Not sure exactly what it does) that basically takes in the end position of the trace (Where you're looking at), two numbers that work their magic in this case, and then stores it in telepos (A vector I created)

 

telepos now holds the exact location in 3d coordinates I wanted to teleport the player to, so I passed that as an argument to TeleportPlayer

 

 

So now you know how that works; here's how to do the same for teleporting to a player.

 

If we just teleport directly to them, we'll telefrag them, so we'll have to safeguard against that.

I also safeguarded against entering an invalid name.

This isn't the cleanest code, and I'm sure someone could offer a less ugly version of this, but here's what I got after playing around for a few minutes...

void TeleportToPlayer( gentity_t *ent ) {
vec3_t		origin = {0, 0, 0};
int			j = -1;
char		name[32];

//	If they didn't supply a name, don't let them teleport
if (trap_Argc() < 2)
{
	trap_SendServerCommand(ent->client->ps.clientNum, va("print \"^1You must provide a name\n\""));
	return;
}

//	Get the name they supplied, and we'll try to work out the clientNum
strcpy( name, ConcatArgs(1) );
j = G_ClientNumberFromName(name);

//Because G_ClientNumberFromName will return -1 if they couldn't find anything,
//	we have to safeguard, otherwise we'd teleport to some weird location
if ( j<0 || j>=MAX_CLIENTS )
{// 'j' wasn't between 0 <-> 32, so warn them and exit this function
	trap_SendServerCommand(ent->client->ps.clientNum, va("print \"^1Player %s is not on the server\n\"", name));
	return;
}

//	They're a valid client, so copy their coordinates into 'origin'
VectorCopy(level.clients[j].ps.origin, origin);

//	TODO: If we teleported straight to those coord's, we'd telefrag them, so play around with 'origin'

//Teleport them to 'origin'
TeleportPlayer(ent, origin, ent->client->ps.viewangles);
}

 

All that's left to do is play around with 'origin' so we won't teleport inside them.

 

 

If you just wanted to print the coords of the player, it's a bit simpler...

 

void TeleportToPlayer( gentity_t *ent ) {
int			j = -1;
char		name[32];
char		theirOrigin[24];

//	If they didn't supply a name, exit this function
if (trap_Argc() < 2)
{
	trap_SendServerCommand(ent->client->ps.clientNum, va("print \"^1You must provide a name\n\""));
	return;
}

//	Get the name they supplied, and we'll try to work out the clientNum
strcpy( name, ConcatArgs(1) );
j = G_ClientNumberFromStrippedName(name);

//Because G_ClientNumberFromName will return -1 if they couldn't find anything,
//	we have to safeguard, otherwise we'd teleport to some weird location
if ( j<0 || j>=MAX_CLIENTS )
{// 'j' wasn't between 0 <-> 32, so warn them and exit this function
	trap_SendServerCommand(ent->client->ps.clientNum, va("print \"^1Player %s is not on the server\n\"", name));
	return;
}

//	They're a valid client, so copy their coordinates into 'myStr'
strcpy(theirOrigin, vtos(level.clients[j].ps.origin));

//	Print out their coords
trap_SendServerCommand(ent->client->ps.clientNum, va("print \"^1^5%s\n\"", theirOrigin));
}

 

I've heavily commented it all so you can understand it. :)

Hope I helped ^_^'

Link to comment
Share on other sites

That's brilliant. Thanks Razor :)

 

I understood how tele'ing worked by looking at setviewpos :p

 

I just didn't know how to get a client ID or origin.

 

Thanks Alot; you've been a great help and i've learnt alot from your code snipets.

 

Oh and about not tele-fragging them, could i just add 100 to one of the axis. Would that work?

:)

Link to comment
Share on other sites

Hey :)

 

Theres a slight issue with the TeletoPlayer code but ive fixed it.

 

//	Get the name they supplied, and we'll try to work out the clientNum
strcpy( name, ConcatArgs(1) );
j = ClientNumberFromString  (ent, name );

//Because G_ClientNumberFromName will return -1 if they couldn't find anything,
//	we have to safeguard, otherwise we'd teleport to some weird location
if ( j == -1 || j>=MAX_CLIENTS )
{// 'j' wasn't between 0 <-> 32, so warn them and exit this function
	return; 

 

I've used ClientNumberFromString instead as its more stable and You don't need to print an error message since that function does it for you :)

 

It also works with ClientNumbers; so /teletoplayer 2 would tele you to Client 2 no matter what their name is.

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...