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