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