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)
296 if not(g_lms && scorefield == SP_LMS_RANK) // allow writing to this field in intermission as it is needed for newly joining players
299 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
300 s = player.scorekeeper;
302 error("Adding score to unknown player!");
304 if(scores_label[scorefield] != "")
305 s.SendFlags |= pow(2, scorefield);
306 return (s.(scores[scorefield]) += score);
309 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
312 r = PlayerScore_Add(player, pscorefield, score);
313 if(teamscores_entities_count) // only for teamplay
314 r = TeamScore_Add(player, tscorefield, score);
318 float PlayerScore_Compare(entity t1, entity t2)
320 if(!t1 || !t2) return (!t2) - !t1;
324 for(i = 0; i < MAX_SCORE; ++i)
328 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
333 void WinningConditionHelper()
341 s = strcat(s, ":", cvar_string("g_nexuizversion"));
342 s = strcat(s, "::", GetPlayerScoreString(world, 2)); // make this 1 once we can
344 fullstatus = cvar("g_full_getstatus_responses");
346 if(teamscores_entities_count)
350 s = strcat(s, ":", GetTeamScoreString(0, 1));
351 for(t = 0; t < 16; ++t)
352 if(teamscorekeepers[t])
353 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
355 WinningConditionHelper_equality = 0;
356 WinningConditionHelper_winnerteam = 0;
357 for(t = 1; t < 16; ++t)
360 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
361 sk2 = teamscorekeepers[t];
362 c = TeamScore_Compare(sk1, sk2);
364 WinningConditionHelper_equality = 1;
367 WinningConditionHelper_equality = 0;
368 WinningConditionHelper_winnerteam = t;
372 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
373 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
374 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
376 if(teamscores_flags_primary & SFL_TIME)
377 WinningConditionHelper_topscore /= 10;
379 if(WinningConditionHelper_equality)
380 WinningConditionHelper_winnerteam = -1;
382 WinningConditionHelper_winner = world;
383 if(WinningConditionHelper_equality)
384 WinningConditionHelper_winnerteam = -1;
386 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
388 if(WinningConditionHelper_topscore == 0)
390 if(scores_flags_primary & SFL_ZERO_IS_WORST)
392 if(WinningConditionHelper_lowerisbetter)
393 WinningConditionHelper_topscore = 999999999;
395 WinningConditionHelper_topscore = -999999999;
397 WinningConditionHelper_equality = 0;
402 WinningConditionHelper_equality = 0;
403 WinningConditionHelper_winner = world;
406 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
408 WinningConditionHelper_equality = 1;
411 WinningConditionHelper_equality = 0;
412 WinningConditionHelper_winner = p;
416 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
417 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
418 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
420 if(teamscores_flags_primary & SFL_TIME)
421 WinningConditionHelper_topscore /= 10;
423 WinningConditionHelper_winnerteam = -1;
424 if(WinningConditionHelper_equality)
425 WinningConditionHelper_winner = world;
427 if(WinningConditionHelper_topscore == 0)
429 if(scores_flags_primary & SFL_ZERO_IS_WORST)
431 if(WinningConditionHelper_lowerisbetter)
432 WinningConditionHelper_topscore = 999999999;
434 WinningConditionHelper_topscore = -999999999;
436 WinningConditionHelper_equality = 0;
441 strunzone(worldstatus);
442 worldstatus = strzone(s);
448 s = GetPlayerScoreString(p, 1);
449 if(clienttype(p) == CLIENTTYPE_REAL)
450 s = strcat(s, ":human");
452 s = strcat(s, ":bot");
453 if(p.classname != "player" && !g_arena && !g_lms)
454 s = strcat(s, ":spectator");
458 if(p.classname == "player" || g_arena || g_lms)
459 s = GetPlayerScoreString(p, 2);
465 strunzone(p.clientstatus);
466 p.clientstatus = strzone(s);
470 string GetScoreLogLabel(string label, float fl)
472 if(fl & SFL_LOWER_IS_BETTER)
473 label = strcat(label, "<");
474 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
475 label = strcat(label, "!!");
476 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
477 label = strcat(label, "!");
481 string GetPlayerScoreString(entity pl, float shortString)
492 for(i = 0; i < MAX_SCORE; ++i)
493 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
497 out = strcat(out, GetScoreLogLabel(l, f), ",");
500 for(i = 0; i < MAX_SCORE; ++i)
501 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
505 out = strcat(out, GetScoreLogLabel(l, f), ",");
508 for(i = 0; i < MAX_SCORE; ++i)
509 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
510 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
514 out = strcat(out, GetScoreLogLabel(l, f), ",");
516 out = substring(out, 0, strlen(out) - 1);
518 else if((sk = pl.scorekeeper))
520 for(i = 0; i < MAX_SCORE; ++i)
521 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
522 out = strcat(out, ftos(sk.(scores[i])), ",");
524 for(i = 0; i < MAX_SCORE; ++i)
525 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
526 out = strcat(out, ftos(sk.(scores[i])), ",");
528 for(i = 0; i < MAX_SCORE; ++i)
529 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
530 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
531 out = strcat(out, ftos(sk.(scores[i])), ",");
532 out = substring(out, 0, strlen(out) - 1);
537 string GetTeamScoreString(float tm, float shortString)
548 for(i = 0; i < MAX_SCORE; ++i)
549 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
551 f = teamscores_flags[i];
552 l = teamscores_label[i];
553 out = strcat(out, GetScoreLogLabel(l, f), ",");
556 for(i = 0; i < MAX_SCORE; ++i)
557 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
559 f = teamscores_flags[i];
560 l = teamscores_label[i];
561 out = strcat(out, GetScoreLogLabel(l, f), ",");
564 for(i = 0; i < MAX_SCORE; ++i)
565 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
566 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
568 f = teamscores_flags[i];
569 l = teamscores_label[i];
570 out = strcat(out, GetScoreLogLabel(l, f), ",");
572 out = substring(out, 0, strlen(out) - 1);
574 else if((sk = teamscorekeepers[tm - 1]))
576 for(i = 0; i < MAX_TEAMSCORE; ++i)
577 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
578 out = strcat(out, ftos(sk.(teamscores[i])), ",");
580 for(i = 0; i < MAX_TEAMSCORE; ++i)
581 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
582 out = strcat(out, ftos(sk.(teamscores[i])), ",");
584 for(i = 0; i < MAX_TEAMSCORE; ++i)
585 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
586 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
587 out = strcat(out, ftos(sk.(teamscores[i])), ",");
588 out = substring(out, 0, strlen(out) - 1);
593 float PlayerTeamScore_Compare(entity p1, entity p2)
595 if(teamscores_entities_count)
596 if(p1.team != p2.team)
600 t1 = teamscorekeepers[p1.team - 1];
601 t2 = teamscorekeepers[p2.team - 1];
602 r = TeamScore_Compare(t1, t2);
603 if(r == 0) // ensure a deterministic order
604 r = p1.team - p2.team;
608 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
611 entity PlayerScore_Sort(.float field)
613 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
621 FOR_EACH_PLAYER(p) if(p.scorekeeper)
626 // Now plist points to the whole list.
628 pfirst = plast = world;
633 pprev = pbestprev = world;
635 for(p = plist; (pprev = p), (p = p.chain); )
637 if(PlayerTeamScore_Compare(p, pbest) > 0)
644 // remove pbest out of the chain
645 if(pbestprev == world)
648 pbestprev.chain = pbest.chain;
663 float TeamScore_GetCompareValue(float t)
668 if(t <= 0 || t >= 16)
669 error("Reading score of invalid team!");
671 sk = teamscorekeepers[t - 1];
674 s = sk.teamscores_primary;
675 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
678 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
684 #define SCORESWIDTH 58
685 // TODO put this somewhere in common?
686 string Score_NicePrint_ItemColor(float vflags)
688 if(vflags & SFL_SORT_PRIO_PRIMARY)
690 else if(vflags & SFL_SORT_PRIO_SECONDARY)
696 void Score_NicePrint_Team(entity to, float t, float w)
704 sk = teamscorekeepers[t - 1];
707 s = strcat(s, ColoredTeamName(t));
708 for(i = 0; i < MAX_TEAMSCORE; ++i)
709 if(teamscores_label[i] != "")
711 fl = teamscores_flags[i];
712 sc = sk.(teamscores[i]);
713 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
719 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
721 for(i = 0; i < MAX_SCORE; ++i)
722 if(scores_label[i] != "")
724 fl = scores_flags[i];
725 s2 = scores_label[i];
726 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
732 void Score_NicePrint_Player(entity to, entity p, float w)
742 s = strcat(s, p.netname);
745 i = strlennocol(s) - NAMEWIDTH;
747 s = substring(s, 0, strlen(s) - i);
750 s = strcat(s, strpad(i, ""));
755 for(i = 0; i < MAX_SCORE; ++i)
756 if(scores_label[i] != "")
758 fl = scores_flags[i];
760 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
766 void Score_NicePrint_Spectators(entity to)
768 print_to(to, "Spectators:");
771 void Score_NicePrint_Spectator(entity to, entity p)
773 print_to(to, strcat(" ", p.netname));
776 .float score_dummyfield;
777 void Score_NicePrint(entity to)
784 for(i = 0; i < MAX_SCORE; ++i)
785 if(scores_label[i] != "")
787 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
789 p = PlayerScore_Sort(score_dummyfield);
792 if(!teamscores_entities_count)
793 Score_NicePrint_Team(to, t, w);
796 if(teamscores_entities_count)
798 Score_NicePrint_Team(to, p.team, w);
799 Score_NicePrint_Player(to, p, w);
806 if(p.classname != "player")
809 Score_NicePrint_Spectators(to);
810 Score_NicePrint_Spectator(to, p);