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) // returns: cmp value, best prio
15 if(!(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)
128 if(!t1 || !t2) return (!t2) - !t1;
132 for(i = 0; i < MAX_TEAMSCORE; ++i)
136 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
142 * the scoreinfo entity
145 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
147 scores_label[i] = label;
148 scores_flags[i] = scoreflags;
149 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
151 scores_primary = scores[i];
152 scores_flags_primary = scoreflags;
156 PlayerStats_AddEvent(strcat(PLAYERSTATS_TOTAL, label));
157 PlayerStats_AddEvent(strcat(PLAYERSTATS_SCOREBOARD, label));
161 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
163 teamscores_label[i] = label;
164 teamscores_flags[i] = scoreflags;
165 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
167 teamscores_primary = teamscores[i];
168 teamscores_flags_primary = scoreflags;
172 float ScoreInfo_SendEntity(entity to, float sf)
175 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
176 WriteByte(MSG_ENTITY, game);
177 for(i = 0; i < MAX_SCORE; ++i)
179 WriteString(MSG_ENTITY, scores_label[i]);
180 WriteByte(MSG_ENTITY, scores_flags[i]);
182 for(i = 0; i < MAX_TEAMSCORE; ++i)
184 WriteString(MSG_ENTITY, teamscores_label[i]);
185 WriteByte(MSG_ENTITY, teamscores_flags[i]);
190 void ScoreInfo_Init(float teams)
192 if(scores_initialized)
194 scores_initialized.SendFlags |= 1; // force a resend
198 scores_initialized = spawn();
199 scores_initialized.classname = "ent_client_scoreinfo";
200 Net_LinkEntity(scores_initialized, FALSE, 0, ScoreInfo_SendEntity);
203 TeamScore_Spawn(COLOR_TEAM1, "Red");
205 TeamScore_Spawn(COLOR_TEAM2, "Blue");
207 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
209 TeamScore_Spawn(COLOR_TEAM4, "Pink");
213 * per-player score entities
216 float PlayerScore_SendEntity(entity to, float sendflags)
218 float i, p, longflags;
220 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
221 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
224 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
225 if(self.(scores[i]) > 127 || self.(scores[i]) <= -128)
229 WriteByte(MSG_ENTITY, sendflags);
230 WriteByte(MSG_ENTITY, longflags);
232 WriteShort(MSG_ENTITY, sendflags);
233 WriteShort(MSG_ENTITY, longflags);
235 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
239 WriteInt24_t(MSG_ENTITY, self.(scores[i]));
241 WriteChar(MSG_ENTITY, self.(scores[i]));
247 void PlayerScore_Clear(entity player)
252 if(teamscores_entities_count)
256 if(g_arena || g_ca) return;
257 if(g_race && !g_race_qualifying) return;
259 sk = player.scorekeeper;
260 for(i = 0; i < MAX_SCORE; ++i)
262 if(sk.(scores[i]) != 0)
263 if(scores_label[i] != "")
264 sk.SendFlags |= pow(2, i);
269 void Score_ClearAll()
273 FOR_EACH_CLIENTSLOT(p)
278 for(i = 0; i < MAX_SCORE; ++i)
280 if(sk.(scores[i]) != 0)
281 if(scores_label[i] != "")
282 sk.SendFlags |= pow(2, i);
286 for(t = 0; t < 16; ++t)
288 sk = teamscorekeepers[t];
291 for(i = 0; i < MAX_TEAMSCORE; ++i)
293 if(sk.(teamscores[i]) != 0)
294 if(teamscores_label[i] != "")
295 sk.SendFlags |= pow(2, i);
296 sk.(teamscores[i]) = 0;
301 void PlayerScore_Attach(entity player)
304 if(player.scorekeeper)
305 error("player already has a scorekeeper");
308 Net_LinkEntity(sk, FALSE, 0, PlayerScore_SendEntity);
309 player.scorekeeper = sk;
312 void PlayerScore_Detach(entity player)
314 if(!player.scorekeeper)
315 error("player has no scorekeeper");
316 remove(player.scorekeeper);
317 player.scorekeeper = world;
320 float PlayerScore_Add(entity player, float scorefield, float score)
325 if not(g_lms && scorefield == SP_LMS_RANK) // allow writing to this field in intermission as it is needed for newly joining players
328 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
329 s = player.scorekeeper;
334 backtrace("Adding score to unknown player!");
338 if(scores_label[scorefield] != "")
339 s.SendFlags |= pow(2, scorefield);
340 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_TOTAL, scores_label[scorefield]), score);
341 s.(scores_accumulated[scorefield]) += score;
342 return (s.(scores[scorefield]) += score);
345 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
348 r = PlayerScore_Add(player, pscorefield, score);
349 if(teamscores_entities_count) // only for teamplay
350 r = TeamScore_Add(player, tscorefield, score);
354 float PlayerScore_Compare(entity t1, entity t2)
356 if(!t1 || !t2) return (!t2) - !t1;
360 for(i = 0; i < MAX_SCORE; ++i)
364 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
369 void WinningConditionHelper()
375 entity winnerscorekeeper;
376 entity secondscorekeeper;
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:
386 s = strcat(s, ":", autocvar_g_xonoticversion);
387 s = strcat(s, ":P", ftos(cvar_purechanges_count));
388 s = strcat(s, ":S", ftos(nJoinAllowed(0)));
389 s = strcat(s, "::", GetPlayerScoreString(world, 1)); // make this 1 once we can, note: this doesn't contain any :<letter>
391 fullstatus = autocvar_g_full_getstatus_responses;
393 if(teamscores_entities_count)
397 s = strcat(s, ":", GetTeamScoreString(0, 1));
398 for(t = 0; t < 16; ++t)
399 if(teamscorekeepers[t])
400 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
402 WinningConditionHelper_winnerteam = -1;
403 WinningConditionHelper_secondteam = -1;
404 winnerscorekeeper = world;
405 secondscorekeeper = world;
406 for(t = 0; t < 16; ++t)
408 sk = teamscorekeepers[t];
409 c = TeamScore_Compare(winnerscorekeeper, sk);
412 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
413 WinningConditionHelper_winnerteam = t + 1;
414 secondscorekeeper = winnerscorekeeper;
415 winnerscorekeeper = sk;
419 c = TeamScore_Compare(secondscorekeeper, sk);
422 WinningConditionHelper_secondteam = t + 1;
423 secondscorekeeper = sk;
428 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
429 if(WinningConditionHelper_equality)
430 WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
432 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
433 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
434 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
435 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
437 WinningConditionHelper_winner = world; // not supported in teamplay
438 WinningConditionHelper_second = world; // not supported in teamplay
442 WinningConditionHelper_winner = world;
443 WinningConditionHelper_second = world;
444 winnerscorekeeper = world;
445 secondscorekeeper = world;
449 c = PlayerScore_Compare(winnerscorekeeper, sk);
452 WinningConditionHelper_second = WinningConditionHelper_winner;
453 WinningConditionHelper_winner = p;
454 secondscorekeeper = winnerscorekeeper;
455 winnerscorekeeper = sk;
459 c = PlayerScore_Compare(secondscorekeeper, sk);
462 WinningConditionHelper_second = p;
463 secondscorekeeper = sk;
468 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
469 if(WinningConditionHelper_equality)
470 WinningConditionHelper_winner = WinningConditionHelper_second = world;
472 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
473 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
474 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
475 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
477 WinningConditionHelper_winnerteam = -1; // no teamplay
478 WinningConditionHelper_secondteam = -1; // no teamplay
481 if(WinningConditionHelper_topscore == 0)
483 if(scores_flags_primary & SFL_ZERO_IS_WORST)
485 if(WinningConditionHelper_lowerisbetter)
486 WinningConditionHelper_topscore = 999999999;
488 WinningConditionHelper_topscore = -999999999;
490 WinningConditionHelper_equality = 0;
493 if(WinningConditionHelper_secondscore == 0)
495 if(scores_flags_primary & SFL_ZERO_IS_WORST)
497 if(WinningConditionHelper_lowerisbetter)
498 WinningConditionHelper_secondscore = 999999999;
500 WinningConditionHelper_secondscore = -999999999;
505 strunzone(worldstatus);
506 worldstatus = strzone(s);
512 s = GetPlayerScoreString(p, 1);
513 if(clienttype(p) == CLIENTTYPE_REAL)
514 s = strcat(s, ":human");
516 s = strcat(s, ":bot");
517 if(p.classname != "player" && !g_arena && !g_ca && !g_lms)
518 s = strcat(s, ":spectator");
522 if(p.classname == "player" || g_arena || g_ca || g_lms)
523 s = GetPlayerScoreString(p, 2);
529 strunzone(p.clientstatus);
530 p.clientstatus = strzone(s);
534 string GetScoreLogLabel(string label, float fl)
536 if(fl & SFL_LOWER_IS_BETTER)
537 label = strcat(label, "<");
538 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
539 label = strcat(label, "!!");
540 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
541 label = strcat(label, "!");
545 string GetPlayerScoreString(entity pl, float shortString)
556 for(i = 0; i < MAX_SCORE; ++i)
557 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
561 out = strcat(out, GetScoreLogLabel(l, f), ",");
564 for(i = 0; i < MAX_SCORE; ++i)
565 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
569 out = strcat(out, GetScoreLogLabel(l, f), ",");
572 for(i = 0; i < MAX_SCORE; ++i)
573 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
574 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
578 out = strcat(out, GetScoreLogLabel(l, f), ",");
580 out = substring(out, 0, strlen(out) - 1);
582 else if((sk = pl.scorekeeper))
584 for(i = 0; i < MAX_SCORE; ++i)
585 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
586 out = strcat(out, ftos(sk.(scores[i])), ",");
588 for(i = 0; i < MAX_SCORE; ++i)
589 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
590 out = strcat(out, ftos(sk.(scores[i])), ",");
592 for(i = 0; i < MAX_SCORE; ++i)
593 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
594 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
595 out = strcat(out, ftos(sk.(scores[i])), ",");
596 out = substring(out, 0, strlen(out) - 1);
601 string GetTeamScoreString(float tm, float shortString)
612 for(i = 0; i < MAX_SCORE; ++i)
613 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
615 f = teamscores_flags[i];
616 l = teamscores_label[i];
617 out = strcat(out, GetScoreLogLabel(l, f), ",");
620 for(i = 0; i < MAX_SCORE; ++i)
621 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
623 f = teamscores_flags[i];
624 l = teamscores_label[i];
625 out = strcat(out, GetScoreLogLabel(l, f), ",");
628 for(i = 0; i < MAX_SCORE; ++i)
629 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
630 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
632 f = teamscores_flags[i];
633 l = teamscores_label[i];
634 out = strcat(out, GetScoreLogLabel(l, f), ",");
636 out = substring(out, 0, strlen(out) - 1);
638 else if((sk = teamscorekeepers[tm - 1]))
640 for(i = 0; i < MAX_TEAMSCORE; ++i)
641 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
642 out = strcat(out, ftos(sk.(teamscores[i])), ",");
644 for(i = 0; i < MAX_TEAMSCORE; ++i)
645 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
646 out = strcat(out, ftos(sk.(teamscores[i])), ",");
648 for(i = 0; i < MAX_TEAMSCORE; ++i)
649 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
650 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
651 out = strcat(out, ftos(sk.(teamscores[i])), ",");
652 out = substring(out, 0, strlen(out) - 1);
657 float PlayerTeamScore_Compare(entity p1, entity p2)
659 if(teamscores_entities_count)
660 if(p1.team != p2.team)
664 t1 = teamscorekeepers[p1.team - 1];
665 t2 = teamscorekeepers[p2.team - 1];
666 r = TeamScore_Compare(t1, t2);
667 if(r == 0) // ensure a deterministic order
668 r = p1.team - p2.team;
672 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
675 entity PlayerScore_Sort(.float field)
677 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
685 FOR_EACH_PLAYER(p) if(p.scorekeeper)
690 // Now plist points to the whole list.
692 pfirst = plast = world;
697 pprev = pbestprev = world;
699 for(p = plist; (pprev = p), (p = p.chain); )
701 if(PlayerTeamScore_Compare(p, pbest) > 0)
708 // remove pbest out of the chain
709 if(pbestprev == world)
712 pbestprev.chain = pbest.chain;
727 float TeamScore_GetCompareValue(float t)
732 if(t <= 0 || t >= 16)
736 error("Reading score of invalid team!");
739 sk = teamscorekeepers[t - 1];
742 s = sk.teamscores_primary;
743 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
746 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
752 #define SCORESWIDTH 58
753 // TODO put this somewhere in common?
754 string Score_NicePrint_ItemColor(float vflags)
756 if(vflags & SFL_SORT_PRIO_PRIMARY)
758 else if(vflags & SFL_SORT_PRIO_SECONDARY)
764 void Score_NicePrint_Team(entity to, float t, float w)
772 sk = teamscorekeepers[t - 1];
775 s = strcat(s, ColoredTeamName(t));
776 for(i = 0; i < MAX_TEAMSCORE; ++i)
777 if(teamscores_label[i] != "")
779 fl = teamscores_flags[i];
780 sc = sk.(teamscores[i]);
781 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
787 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
789 for(i = 0; i < MAX_SCORE; ++i)
790 if(scores_label[i] != "")
792 fl = scores_flags[i];
793 s2 = scores_label[i];
794 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
800 void Score_NicePrint_Player(entity to, entity p, float w)
810 s = strcat(s, p.netname);
813 i = strlennocol(s) - NAMEWIDTH;
815 s = substring(s, 0, strlen(s) - i);
818 s = strcat(s, strpad(i, ""));
823 for(i = 0; i < MAX_SCORE; ++i)
824 if(scores_label[i] != "")
826 fl = scores_flags[i];
828 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
834 void Score_NicePrint_Spectators(entity to)
836 print_to(to, "Spectators:");
839 void Score_NicePrint_Spectator(entity to, entity p)
841 print_to(to, strcat(" ", p.netname));
844 .float score_dummyfield;
845 void Score_NicePrint(entity to)
852 for(i = 0; i < MAX_SCORE; ++i)
853 if(scores_label[i] != "")
855 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
857 p = PlayerScore_Sort(score_dummyfield);
860 if(!teamscores_entities_count)
861 Score_NicePrint_Team(to, t, w);
864 if(teamscores_entities_count)
866 Score_NicePrint_Team(to, p.team, w);
867 Score_NicePrint_Player(to, p, w);
874 if(p.classname != "player")
877 Score_NicePrint_Spectators(to);
878 Score_NicePrint_Spectator(to, p);
883 void PlayerScore_PlayerStats(entity p)
889 for(i = 0; i < MAX_SCORE; ++i)
890 if(s.(scores[i]) != 0)
891 if(scores_label[i] != "")
892 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_SCOREBOARD, scores_label[i]), s.(scores[i]));
895 void PlayerScore_TeamStats(void)
899 for(t = 0; t < 16; ++t)
901 sk = teamscorekeepers[t];
904 for(i = 0; i < MAX_TEAMSCORE; ++i)
905 if(sk.(teamscores[i]) != 0)
906 if(teamscores_label[i] != "")
907 PlayerStats_TeamScore(t, strcat(PLAYERSTATS_SCOREBOARD, teamscores_label[i]), sk.(teamscores[i]));