new parameter "leadlimit", abort the game if someone leads by that amount
[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         entity winnerscorekeeper;
340         entity secondscorekeeper;
341         entity sk;
342
343         s = GetGametype();
344         s = strcat(s, ":", cvar_string("g_nexuizversion"));
345         s = strcat(s, "::", GetPlayerScoreString(world, 2)); // make this 1 once we can
346
347         fullstatus = cvar("g_full_getstatus_responses");
348
349         if(teamscores_entities_count)
350         {
351                 float t;
352
353                 s = strcat(s, ":", GetTeamScoreString(0, 1));
354                 for(t = 0; t < 16; ++t)
355                         if(teamscorekeepers[t])
356                                 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
357
358                 WinningConditionHelper_winnerteam = -1;
359                 WinningConditionHelper_secondteam = -1;
360                 winnerscorekeeper = world;
361                 secondscorekeeper = world;
362                 for(t = 0; t < 16; ++t)
363                 {
364                         sk = teamscorekeepers[t];
365                         c = TeamScore_Compare(winnerscorekeeper, sk);
366                         if(c < 0)
367                         {
368                                 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
369                                 WinningConditionHelper_winnerteam = t + 1;
370                                 secondscorekeeper = winnerscorekeeper;
371                                 winnerscorekeeper = sk;
372                         }
373                         else
374                         {
375                                 c = TeamScore_Compare(secondscorekeeper, sk);
376                                 if(c < 0)
377                                 {
378                                         WinningConditionHelper_secondteam = t + 1;
379                                         secondscorekeeper = sk;
380                                 }
381                         }
382                 }
383
384                 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
385                 if(WinningConditionHelper_equality)
386                         WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
387
388                 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
389                 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
390                 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
391                 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
392
393                 WinningConditionHelper_winner = world; // not supported in teamplay
394                 WinningConditionHelper_second = world; // not supported in teamplay
395         }
396         else
397         {
398                 WinningConditionHelper_winner = world;
399                 WinningConditionHelper_second = world;
400                 winnerscorekeeper = world;
401                 secondscorekeeper = world;
402                 FOR_EACH_PLAYER(p)
403                 {
404                         sk = p.scorekeeper;
405                         c = PlayerScore_Compare(winnerscorekeeper, sk);
406                         if(c < 0)
407                         {
408                                 WinningConditionHelper_second = WinningConditionHelper_winner;
409                                 WinningConditionHelper_winner = p;
410                                 secondscorekeeper = winnerscorekeeper;
411                                 winnerscorekeeper = sk;
412                         }
413                         else
414                         {
415                                 c = PlayerScore_Compare(secondscorekeeper, sk);
416                                 if(c < 0)
417                                 {
418                                         WinningConditionHelper_second = p;
419                                         secondscorekeeper = sk;
420                                 }
421                         }
422                 }
423
424                 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
425                 if(WinningConditionHelper_equality)
426                         WinningConditionHelper_winner = WinningConditionHelper_second = world;
427
428                 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
429                 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
430                 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
431                 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
432
433                 WinningConditionHelper_winnerteam = -1; // no teamplay
434                 WinningConditionHelper_secondteam = -1; // no teamplay
435         }
436
437         if(teamscores_flags_primary & SFL_TIME)
438                 WinningConditionHelper_topscore /= 10;
439
440         if(WinningConditionHelper_topscore == 0)
441         {
442                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
443                 {
444                         if(WinningConditionHelper_lowerisbetter)
445                                 WinningConditionHelper_topscore = 999999999;
446                         else
447                                 WinningConditionHelper_topscore = -999999999;
448                 }
449         }
450
451         if(WinningConditionHelper_secondscore == 0)
452         {
453                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
454                 {
455                         if(WinningConditionHelper_lowerisbetter)
456                                 WinningConditionHelper_secondscore = 999999999;
457                         else
458                                 WinningConditionHelper_secondscore = -999999999;
459                 }
460         }
461
462         if(worldstatus)
463                 strunzone(worldstatus);
464         worldstatus = strzone(s);
465
466         FOR_EACH_CLIENT(p)
467         {
468                 if(fullstatus)
469                 {
470                         s = GetPlayerScoreString(p, 1);
471                         if(clienttype(p) == CLIENTTYPE_REAL)
472                                 s = strcat(s, ":human");
473                         else
474                                 s = strcat(s, ":bot");
475                         if(p.classname != "player" && !g_arena && !g_lms)
476                                 s = strcat(s, ":spectator");
477                 }
478                 else
479                 {
480                         if(p.classname == "player" || g_arena || g_lms)
481                                 s = GetPlayerScoreString(p, 2);
482                         else
483                                 s = "-666";
484                 }
485
486                 if(p.clientstatus)
487                         strunzone(p.clientstatus);
488                 p.clientstatus = strzone(s);
489         }
490 }
491
492 string GetScoreLogLabel(string label, float fl)
493 {
494         if(fl & SFL_LOWER_IS_BETTER)
495                 label = strcat(label, "<");
496         if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
497                 label = strcat(label, "!!");
498         else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
499                 label = strcat(label, "!");
500         return label;
501 }
502
503 string GetPlayerScoreString(entity pl, float shortString)
504 {
505         string out;
506         entity sk;
507         float i, f;
508         string l;
509
510         out = "";
511         if(!pl)
512         {
513                 // label
514                 for(i = 0; i < MAX_SCORE; ++i)
515                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
516                         {
517                                 f = scores_flags[i];
518                                 l = scores_label[i];
519                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
520                         }
521                 if(shortString < 2)
522                 for(i = 0; i < MAX_SCORE; ++i)
523                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
524                         {
525                                 f = scores_flags[i];
526                                 l = scores_label[i];
527                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
528                         }
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                         {
534                                 f = scores_flags[i];
535                                 l = scores_label[i];
536                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
537                         }
538                 out = substring(out, 0, strlen(out) - 1);
539         }
540         else if((sk = pl.scorekeeper))
541         {
542                 for(i = 0; i < MAX_SCORE; ++i)
543                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
544                                 out = strcat(out, ftos(sk.(scores[i])), ",");
545                 if(shortString < 2)
546                 for(i = 0; i < MAX_SCORE; ++i)
547                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
548                                 out = strcat(out, ftos(sk.(scores[i])), ",");
549                 if(shortString < 1)
550                 for(i = 0; i < MAX_SCORE; ++i)
551                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
552                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
553                                 out = strcat(out, ftos(sk.(scores[i])), ",");
554                 out = substring(out, 0, strlen(out) - 1);
555         }
556         return out;
557 }
558
559 string GetTeamScoreString(float tm, float shortString)
560 {
561         string out;
562         entity sk;
563         float i, f;
564         string l;
565
566         out = "";
567         if(tm == 0)
568         {
569                 // label
570                 for(i = 0; i < MAX_SCORE; ++i)
571                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
572                         {
573                                 f = teamscores_flags[i];
574                                 l = teamscores_label[i];
575                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
576                         }
577                 if(shortString < 2)
578                 for(i = 0; i < MAX_SCORE; ++i)
579                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
580                         {
581                                 f = teamscores_flags[i];
582                                 l = teamscores_label[i];
583                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
584                         }
585                 if(shortString < 1)
586                 for(i = 0; i < MAX_SCORE; ++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                         {
590                                 f = teamscores_flags[i];
591                                 l = teamscores_label[i];
592                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
593                         }
594                 out = substring(out, 0, strlen(out) - 1);
595         }
596         else if((sk = teamscorekeepers[tm - 1]))
597         {
598                 for(i = 0; i < MAX_TEAMSCORE; ++i)
599                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
600                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
601                 if(shortString < 2)
602                 for(i = 0; i < MAX_TEAMSCORE; ++i)
603                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
604                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
605                 if(shortString < 1)
606                 for(i = 0; i < MAX_TEAMSCORE; ++i)
607                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
608                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
609                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
610                 out = substring(out, 0, strlen(out) - 1);
611         }
612         return out;
613 }
614
615 float PlayerTeamScore_Compare(entity p1, entity p2)
616 {
617         if(teamscores_entities_count)
618                 if(p1.team != p2.team)
619                 {
620                         entity t1, t2;
621                         float r;
622                         t1 = teamscorekeepers[p1.team - 1];
623                         t2 = teamscorekeepers[p2.team - 1];
624                         r = TeamScore_Compare(t1, t2);
625                         if(r == 0) // ensure a deterministic order
626                                 r = p1.team - p2.team;
627                         return r;
628                 }
629         
630         return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
631 }
632
633 entity PlayerScore_Sort(.float field)
634 {
635         entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
636         float i;
637
638         plist = world;
639
640         FOR_EACH_CLIENT(p)
641                 p.field = 0;
642
643         FOR_EACH_PLAYER(p) if(p.scorekeeper)
644         {
645                 p.chain = plist;
646                 plist = p;
647         }
648         // Now plist points to the whole list.
649         
650         pfirst = plast = world;
651
652         i = 0;
653         while(plist)
654         {
655                 pprev = pbestprev = world;
656                 pbest = plist;
657                 for(p = plist; (pprev = p), (p = p.chain); )
658                 {
659                         if(PlayerTeamScore_Compare(p, pbest) > 0)
660                         {
661                                 pbest = p;
662                                 pbestprev = pprev;
663                         }
664                 }
665
666                 // remove pbest out of the chain
667                 if(pbestprev == world)
668                         plist = pbest.chain;
669                 else
670                         pbestprev.chain = pbest.chain;
671                 pbest.chain = world;
672
673                 pbest.field = ++i;
674
675                 if not(pfirst)
676                         pfirst = pbest;
677                 if(plast)
678                         plast.chain = pbest;
679                 plast = pbest;
680         }
681
682         return pfirst;
683 }
684
685 float TeamScore_GetCompareValue(float t)
686 {
687         float s;
688         entity sk;
689
690         if(t <= 0 || t >= 16)
691                 error("Reading score of invalid team!");
692
693         sk = teamscorekeepers[t - 1];
694         if not(sk)
695                 return -999999999;
696         s = sk.teamscores_primary;
697         if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
698                 if(!s)
699                         return -999999999;
700         if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
701                 s = -s;
702         return s;
703 }
704
705 #define NAMEWIDTH 22
706 #define SCORESWIDTH 58
707 // TODO put this somewhere in common?
708 string Score_NicePrint_ItemColor(float vflags)
709 {
710         if(vflags & SFL_SORT_PRIO_PRIMARY)
711                 return "^3";
712         else if(vflags & SFL_SORT_PRIO_SECONDARY)
713                 return "^5";
714         else
715                 return "^7";
716 }
717
718 void Score_NicePrint_Team(entity to, float t, float w)
719 {
720         string s, s2;
721         float i;
722         entity sk;
723         float fl, sc;
724         s = "";
725
726         sk = teamscorekeepers[t - 1];
727         if(sk)
728         {
729                 s = strcat(s, ColoredTeamName(t));
730                 for(i = 0; i < MAX_TEAMSCORE; ++i)
731                         if(teamscores_label[i] != "")
732                         {
733                                 fl = teamscores_flags[i];
734                                 sc = sk.(teamscores[i]);
735                                 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
736                         }
737         }
738         else
739                 s = "Scores:";
740
741         s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
742         
743         for(i = 0; i < MAX_SCORE; ++i)
744                 if(scores_label[i] != "")
745                 {
746                         fl = scores_flags[i];
747                         s2 = scores_label[i];
748                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
749                 }
750
751         print_to(to, s);
752 }
753
754 void Score_NicePrint_Player(entity to, entity p, float w)
755 {
756         string s;
757         float i;
758         entity sk;
759         float fl, sc;
760         s = "  ";
761
762         sk = p.scorekeeper;
763
764         s = strcat(s, p.netname);
765         for(;;)
766         {
767                 i = strlennocol(s) - NAMEWIDTH;
768                 if(i > 0)
769                         s = substring(s, 0, strlen(s) - i);
770                 else
771                 {
772                         s = strcat(s, strpad(i, ""));
773                         break;
774                 }
775         }
776         
777         for(i = 0; i < MAX_SCORE; ++i)
778                 if(scores_label[i] != "")
779                 {
780                         fl = scores_flags[i];
781                         sc = sk.(scores[i]);
782                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
783                 }
784
785         print_to(to, s);
786 }
787
788 void Score_NicePrint_Spectators(entity to)
789 {
790         print_to(to, "Spectators:");
791 }
792
793 void Score_NicePrint_Spectator(entity to, entity p)
794 {
795         print_to(to, strcat("  ", p.netname));
796 }
797
798 .float score_dummyfield;
799 void Score_NicePrint(entity to)
800 {
801         entity p;
802         float t, i;
803         float w;
804
805         t = 0;
806         for(i = 0; i < MAX_SCORE; ++i)
807                 if(scores_label[i] != "")
808                         ++t;
809         w = bound(6, floor(SCORESWIDTH / t - 1), 9);
810
811         p = PlayerScore_Sort(score_dummyfield);
812         t = -1;
813
814         if(!teamscores_entities_count)
815                 Score_NicePrint_Team(to, t, w);
816         while(p)
817         {
818                 if(teamscores_entities_count)
819                         if(t != p.team)
820                                 Score_NicePrint_Team(to, p.team, w);
821                 Score_NicePrint_Player(to, p, w);
822                 t = p.team;
823                 p = p.chain;
824         }
825         
826         t = 0;
827         FOR_EACH_CLIENT(p)
828         if(p.classname != "player")
829         {
830                 if not(t)
831                         Score_NicePrint_Spectators(to);
832                 Score_NicePrint_Spectator(to, p);
833                 t = 1;
834         }
835 }
836