4 entity teamscorekeepers[16];
5 string scores_label[MAX_SCORE];
6 float scores_flags[MAX_SCORE];
7 string teamscores_label[MAX_TEAMSCORE];
8 float teamscores_flags[MAX_TEAMSCORE];
9 float teamscores_entities_count;
10 var .float scores_primary;
11 var .float teamscores_primary;
12 float scores_flags_primary;
13 float teamscores_flags_primary;
15 vector ScoreField_Compare(entity t1, entity t2, .float field, float fieldflags, vector previous, float strict) // returns: cmp value, best prio
17 if(!strict && !(fieldflags & SFL_SORT_PRIO_MASK)) // column does not sort
19 if((fieldflags & SFL_SORT_PRIO_MASK) < previous.y)
21 if(t1.field == t2.field)
24 previous.y = fieldflags & SFL_SORT_PRIO_MASK;
26 if(fieldflags & SFL_ZERO_IS_WORST)
33 else if(t2.field == 0)
40 if(fieldflags & SFL_LOWER_IS_BETTER)
41 previous.x = (t2.field - t1.field);
43 previous.x = (t1.field - t2.field);
52 float TeamScore_SendEntity(entity to, float sendflags)
54 float i, p, longflags;
56 WriteByte(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
57 WriteByte(MSG_ENTITY, self.team - 1);
60 for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
61 if(self.(teamscores[i]) > 127 || self.(teamscores[i]) <= -128)
64 #if MAX_TEAMSCORE <= 8
65 WriteByte(MSG_ENTITY, sendflags);
66 WriteByte(MSG_ENTITY, longflags);
68 WriteShort(MSG_ENTITY, sendflags);
69 WriteShort(MSG_ENTITY, longflags);
71 for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
75 WriteInt24_t(MSG_ENTITY, self.(teamscores[i]));
77 WriteChar(MSG_ENTITY, self.(teamscores[i]));
83 void TeamScore_Spawn(float t, string name)
87 ts.classname = "csqc_score_team";
88 ts.netname = name; // not used yet, FIXME
90 Net_LinkEntity(ts, false, 0, TeamScore_SendEntity);
91 teamscorekeepers[t - 1] = ts;
92 ++teamscores_entities_count;
93 PlayerStats_GameReport_AddTeam(t);
96 float TeamScore_AddToTeam(float t, float scorefield, float score)
103 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
104 if(t <= 0 || t >= 16)
108 error("Adding score to invalid team!");
110 s = teamscorekeepers[t - 1];
115 error("Adding score to unknown team!");
118 if(teamscores_label[scorefield] != "")
119 s.SendFlags |= pow(2, scorefield);
120 return (s.(teamscores[scorefield]) += score);
123 float TeamScore_Add(entity player, float scorefield, float score)
125 return TeamScore_AddToTeam(player.team, scorefield, score);
128 float TeamScore_Compare(entity t1, entity t2, float strict)
130 if(!t1 || !t2) return (!t2) - !t1;
132 vector result = '0 0 0';
134 for(i = 0; i < MAX_TEAMSCORE; ++i)
138 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result, strict);
141 if (result.x == 0 && strict)
142 result.x = t1.team - t2.team;
148 * the scoreinfo entity
151 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
153 scores_label[i] = label;
154 scores_flags[i] = scoreflags;
155 if((scoreflags & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
157 scores_primary = scores[i];
158 scores_flags_primary = scoreflags;
162 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
163 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
167 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
169 teamscores_label[i] = label;
170 teamscores_flags[i] = scoreflags;
171 if((scoreflags & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
173 teamscores_primary = teamscores[i];
174 teamscores_flags_primary = scoreflags;
178 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
179 PlayerStats_GameReport_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
183 float ScoreInfo_SendEntity(entity to, int sf)
186 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
187 WriteInt24_t(MSG_ENTITY, MapInfo_LoadedGametype);
188 for(i = 0; i < MAX_SCORE; ++i)
190 WriteString(MSG_ENTITY, scores_label[i]);
191 WriteByte(MSG_ENTITY, scores_flags[i]);
193 for(i = 0; i < MAX_TEAMSCORE; ++i)
195 WriteString(MSG_ENTITY, teamscores_label[i]);
196 WriteByte(MSG_ENTITY, teamscores_flags[i]);
201 void ScoreInfo_Init(float teams)
203 if(scores_initialized)
205 scores_initialized.SendFlags |= 1; // force a resend
209 scores_initialized = spawn();
210 scores_initialized.classname = "ent_client_scoreinfo";
211 Net_LinkEntity(scores_initialized, false, 0, ScoreInfo_SendEntity);
214 TeamScore_Spawn(NUM_TEAM_1, "Red");
216 TeamScore_Spawn(NUM_TEAM_2, "Blue");
218 TeamScore_Spawn(NUM_TEAM_3, "Yellow");
220 TeamScore_Spawn(NUM_TEAM_4, "Pink");
224 * per-player score entities
227 float PlayerScore_SendEntity(entity to, float sendflags)
229 float i, p, longflags;
231 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
232 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
235 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
236 if(self.(scores[i]) > 127 || self.(scores[i]) <= -128)
240 WriteByte(MSG_ENTITY, sendflags);
241 WriteByte(MSG_ENTITY, longflags);
243 WriteShort(MSG_ENTITY, sendflags);
244 WriteShort(MSG_ENTITY, longflags);
246 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
250 WriteInt24_t(MSG_ENTITY, self.(scores[i]));
252 WriteChar(MSG_ENTITY, self.(scores[i]));
258 float PlayerScore_Clear(entity player)
263 if(teamscores_entities_count)
266 if(MUTATOR_CALLHOOK(ForbidPlayerScore_Clear)) return 0;
268 sk = player.scorekeeper;
269 for(i = 0; i < MAX_SCORE; ++i)
271 if(sk.(scores[i]) != 0)
272 if(scores_label[i] != "")
273 sk.SendFlags |= pow(2, i);
280 void Score_ClearAll()
284 FOR_EACH_CLIENTSLOT(p)
289 for(i = 0; i < MAX_SCORE; ++i)
291 if(sk.(scores[i]) != 0)
292 if(scores_label[i] != "")
293 sk.SendFlags |= pow(2, i);
297 for(t = 0; t < 16; ++t)
299 sk = teamscorekeepers[t];
302 for(i = 0; i < MAX_TEAMSCORE; ++i)
304 if(sk.(teamscores[i]) != 0)
305 if(teamscores_label[i] != "")
306 sk.SendFlags |= pow(2, i);
307 sk.(teamscores[i]) = 0;
312 void PlayerScore_Attach(entity player)
315 if(player.scorekeeper)
316 error("player already has a scorekeeper");
319 Net_LinkEntity(sk, false, 0, PlayerScore_SendEntity);
320 player.scorekeeper = sk;
323 void PlayerScore_Detach(entity player)
325 if(!player.scorekeeper)
326 error("player has no scorekeeper");
327 remove(player.scorekeeper);
328 player.scorekeeper = world;
331 float PlayerScore_Add(entity player, float scorefield, float score)
336 if(!(g_lms && scorefield == SP_LMS_RANK)) // allow writing to this field in intermission as it is needed for newly joining players
339 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
340 s = player.scorekeeper;
345 backtrace("Adding score to unknown player!");
349 if(scores_label[scorefield] != "")
350 s.SendFlags |= pow(2, scorefield);
352 PS_GR_P_ADDVAL(s.owner, strcat(PLAYERSTATS_TOTAL, scores_label[scorefield]), score);
353 return (s.(scores[scorefield]) += score);
356 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
359 r = PlayerScore_Add(player, pscorefield, score);
360 if(teamscores_entities_count) // only for teamplay
361 r = TeamScore_Add(player, tscorefield, score);
365 float PlayerScore_Compare(entity t1, entity t2, float strict)
367 if(!t1 || !t2) return (!t2) - !t1;
369 vector result = '0 0 0';
371 for(i = 0; i < MAX_SCORE; ++i)
375 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result, strict);
378 if (result.x == 0 && strict)
379 result.x = num_for_edict(t1.owner) - num_for_edict(t2.owner);
384 void WinningConditionHelper()
390 entity winnerscorekeeper;
391 entity secondscorekeeper;
395 // gametype:P<pure>:S<slots>::plabel,plabel:tlabel,tlabel:teamid:tscore,tscore:teamid:tscore,tscore
396 // score labels always start with a symbol or with lower case
397 // so to match pure, match for :P0:
398 // to match full, match for :S0:
400 fullstatus = autocvar_g_full_getstatus_responses;
403 s = strcat(s, ":", autocvar_g_xonoticversion);
404 s = strcat(s, ":P", ftos(cvar_purechanges_count));
405 s = strcat(s, ":S", ftos(nJoinAllowed(world)));
406 s = strcat(s, ":F", ftos(serverflags));
407 s = strcat(s, ":M", modname);
408 s = strcat(s, "::", GetPlayerScoreString(world, (fullstatus ? 1 : 2)));
410 if(teamscores_entities_count)
414 s = strcat(s, ":", GetTeamScoreString(0, 1));
415 for(t = 0; t < 16; ++t)
416 if(teamscorekeepers[t])
417 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
419 WinningConditionHelper_winnerteam = -1;
420 WinningConditionHelper_secondteam = -1;
421 winnerscorekeeper = world;
422 secondscorekeeper = world;
423 for(t = 0; t < 16; ++t)
425 sk = teamscorekeepers[t];
426 c = TeamScore_Compare(winnerscorekeeper, sk, 1);
429 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
430 WinningConditionHelper_winnerteam = t + 1;
431 secondscorekeeper = winnerscorekeeper;
432 winnerscorekeeper = sk;
436 c = TeamScore_Compare(secondscorekeeper, sk, 1);
439 WinningConditionHelper_secondteam = t + 1;
440 secondscorekeeper = sk;
445 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper, 0) == 0);
446 if(WinningConditionHelper_equality)
447 WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
449 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
450 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
451 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
452 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
454 WinningConditionHelper_winner = world; // not supported in teamplay
455 WinningConditionHelper_second = world; // not supported in teamplay
459 WinningConditionHelper_winner = world;
460 WinningConditionHelper_second = world;
461 winnerscorekeeper = world;
462 secondscorekeeper = world;
466 c = PlayerScore_Compare(winnerscorekeeper, sk, 1);
469 WinningConditionHelper_second = WinningConditionHelper_winner;
470 WinningConditionHelper_winner = p;
471 secondscorekeeper = winnerscorekeeper;
472 winnerscorekeeper = sk;
476 c = PlayerScore_Compare(secondscorekeeper, sk, 1);
479 WinningConditionHelper_second = p;
480 secondscorekeeper = sk;
485 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper, 0) == 0);
486 if(WinningConditionHelper_equality)
487 WinningConditionHelper_winner = WinningConditionHelper_second = world;
489 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
490 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
491 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
492 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
494 WinningConditionHelper_winnerteam = -1; // no teamplay
495 WinningConditionHelper_secondteam = -1; // no teamplay
498 if(WinningConditionHelper_topscore == 0)
500 if(scores_flags_primary & SFL_ZERO_IS_WORST)
502 if(WinningConditionHelper_lowerisbetter)
503 WinningConditionHelper_topscore = 999999999;
505 WinningConditionHelper_topscore = -999999999;
507 WinningConditionHelper_equality = 0;
510 if(WinningConditionHelper_secondscore == 0)
512 if(scores_flags_primary & SFL_ZERO_IS_WORST)
514 if(WinningConditionHelper_lowerisbetter)
515 WinningConditionHelper_secondscore = 999999999;
517 WinningConditionHelper_secondscore = -999999999;
522 strunzone(worldstatus);
523 worldstatus = strzone(s);
529 s = GetPlayerScoreString(p, 1);
530 if(IS_REAL_CLIENT(p))
531 s = strcat(s, ":human");
533 s = strcat(s, ":bot");
534 if(!IS_PLAYER(p) && p.caplayer != 1 && !g_lms)
535 s = strcat(s, ":spectator");
539 if(IS_PLAYER(p) || p.caplayer == 1 || g_lms)
540 s = GetPlayerScoreString(p, 2);
546 strunzone(p.clientstatus);
547 p.clientstatus = strzone(s);
551 string GetScoreLogLabel(string label, float fl)
553 if(fl & SFL_LOWER_IS_BETTER)
554 label = strcat(label, "<");
555 if((fl & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
556 label = strcat(label, "!!");
557 else if((fl & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
558 label = strcat(label, "!");
562 string GetPlayerScoreString(entity pl, float shortString)
573 for(i = 0; i < MAX_SCORE; ++i)
574 if((scores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
578 out = strcat(out, GetScoreLogLabel(l, f), ",");
581 for(i = 0; i < MAX_SCORE; ++i)
582 if((scores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
586 out = strcat(out, GetScoreLogLabel(l, f), ",");
589 for(i = 0; i < MAX_SCORE; ++i)
590 if((scores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
591 if((scores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
595 out = strcat(out, GetScoreLogLabel(l, f), ",");
597 out = substring(out, 0, strlen(out) - 1);
599 else if((sk = pl.scorekeeper))
601 for(i = 0; i < MAX_SCORE; ++i)
602 if((scores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
603 out = strcat(out, ftos(sk.(scores[i])), ",");
605 for(i = 0; i < MAX_SCORE; ++i)
606 if((scores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
607 out = strcat(out, ftos(sk.(scores[i])), ",");
609 for(i = 0; i < MAX_SCORE; ++i)
610 if((scores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
611 if((scores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
612 out = strcat(out, ftos(sk.(scores[i])), ",");
613 out = substring(out, 0, strlen(out) - 1);
618 string GetTeamScoreString(float tm, float shortString)
629 for(i = 0; i < MAX_TEAMSCORE; ++i)
630 if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
632 f = teamscores_flags[i];
633 l = teamscores_label[i];
634 out = strcat(out, GetScoreLogLabel(l, f), ",");
637 for(i = 0; i < MAX_TEAMSCORE; ++i)
638 if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
640 f = teamscores_flags[i];
641 l = teamscores_label[i];
642 out = strcat(out, GetScoreLogLabel(l, f), ",");
645 for(i = 0; i < MAX_TEAMSCORE; ++i)
646 if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
647 if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
649 f = teamscores_flags[i];
650 l = teamscores_label[i];
651 out = strcat(out, GetScoreLogLabel(l, f), ",");
653 out = substring(out, 0, strlen(out) - 1);
655 else if((sk = teamscorekeepers[tm - 1]))
657 for(i = 0; i < MAX_TEAMSCORE; ++i)
658 if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_PRIMARY)
659 out = strcat(out, ftos(sk.(teamscores[i])), ",");
661 for(i = 0; i < MAX_TEAMSCORE; ++i)
662 if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) == SFL_SORT_PRIO_SECONDARY)
663 out = strcat(out, ftos(sk.(teamscores[i])), ",");
665 for(i = 0; i < MAX_TEAMSCORE; ++i)
666 if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_PRIMARY)
667 if((teamscores_flags[i] & SFL_SORT_PRIO_MASK) != SFL_SORT_PRIO_SECONDARY)
668 out = strcat(out, ftos(sk.(teamscores[i])), ",");
669 out = substring(out, 0, strlen(out) - 1);
674 float PlayerTeamScore_Compare(entity p1, entity p2, float teams, float strict)
676 if(teams && teamscores_entities_count)
678 if(p1.team != p2.team)
682 t1 = teamscorekeepers[p1.team - 1];
683 t2 = teamscorekeepers[p2.team - 1];
684 r = TeamScore_Compare(t1, t2, ((teams >= 0) ? 1 : strict));
691 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper, strict);
694 entity PlayerScore_Sort(.float field, float teams, float strict, float nospectators)
696 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
704 FOR_EACH_CLIENT(p) if(p.scorekeeper)
707 if(p.frags == FRAGS_SPECTATOR)
713 // Now plist points to the whole list.
715 pfirst = plast = world;
720 pprev = pbestprev = world;
722 for(p = plist; (pprev = p), (p = p.chain); )
724 if(PlayerTeamScore_Compare(p, pbest, teams, strict) > 0)
731 // remove pbest out of the chain
732 if(pbestprev == world)
735 pbestprev.chain = pbest.chain;
739 if(!plast || PlayerTeamScore_Compare(plast, pbest, teams, 0))
754 float TeamScore_GetCompareValue(float t)
759 if(t <= 0 || t >= 16)
763 error("Reading score of invalid team!");
766 sk = teamscorekeepers[t - 1];
769 s = sk.teamscores_primary;
770 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
773 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
778 const float NAMEWIDTH = 22;
779 const float SCORESWIDTH = 58;
780 // TODO put this somewhere in common?
781 string Score_NicePrint_ItemColor(float vflags)
783 if(vflags & SFL_SORT_PRIO_PRIMARY)
785 else if(vflags & SFL_SORT_PRIO_SECONDARY)
791 void Score_NicePrint_Team(entity to, float t, float w)
799 sk = teamscorekeepers[t - 1];
802 s = strcat(s, Team_ColoredFullName(t));
803 for(i = 0; i < MAX_TEAMSCORE; ++i)
804 if(teamscores_label[i] != "")
806 fl = teamscores_flags[i];
807 sc = sk.(teamscores[i]);
808 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
814 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
816 for(i = 0; i < MAX_SCORE; ++i)
817 if(scores_label[i] != "")
819 fl = scores_flags[i];
820 s2 = scores_label[i];
821 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
827 void Score_NicePrint_Player(entity to, entity p, float w)
837 s = strcat(s, p.netname);
840 i = strlennocol(s) - NAMEWIDTH;
842 s = substring(s, 0, strlen(s) - i);
845 s = strcat(s, strpad(i, ""));
850 for(i = 0; i < MAX_SCORE; ++i)
851 if(scores_label[i] != "")
853 fl = scores_flags[i];
855 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
861 void Score_NicePrint_Spectators(entity to)
863 print_to(to, "Spectators:");
866 void Score_NicePrint_Spectator(entity to, entity p)
868 print_to(to, strcat(" ", p.netname));
871 .float score_dummyfield;
872 void Score_NicePrint(entity to)
879 for(i = 0; i < MAX_SCORE; ++i)
880 if(scores_label[i] != "")
882 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
884 p = PlayerScore_Sort(score_dummyfield, 1, 1, 0);
887 if(!teamscores_entities_count)
888 Score_NicePrint_Team(to, t, w);
891 if(teamscores_entities_count)
893 Score_NicePrint_Team(to, p.team, w);
894 Score_NicePrint_Player(to, p, w);
904 Score_NicePrint_Spectators(to);
905 Score_NicePrint_Spectator(to, p);
910 void PlayerScore_PlayerStats(entity p)
916 for(i = 0; i < MAX_SCORE; ++i)
917 if(s.(scores[i]) != 0)
918 if(scores_label[i] != "")
919 PS_GR_P_ADDVAL(s.owner, strcat(PLAYERSTATS_SCOREBOARD, scores_label[i]), s.(scores[i]));
922 void PlayerScore_TeamStats(void)
926 for(t = 0; t < 16; ++t)
928 sk = teamscorekeepers[t];
931 for(i = 0; i < MAX_TEAMSCORE; ++i)
932 if(sk.(teamscores[i]) != 0)
933 if(teamscores_label[i] != "")
934 // the +1 is important here!
935 PS_GR_T_ADDVAL(t+1, strcat(PLAYERSTATS_SCOREBOARD, teamscores_label[i]), sk.(teamscores[i]));