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(0)));
389 s = strcat(s, ":F", ftos(serverflags));
390 s = strcat(s, "::", GetPlayerScoreString(world, 1)); // make this 1 once we can, note: this doesn't contain any :<letter>
392 fullstatus = autocvar_g_full_getstatus_responses;
394 if(teamscores_entities_count)
398 s = strcat(s, ":", GetTeamScoreString(0, 1));
399 for(t = 0; t < 16; ++t)
400 if(teamscorekeepers[t])
401 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
403 WinningConditionHelper_winnerteam = -1;
404 WinningConditionHelper_secondteam = -1;
405 winnerscorekeeper = world;
406 secondscorekeeper = world;
407 for(t = 0; t < 16; ++t)
409 sk = teamscorekeepers[t];
410 c = TeamScore_Compare(winnerscorekeeper, sk);
413 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
414 WinningConditionHelper_winnerteam = t + 1;
415 secondscorekeeper = winnerscorekeeper;
416 winnerscorekeeper = sk;
420 c = TeamScore_Compare(secondscorekeeper, sk);
423 WinningConditionHelper_secondteam = t + 1;
424 secondscorekeeper = sk;
429 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
430 if(WinningConditionHelper_equality)
431 WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
433 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
434 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
435 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
436 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
438 WinningConditionHelper_winner = world; // not supported in teamplay
439 WinningConditionHelper_second = world; // not supported in teamplay
443 WinningConditionHelper_winner = world;
444 WinningConditionHelper_second = world;
445 winnerscorekeeper = world;
446 secondscorekeeper = world;
450 c = PlayerScore_Compare(winnerscorekeeper, sk);
453 WinningConditionHelper_second = WinningConditionHelper_winner;
454 WinningConditionHelper_winner = p;
455 secondscorekeeper = winnerscorekeeper;
456 winnerscorekeeper = sk;
460 c = PlayerScore_Compare(secondscorekeeper, sk);
463 WinningConditionHelper_second = p;
464 secondscorekeeper = sk;
469 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
470 if(WinningConditionHelper_equality)
471 WinningConditionHelper_winner = WinningConditionHelper_second = world;
473 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
474 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
475 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
476 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
478 WinningConditionHelper_winnerteam = -1; // no teamplay
479 WinningConditionHelper_secondteam = -1; // no teamplay
482 if(WinningConditionHelper_topscore == 0)
484 if(scores_flags_primary & SFL_ZERO_IS_WORST)
486 if(WinningConditionHelper_lowerisbetter)
487 WinningConditionHelper_topscore = 999999999;
489 WinningConditionHelper_topscore = -999999999;
491 WinningConditionHelper_equality = 0;
494 if(WinningConditionHelper_secondscore == 0)
496 if(scores_flags_primary & SFL_ZERO_IS_WORST)
498 if(WinningConditionHelper_lowerisbetter)
499 WinningConditionHelper_secondscore = 999999999;
501 WinningConditionHelper_secondscore = -999999999;
506 strunzone(worldstatus);
507 worldstatus = strzone(s);
513 s = GetPlayerScoreString(p, 1);
514 if(clienttype(p) == CLIENTTYPE_REAL)
515 s = strcat(s, ":human");
517 s = strcat(s, ":bot");
518 if(p.classname != "player" && !g_arena && !g_ca && !g_lms)
519 s = strcat(s, ":spectator");
523 if(p.classname == "player" || g_arena || g_ca || g_lms)
524 s = GetPlayerScoreString(p, 2);
530 strunzone(p.clientstatus);
531 p.clientstatus = strzone(s);
535 string GetScoreLogLabel(string label, float fl)
537 if(fl & SFL_LOWER_IS_BETTER)
538 label = strcat(label, "<");
539 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
540 label = strcat(label, "!!");
541 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
542 label = strcat(label, "!");
546 string GetPlayerScoreString(entity pl, float shortString)
557 for(i = 0; i < MAX_SCORE; ++i)
558 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
562 out = strcat(out, GetScoreLogLabel(l, f), ",");
565 for(i = 0; i < MAX_SCORE; ++i)
566 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
570 out = strcat(out, GetScoreLogLabel(l, f), ",");
573 for(i = 0; i < MAX_SCORE; ++i)
574 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
575 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
579 out = strcat(out, GetScoreLogLabel(l, f), ",");
581 out = substring(out, 0, strlen(out) - 1);
583 else if((sk = pl.scorekeeper))
585 for(i = 0; i < MAX_SCORE; ++i)
586 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
587 out = strcat(out, ftos(sk.(scores[i])), ",");
589 for(i = 0; i < MAX_SCORE; ++i)
590 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
591 out = strcat(out, ftos(sk.(scores[i])), ",");
593 for(i = 0; i < MAX_SCORE; ++i)
594 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
595 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
596 out = strcat(out, ftos(sk.(scores[i])), ",");
597 out = substring(out, 0, strlen(out) - 1);
602 string GetTeamScoreString(float tm, float shortString)
613 for(i = 0; i < MAX_SCORE; ++i)
614 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
616 f = teamscores_flags[i];
617 l = teamscores_label[i];
618 out = strcat(out, GetScoreLogLabel(l, f), ",");
621 for(i = 0; i < MAX_SCORE; ++i)
622 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
624 f = teamscores_flags[i];
625 l = teamscores_label[i];
626 out = strcat(out, GetScoreLogLabel(l, f), ",");
629 for(i = 0; i < MAX_SCORE; ++i)
630 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
631 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
633 f = teamscores_flags[i];
634 l = teamscores_label[i];
635 out = strcat(out, GetScoreLogLabel(l, f), ",");
637 out = substring(out, 0, strlen(out) - 1);
639 else if((sk = teamscorekeepers[tm - 1]))
641 for(i = 0; i < MAX_TEAMSCORE; ++i)
642 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
643 out = strcat(out, ftos(sk.(teamscores[i])), ",");
645 for(i = 0; i < MAX_TEAMSCORE; ++i)
646 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
647 out = strcat(out, ftos(sk.(teamscores[i])), ",");
649 for(i = 0; i < MAX_TEAMSCORE; ++i)
650 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
651 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
652 out = strcat(out, ftos(sk.(teamscores[i])), ",");
653 out = substring(out, 0, strlen(out) - 1);
658 float PlayerTeamScore_Compare(entity p1, entity p2)
660 if(teamscores_entities_count)
661 if(p1.team != p2.team)
665 t1 = teamscorekeepers[p1.team - 1];
666 t2 = teamscorekeepers[p2.team - 1];
667 r = TeamScore_Compare(t1, t2);
668 if(r == 0) // ensure a deterministic order
669 r = p1.team - p2.team;
673 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
676 entity PlayerScore_Sort(.float field)
678 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
686 FOR_EACH_PLAYER(p) if(p.scorekeeper)
691 // Now plist points to the whole list.
693 pfirst = plast = world;
698 pprev = pbestprev = world;
700 for(p = plist; (pprev = p), (p = p.chain); )
702 if(PlayerTeamScore_Compare(p, pbest) > 0)
709 // remove pbest out of the chain
710 if(pbestprev == world)
713 pbestprev.chain = pbest.chain;
728 float TeamScore_GetCompareValue(float t)
733 if(t <= 0 || t >= 16)
737 error("Reading score of invalid team!");
740 sk = teamscorekeepers[t - 1];
743 s = sk.teamscores_primary;
744 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
747 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
753 #define SCORESWIDTH 58
754 // TODO put this somewhere in common?
755 string Score_NicePrint_ItemColor(float vflags)
757 if(vflags & SFL_SORT_PRIO_PRIMARY)
759 else if(vflags & SFL_SORT_PRIO_SECONDARY)
765 void Score_NicePrint_Team(entity to, float t, float w)
773 sk = teamscorekeepers[t - 1];
776 s = strcat(s, ColoredTeamName(t));
777 for(i = 0; i < MAX_TEAMSCORE; ++i)
778 if(teamscores_label[i] != "")
780 fl = teamscores_flags[i];
781 sc = sk.(teamscores[i]);
782 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
788 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
790 for(i = 0; i < MAX_SCORE; ++i)
791 if(scores_label[i] != "")
793 fl = scores_flags[i];
794 s2 = scores_label[i];
795 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
801 void Score_NicePrint_Player(entity to, entity p, float w)
811 s = strcat(s, p.netname);
814 i = strlennocol(s) - NAMEWIDTH;
816 s = substring(s, 0, strlen(s) - i);
819 s = strcat(s, strpad(i, ""));
824 for(i = 0; i < MAX_SCORE; ++i)
825 if(scores_label[i] != "")
827 fl = scores_flags[i];
829 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
835 void Score_NicePrint_Spectators(entity to)
837 print_to(to, "Spectators:");
840 void Score_NicePrint_Spectator(entity to, entity p)
842 print_to(to, strcat(" ", p.netname));
845 .float score_dummyfield;
846 void Score_NicePrint(entity to)
853 for(i = 0; i < MAX_SCORE; ++i)
854 if(scores_label[i] != "")
856 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
858 p = PlayerScore_Sort(score_dummyfield);
861 if(!teamscores_entities_count)
862 Score_NicePrint_Team(to, t, w);
865 if(teamscores_entities_count)
867 Score_NicePrint_Team(to, p.team, w);
868 Score_NicePrint_Player(to, p, w);
875 if(p.classname != "player")
878 Score_NicePrint_Spectators(to);
879 Score_NicePrint_Spectator(to, p);
884 void PlayerScore_PlayerStats(entity p)
890 for(i = 0; i < MAX_SCORE; ++i)
891 if(s.(scores[i]) != 0)
892 if(scores_label[i] != "")
893 PlayerStats_Event(s.owner, strcat(PLAYERSTATS_SCOREBOARD, scores_label[i]), s.(scores[i]));
896 void PlayerScore_TeamStats(void)
900 for(t = 0; t < 16; ++t)
902 sk = teamscorekeepers[t];
905 for(i = 0; i < MAX_TEAMSCORE; ++i)
906 if(sk.(teamscores[i]) != 0)
907 if(teamscores_label[i] != "")
908 PlayerStats_TeamScore(t, strcat(PLAYERSTATS_SCOREBOARD, teamscores_label[i]), sk.(teamscores[i]));