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 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_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);
341 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_TOTAL, scores_label[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(world)));
389 s = strcat(s, ":F", ftos(serverflags));
390 s = strcat(s, ":M", modname);
391 s = strcat(s, "::", GetPlayerScoreString(world, 1)); // make this 1 once we can, note: this doesn't contain any :<letter>
393 fullstatus = autocvar_g_full_getstatus_responses;
395 if(teamscores_entities_count)
399 s = strcat(s, ":", GetTeamScoreString(0, 1));
400 for(t = 0; t < 16; ++t)
401 if(teamscorekeepers[t])
402 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
404 WinningConditionHelper_winnerteam = -1;
405 WinningConditionHelper_secondteam = -1;
406 winnerscorekeeper = world;
407 secondscorekeeper = world;
408 for(t = 0; t < 16; ++t)
410 sk = teamscorekeepers[t];
411 c = TeamScore_Compare(winnerscorekeeper, sk);
414 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
415 WinningConditionHelper_winnerteam = t + 1;
416 secondscorekeeper = winnerscorekeeper;
417 winnerscorekeeper = sk;
421 c = TeamScore_Compare(secondscorekeeper, sk);
424 WinningConditionHelper_secondteam = t + 1;
425 secondscorekeeper = sk;
430 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
431 if(WinningConditionHelper_equality)
432 WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
434 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
435 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
436 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
437 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
439 WinningConditionHelper_winner = world; // not supported in teamplay
440 WinningConditionHelper_second = world; // not supported in teamplay
444 WinningConditionHelper_winner = world;
445 WinningConditionHelper_second = world;
446 winnerscorekeeper = world;
447 secondscorekeeper = world;
451 c = PlayerScore_Compare(winnerscorekeeper, sk);
454 WinningConditionHelper_second = WinningConditionHelper_winner;
455 WinningConditionHelper_winner = p;
456 secondscorekeeper = winnerscorekeeper;
457 winnerscorekeeper = sk;
461 c = PlayerScore_Compare(secondscorekeeper, sk);
464 WinningConditionHelper_second = p;
465 secondscorekeeper = sk;
470 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
471 if(WinningConditionHelper_equality)
472 WinningConditionHelper_winner = WinningConditionHelper_second = world;
474 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
475 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
476 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
477 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
479 WinningConditionHelper_winnerteam = -1; // no teamplay
480 WinningConditionHelper_secondteam = -1; // no teamplay
483 if(WinningConditionHelper_topscore == 0)
485 if(scores_flags_primary & SFL_ZERO_IS_WORST)
487 if(WinningConditionHelper_lowerisbetter)
488 WinningConditionHelper_topscore = 999999999;
490 WinningConditionHelper_topscore = -999999999;
492 WinningConditionHelper_equality = 0;
495 if(WinningConditionHelper_secondscore == 0)
497 if(scores_flags_primary & SFL_ZERO_IS_WORST)
499 if(WinningConditionHelper_lowerisbetter)
500 WinningConditionHelper_secondscore = 999999999;
502 WinningConditionHelper_secondscore = -999999999;
507 strunzone(worldstatus);
508 worldstatus = strzone(s);
514 s = GetPlayerScoreString(p, 1);
515 if(clienttype(p) == CLIENTTYPE_REAL)
516 s = strcat(s, ":human");
518 s = strcat(s, ":bot");
519 if(p.classname != "player" && !g_arena && !g_ca && !g_lms)
520 s = strcat(s, ":spectator");
524 if(p.classname == "player" || g_arena || g_ca || g_lms)
525 s = GetPlayerScoreString(p, 2);
531 strunzone(p.clientstatus);
532 p.clientstatus = strzone(s);
536 string GetScoreLogLabel(string label, float fl)
538 if(fl & SFL_LOWER_IS_BETTER)
539 label = strcat(label, "<");
540 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
541 label = strcat(label, "!!");
542 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
543 label = strcat(label, "!");
547 string GetPlayerScoreString(entity pl, float shortString)
558 for(i = 0; i < MAX_SCORE; ++i)
559 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
563 out = strcat(out, GetScoreLogLabel(l, f), ",");
566 for(i = 0; i < MAX_SCORE; ++i)
567 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
571 out = strcat(out, GetScoreLogLabel(l, f), ",");
574 for(i = 0; i < MAX_SCORE; ++i)
575 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
576 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
580 out = strcat(out, GetScoreLogLabel(l, f), ",");
582 out = substring(out, 0, strlen(out) - 1);
584 else if((sk = pl.scorekeeper))
586 for(i = 0; i < MAX_SCORE; ++i)
587 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
588 out = strcat(out, ftos(sk.(scores[i])), ",");
590 for(i = 0; i < MAX_SCORE; ++i)
591 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
592 out = strcat(out, ftos(sk.(scores[i])), ",");
594 for(i = 0; i < MAX_SCORE; ++i)
595 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
596 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
597 out = strcat(out, ftos(sk.(scores[i])), ",");
598 out = substring(out, 0, strlen(out) - 1);
603 string GetTeamScoreString(float tm, float shortString)
614 for(i = 0; i < MAX_SCORE; ++i)
615 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
617 f = teamscores_flags[i];
618 l = teamscores_label[i];
619 out = strcat(out, GetScoreLogLabel(l, f), ",");
622 for(i = 0; i < MAX_SCORE; ++i)
623 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
625 f = teamscores_flags[i];
626 l = teamscores_label[i];
627 out = strcat(out, GetScoreLogLabel(l, f), ",");
630 for(i = 0; i < MAX_SCORE; ++i)
631 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
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), ",");
638 out = substring(out, 0, strlen(out) - 1);
640 else if((sk = teamscorekeepers[tm - 1]))
642 for(i = 0; i < MAX_TEAMSCORE; ++i)
643 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
644 out = strcat(out, ftos(sk.(teamscores[i])), ",");
646 for(i = 0; i < MAX_TEAMSCORE; ++i)
647 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
648 out = strcat(out, ftos(sk.(teamscores[i])), ",");
650 for(i = 0; i < MAX_TEAMSCORE; ++i)
651 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
652 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
653 out = strcat(out, ftos(sk.(teamscores[i])), ",");
654 out = substring(out, 0, strlen(out) - 1);
659 float PlayerTeamScore_Compare(entity p1, entity p2)
661 if(teamscores_entities_count)
662 if(p1.team != p2.team)
666 t1 = teamscorekeepers[p1.team - 1];
667 t2 = teamscorekeepers[p2.team - 1];
668 r = TeamScore_Compare(t1, t2);
669 if(r == 0) // ensure a deterministic order
670 r = p1.team - p2.team;
674 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
677 entity PlayerScore_Sort(.float field)
679 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
687 FOR_EACH_PLAYER(p) if(p.scorekeeper)
692 // Now plist points to the whole list.
694 pfirst = plast = world;
699 pprev = pbestprev = world;
701 for(p = plist; (pprev = p), (p = p.chain); )
703 if(PlayerTeamScore_Compare(p, pbest) > 0)
710 // remove pbest out of the chain
711 if(pbestprev == world)
714 pbestprev.chain = pbest.chain;
729 float TeamScore_GetCompareValue(float t)
734 if(t <= 0 || t >= 16)
738 error("Reading score of invalid team!");
741 sk = teamscorekeepers[t - 1];
744 s = sk.teamscores_primary;
745 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
748 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
754 #define SCORESWIDTH 58
755 // TODO put this somewhere in common?
756 string Score_NicePrint_ItemColor(float vflags)
758 if(vflags & SFL_SORT_PRIO_PRIMARY)
760 else if(vflags & SFL_SORT_PRIO_SECONDARY)
766 void Score_NicePrint_Team(entity to, float t, float w)
774 sk = teamscorekeepers[t - 1];
777 s = strcat(s, ColoredTeamName(t));
778 for(i = 0; i < MAX_TEAMSCORE; ++i)
779 if(teamscores_label[i] != "")
781 fl = teamscores_flags[i];
782 sc = sk.(teamscores[i]);
783 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
789 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
791 for(i = 0; i < MAX_SCORE; ++i)
792 if(scores_label[i] != "")
794 fl = scores_flags[i];
795 s2 = scores_label[i];
796 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
802 void Score_NicePrint_Player(entity to, entity p, float w)
812 s = strcat(s, p.netname);
815 i = strlennocol(s) - NAMEWIDTH;
817 s = substring(s, 0, strlen(s) - i);
820 s = strcat(s, strpad(i, ""));
825 for(i = 0; i < MAX_SCORE; ++i)
826 if(scores_label[i] != "")
828 fl = scores_flags[i];
830 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
836 void Score_NicePrint_Spectators(entity to)
838 print_to(to, "Spectators:");
841 void Score_NicePrint_Spectator(entity to, entity p)
843 print_to(to, strcat(" ", p.netname));
846 .float score_dummyfield;
847 void Score_NicePrint(entity to)
854 for(i = 0; i < MAX_SCORE; ++i)
855 if(scores_label[i] != "")
857 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
859 p = PlayerScore_Sort(score_dummyfield);
862 if(!teamscores_entities_count)
863 Score_NicePrint_Team(to, t, w);
866 if(teamscores_entities_count)
868 Score_NicePrint_Team(to, p.team, w);
869 Score_NicePrint_Player(to, p, w);
876 if(p.classname != "player")
879 Score_NicePrint_Spectators(to);
880 Score_NicePrint_Spectator(to, p);
885 void PlayerScore_PlayerStats(entity p)
891 for(i = 0; i < MAX_SCORE; ++i)
892 if(s.(scores[i]) != 0)
893 if(scores_label[i] != "")
894 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_SCOREBOARD, scores_label[i]), s.(scores[i]));
897 void PlayerScore_TeamStats(void)
901 for(t = 0; t < 16; ++t)
903 sk = teamscorekeepers[t];
906 for(i = 0; i < MAX_TEAMSCORE; ++i)
907 if(sk.(teamscores[i]) != 0)
908 if(teamscores_label[i] != "")
909 PlayerStats_TeamScore(t, strcat(PLAYERSTATS_SCOREBOARD, teamscores_label[i]), sk.(teamscores[i]));