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