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