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