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;
13 vector ScoreField_Compare(entity t1, entity t2, .float field, float fieldflags, vector previous, float strict) // returns: cmp value, best prio
15 if(!strict && !(fieldflags & SFL_SORT_PRIO_MASK)) // column does not sort
17 if(fieldflags & SFL_SORT_PRIO_MASK < previous_y)
19 if(t1.field == t2.field)
22 previous_y = fieldflags & SFL_SORT_PRIO_MASK;
24 if(fieldflags & SFL_ZERO_IS_WORST)
31 else if(t2.field == 0)
38 if(fieldflags & SFL_LOWER_IS_BETTER)
39 previous_x = (t2.field - t1.field);
41 previous_x = (t1.field - t2.field);
50 float TeamScore_SendEntity(entity to, float sendflags)
52 float i, p, longflags;
54 WriteByte(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
55 WriteByte(MSG_ENTITY, self.team - 1);
58 for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
59 if(self.(teamscores[i]) > 127 || self.(teamscores[i]) <= -128)
62 #if MAX_TEAMSCORE <= 8
63 WriteByte(MSG_ENTITY, sendflags);
64 WriteByte(MSG_ENTITY, longflags);
66 WriteShort(MSG_ENTITY, sendflags);
67 WriteShort(MSG_ENTITY, longflags);
69 for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
73 WriteInt24_t(MSG_ENTITY, self.(teamscores[i]));
75 WriteChar(MSG_ENTITY, self.(teamscores[i]));
81 void TeamScore_Spawn(float t, string name)
85 ts.classname = "csqc_score_team";
86 ts.netname = name; // not used yet, FIXME
88 Net_LinkEntity(ts, FALSE, 0, TeamScore_SendEntity);
89 teamscorekeepers[t - 1] = ts;
90 ++teamscores_entities_count;
91 PlayerStats_AddTeam(t);
94 float TeamScore_AddToTeam(float t, float scorefield, float score)
101 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
102 if(t <= 0 || t >= 16)
106 error("Adding score to invalid team!");
108 s = teamscorekeepers[t - 1];
113 error("Adding score to unknown team!");
116 if(teamscores_label[scorefield] != "")
117 s.SendFlags |= pow(2, scorefield);
118 return (s.(teamscores[scorefield]) += score);
121 float TeamScore_Add(entity player, float scorefield, float score)
123 return TeamScore_AddToTeam(player.team, scorefield, score);
126 float TeamScore_Compare(entity t1, entity t2, float strict)
128 if(!t1 || !t2) return (!t2) - !t1;
130 vector result = '0 0 0';
132 for(i = 0; i < MAX_TEAMSCORE; ++i)
136 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result, strict);
139 if (result_x == 0 && strict)
140 result_x = t1.team - t2.team;
146 * the scoreinfo entity
149 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
151 scores_label[i] = label;
152 scores_flags[i] = scoreflags;
153 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
155 scores_primary = scores[i];
156 scores_flags_primary = scoreflags;
160 PlayerStats_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
161 PlayerStats_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
165 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
167 teamscores_label[i] = label;
168 teamscores_flags[i] = scoreflags;
169 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
171 teamscores_primary = teamscores[i];
172 teamscores_flags_primary = scoreflags;
176 float ScoreInfo_SendEntity(entity to, float sf)
179 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
180 WriteInt24_t(MSG_ENTITY, MapInfo_LoadedGametype);
181 for(i = 0; i < MAX_SCORE; ++i)
183 WriteString(MSG_ENTITY, scores_label[i]);
184 WriteByte(MSG_ENTITY, scores_flags[i]);
186 for(i = 0; i < MAX_TEAMSCORE; ++i)
188 WriteString(MSG_ENTITY, teamscores_label[i]);
189 WriteByte(MSG_ENTITY, teamscores_flags[i]);
194 void ScoreInfo_Init(float teams)
196 if(scores_initialized)
198 scores_initialized.SendFlags |= 1; // force a resend
202 scores_initialized = spawn();
203 scores_initialized.classname = "ent_client_scoreinfo";
204 Net_LinkEntity(scores_initialized, FALSE, 0, ScoreInfo_SendEntity);
207 TeamScore_Spawn(COLOR_TEAM1, "Red");
209 TeamScore_Spawn(COLOR_TEAM2, "Blue");
211 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
213 TeamScore_Spawn(COLOR_TEAM4, "Pink");
217 * per-player score entities
220 float PlayerScore_SendEntity(entity to, float sendflags)
222 float i, p, longflags;
224 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
225 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
228 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
229 if(self.(scores[i]) > 127 || self.(scores[i]) <= -128)
233 WriteByte(MSG_ENTITY, sendflags);
234 WriteByte(MSG_ENTITY, longflags);
236 WriteShort(MSG_ENTITY, sendflags);
237 WriteShort(MSG_ENTITY, longflags);
239 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
243 WriteInt24_t(MSG_ENTITY, self.(scores[i]));
245 WriteChar(MSG_ENTITY, self.(scores[i]));
251 void PlayerScore_Clear(entity player)
256 if(teamscores_entities_count)
260 if(g_arena || g_ca) return;
261 if(g_cts) return; // in CTS, you don't lose score by observing
262 if(g_race && g_race_qualifying) return; // in qualifying, you don't lose score by observing
264 sk = player.scorekeeper;
265 for(i = 0; i < MAX_SCORE; ++i)
267 if(sk.(scores[i]) != 0)
268 if(scores_label[i] != "")
269 sk.SendFlags |= pow(2, i);
274 void Score_ClearAll()
278 FOR_EACH_CLIENTSLOT(p)
283 for(i = 0; i < MAX_SCORE; ++i)
285 if(sk.(scores[i]) != 0)
286 if(scores_label[i] != "")
287 sk.SendFlags |= pow(2, i);
291 for(t = 0; t < 16; ++t)
293 sk = teamscorekeepers[t];
296 for(i = 0; i < MAX_TEAMSCORE; ++i)
298 if(sk.(teamscores[i]) != 0)
299 if(teamscores_label[i] != "")
300 sk.SendFlags |= pow(2, i);
301 sk.(teamscores[i]) = 0;
306 void PlayerScore_Attach(entity player)
309 if(player.scorekeeper)
310 error("player already has a scorekeeper");
313 Net_LinkEntity(sk, FALSE, 0, PlayerScore_SendEntity);
314 player.scorekeeper = sk;
317 void PlayerScore_Detach(entity player)
319 if(!player.scorekeeper)
320 error("player has no scorekeeper");
321 remove(player.scorekeeper);
322 player.scorekeeper = world;
325 float PlayerScore_Add(entity player, float scorefield, float score)
330 if not(g_lms && scorefield == SP_LMS_RANK) // allow writing to this field in intermission as it is needed for newly joining players
333 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
334 s = player.scorekeeper;
339 backtrace("Adding score to unknown player!");
343 if(scores_label[scorefield] != "")
344 s.SendFlags |= pow(2, scorefield);
346 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_TOTAL, scores_label[scorefield]), score);
347 return (s.(scores[scorefield]) += score);
350 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
353 r = PlayerScore_Add(player, pscorefield, score);
354 if(teamscores_entities_count) // only for teamplay
355 r = TeamScore_Add(player, tscorefield, score);
359 float PlayerScore_Compare(entity t1, entity t2, float strict)
361 if(!t1 || !t2) return (!t2) - !t1;
363 vector result = '0 0 0';
365 for(i = 0; i < MAX_SCORE; ++i)
369 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result, strict);
372 if (result_x == 0 && strict)
373 result_x = num_for_edict(t1.owner) - num_for_edict(t2.owner);
378 void WinningConditionHelper()
384 entity winnerscorekeeper;
385 entity secondscorekeeper;
389 // gametype:P<pure>:S<slots>::plabel,plabel:tlabel,tlabel:teamid:tscore,tscore:teamid:tscore,tscore
390 // score labels always start with a symbol or with lower case
391 // so to match pure, match for :P0:
392 // to match full, match for :S0:
395 s = strcat(s, ":", autocvar_g_xonoticversion);
396 s = strcat(s, ":P", ftos(cvar_purechanges_count));
397 s = strcat(s, ":S", ftos(nJoinAllowed(world)));
398 s = strcat(s, ":F", ftos(serverflags));
399 s = strcat(s, ":M", modname);
400 s = strcat(s, "::", GetPlayerScoreString(world, 1)); // make this 1 once we can, note: this doesn't contain any :<letter>
402 fullstatus = autocvar_g_full_getstatus_responses;
404 if(teamscores_entities_count)
408 s = strcat(s, ":", GetTeamScoreString(0, 1));
409 for(t = 0; t < 16; ++t)
410 if(teamscorekeepers[t])
411 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
413 WinningConditionHelper_winnerteam = -1;
414 WinningConditionHelper_secondteam = -1;
415 winnerscorekeeper = world;
416 secondscorekeeper = world;
417 for(t = 0; t < 16; ++t)
419 sk = teamscorekeepers[t];
420 c = TeamScore_Compare(winnerscorekeeper, sk, 1);
423 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
424 WinningConditionHelper_winnerteam = t + 1;
425 secondscorekeeper = winnerscorekeeper;
426 winnerscorekeeper = sk;
430 c = TeamScore_Compare(secondscorekeeper, sk, 1);
433 WinningConditionHelper_secondteam = t + 1;
434 secondscorekeeper = sk;
439 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper, 0) == 0);
440 if(WinningConditionHelper_equality)
441 WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
443 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
444 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
445 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
446 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
448 WinningConditionHelper_winner = world; // not supported in teamplay
449 WinningConditionHelper_second = world; // not supported in teamplay
453 WinningConditionHelper_winner = world;
454 WinningConditionHelper_second = world;
455 winnerscorekeeper = world;
456 secondscorekeeper = world;
460 c = PlayerScore_Compare(winnerscorekeeper, sk, 1);
463 WinningConditionHelper_second = WinningConditionHelper_winner;
464 WinningConditionHelper_winner = p;
465 secondscorekeeper = winnerscorekeeper;
466 winnerscorekeeper = sk;
470 c = PlayerScore_Compare(secondscorekeeper, sk, 1);
473 WinningConditionHelper_second = p;
474 secondscorekeeper = sk;
479 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper, 0) == 0);
480 if(WinningConditionHelper_equality)
481 WinningConditionHelper_winner = WinningConditionHelper_second = world;
483 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
484 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
485 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
486 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
488 WinningConditionHelper_winnerteam = -1; // no teamplay
489 WinningConditionHelper_secondteam = -1; // no teamplay
492 if(WinningConditionHelper_topscore == 0)
494 if(scores_flags_primary & SFL_ZERO_IS_WORST)
496 if(WinningConditionHelper_lowerisbetter)
497 WinningConditionHelper_topscore = 999999999;
499 WinningConditionHelper_topscore = -999999999;
501 WinningConditionHelper_equality = 0;
504 if(WinningConditionHelper_secondscore == 0)
506 if(scores_flags_primary & SFL_ZERO_IS_WORST)
508 if(WinningConditionHelper_lowerisbetter)
509 WinningConditionHelper_secondscore = 999999999;
511 WinningConditionHelper_secondscore = -999999999;
516 strunzone(worldstatus);
517 worldstatus = strzone(s);
523 s = GetPlayerScoreString(p, 1);
524 if(clienttype(p) == CLIENTTYPE_REAL)
525 s = strcat(s, ":human");
527 s = strcat(s, ":bot");
528 if(p.classname != "player" && !g_arena && !g_ca && !g_lms)
529 s = strcat(s, ":spectator");
533 if(p.classname == "player" || g_arena || g_ca || g_lms)
534 s = GetPlayerScoreString(p, 2);
540 strunzone(p.clientstatus);
541 p.clientstatus = strzone(s);
545 string GetScoreLogLabel(string label, float fl)
547 if(fl & SFL_LOWER_IS_BETTER)
548 label = strcat(label, "<");
549 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
550 label = strcat(label, "!!");
551 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
552 label = strcat(label, "!");
556 string GetPlayerScoreString(entity pl, float shortString)
567 for(i = 0; i < MAX_SCORE; ++i)
568 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
572 out = strcat(out, GetScoreLogLabel(l, f), ",");
575 for(i = 0; i < MAX_SCORE; ++i)
576 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
580 out = strcat(out, GetScoreLogLabel(l, f), ",");
583 for(i = 0; i < MAX_SCORE; ++i)
584 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
585 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
589 out = strcat(out, GetScoreLogLabel(l, f), ",");
591 out = substring(out, 0, strlen(out) - 1);
593 else if((sk = pl.scorekeeper))
595 for(i = 0; i < MAX_SCORE; ++i)
596 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
597 out = strcat(out, ftos(sk.(scores[i])), ",");
599 for(i = 0; i < MAX_SCORE; ++i)
600 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
601 out = strcat(out, ftos(sk.(scores[i])), ",");
603 for(i = 0; i < MAX_SCORE; ++i)
604 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
605 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
606 out = strcat(out, ftos(sk.(scores[i])), ",");
607 out = substring(out, 0, strlen(out) - 1);
612 string GetTeamScoreString(float tm, float shortString)
623 for(i = 0; i < MAX_SCORE; ++i)
624 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
626 f = teamscores_flags[i];
627 l = teamscores_label[i];
628 out = strcat(out, GetScoreLogLabel(l, f), ",");
631 for(i = 0; i < MAX_SCORE; ++i)
632 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
634 f = teamscores_flags[i];
635 l = teamscores_label[i];
636 out = strcat(out, GetScoreLogLabel(l, f), ",");
639 for(i = 0; i < MAX_SCORE; ++i)
640 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
641 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
643 f = teamscores_flags[i];
644 l = teamscores_label[i];
645 out = strcat(out, GetScoreLogLabel(l, f), ",");
647 out = substring(out, 0, strlen(out) - 1);
649 else if((sk = teamscorekeepers[tm - 1]))
651 for(i = 0; i < MAX_TEAMSCORE; ++i)
652 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
653 out = strcat(out, ftos(sk.(teamscores[i])), ",");
655 for(i = 0; i < MAX_TEAMSCORE; ++i)
656 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
657 out = strcat(out, ftos(sk.(teamscores[i])), ",");
659 for(i = 0; i < MAX_TEAMSCORE; ++i)
660 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
661 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
662 out = strcat(out, ftos(sk.(teamscores[i])), ",");
663 out = substring(out, 0, strlen(out) - 1);
668 float PlayerTeamScore_Compare(entity p1, entity p2, float teams, float strict)
670 if(teams && teamscores_entities_count)
672 if(p1.team != p2.team)
676 t1 = teamscorekeepers[p1.team - 1];
677 t2 = teamscorekeepers[p2.team - 1];
678 r = TeamScore_Compare(t1, t2, ((teams >= 0) ? 1 : strict));
685 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper, strict);
688 entity PlayerScore_Sort(.float field, float teams, float strict)
690 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
698 FOR_EACH_PLAYER(p) if(p.scorekeeper)
703 // Now plist points to the whole list.
705 pfirst = plast = world;
710 pprev = pbestprev = world;
712 for(p = plist; (pprev = p), (p = p.chain); )
714 if(PlayerTeamScore_Compare(p, pbest, teams, strict) > 0)
721 // remove pbest out of the chain
722 if(pbestprev == world)
725 pbestprev.chain = pbest.chain;
729 if(!plast || PlayerTeamScore_Compare(plast, pbest, teams, 0))
744 float TeamScore_GetCompareValue(float t)
749 if(t <= 0 || t >= 16)
753 error("Reading score of invalid team!");
756 sk = teamscorekeepers[t - 1];
759 s = sk.teamscores_primary;
760 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
763 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
769 #define SCORESWIDTH 58
770 // TODO put this somewhere in common?
771 string Score_NicePrint_ItemColor(float vflags)
773 if(vflags & SFL_SORT_PRIO_PRIMARY)
775 else if(vflags & SFL_SORT_PRIO_SECONDARY)
781 void Score_NicePrint_Team(entity to, float t, float w)
789 sk = teamscorekeepers[t - 1];
792 s = strcat(s, ColoredTeamName(t));
793 for(i = 0; i < MAX_TEAMSCORE; ++i)
794 if(teamscores_label[i] != "")
796 fl = teamscores_flags[i];
797 sc = sk.(teamscores[i]);
798 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
804 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
806 for(i = 0; i < MAX_SCORE; ++i)
807 if(scores_label[i] != "")
809 fl = scores_flags[i];
810 s2 = scores_label[i];
811 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
817 void Score_NicePrint_Player(entity to, entity p, float w)
827 s = strcat(s, p.netname);
830 i = strlennocol(s) - NAMEWIDTH;
832 s = substring(s, 0, strlen(s) - i);
835 s = strcat(s, strpad(i, ""));
840 for(i = 0; i < MAX_SCORE; ++i)
841 if(scores_label[i] != "")
843 fl = scores_flags[i];
845 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
851 void Score_NicePrint_Spectators(entity to)
853 print_to(to, "Spectators:");
856 void Score_NicePrint_Spectator(entity to, entity p)
858 print_to(to, strcat(" ", p.netname));
861 .float score_dummyfield;
862 void Score_NicePrint(entity to)
869 for(i = 0; i < MAX_SCORE; ++i)
870 if(scores_label[i] != "")
872 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
874 p = PlayerScore_Sort(score_dummyfield, 1, 1);
877 if(!teamscores_entities_count)
878 Score_NicePrint_Team(to, t, w);
881 if(teamscores_entities_count)
883 Score_NicePrint_Team(to, p.team, w);
884 Score_NicePrint_Player(to, p, w);
891 if(p.classname != "player")
894 Score_NicePrint_Spectators(to);
895 Score_NicePrint_Spectator(to, p);
900 void PlayerScore_PlayerStats(entity p)
906 for(i = 0; i < MAX_SCORE; ++i)
907 if(s.(scores[i]) != 0)
908 if(scores_label[i] != "")
909 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_SCOREBOARD, scores_label[i]), s.(scores[i]));
912 void PlayerScore_TeamStats(void)
916 for(t = 0; t < 16; ++t)
918 sk = teamscorekeepers[t];
921 for(i = 0; i < MAX_TEAMSCORE; ++i)
922 if(sk.(teamscores[i]) != 0)
923 if(teamscores_label[i] != "")
924 PlayerStats_TeamScore(t, strcat(PLAYERSTATS_SCOREBOARD, teamscores_label[i]), sk.(teamscores[i]));