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