View Single Post
Old 09-12-2009, 04:00 AM   #5
-=*Raz0r*=-
Rookie
 
-=*Raz0r*=-'s Avatar
 
Join Date: Oct 2006
Location: Australia
Posts: 243
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...
Code:
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...

Code:
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 ^_^'


Last edited by -=*Raz0r*=-; 09-12-2009 at 04:16 AM.
-=*Raz0r*=- is offline   you may: quote & reply,