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