File: PlayerRoster.cpp

package info (click to toggle)
spring 106.0%2Bdfsg-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 55,316 kB
  • sloc: cpp: 543,954; ansic: 44,800; python: 12,575; java: 12,201; awk: 5,889; sh: 1,796; asm: 1,546; xml: 655; perl: 405; php: 211; objc: 194; makefile: 76; sed: 2
file content (263 lines) | stat: -rw-r--r-- 7,582 bytes parent folder | download | duplicates (3)
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
/* 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/StringUtil.h"

#include <cassert>
#include <cstring>

static int CompareDummy      (const int aIdx, const int bIdx) { return ((aIdx > bIdx) * 2 - 1); }
static int CompareAllies     (const int aIdx, const int bIdx);
static int CompareTeamIDs    (const int aIdx, const int bIdx);
static int ComparePlayerNames(const int aIdx, const int bIdx);
static int ComparePlayerCPUs (const int aIdx, const int bIdx);
static int ComparePlayerPings(const int aIdx, const int bIdx);


PlayerRoster playerRoster;


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


void PlayerRoster::SetCompareFunc(SortType cmpType)
{
	switch (cmpType) {
		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 = CompareDummy;       } 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);

	switch (name[0]) {
		case 'a': case 'A': { SetCompareFunc(compareType = Allies    ); return true; } break;
		case 't': case 'T': { SetCompareFunc(compareType = TeamID    ); return true; } break;
		case 'n': case 'N': { SetCompareFunc(compareType = PlayerName); return true; } break;
		case 'c': case 'C': { SetCompareFunc(compareType = PlayerCPU ); return true; } break;
		case 'p': case 'P': { SetCompareFunc(compareType = PlayerPing); return true; } break;
		default:            {                                                        } break;
	}

	SetCompareFunc(compareType = Disabled);
	return false;
}


bool PlayerRoster::SetSortTypeByCode(SortType type)
{
	switch (type) {
		case Allies:     {                                                   } // fall-through
		case TeamID:     {                                                   } // fall-through
		case PlayerName: {                                                   } // fall-through
		case PlayerCPU:  {                                                   } // fall-through
		case PlayerPing: { SetCompareFunc(compareType = type); return  true; } break;
		case Disabled:   { SetCompareFunc(compareType = type); return false; } break;
		default:         {                                                   } break;
	}

	return false;
}


const char* PlayerRoster::GetSortName() const
{
	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 nullptr;
}


const std::vector<int>& PlayerRoster::GetIndices(bool includePathingFlag, bool callerBlockResort)
{
	// if Disabled, compareFunc is a dummy so the indices are left alone
	// assert(compareType != Disabled);

	// number of players can change at runtime, must test this each call
	// or make PlayerRoster an EventClient and listen for Player* events
	// caller should ensure not to block resorting if a player joins and
	// another quits in between calls
	if (playerIndices.size() != playerHandler.ActivePlayers()) {
		playerIndices.clear();
		playerIndices.resize(playerHandler.ActivePlayers(), 0);
	}

	if (!callerBlockResort) {
		int playerIdx = 0;

		std::generate(playerIndices.begin(), playerIndices.end(), [&playerIdx]() { return (playerIdx++); });
		std::sort(playerIndices.begin(), playerIndices.end(), [&](int aIdx, int bIdx) { return (compareFunc(aIdx, bIdx) <= 0); });
	}

	// caller should do filtering for active players
	return playerIndices;
}




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

	// active players first
	if ( a->active && !b->active) return -1;
	if (!a->active &&  b->active) return +1;

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

	return 0;
}


static int CompareAllies(const int aIdx, const int bIdx)
{
	const CPlayer* a = playerHandler.Player(aIdx);
	const CPlayer* b = playerHandler.Player(bIdx);

	const int basic = CompareBasics(a, b);
	if (basic != 0)
		return basic;

	// non-spectators first
	if (!a->spectator &&  b->spectator) return -1;
	if ( a->spectator && !b->spectator) return +1;

	// my player first
	const int myPNum = gu->myPlayerNum;
	if ((aIdx == myPNum) && (bIdx != myPNum)) return -1;
	if ((aIdx != myPNum) && (bIdx == myPNum)) return +1;

	// my teammates first
	const int aTeam = a->team;
	const int bTeam = b->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 (aIdx < bIdx) return -1;
	if (aIdx > bIdx) return +1;

	return 0;
}

static int CompareTeamIDs(const int aIdx, const int bIdx)
{
	const CPlayer* a = playerHandler.Player(aIdx);
	const CPlayer* b = playerHandler.Player(bIdx);

	const int basic = CompareBasics(a, b);
	if (basic != 0)
		return basic;

	// sort by team id
	if (a->team < b->team) return -1;
	if (a->team > b->team) return +1;

	// sort by player id
	if (aIdx < bIdx) return -1;
	if (aIdx > bIdx) return +1;

	return 0;
}



static int ComparePlayerNames(const int aIdx, const int bIdx)
{
	const CPlayer* a = playerHandler.Player(aIdx);
	const CPlayer* b = playerHandler.Player(bIdx);

	const int basic = CompareBasics(a, b);
	if (basic != 0)
		return basic;

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

static int ComparePlayerCPUs(const int aIdx, const int bIdx)
{
	const CPlayer* a = playerHandler.Player(aIdx);
	const CPlayer* b = playerHandler.Player(bIdx);

	const int basic = CompareBasics(a, b);
	if (basic != 0)
		return basic;

	// sort by player cpu usage
	if (a->cpuUsage < b->cpuUsage) return -1;
	if (a->cpuUsage > b->cpuUsage) return +1;

	return 0;
}

static int ComparePlayerPings(const int aIdx, const int bIdx)
{
	const CPlayer* a = playerHandler.Player(aIdx);
	const CPlayer* b = playerHandler.Player(bIdx);

	const int basic = CompareBasics(a, b);
	if (basic != 0)
		return basic;

	// sort by player pings
	if (a->ping < b->ping) return -1;
	if (a->ping > b->ping) return +1;

	return 0;
}