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