]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/server/scores.qc
try to fix vehicle chase cam (untested)
[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         if(!inWarmupStage)
341                 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_TOTAL, scores_label[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, ":F", ftos(serverflags));
390         s = strcat(s, "::", GetPlayerScoreString(world, 1)); // make this 1 once we can, note: this doesn't contain any :<letter>
391
392         fullstatus = autocvar_g_full_getstatus_responses;
393
394         if(teamscores_entities_count)
395         {
396                 float t;
397
398                 s = strcat(s, ":", GetTeamScoreString(0, 1));
399                 for(t = 0; t < 16; ++t)
400                         if(teamscorekeepers[t])
401                                 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
402
403                 WinningConditionHelper_winnerteam = -1;
404                 WinningConditionHelper_secondteam = -1;
405                 winnerscorekeeper = world;
406                 secondscorekeeper = world;
407                 for(t = 0; t < 16; ++t)
408                 {
409                         sk = teamscorekeepers[t];
410                         c = TeamScore_Compare(winnerscorekeeper, sk);
411                         if(c < 0)
412                         {
413                                 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
414                                 WinningConditionHelper_winnerteam = t + 1;
415                                 secondscorekeeper = winnerscorekeeper;
416                                 winnerscorekeeper = sk;
417                         }
418                         else
419                         {
420                                 c = TeamScore_Compare(secondscorekeeper, sk);
421                                 if(c < 0)
422                                 {
423                                         WinningConditionHelper_secondteam = t + 1;
424                                         secondscorekeeper = sk;
425                                 }
426                         }
427                 }
428
429                 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
430                 if(WinningConditionHelper_equality)
431                         WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
432
433                 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
434                 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
435                 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
436                 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
437
438                 WinningConditionHelper_winner = world; // not supported in teamplay
439                 WinningConditionHelper_second = world; // not supported in teamplay
440         }
441         else
442         {
443                 WinningConditionHelper_winner = world;
444                 WinningConditionHelper_second = world;
445                 winnerscorekeeper = world;
446                 secondscorekeeper = world;
447                 FOR_EACH_PLAYER(p)
448                 {
449                         sk = p.scorekeeper;
450                         c = PlayerScore_Compare(winnerscorekeeper, sk);
451                         if(c < 0)
452                         {
453                                 WinningConditionHelper_second = WinningConditionHelper_winner;
454                                 WinningConditionHelper_winner = p;
455                                 secondscorekeeper = winnerscorekeeper;
456                                 winnerscorekeeper = sk;
457                         }
458                         else
459                         {
460                                 c = PlayerScore_Compare(secondscorekeeper, sk);
461                                 if(c < 0)
462                                 {
463                                         WinningConditionHelper_second = p;
464                                         secondscorekeeper = sk;
465                                 }
466                         }
467                 }
468
469                 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
470                 if(WinningConditionHelper_equality)
471                         WinningConditionHelper_winner = WinningConditionHelper_second = world;
472
473                 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
474                 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
475                 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
476                 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
477
478                 WinningConditionHelper_winnerteam = -1; // no teamplay
479                 WinningConditionHelper_secondteam = -1; // no teamplay
480         }
481
482         if(WinningConditionHelper_topscore == 0)
483         {
484                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
485                 {
486                         if(WinningConditionHelper_lowerisbetter)
487                                 WinningConditionHelper_topscore = 999999999;
488                         else
489                                 WinningConditionHelper_topscore = -999999999;
490                 }
491                 WinningConditionHelper_equality = 0;
492         }
493
494         if(WinningConditionHelper_secondscore == 0)
495         {
496                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
497                 {
498                         if(WinningConditionHelper_lowerisbetter)
499                                 WinningConditionHelper_secondscore = 999999999;
500                         else
501                                 WinningConditionHelper_secondscore = -999999999;
502                 }
503         }
504
505         if(worldstatus)
506                 strunzone(worldstatus);
507         worldstatus = strzone(s);
508
509         FOR_EACH_CLIENT(p)
510         {
511                 if(fullstatus)
512                 {
513                         s = GetPlayerScoreString(p, 1);
514                         if(clienttype(p) == CLIENTTYPE_REAL)
515                                 s = strcat(s, ":human");
516                         else
517                                 s = strcat(s, ":bot");
518                         if(p.classname != "player" && !g_arena && !g_ca && !g_lms)
519                                 s = strcat(s, ":spectator");
520                 }
521                 else
522                 {
523                         if(p.classname == "player" || g_arena || g_ca || g_lms)
524                                 s = GetPlayerScoreString(p, 2);
525                         else
526                                 s = "-666";
527                 }
528
529                 if(p.clientstatus)
530                         strunzone(p.clientstatus);
531                 p.clientstatus = strzone(s);
532         }
533 }
534
535 string GetScoreLogLabel(string label, float fl)
536 {
537         if(fl & SFL_LOWER_IS_BETTER)
538                 label = strcat(label, "<");
539         if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
540                 label = strcat(label, "!!");
541         else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
542                 label = strcat(label, "!");
543         return label;
544 }
545
546 string GetPlayerScoreString(entity pl, float shortString)
547 {
548         string out;
549         entity sk;
550         float i, f;
551         string l;
552
553         out = "";
554         if(!pl)
555         {
556                 // label
557                 for(i = 0; i < MAX_SCORE; ++i)
558                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
559                         {
560                                 f = scores_flags[i];
561                                 l = scores_label[i];
562                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
563                         }
564                 if(shortString < 2)
565                 for(i = 0; i < MAX_SCORE; ++i)
566                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
567                         {
568                                 f = scores_flags[i];
569                                 l = scores_label[i];
570                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
571                         }
572                 if(shortString < 1)
573                 for(i = 0; i < MAX_SCORE; ++i)
574                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
575                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
576                         {
577                                 f = scores_flags[i];
578                                 l = scores_label[i];
579                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
580                         }
581                 out = substring(out, 0, strlen(out) - 1);
582         }
583         else if((sk = pl.scorekeeper))
584         {
585                 for(i = 0; i < MAX_SCORE; ++i)
586                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
587                                 out = strcat(out, ftos(sk.(scores[i])), ",");
588                 if(shortString < 2)
589                 for(i = 0; i < MAX_SCORE; ++i)
590                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
591                                 out = strcat(out, ftos(sk.(scores[i])), ",");
592                 if(shortString < 1)
593                 for(i = 0; i < MAX_SCORE; ++i)
594                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
595                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
596                                 out = strcat(out, ftos(sk.(scores[i])), ",");
597                 out = substring(out, 0, strlen(out) - 1);
598         }
599         return out;
600 }
601
602 string GetTeamScoreString(float tm, float shortString)
603 {
604         string out;
605         entity sk;
606         float i, f;
607         string l;
608
609         out = "";
610         if(tm == 0)
611         {
612                 // label
613                 for(i = 0; i < MAX_SCORE; ++i)
614                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
615                         {
616                                 f = teamscores_flags[i];
617                                 l = teamscores_label[i];
618                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
619                         }
620                 if(shortString < 2)
621                 for(i = 0; i < MAX_SCORE; ++i)
622                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
623                         {
624                                 f = teamscores_flags[i];
625                                 l = teamscores_label[i];
626                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
627                         }
628                 if(shortString < 1)
629                 for(i = 0; i < MAX_SCORE; ++i)
630                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
631                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
632                         {
633                                 f = teamscores_flags[i];
634                                 l = teamscores_label[i];
635                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
636                         }
637                 out = substring(out, 0, strlen(out) - 1);
638         }
639         else if((sk = teamscorekeepers[tm - 1]))
640         {
641                 for(i = 0; i < MAX_TEAMSCORE; ++i)
642                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
643                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
644                 if(shortString < 2)
645                 for(i = 0; i < MAX_TEAMSCORE; ++i)
646                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
647                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
648                 if(shortString < 1)
649                 for(i = 0; i < MAX_TEAMSCORE; ++i)
650                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
651                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
652                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
653                 out = substring(out, 0, strlen(out) - 1);
654         }
655         return out;
656 }
657
658 float PlayerTeamScore_Compare(entity p1, entity p2)
659 {
660         if(teamscores_entities_count)
661                 if(p1.team != p2.team)
662                 {
663                         entity t1, t2;
664                         float r;
665                         t1 = teamscorekeepers[p1.team - 1];
666                         t2 = teamscorekeepers[p2.team - 1];
667                         r = TeamScore_Compare(t1, t2);
668                         if(r == 0) // ensure a deterministic order
669                                 r = p1.team - p2.team;
670                         return r;
671                 }
672         
673         return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
674 }
675
676 entity PlayerScore_Sort(.float field)
677 {
678         entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
679         float i;
680
681         plist = world;
682
683         FOR_EACH_CLIENT(p)
684                 p.field = 0;
685
686         FOR_EACH_PLAYER(p) if(p.scorekeeper)
687         {
688                 p.chain = plist;
689                 plist = p;
690         }
691         // Now plist points to the whole list.
692         
693         pfirst = plast = world;
694
695         i = 0;
696         while(plist)
697         {
698                 pprev = pbestprev = world;
699                 pbest = plist;
700                 for(p = plist; (pprev = p), (p = p.chain); )
701                 {
702                         if(PlayerTeamScore_Compare(p, pbest) > 0)
703                         {
704                                 pbest = p;
705                                 pbestprev = pprev;
706                         }
707                 }
708
709                 // remove pbest out of the chain
710                 if(pbestprev == world)
711                         plist = pbest.chain;
712                 else
713                         pbestprev.chain = pbest.chain;
714                 pbest.chain = world;
715
716                 pbest.field = ++i;
717
718                 if not(pfirst)
719                         pfirst = pbest;
720                 if(plast)
721                         plast.chain = pbest;
722                 plast = pbest;
723         }
724
725         return pfirst;
726 }
727
728 float TeamScore_GetCompareValue(float t)
729 {
730         float s;
731         entity sk;
732
733         if(t <= 0 || t >= 16)
734         {
735                 if(gameover)
736                         return 0;
737                 error("Reading score of invalid team!");
738         }
739
740         sk = teamscorekeepers[t - 1];
741         if not(sk)
742                 return -999999999;
743         s = sk.teamscores_primary;
744         if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
745                 if(!s)
746                         return -999999999;
747         if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
748                 s = -s;
749         return s;
750 }
751
752 #define NAMEWIDTH 22
753 #define SCORESWIDTH 58
754 // TODO put this somewhere in common?
755 string Score_NicePrint_ItemColor(float vflags)
756 {
757         if(vflags & SFL_SORT_PRIO_PRIMARY)
758                 return "^3";
759         else if(vflags & SFL_SORT_PRIO_SECONDARY)
760                 return "^5";
761         else
762                 return "^7";
763 }
764
765 void Score_NicePrint_Team(entity to, float t, float w)
766 {
767         string s, s2;
768         float i;
769         entity sk;
770         float fl, sc;
771         s = "";
772
773         sk = teamscorekeepers[t - 1];
774         if(sk)
775         {
776                 s = strcat(s, ColoredTeamName(t));
777                 for(i = 0; i < MAX_TEAMSCORE; ++i)
778                         if(teamscores_label[i] != "")
779                         {
780                                 fl = teamscores_flags[i];
781                                 sc = sk.(teamscores[i]);
782                                 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
783                         }
784         }
785         else
786                 s = "Scores:";
787
788         s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
789         
790         for(i = 0; i < MAX_SCORE; ++i)
791                 if(scores_label[i] != "")
792                 {
793                         fl = scores_flags[i];
794                         s2 = scores_label[i];
795                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
796                 }
797
798         print_to(to, s);
799 }
800
801 void Score_NicePrint_Player(entity to, entity p, float w)
802 {
803         string s;
804         float i;
805         entity sk;
806         float fl, sc;
807         s = "  ";
808
809         sk = p.scorekeeper;
810
811         s = strcat(s, p.netname);
812         for(;;)
813         {
814                 i = strlennocol(s) - NAMEWIDTH;
815                 if(i > 0)
816                         s = substring(s, 0, strlen(s) - i);
817                 else
818                 {
819                         s = strcat(s, strpad(i, ""));
820                         break;
821                 }
822         }
823         
824         for(i = 0; i < MAX_SCORE; ++i)
825                 if(scores_label[i] != "")
826                 {
827                         fl = scores_flags[i];
828                         sc = sk.(scores[i]);
829                         s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
830                 }
831
832         print_to(to, s);
833 }
834
835 void Score_NicePrint_Spectators(entity to)
836 {
837         print_to(to, "Spectators:");
838 }
839
840 void Score_NicePrint_Spectator(entity to, entity p)
841 {
842         print_to(to, strcat("  ", p.netname));
843 }
844
845 .float score_dummyfield;
846 void Score_NicePrint(entity to)
847 {
848         entity p;
849         float t, i;
850         float w;
851
852         t = 0;
853         for(i = 0; i < MAX_SCORE; ++i)
854                 if(scores_label[i] != "")
855                         ++t;
856         w = bound(6, floor(SCORESWIDTH / t - 1), 9);
857
858         p = PlayerScore_Sort(score_dummyfield);
859         t = -1;
860
861         if(!teamscores_entities_count)
862                 Score_NicePrint_Team(to, t, w);
863         while(p)
864         {
865                 if(teamscores_entities_count)
866                         if(t != p.team)
867                                 Score_NicePrint_Team(to, p.team, w);
868                 Score_NicePrint_Player(to, p, w);
869                 t = p.team;
870                 p = p.chain;
871         }
872         
873         t = 0;
874         FOR_EACH_CLIENT(p)
875         if(p.classname != "player")
876         {
877                 if not(t)
878                         Score_NicePrint_Spectators(to);
879                 Score_NicePrint_Spectator(to, p);
880                 t = 1;
881         }
882 }
883
884 void PlayerScore_PlayerStats(entity p)
885 {
886         entity s;
887         float i;
888         s = p.scorekeeper;
889
890         for(i = 0; i < MAX_SCORE; ++i)
891                 if(s.(scores[i]) != 0)
892                         if(scores_label[i] != "")
893                                 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_SCOREBOARD, scores_label[i]), s.(scores[i]));
894 }
895
896 void PlayerScore_TeamStats(void)
897 {
898         entity sk;
899         float t, i;
900         for(t = 0; t < 16; ++t)
901         {
902                 sk = teamscorekeepers[t];
903                 if(!sk)
904                         continue;
905                 for(i = 0; i < MAX_TEAMSCORE; ++i)
906                         if(sk.(teamscores[i]) != 0)
907                                 if(teamscores_label[i] != "")
908                                         PlayerStats_TeamScore(t, strcat(PLAYERSTATS_SCOREBOARD, teamscores_label[i]), sk.(teamscores[i]));
909         }
910 }