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