]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/scores.qc
Merge branch 'master' into Mario/bulldozer
[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         WriteHeader(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         WriteHeader(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         WriteHeader(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                 string s = "";
533                 if(fullstatus)
534                 {
535                         s = GetPlayerScoreString(p, 1);
536                         if(IS_REAL_CLIENT(p))
537                                 s = strcat(s, ":human");
538                         else
539                                 s = strcat(s, ":bot");
540                         if(!IS_PLAYER(p) && !MUTATOR_CALLHOOK(GetPlayerStatus, p, s))
541                                 s = strcat(s, ":spectator");
542                         s = strcat(s, ret_string);
543                 }
544                 else
545                 {
546                         if(IS_PLAYER(p) || MUTATOR_CALLHOOK(GetPlayerStatus, p, s))
547                                 s = GetPlayerScoreString(p, 2);
548                         else
549                                 s = "-666";
550                         s = strcat(s, ret_string);
551                 }
552
553                 if(p.clientstatus)
554                         strunzone(p.clientstatus);
555                 p.clientstatus = strzone(s);
556         }
557 }
558
559 string GetScoreLogLabel(string label, float fl)
560 {
561         if(fl & SFL_LOWER_IS_BETTER)
562                 label = strcat(label, "<");
563         if((fl & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
564                 label = strcat(label, "!!");
565         else if((fl & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
566                 label = strcat(label, "!");
567         return label;
568 }
569
570 string GetPlayerScoreString(entity pl, float shortString)
571 {
572         string out;
573         entity sk;
574         float i, f;
575         string l;
576
577         out = "";
578         if(!pl)
579         {
580                 // label
581                 for(i = 0; i < MAX_SCORE; ++i)
582                         if((scores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
583                         {
584                                 f = scores_flags[i];
585                                 l = scores_label[i];
586                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
587                         }
588                 if(shortString < 2)
589                 for(i = 0; i < MAX_SCORE; ++i)
590                         if((scores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
591                         {
592                                 f = scores_flags[i];
593                                 l = scores_label[i];
594                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
595                         }
596                 if(shortString < 1)
597                 for(i = 0; i < MAX_SCORE; ++i)
598                         if((scores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
599                         if((scores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
600                         {
601                                 f = scores_flags[i];
602                                 l = scores_label[i];
603                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
604                         }
605                 out = substring(out, 0, strlen(out) - 1);
606         }
607         else if((sk = pl.scorekeeper))
608         {
609                 for(i = 0; i < MAX_SCORE; ++i)
610                         if((scores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
611                                 out = strcat(out, ftos(sk.(scores[i])), ",");
612                 if(shortString < 2)
613                 for(i = 0; i < MAX_SCORE; ++i)
614                         if((scores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
615                                 out = strcat(out, ftos(sk.(scores[i])), ",");
616                 if(shortString < 1)
617                 for(i = 0; i < MAX_SCORE; ++i)
618                         if((scores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
619                         if((scores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
620                                 out = strcat(out, ftos(sk.(scores[i])), ",");
621                 out = substring(out, 0, strlen(out) - 1);
622         }
623         return out;
624 }
625
626 string GetTeamScoreString(float tm, float shortString)
627 {
628         string out;
629         entity sk;
630         float i, f;
631         string l;
632
633         out = "";
634         if(tm == 0)
635         {
636                 // label
637                 for(i = 0; i < MAX_TEAMSCORE; ++i)
638                         if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
639                         {
640                                 f = teamscores_flags[i];
641                                 l = teamscores_label[i];
642                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
643                         }
644                 if(shortString < 2)
645                 for(i = 0; i < MAX_TEAMSCORE; ++i)
646                         if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
647                         {
648                                 f = teamscores_flags[i];
649                                 l = teamscores_label[i];
650                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
651                         }
652                 if(shortString < 1)
653                 for(i = 0; i < MAX_TEAMSCORE; ++i)
654                         if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
655                         if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
656                         {
657                                 f = teamscores_flags[i];
658                                 l = teamscores_label[i];
659                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
660                         }
661                 out = substring(out, 0, strlen(out) - 1);
662         }
663         else if((sk = teamscorekeepers[tm - 1]))
664         {
665                 for(i = 0; i < MAX_TEAMSCORE; ++i)
666                         if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
667                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
668                 if(shortString < 2)
669                 for(i = 0; i < MAX_TEAMSCORE; ++i)
670                         if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
671                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
672                 if(shortString < 1)
673                 for(i = 0; i < MAX_TEAMSCORE; ++i)
674                         if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
675                         if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
676                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
677                 out = substring(out, 0, strlen(out) - 1);
678         }
679         return out;
680 }
681
682 float PlayerTeamScore_Compare(entity p1, entity p2, float teams, float strict)
683 {
684         if(teams && teamscores_entities_count)
685         {
686                 if(p1.team != p2.team)
687                 {
688                         entity t1, t2;
689                         float r;
690                         t1 = teamscorekeepers[p1.team - 1];
691                         t2 = teamscorekeepers[p2.team - 1];
692                         r = TeamScore_Compare(t1, t2, ((teams >= 0) ? 1 : strict));
693                         return r;
694                 }
695                 if(teams < 0)
696                         return 0;
697         }
698
699         return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper, strict);
700 }
701
702 entity PlayerScore_Sort(.float field, float teams, float strict, float nospectators)
703 {
704         entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
705         float i, j;
706
707         plist = world;
708
709         FOR_EACH_CLIENT(p)
710                 p.(field) = 0;
711
712         FOR_EACH_CLIENT(p) if(p.scorekeeper)
713         {
714                 if(nospectators)
715                         if(p.frags == FRAGS_SPECTATOR)
716                                 continue;
717
718                 p.chain = plist;
719                 plist = p;
720         }
721         // Now plist points to the whole list.
722
723         pfirst = plast = world;
724
725         i = j = 0;
726         while(plist)
727         {
728                 pprev = pbestprev = world;
729                 pbest = plist;
730                 for(p = plist; (pprev = p), (p = p.chain); )
731                 {
732                         if(PlayerTeamScore_Compare(p, pbest, teams, strict) > 0)
733                         {
734                                 pbest = p;
735                                 pbestprev = pprev;
736                         }
737                 }
738
739                 // remove pbest out of the chain
740                 if(pbestprev == world)
741                         plist = pbest.chain;
742                 else
743                         pbestprev.chain = pbest.chain;
744                 pbest.chain = world;
745
746                 ++i;
747                 if(!plast || PlayerTeamScore_Compare(plast, pbest, teams, 0))
748                         j = i;
749
750                 pbest.(field) = j;
751
752                 if (!pfirst)
753                         pfirst = pbest;
754                 if(plast)
755                         plast.chain = pbest;
756                 plast = pbest;
757         }
758
759         return pfirst;
760 }
761
762 float TeamScore_GetCompareValue(float t)
763 {
764         float s;
765         entity sk;
766
767         if(t <= 0 || t >= 16)
768         {
769                 if(gameover)
770                         return 0;
771                 error("Reading score of invalid team!");
772         }
773
774         sk = teamscorekeepers[t - 1];
775         if (!sk)
776                 return -999999999;
777         s = sk.teamscores_primary;
778         if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
779                 if(!s)
780                         return -999999999;
781         if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
782                 s = -s;
783         return s;
784 }
785
786 const float NAMEWIDTH = 22;
787 const float SCORESWIDTH = 58;
788 // TODO put this somewhere in common?
789 string Score_NicePrint_ItemColor(float vflags)
790 {
791         if(vflags & SFL_SORT_PRIO_PRIMARY)
792                 return "^3";
793         else if(vflags & SFL_SORT_PRIO_SECONDARY)
794                 return "^5";
795         else
796                 return "^7";
797 }
798
799 void Score_NicePrint_Team(entity to, float t, float w)
800 {
801         string s, s2;
802         float i;
803         entity sk;
804         float fl, sc;
805         s = "";
806
807         sk = teamscorekeepers[t - 1];
808         if(sk)
809         {
810                 s = strcat(s, Team_ColoredFullName(t));
811                 for(i = 0; i < MAX_TEAMSCORE; ++i)
812                         if(teamscores_label[i] != "")
813                         {
814                                 fl = teamscores_flags[i];
815                                 sc = sk.(teamscores[i]);
816                                 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
817                         }
818         }
819         else
820                 s = "Scores:";
821
822         s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
823
824         for(i = 0; i < MAX_SCORE; ++i)
825                 if(scores_label[i] != "")
826                 {
827                         fl = scores_flags[i];
828                         s2 = scores_label[i];
829                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
830                 }
831
832         print_to(to, s);
833 }
834
835 void Score_NicePrint_Player(entity to, entity p, float w)
836 {
837         string s;
838         float i;
839         entity sk;
840         float fl, sc;
841         s = "  ";
842
843         sk = p.scorekeeper;
844
845         s = strcat(s, p.netname);
846         for (;;)
847         {
848                 i = strlennocol(s) - NAMEWIDTH;
849                 if(i > 0)
850                         s = substring(s, 0, strlen(s) - i);
851                 else
852                 {
853                         s = strcat(s, strpad(i, ""));
854                         break;
855                 }
856         }
857
858         for(i = 0; i < MAX_SCORE; ++i)
859                 if(scores_label[i] != "")
860                 {
861                         fl = scores_flags[i];
862                         sc = sk.(scores[i]);
863                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
864                 }
865
866         print_to(to, s);
867 }
868
869 void Score_NicePrint_Spectators(entity to)
870 {
871         print_to(to, "Spectators:");
872 }
873
874 void Score_NicePrint_Spectator(entity to, entity p)
875 {
876         print_to(to, strcat("  ", p.netname));
877 }
878
879 .float score_dummyfield;
880 void Score_NicePrint(entity to)
881 {
882         entity p;
883         float t, i;
884         float w;
885
886         t = 0;
887         for(i = 0; i < MAX_SCORE; ++i)
888                 if(scores_label[i] != "")
889                         ++t;
890         w = bound(6, floor(SCORESWIDTH / t - 1), 9);
891
892         p = PlayerScore_Sort(score_dummyfield, 1, 1, 0);
893         t = -1;
894
895         if(!teamscores_entities_count)
896                 Score_NicePrint_Team(to, t, w);
897         while(p)
898         {
899                 if(teamscores_entities_count)
900                         if(t != p.team)
901                                 Score_NicePrint_Team(to, p.team, w);
902                 Score_NicePrint_Player(to, p, w);
903                 t = p.team;
904                 p = p.chain;
905         }
906
907         t = 0;
908         FOR_EACH_CLIENT(p)
909         if (!IS_PLAYER(p))
910         {
911                 if (!t)
912                         Score_NicePrint_Spectators(to);
913                 Score_NicePrint_Spectator(to, p);
914                 t = 1;
915         }
916 }
917
918 void PlayerScore_PlayerStats(entity p)
919 {
920         entity s;
921         float i;
922         s = p.scorekeeper;
923
924         for(i = 0; i < MAX_SCORE; ++i)
925                 if(s.(scores[i]) != 0)
926                         if(scores_label[i] != "")
927                                 PS_GR_P_ADDVAL(s.owner, strcat(PLAYERSTATS_SCOREBOARD, scores_label[i]), s.(scores[i]));
928 }
929
930 void PlayerScore_TeamStats()
931 {
932         entity sk;
933         float t, i;
934         for(t = 0; t < 16; ++t)
935         {
936                 sk = teamscorekeepers[t];
937                 if(!sk)
938                         continue;
939                 for(i = 0; i < MAX_TEAMSCORE; ++i)
940                         if(sk.(teamscores[i]) != 0)
941                                 if(teamscores_label[i] != "")
942                                         // the +1 is important here!
943                                         PS_GR_T_ADDVAL(t+1, strcat(PLAYERSTATS_SCOREBOARD, teamscores_label[i]), sk.(teamscores[i]));
944         }
945 }