]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/scores.qc
Remove legacy MOTD centreprint (retaining support for old servers)
[xonotic/xonotic-data.pk3dir.git] / qcsrc / server / scores.qc
1 #include "scores.qh"
2
3 #include <common/mapinfo.qh>
4 #include <common/mutators/base.qh>
5 #include <common/net_linked.qh>
6 #include <common/playerstats.qh>
7 #include <common/scores.qh>
8 #include <common/state.qh>
9 #include <common/stats.qh>
10 #include <common/teams.qh>
11 #include <common/weapons/_all.qh>
12 #include <server/client.qh>
13 #include <server/command/common.qh>
14 #include <server/intermission.qh>
15 #include <server/mutators/_mod.qh>
16 #include <server/round_handler.qh>
17 #include <server/world.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, bool 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 |= BIT(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, bool 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         // prevent sending the welcome message again when score types are sent again because the scoring system has changed
212         // it can happen in some game modes like Race when the qualyfing session ends and the race starts
213         bool welcome_msg_too = (!CS(to) || time < CS(to).jointime + 5);
214         WriteByte(MSG_ENTITY, welcome_msg_too);
215         // welcome message is sent here because it needs to know the gametype
216         if (welcome_msg_too)
217                 SendWelcomemessage_msg_type(to, MSG_ENTITY);
218         return true;
219 }
220
221 void ScoreInfo_Init(int teams)
222 {
223         if(scores_initialized)
224         {
225                 scores_initialized.SendFlags |= 1; // force a resend
226         }
227         else
228         {
229                 scores_initialized = new_pure(ent_client_scoreinfo);
230                 Net_LinkEntity(scores_initialized, false, 0, ScoreInfo_SendEntity);
231         }
232         if(teams & BIT(0))
233                 TeamScore_Spawn(NUM_TEAM_1, "Red");
234         if(teams & BIT(1))
235                 TeamScore_Spawn(NUM_TEAM_2, "Blue");
236         if(teams & BIT(2))
237                 TeamScore_Spawn(NUM_TEAM_3, "Yellow");
238         if(teams & BIT(3))
239                 TeamScore_Spawn(NUM_TEAM_4, "Pink");
240 }
241
242 /*
243  * per-player score entities
244  */
245
246 bool PlayerScore_SendEntity(entity this, entity to, float sendflags)
247 {
248         WriteHeader(MSG_ENTITY, ENT_CLIENT_SCORES);
249         WriteByte(MSG_ENTITY, etof(this.owner));
250
251         int longflags = 0;
252         FOREACH(Scores, true, {
253             int p = 1 << (i % 16);
254                 if (this.(scores(it)) > 127 || this.(scores(it)) <= -128)
255                         longflags |= p;
256     });
257
258         WriteShort(MSG_ENTITY, sendflags);
259         WriteShort(MSG_ENTITY, longflags);
260         FOREACH(Scores, true, {
261             int p = 1 << (i % 16);
262                 if (sendflags & p)
263                 {
264                         if(longflags & p)
265                                 WriteInt24_t(MSG_ENTITY, this.(scores(it)));
266                         else
267                                 WriteChar(MSG_ENTITY, this.(scores(it)));
268                 }
269     });
270
271         return true;
272 }
273
274 float PlayerScore_Clear(entity player)
275 {
276         entity sk;
277
278         if(teamscores_entities_count)
279                 return 0;
280
281         if(MUTATOR_CALLHOOK(ForbidPlayerScore_Clear)) return 0;
282
283         sk = CS(player).scorekeeper;
284         FOREACH(Scores, true, {
285                 if(sk.(scores(it)) != 0)
286                         if(scores_label(it) != "")
287                                 sk.SendFlags |= BIT(i % 16);
288                 if(i != SP_ELO.m_id)
289                         sk.(scores(it)) = 0;
290         });
291
292         return 1;
293 }
294
295 void Score_ClearAll()
296 {
297         entity sk;
298         float t;
299         FOREACH_CLIENTSLOT(true, {
300                 sk = CS(it).scorekeeper;
301                 if (!sk) continue;
302                 FOREACH(Scores, true, {
303                         if(sk.(scores(it)) != 0)
304                                 if(scores_label(it) != "")
305                                         sk.SendFlags |= BIT(i % 16);
306                         if(i != SP_ELO.m_id)
307                                 sk.(scores(it)) = 0;
308                 });
309         });
310         for(t = 0; t < 16; ++t)
311         {
312                 sk = teamscorekeepers[t];
313                 if(!sk)
314                         continue;
315                 for(int j = 0; j < MAX_TEAMSCORE; ++j)
316                 {
317                         if(sk.(teamscores(j)) != 0)
318                                 if(teamscores_label(j) != "")
319                                         sk.SendFlags |= BIT(j);
320                         sk.(teamscores(j)) = 0;
321                 }
322         }
323 }
324
325 void PlayerScore_Attach(entity player)
326 {
327         if(CS(player).scorekeeper)
328                 error("player already has a scorekeeper");
329         entity sk = new_pure(scorekeeper);
330         sk.owner = player;
331         Net_LinkEntity(sk, false, 0, PlayerScore_SendEntity);
332         CS(player).scorekeeper = sk;
333 }
334
335 void PlayerScore_Detach(entity player)
336 {
337         if(!CS(player).scorekeeper)
338                 error("player has no scorekeeper");
339         delete(CS(player).scorekeeper);
340         CS(player).scorekeeper = NULL;
341 }
342
343 float PlayerScore_Add(entity player, PlayerScoreField scorefield, float score)
344 {
345         bool mutator_returnvalue = MUTATOR_CALLHOOK(AddPlayerScore, scorefield, score, player);
346         score = M_ARGV(1, float);
347
348         if(!mutator_returnvalue && game_stopped)
349         {
350                 score = 0;
351         }
352
353         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
354         entity s = CS(player).scorekeeper;
355         if(!s)
356         {
357                 if(game_stopped)
358                         return 0;
359                 LOG_WARN("Adding score to unknown player!");
360                 return 0;
361         }
362         if(!score)
363         {
364                 return s.(scores(scorefield));
365         }
366         if(scores_label(scorefield) != "")
367                 s.SendFlags |= BIT(scorefield.m_id % 16);
368         if(!warmup_stage)
369                 PlayerStats_GameReport_Event_Player(s.owner, strcat(PLAYERSTATS_TOTAL, scores_label(scorefield)), score);
370         s.(scores(scorefield)) += score;
371         MUTATOR_CALLHOOK(AddedPlayerScore, scorefield, score, player);
372         return s.(scores(scorefield));
373 }
374
375 float PlayerScore_Set(entity player, PlayerScoreField scorefield, float score)
376 {
377         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
378         entity s = CS(player).scorekeeper;
379         if(!s)
380         {
381                 if(game_stopped)
382                         return 0;
383                 LOG_WARN("Setting score of unknown player!");
384                 return 0;
385         }
386
387         float oldscore = s.(scores(scorefield));
388         if(oldscore == score)
389                 return oldscore;
390
391         if(scores_label(scorefield) != "")
392                 s.SendFlags |= BIT(scorefield.m_id % 16);
393         s.(scores(scorefield)) = score;
394         return s.(scores(scorefield));
395 }
396
397 float PlayerTeamScore_Add(entity player, PlayerScoreField pscorefield, float tscorefield, float score)
398 {
399         float r;
400         r = PlayerScore_Add(player, pscorefield, score);
401         if(teamscores_entities_count) // only for teamplay
402                 r = TeamScore_Add(player, tscorefield, score);
403         return r;
404 }
405
406 float PlayerScore_Compare(entity t1, entity t2, bool strict)
407 {
408         if(!t1 || !t2) return (!t2) - !t1;
409
410         vector result = '0 0 0';
411         FOREACH(Scores, true, {
412                 var .float f = scores(it);
413                 result = ScoreField_Compare(t1, t2, f, scores_flags(it), result, strict);
414         });
415
416         if (result.x == 0 && strict)
417                 result.x = t1.owner.playerid - t2.owner.playerid;
418
419         return result.x;
420 }
421
422 void WinningConditionHelper(entity this)
423 {
424         float c;
425         string s;
426         float fullstatus;
427         entity winnerscorekeeper;
428         entity secondscorekeeper;
429         entity sk;
430
431         // format:
432         // gametype:P<pure>:S<slots>::plabel,plabel:tlabel,tlabel:teamid:tscore,tscore:teamid:tscore,tscore
433         // score labels always start with a symbol or with lower case
434         // so to match pure, match for :P0:
435         // to match full, match for :S0:
436
437         fullstatus = autocvar_g_full_getstatus_responses;
438
439         s = GetGametype();
440         s = strcat(s, ":", autocvar_g_xonoticversion);
441         s = strcat(s, ":P", ftos(cvar_purechanges_count));
442         s = strcat(s, ":S", ftos(nJoinAllowed(this, NULL)));
443         s = strcat(s, ":F", ftos(serverflags));
444         s = strcat(s, ":T", sv_termsofservice_url_escaped);
445         s = strcat(s, ":M", modname);
446         s = strcat(s, "::", GetPlayerScoreString(NULL, (fullstatus ? 1 : 2)));
447
448         if(teamscores_entities_count)
449         {
450                 float t;
451
452                 s = strcat(s, ":", GetTeamScoreString(0, 1));
453                 for(t = 0; t < 16; ++t)
454                         if(teamscorekeepers[t])
455                                 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
456
457                 WinningConditionHelper_winnerteam = -1;
458                 WinningConditionHelper_secondteam = -1;
459                 winnerscorekeeper = NULL;
460                 secondscorekeeper = NULL;
461                 for(t = 0; t < 16; ++t)
462                 {
463                         sk = teamscorekeepers[t];
464                         c = TeamScore_Compare(winnerscorekeeper, sk, 1);
465                         if(c < 0)
466                         {
467                                 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
468                                 WinningConditionHelper_winnerteam = t + 1;
469                                 secondscorekeeper = winnerscorekeeper;
470                                 winnerscorekeeper = sk;
471                         }
472                         else
473                         {
474                                 c = TeamScore_Compare(secondscorekeeper, sk, 1);
475                                 if(c < 0)
476                                 {
477                                         WinningConditionHelper_secondteam = t + 1;
478                                         secondscorekeeper = sk;
479                                 }
480                         }
481                 }
482
483                 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper, 0) == 0);
484                 if(WinningConditionHelper_equality)
485                         WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
486
487                 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
488                 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
489                 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
490                 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
491
492                 WinningConditionHelper_winner = NULL; // not supported in teamplay
493                 WinningConditionHelper_second = NULL; // not supported in teamplay
494         }
495         else
496         {
497                 WinningConditionHelper_winner = NULL;
498                 WinningConditionHelper_second = NULL;
499                 winnerscorekeeper = NULL;
500                 secondscorekeeper = NULL;
501                 FOREACH_CLIENT(IS_PLAYER(it), {
502                         sk = CS(it).scorekeeper;
503                         c = PlayerScore_Compare(winnerscorekeeper, sk, true);
504                         if(c < 0)
505                         {
506                                 WinningConditionHelper_second = WinningConditionHelper_winner;
507                                 WinningConditionHelper_winner = it;
508                                 secondscorekeeper = winnerscorekeeper;
509                                 winnerscorekeeper = sk;
510                         }
511                         else
512                         {
513                                 c = PlayerScore_Compare(secondscorekeeper, sk, true);
514                                 if(c < 0)
515                                 {
516                                         WinningConditionHelper_second = it;
517                                         secondscorekeeper = sk;
518                                 }
519                         }
520                 });
521
522                 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper, false) == 0);
523                 if(WinningConditionHelper_equality)
524                         WinningConditionHelper_winner = WinningConditionHelper_second = NULL;
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                 if(player_count == 0) // special case: empty servers DO end the match at a 0:0 tie
545                         WinningConditionHelper_equality = 0;
546         }
547
548         if(WinningConditionHelper_secondscore == 0)
549         {
550                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
551                 {
552                         if(WinningConditionHelper_lowerisbetter)
553                                 WinningConditionHelper_secondscore = 999999999;
554                         else
555                                 WinningConditionHelper_secondscore = -999999999;
556                 }
557         }
558
559         strcpy(worldstatus, s);
560
561         FOREACH_CLIENT(true, {
562                 string s = "";
563                 if(fullstatus)
564                 {
565                         s = GetPlayerScoreString(it, 1);
566                         s = strcat(s, IS_REAL_CLIENT(it) ? ":human" : ":bot");
567                         if(!(IS_PLAYER(it) || INGAME_JOINED(it)))
568                                 s = strcat(s, ":spectator");
569                 }
570                 else
571                 {
572                         if (IS_PLAYER(it) || INGAME_JOINED(it))
573                                 s = GetPlayerScoreString(it, 2);
574                         else
575                                 s = "-666";
576                 }
577
578                 strcpy(it.clientstatus, s);
579         });
580 }
581
582 string GetScoreLogLabel(string label, float fl)
583 {
584         if(fl & SFL_LOWER_IS_BETTER)
585                 label = strcat(label, "<");
586         if((fl & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
587                 label = strcat(label, "!!");
588         else if((fl & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
589                 label = strcat(label, "!");
590         return label;
591 }
592
593 string GetPlayerScoreString(entity pl, float shortString)
594 {
595         string out;
596         entity sk;
597         float f;
598         string l;
599
600         out = "";
601         if(!pl)
602         {
603                 // label
604                 FOREACH(Scores, true, {
605                         if ((scores_flags(it) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
606                         {
607                                 f = scores_flags(it);
608                                 l = scores_label(it);
609                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
610                         }
611         });
612                 if(shortString < 2)
613                 FOREACH(Scores, true, {
614                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
615                         {
616                                 f = scores_flags(it);
617                                 l = scores_label(it);
618                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
619                         }
620         });
621                 if(shortString < 1)
622                 FOREACH(Scores, true, {
623                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
624                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
625                         {
626                                 f = scores_flags(it);
627                                 l = scores_label(it);
628                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
629                         }
630         });
631                 out = substring(out, 0, strlen(out) - 1);
632         }
633         else if((sk = CS(pl).scorekeeper))
634         {
635                 FOREACH(Scores, true, {
636                         if ((scores_flags(it) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
637                                 out = strcat(out, ftos(sk.(scores(it))), ",");
638         });
639                 if(shortString < 2)
640                 FOREACH(Scores, true, {
641                         if ((scores_flags(it) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
642                                 out = strcat(out, ftos(sk.(scores(it))), ",");
643         });
644                 if(shortString < 1)
645                 FOREACH(Scores, true, {
646                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
647                         if((scores_flags(it) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
648                                 out = strcat(out, ftos(sk.(scores(it))), ",");
649         });
650                 out = substring(out, 0, strlen(out) - 1);
651         }
652         return out;
653 }
654
655 string GetTeamScoreString(float tm, float shortString)
656 {
657         string out;
658         entity sk;
659         float i, f;
660         string l;
661
662         out = "";
663         if(tm == 0)
664         {
665                 // label
666                 for(i = 0; i < MAX_TEAMSCORE; ++i)
667                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
668                         {
669                                 f = teamscores_flags(i);
670                                 l = teamscores_label(i);
671                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
672                         }
673                 if(shortString < 2)
674                 for(i = 0; i < MAX_TEAMSCORE; ++i)
675                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
676                         {
677                                 f = teamscores_flags(i);
678                                 l = teamscores_label(i);
679                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
680                         }
681                 if(shortString < 1)
682                 for(i = 0; i < MAX_TEAMSCORE; ++i)
683                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
684                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
685                         {
686                                 f = teamscores_flags(i);
687                                 l = teamscores_label(i);
688                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
689                         }
690                 out = substring(out, 0, strlen(out) - 1);
691         }
692         else if((sk = teamscorekeepers[tm - 1]))
693         {
694                 for(i = 0; i < MAX_TEAMSCORE; ++i)
695                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
696                                 out = strcat(out, ftos(sk.(teamscores(i))), ",");
697                 if(shortString < 2)
698                 for(i = 0; i < MAX_TEAMSCORE; ++i)
699                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
700                                 out = strcat(out, ftos(sk.(teamscores(i))), ",");
701                 if(shortString < 1)
702                 for(i = 0; i < MAX_TEAMSCORE; ++i)
703                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
704                         if((teamscores_flags(i) & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
705                                 out = strcat(out, ftos(sk.(teamscores(i))), ",");
706                 out = substring(out, 0, strlen(out) - 1);
707         }
708         return out;
709 }
710
711 float PlayerTeamScore_Compare(entity p1, entity p2, float teams, bool strict)
712 {
713         if(teams && teamscores_entities_count)
714         {
715                 if(p1.team != p2.team)
716                 {
717                         entity t1, t2;
718                         float r;
719                         t1 = teamscorekeepers[p1.team - 1];
720                         t2 = teamscorekeepers[p2.team - 1];
721                         r = TeamScore_Compare(t1, t2, ((teams >= 0) ? 1 : strict));
722                         return r;
723                 }
724                 if(teams < 0)
725                         return 0;
726         }
727
728         return PlayerScore_Compare(CS(p1).scorekeeper, CS(p2).scorekeeper, strict);
729 }
730
731 entity PlayerScore_Sort(.float field, int teams, bool strict, bool nospectators)
732 {
733         entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
734         float i, j;
735
736         plist = NULL;
737
738         FOREACH_CLIENT(true, { it.(field) = 0; });
739
740         FOREACH_CLIENT(CS(it).scorekeeper,
741         {
742                 if(nospectators)
743                         if(it.frags == FRAGS_SPECTATOR)
744                                 continue;
745
746                 it.chain = plist;
747                 plist = it;
748         });
749         // Now plist points to the whole list.
750
751         pfirst = plast = NULL;
752
753         i = j = 0;
754         while(plist)
755         {
756                 pprev = pbestprev = NULL;
757                 pbest = plist;
758                 for(p = plist; (pprev = p), (p = p.chain); )
759                 {
760                         if(PlayerTeamScore_Compare(p, pbest, teams, strict) > 0)
761                         {
762                                 pbest = p;
763                                 pbestprev = pprev;
764                         }
765                 }
766
767                 // remove pbest out of the chain
768                 if(pbestprev == NULL)
769                         plist = pbest.chain;
770                 else
771                         pbestprev.chain = pbest.chain;
772                 pbest.chain = NULL;
773
774                 ++i;
775                 if(!plast || PlayerTeamScore_Compare(plast, pbest, teams, strict))
776                         j = i;
777
778                 pbest.(field) = j;
779
780                 if (!pfirst)
781                         pfirst = pbest;
782                 if(plast)
783                         plast.chain = pbest;
784                 plast = pbest;
785         }
786
787         return pfirst;
788 }
789
790 float TeamScore_GetCompareValue(float t)
791 {
792         float s;
793         entity sk;
794
795         if(t <= 0 || t >= 16)
796         {
797                 if(game_stopped)
798                         return 0;
799                 error("Reading score of invalid team!");
800         }
801
802         sk = teamscorekeepers[t - 1];
803         if (!sk)
804                 return -999999999;
805         s = sk.teamscores_primary;
806         if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
807                 if(!s)
808                         return -999999999;
809         if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
810                 s = -s;
811         return s;
812 }
813
814 const float NAMEWIDTH = 22;
815 const float SCORESWIDTH = 58;
816 // TODO put this somewhere in common?
817 string Score_NicePrint_ItemColor(float vflags)
818 {
819         if(vflags & SFL_SORT_PRIO_PRIMARY)
820                 return "^3";
821         else if(vflags & SFL_SORT_PRIO_SECONDARY)
822                 return "^5";
823         else
824                 return "^7";
825 }
826
827 void Score_NicePrint_Team(entity to, float t, float w)
828 {
829         string s, s2;
830         float i;
831         entity sk;
832         float fl, sc;
833         s = "";
834
835         sk = teamscorekeepers[t - 1];
836         if(sk)
837         {
838                 s = strcat(s, Team_ColoredFullName(t));
839                 for(i = 0; i < MAX_TEAMSCORE; ++i)
840                         if(teamscores_label(i) != "")
841                         {
842                                 fl = teamscores_flags(i);
843                                 sc = sk.(teamscores(i));
844                                 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
845                         }
846         }
847         else
848                 s = "Scores:";
849
850         s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
851
852         FOREACH(Scores, true, {
853                 if(scores_label(it) != "")
854                 {
855                         fl = scores_flags(it);
856                         s2 = scores_label(it);
857                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
858                 }
859     });
860
861         print_to(to, s);
862 }
863
864 void Score_NicePrint_Player(entity to, entity p, float w)
865 {
866         string s;
867         float i;
868         entity sk;
869         float fl, sc;
870         s = "  ";
871
872         sk = CS(p).scorekeeper;
873
874         s = strcat(s, playername(p.netname, p.team, false));
875         for (;;)
876         {
877                 i = strlennocol(s) - NAMEWIDTH;
878                 if(i > 0)
879                         s = substring(s, 0, strlen(s) - i);
880                 else
881                 {
882                         s = strcat(s, strpad(i, ""));
883                         break;
884                 }
885         }
886
887         FOREACH(Scores, true, {
888                 if(scores_label(it) != "")
889                 {
890                         fl = scores_flags(it);
891                         sc = sk.(scores(it));
892                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
893                 }
894     });
895
896         print_to(to, s);
897 }
898
899 void Score_NicePrint_Spectators(entity to)
900 {
901         print_to(to, "Spectators:");
902 }
903
904 void Score_NicePrint_Spectator(entity to, entity p)
905 {
906         print_to(to, strcat("  ", playername(p.netname, p.team, false)));
907 }
908
909 .float score_dummyfield;
910 void Score_NicePrint(entity to)
911 {
912         entity p;
913         float w;
914
915         int t = 0;
916         FOREACH(Scores, true, {
917                 if(scores_label(it) != "")
918                         ++t;
919     });
920         w = bound(6, floor(SCORESWIDTH / t - 1), 9);
921
922         p = PlayerScore_Sort(score_dummyfield, 1, true, false);
923         t = -1;
924
925         if(!teamscores_entities_count)
926                 Score_NicePrint_Team(to, t, w);
927         while(p)
928         {
929                 if(teamscores_entities_count)
930                         if(t != p.team)
931                                 Score_NicePrint_Team(to, p.team, w);
932                 Score_NicePrint_Player(to, p, w);
933                 t = p.team;
934                 p = p.chain;
935         }
936
937         t = 0;
938         FOREACH_CLIENT(!IS_PLAYER(it), {
939                 if (!t)
940                         Score_NicePrint_Spectators(to);
941                 Score_NicePrint_Spectator(to, it);
942                 t = 1;
943         });
944 }
945
946 void PlayerScore_PlayerStats(entity p)
947 {
948         entity s = CS(p).scorekeeper;
949         FOREACH(Scores, true, {
950                 if(s.(scores(it)) != 0 && scores_label(it) != "")
951                         PlayerStats_GameReport_Event_Player(s.owner,
952                                 strcat(PLAYERSTATS_SCOREBOARD, scores_label(it)), s.(scores(it)));
953         });
954 }
955
956 void PlayerScore_TeamStats()
957 {
958         entity sk;
959         float t, i;
960         for(t = 0; t < 16; ++t)
961         {
962                 sk = teamscorekeepers[t];
963                 if(!sk)
964                         continue;
965                 for(i = 0; i < MAX_TEAMSCORE; ++i)
966                         if(sk.(teamscores(i)) != 0 && teamscores_label(i) != "")
967                                 // the +1 is important here!
968                                 PlayerStats_GameReport_Event_Team(t+1,
969                                         strcat(PLAYERSTATS_SCOREBOARD, teamscores_label(i)), sk.(teamscores(i)));
970         }
971 }