]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/scores.qc
Added PlayerScore_Get function.
[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                 if(scores_label(scorefield) != "")
344                         s.SendFlags |= (2 ** (scorefield.m_id % 16));
345         if(!warmup_stage)
346                 PS_GR_P_ADDVAL(s.owner, strcat(PLAYERSTATS_TOTAL, scores_label(scorefield)), score);
347         return (s.(scores(scorefield)) += score);
348 }
349
350 float PlayerScore_Get(entity player, PlayerScoreField scorefield)
351 {
352         return player.scorekeeper.scores(scorefield);
353 }
354
355 float PlayerTeamScore_Add(entity player, PlayerScoreField pscorefield, float tscorefield, float score)
356 {
357         float r;
358         r = PlayerScore_Add(player, pscorefield, score);
359         if(teamscores_entities_count) // only for teamplay
360                 r = TeamScore_Add(player, tscorefield, score);
361         return r;
362 }
363
364 float PlayerScore_Compare(entity t1, entity t2, float strict)
365 {
366         if(!t1 || !t2) return (!t2) - !t1;
367
368         vector result = '0 0 0';
369         FOREACH(Scores, true, {
370                 var .float f = scores(it);
371                 result = ScoreField_Compare(t1, t2, f, scores_flags(it), result, strict);
372         });
373
374         if (result.x == 0 && strict)
375                 result.x = etof(t1.owner) - etof(t2.owner);
376
377         return result.x;
378 }
379
380 void WinningConditionHelper(entity this)
381 {
382         float c;
383         string s;
384         float fullstatus;
385         entity winnerscorekeeper;
386         entity secondscorekeeper;
387         entity sk;
388
389         // format:
390         // gametype:P<pure>:S<slots>::plabel,plabel:tlabel,tlabel:teamid:tscore,tscore:teamid:tscore,tscore
391         // score labels always start with a symbol or with lower case
392         // so to match pure, match for :P0:
393         // to match full, match for :S0:
394
395         fullstatus = autocvar_g_full_getstatus_responses;
396
397         s = GetGametype();
398         s = strcat(s, ":", autocvar_g_xonoticversion);
399         s = strcat(s, ":P", ftos(cvar_purechanges_count));
400         s = strcat(s, ":S", ftos(nJoinAllowed(this, NULL)));
401         s = strcat(s, ":F", ftos(serverflags));
402         s = strcat(s, ":M", modname);
403         s = strcat(s, "::", GetPlayerScoreString(NULL, (fullstatus ? 1 : 2)));
404
405         if(teamscores_entities_count)
406         {
407                 float t;
408
409                 s = strcat(s, ":", GetTeamScoreString(0, 1));
410                 for(t = 0; t < 16; ++t)
411                         if(teamscorekeepers[t])
412                                 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
413
414                 WinningConditionHelper_winnerteam = -1;
415                 WinningConditionHelper_secondteam = -1;
416                 winnerscorekeeper = NULL;
417                 secondscorekeeper = NULL;
418                 for(t = 0; t < 16; ++t)
419                 {
420                         sk = teamscorekeepers[t];
421                         c = TeamScore_Compare(winnerscorekeeper, sk, 1);
422                         if(c < 0)
423                         {
424                                 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
425                                 WinningConditionHelper_winnerteam = t + 1;
426                                 secondscorekeeper = winnerscorekeeper;
427                                 winnerscorekeeper = sk;
428                         }
429                         else
430                         {
431                                 c = TeamScore_Compare(secondscorekeeper, sk, 1);
432                                 if(c < 0)
433                                 {
434                                         WinningConditionHelper_secondteam = t + 1;
435                                         secondscorekeeper = sk;
436                                 }
437                         }
438                 }
439
440                 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper, 0) == 0);
441                 if(WinningConditionHelper_equality)
442                         WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
443
444                 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
445                 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
446                 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
447                 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
448
449                 WinningConditionHelper_winner = NULL; // not supported in teamplay
450                 WinningConditionHelper_second = NULL; // not supported in teamplay
451         }
452         else
453         {
454                 WinningConditionHelper_winner = NULL;
455                 WinningConditionHelper_second = NULL;
456                 winnerscorekeeper = NULL;
457                 secondscorekeeper = NULL;
458                 FOREACH_CLIENT(IS_PLAYER(it), LAMBDA(
459                         sk = it.scorekeeper;
460                         c = PlayerScore_Compare(winnerscorekeeper, sk, 1);
461                         if(c < 0)
462                         {
463                                 WinningConditionHelper_second = WinningConditionHelper_winner;
464                                 WinningConditionHelper_winner = it;
465                                 secondscorekeeper = winnerscorekeeper;
466                                 winnerscorekeeper = sk;
467                         }
468                         else
469                         {
470                                 c = PlayerScore_Compare(secondscorekeeper, sk, 1);
471                                 if(c < 0)
472                                 {
473                                         WinningConditionHelper_second = it;
474                                         secondscorekeeper = sk;
475                                 }
476                         }
477                 ));
478
479                 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper, 0) == 0);
480                 if(WinningConditionHelper_equality)
481                         WinningConditionHelper_winner = WinningConditionHelper_second = NULL;
482
483                 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
484                 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
485                 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
486                 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
487
488                 WinningConditionHelper_winnerteam = -1; // no teamplay
489                 WinningConditionHelper_secondteam = -1; // no teamplay
490         }
491
492         if(WinningConditionHelper_topscore == 0)
493         {
494                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
495                 {
496                         if(WinningConditionHelper_lowerisbetter)
497                                 WinningConditionHelper_topscore = 999999999;
498                         else
499                                 WinningConditionHelper_topscore = -999999999;
500                 }
501                 WinningConditionHelper_equality = 0;
502         }
503
504         if(WinningConditionHelper_secondscore == 0)
505         {
506                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
507                 {
508                         if(WinningConditionHelper_lowerisbetter)
509                                 WinningConditionHelper_secondscore = 999999999;
510                         else
511                                 WinningConditionHelper_secondscore = -999999999;
512                 }
513         }
514
515         if(worldstatus)
516                 strunzone(worldstatus);
517         worldstatus = strzone(s);
518
519         FOREACH_CLIENT(true, LAMBDA(
520                 string s = "";
521                 if(fullstatus)
522                 {
523                         s = GetPlayerScoreString(it, 1);
524                         s = strcat(s, IS_REAL_CLIENT(it) ? ":human" : ":bot");
525                         if(!IS_PLAYER(it) && !MUTATOR_CALLHOOK(GetPlayerStatus, it))
526                                 s = strcat(s, ":spectator");
527                 }
528                 else
529                 {
530                         if (IS_PLAYER(it) || MUTATOR_CALLHOOK(GetPlayerStatus, it))
531                                 s = GetPlayerScoreString(it, 2);
532                         else
533                                 s = "-666";
534                 }
535
536                 if(it.clientstatus)
537                         strunzone(it.clientstatus);
538                 it.clientstatus = strzone(s);
539         ));
540 }
541
542 string GetScoreLogLabel(string label, float fl)
543 {
544         if(fl & SFL_LOWER_IS_BETTER)
545                 label = strcat(label, "<");
546         if((fl & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
547                 label = strcat(label, "!!");
548         else if((fl & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
549                 label = strcat(label, "!");
550         return label;
551 }
552
553 string GetPlayerScoreString(entity pl, float shortString)
554 {
555         string out;
556         entity sk;
557         float f;
558         string l;
559
560         out = "";
561         if(!pl)
562         {
563                 // label
564                 FOREACH(Scores, true, {
565                         if ((scores_flags(it) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
566                         {
567                                 f = scores_flags(it);
568                                 l = scores_label(it);
569                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
570                         }
571         });
572                 if(shortString < 2)
573                 FOREACH(Scores, true, {
574                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
575                         {
576                                 f = scores_flags(it);
577                                 l = scores_label(it);
578                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
579                         }
580         });
581                 if(shortString < 1)
582                 FOREACH(Scores, true, {
583                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
584                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
585                         {
586                                 f = scores_flags(it);
587                                 l = scores_label(it);
588                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
589                         }
590         });
591                 out = substring(out, 0, strlen(out) - 1);
592         }
593         else if((sk = pl.scorekeeper))
594         {
595                 FOREACH(Scores, true, {
596                         if ((scores_flags(it) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
597                                 out = strcat(out, ftos(sk.(scores(it))), ",");
598         });
599                 if(shortString < 2)
600                 FOREACH(Scores, true, {
601                         if ((scores_flags(it) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
602                                 out = strcat(out, ftos(sk.(scores(it))), ",");
603         });
604                 if(shortString < 1)
605                 FOREACH(Scores, true, {
606                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
607                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
608                                 out = strcat(out, ftos(sk.(scores(it))), ",");
609         });
610                 out = substring(out, 0, strlen(out) - 1);
611         }
612         return out;
613 }
614
615 string GetTeamScoreString(float tm, float shortString)
616 {
617         string out;
618         entity sk;
619         float i, f;
620         string l;
621
622         out = "";
623         if(tm == 0)
624         {
625                 // label
626                 for(i = 0; i < MAX_TEAMSCORE; ++i)
627                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
628                         {
629                                 f = teamscores_flags(i);
630                                 l = teamscores_label(i);
631                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
632                         }
633                 if(shortString < 2)
634                 for(i = 0; i < MAX_TEAMSCORE; ++i)
635                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
636                         {
637                                 f = teamscores_flags(i);
638                                 l = teamscores_label(i);
639                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
640                         }
641                 if(shortString < 1)
642                 for(i = 0; i < MAX_TEAMSCORE; ++i)
643                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
644                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
645                         {
646                                 f = teamscores_flags(i);
647                                 l = teamscores_label(i);
648                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
649                         }
650                 out = substring(out, 0, strlen(out) - 1);
651         }
652         else if((sk = teamscorekeepers[tm - 1]))
653         {
654                 for(i = 0; i < MAX_TEAMSCORE; ++i)
655                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
656                                 out = strcat(out, ftos(sk.(teamscores(i))), ",");
657                 if(shortString < 2)
658                 for(i = 0; i < MAX_TEAMSCORE; ++i)
659                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
660                                 out = strcat(out, ftos(sk.(teamscores(i))), ",");
661                 if(shortString < 1)
662                 for(i = 0; i < MAX_TEAMSCORE; ++i)
663                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
664                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
665                                 out = strcat(out, ftos(sk.(teamscores(i))), ",");
666                 out = substring(out, 0, strlen(out) - 1);
667         }
668         return out;
669 }
670
671 float PlayerTeamScore_Compare(entity p1, entity p2, float teams, float strict)
672 {
673         if(teams && teamscores_entities_count)
674         {
675                 if(p1.team != p2.team)
676                 {
677                         entity t1, t2;
678                         float r;
679                         t1 = teamscorekeepers[p1.team - 1];
680                         t2 = teamscorekeepers[p2.team - 1];
681                         r = TeamScore_Compare(t1, t2, ((teams >= 0) ? 1 : strict));
682                         return r;
683                 }
684                 if(teams < 0)
685                         return 0;
686         }
687
688         return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper, strict);
689 }
690
691 entity PlayerScore_Sort(.float field, float teams, float strict, float nospectators)
692 {
693         entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
694         float i, j;
695
696         plist = NULL;
697
698         FOREACH_CLIENT(true, LAMBDA(it.(field) = 0));
699
700         FOREACH_CLIENT(it.scorekeeper,
701         {
702                 if(nospectators)
703                         if(it.frags == FRAGS_SPECTATOR)
704                                 continue;
705
706                 it.chain = plist;
707                 plist = it;
708         });
709         // Now plist points to the whole list.
710
711         pfirst = plast = NULL;
712
713         i = j = 0;
714         while(plist)
715         {
716                 pprev = pbestprev = NULL;
717                 pbest = plist;
718                 for(p = plist; (pprev = p), (p = p.chain); )
719                 {
720                         if(PlayerTeamScore_Compare(p, pbest, teams, strict) > 0)
721                         {
722                                 pbest = p;
723                                 pbestprev = pprev;
724                         }
725                 }
726
727                 // remove pbest out of the chain
728                 if(pbestprev == NULL)
729                         plist = pbest.chain;
730                 else
731                         pbestprev.chain = pbest.chain;
732                 pbest.chain = NULL;
733
734                 ++i;
735                 if(!plast || PlayerTeamScore_Compare(plast, pbest, teams, 0))
736                         j = i;
737
738                 pbest.(field) = j;
739
740                 if (!pfirst)
741                         pfirst = pbest;
742                 if(plast)
743                         plast.chain = pbest;
744                 plast = pbest;
745         }
746
747         return pfirst;
748 }
749
750 float TeamScore_GetCompareValue(float t)
751 {
752         float s;
753         entity sk;
754
755         if(t <= 0 || t >= 16)
756         {
757                 if(game_stopped)
758                         return 0;
759                 error("Reading score of invalid team!");
760         }
761
762         sk = teamscorekeepers[t - 1];
763         if (!sk)
764                 return -999999999;
765         s = sk.teamscores_primary;
766         if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
767                 if(!s)
768                         return -999999999;
769         if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
770                 s = -s;
771         return s;
772 }
773
774 const float NAMEWIDTH = 22;
775 const float SCORESWIDTH = 58;
776 // TODO put this somewhere in common?
777 string Score_NicePrint_ItemColor(float vflags)
778 {
779         if(vflags & SFL_SORT_PRIO_PRIMARY)
780                 return "^3";
781         else if(vflags & SFL_SORT_PRIO_SECONDARY)
782                 return "^5";
783         else
784                 return "^7";
785 }
786
787 void Score_NicePrint_Team(entity to, float t, float w)
788 {
789         string s, s2;
790         float i;
791         entity sk;
792         float fl, sc;
793         s = "";
794
795         sk = teamscorekeepers[t - 1];
796         if(sk)
797         {
798                 s = strcat(s, Team_ColoredFullName(t));
799                 for(i = 0; i < MAX_TEAMSCORE; ++i)
800                         if(teamscores_label(i) != "")
801                         {
802                                 fl = teamscores_flags(i);
803                                 sc = sk.(teamscores(i));
804                                 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
805                         }
806         }
807         else
808                 s = "Scores:";
809
810         s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
811
812         FOREACH(Scores, true, {
813                 if(scores_label(it) != "")
814                 {
815                         fl = scores_flags(it);
816                         s2 = scores_label(it);
817                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
818                 }
819     });
820
821         print_to(to, s);
822 }
823
824 void Score_NicePrint_Player(entity to, entity p, float w)
825 {
826         string s;
827         float i;
828         entity sk;
829         float fl, sc;
830         s = "  ";
831
832         sk = p.scorekeeper;
833
834         s = strcat(s, playername(p, false));
835         for (;;)
836         {
837                 i = strlennocol(s) - NAMEWIDTH;
838                 if(i > 0)
839                         s = substring(s, 0, strlen(s) - i);
840                 else
841                 {
842                         s = strcat(s, strpad(i, ""));
843                         break;
844                 }
845         }
846
847         FOREACH(Scores, true, {
848                 if(scores_label(it) != "")
849                 {
850                         fl = scores_flags(it);
851                         sc = sk.(scores(it));
852                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
853                 }
854     });
855
856         print_to(to, s);
857 }
858
859 void Score_NicePrint_Spectators(entity to)
860 {
861         print_to(to, "Spectators:");
862 }
863
864 void Score_NicePrint_Spectator(entity to, entity p)
865 {
866         print_to(to, strcat("  ", playername(p, false)));
867 }
868
869 .float score_dummyfield;
870 void Score_NicePrint(entity to)
871 {
872         entity p;
873         float w;
874
875         int t = 0;
876         FOREACH(Scores, true, {
877                 if(scores_label(it) != "")
878                         ++t;
879     });
880         w = bound(6, floor(SCORESWIDTH / t - 1), 9);
881
882         p = PlayerScore_Sort(score_dummyfield, 1, 1, 0);
883         t = -1;
884
885         if(!teamscores_entities_count)
886                 Score_NicePrint_Team(to, t, w);
887         while(p)
888         {
889                 if(teamscores_entities_count)
890                         if(t != p.team)
891                                 Score_NicePrint_Team(to, p.team, w);
892                 Score_NicePrint_Player(to, p, w);
893                 t = p.team;
894                 p = p.chain;
895         }
896
897         t = 0;
898         FOREACH_CLIENT(!IS_PLAYER(it), LAMBDA(
899                 if (!t)
900                         Score_NicePrint_Spectators(to);
901                 Score_NicePrint_Spectator(to, it);
902                 t = 1;
903         ));
904 }
905
906 void PlayerScore_PlayerStats(entity p)
907 {
908         entity s = p.scorekeeper;
909         FOREACH(Scores, true, {
910                 if(s.(scores(it)) != 0)
911                         if(scores_label(it) != "")
912                                 PS_GR_P_ADDVAL(s.owner, strcat(PLAYERSTATS_SCOREBOARD, scores_label(it)), s.(scores(it)));
913     });
914 }
915
916 void PlayerScore_TeamStats()
917 {
918         entity sk;
919         float t, i;
920         for(t = 0; t < 16; ++t)
921         {
922                 sk = teamscorekeepers[t];
923                 if(!sk)
924                         continue;
925                 for(i = 0; i < MAX_TEAMSCORE; ++i)
926                         if(sk.(teamscores(i)) != 0)
927                                 if(teamscores_label(i) != "")
928                                         // the +1 is important here!
929                                         PS_GR_T_ADDVAL(t+1, strcat(PLAYERSTATS_SCOREBOARD, teamscores_label(i)), sk.(teamscores(i)));
930         }
931 }