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