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