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 void ScoreInfo_Write(float targ)
156 if not(scores_initialized)
158 WriteByte(targ, SVC_TEMPENTITY);
159 WriteByte(targ, TE_CSQC_SCORESINFO);
160 WriteByte(targ, game);
161 for(i = 0; i < MAX_SCORE; ++i)
163 WriteString(targ, scores_label[i]);
164 WriteByte(targ, scores_flags[i]);
166 for(i = 0; i < MAX_TEAMSCORE; ++i)
168 WriteString(targ, teamscores_label[i]);
169 WriteByte(targ, teamscores_flags[i]);
173 void ScoreInfo_Init(float teams)
175 scores_initialized = 1;
177 TeamScore_Spawn(COLOR_TEAM1, "Red");
179 TeamScore_Spawn(COLOR_TEAM2, "Blue");
181 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
183 TeamScore_Spawn(COLOR_TEAM4, "Pink");
184 ScoreInfo_Write(MSG_ALL);
188 * per-player score entities
191 float PlayerScore_SendEntity(entity to, float sendflags)
195 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
196 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
198 for(i = 0; i < MAX_SCORE; ++i)
199 WriteShort(MSG_ENTITY, self.scores[i]);
202 WriteByte(MSG_ENTITY, sendflags);
204 WriteShort(MSG_ENTITY, sendflags);
207 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
209 WriteShort(MSG_ENTITY, self.scores[i]);
215 void PlayerScore_Clear(entity player)
220 if(teamscores_entities_count)
224 if(g_race && !g_race_qualifying) return;
226 sk = player.scorekeeper;
227 for(i = 0; i < MAX_SCORE; ++i)
229 if(sk.(scores[i]) != 0)
230 if(scores_label[i] != "")
231 sk.SendFlags |= pow(2, i);
236 void Score_ClearAll()
240 FOR_EACH_CLIENTSLOT(p)
245 for(i = 0; i < MAX_SCORE; ++i)
247 if(sk.(scores[i]) != 0)
248 if(scores_label[i] != "")
249 sk.SendFlags |= pow(2, i);
253 for(t = 0; t < 16; ++t)
255 sk = teamscorekeepers[t];
258 for(i = 0; i < MAX_TEAMSCORE; ++i)
260 if(sk.(teamscores[i]) != 0)
261 if(teamscores_label[i] != "")
262 sk.SendFlags |= pow(2, i);
263 sk.(teamscores[i]) = 0;
268 void PlayerScore_Attach(entity player)
271 if(player.scorekeeper)
272 error("player already has a scorekeeper");
275 sk.SendEntity = PlayerScore_SendEntity;
277 player.scorekeeper = sk;
280 void PlayerScore_Detach(entity player)
282 if(!player.scorekeeper)
283 error("player has no scorekeeper");
284 remove(player.scorekeeper);
285 player.scorekeeper = world;
288 float PlayerScore_Add(entity player, float scorefield, float score)
295 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
296 s = player.scorekeeper;
298 error("Adding score to unknown player!");
300 if(scores_label[scorefield] != "")
301 s.SendFlags |= pow(2, scorefield);
302 return (s.(scores[scorefield]) += score);
305 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
308 r = PlayerScore_Add(player, pscorefield, score);
309 if(teamscores_entities_count) // only for teamplay
310 r = TeamScore_Add(player, tscorefield, score);
314 float PlayerScore_Compare(entity t1, entity t2)
316 if(!t1 || !t2) return (!t2) - !t1;
320 for(i = 0; i < MAX_SCORE; ++i)
324 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
329 void WinningConditionHelper()
337 s = strcat(s, ":", cvar_string("g_nexuizversion"));
338 s = strcat(s, "::", GetPlayerScoreString(world, 2)); // make this 1 once we can
340 fullstatus = cvar("g_full_getstatus_responses");
342 if(teamscores_entities_count)
346 s = strcat(s, ":", GetTeamScoreString(0, 1));
347 for(t = 0; t < 16; ++t)
348 if(teamscorekeepers[t])
349 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
351 WinningConditionHelper_equality = 0;
352 WinningConditionHelper_winnerteam = 0;
353 for(t = 1; t < 16; ++t)
356 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
357 sk2 = teamscorekeepers[t];
358 c = TeamScore_Compare(sk1, sk2);
360 WinningConditionHelper_equality = 1;
363 WinningConditionHelper_equality = 0;
364 WinningConditionHelper_winnerteam = t;
368 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
369 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
370 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
372 if(teamscores_flags_primary & SFL_TIME)
373 WinningConditionHelper_topscore /= 10;
375 if(WinningConditionHelper_equality)
376 WinningConditionHelper_winnerteam = -1;
378 WinningConditionHelper_winner = world;
379 if(WinningConditionHelper_equality)
380 WinningConditionHelper_winnerteam = -1;
382 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
384 if(WinningConditionHelper_topscore == 0)
386 if(scores_flags_primary & SFL_ZERO_IS_WORST)
388 if(WinningConditionHelper_lowerisbetter)
389 WinningConditionHelper_topscore = 999999999;
391 WinningConditionHelper_topscore = -999999999;
393 WinningConditionHelper_equality = 0;
398 WinningConditionHelper_equality = 0;
399 WinningConditionHelper_winner = world;
402 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
404 WinningConditionHelper_equality = 1;
407 WinningConditionHelper_equality = 0;
408 WinningConditionHelper_winner = p;
412 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
413 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
414 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
416 if(teamscores_flags_primary & SFL_TIME)
417 WinningConditionHelper_topscore /= 10;
419 WinningConditionHelper_winnerteam = -1;
420 if(WinningConditionHelper_equality)
421 WinningConditionHelper_winner = world;
423 if(WinningConditionHelper_topscore == 0)
425 if(scores_flags_primary & SFL_ZERO_IS_WORST)
427 if(WinningConditionHelper_lowerisbetter)
428 WinningConditionHelper_topscore = 999999999;
430 WinningConditionHelper_topscore = -999999999;
432 WinningConditionHelper_equality = 0;
437 strunzone(worldstatus);
438 worldstatus = strzone(s);
444 s = GetPlayerScoreString(p, 1);
445 if(clienttype(p) == CLIENTTYPE_REAL)
446 s = strcat(s, ":human");
448 s = strcat(s, ":bot");
449 if(p.classname == "player" || g_arena || g_lms)
450 s = strcat(s, ":", ftos(p.team));
452 s = strcat(s, ":spectator");
456 if(p.classname == "player" || g_arena || g_lms)
457 s = GetPlayerScoreString(p, 2);
463 strunzone(p.clientstatus);
464 p.clientstatus = strzone(s);
468 string GetScoreLogLabel(string label, float fl)
470 if(fl & SFL_LOWER_IS_BETTER)
471 label = strcat(label, "<");
472 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
473 label = strcat(label, "!!");
474 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
475 label = strcat(label, "!");
479 string GetPlayerScoreString(entity pl, float shortString)
490 for(i = 0; i < MAX_SCORE; ++i)
491 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
495 out = strcat(out, GetScoreLogLabel(l, f), ",");
498 for(i = 0; i < MAX_SCORE; ++i)
499 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
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_PRIMARY)
508 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
512 out = strcat(out, GetScoreLogLabel(l, f), ",");
514 out = substring(out, 0, strlen(out) - 1);
516 else if((sk = pl.scorekeeper))
518 for(i = 0; i < MAX_SCORE; ++i)
519 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
520 out = strcat(out, ftos(sk.(scores[i])), ",");
522 for(i = 0; i < MAX_SCORE; ++i)
523 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
524 out = strcat(out, ftos(sk.(scores[i])), ",");
526 for(i = 0; i < MAX_SCORE; ++i)
527 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
528 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
529 out = strcat(out, ftos(sk.(scores[i])), ",");
530 out = substring(out, 0, strlen(out) - 1);
535 string GetTeamScoreString(float tm, float shortString)
546 for(i = 0; i < MAX_SCORE; ++i)
547 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
549 f = teamscores_flags[i];
550 l = teamscores_label[i];
551 out = strcat(out, GetScoreLogLabel(l, f), ",");
554 for(i = 0; i < MAX_SCORE; ++i)
555 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
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_PRIMARY)
564 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
566 f = teamscores_flags[i];
567 l = teamscores_label[i];
568 out = strcat(out, GetScoreLogLabel(l, f), ",");
570 out = substring(out, 0, strlen(out) - 1);
572 else if((sk = teamscorekeepers[tm - 1]))
574 for(i = 0; i < MAX_TEAMSCORE; ++i)
575 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
576 out = strcat(out, ftos(sk.(teamscores[i])), ",");
578 for(i = 0; i < MAX_TEAMSCORE; ++i)
579 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
580 out = strcat(out, ftos(sk.(teamscores[i])), ",");
582 for(i = 0; i < MAX_TEAMSCORE; ++i)
583 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
584 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
585 out = strcat(out, ftos(sk.(teamscores[i])), ",");
586 out = substring(out, 0, strlen(out) - 1);
591 float PlayerTeamScore_Compare(entity p1, entity p2)
593 if(teamscores_entities_count)
594 if(p1.team != p2.team)
598 t1 = teamscorekeepers[p1.team - 1];
599 t2 = teamscorekeepers[p2.team - 1];
600 r = TeamScore_Compare(t1, t2);
601 if(r == 0) // ensure a deterministic order
602 r = p1.team - p2.team;
606 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
609 entity PlayerScore_Sort(.float field)
611 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
619 FOR_EACH_PLAYER(p) if(p.scorekeeper)
624 // Now plist points to the whole list.
626 pfirst = plast = world;
631 pprev = pbestprev = world;
633 for(p = plist; (pprev = p), (p = p.chain); )
635 if(PlayerTeamScore_Compare(p, pbest) > 0)
642 // remove pbest out of the chain
643 if(pbestprev == world)
646 pbestprev.chain = pbest.chain;
661 float TeamScore_GetCompareValue(float t)
666 if(t <= 0 || t >= 16)
667 error("Reading score of invalid team!");
669 sk = teamscorekeepers[t - 1];
672 s = sk.teamscores_primary;
673 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
676 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
682 #define SCORESWIDTH 58
683 // TODO put this somewhere in common?
684 string Score_NicePrint_ItemColor(float vflags)
686 if(vflags & SFL_SORT_PRIO_PRIMARY)
688 else if(vflags & SFL_SORT_PRIO_SECONDARY)
694 void Score_NicePrint_Team(entity to, float t, float w)
702 sk = teamscorekeepers[t - 1];
705 s = strcat(s, ColoredTeamName(t));
706 for(i = 0; i < MAX_TEAMSCORE; ++i)
707 if(teamscores_label[i] != "")
709 fl = teamscores_flags[i];
710 sc = sk.(teamscores[i]);
711 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
717 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
719 for(i = 0; i < MAX_SCORE; ++i)
720 if(scores_label[i] != "")
722 fl = scores_flags[i];
723 s2 = scores_label[i];
724 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
730 void Score_NicePrint_Player(entity to, entity p, float w)
740 s = strcat(s, p.netname);
743 i = strlennocol(s) - NAMEWIDTH;
745 s = substring(s, 0, strlen(s) - i);
748 s = strcat(s, strpad(i, ""));
753 for(i = 0; i < MAX_SCORE; ++i)
754 if(scores_label[i] != "")
756 fl = scores_flags[i];
758 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
764 void Score_NicePrint_Spectators(entity to)
766 print_to(to, "Spectators:");
769 void Score_NicePrint_Spectator(entity to, entity p)
771 print_to(to, strcat(" ", p.netname));
774 .float score_dummyfield;
775 void Score_NicePrint(entity to)
782 for(i = 0; i < MAX_SCORE; ++i)
783 if(scores_label[i] != "")
785 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
787 p = PlayerScore_Sort(score_dummyfield);
790 if(!teamscores_entities_count)
791 Score_NicePrint_Team(to, t, w);
794 if(teamscores_entities_count)
796 Score_NicePrint_Team(to, p.team, w);
797 Score_NicePrint_Player(to, p, w);
804 if(p.classname != "player")
807 Score_NicePrint_Spectators(to);
808 Score_NicePrint_Spectator(to, p);