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