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;
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);
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 WriteInt24_t(MSG_ENTITY, MapInfo_LoadedGametype);
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_cts) return; // in CTS, you don't lose score by observing
258 if(g_race && g_race_qualifying) return; // in qualifying, you don't lose score by observing
260 sk = player.scorekeeper;
261 for(i = 0; i < MAX_SCORE; ++i)
263 if(sk.(scores[i]) != 0)
264 if(scores_label[i] != "")
265 sk.SendFlags |= pow(2, i);
270 void Score_ClearAll()
274 FOR_EACH_CLIENTSLOT(p)
279 for(i = 0; i < MAX_SCORE; ++i)
281 if(sk.(scores[i]) != 0)
282 if(scores_label[i] != "")
283 sk.SendFlags |= pow(2, i);
287 for(t = 0; t < 16; ++t)
289 sk = teamscorekeepers[t];
292 for(i = 0; i < MAX_TEAMSCORE; ++i)
294 if(sk.(teamscores[i]) != 0)
295 if(teamscores_label[i] != "")
296 sk.SendFlags |= pow(2, i);
297 sk.(teamscores[i]) = 0;
302 void PlayerScore_Attach(entity player)
305 if(player.scorekeeper)
306 error("player already has a scorekeeper");
309 Net_LinkEntity(sk, FALSE, 0, PlayerScore_SendEntity);
310 player.scorekeeper = sk;
313 void PlayerScore_Detach(entity player)
315 if(!player.scorekeeper)
316 error("player has no scorekeeper");
317 remove(player.scorekeeper);
318 player.scorekeeper = world;
321 float PlayerScore_Add(entity player, float scorefield, float score)
326 if not(g_lms && scorefield == SP_LMS_RANK) // allow writing to this field in intermission as it is needed for newly joining players
329 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
330 s = player.scorekeeper;
335 backtrace("Adding score to unknown player!");
339 if(scores_label[scorefield] != "")
340 s.SendFlags |= pow(2, scorefield);
342 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_TOTAL, scores_label[scorefield]), score);
343 return (s.(scores[scorefield]) += score);
346 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
349 r = PlayerScore_Add(player, pscorefield, score);
350 if(teamscores_entities_count) // only for teamplay
351 r = TeamScore_Add(player, tscorefield, score);
355 float PlayerScore_Compare(entity t1, entity t2)
357 if(!t1 || !t2) return (!t2) - !t1;
359 vector result = '0 0 0';
361 for(i = 0; i < MAX_SCORE; ++i)
365 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
370 void WinningConditionHelper()
376 entity winnerscorekeeper;
377 entity secondscorekeeper;
381 // gametype:P<pure>:S<slots>::plabel,plabel:tlabel,tlabel:teamid:tscore,tscore:teamid:tscore,tscore
382 // score labels always start with a symbol or with lower case
383 // so to match pure, match for :P0:
384 // to match full, match for :S0:
387 s = strcat(s, ":", autocvar_g_xonoticversion);
388 s = strcat(s, ":P", ftos(cvar_purechanges_count));
389 s = strcat(s, ":S", ftos(nJoinAllowed(world)));
390 s = strcat(s, ":F", ftos(serverflags));
391 s = strcat(s, ":M", modname);
392 s = strcat(s, "::", GetPlayerScoreString(world, 1)); // make this 1 once we can, note: this doesn't contain any :<letter>
394 fullstatus = autocvar_g_full_getstatus_responses;
396 if(teamscores_entities_count)
400 s = strcat(s, ":", GetTeamScoreString(0, 1));
401 for(t = 0; t < 16; ++t)
402 if(teamscorekeepers[t])
403 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
405 WinningConditionHelper_winnerteam = -1;
406 WinningConditionHelper_secondteam = -1;
407 winnerscorekeeper = world;
408 secondscorekeeper = world;
409 for(t = 0; t < 16; ++t)
411 sk = teamscorekeepers[t];
412 c = TeamScore_Compare(winnerscorekeeper, sk);
415 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
416 WinningConditionHelper_winnerteam = t + 1;
417 secondscorekeeper = winnerscorekeeper;
418 winnerscorekeeper = sk;
422 c = TeamScore_Compare(secondscorekeeper, sk);
425 WinningConditionHelper_secondteam = t + 1;
426 secondscorekeeper = sk;
431 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
432 if(WinningConditionHelper_equality)
433 WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
435 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
436 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
437 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
438 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
440 WinningConditionHelper_winner = world; // not supported in teamplay
441 WinningConditionHelper_second = world; // not supported in teamplay
445 WinningConditionHelper_winner = world;
446 WinningConditionHelper_second = world;
447 winnerscorekeeper = world;
448 secondscorekeeper = world;
452 c = PlayerScore_Compare(winnerscorekeeper, sk);
455 WinningConditionHelper_second = WinningConditionHelper_winner;
456 WinningConditionHelper_winner = p;
457 secondscorekeeper = winnerscorekeeper;
458 winnerscorekeeper = sk;
462 c = PlayerScore_Compare(secondscorekeeper, sk);
465 WinningConditionHelper_second = p;
466 secondscorekeeper = sk;
471 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
472 if(WinningConditionHelper_equality)
473 WinningConditionHelper_winner = WinningConditionHelper_second = world;
475 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
476 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
477 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
478 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
480 WinningConditionHelper_winnerteam = -1; // no teamplay
481 WinningConditionHelper_secondteam = -1; // no teamplay
484 if(WinningConditionHelper_topscore == 0)
486 if(scores_flags_primary & SFL_ZERO_IS_WORST)
488 if(WinningConditionHelper_lowerisbetter)
489 WinningConditionHelper_topscore = 999999999;
491 WinningConditionHelper_topscore = -999999999;
493 WinningConditionHelper_equality = 0;
496 if(WinningConditionHelper_secondscore == 0)
498 if(scores_flags_primary & SFL_ZERO_IS_WORST)
500 if(WinningConditionHelper_lowerisbetter)
501 WinningConditionHelper_secondscore = 999999999;
503 WinningConditionHelper_secondscore = -999999999;
508 strunzone(worldstatus);
509 worldstatus = strzone(s);
515 s = GetPlayerScoreString(p, 1);
516 if(clienttype(p) == CLIENTTYPE_REAL)
517 s = strcat(s, ":human");
519 s = strcat(s, ":bot");
520 if(p.classname != "player" && !g_arena && !g_ca && !g_lms)
521 s = strcat(s, ":spectator");
525 if(p.classname == "player" || g_arena || g_ca || g_lms)
526 s = GetPlayerScoreString(p, 2);
532 strunzone(p.clientstatus);
533 p.clientstatus = strzone(s);
537 string GetScoreLogLabel(string label, float fl)
539 if(fl & SFL_LOWER_IS_BETTER)
540 label = strcat(label, "<");
541 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
542 label = strcat(label, "!!");
543 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
544 label = strcat(label, "!");
548 string GetPlayerScoreString(entity pl, float shortString)
559 for(i = 0; i < MAX_SCORE; ++i)
560 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
564 out = strcat(out, GetScoreLogLabel(l, f), ",");
567 for(i = 0; i < MAX_SCORE; ++i)
568 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
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_PRIMARY)
577 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
581 out = strcat(out, GetScoreLogLabel(l, f), ",");
583 out = substring(out, 0, strlen(out) - 1);
585 else if((sk = pl.scorekeeper))
587 for(i = 0; i < MAX_SCORE; ++i)
588 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
589 out = strcat(out, ftos(sk.(scores[i])), ",");
591 for(i = 0; i < MAX_SCORE; ++i)
592 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
593 out = strcat(out, ftos(sk.(scores[i])), ",");
595 for(i = 0; i < MAX_SCORE; ++i)
596 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
597 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
598 out = strcat(out, ftos(sk.(scores[i])), ",");
599 out = substring(out, 0, strlen(out) - 1);
604 string GetTeamScoreString(float tm, float shortString)
615 for(i = 0; i < MAX_SCORE; ++i)
616 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
618 f = teamscores_flags[i];
619 l = teamscores_label[i];
620 out = strcat(out, GetScoreLogLabel(l, f), ",");
623 for(i = 0; i < MAX_SCORE; ++i)
624 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
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_PRIMARY)
633 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
635 f = teamscores_flags[i];
636 l = teamscores_label[i];
637 out = strcat(out, GetScoreLogLabel(l, f), ",");
639 out = substring(out, 0, strlen(out) - 1);
641 else if((sk = teamscorekeepers[tm - 1]))
643 for(i = 0; i < MAX_TEAMSCORE; ++i)
644 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
645 out = strcat(out, ftos(sk.(teamscores[i])), ",");
647 for(i = 0; i < MAX_TEAMSCORE; ++i)
648 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
649 out = strcat(out, ftos(sk.(teamscores[i])), ",");
651 for(i = 0; i < MAX_TEAMSCORE; ++i)
652 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
653 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
654 out = strcat(out, ftos(sk.(teamscores[i])), ",");
655 out = substring(out, 0, strlen(out) - 1);
660 float PlayerTeamScore_Compare(entity p1, entity p2)
662 if(teamscores_entities_count)
663 if(p1.team != p2.team)
667 t1 = teamscorekeepers[p1.team - 1];
668 t2 = teamscorekeepers[p2.team - 1];
669 r = TeamScore_Compare(t1, t2);
670 if(r == 0) // ensure a deterministic order
671 r = p1.team - p2.team;
675 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
678 entity PlayerScore_Sort(.float field)
680 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
688 FOR_EACH_PLAYER(p) if(p.scorekeeper)
693 // Now plist points to the whole list.
695 pfirst = plast = world;
700 pprev = pbestprev = world;
702 for(p = plist; (pprev = p), (p = p.chain); )
704 if(PlayerTeamScore_Compare(p, pbest) > 0)
711 // remove pbest out of the chain
712 if(pbestprev == world)
715 pbestprev.chain = pbest.chain;
730 float TeamScore_GetCompareValue(float t)
735 if(t <= 0 || t >= 16)
739 error("Reading score of invalid team!");
742 sk = teamscorekeepers[t - 1];
745 s = sk.teamscores_primary;
746 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
749 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
755 #define SCORESWIDTH 58
756 // TODO put this somewhere in common?
757 string Score_NicePrint_ItemColor(float vflags)
759 if(vflags & SFL_SORT_PRIO_PRIMARY)
761 else if(vflags & SFL_SORT_PRIO_SECONDARY)
767 void Score_NicePrint_Team(entity to, float t, float w)
775 sk = teamscorekeepers[t - 1];
778 s = strcat(s, ColoredTeamName(t));
779 for(i = 0; i < MAX_TEAMSCORE; ++i)
780 if(teamscores_label[i] != "")
782 fl = teamscores_flags[i];
783 sc = sk.(teamscores[i]);
784 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
790 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
792 for(i = 0; i < MAX_SCORE; ++i)
793 if(scores_label[i] != "")
795 fl = scores_flags[i];
796 s2 = scores_label[i];
797 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
803 void Score_NicePrint_Player(entity to, entity p, float w)
813 s = strcat(s, p.netname);
816 i = strlennocol(s) - NAMEWIDTH;
818 s = substring(s, 0, strlen(s) - i);
821 s = strcat(s, strpad(i, ""));
826 for(i = 0; i < MAX_SCORE; ++i)
827 if(scores_label[i] != "")
829 fl = scores_flags[i];
831 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
837 void Score_NicePrint_Spectators(entity to)
839 print_to(to, "Spectators:");
842 void Score_NicePrint_Spectator(entity to, entity p)
844 print_to(to, strcat(" ", p.netname));
847 .float score_dummyfield;
848 void Score_NicePrint(entity to)
855 for(i = 0; i < MAX_SCORE; ++i)
856 if(scores_label[i] != "")
858 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
860 p = PlayerScore_Sort(score_dummyfield);
863 if(!teamscores_entities_count)
864 Score_NicePrint_Team(to, t, w);
867 if(teamscores_entities_count)
869 Score_NicePrint_Team(to, p.team, w);
870 Score_NicePrint_Player(to, p, w);
877 if(p.classname != "player")
880 Score_NicePrint_Spectators(to);
881 Score_NicePrint_Spectator(to, p);
886 void PlayerScore_PlayerStats(entity p)
892 for(i = 0; i < MAX_SCORE; ++i)
893 if(s.(scores[i]) != 0)
894 if(scores_label[i] != "")
895 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_SCOREBOARD, scores_label[i]), s.(scores[i]));
898 void PlayerScore_TeamStats(void)
902 for(t = 0; t < 16; ++t)
904 sk = teamscorekeepers[t];
907 for(i = 0; i < MAX_TEAMSCORE; ++i)
908 if(sk.(teamscores[i]) != 0)
909 if(teamscores_label[i] != "")
910 PlayerStats_TeamScore(t, strcat(PLAYERSTATS_SCOREBOARD, teamscores_label[i]), sk.(teamscores[i]));