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