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