File: PlayerRoster.cpp

package info (click to toggle)
spring 103.0%2Bdfsg2-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 43,720 kB
  • ctags: 63,685
  • sloc: cpp: 368,283; ansic: 33,988; python: 12,417; java: 12,203; awk: 5,879; sh: 1,846; xml: 655; perl: 405; php: 211; objc: 194; makefile: 77; sed: 2
file content (310 lines) | stat: -rw-r--r-- 8,051 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/* This file is part of the Spring engine (GPL v2 or later), see LICENSE.html */

#include "PlayerRoster.h"

#include "Game/Players/Player.h"
#include "Game/Players/PlayerHandler.h"
#include "Game/GlobalUnsynced.h"
#include "Sim/Misc/TeamHandler.h"
#include "Sim/Misc/GlobalSynced.h"
#include "System/Util.h"

#include <cassert>
#include <cstring>

static int CompareAllies     (const void* a, const void* b);
static int CompareTeamIDs    (const void* a, const void* b);
static int ComparePlayerNames(const void* a, const void* b);
static int ComparePlayerCPUs (const void* a, const void* b);
static int ComparePlayerPings(const void* a, const void* b);


PlayerRoster playerRoster;


/******************************************************************************/

PlayerRoster::PlayerRoster()
{
	compareType = Allies;
	compareFunc = CompareAllies;
}


void PlayerRoster::SetCompareFunc()
{
	switch (compareType) {
		case Allies:     { compareFunc = CompareAllies;      break; }
		case TeamID:     { compareFunc = CompareTeamIDs;     break; }
		case PlayerName: { compareFunc = ComparePlayerNames; break; }
		case PlayerCPU:  { compareFunc = ComparePlayerCPUs;  break; }
		case PlayerPing: { compareFunc = ComparePlayerPings; break; }
		case Disabled:   { compareFunc = CompareAllies;      break; }
		default:         { compareFunc = CompareAllies;      break; }
	}
}


bool PlayerRoster::SetSortTypeByName(const std::string& name)
{
	const int num = atoi(name.c_str());
	if (num > 0) {
		return SetSortTypeByCode((SortType)num);
	}

	const std::string lower = StringToLower(name);
	     if (lower == "ally") { compareType = Allies;     }
	else if (lower == "team") { compareType = TeamID;     }
	else if (lower == "name") { compareType = PlayerName; }
	else if (lower == "cpu")  { compareType = PlayerCPU;  }
	else if (lower == "ping") { compareType = PlayerPing; }
	else {
		compareType = Disabled;
		SetCompareFunc();
		return false;
	}

	SetCompareFunc();
	return true;
}


bool PlayerRoster::SetSortTypeByCode(SortType type)
{
	if ((type == Allies)     ||
	    (type == TeamID)     ||
	    (type == PlayerName) ||
	    (type == PlayerCPU)  ||
	    (type == PlayerPing)) {
		compareType = type;
		SetCompareFunc();
		return true;
	}
	compareType = Disabled;
	SetCompareFunc();
	return false;
}


const char* PlayerRoster::GetSortName()
{
	switch (compareType) {
		case Allies:     return "Allies";
		case TeamID:     return "TeamID";
		case PlayerName: return "Name";
		case PlayerCPU:  return "CPU Usage";
		case PlayerPing: return "Ping";
		case Disabled:   return "Disabled";
		default: assert(false); break;
	}
	return NULL;
}


/******************************************************************************/

const std::vector<int>& PlayerRoster::GetIndices(int* count, bool includePathingFlag) const
{
	static std::vector<int> players;
	static int knownPlayers = 0;
	players.resize(playerHandler->ActivePlayers());

	if (playerHandler->ActivePlayers() > knownPlayers) {
		for (int i = 0; i < playerHandler->ActivePlayers(); i++) {
			players[i] = i;
		}
		knownPlayers = playerHandler->ActivePlayers();
	}

	// TODO: use std::sort
	qsort(&players[0], players.size(), sizeof(int), compareFunc);

	if (count != NULL) {
		// set the count
		int& c = *count;
		for (c = 0; c < playerHandler->ActivePlayers(); c++) {
			const CPlayer* p = playerHandler->Player(players[c]);

			if (p == NULL)
				break;
			if (p->active)
				continue;

			if (!includePathingFlag || p->ping != PATHING_FLAG || !gs->PreSimFrame()) {
				break;
			}
		}
	}

	return players;
}


/******************************************************************************/

static inline int CompareBasics(const CPlayer* aP, const CPlayer* bP)
{
	// non-NULL first  (and return 0 if both NULL)
	if ((aP != NULL) && (bP == NULL)) { return -1; }
	if ((aP == NULL) && (bP != NULL)) { return +1; }
	if ((aP == NULL) && (bP == NULL)) { return  0; }

	// active players first
	if ( aP->active && !bP->active) { return -1; }
	if (!aP->active &&  bP->active) { return +1; }

	// then pathing players
	if (gs->PreSimFrame()) {
		if (aP->ping == PATHING_FLAG && bP->ping != PATHING_FLAG) { return -1; }
		if (aP->ping != PATHING_FLAG && bP->ping == PATHING_FLAG) { return +1; }
	}

	return 0;
}


/******************************************************************************/

static int CompareAllies(const void* a, const void* b)
{
	const int aID = *((const int*)a);
	const int bID = *((const int*)b);
	const CPlayer* aP = playerHandler->Player(aID);
	const CPlayer* bP = playerHandler->Player(bID);

	const int basic = CompareBasics(aP, bP);
	if (basic != 0) {
		return basic;
	}

	// non-spectators first
	if (!aP->spectator &&  bP->spectator) { return -1; }
	if ( aP->spectator && !bP->spectator) { return +1; }

	// my player first
	const int myPNum = gu->myPlayerNum;
	if ((aID == myPNum) && (bID != myPNum)) { return -1; }
	if ((aID != myPNum) && (bID == myPNum)) { return +1; }

	// my teammates first
	const int aTeam = aP->team;
	const int bTeam = bP->team;
	const int myTeam = gu->myTeam;
	if ((aTeam == myTeam) && (bTeam != myTeam)) { return -1; }
	if ((aTeam != myTeam) && (bTeam == myTeam)) { return +1; }

	// my allies first
	const int aAlly = teamHandler->AllyTeam(aTeam);
	const int bAlly = teamHandler->AllyTeam(bTeam);
	const int myATeam = gu->myAllyTeam;
	if ((aAlly == myATeam) && (bAlly != myATeam)) { return -1; }
	if ((aAlly != myATeam) && (bAlly == myATeam)) { return +1; }

	// sort by ally team
	if (aAlly < bAlly) { return -1; }
	if (aAlly > bAlly) { return +1; }

	// sort by team
	if (aTeam < bTeam) { return -1; }
	if (aTeam > bTeam) { return +1; }

	// sort by player id
	if (aID < bID) { return -1; }
	if (aID > bID) { return +1; }

	return 0;
}


/******************************************************************************/

static int CompareTeamIDs(const void* a, const void* b)
{
	const int aID = *((const int*)a);
	const int bID = *((const int*)b);
	const CPlayer* aP = playerHandler->Player(aID);
	const CPlayer* bP = playerHandler->Player(bID);

	const int basic = CompareBasics(aP, bP);
	if (basic != 0) {
		return basic;
	}

	// sort by team id
	if (aP->team < bP->team) { return -1; }
	if (aP->team > bP->team) { return +1; }

	// sort by player id
	if (aID < bID) { return -1; }
	if (aID > bID) { return +1; }

	return 0;
}


/******************************************************************************/

static int ComparePlayerNames(const void* a, const void* b)
{
	const int aID = *((const int*)a);
	const int bID = *((const int*)b);
	const CPlayer* aP = playerHandler->Player(aID);
	const CPlayer* bP = playerHandler->Player(bID);

	const int basic = CompareBasics(aP, bP);
	if (basic != 0) {
		return basic;
	}

	// sort by player name
	const std::string aName = StringToLower(aP->name);
	const std::string bName = StringToLower(bP->name);
	return strcmp(aName.c_str(), bName.c_str());
}


/******************************************************************************/

static int ComparePlayerCPUs(const void* a, const void* b)
{
	const int aID = *((const int*)a);
	const int bID = *((const int*)b);
	const CPlayer* aP = playerHandler->Player(aID);
	const CPlayer* bP = playerHandler->Player(bID);

	const int basic = CompareBasics(aP, bP);
	if (basic != 0) {
		return basic;
	}

	// sort by player cpu usage
	if (aP->cpuUsage < bP->cpuUsage) { return -1; }
	if (aP->cpuUsage > bP->cpuUsage) { return +1; }

	return 0;
}


/******************************************************************************/

static int ComparePlayerPings(const void* a, const void* b)
{
	const int aID = *((const int*)a);
	const int bID = *((const int*)b);
	const CPlayer* aP = playerHandler->Player(aID);
	const CPlayer* bP = playerHandler->Player(bID);

	const int basic = CompareBasics(aP, bP);
	if (basic != 0) {
		return basic;
	}

	// sort by player pings
	if (aP->ping < bP->ping) { return -1; }
	if (aP->ping > bP->ping) { return +1; }

	return 0;
}


/******************************************************************************/