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