[Wolf:ET] Sort players for aiming

mowl

Coders
Hello all.

A friend asked me I could show him how I sort targets.
I found it interesting to share this with you all. I used nexus as test-platform. Enjoy!

PS: Using an iterator on class-level inside the header :)

Player sorting class:
Code:
class listPlayer_t /*typedef struct*/ 
{
public:
	// sorting data
	struct compareClientNum : public binary_function<listPlayer_t, listPlayer_t, bool>
	{
		bool operator()(listPlayer_t p1, listPlayer_t p2)
		{
			return (p1.clientNum < p2.clientNum);
		}
	};

	int			clientNum;
	qboolean*	currentValid;
	vec3_t		origin;
	int			screen[2]; // x & y values

} /*listPlayer_t*/;

Sorting function:
Code:
void CNexEngine::sortOnClientnum(vector<listPlayer_t> vec)
{
	std::sort(vec.begin(), vec.end(), listPlayer_t::compareClientNum());
}

As you can see, I sort by client number.

The build-up of the vectorlist:
Code:
vector<listPlayer_t> players;
Code:
extern std::vector<listPlayer_t> players;

And in init:
Code:
for (int i=0; i < MAX_CLIENTS ; i++) {
		nex_centity_t cg_ent = nex.cg_entities[i];
		nexEntity_t* ent = &nex.entities[i]; 

		if (!cg_ent.currentValid) 
			continue;

		if (!cg_ent.currentState->eType == ET_PLAYER) // mOwl: lets only add if its a player
			continue;

		// mOwl: construct the datatype based on the player struct
		listPlayer_t p;

		p.clientNum = i;
		p.currentValid = cg_ent.currentValid;
		VectorCopy(ent->origin, p.origin);
		p.screen[SCREEN_X] = ent->screenX;
		p.screen[SCREEN_Y] = ent->screenY;

		players.push_back(p); // mOwl: finally we'll add the data to the list
	}

Kind regards,
mOwl
 

sp00f

New member
Well done, a truly leet way of sorting. :)
Thanks by the way!

This is the way that I loop thru the players using it.
Code:
for(int i=0;i<sizeof(players);i++)
{
    CTools.Writer("%i",i);
}
 
Top