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