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.SendEntity = TeamScore_SendEntity;
80 ts.netname = name; // not used yet, FIXME
81 //ts.SendFlags = SENDFLAGS_CREATE; // immediately send, so csqc knows about the team
84 teamscorekeepers[t - 1] = ts;
85 ++teamscores_entities_count;
88 float TeamScore_AddToTeam(float t, float scorefield, float score)
95 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
97 error("Adding score to invalid team!");
98 s = teamscorekeepers[t - 1];
100 error("Adding score to unknown team!");
102 if(teamscores_label[scorefield] != "")
103 s.SendFlags |= pow(2, scorefield);
104 return (s.(teamscores[scorefield]) += score);
107 float TeamScore_Add(entity player, float scorefield, float score)
109 return TeamScore_AddToTeam(player.team, scorefield, score);
112 float TeamScore_Compare(entity t1, entity t2)
114 if(!t1 || !t2) return (!t2) - !t1;
118 for(i = 0; i < MAX_TEAMSCORE; ++i)
122 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
128 * the scoreinfo entity
131 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
133 scores_label[i] = label;
134 scores_flags[i] = scoreflags;
135 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
137 scores_primary = scores[i];
138 scores_flags_primary = scoreflags;
142 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
144 teamscores_label[i] = label;
145 teamscores_flags[i] = scoreflags;
146 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
148 teamscores_primary = teamscores[i];
149 teamscores_flags_primary = scoreflags;
153 float ScoreInfo_SendEntity(entity to, float sf)
156 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
157 WriteByte(MSG_ENTITY, game);
158 for(i = 0; i < MAX_SCORE; ++i)
160 WriteString(MSG_ENTITY, scores_label[i]);
161 WriteByte(MSG_ENTITY, scores_flags[i]);
163 for(i = 0; i < MAX_TEAMSCORE; ++i)
165 WriteString(MSG_ENTITY, teamscores_label[i]);
166 WriteByte(MSG_ENTITY, teamscores_flags[i]);
171 void ScoreInfo_Init(float teams)
173 if(scores_initialized)
175 scores_initialized.SendFlags |= 1; // force a resend
179 scores_initialized = spawn();
180 scores_initialized.classname = "ent_client_scoreinfo";
181 scores_initialized.SendEntity = ScoreInfo_SendEntity;
182 setmodel(scores_initialized, "null");
183 scores_initialized.effects = EF_NODEPTHTEST;
186 TeamScore_Spawn(COLOR_TEAM1, "Red");
188 TeamScore_Spawn(COLOR_TEAM2, "Blue");
190 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
192 TeamScore_Spawn(COLOR_TEAM4, "Pink");
196 * per-player score entities
199 float PlayerScore_SendEntity(entity to, float sendflags)
203 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
204 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
206 for(i = 0; i < MAX_SCORE; ++i)
207 WriteShort(MSG_ENTITY, self.scores[i]);
210 WriteByte(MSG_ENTITY, sendflags);
212 WriteShort(MSG_ENTITY, sendflags);
215 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
217 WriteShort(MSG_ENTITY, self.scores[i]);
223 void PlayerScore_Clear(entity player)
228 if(teamscores_entities_count)
232 if(g_race && !g_race_qualifying) return;
234 sk = player.scorekeeper;
235 for(i = 0; i < MAX_SCORE; ++i)
237 if(sk.(scores[i]) != 0)
238 if(scores_label[i] != "")
239 sk.SendFlags |= pow(2, i);
244 void Score_ClearAll()
248 FOR_EACH_CLIENTSLOT(p)
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);
261 for(t = 0; t < 16; ++t)
263 sk = teamscorekeepers[t];
266 for(i = 0; i < MAX_TEAMSCORE; ++i)
268 if(sk.(teamscores[i]) != 0)
269 if(teamscores_label[i] != "")
270 sk.SendFlags |= pow(2, i);
271 sk.(teamscores[i]) = 0;
276 void PlayerScore_Attach(entity player)
279 if(player.scorekeeper)
280 error("player already has a scorekeeper");
283 sk.SendEntity = PlayerScore_SendEntity;
285 player.scorekeeper = sk;
288 void PlayerScore_Detach(entity player)
290 if(!player.scorekeeper)
291 error("player has no scorekeeper");
292 remove(player.scorekeeper);
293 player.scorekeeper = world;
296 float PlayerScore_Add(entity player, float scorefield, float score)
303 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
304 s = player.scorekeeper;
306 error("Adding score to unknown player!");
308 if(scores_label[scorefield] != "")
309 s.SendFlags |= pow(2, scorefield);
310 return (s.(scores[scorefield]) += score);
313 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
316 r = PlayerScore_Add(player, pscorefield, score);
317 if(teamscores_entities_count) // only for teamplay
318 r = TeamScore_Add(player, tscorefield, score);
322 float PlayerScore_Compare(entity t1, entity t2)
324 if(!t1 || !t2) return (!t2) - !t1;
328 for(i = 0; i < MAX_SCORE; ++i)
332 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
337 void WinningConditionHelper()
345 s = strcat(s, ":", cvar_string("g_nexuizversion"));
346 s = strcat(s, "::", GetPlayerScoreString(world, 2)); // make this 1 once we can
348 fullstatus = cvar("g_full_getstatus_responses");
350 if(teamscores_entities_count)
354 s = strcat(s, ":", GetTeamScoreString(0, 1));
355 for(t = 0; t < 16; ++t)
356 if(teamscorekeepers[t])
357 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
359 WinningConditionHelper_equality = 0;
360 WinningConditionHelper_winnerteam = 0;
361 for(t = 1; t < 16; ++t)
364 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
365 sk2 = teamscorekeepers[t];
366 c = TeamScore_Compare(sk1, sk2);
368 WinningConditionHelper_equality = 1;
371 WinningConditionHelper_equality = 0;
372 WinningConditionHelper_winnerteam = t;
376 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
377 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
378 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
380 if(teamscores_flags_primary & SFL_TIME)
381 WinningConditionHelper_topscore /= 10;
383 if(WinningConditionHelper_equality)
384 WinningConditionHelper_winnerteam = -1;
386 WinningConditionHelper_winner = world;
387 if(WinningConditionHelper_equality)
388 WinningConditionHelper_winnerteam = -1;
390 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
392 if(WinningConditionHelper_topscore == 0)
394 if(scores_flags_primary & SFL_ZERO_IS_WORST)
396 if(WinningConditionHelper_lowerisbetter)
397 WinningConditionHelper_topscore = 999999999;
399 WinningConditionHelper_topscore = -999999999;
401 WinningConditionHelper_equality = 0;
406 WinningConditionHelper_equality = 0;
407 WinningConditionHelper_winner = world;
410 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
412 WinningConditionHelper_equality = 1;
415 WinningConditionHelper_equality = 0;
416 WinningConditionHelper_winner = p;
420 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
421 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
422 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
424 if(teamscores_flags_primary & SFL_TIME)
425 WinningConditionHelper_topscore /= 10;
427 WinningConditionHelper_winnerteam = -1;
428 if(WinningConditionHelper_equality)
429 WinningConditionHelper_winner = world;
431 if(WinningConditionHelper_topscore == 0)
433 if(scores_flags_primary & SFL_ZERO_IS_WORST)
435 if(WinningConditionHelper_lowerisbetter)
436 WinningConditionHelper_topscore = 999999999;
438 WinningConditionHelper_topscore = -999999999;
440 WinningConditionHelper_equality = 0;
445 strunzone(worldstatus);
446 worldstatus = strzone(s);
452 s = GetPlayerScoreString(p, 1);
453 if(clienttype(p) == CLIENTTYPE_REAL)
454 s = strcat(s, ":human");
456 s = strcat(s, ":bot");
457 if(p.classname == "player" || g_arena || g_lms)
458 s = strcat(s, ":", ftos(p.team));
460 s = strcat(s, ":spectator");
464 if(p.classname == "player" || g_arena || g_lms)
465 s = GetPlayerScoreString(p, 2);
471 strunzone(p.clientstatus);
472 p.clientstatus = strzone(s);
476 string GetScoreLogLabel(string label, float fl)
478 if(fl & SFL_LOWER_IS_BETTER)
479 label = strcat(label, "<");
480 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
481 label = strcat(label, "!!");
482 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
483 label = strcat(label, "!");
487 string GetPlayerScoreString(entity pl, float shortString)
498 for(i = 0; i < MAX_SCORE; ++i)
499 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
503 out = strcat(out, GetScoreLogLabel(l, f), ",");
506 for(i = 0; i < MAX_SCORE; ++i)
507 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
511 out = strcat(out, GetScoreLogLabel(l, f), ",");
514 for(i = 0; i < MAX_SCORE; ++i)
515 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
516 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
520 out = strcat(out, GetScoreLogLabel(l, f), ",");
522 out = substring(out, 0, strlen(out) - 1);
524 else if((sk = pl.scorekeeper))
526 for(i = 0; i < MAX_SCORE; ++i)
527 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
528 out = strcat(out, ftos(sk.(scores[i])), ",");
530 for(i = 0; i < MAX_SCORE; ++i)
531 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
532 out = strcat(out, ftos(sk.(scores[i])), ",");
534 for(i = 0; i < MAX_SCORE; ++i)
535 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
536 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
537 out = strcat(out, ftos(sk.(scores[i])), ",");
538 out = substring(out, 0, strlen(out) - 1);
543 string GetTeamScoreString(float tm, float shortString)
554 for(i = 0; i < MAX_SCORE; ++i)
555 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
557 f = teamscores_flags[i];
558 l = teamscores_label[i];
559 out = strcat(out, GetScoreLogLabel(l, f), ",");
562 for(i = 0; i < MAX_SCORE; ++i)
563 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
565 f = teamscores_flags[i];
566 l = teamscores_label[i];
567 out = strcat(out, GetScoreLogLabel(l, f), ",");
570 for(i = 0; i < MAX_SCORE; ++i)
571 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
572 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
574 f = teamscores_flags[i];
575 l = teamscores_label[i];
576 out = strcat(out, GetScoreLogLabel(l, f), ",");
578 out = substring(out, 0, strlen(out) - 1);
580 else if((sk = teamscorekeepers[tm - 1]))
582 for(i = 0; i < MAX_TEAMSCORE; ++i)
583 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
584 out = strcat(out, ftos(sk.(teamscores[i])), ",");
586 for(i = 0; i < MAX_TEAMSCORE; ++i)
587 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
588 out = strcat(out, ftos(sk.(teamscores[i])), ",");
590 for(i = 0; i < MAX_TEAMSCORE; ++i)
591 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
592 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
593 out = strcat(out, ftos(sk.(teamscores[i])), ",");
594 out = substring(out, 0, strlen(out) - 1);
599 float PlayerTeamScore_Compare(entity p1, entity p2)
601 if(teamscores_entities_count)
602 if(p1.team != p2.team)
606 t1 = teamscorekeepers[p1.team - 1];
607 t2 = teamscorekeepers[p2.team - 1];
608 r = TeamScore_Compare(t1, t2);
609 if(r == 0) // ensure a deterministic order
610 r = p1.team - p2.team;
614 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
617 entity PlayerScore_Sort(.float field)
619 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
627 FOR_EACH_PLAYER(p) if(p.scorekeeper)
632 // Now plist points to the whole list.
634 pfirst = plast = world;
639 pprev = pbestprev = world;
641 for(p = plist; (pprev = p), (p = p.chain); )
643 if(PlayerTeamScore_Compare(p, pbest) > 0)
650 // remove pbest out of the chain
651 if(pbestprev == world)
654 pbestprev.chain = pbest.chain;
669 float TeamScore_GetCompareValue(float t)
674 if(t <= 0 || t >= 16)
675 error("Reading score of invalid team!");
677 sk = teamscorekeepers[t - 1];
680 s = sk.teamscores_primary;
681 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
684 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
690 #define SCORESWIDTH 58
691 // TODO put this somewhere in common?
692 string Score_NicePrint_ItemColor(float vflags)
694 if(vflags & SFL_SORT_PRIO_PRIMARY)
696 else if(vflags & SFL_SORT_PRIO_SECONDARY)
702 void Score_NicePrint_Team(entity to, float t, float w)
710 sk = teamscorekeepers[t - 1];
713 s = strcat(s, ColoredTeamName(t));
714 for(i = 0; i < MAX_TEAMSCORE; ++i)
715 if(teamscores_label[i] != "")
717 fl = teamscores_flags[i];
718 sc = sk.(teamscores[i]);
719 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
725 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
727 for(i = 0; i < MAX_SCORE; ++i)
728 if(scores_label[i] != "")
730 fl = scores_flags[i];
731 s2 = scores_label[i];
732 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
738 void Score_NicePrint_Player(entity to, entity p, float w)
748 s = strcat(s, p.netname);
751 i = strlennocol(s) - NAMEWIDTH;
753 s = substring(s, 0, strlen(s) - i);
756 s = strcat(s, strpad(i, ""));
761 for(i = 0; i < MAX_SCORE; ++i)
762 if(scores_label[i] != "")
764 fl = scores_flags[i];
766 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
772 void Score_NicePrint_Spectators(entity to)
774 print_to(to, "Spectators:");
777 void Score_NicePrint_Spectator(entity to, entity p)
779 print_to(to, strcat(" ", p.netname));
782 .float score_dummyfield;
783 void Score_NicePrint(entity to)
790 for(i = 0; i < MAX_SCORE; ++i)
791 if(scores_label[i] != "")
793 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
795 p = PlayerScore_Sort(score_dummyfield);
798 if(!teamscores_entities_count)
799 Score_NicePrint_Team(to, t, w);
802 if(teamscores_entities_count)
804 Score_NicePrint_Team(to, p.team, w);
805 Score_NicePrint_Player(to, p, w);
812 if(p.classname != "player")
815 Score_NicePrint_Spectators(to);
816 Score_NicePrint_Spectator(to, p);