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