]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/scores.qc
Merge remote branch 'origin/terencehill/powerups_respawntime_fix'
[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_cts) return; // in CTS, you don't lose score by observing
258         if(g_race && g_race_qualifying) return; // in qualifying, you don't lose score by observing
259
260         sk = player.scorekeeper;
261         for(i = 0; i < MAX_SCORE; ++i)
262         {
263                 if(sk.(scores[i]) != 0)
264                         if(scores_label[i] != "")
265                                 sk.SendFlags |= pow(2, i);
266                 sk.(scores[i]) = 0;
267         }
268 }
269
270 void Score_ClearAll()
271 {
272         entity p, sk;
273         float i, t;
274         FOR_EACH_CLIENTSLOT(p)
275         {
276                 sk = p.scorekeeper;
277                 if(!sk)
278                         continue;
279                 for(i = 0; i < MAX_SCORE; ++i)
280                 {
281                         if(sk.(scores[i]) != 0)
282                                 if(scores_label[i] != "")
283                                         sk.SendFlags |= pow(2, i);
284                         sk.(scores[i]) = 0;
285                 }
286         }
287         for(t = 0; t < 16; ++t)
288         {
289                 sk = teamscorekeepers[t];
290                 if(!sk)
291                         continue;
292                 for(i = 0; i < MAX_TEAMSCORE; ++i)
293                 {
294                         if(sk.(teamscores[i]) != 0)
295                                 if(teamscores_label[i] != "")
296                                         sk.SendFlags |= pow(2, i);
297                         sk.(teamscores[i]) = 0;
298                 }
299         }
300 }
301
302 void PlayerScore_Attach(entity player)
303 {
304         entity sk;
305         if(player.scorekeeper)
306                 error("player already has a scorekeeper");
307         sk = spawn();
308         sk.owner = player;
309         Net_LinkEntity(sk, FALSE, 0, PlayerScore_SendEntity);
310         player.scorekeeper = sk;
311 }
312
313 void PlayerScore_Detach(entity player)
314 {
315         if(!player.scorekeeper)
316                 error("player has no scorekeeper");
317         remove(player.scorekeeper);
318         player.scorekeeper = world;
319 }
320
321 float PlayerScore_Add(entity player, float scorefield, float score)
322 {
323         entity s;
324
325         if(gameover)
326         if not(g_lms && scorefield == SP_LMS_RANK) // allow writing to this field in intermission as it is needed for newly joining players
327                 score = 0;
328
329         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
330         s = player.scorekeeper;
331         if(!s)
332         {
333                 if(gameover)
334                         return 0;
335                 backtrace("Adding score to unknown player!");
336                 return 0;
337         }
338         if(score)
339                 if(scores_label[scorefield] != "")
340                         s.SendFlags |= pow(2, scorefield);
341         if(!inWarmupStage)
342                 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_TOTAL, scores_label[scorefield]), score);
343         return (s.(scores[scorefield]) += score);
344 }
345
346 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
347 {
348         float r;
349         r = PlayerScore_Add(player, pscorefield, score);
350         if(teamscores_entities_count) // only for teamplay
351                 r = TeamScore_Add(player, tscorefield, score);
352         return r;
353 }
354
355 float PlayerScore_Compare(entity t1, entity t2)
356 {
357         if(!t1 || !t2) return (!t2) - !t1;
358
359         vector result;
360         float i;
361         for(i = 0; i < MAX_SCORE; ++i)
362         {
363                 var .float f;
364                 f = scores[i];
365                 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
366         }
367         return result_x;
368 }
369
370 void WinningConditionHelper()
371 {
372         float c;
373         string s;
374         entity p;
375         float fullstatus;
376         entity winnerscorekeeper;
377         entity secondscorekeeper;
378         entity sk;
379
380         // format:
381         // gametype:P<pure>:S<slots>::plabel,plabel:tlabel,tlabel:teamid:tscore,tscore:teamid:tscore,tscore
382         // score labels always start with a symbol or with lower case
383         // so to match pure, match for :P0:
384         // to match full, match for :S0:
385
386         s = GetGametype();
387         s = strcat(s, ":", autocvar_g_xonoticversion);
388         s = strcat(s, ":P", ftos(cvar_purechanges_count));
389         s = strcat(s, ":S", ftos(nJoinAllowed(world)));
390         s = strcat(s, ":F", ftos(serverflags));
391         s = strcat(s, ":M", modname);
392         s = strcat(s, "::", GetPlayerScoreString(world, 1)); // make this 1 once we can, note: this doesn't contain any :<letter>
393
394         fullstatus = autocvar_g_full_getstatus_responses;
395
396         if(teamscores_entities_count)
397         {
398                 float t;
399
400                 s = strcat(s, ":", GetTeamScoreString(0, 1));
401                 for(t = 0; t < 16; ++t)
402                         if(teamscorekeepers[t])
403                                 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
404
405                 WinningConditionHelper_winnerteam = -1;
406                 WinningConditionHelper_secondteam = -1;
407                 winnerscorekeeper = world;
408                 secondscorekeeper = world;
409                 for(t = 0; t < 16; ++t)
410                 {
411                         sk = teamscorekeepers[t];
412                         c = TeamScore_Compare(winnerscorekeeper, sk);
413                         if(c < 0)
414                         {
415                                 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
416                                 WinningConditionHelper_winnerteam = t + 1;
417                                 secondscorekeeper = winnerscorekeeper;
418                                 winnerscorekeeper = sk;
419                         }
420                         else
421                         {
422                                 c = TeamScore_Compare(secondscorekeeper, sk);
423                                 if(c < 0)
424                                 {
425                                         WinningConditionHelper_secondteam = t + 1;
426                                         secondscorekeeper = sk;
427                                 }
428                         }
429                 }
430
431                 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
432                 if(WinningConditionHelper_equality)
433                         WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
434
435                 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
436                 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
437                 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
438                 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
439
440                 WinningConditionHelper_winner = world; // not supported in teamplay
441                 WinningConditionHelper_second = world; // not supported in teamplay
442         }
443         else
444         {
445                 WinningConditionHelper_winner = world;
446                 WinningConditionHelper_second = world;
447                 winnerscorekeeper = world;
448                 secondscorekeeper = world;
449                 FOR_EACH_PLAYER(p)
450                 {
451                         sk = p.scorekeeper;
452                         c = PlayerScore_Compare(winnerscorekeeper, sk);
453                         if(c < 0)
454                         {
455                                 WinningConditionHelper_second = WinningConditionHelper_winner;
456                                 WinningConditionHelper_winner = p;
457                                 secondscorekeeper = winnerscorekeeper;
458                                 winnerscorekeeper = sk;
459                         }
460                         else
461                         {
462                                 c = PlayerScore_Compare(secondscorekeeper, sk);
463                                 if(c < 0)
464                                 {
465                                         WinningConditionHelper_second = p;
466                                         secondscorekeeper = sk;
467                                 }
468                         }
469                 }
470
471                 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
472                 if(WinningConditionHelper_equality)
473                         WinningConditionHelper_winner = WinningConditionHelper_second = world;
474
475                 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
476                 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
477                 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
478                 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
479
480                 WinningConditionHelper_winnerteam = -1; // no teamplay
481                 WinningConditionHelper_secondteam = -1; // no teamplay
482         }
483
484         if(WinningConditionHelper_topscore == 0)
485         {
486                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
487                 {
488                         if(WinningConditionHelper_lowerisbetter)
489                                 WinningConditionHelper_topscore = 999999999;
490                         else
491                                 WinningConditionHelper_topscore = -999999999;
492                 }
493                 WinningConditionHelper_equality = 0;
494         }
495
496         if(WinningConditionHelper_secondscore == 0)
497         {
498                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
499                 {
500                         if(WinningConditionHelper_lowerisbetter)
501                                 WinningConditionHelper_secondscore = 999999999;
502                         else
503                                 WinningConditionHelper_secondscore = -999999999;
504                 }
505         }
506
507         if(worldstatus)
508                 strunzone(worldstatus);
509         worldstatus = strzone(s);
510
511         FOR_EACH_CLIENT(p)
512         {
513                 if(fullstatus)
514                 {
515                         s = GetPlayerScoreString(p, 1);
516                         if(clienttype(p) == CLIENTTYPE_REAL)
517                                 s = strcat(s, ":human");
518                         else
519                                 s = strcat(s, ":bot");
520                         if(p.classname != "player" && !g_arena && !g_ca && !g_lms)
521                                 s = strcat(s, ":spectator");
522                 }
523                 else
524                 {
525                         if(p.classname == "player" || g_arena || g_ca || g_lms)
526                                 s = GetPlayerScoreString(p, 2);
527                         else
528                                 s = "-666";
529                 }
530
531                 if(p.clientstatus)
532                         strunzone(p.clientstatus);
533                 p.clientstatus = strzone(s);
534         }
535 }
536
537 string GetScoreLogLabel(string label, float fl)
538 {
539         if(fl & SFL_LOWER_IS_BETTER)
540                 label = strcat(label, "<");
541         if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
542                 label = strcat(label, "!!");
543         else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
544                 label = strcat(label, "!");
545         return label;
546 }
547
548 string GetPlayerScoreString(entity pl, float shortString)
549 {
550         string out;
551         entity sk;
552         float i, f;
553         string l;
554
555         out = "";
556         if(!pl)
557         {
558                 // label
559                 for(i = 0; i < MAX_SCORE; ++i)
560                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
561                         {
562                                 f = scores_flags[i];
563                                 l = scores_label[i];
564                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
565                         }
566                 if(shortString < 2)
567                 for(i = 0; i < MAX_SCORE; ++i)
568                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
569                         {
570                                 f = scores_flags[i];
571                                 l = scores_label[i];
572                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
573                         }
574                 if(shortString < 1)
575                 for(i = 0; i < MAX_SCORE; ++i)
576                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
577                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
578                         {
579                                 f = scores_flags[i];
580                                 l = scores_label[i];
581                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
582                         }
583                 out = substring(out, 0, strlen(out) - 1);
584         }
585         else if((sk = pl.scorekeeper))
586         {
587                 for(i = 0; i < MAX_SCORE; ++i)
588                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
589                                 out = strcat(out, ftos(sk.(scores[i])), ",");
590                 if(shortString < 2)
591                 for(i = 0; i < MAX_SCORE; ++i)
592                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
593                                 out = strcat(out, ftos(sk.(scores[i])), ",");
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                                 out = strcat(out, ftos(sk.(scores[i])), ",");
599                 out = substring(out, 0, strlen(out) - 1);
600         }
601         return out;
602 }
603
604 string GetTeamScoreString(float tm, float shortString)
605 {
606         string out;
607         entity sk;
608         float i, f;
609         string l;
610
611         out = "";
612         if(tm == 0)
613         {
614                 // label
615                 for(i = 0; i < MAX_SCORE; ++i)
616                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
617                         {
618                                 f = teamscores_flags[i];
619                                 l = teamscores_label[i];
620                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
621                         }
622                 if(shortString < 2)
623                 for(i = 0; i < MAX_SCORE; ++i)
624                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
625                         {
626                                 f = teamscores_flags[i];
627                                 l = teamscores_label[i];
628                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
629                         }
630                 if(shortString < 1)
631                 for(i = 0; i < MAX_SCORE; ++i)
632                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
633                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
634                         {
635                                 f = teamscores_flags[i];
636                                 l = teamscores_label[i];
637                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
638                         }
639                 out = substring(out, 0, strlen(out) - 1);
640         }
641         else if((sk = teamscorekeepers[tm - 1]))
642         {
643                 for(i = 0; i < MAX_TEAMSCORE; ++i)
644                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
645                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
646                 if(shortString < 2)
647                 for(i = 0; i < MAX_TEAMSCORE; ++i)
648                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
649                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
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                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
655                 out = substring(out, 0, strlen(out) - 1);
656         }
657         return out;
658 }
659
660 float PlayerTeamScore_Compare(entity p1, entity p2)
661 {
662         if(teamscores_entities_count)
663                 if(p1.team != p2.team)
664                 {
665                         entity t1, t2;
666                         float r;
667                         t1 = teamscorekeepers[p1.team - 1];
668                         t2 = teamscorekeepers[p2.team - 1];
669                         r = TeamScore_Compare(t1, t2);
670                         if(r == 0) // ensure a deterministic order
671                                 r = p1.team - p2.team;
672                         return r;
673                 }
674         
675         return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
676 }
677
678 entity PlayerScore_Sort(.float field)
679 {
680         entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
681         float i;
682
683         plist = world;
684
685         FOR_EACH_CLIENT(p)
686                 p.field = 0;
687
688         FOR_EACH_PLAYER(p) if(p.scorekeeper)
689         {
690                 p.chain = plist;
691                 plist = p;
692         }
693         // Now plist points to the whole list.
694         
695         pfirst = plast = world;
696
697         i = 0;
698         while(plist)
699         {
700                 pprev = pbestprev = world;
701                 pbest = plist;
702                 for(p = plist; (pprev = p), (p = p.chain); )
703                 {
704                         if(PlayerTeamScore_Compare(p, pbest) > 0)
705                         {
706                                 pbest = p;
707                                 pbestprev = pprev;
708                         }
709                 }
710
711                 // remove pbest out of the chain
712                 if(pbestprev == world)
713                         plist = pbest.chain;
714                 else
715                         pbestprev.chain = pbest.chain;
716                 pbest.chain = world;
717
718                 pbest.field = ++i;
719
720                 if not(pfirst)
721                         pfirst = pbest;
722                 if(plast)
723                         plast.chain = pbest;
724                 plast = pbest;
725         }
726
727         return pfirst;
728 }
729
730 float TeamScore_GetCompareValue(float t)
731 {
732         float s;
733         entity sk;
734
735         if(t <= 0 || t >= 16)
736         {
737                 if(gameover)
738                         return 0;
739                 error("Reading score of invalid team!");
740         }
741
742         sk = teamscorekeepers[t - 1];
743         if not(sk)
744                 return -999999999;
745         s = sk.teamscores_primary;
746         if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
747                 if(!s)
748                         return -999999999;
749         if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
750                 s = -s;
751         return s;
752 }
753
754 #define NAMEWIDTH 22
755 #define SCORESWIDTH 58
756 // TODO put this somewhere in common?
757 string Score_NicePrint_ItemColor(float vflags)
758 {
759         if(vflags & SFL_SORT_PRIO_PRIMARY)
760                 return "^3";
761         else if(vflags & SFL_SORT_PRIO_SECONDARY)
762                 return "^5";
763         else
764                 return "^7";
765 }
766
767 void Score_NicePrint_Team(entity to, float t, float w)
768 {
769         string s, s2;
770         float i;
771         entity sk;
772         float fl, sc;
773         s = "";
774
775         sk = teamscorekeepers[t - 1];
776         if(sk)
777         {
778                 s = strcat(s, ColoredTeamName(t));
779                 for(i = 0; i < MAX_TEAMSCORE; ++i)
780                         if(teamscores_label[i] != "")
781                         {
782                                 fl = teamscores_flags[i];
783                                 sc = sk.(teamscores[i]);
784                                 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
785                         }
786         }
787         else
788                 s = "Scores:";
789
790         s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
791         
792         for(i = 0; i < MAX_SCORE; ++i)
793                 if(scores_label[i] != "")
794                 {
795                         fl = scores_flags[i];
796                         s2 = scores_label[i];
797                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
798                 }
799
800         print_to(to, s);
801 }
802
803 void Score_NicePrint_Player(entity to, entity p, float w)
804 {
805         string s;
806         float i;
807         entity sk;
808         float fl, sc;
809         s = "  ";
810
811         sk = p.scorekeeper;
812
813         s = strcat(s, p.netname);
814         for(;;)
815         {
816                 i = strlennocol(s) - NAMEWIDTH;
817                 if(i > 0)
818                         s = substring(s, 0, strlen(s) - i);
819                 else
820                 {
821                         s = strcat(s, strpad(i, ""));
822                         break;
823                 }
824         }
825         
826         for(i = 0; i < MAX_SCORE; ++i)
827                 if(scores_label[i] != "")
828                 {
829                         fl = scores_flags[i];
830                         sc = sk.(scores[i]);
831                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
832                 }
833
834         print_to(to, s);
835 }
836
837 void Score_NicePrint_Spectators(entity to)
838 {
839         print_to(to, "Spectators:");
840 }
841
842 void Score_NicePrint_Spectator(entity to, entity p)
843 {
844         print_to(to, strcat("  ", p.netname));
845 }
846
847 .float score_dummyfield;
848 void Score_NicePrint(entity to)
849 {
850         entity p;
851         float t, i;
852         float w;
853
854         t = 0;
855         for(i = 0; i < MAX_SCORE; ++i)
856                 if(scores_label[i] != "")
857                         ++t;
858         w = bound(6, floor(SCORESWIDTH / t - 1), 9);
859
860         p = PlayerScore_Sort(score_dummyfield);
861         t = -1;
862
863         if(!teamscores_entities_count)
864                 Score_NicePrint_Team(to, t, w);
865         while(p)
866         {
867                 if(teamscores_entities_count)
868                         if(t != p.team)
869                                 Score_NicePrint_Team(to, p.team, w);
870                 Score_NicePrint_Player(to, p, w);
871                 t = p.team;
872                 p = p.chain;
873         }
874         
875         t = 0;
876         FOR_EACH_CLIENT(p)
877         if(p.classname != "player")
878         {
879                 if not(t)
880                         Score_NicePrint_Spectators(to);
881                 Score_NicePrint_Spectator(to, p);
882                 t = 1;
883         }
884 }
885
886 void PlayerScore_PlayerStats(entity p)
887 {
888         entity s;
889         float i;
890         s = p.scorekeeper;
891
892         for(i = 0; i < MAX_SCORE; ++i)
893                 if(s.(scores[i]) != 0)
894                         if(scores_label[i] != "")
895                                 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_SCOREBOARD, scores_label[i]), s.(scores[i]));
896 }
897
898 void PlayerScore_TeamStats(void)
899 {
900         entity sk;
901         float t, i;
902         for(t = 0; t < 16; ++t)
903         {
904                 sk = teamscorekeepers[t];
905                 if(!sk)
906                         continue;
907                 for(i = 0; i < MAX_TEAMSCORE; ++i)
908                         if(sk.(teamscores[i]) != 0)
909                                 if(teamscores_label[i] != "")
910                                         PlayerStats_TeamScore(t, strcat(PLAYERSTATS_SCOREBOARD, teamscores_label[i]), sk.(teamscores[i]));
911         }
912 }