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