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