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)
249 if(g_arena || g_ca) return;
250 if(g_race && !g_race_qualifying) return;
252 sk = player.scorekeeper;
253 for(i = 0; i < MAX_SCORE; ++i)
255 if(sk.(scores[i]) != 0)
256 if(scores_label[i] != "")
257 sk.SendFlags |= pow(2, i);
262 void Score_ClearAll()
266 FOR_EACH_CLIENTSLOT(p)
271 for(i = 0; i < MAX_SCORE; ++i)
273 if(sk.(scores[i]) != 0)
274 if(scores_label[i] != "")
275 sk.SendFlags |= pow(2, i);
279 for(t = 0; t < 16; ++t)
281 sk = teamscorekeepers[t];
284 for(i = 0; i < MAX_TEAMSCORE; ++i)
286 if(sk.(teamscores[i]) != 0)
287 if(teamscores_label[i] != "")
288 sk.SendFlags |= pow(2, i);
289 sk.(teamscores[i]) = 0;
294 void PlayerScore_Attach(entity player)
297 if(player.scorekeeper)
298 error("player already has a scorekeeper");
301 Net_LinkEntity(sk, FALSE, 0, PlayerScore_SendEntity);
302 player.scorekeeper = sk;
305 void PlayerScore_Detach(entity player)
307 if(!player.scorekeeper)
308 error("player has no scorekeeper");
309 remove(player.scorekeeper);
310 player.scorekeeper = world;
313 float PlayerScore_Add(entity player, float scorefield, float score)
318 if not(g_lms && scorefield == SP_LMS_RANK) // allow writing to this field in intermission as it is needed for newly joining players
321 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
322 s = player.scorekeeper;
327 backtrace("Adding score to unknown player!");
331 if(scores_label[scorefield] != "")
332 s.SendFlags |= pow(2, scorefield);
333 return (s.(scores[scorefield]) += score);
336 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
339 r = PlayerScore_Add(player, pscorefield, score);
340 if(teamscores_entities_count) // only for teamplay
341 r = TeamScore_Add(player, tscorefield, score);
345 float PlayerScore_Compare(entity t1, entity t2)
347 if(!t1 || !t2) return (!t2) - !t1;
351 for(i = 0; i < MAX_SCORE; ++i)
355 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
360 void WinningConditionHelper()
366 entity winnerscorekeeper;
367 entity secondscorekeeper;
371 s = strcat(s, ":", cvar_string("g_xonoticversion"));
372 s = strcat(s, "::", GetPlayerScoreString(world, 2)); // make this 1 once we can
374 fullstatus = cvar("g_full_getstatus_responses");
376 if(teamscores_entities_count)
380 s = strcat(s, ":", GetTeamScoreString(0, 1));
381 for(t = 0; t < 16; ++t)
382 if(teamscorekeepers[t])
383 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
385 WinningConditionHelper_winnerteam = -1;
386 WinningConditionHelper_secondteam = -1;
387 winnerscorekeeper = world;
388 secondscorekeeper = world;
389 for(t = 0; t < 16; ++t)
391 sk = teamscorekeepers[t];
392 c = TeamScore_Compare(winnerscorekeeper, sk);
395 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
396 WinningConditionHelper_winnerteam = t + 1;
397 secondscorekeeper = winnerscorekeeper;
398 winnerscorekeeper = sk;
402 c = TeamScore_Compare(secondscorekeeper, sk);
405 WinningConditionHelper_secondteam = t + 1;
406 secondscorekeeper = sk;
411 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
412 if(WinningConditionHelper_equality)
413 WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
415 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
416 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
417 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
418 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
420 WinningConditionHelper_winner = world; // not supported in teamplay
421 WinningConditionHelper_second = world; // not supported in teamplay
425 WinningConditionHelper_winner = world;
426 WinningConditionHelper_second = world;
427 winnerscorekeeper = world;
428 secondscorekeeper = world;
432 c = PlayerScore_Compare(winnerscorekeeper, sk);
435 WinningConditionHelper_second = WinningConditionHelper_winner;
436 WinningConditionHelper_winner = p;
437 secondscorekeeper = winnerscorekeeper;
438 winnerscorekeeper = sk;
442 c = PlayerScore_Compare(secondscorekeeper, sk);
445 WinningConditionHelper_second = p;
446 secondscorekeeper = sk;
451 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
452 if(WinningConditionHelper_equality)
453 WinningConditionHelper_winner = WinningConditionHelper_second = world;
455 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
456 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
457 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
458 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
460 WinningConditionHelper_winnerteam = -1; // no teamplay
461 WinningConditionHelper_secondteam = -1; // no teamplay
464 if(WinningConditionHelper_topscore == 0)
466 if(scores_flags_primary & SFL_ZERO_IS_WORST)
468 if(WinningConditionHelper_lowerisbetter)
469 WinningConditionHelper_topscore = 999999999;
471 WinningConditionHelper_topscore = -999999999;
473 WinningConditionHelper_equality = 0;
476 if(WinningConditionHelper_secondscore == 0)
478 if(scores_flags_primary & SFL_ZERO_IS_WORST)
480 if(WinningConditionHelper_lowerisbetter)
481 WinningConditionHelper_secondscore = 999999999;
483 WinningConditionHelper_secondscore = -999999999;
488 strunzone(worldstatus);
489 worldstatus = strzone(s);
495 s = GetPlayerScoreString(p, 1);
496 if(clienttype(p) == CLIENTTYPE_REAL)
497 s = strcat(s, ":human");
499 s = strcat(s, ":bot");
500 if(p.classname != "player" && !g_arena && !g_ca && !g_lms)
501 s = strcat(s, ":spectator");
505 if(p.classname == "player" || g_arena || g_ca || g_lms)
506 s = GetPlayerScoreString(p, 2);
512 strunzone(p.clientstatus);
513 p.clientstatus = strzone(s);
517 string GetScoreLogLabel(string label, float fl)
519 if(fl & SFL_LOWER_IS_BETTER)
520 label = strcat(label, "<");
521 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
522 label = strcat(label, "!!");
523 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
524 label = strcat(label, "!");
528 string GetPlayerScoreString(entity pl, float shortString)
539 for(i = 0; i < MAX_SCORE; ++i)
540 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
544 out = strcat(out, GetScoreLogLabel(l, f), ",");
547 for(i = 0; i < MAX_SCORE; ++i)
548 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
552 out = strcat(out, GetScoreLogLabel(l, f), ",");
555 for(i = 0; i < MAX_SCORE; ++i)
556 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
557 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
561 out = strcat(out, GetScoreLogLabel(l, f), ",");
563 out = substring(out, 0, strlen(out) - 1);
565 else if((sk = pl.scorekeeper))
567 for(i = 0; i < MAX_SCORE; ++i)
568 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
569 out = strcat(out, ftos(sk.(scores[i])), ",");
571 for(i = 0; i < MAX_SCORE; ++i)
572 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
573 out = strcat(out, ftos(sk.(scores[i])), ",");
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)
578 out = strcat(out, ftos(sk.(scores[i])), ",");
579 out = substring(out, 0, strlen(out) - 1);
584 string GetTeamScoreString(float tm, float shortString)
595 for(i = 0; i < MAX_SCORE; ++i)
596 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
598 f = teamscores_flags[i];
599 l = teamscores_label[i];
600 out = strcat(out, GetScoreLogLabel(l, f), ",");
603 for(i = 0; i < MAX_SCORE; ++i)
604 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
606 f = teamscores_flags[i];
607 l = teamscores_label[i];
608 out = strcat(out, GetScoreLogLabel(l, f), ",");
611 for(i = 0; i < MAX_SCORE; ++i)
612 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
613 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
615 f = teamscores_flags[i];
616 l = teamscores_label[i];
617 out = strcat(out, GetScoreLogLabel(l, f), ",");
619 out = substring(out, 0, strlen(out) - 1);
621 else if((sk = teamscorekeepers[tm - 1]))
623 for(i = 0; i < MAX_TEAMSCORE; ++i)
624 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
625 out = strcat(out, ftos(sk.(teamscores[i])), ",");
627 for(i = 0; i < MAX_TEAMSCORE; ++i)
628 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
629 out = strcat(out, ftos(sk.(teamscores[i])), ",");
631 for(i = 0; i < MAX_TEAMSCORE; ++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)
634 out = strcat(out, ftos(sk.(teamscores[i])), ",");
635 out = substring(out, 0, strlen(out) - 1);
640 float PlayerTeamScore_Compare(entity p1, entity p2)
642 if(teamscores_entities_count)
643 if(p1.team != p2.team)
647 t1 = teamscorekeepers[p1.team - 1];
648 t2 = teamscorekeepers[p2.team - 1];
649 r = TeamScore_Compare(t1, t2);
650 if(r == 0) // ensure a deterministic order
651 r = p1.team - p2.team;
655 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
658 entity PlayerScore_Sort(.float field)
660 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
668 FOR_EACH_PLAYER(p) if(p.scorekeeper)
673 // Now plist points to the whole list.
675 pfirst = plast = world;
680 pprev = pbestprev = world;
682 for(p = plist; (pprev = p), (p = p.chain); )
684 if(PlayerTeamScore_Compare(p, pbest) > 0)
691 // remove pbest out of the chain
692 if(pbestprev == world)
695 pbestprev.chain = pbest.chain;
710 float TeamScore_GetCompareValue(float t)
715 if(t <= 0 || t >= 16)
719 error("Reading score of invalid team!");
722 sk = teamscorekeepers[t - 1];
725 s = sk.teamscores_primary;
726 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
729 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
735 #define SCORESWIDTH 58
736 // TODO put this somewhere in common?
737 string Score_NicePrint_ItemColor(float vflags)
739 if(vflags & SFL_SORT_PRIO_PRIMARY)
741 else if(vflags & SFL_SORT_PRIO_SECONDARY)
747 void Score_NicePrint_Team(entity to, float t, float w)
755 sk = teamscorekeepers[t - 1];
758 s = strcat(s, ColoredTeamName(t));
759 for(i = 0; i < MAX_TEAMSCORE; ++i)
760 if(teamscores_label[i] != "")
762 fl = teamscores_flags[i];
763 sc = sk.(teamscores[i]);
764 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
770 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
772 for(i = 0; i < MAX_SCORE; ++i)
773 if(scores_label[i] != "")
775 fl = scores_flags[i];
776 s2 = scores_label[i];
777 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
783 void Score_NicePrint_Player(entity to, entity p, float w)
793 s = strcat(s, p.netname);
796 i = strlennocol(s) - NAMEWIDTH;
798 s = substring(s, 0, strlen(s) - i);
801 s = strcat(s, strpad(i, ""));
806 for(i = 0; i < MAX_SCORE; ++i)
807 if(scores_label[i] != "")
809 fl = scores_flags[i];
811 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
817 void Score_NicePrint_Spectators(entity to)
819 print_to(to, "Spectators:");
822 void Score_NicePrint_Spectator(entity to, entity p)
824 print_to(to, strcat(" ", p.netname));
827 .float score_dummyfield;
828 void Score_NicePrint(entity to)
835 for(i = 0; i < MAX_SCORE; ++i)
836 if(scores_label[i] != "")
838 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
840 p = PlayerScore_Sort(score_dummyfield);
843 if(!teamscores_entities_count)
844 Score_NicePrint_Team(to, t, w);
847 if(teamscores_entities_count)
849 Score_NicePrint_Team(to, p.team, w);
850 Score_NicePrint_Player(to, p, w);
857 if(p.classname != "player")
860 Score_NicePrint_Spectators(to);
861 Score_NicePrint_Spectator(to, p);