]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/scores.qc
Merge branch 'Lyberta/PlayerScore_Get' into 'master'
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / scores.qc
1 #include "scores.qh"
2
3 #include "command/common.qh"
4 #include "mutators/_mod.qh"
5 #include <common/net_linked.qh>
6 #include "../common/playerstats.qh"
7 #include "../common/teams.qh"
8
9 .entity scorekeeper;
10 entity teamscorekeepers[16];
11 float teamscores_entities_count;
12 var .float scores_primary;
13 var .float teamscores_primary;
14 float scores_flags_primary;
15 float teamscores_flags_primary;
16
17 vector ScoreField_Compare(entity t1, entity t2, .float field, float fieldflags, vector previous, float strict) // returns: cmp value, best prio
18 {
19         if(!strict && !(fieldflags & SFL_SORT_PRIO_MASK)) // column does not sort
20                 return previous;
21         if((fieldflags & SFL_SORT_PRIO_MASK) < previous.y)
22                 return previous;
23         if (t1.(field) == t2.(field))
24                 return previous;
25
26         previous.y = fieldflags & SFL_SORT_PRIO_MASK;
27
28         if(fieldflags & SFL_ZERO_IS_WORST)
29         {
30                 if (t1.(field) == 0)
31                 {
32                         previous.x = -1;
33                         return previous;
34                 }
35                 else if (t2.(field) == 0)
36                 {
37                         previous.x = +1;
38                         return previous;
39                 }
40         }
41
42         if (fieldflags & SFL_LOWER_IS_BETTER)
43                 previous.x = (t2.(field) - t1.(field));
44         else
45                 previous.x = (t1.(field) - t2.(field));
46
47         return previous;
48 }
49
50 /*
51  * teamscore entities
52  */
53
54 bool TeamScore_SendEntity(entity this, entity to, float sendflags)
55 {
56         float i, p, longflags;
57
58         WriteHeader(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
59         int t = this.team - 1;
60         assert(t, eprint(this));
61         WriteByte(MSG_ENTITY, t);
62
63         longflags = 0;
64         for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
65                 if(this.(teamscores(i)) > 127 || this.(teamscores(i)) <= -128)
66                         longflags |= p;
67
68 #if MAX_TEAMSCORE <= 8
69         WriteByte(MSG_ENTITY, sendflags);
70         WriteByte(MSG_ENTITY, longflags);
71 #else
72         WriteShort(MSG_ENTITY, sendflags);
73         WriteShort(MSG_ENTITY, longflags);
74 #endif
75         for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
76                 if(sendflags & p)
77                 {
78                         if(longflags & p)
79                                 WriteInt24_t(MSG_ENTITY, this.(teamscores(i)));
80                         else
81                                 WriteChar(MSG_ENTITY, this.(teamscores(i)));
82                 }
83
84         return true;
85 }
86
87 void TeamScore_Spawn(float t, string name)
88 {
89         entity ts = new_pure(csqc_score_team);
90         ts.netname = name; // not used yet, FIXME
91         ts.team = t;
92         Net_LinkEntity(ts, false, 0, TeamScore_SendEntity);
93         teamscorekeepers[t - 1] = ts;
94         ++teamscores_entities_count;
95         PlayerStats_GameReport_AddTeam(t);
96 }
97
98 float TeamScore_AddToTeam(float t, float scorefield, float score)
99 {
100         entity s;
101
102         if(game_stopped)
103                 score = 0;
104
105         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
106         if(t <= 0 || t >= 16)
107         {
108                 if(game_stopped)
109                         return 0;
110                 error("Adding score to invalid team!");
111         }
112         s = teamscorekeepers[t - 1];
113         if(!s)
114         {
115                 if(game_stopped)
116                         return 0;
117                 error("Adding score to unknown team!");
118         }
119         if(score)
120                 if(teamscores_label(scorefield) != "")
121                         s.SendFlags |= (2 ** scorefield);
122         return (s.(teamscores(scorefield)) += score);
123 }
124
125 float TeamScore_Add(entity player, float scorefield, float score)
126 {
127         return TeamScore_AddToTeam(player.team, scorefield, score);
128 }
129
130 float TeamScore_Compare(entity t1, entity t2, float strict)
131 {
132         if(!t1 || !t2) return (!t2) - !t1;
133
134         vector result = '0 0 0';
135         float i;
136         for(i = 0; i < MAX_TEAMSCORE; ++i)
137         {
138                 var .float f;
139                 f = teamscores(i);
140                 result = ScoreField_Compare(t1, t2, f, teamscores_flags(i), result, strict);
141         }
142
143         if (result.x == 0 && strict)
144                 result.x = t1.team - t2.team;
145
146         return result.x;
147 }
148
149 /*
150  * the scoreinfo entity
151  */
152
153 void ScoreInfo_SetLabel_PlayerScore(PlayerScoreField i, string label, float scoreflags)
154 {
155         scores_label(i) = label;
156         scores_flags(i) = scoreflags;
157         if((scoreflags & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
158         {
159                 scores_primary = scores(i);
160                 scores_flags_primary = scoreflags;
161         }
162         if(label != "")
163         {
164                 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
165                 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
166         }
167 }
168
169 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
170 {
171         teamscores_label(i) = label;
172         teamscores_flags(i) = scoreflags;
173         if((scoreflags & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
174         {
175                 teamscores_primary = teamscores(i);
176                 teamscores_flags_primary = scoreflags;
177         }
178         if(label != "")
179         {
180                 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
181                 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
182         }
183 }
184
185 bool ScoreInfo_SendEntity(entity this, entity to, int sf)
186 {
187         float i;
188         WriteHeader(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
189         WriteRegistered(Gametypes, MSG_ENTITY, MapInfo_LoadedGametype);
190         FOREACH(Scores, true, {
191                 WriteString(MSG_ENTITY, scores_label(it));
192                 WriteByte(MSG_ENTITY, scores_flags(it));
193         });
194         for(i = 0; i < MAX_TEAMSCORE; ++i)
195         {
196                 WriteString(MSG_ENTITY, teamscores_label(i));
197                 WriteByte(MSG_ENTITY, teamscores_flags(i));
198         }
199         return true;
200 }
201
202 void ScoreInfo_Init(int teams)
203 {
204         if(scores_initialized)
205         {
206                 scores_initialized.SendFlags |= 1; // force a resend
207         }
208         else
209         {
210                 scores_initialized = new_pure(ent_client_scoreinfo);
211                 Net_LinkEntity(scores_initialized, false, 0, ScoreInfo_SendEntity);
212         }
213         if(teams & BIT(0))
214                 TeamScore_Spawn(NUM_TEAM_1, "Red");
215         if(teams & BIT(1))
216                 TeamScore_Spawn(NUM_TEAM_2, "Blue");
217         if(teams & BIT(2))
218                 TeamScore_Spawn(NUM_TEAM_3, "Yellow");
219         if(teams & BIT(3))
220                 TeamScore_Spawn(NUM_TEAM_4, "Pink");
221 }
222
223 /*
224  * per-player score entities
225  */
226
227 bool PlayerScore_SendEntity(entity this, entity to, float sendflags)
228 {
229         WriteHeader(MSG_ENTITY, ENT_CLIENT_SCORES);
230         WriteByte(MSG_ENTITY, etof(this.owner));
231
232         int longflags = 0;
233         FOREACH(Scores, true, {
234             int p = 1 << (i % 16);
235                 if (this.(scores(it)) > 127 || this.(scores(it)) <= -128)
236                         longflags |= p;
237     });
238
239         WriteShort(MSG_ENTITY, sendflags);
240         WriteShort(MSG_ENTITY, longflags);
241         FOREACH(Scores, true, {
242             int p = 1 << (i % 16);
243                 if (sendflags & p)
244                 {
245                         if(longflags & p)
246                                 WriteInt24_t(MSG_ENTITY, this.(scores(it)));
247                         else
248                                 WriteChar(MSG_ENTITY, this.(scores(it)));
249                 }
250     });
251
252         return true;
253 }
254
255 float PlayerScore_Clear(entity player)
256 {
257         entity sk;
258
259         if(teamscores_entities_count)
260                 return 0;
261
262         if(MUTATOR_CALLHOOK(ForbidPlayerScore_Clear)) return 0;
263
264         sk = player.scorekeeper;
265         FOREACH(Scores, true, {
266                 if(sk.(scores(it)) != 0)
267                         if(scores_label(it) != "")
268                                 sk.SendFlags |= (2 ** (i % 16));
269                 if(i != SP_ELO.m_id)
270                         sk.(scores(it)) = 0;
271         });
272
273         return 1;
274 }
275
276 void Score_ClearAll()
277 {
278         entity sk;
279         float t;
280         FOREACH_CLIENTSLOT(true, {
281                 sk = it.scorekeeper;
282                 if (!sk) continue;
283                 FOREACH(Scores, true, {
284                         if(sk.(scores(it)) != 0)
285                                 if(scores_label(it) != "")
286                                         sk.SendFlags |= (2 ** (i % 16));
287                         if(i != SP_ELO.m_id)
288                                 sk.(scores(it)) = 0;
289                 });
290         });
291         for(t = 0; t < 16; ++t)
292         {
293                 sk = teamscorekeepers[t];
294                 if(!sk)
295                         continue;
296                 for(int j = 0; j < MAX_TEAMSCORE; ++j)
297                 {
298                         if(sk.(teamscores(j)) != 0)
299                                 if(teamscores_label(j) != "")
300                                         sk.SendFlags |= (2 ** j);
301                         sk.(teamscores(j)) = 0;
302                 }
303         }
304 }
305
306 void PlayerScore_Attach(entity player)
307 {
308         if(player.scorekeeper)
309                 error("player already has a scorekeeper");
310         entity sk = new_pure(scorekeeper);
311         sk.owner = player;
312         Net_LinkEntity(sk, false, 0, PlayerScore_SendEntity);
313         player.scorekeeper = sk;
314 }
315
316 void PlayerScore_Detach(entity player)
317 {
318         if(!player.scorekeeper)
319                 error("player has no scorekeeper");
320         delete(player.scorekeeper);
321         player.scorekeeper = NULL;
322 }
323
324 float PlayerScore_Add(entity player, PlayerScoreField scorefield, float score)
325 {
326         bool mutator_returnvalue = MUTATOR_CALLHOOK(AddPlayerScore, scorefield, score, player);
327         score = M_ARGV(1, float);
328
329         if(game_stopped)
330         if(!mutator_returnvalue)
331                 score = 0;
332
333         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
334         entity s = player.scorekeeper;
335         if(!s)
336         {
337                 if(game_stopped)
338                         return 0;
339                 LOG_WARN("Adding score to unknown player!");
340                 return 0;
341         }
342         if(!score)
343         {
344                 return s.(scores(scorefield));
345         }
346         if(scores_label(scorefield) != "")
347                 s.SendFlags |= (2 ** (scorefield.m_id % 16));
348         if(!warmup_stage)
349                 PS_GR_P_ADDVAL(s.owner, strcat(PLAYERSTATS_TOTAL, scores_label(scorefield)), score);
350         return (s.(scores(scorefield)) += score);
351 }
352
353 float PlayerTeamScore_Add(entity player, PlayerScoreField pscorefield, float tscorefield, float score)
354 {
355         float r;
356         r = PlayerScore_Add(player, pscorefield, score);
357         if(teamscores_entities_count) // only for teamplay
358                 r = TeamScore_Add(player, tscorefield, score);
359         return r;
360 }
361
362 float PlayerScore_Compare(entity t1, entity t2, float strict)
363 {
364         if(!t1 || !t2) return (!t2) - !t1;
365
366         vector result = '0 0 0';
367         FOREACH(Scores, true, {
368                 var .float f = scores(it);
369                 result = ScoreField_Compare(t1, t2, f, scores_flags(it), result, strict);
370         });
371
372         if (result.x == 0 && strict)
373                 result.x = etof(t1.owner) - etof(t2.owner);
374
375         return result.x;
376 }
377
378 void WinningConditionHelper(entity this)
379 {
380         float c;
381         string s;
382         float fullstatus;
383         entity winnerscorekeeper;
384         entity secondscorekeeper;
385         entity sk;
386
387         // format:
388         // gametype:P<pure>:S<slots>::plabel,plabel:tlabel,tlabel:teamid:tscore,tscore:teamid:tscore,tscore
389         // score labels always start with a symbol or with lower case
390         // so to match pure, match for :P0:
391         // to match full, match for :S0:
392
393         fullstatus = autocvar_g_full_getstatus_responses;
394
395         s = GetGametype();
396         s = strcat(s, ":", autocvar_g_xonoticversion);
397         s = strcat(s, ":P", ftos(cvar_purechanges_count));
398         s = strcat(s, ":S", ftos(nJoinAllowed(this, NULL)));
399         s = strcat(s, ":F", ftos(serverflags));
400         s = strcat(s, ":M", modname);
401         s = strcat(s, "::", GetPlayerScoreString(NULL, (fullstatus ? 1 : 2)));
402
403         if(teamscores_entities_count)
404         {
405                 float t;
406
407                 s = strcat(s, ":", GetTeamScoreString(0, 1));
408                 for(t = 0; t < 16; ++t)
409                         if(teamscorekeepers[t])
410                                 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
411
412                 WinningConditionHelper_winnerteam = -1;
413                 WinningConditionHelper_secondteam = -1;
414                 winnerscorekeeper = NULL;
415                 secondscorekeeper = NULL;
416                 for(t = 0; t < 16; ++t)
417                 {
418                         sk = teamscorekeepers[t];
419                         c = TeamScore_Compare(winnerscorekeeper, sk, 1);
420                         if(c < 0)
421                         {
422                                 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
423                                 WinningConditionHelper_winnerteam = t + 1;
424                                 secondscorekeeper = winnerscorekeeper;
425                                 winnerscorekeeper = sk;
426                         }
427                         else
428                         {
429                                 c = TeamScore_Compare(secondscorekeeper, sk, 1);
430                                 if(c < 0)
431                                 {
432                                         WinningConditionHelper_secondteam = t + 1;
433                                         secondscorekeeper = sk;
434                                 }
435                         }
436                 }
437
438                 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper, 0) == 0);
439                 if(WinningConditionHelper_equality)
440                         WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
441
442                 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
443                 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
444                 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
445                 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
446
447                 WinningConditionHelper_winner = NULL; // not supported in teamplay
448                 WinningConditionHelper_second = NULL; // not supported in teamplay
449         }
450         else
451         {
452                 WinningConditionHelper_winner = NULL;
453                 WinningConditionHelper_second = NULL;
454                 winnerscorekeeper = NULL;
455                 secondscorekeeper = NULL;
456                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
457                         sk = it.scorekeeper;
458                         c = PlayerScore_Compare(winnerscorekeeper, sk, 1);
459                         if(c < 0)
460                         {
461                                 WinningConditionHelper_second = WinningConditionHelper_winner;
462                                 WinningConditionHelper_winner = it;
463                                 secondscorekeeper = winnerscorekeeper;
464                                 winnerscorekeeper = sk;
465                         }
466                         else
467                         {
468                                 c = PlayerScore_Compare(secondscorekeeper, sk, 1);
469                                 if(c < 0)
470                                 {
471                                         WinningConditionHelper_second = it;
472                                         secondscorekeeper = sk;
473                                 }
474                         }
475                 ));
476
477                 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper, 0) == 0);
478                 if(WinningConditionHelper_equality)
479                         WinningConditionHelper_winner = WinningConditionHelper_second = NULL;
480
481                 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
482                 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
483                 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
484                 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
485
486                 WinningConditionHelper_winnerteam = -1; // no teamplay
487                 WinningConditionHelper_secondteam = -1; // no teamplay
488         }
489
490         if(WinningConditionHelper_topscore == 0)
491         {
492                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
493                 {
494                         if(WinningConditionHelper_lowerisbetter)
495                                 WinningConditionHelper_topscore = 999999999;
496                         else
497                                 WinningConditionHelper_topscore = -999999999;
498                 }
499                 WinningConditionHelper_equality = 0;
500         }
501
502         if(WinningConditionHelper_secondscore == 0)
503         {
504                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
505                 {
506                         if(WinningConditionHelper_lowerisbetter)
507                                 WinningConditionHelper_secondscore = 999999999;
508                         else
509                                 WinningConditionHelper_secondscore = -999999999;
510                 }
511         }
512
513         if(worldstatus)
514                 strunzone(worldstatus);
515         worldstatus = strzone(s);
516
517         FOREACH_CLIENT(true, LAMBDA(
518                 string s = "";
519                 if(fullstatus)
520                 {
521                         s = GetPlayerScoreString(it, 1);
522                         s = strcat(s, IS_REAL_CLIENT(it) ? ":human" : ":bot");
523                         if(!IS_PLAYER(it) && !MUTATOR_CALLHOOK(GetPlayerStatus, it))
524                                 s = strcat(s, ":spectator");
525                 }
526                 else
527                 {
528                         if (IS_PLAYER(it) || MUTATOR_CALLHOOK(GetPlayerStatus, it))
529                                 s = GetPlayerScoreString(it, 2);
530                         else
531                                 s = "-666";
532                 }
533
534                 if(it.clientstatus)
535                         strunzone(it.clientstatus);
536                 it.clientstatus = strzone(s);
537         ));
538 }
539
540 string GetScoreLogLabel(string label, float fl)
541 {
542         if(fl & SFL_LOWER_IS_BETTER)
543                 label = strcat(label, "<");
544         if((fl & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
545                 label = strcat(label, "!!");
546         else if((fl & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
547                 label = strcat(label, "!");
548         return label;
549 }
550
551 string GetPlayerScoreString(entity pl, float shortString)
552 {
553         string out;
554         entity sk;
555         float f;
556         string l;
557
558         out = "";
559         if(!pl)
560         {
561                 // label
562                 FOREACH(Scores, true, {
563                         if ((scores_flags(it) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
564                         {
565                                 f = scores_flags(it);
566                                 l = scores_label(it);
567                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
568                         }
569         });
570                 if(shortString < 2)
571                 FOREACH(Scores, true, {
572                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
573                         {
574                                 f = scores_flags(it);
575                                 l = scores_label(it);
576                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
577                         }
578         });
579                 if(shortString < 1)
580                 FOREACH(Scores, true, {
581                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
582                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
583                         {
584                                 f = scores_flags(it);
585                                 l = scores_label(it);
586                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
587                         }
588         });
589                 out = substring(out, 0, strlen(out) - 1);
590         }
591         else if((sk = pl.scorekeeper))
592         {
593                 FOREACH(Scores, true, {
594                         if ((scores_flags(it) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
595                                 out = strcat(out, ftos(sk.(scores(it))), ",");
596         });
597                 if(shortString < 2)
598                 FOREACH(Scores, true, {
599                         if ((scores_flags(it) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
600                                 out = strcat(out, ftos(sk.(scores(it))), ",");
601         });
602                 if(shortString < 1)
603                 FOREACH(Scores, true, {
604                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
605                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
606                                 out = strcat(out, ftos(sk.(scores(it))), ",");
607         });
608                 out = substring(out, 0, strlen(out) - 1);
609         }
610         return out;
611 }
612
613 string GetTeamScoreString(float tm, float shortString)
614 {
615         string out;
616         entity sk;
617         float i, f;
618         string l;
619
620         out = "";
621         if(tm == 0)
622         {
623                 // label
624                 for(i = 0; i < MAX_TEAMSCORE; ++i)
625                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
626                         {
627                                 f = teamscores_flags(i);
628                                 l = teamscores_label(i);
629                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
630                         }
631                 if(shortString < 2)
632                 for(i = 0; i < MAX_TEAMSCORE; ++i)
633                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
634                         {
635                                 f = teamscores_flags(i);
636                                 l = teamscores_label(i);
637                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
638                         }
639                 if(shortString < 1)
640                 for(i = 0; i < MAX_TEAMSCORE; ++i)
641                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
642                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
643                         {
644                                 f = teamscores_flags(i);
645                                 l = teamscores_label(i);
646                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
647                         }
648                 out = substring(out, 0, strlen(out) - 1);
649         }
650         else if((sk = teamscorekeepers[tm - 1]))
651         {
652                 for(i = 0; i < MAX_TEAMSCORE; ++i)
653                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
654                                 out = strcat(out, ftos(sk.(teamscores(i))), ",");
655                 if(shortString < 2)
656                 for(i = 0; i < MAX_TEAMSCORE; ++i)
657                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
658                                 out = strcat(out, ftos(sk.(teamscores(i))), ",");
659                 if(shortString < 1)
660                 for(i = 0; i < MAX_TEAMSCORE; ++i)
661                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
662                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
663                                 out = strcat(out, ftos(sk.(teamscores(i))), ",");
664                 out = substring(out, 0, strlen(out) - 1);
665         }
666         return out;
667 }
668
669 float PlayerTeamScore_Compare(entity p1, entity p2, float teams, float strict)
670 {
671         if(teams && teamscores_entities_count)
672         {
673                 if(p1.team != p2.team)
674                 {
675                         entity t1, t2;
676                         float r;
677                         t1 = teamscorekeepers[p1.team - 1];
678                         t2 = teamscorekeepers[p2.team - 1];
679                         r = TeamScore_Compare(t1, t2, ((teams >= 0) ? 1 : strict));
680                         return r;
681                 }
682                 if(teams < 0)
683                         return 0;
684         }
685
686         return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper, strict);
687 }
688
689 entity PlayerScore_Sort(.float field, float teams, float strict, float nospectators)
690 {
691         entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
692         float i, j;
693
694         plist = NULL;
695
696         FOREACH_CLIENT(true, LAMBDA(it.(field) = 0));
697
698         FOREACH_CLIENT(it.scorekeeper,
699         {
700                 if(nospectators)
701                         if(it.frags == FRAGS_SPECTATOR)
702                                 continue;
703
704                 it.chain = plist;
705                 plist = it;
706         });
707         // Now plist points to the whole list.
708
709         pfirst = plast = NULL;
710
711         i = j = 0;
712         while(plist)
713         {
714                 pprev = pbestprev = NULL;
715                 pbest = plist;
716                 for(p = plist; (pprev = p), (p = p.chain); )
717                 {
718                         if(PlayerTeamScore_Compare(p, pbest, teams, strict) > 0)
719                         {
720                                 pbest = p;
721                                 pbestprev = pprev;
722                         }
723                 }
724
725                 // remove pbest out of the chain
726                 if(pbestprev == NULL)
727                         plist = pbest.chain;
728                 else
729                         pbestprev.chain = pbest.chain;
730                 pbest.chain = NULL;
731
732                 ++i;
733                 if(!plast || PlayerTeamScore_Compare(plast, pbest, teams, 0))
734                         j = i;
735
736                 pbest.(field) = j;
737
738                 if (!pfirst)
739                         pfirst = pbest;
740                 if(plast)
741                         plast.chain = pbest;
742                 plast = pbest;
743         }
744
745         return pfirst;
746 }
747
748 float TeamScore_GetCompareValue(float t)
749 {
750         float s;
751         entity sk;
752
753         if(t <= 0 || t >= 16)
754         {
755                 if(game_stopped)
756                         return 0;
757                 error("Reading score of invalid team!");
758         }
759
760         sk = teamscorekeepers[t - 1];
761         if (!sk)
762                 return -999999999;
763         s = sk.teamscores_primary;
764         if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
765                 if(!s)
766                         return -999999999;
767         if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
768                 s = -s;
769         return s;
770 }
771
772 const float NAMEWIDTH = 22;
773 const float SCORESWIDTH = 58;
774 // TODO put this somewhere in common?
775 string Score_NicePrint_ItemColor(float vflags)
776 {
777         if(vflags & SFL_SORT_PRIO_PRIMARY)
778                 return "^3";
779         else if(vflags & SFL_SORT_PRIO_SECONDARY)
780                 return "^5";
781         else
782                 return "^7";
783 }
784
785 void Score_NicePrint_Team(entity to, float t, float w)
786 {
787         string s, s2;
788         float i;
789         entity sk;
790         float fl, sc;
791         s = "";
792
793         sk = teamscorekeepers[t - 1];
794         if(sk)
795         {
796                 s = strcat(s, Team_ColoredFullName(t));
797                 for(i = 0; i < MAX_TEAMSCORE; ++i)
798                         if(teamscores_label(i) != "")
799                         {
800                                 fl = teamscores_flags(i);
801                                 sc = sk.(teamscores(i));
802                                 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
803                         }
804         }
805         else
806                 s = "Scores:";
807
808         s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
809
810         FOREACH(Scores, true, {
811                 if(scores_label(it) != "")
812                 {
813                         fl = scores_flags(it);
814                         s2 = scores_label(it);
815                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
816                 }
817     });
818
819         print_to(to, s);
820 }
821
822 void Score_NicePrint_Player(entity to, entity p, float w)
823 {
824         string s;
825         float i;
826         entity sk;
827         float fl, sc;
828         s = "  ";
829
830         sk = p.scorekeeper;
831
832         s = strcat(s, playername(p, false));
833         for (;;)
834         {
835                 i = strlennocol(s) - NAMEWIDTH;
836                 if(i > 0)
837                         s = substring(s, 0, strlen(s) - i);
838                 else
839                 {
840                         s = strcat(s, strpad(i, ""));
841                         break;
842                 }
843         }
844
845         FOREACH(Scores, true, {
846                 if(scores_label(it) != "")
847                 {
848                         fl = scores_flags(it);
849                         sc = sk.(scores(it));
850                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
851                 }
852     });
853
854         print_to(to, s);
855 }
856
857 void Score_NicePrint_Spectators(entity to)
858 {
859         print_to(to, "Spectators:");
860 }
861
862 void Score_NicePrint_Spectator(entity to, entity p)
863 {
864         print_to(to, strcat("  ", playername(p, false)));
865 }
866
867 .float score_dummyfield;
868 void Score_NicePrint(entity to)
869 {
870         entity p;
871         float w;
872
873         int t = 0;
874         FOREACH(Scores, true, {
875                 if(scores_label(it) != "")
876                         ++t;
877     });
878         w = bound(6, floor(SCORESWIDTH / t - 1), 9);
879
880         p = PlayerScore_Sort(score_dummyfield, 1, 1, 0);
881         t = -1;
882
883         if(!teamscores_entities_count)
884                 Score_NicePrint_Team(to, t, w);
885         while(p)
886         {
887                 if(teamscores_entities_count)
888                         if(t != p.team)
889                                 Score_NicePrint_Team(to, p.team, w);
890                 Score_NicePrint_Player(to, p, w);
891                 t = p.team;
892                 p = p.chain;
893         }
894
895         t = 0;
896         FOREACH_CLIENT(!IS_PLAYER(it), LAMBDA(
897                 if (!t)
898                         Score_NicePrint_Spectators(to);
899                 Score_NicePrint_Spectator(to, it);
900                 t = 1;
901         ));
902 }
903
904 void PlayerScore_PlayerStats(entity p)
905 {
906         entity s = p.scorekeeper;
907         FOREACH(Scores, true, {
908                 if(s.(scores(it)) != 0)
909                         if(scores_label(it) != "")
910                                 PS_GR_P_ADDVAL(s.owner, strcat(PLAYERSTATS_SCOREBOARD, scores_label(it)), s.(scores(it)));
911     });
912 }
913
914 void PlayerScore_TeamStats()
915 {
916         entity sk;
917         float t, i;
918         for(t = 0; t < 16; ++t)
919         {
920                 sk = teamscorekeepers[t];
921                 if(!sk)
922                         continue;
923                 for(i = 0; i < MAX_TEAMSCORE; ++i)
924                         if(sk.(teamscores(i)) != 0)
925                                 if(teamscores_label(i) != "")
926                                         // the +1 is important here!
927                                         PS_GR_T_ADDVAL(t+1, strcat(PLAYERSTATS_SCOREBOARD, teamscores_label(i)), sk.(teamscores(i)));
928         }
929 }