LucasForums

LucasForums (http://www.lucasforums.com/index.php)
-   JA Coding (http://www.lucasforums.com/forumdisplay.php?f=539)
-   -   Getting a Clients Location? (http://www.lucasforums.com/showthread.php?t=200404)

TJ01 09-10-2009 05:02 PM

Getting a Clients Location?
 
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 :)

-=*Raz0r*=- 09-10-2009 05:43 PM

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

Code:

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 );
}


TJ01 09-11-2009 12:16 PM

Thanks a lot razor :)
That's a great help.

I'll have a poke around and see if i can find the clients location in ent client ;)


Much Appreciated :D

TJ01 09-11-2009 03:20 PM

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?

-=*Raz0r*=- 09-12-2009 04:00 AM

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

TJ01 09-12-2009 12:58 PM

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

-=*Raz0r*=- 09-12-2009 01:41 PM

Yeah, something like that - I just wanted a clean way to do it =P
You'd best put that in before trap_SendServerCommand is called

TJ01 09-12-2009 01:57 PM

Oh.

I thought i'd put it after this line
Code:

VectorCopy(level.clients[j].ps.origin, origin);
And then edit 'origin'. :confused:

-=*Raz0r*=- 09-12-2009 03:13 PM

Ah, I meant before TeleportPlayer is called, so yeah =P
Bit tired here >_> <_<

TJ01 09-13-2009 08:05 AM

Hey :)

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

Code:

//        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.


All times are GMT -4. The time now is 11:08 PM.

Powered by vBulletin®
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
LFNetwork, LLC ©2002-2011 - All rights reserved.