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