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