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)
54 WriteByte(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
55 WriteByte(MSG_ENTITY, self.team - 1);
56 #if MAX_TEAMSCORE <= 3
57 for(i = 0; i < MAX_TEAMSCORE; ++i)
58 WriteShort(MSG_ENTITY, self.teamscores[i]);
60 #if MAX_TEAMSCORE <= 8
61 WriteByte(MSG_ENTITY, sendflags);
63 WriteShort(MSG_ENTITY, sendflags);
66 for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
68 WriteShort(MSG_ENTITY, self.teamscores[i]);
74 void TeamScore_Spawn(float t, string name)
78 ts.classname = "csqc_score_team";
79 ts.netname = name; // not used yet, FIXME
81 Net_LinkEntity(ts, FALSE, 0, TeamScore_SendEntity);
82 teamscorekeepers[t - 1] = ts;
83 ++teamscores_entities_count;
86 float TeamScore_AddToTeam(float t, float scorefield, float score)
93 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
95 error("Adding score to invalid team!");
96 s = teamscorekeepers[t - 1];
98 error("Adding score to unknown team!");
100 if(teamscores_label[scorefield] != "")
101 s.SendFlags |= pow(2, scorefield);
102 return (s.(teamscores[scorefield]) += score);
105 float TeamScore_Add(entity player, float scorefield, float score)
107 return TeamScore_AddToTeam(player.team, scorefield, score);
110 float TeamScore_Compare(entity t1, entity t2)
112 if(!t1 || !t2) return (!t2) - !t1;
116 for(i = 0; i < MAX_TEAMSCORE; ++i)
120 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
126 * the scoreinfo entity
129 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
131 scores_label[i] = label;
132 scores_flags[i] = scoreflags;
133 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
135 scores_primary = scores[i];
136 scores_flags_primary = scoreflags;
140 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
142 teamscores_label[i] = label;
143 teamscores_flags[i] = scoreflags;
144 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
146 teamscores_primary = teamscores[i];
147 teamscores_flags_primary = scoreflags;
151 float ScoreInfo_SendEntity(entity to, float sf)
154 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
155 WriteByte(MSG_ENTITY, game);
156 for(i = 0; i < MAX_SCORE; ++i)
158 WriteString(MSG_ENTITY, scores_label[i]);
159 WriteByte(MSG_ENTITY, scores_flags[i]);
161 for(i = 0; i < MAX_TEAMSCORE; ++i)
163 WriteString(MSG_ENTITY, teamscores_label[i]);
164 WriteByte(MSG_ENTITY, teamscores_flags[i]);
169 void ScoreInfo_Init(float teams)
171 if(scores_initialized)
173 scores_initialized.SendFlags |= 1; // force a resend
177 scores_initialized = spawn();
178 scores_initialized.classname = "ent_client_scoreinfo";
179 Net_LinkEntity(scores_initialized, FALSE, 0, ScoreInfo_SendEntity);
182 TeamScore_Spawn(COLOR_TEAM1, "Red");
184 TeamScore_Spawn(COLOR_TEAM2, "Blue");
186 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
188 TeamScore_Spawn(COLOR_TEAM4, "Pink");
192 * per-player score entities
195 float PlayerScore_SendEntity(entity to, float sendflags)
199 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
200 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
202 for(i = 0; i < MAX_SCORE; ++i)
203 WriteShort(MSG_ENTITY, self.scores[i]);
206 WriteByte(MSG_ENTITY, sendflags);
208 WriteShort(MSG_ENTITY, sendflags);
211 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
213 WriteShort(MSG_ENTITY, self.scores[i]);
219 void PlayerScore_Clear(entity player)
224 if(teamscores_entities_count)
228 if(g_race && !g_race_qualifying) return;
230 sk = player.scorekeeper;
231 for(i = 0; i < MAX_SCORE; ++i)
233 if(sk.(scores[i]) != 0)
234 if(scores_label[i] != "")
235 sk.SendFlags |= pow(2, i);
240 void Score_ClearAll()
244 FOR_EACH_CLIENTSLOT(p)
249 for(i = 0; i < MAX_SCORE; ++i)
251 if(sk.(scores[i]) != 0)
252 if(scores_label[i] != "")
253 sk.SendFlags |= pow(2, i);
257 for(t = 0; t < 16; ++t)
259 sk = teamscorekeepers[t];
262 for(i = 0; i < MAX_TEAMSCORE; ++i)
264 if(sk.(teamscores[i]) != 0)
265 if(teamscores_label[i] != "")
266 sk.SendFlags |= pow(2, i);
267 sk.(teamscores[i]) = 0;
272 void PlayerScore_Attach(entity player)
275 if(player.scorekeeper)
276 error("player already has a scorekeeper");
279 Net_LinkEntity(sk, FALSE, 0, PlayerScore_SendEntity);
280 player.scorekeeper = sk;
283 void PlayerScore_Detach(entity player)
285 if(!player.scorekeeper)
286 error("player has no scorekeeper");
287 remove(player.scorekeeper);
288 player.scorekeeper = world;
291 float PlayerScore_Add(entity player, float scorefield, float score)
298 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
299 s = player.scorekeeper;
301 error("Adding score to unknown player!");
303 if(scores_label[scorefield] != "")
304 s.SendFlags |= pow(2, scorefield);
305 return (s.(scores[scorefield]) += score);
308 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
311 r = PlayerScore_Add(player, pscorefield, score);
312 if(teamscores_entities_count) // only for teamplay
313 r = TeamScore_Add(player, tscorefield, score);
317 float PlayerScore_Compare(entity t1, entity t2)
319 if(!t1 || !t2) return (!t2) - !t1;
323 for(i = 0; i < MAX_SCORE; ++i)
327 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
332 void WinningConditionHelper()
340 s = strcat(s, ":", cvar_string("g_nexuizversion"));
341 s = strcat(s, "::", GetPlayerScoreString(world, 2)); // make this 1 once we can
343 fullstatus = cvar("g_full_getstatus_responses");
345 if(teamscores_entities_count)
349 s = strcat(s, ":", GetTeamScoreString(0, 1));
350 for(t = 0; t < 16; ++t)
351 if(teamscorekeepers[t])
352 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
354 WinningConditionHelper_equality = 0;
355 WinningConditionHelper_winnerteam = 0;
356 for(t = 1; t < 16; ++t)
359 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
360 sk2 = teamscorekeepers[t];
361 c = TeamScore_Compare(sk1, sk2);
363 WinningConditionHelper_equality = 1;
366 WinningConditionHelper_equality = 0;
367 WinningConditionHelper_winnerteam = t;
371 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
372 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
373 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
375 if(teamscores_flags_primary & SFL_TIME)
376 WinningConditionHelper_topscore /= 10;
378 if(WinningConditionHelper_equality)
379 WinningConditionHelper_winnerteam = -1;
381 WinningConditionHelper_winner = world;
382 if(WinningConditionHelper_equality)
383 WinningConditionHelper_winnerteam = -1;
385 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
387 if(WinningConditionHelper_topscore == 0)
389 if(scores_flags_primary & SFL_ZERO_IS_WORST)
391 if(WinningConditionHelper_lowerisbetter)
392 WinningConditionHelper_topscore = 999999999;
394 WinningConditionHelper_topscore = -999999999;
396 WinningConditionHelper_equality = 0;
401 WinningConditionHelper_equality = 0;
402 WinningConditionHelper_winner = world;
405 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
407 WinningConditionHelper_equality = 1;
410 WinningConditionHelper_equality = 0;
411 WinningConditionHelper_winner = p;
415 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
416 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
417 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
419 if(teamscores_flags_primary & SFL_TIME)
420 WinningConditionHelper_topscore /= 10;
422 WinningConditionHelper_winnerteam = -1;
423 if(WinningConditionHelper_equality)
424 WinningConditionHelper_winner = world;
426 if(WinningConditionHelper_topscore == 0)
428 if(scores_flags_primary & SFL_ZERO_IS_WORST)
430 if(WinningConditionHelper_lowerisbetter)
431 WinningConditionHelper_topscore = 999999999;
433 WinningConditionHelper_topscore = -999999999;
435 WinningConditionHelper_equality = 0;
440 strunzone(worldstatus);
441 worldstatus = strzone(s);
447 s = GetPlayerScoreString(p, 1);
448 if(clienttype(p) == CLIENTTYPE_REAL)
449 s = strcat(s, ":human");
451 s = strcat(s, ":bot");
452 if(p.classname == "player" || g_arena || g_lms)
453 s = strcat(s, ":", ftos(p.team));
455 s = strcat(s, ":spectator");
459 if(p.classname == "player" || g_arena || g_lms)
460 s = GetPlayerScoreString(p, 2);
466 strunzone(p.clientstatus);
467 p.clientstatus = strzone(s);
471 string GetScoreLogLabel(string label, float fl)
473 if(fl & SFL_LOWER_IS_BETTER)
474 label = strcat(label, "<");
475 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
476 label = strcat(label, "!!");
477 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
478 label = strcat(label, "!");
482 string GetPlayerScoreString(entity pl, float shortString)
493 for(i = 0; i < MAX_SCORE; ++i)
494 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
498 out = strcat(out, GetScoreLogLabel(l, f), ",");
501 for(i = 0; i < MAX_SCORE; ++i)
502 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
506 out = strcat(out, GetScoreLogLabel(l, f), ",");
509 for(i = 0; i < MAX_SCORE; ++i)
510 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
511 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
515 out = strcat(out, GetScoreLogLabel(l, f), ",");
517 out = substring(out, 0, strlen(out) - 1);
519 else if((sk = pl.scorekeeper))
521 for(i = 0; i < MAX_SCORE; ++i)
522 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
523 out = strcat(out, ftos(sk.(scores[i])), ",");
525 for(i = 0; i < MAX_SCORE; ++i)
526 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
527 out = strcat(out, ftos(sk.(scores[i])), ",");
529 for(i = 0; i < MAX_SCORE; ++i)
530 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
531 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
532 out = strcat(out, ftos(sk.(scores[i])), ",");
533 out = substring(out, 0, strlen(out) - 1);
538 string GetTeamScoreString(float tm, float shortString)
549 for(i = 0; i < MAX_SCORE; ++i)
550 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
552 f = teamscores_flags[i];
553 l = teamscores_label[i];
554 out = strcat(out, GetScoreLogLabel(l, f), ",");
557 for(i = 0; i < MAX_SCORE; ++i)
558 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
560 f = teamscores_flags[i];
561 l = teamscores_label[i];
562 out = strcat(out, GetScoreLogLabel(l, f), ",");
565 for(i = 0; i < MAX_SCORE; ++i)
566 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
567 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
569 f = teamscores_flags[i];
570 l = teamscores_label[i];
571 out = strcat(out, GetScoreLogLabel(l, f), ",");
573 out = substring(out, 0, strlen(out) - 1);
575 else if((sk = teamscorekeepers[tm - 1]))
577 for(i = 0; i < MAX_TEAMSCORE; ++i)
578 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
579 out = strcat(out, ftos(sk.(teamscores[i])), ",");
581 for(i = 0; i < MAX_TEAMSCORE; ++i)
582 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
583 out = strcat(out, ftos(sk.(teamscores[i])), ",");
585 for(i = 0; i < MAX_TEAMSCORE; ++i)
586 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
587 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
588 out = strcat(out, ftos(sk.(teamscores[i])), ",");
589 out = substring(out, 0, strlen(out) - 1);
594 float PlayerTeamScore_Compare(entity p1, entity p2)
596 if(teamscores_entities_count)
597 if(p1.team != p2.team)
601 t1 = teamscorekeepers[p1.team - 1];
602 t2 = teamscorekeepers[p2.team - 1];
603 r = TeamScore_Compare(t1, t2);
604 if(r == 0) // ensure a deterministic order
605 r = p1.team - p2.team;
609 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
612 entity PlayerScore_Sort(.float field)
614 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
622 FOR_EACH_PLAYER(p) if(p.scorekeeper)
627 // Now plist points to the whole list.
629 pfirst = plast = world;
634 pprev = pbestprev = world;
636 for(p = plist; (pprev = p), (p = p.chain); )
638 if(PlayerTeamScore_Compare(p, pbest) > 0)
645 // remove pbest out of the chain
646 if(pbestprev == world)
649 pbestprev.chain = pbest.chain;
664 float TeamScore_GetCompareValue(float t)
669 if(t <= 0 || t >= 16)
670 error("Reading score of invalid team!");
672 sk = teamscorekeepers[t - 1];
675 s = sk.teamscores_primary;
676 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
679 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
685 #define SCORESWIDTH 58
686 // TODO put this somewhere in common?
687 string Score_NicePrint_ItemColor(float vflags)
689 if(vflags & SFL_SORT_PRIO_PRIMARY)
691 else if(vflags & SFL_SORT_PRIO_SECONDARY)
697 void Score_NicePrint_Team(entity to, float t, float w)
705 sk = teamscorekeepers[t - 1];
708 s = strcat(s, ColoredTeamName(t));
709 for(i = 0; i < MAX_TEAMSCORE; ++i)
710 if(teamscores_label[i] != "")
712 fl = teamscores_flags[i];
713 sc = sk.(teamscores[i]);
714 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
720 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
722 for(i = 0; i < MAX_SCORE; ++i)
723 if(scores_label[i] != "")
725 fl = scores_flags[i];
726 s2 = scores_label[i];
727 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
733 void Score_NicePrint_Player(entity to, entity p, float w)
743 s = strcat(s, p.netname);
746 i = strlennocol(s) - NAMEWIDTH;
748 s = substring(s, 0, strlen(s) - i);
751 s = strcat(s, strpad(i, ""));
756 for(i = 0; i < MAX_SCORE; ++i)
757 if(scores_label[i] != "")
759 fl = scores_flags[i];
761 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
767 void Score_NicePrint_Spectators(entity to)
769 print_to(to, "Spectators:");
772 void Score_NicePrint_Spectator(entity to, entity p)
774 print_to(to, strcat(" ", p.netname));
777 .float score_dummyfield;
778 void Score_NicePrint(entity to)
785 for(i = 0; i < MAX_SCORE; ++i)
786 if(scores_label[i] != "")
788 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
790 p = PlayerScore_Sort(score_dummyfield);
793 if(!teamscores_entities_count)
794 Score_NicePrint_Team(to, t, w);
797 if(teamscores_entities_count)
799 Score_NicePrint_Team(to, p.team, w);
800 Score_NicePrint_Player(to, p, w);
807 if(p.classname != "player")
810 Score_NicePrint_Spectators(to);
811 Score_NicePrint_Spectator(to, p);