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