]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/scores.qc
prevent a "modify free entity" warning in the porto
[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, ":", ftos(p.team));
455                         else
456                                 s = strcat(s, ":spectator");
457                 }
458                 else
459                 {
460                         if(p.classname == "player" || g_arena || g_lms)
461                                 s = GetPlayerScoreString(p, 2);
462                         else
463                                 s = "-666";
464                 }
465
466                 if(p.clientstatus)
467                         strunzone(p.clientstatus);
468                 p.clientstatus = strzone(s);
469         }
470 }
471
472 string GetScoreLogLabel(string label, float fl)
473 {
474         if(fl & SFL_LOWER_IS_BETTER)
475                 label = strcat(label, "<");
476         if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
477                 label = strcat(label, "!!");
478         else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
479                 label = strcat(label, "!");
480         return label;
481 }
482
483 string GetPlayerScoreString(entity pl, float shortString)
484 {
485         string out;
486         entity sk;
487         float i, f;
488         string l;
489
490         out = "";
491         if(!pl)
492         {
493                 // label
494                 for(i = 0; i < MAX_SCORE; ++i)
495                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
496                         {
497                                 f = scores_flags[i];
498                                 l = scores_label[i];
499                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
500                         }
501                 if(shortString < 2)
502                 for(i = 0; i < MAX_SCORE; ++i)
503                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
504                         {
505                                 f = scores_flags[i];
506                                 l = scores_label[i];
507                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
508                         }
509                 if(shortString < 1)
510                 for(i = 0; i < MAX_SCORE; ++i)
511                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
512                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
513                         {
514                                 f = scores_flags[i];
515                                 l = scores_label[i];
516                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
517                         }
518                 out = substring(out, 0, strlen(out) - 1);
519         }
520         else if((sk = pl.scorekeeper))
521         {
522                 for(i = 0; i < MAX_SCORE; ++i)
523                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
524                                 out = strcat(out, ftos(sk.(scores[i])), ",");
525                 if(shortString < 2)
526                 for(i = 0; i < MAX_SCORE; ++i)
527                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
528                                 out = strcat(out, ftos(sk.(scores[i])), ",");
529                 if(shortString < 1)
530                 for(i = 0; i < MAX_SCORE; ++i)
531                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
532                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
533                                 out = strcat(out, ftos(sk.(scores[i])), ",");
534                 out = substring(out, 0, strlen(out) - 1);
535         }
536         return out;
537 }
538
539 string GetTeamScoreString(float tm, float shortString)
540 {
541         string out;
542         entity sk;
543         float i, f;
544         string l;
545
546         out = "";
547         if(tm == 0)
548         {
549                 // label
550                 for(i = 0; i < MAX_SCORE; ++i)
551                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
552                         {
553                                 f = teamscores_flags[i];
554                                 l = teamscores_label[i];
555                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
556                         }
557                 if(shortString < 2)
558                 for(i = 0; i < MAX_SCORE; ++i)
559                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
560                         {
561                                 f = teamscores_flags[i];
562                                 l = teamscores_label[i];
563                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
564                         }
565                 if(shortString < 1)
566                 for(i = 0; i < MAX_SCORE; ++i)
567                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
568                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
569                         {
570                                 f = teamscores_flags[i];
571                                 l = teamscores_label[i];
572                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
573                         }
574                 out = substring(out, 0, strlen(out) - 1);
575         }
576         else if((sk = teamscorekeepers[tm - 1]))
577         {
578                 for(i = 0; i < MAX_TEAMSCORE; ++i)
579                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
580                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
581                 if(shortString < 2)
582                 for(i = 0; i < MAX_TEAMSCORE; ++i)
583                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
584                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
585                 if(shortString < 1)
586                 for(i = 0; i < MAX_TEAMSCORE; ++i)
587                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
588                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
589                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
590                 out = substring(out, 0, strlen(out) - 1);
591         }
592         return out;
593 }
594
595 float PlayerTeamScore_Compare(entity p1, entity p2)
596 {
597         if(teamscores_entities_count)
598                 if(p1.team != p2.team)
599                 {
600                         entity t1, t2;
601                         float r;
602                         t1 = teamscorekeepers[p1.team - 1];
603                         t2 = teamscorekeepers[p2.team - 1];
604                         r = TeamScore_Compare(t1, t2);
605                         if(r == 0) // ensure a deterministic order
606                                 r = p1.team - p2.team;
607                         return r;
608                 }
609         
610         return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
611 }
612
613 entity PlayerScore_Sort(.float field)
614 {
615         entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
616         float i;
617
618         plist = world;
619
620         FOR_EACH_CLIENT(p)
621                 p.field = 0;
622
623         FOR_EACH_PLAYER(p) if(p.scorekeeper)
624         {
625                 p.chain = plist;
626                 plist = p;
627         }
628         // Now plist points to the whole list.
629         
630         pfirst = plast = world;
631
632         i = 0;
633         while(plist)
634         {
635                 pprev = pbestprev = world;
636                 pbest = plist;
637                 for(p = plist; (pprev = p), (p = p.chain); )
638                 {
639                         if(PlayerTeamScore_Compare(p, pbest) > 0)
640                         {
641                                 pbest = p;
642                                 pbestprev = pprev;
643                         }
644                 }
645
646                 // remove pbest out of the chain
647                 if(pbestprev == world)
648                         plist = pbest.chain;
649                 else
650                         pbestprev.chain = pbest.chain;
651                 pbest.chain = world;
652
653                 pbest.field = ++i;
654
655                 if not(pfirst)
656                         pfirst = pbest;
657                 if(plast)
658                         plast.chain = pbest;
659                 plast = pbest;
660         }
661
662         return pfirst;
663 }
664
665 float TeamScore_GetCompareValue(float t)
666 {
667         float s;
668         entity sk;
669
670         if(t <= 0 || t >= 16)
671                 error("Reading score of invalid team!");
672
673         sk = teamscorekeepers[t - 1];
674         if not(sk)
675                 return -999999999;
676         s = sk.teamscores_primary;
677         if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
678                 if(!s)
679                         return -999999999;
680         if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
681                 s = -s;
682         return s;
683 }
684
685 #define NAMEWIDTH 22
686 #define SCORESWIDTH 58
687 // TODO put this somewhere in common?
688 string Score_NicePrint_ItemColor(float vflags)
689 {
690         if(vflags & SFL_SORT_PRIO_PRIMARY)
691                 return "^3";
692         else if(vflags & SFL_SORT_PRIO_SECONDARY)
693                 return "^5";
694         else
695                 return "^7";
696 }
697
698 void Score_NicePrint_Team(entity to, float t, float w)
699 {
700         string s, s2;
701         float i;
702         entity sk;
703         float fl, sc;
704         s = "";
705
706         sk = teamscorekeepers[t - 1];
707         if(sk)
708         {
709                 s = strcat(s, ColoredTeamName(t));
710                 for(i = 0; i < MAX_TEAMSCORE; ++i)
711                         if(teamscores_label[i] != "")
712                         {
713                                 fl = teamscores_flags[i];
714                                 sc = sk.(teamscores[i]);
715                                 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
716                         }
717         }
718         else
719                 s = "Scores:";
720
721         s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
722         
723         for(i = 0; i < MAX_SCORE; ++i)
724                 if(scores_label[i] != "")
725                 {
726                         fl = scores_flags[i];
727                         s2 = scores_label[i];
728                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
729                 }
730
731         print_to(to, s);
732 }
733
734 void Score_NicePrint_Player(entity to, entity p, float w)
735 {
736         string s;
737         float i;
738         entity sk;
739         float fl, sc;
740         s = "  ";
741
742         sk = p.scorekeeper;
743
744         s = strcat(s, p.netname);
745         for(;;)
746         {
747                 i = strlennocol(s) - NAMEWIDTH;
748                 if(i > 0)
749                         s = substring(s, 0, strlen(s) - i);
750                 else
751                 {
752                         s = strcat(s, strpad(i, ""));
753                         break;
754                 }
755         }
756         
757         for(i = 0; i < MAX_SCORE; ++i)
758                 if(scores_label[i] != "")
759                 {
760                         fl = scores_flags[i];
761                         sc = sk.(scores[i]);
762                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
763                 }
764
765         print_to(to, s);
766 }
767
768 void Score_NicePrint_Spectators(entity to)
769 {
770         print_to(to, "Spectators:");
771 }
772
773 void Score_NicePrint_Spectator(entity to, entity p)
774 {
775         print_to(to, strcat("  ", p.netname));
776 }
777
778 .float score_dummyfield;
779 void Score_NicePrint(entity to)
780 {
781         entity p;
782         float t, i;
783         float w;
784
785         t = 0;
786         for(i = 0; i < MAX_SCORE; ++i)
787                 if(scores_label[i] != "")
788                         ++t;
789         w = bound(6, floor(SCORESWIDTH / t - 1), 9);
790
791         p = PlayerScore_Sort(score_dummyfield);
792         t = -1;
793
794         if(!teamscores_entities_count)
795                 Score_NicePrint_Team(to, t, w);
796         while(p)
797         {
798                 if(teamscores_entities_count)
799                         if(t != p.team)
800                                 Score_NicePrint_Team(to, p.team, w);
801                 Score_NicePrint_Player(to, p, w);
802                 t = p.team;
803                 p = p.chain;
804         }
805         
806         t = 0;
807         FOR_EACH_CLIENT(p)
808         if(p.classname != "player")
809         {
810                 if not(t)
811                         Score_NicePrint_Spectators(to);
812                 Score_NicePrint_Spectator(to, p);
813                 t = 1;
814         }
815 }
816