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