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;
93 float TeamScore_AddToTeam(float t, float scorefield, float score)
100 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
101 if(t <= 0 || t >= 16)
105 error("Adding score to invalid team!");
107 s = teamscorekeepers[t - 1];
112 error("Adding score to unknown team!");
115 if(teamscores_label[scorefield] != "")
116 s.SendFlags |= pow(2, scorefield);
117 return (s.(teamscores[scorefield]) += score);
120 float TeamScore_Add(entity player, float scorefield, float score)
122 return TeamScore_AddToTeam(player.team, scorefield, score);
125 float TeamScore_Compare(entity t1, entity t2)
127 if(!t1 || !t2) return (!t2) - !t1;
131 for(i = 0; i < MAX_TEAMSCORE; ++i)
135 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
141 * the scoreinfo entity
144 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
146 scores_label[i] = label;
147 scores_flags[i] = scoreflags;
148 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
150 scores_primary = scores[i];
151 scores_flags_primary = scoreflags;
155 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
157 teamscores_label[i] = label;
158 teamscores_flags[i] = scoreflags;
159 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
161 teamscores_primary = teamscores[i];
162 teamscores_flags_primary = scoreflags;
166 float ScoreInfo_SendEntity(entity to, float sf)
169 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
170 WriteByte(MSG_ENTITY, game);
171 for(i = 0; i < MAX_SCORE; ++i)
173 WriteString(MSG_ENTITY, scores_label[i]);
174 WriteByte(MSG_ENTITY, scores_flags[i]);
176 for(i = 0; i < MAX_TEAMSCORE; ++i)
178 WriteString(MSG_ENTITY, teamscores_label[i]);
179 WriteByte(MSG_ENTITY, teamscores_flags[i]);
184 void ScoreInfo_Init(float teams)
186 if(scores_initialized)
188 scores_initialized.SendFlags |= 1; // force a resend
192 scores_initialized = spawn();
193 scores_initialized.classname = "ent_client_scoreinfo";
194 Net_LinkEntity(scores_initialized, FALSE, 0, ScoreInfo_SendEntity);
197 TeamScore_Spawn(COLOR_TEAM1, "Red");
199 TeamScore_Spawn(COLOR_TEAM2, "Blue");
201 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
203 TeamScore_Spawn(COLOR_TEAM4, "Pink");
207 * per-player score entities
210 float PlayerScore_SendEntity(entity to, float sendflags)
212 float i, p, longflags;
214 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
215 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
218 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
219 if(self.scores[i] > 127 || self.scores[i] <= -128)
223 WriteByte(MSG_ENTITY, sendflags);
224 WriteByte(MSG_ENTITY, longflags);
226 WriteShort(MSG_ENTITY, sendflags);
227 WriteShort(MSG_ENTITY, longflags);
229 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
233 WriteInt24_t(MSG_ENTITY, self.scores[i]);
235 WriteChar(MSG_ENTITY, self.scores[i]);
241 void PlayerScore_Clear(entity player)
246 if(teamscores_entities_count)
250 if(g_arena || g_ca) return;
251 if(g_race && !g_race_qualifying) return;
253 sk = player.scorekeeper;
254 for(i = 0; i < MAX_SCORE; ++i)
256 if(sk.(scores[i]) != 0)
257 if(scores_label[i] != "")
258 sk.SendFlags |= pow(2, i);
260 // do NOT clear scores_accumulated!
264 void Score_ClearAll()
268 FOR_EACH_CLIENTSLOT(p)
273 for(i = 0; i < MAX_SCORE; ++i)
275 if(sk.(scores[i]) != 0)
276 if(scores_label[i] != "")
277 sk.SendFlags |= pow(2, i);
279 // do NOT clear scores_accumulated!
282 for(t = 0; t < 16; ++t)
284 sk = teamscorekeepers[t];
287 for(i = 0; i < MAX_TEAMSCORE; ++i)
289 if(sk.(teamscores[i]) != 0)
290 if(teamscores_label[i] != "")
291 sk.SendFlags |= pow(2, i);
292 sk.(teamscores[i]) = 0;
297 void PlayerScore_Attach(entity player)
300 if(player.scorekeeper)
301 error("player already has a scorekeeper");
304 Net_LinkEntity(sk, FALSE, 0, PlayerScore_SendEntity);
305 player.scorekeeper = sk;
308 void PlayerScore_Detach(entity player)
310 if(!player.scorekeeper)
311 error("player has no scorekeeper");
312 remove(player.scorekeeper);
313 player.scorekeeper = world;
316 float PlayerScore_Add(entity player, float scorefield, float score)
321 if not(g_lms && scorefield == SP_LMS_RANK) // allow writing to this field in intermission as it is needed for newly joining players
324 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
325 s = player.scorekeeper;
330 backtrace("Adding score to unknown player!");
334 if(scores_label[scorefield] != "")
335 s.SendFlags |= pow(2, scorefield);
336 s.(scores_accumulated[scorefield]) += score;
337 return (s.(scores[scorefield]) += score);
340 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
343 r = PlayerScore_Add(player, pscorefield, score);
344 if(teamscores_entities_count) // only for teamplay
345 r = TeamScore_Add(player, tscorefield, score);
349 float PlayerScore_Compare(entity t1, entity t2)
351 if(!t1 || !t2) return (!t2) - !t1;
355 for(i = 0; i < MAX_SCORE; ++i)
359 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
364 void WinningConditionHelper()
370 entity winnerscorekeeper;
371 entity secondscorekeeper;
375 s = strcat(s, ":", autocvar_g_xonoticversion);
376 s = strcat(s, ":P", ftos(cvar_purechanges_count));
377 s = strcat(s, "::", GetPlayerScoreString(world, 1)); // make this 1 once we can, note: this doesn't contain any :<letter>
379 fullstatus = autocvar_g_full_getstatus_responses;
381 if(teamscores_entities_count)
385 s = strcat(s, ":", GetTeamScoreString(0, 1));
386 for(t = 0; t < 16; ++t)
387 if(teamscorekeepers[t])
388 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
390 WinningConditionHelper_winnerteam = -1;
391 WinningConditionHelper_secondteam = -1;
392 winnerscorekeeper = world;
393 secondscorekeeper = world;
394 for(t = 0; t < 16; ++t)
396 sk = teamscorekeepers[t];
397 c = TeamScore_Compare(winnerscorekeeper, sk);
400 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
401 WinningConditionHelper_winnerteam = t + 1;
402 secondscorekeeper = winnerscorekeeper;
403 winnerscorekeeper = sk;
407 c = TeamScore_Compare(secondscorekeeper, sk);
410 WinningConditionHelper_secondteam = t + 1;
411 secondscorekeeper = sk;
416 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
417 if(WinningConditionHelper_equality)
418 WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
420 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
421 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
422 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
423 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
425 WinningConditionHelper_winner = world; // not supported in teamplay
426 WinningConditionHelper_second = world; // not supported in teamplay
430 WinningConditionHelper_winner = world;
431 WinningConditionHelper_second = world;
432 winnerscorekeeper = world;
433 secondscorekeeper = world;
437 c = PlayerScore_Compare(winnerscorekeeper, sk);
440 WinningConditionHelper_second = WinningConditionHelper_winner;
441 WinningConditionHelper_winner = p;
442 secondscorekeeper = winnerscorekeeper;
443 winnerscorekeeper = sk;
447 c = PlayerScore_Compare(secondscorekeeper, sk);
450 WinningConditionHelper_second = p;
451 secondscorekeeper = sk;
456 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
457 if(WinningConditionHelper_equality)
458 WinningConditionHelper_winner = WinningConditionHelper_second = world;
460 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
461 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
462 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
463 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
465 WinningConditionHelper_winnerteam = -1; // no teamplay
466 WinningConditionHelper_secondteam = -1; // no teamplay
469 if(WinningConditionHelper_topscore == 0)
471 if(scores_flags_primary & SFL_ZERO_IS_WORST)
473 if(WinningConditionHelper_lowerisbetter)
474 WinningConditionHelper_topscore = 999999999;
476 WinningConditionHelper_topscore = -999999999;
478 WinningConditionHelper_equality = 0;
481 if(WinningConditionHelper_secondscore == 0)
483 if(scores_flags_primary & SFL_ZERO_IS_WORST)
485 if(WinningConditionHelper_lowerisbetter)
486 WinningConditionHelper_secondscore = 999999999;
488 WinningConditionHelper_secondscore = -999999999;
493 strunzone(worldstatus);
494 worldstatus = strzone(s);
500 s = GetPlayerScoreString(p, 1);
501 if(clienttype(p) == CLIENTTYPE_REAL)
502 s = strcat(s, ":human");
504 s = strcat(s, ":bot");
505 if(p.classname != "player" && !g_arena && !g_ca && !g_lms)
506 s = strcat(s, ":spectator");
510 if(p.classname == "player" || g_arena || g_ca || g_lms)
511 s = GetPlayerScoreString(p, 2);
517 strunzone(p.clientstatus);
518 p.clientstatus = strzone(s);
522 string GetScoreLogLabel(string label, float fl)
524 if(fl & SFL_LOWER_IS_BETTER)
525 label = strcat(label, "<");
526 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
527 label = strcat(label, "!!");
528 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
529 label = strcat(label, "!");
533 string GetPlayerScoreString(entity pl, float shortString)
544 for(i = 0; i < MAX_SCORE; ++i)
545 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
549 out = strcat(out, GetScoreLogLabel(l, f), ",");
552 for(i = 0; i < MAX_SCORE; ++i)
553 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
557 out = strcat(out, GetScoreLogLabel(l, f), ",");
560 for(i = 0; i < MAX_SCORE; ++i)
561 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
562 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
566 out = strcat(out, GetScoreLogLabel(l, f), ",");
568 out = substring(out, 0, strlen(out) - 1);
570 else if((sk = pl.scorekeeper))
572 for(i = 0; i < MAX_SCORE; ++i)
573 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
574 out = strcat(out, ftos(sk.(scores[i])), ",");
576 for(i = 0; i < MAX_SCORE; ++i)
577 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
578 out = strcat(out, ftos(sk.(scores[i])), ",");
580 for(i = 0; i < MAX_SCORE; ++i)
581 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
582 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
583 out = strcat(out, ftos(sk.(scores[i])), ",");
584 out = substring(out, 0, strlen(out) - 1);
589 string GetTeamScoreString(float tm, float shortString)
600 for(i = 0; i < MAX_SCORE; ++i)
601 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
603 f = teamscores_flags[i];
604 l = teamscores_label[i];
605 out = strcat(out, GetScoreLogLabel(l, f), ",");
608 for(i = 0; i < MAX_SCORE; ++i)
609 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
611 f = teamscores_flags[i];
612 l = teamscores_label[i];
613 out = strcat(out, GetScoreLogLabel(l, f), ",");
616 for(i = 0; i < MAX_SCORE; ++i)
617 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
618 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
620 f = teamscores_flags[i];
621 l = teamscores_label[i];
622 out = strcat(out, GetScoreLogLabel(l, f), ",");
624 out = substring(out, 0, strlen(out) - 1);
626 else if((sk = teamscorekeepers[tm - 1]))
628 for(i = 0; i < MAX_TEAMSCORE; ++i)
629 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
630 out = strcat(out, ftos(sk.(teamscores[i])), ",");
632 for(i = 0; i < MAX_TEAMSCORE; ++i)
633 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
634 out = strcat(out, ftos(sk.(teamscores[i])), ",");
636 for(i = 0; i < MAX_TEAMSCORE; ++i)
637 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
638 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
639 out = strcat(out, ftos(sk.(teamscores[i])), ",");
640 out = substring(out, 0, strlen(out) - 1);
645 float PlayerTeamScore_Compare(entity p1, entity p2)
647 if(teamscores_entities_count)
648 if(p1.team != p2.team)
652 t1 = teamscorekeepers[p1.team - 1];
653 t2 = teamscorekeepers[p2.team - 1];
654 r = TeamScore_Compare(t1, t2);
655 if(r == 0) // ensure a deterministic order
656 r = p1.team - p2.team;
660 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
663 entity PlayerScore_Sort(.float field)
665 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
673 FOR_EACH_PLAYER(p) if(p.scorekeeper)
678 // Now plist points to the whole list.
680 pfirst = plast = world;
685 pprev = pbestprev = world;
687 for(p = plist; (pprev = p), (p = p.chain); )
689 if(PlayerTeamScore_Compare(p, pbest) > 0)
696 // remove pbest out of the chain
697 if(pbestprev == world)
700 pbestprev.chain = pbest.chain;
715 float TeamScore_GetCompareValue(float t)
720 if(t <= 0 || t >= 16)
724 error("Reading score of invalid team!");
727 sk = teamscorekeepers[t - 1];
730 s = sk.teamscores_primary;
731 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
734 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
740 #define SCORESWIDTH 58
741 // TODO put this somewhere in common?
742 string Score_NicePrint_ItemColor(float vflags)
744 if(vflags & SFL_SORT_PRIO_PRIMARY)
746 else if(vflags & SFL_SORT_PRIO_SECONDARY)
752 void Score_NicePrint_Team(entity to, float t, float w)
760 sk = teamscorekeepers[t - 1];
763 s = strcat(s, ColoredTeamName(t));
764 for(i = 0; i < MAX_TEAMSCORE; ++i)
765 if(teamscores_label[i] != "")
767 fl = teamscores_flags[i];
768 sc = sk.(teamscores[i]);
769 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
775 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
777 for(i = 0; i < MAX_SCORE; ++i)
778 if(scores_label[i] != "")
780 fl = scores_flags[i];
781 s2 = scores_label[i];
782 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
788 void Score_NicePrint_Player(entity to, entity p, float w)
798 s = strcat(s, p.netname);
801 i = strlennocol(s) - NAMEWIDTH;
803 s = substring(s, 0, strlen(s) - i);
806 s = strcat(s, strpad(i, ""));
811 for(i = 0; i < MAX_SCORE; ++i)
812 if(scores_label[i] != "")
814 fl = scores_flags[i];
816 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
822 void Score_NicePrint_Spectators(entity to)
824 print_to(to, "Spectators:");
827 void Score_NicePrint_Spectator(entity to, entity p)
829 print_to(to, strcat(" ", p.netname));
832 .float score_dummyfield;
833 void Score_NicePrint(entity to)
840 for(i = 0; i < MAX_SCORE; ++i)
841 if(scores_label[i] != "")
843 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
845 p = PlayerScore_Sort(score_dummyfield);
848 if(!teamscores_entities_count)
849 Score_NicePrint_Team(to, t, w);
852 if(teamscores_entities_count)
854 Score_NicePrint_Team(to, p.team, w);
855 Score_NicePrint_Player(to, p, w);
862 if(p.classname != "player")
865 Score_NicePrint_Spectators(to);
866 Score_NicePrint_Spectator(to, p);