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