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 void Net_LinkEntity(entity e)
15 e.model = "net_entity";
17 e.effects = EF_NODEPTHTEST | EF_LOWPRECISION;
20 vector ScoreField_Compare(entity t1, entity t2, .float field, float fieldflags, vector previous) // returns: cmp value, best prio
22 if(!(fieldflags & SFL_SORT_PRIO_MASK)) // column does not sort
24 if(fieldflags & SFL_SORT_PRIO_MASK < previous_y)
26 if(t1.field == t2.field)
29 previous_y = fieldflags & SFL_SORT_PRIO_MASK;
31 if(fieldflags & SFL_ZERO_IS_WORST)
38 else if(t2.field == 0)
45 if(fieldflags & SFL_LOWER_IS_BETTER)
46 previous_x = (t2.field - t1.field);
48 previous_x = (t1.field - t2.field);
57 float TeamScore_SendEntity(entity to, float sendflags)
61 WriteByte(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
62 WriteByte(MSG_ENTITY, self.team - 1);
63 #if MAX_TEAMSCORE <= 3
64 for(i = 0; i < MAX_TEAMSCORE; ++i)
65 WriteShort(MSG_ENTITY, self.teamscores[i]);
67 #if MAX_TEAMSCORE <= 8
68 WriteByte(MSG_ENTITY, sendflags);
70 WriteShort(MSG_ENTITY, sendflags);
73 for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
75 WriteShort(MSG_ENTITY, self.teamscores[i]);
81 void TeamScore_Spawn(float t, string name)
85 ts.classname = "csqc_score_team";
86 ts.SendEntity = TeamScore_SendEntity;
87 ts.netname = name; // not used yet, FIXME
88 //ts.SendFlags = SENDFLAGS_CREATE; // immediately send, so csqc knows about the team
91 teamscorekeepers[t - 1] = ts;
92 ++teamscores_entities_count;
95 float TeamScore_AddToTeam(float t, float scorefield, float score)
98 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
100 error("Adding score to invalid team!");
101 s = teamscorekeepers[t - 1];
103 error("Adding score to unknown team!");
105 if(teamscores_label[scorefield] != "")
106 s.SendFlags |= pow(2, scorefield);
107 return (s.(teamscores[scorefield]) += score);
110 float TeamScore_Add(entity player, float scorefield, float score)
112 return TeamScore_AddToTeam(player.team, scorefield, score);
115 float TeamScore_Compare(entity t1, entity t2)
117 if(!t1 || !t2) return (!t2) - !t1;
121 for(i = 0; i < MAX_TEAMSCORE; ++i)
125 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
131 * the scoreinfo entity
134 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
136 scores_label[i] = label;
137 scores_flags[i] = scoreflags;
138 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
140 scores_primary = scores[i];
141 scores_flags_primary = scoreflags;
145 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
147 teamscores_label[i] = label;
148 teamscores_flags[i] = scoreflags;
149 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
151 teamscores_primary = teamscores[i];
152 teamscores_flags_primary = scoreflags;
156 void ScoreInfo_Write(float targ)
159 if not(scores_initialized)
161 WriteByte(targ, SVC_TEMPENTITY);
162 WriteByte(targ, TE_CSQC_SCORESINFO);
163 WriteByte(targ, game);
164 for(i = 0; i < MAX_SCORE; ++i)
166 WriteString(targ, scores_label[i]);
167 WriteByte(targ, scores_flags[i]);
169 for(i = 0; i < MAX_TEAMSCORE; ++i)
171 WriteString(targ, teamscores_label[i]);
172 WriteByte(targ, teamscores_flags[i]);
176 void ScoreInfo_Init(float teams)
178 scores_initialized = 1;
180 TeamScore_Spawn(COLOR_TEAM1, "Red");
182 TeamScore_Spawn(COLOR_TEAM2, "Blue");
184 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
186 TeamScore_Spawn(COLOR_TEAM4, "Pink");
187 ScoreInfo_Write(MSG_ALL);
191 * per-player score entities
194 float PlayerScore_SendEntity(entity to, float sendflags)
198 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
199 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
201 for(i = 0; i < MAX_SCORE; ++i)
202 WriteShort(MSG_ENTITY, self.scores[i]);
205 WriteByte(MSG_ENTITY, sendflags);
207 WriteShort(MSG_ENTITY, sendflags);
210 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
212 WriteShort(MSG_ENTITY, self.scores[i]);
218 void PlayerScore_Clear(entity player)
223 if(teamscores_entities_count)
227 if(g_race && !g_race_qualifying) return;
229 sk = player.scorekeeper;
230 for(i = 0; i < MAX_SCORE; ++i)
232 if(sk.(scores[i]) != 0)
233 if(scores_label[i] != "")
234 sk.SendFlags |= pow(2, i);
239 void Score_ClearAll()
243 FOR_EACH_CLIENTSLOT(p)
248 for(i = 0; i < MAX_SCORE; ++i)
250 if(sk.(scores[i]) != 0)
251 if(scores_label[i] != "")
252 sk.SendFlags |= pow(2, i);
256 for(t = 0; t < 16; ++t)
258 sk = teamscorekeepers[t];
261 for(i = 0; i < MAX_TEAMSCORE; ++i)
263 if(sk.(teamscores[i]) != 0)
264 if(teamscores_label[i] != "")
265 sk.SendFlags |= pow(2, i);
266 sk.(teamscores[i]) = 0;
271 void PlayerScore_Attach(entity player)
274 if(player.scorekeeper)
275 error("player already has a scorekeeper");
278 sk.SendEntity = 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)
294 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
295 s = player.scorekeeper;
297 error("Adding score to unknown player!");
299 if(scores_label[scorefield] != "")
300 s.SendFlags |= pow(2, scorefield);
301 return (s.(scores[scorefield]) += score);
304 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
307 r = PlayerScore_Add(player, pscorefield, score);
308 if(teamscores_entities_count) // only for teamplay
309 r = TeamScore_Add(player, tscorefield, score);
313 float PlayerScore_Compare(entity t1, entity t2)
315 if(!t1 || !t2) return (!t2) - !t1;
319 for(i = 0; i < MAX_SCORE; ++i)
323 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
328 void WinningConditionHelper()
334 s = strcat(s, ":", cvar_string("g_nexuizversion"));
335 s = strcat(s, "::", GetPlayerScoreString(world, 2)); // make this 1 once we can
337 if(teamscores_entities_count)
341 s = strcat(s, ":", GetTeamScoreString(0, 1));
342 for(t = 0; t < 16; ++t)
343 if(teamscorekeepers[t])
344 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
346 WinningConditionHelper_equality = 0;
347 WinningConditionHelper_winnerteam = 0;
348 for(t = 1; t < 16; ++t)
351 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
352 sk2 = teamscorekeepers[t];
353 c = TeamScore_Compare(sk1, sk2);
355 WinningConditionHelper_equality = 1;
358 WinningConditionHelper_equality = 0;
359 WinningConditionHelper_winnerteam = t;
363 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
364 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
365 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
367 if(teamscores_flags_primary & SFL_TIME)
368 WinningConditionHelper_topscore /= 10;
370 if(WinningConditionHelper_equality)
371 WinningConditionHelper_winnerteam = -1;
373 WinningConditionHelper_winner = world;
374 if(WinningConditionHelper_equality)
375 WinningConditionHelper_winnerteam = -1;
377 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
379 if(WinningConditionHelper_topscore == 0)
381 if(scores_flags_primary & SFL_ZERO_IS_WORST)
383 if(WinningConditionHelper_lowerisbetter)
384 WinningConditionHelper_topscore = 999999999;
386 WinningConditionHelper_topscore = -999999999;
388 WinningConditionHelper_equality = 0;
393 WinningConditionHelper_equality = 0;
394 WinningConditionHelper_winner = world;
397 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
399 WinningConditionHelper_equality = 1;
402 WinningConditionHelper_equality = 0;
403 WinningConditionHelper_winner = p;
407 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
408 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
409 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
411 if(teamscores_flags_primary & SFL_TIME)
412 WinningConditionHelper_topscore /= 10;
414 WinningConditionHelper_winnerteam = -1;
415 if(WinningConditionHelper_equality)
416 WinningConditionHelper_winner = world;
418 if(WinningConditionHelper_topscore == 0)
420 if(scores_flags_primary & SFL_ZERO_IS_WORST)
422 if(WinningConditionHelper_lowerisbetter)
423 WinningConditionHelper_topscore = 999999999;
425 WinningConditionHelper_topscore = -999999999;
427 WinningConditionHelper_equality = 0;
432 strunzone(worldstatus);
433 worldstatus = strzone(s);
437 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
438 s = GetPlayerScoreString(p, 1);
439 if(clienttype(p) == CLIENTTYPE_REAL)
440 s = strcat(s, ":human");
442 s = strcat(s, ":bot");
443 if(p.classname == "player" || g_arena || g_lms)
444 s = strcat(s, ":", ftos(p.team));
446 s = strcat(s, ":spectator");
448 if(p.classname == "player" || g_arena || g_lms)
451 s = GetPlayerScoreString(p, 2);
454 strunzone(p.clientstatus);
455 p.clientstatus = strzone(s);
459 string GetScoreLogLabel(string label, float fl)
461 if(fl & SFL_LOWER_IS_BETTER)
462 label = strcat(label, "<");
463 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
464 label = strcat(label, "!!");
465 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
466 label = strcat(label, "!");
470 string GetPlayerScoreString(entity pl, float shortString)
481 for(i = 0; i < MAX_SCORE; ++i)
482 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
486 out = strcat(out, GetScoreLogLabel(l, f), ",");
489 for(i = 0; i < MAX_SCORE; ++i)
490 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
494 out = strcat(out, GetScoreLogLabel(l, f), ",");
497 for(i = 0; i < MAX_SCORE; ++i)
498 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
499 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
503 out = strcat(out, GetScoreLogLabel(l, f), ",");
505 out = substring(out, 0, strlen(out) - 1);
507 else if((sk = pl.scorekeeper))
509 for(i = 0; i < MAX_SCORE; ++i)
510 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
511 out = strcat(out, ftos(sk.(scores[i])), ",");
513 for(i = 0; i < MAX_SCORE; ++i)
514 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
515 out = strcat(out, ftos(sk.(scores[i])), ",");
517 for(i = 0; i < MAX_SCORE; ++i)
518 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
519 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
520 out = strcat(out, ftos(sk.(scores[i])), ",");
521 out = substring(out, 0, strlen(out) - 1);
526 string GetTeamScoreString(float tm, float shortString)
537 for(i = 0; i < MAX_SCORE; ++i)
538 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
540 f = teamscores_flags[i];
541 l = teamscores_label[i];
542 out = strcat(out, GetScoreLogLabel(l, f), ",");
545 for(i = 0; i < MAX_SCORE; ++i)
546 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
548 f = teamscores_flags[i];
549 l = teamscores_label[i];
550 out = strcat(out, GetScoreLogLabel(l, f), ",");
553 for(i = 0; i < MAX_SCORE; ++i)
554 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
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), ",");
561 out = substring(out, 0, strlen(out) - 1);
563 else if((sk = teamscorekeepers[tm - 1]))
565 for(i = 0; i < MAX_TEAMSCORE; ++i)
566 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
567 out = strcat(out, ftos(sk.(teamscores[i])), ",");
569 for(i = 0; i < MAX_TEAMSCORE; ++i)
570 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
571 out = strcat(out, ftos(sk.(teamscores[i])), ",");
573 for(i = 0; i < MAX_TEAMSCORE; ++i)
574 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
575 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
576 out = strcat(out, ftos(sk.(teamscores[i])), ",");
577 out = substring(out, 0, strlen(out) - 1);
582 float PlayerTeamScore_Compare(entity p1, entity p2)
584 if(teamscores_entities_count)
585 if(p1.team != p2.team)
589 t1 = teamscorekeepers[p1.team - 1];
590 t2 = teamscorekeepers[p2.team - 1];
591 r = TeamScore_Compare(t1, t2);
592 if(r == 0) // ensure a deterministic order
593 r = p1.team - p2.team;
597 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
600 entity PlayerScore_Sort(.float field)
602 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
609 FOR_EACH_PLAYER(p) if(p.scorekeeper)
614 // Now plist points to the whole list.
616 pfirst = plast = world;
621 pprev = pbestprev = world;
623 for(p = plist; (pprev = p), (p = p.chain); )
625 if(PlayerTeamScore_Compare(p, pbest) > 0)
632 // remove pbest out of the chain
633 if(pbestprev == world)
636 pbestprev.chain = pbest.chain;
650 float TeamScore_GetCompareValue(float t)
655 if(t <= 0 || t >= 16)
656 error("Reading score of invalid team!");
658 sk = teamscorekeepers[t - 1];
661 s = sk.teamscores_primary;
662 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
665 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
671 #define SCORESWIDTH 58
672 // TODO put this somewhere in common?
673 string Score_NicePrint_ItemColor(float vflags)
675 if(vflags & SFL_SORT_PRIO_PRIMARY)
677 else if(vflags & SFL_SORT_PRIO_SECONDARY)
683 void Score_NicePrint_Team(entity to, float t, float w)
691 sk = teamscorekeepers[t - 1];
694 s = strcat(s, ColoredTeamName(t));
695 for(i = 0; i < MAX_TEAMSCORE; ++i)
696 if(teamscores_label[i] != "")
698 fl = teamscores_flags[i];
699 sc = sk.(teamscores[i]);
700 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
706 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
708 for(i = 0; i < MAX_SCORE; ++i)
709 if(scores_label[i] != "")
711 fl = scores_flags[i];
712 s2 = scores_label[i];
713 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
719 void Score_NicePrint_Player(entity to, entity p, float w)
729 s = strcat(s, p.netname);
732 i = strlennocol(s) - NAMEWIDTH;
734 s = substring(s, 0, strlen(s) - i);
737 s = strcat(s, strpad(i, ""));
742 for(i = 0; i < MAX_SCORE; ++i)
743 if(scores_label[i] != "")
745 fl = scores_flags[i];
747 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
753 void Score_NicePrint_Spectators(entity to)
755 print_to(to, "Spectators:");
758 void Score_NicePrint_Spectator(entity to, entity p)
760 print_to(to, strcat(" ", p.netname));
763 .float score_dummyfield;
764 void Score_NicePrint(entity to)
771 for(i = 0; i < MAX_SCORE; ++i)
772 if(scores_label[i] != "")
774 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
776 p = PlayerScore_Sort(score_dummyfield);
779 if(!teamscores_entities_count)
780 Score_NicePrint_Team(to, t, w);
783 if(teamscores_entities_count)
785 Score_NicePrint_Team(to, p.team, w);
786 Score_NicePrint_Player(to, p, w);
793 if(p.classname != "player")
796 Score_NicePrint_Spectators(to);
797 Score_NicePrint_Spectator(to, p);