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