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