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