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);
366 if(teamscores_flags_primary & SFL_TIME)
367 WinningConditionHelper_topscore /= 10;
369 WinningConditionHelper_winner = world;
370 if(WinningConditionHelper_equality)
371 WinningConditionHelper_winnerteam = -1;
373 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
377 WinningConditionHelper_equality = 0;
378 WinningConditionHelper_winner = world;
381 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
383 WinningConditionHelper_equality = 1;
386 WinningConditionHelper_equality = 0;
387 WinningConditionHelper_winner = p;
391 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
392 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
393 if(scores_flags_primary & SFL_ZERO_IS_WORST)
394 if(WinningConditionHelper_topscore == 0)
396 if(WinningConditionHelper_lowerisbetter)
397 WinningConditionHelper_topscore = 999999999;
399 WinningConditionHelper_topscore = -999999999;
401 if(teamscores_flags_primary & SFL_TIME)
402 WinningConditionHelper_topscore /= 10;
404 if(WinningConditionHelper_equality)
405 WinningConditionHelper_winner = world;
406 WinningConditionHelper_winnerteam = -1;
410 strunzone(worldstatus);
411 worldstatus = strzone(s);
415 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
416 s = GetPlayerScoreString(p, 1);
417 if(clienttype(p) == CLIENTTYPE_REAL)
418 s = strcat(s, ":human");
420 s = strcat(s, ":bot");
421 if(p.classname == "player" || g_arena || g_lms)
422 s = strcat(s, ":", ftos(p.team));
424 s = strcat(s, ":spectator");
426 if(p.classname == "player" || g_arena || g_lms)
429 s = GetPlayerScoreString(p, 2);
432 strunzone(p.clientstatus);
433 p.clientstatus = strzone(s);
437 string GetScoreLogLabel(string label, float fl)
439 if(fl & SFL_LOWER_IS_BETTER)
440 label = strcat(label, "<");
441 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
442 label = strcat(label, "!!");
443 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
444 label = strcat(label, "!");
448 string GetPlayerScoreString(entity pl, float shortString)
459 for(i = 0; i < MAX_SCORE; ++i)
460 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
464 out = strcat(out, GetScoreLogLabel(l, f), ",");
467 for(i = 0; i < MAX_SCORE; ++i)
468 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
472 out = strcat(out, GetScoreLogLabel(l, f), ",");
475 for(i = 0; i < MAX_SCORE; ++i)
476 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
477 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
481 out = strcat(out, GetScoreLogLabel(l, f), ",");
483 out = substring(out, 0, strlen(out) - 1);
485 else if((sk = pl.scorekeeper))
487 for(i = 0; i < MAX_SCORE; ++i)
488 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
489 out = strcat(out, ftos(sk.(scores[i])), ",");
491 for(i = 0; i < MAX_SCORE; ++i)
492 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
493 out = strcat(out, ftos(sk.(scores[i])), ",");
495 for(i = 0; i < MAX_SCORE; ++i)
496 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
497 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
498 out = strcat(out, ftos(sk.(scores[i])), ",");
499 out = substring(out, 0, strlen(out) - 1);
504 string GetTeamScoreString(float tm, float shortString)
515 for(i = 0; i < MAX_SCORE; ++i)
516 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
518 f = teamscores_flags[i];
519 l = teamscores_label[i];
520 out = strcat(out, GetScoreLogLabel(l, f), ",");
523 for(i = 0; i < MAX_SCORE; ++i)
524 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
526 f = teamscores_flags[i];
527 l = teamscores_label[i];
528 out = strcat(out, GetScoreLogLabel(l, f), ",");
531 for(i = 0; i < MAX_SCORE; ++i)
532 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
533 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
535 f = teamscores_flags[i];
536 l = teamscores_label[i];
537 out = strcat(out, GetScoreLogLabel(l, f), ",");
539 out = substring(out, 0, strlen(out) - 1);
541 else if((sk = teamscorekeepers[tm - 1]))
543 for(i = 0; i < MAX_TEAMSCORE; ++i)
544 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
545 out = strcat(out, ftos(sk.(teamscores[i])), ",");
547 for(i = 0; i < MAX_TEAMSCORE; ++i)
548 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
549 out = strcat(out, ftos(sk.(teamscores[i])), ",");
551 for(i = 0; i < MAX_TEAMSCORE; ++i)
552 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
553 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
554 out = strcat(out, ftos(sk.(teamscores[i])), ",");
555 out = substring(out, 0, strlen(out) - 1);
560 float PlayerTeamScore_Compare(entity p1, entity p2)
562 if(teamscores_entities_count)
563 if(p1.team != p2.team)
567 t1 = teamscorekeepers[p1.team - 1];
568 t2 = teamscorekeepers[p2.team - 1];
569 r = TeamScore_Compare(t1, t2);
570 if(r == 0) // ensure a deterministic order
571 r = p1.team - p2.team;
575 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
578 entity PlayerScore_Sort(.float field)
580 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
587 FOR_EACH_PLAYER(p) if(p.scorekeeper)
592 // Now plist points to the whole list.
594 pfirst = plast = world;
599 pprev = pbestprev = world;
601 for(p = plist; (pprev = p), (p = p.chain); )
603 if(PlayerTeamScore_Compare(p, pbest) > 0)
610 // remove pbest out of the chain
611 if(pbestprev == world)
614 pbestprev.chain = pbest.chain;
628 float TeamScore_GetCompareValue(float t)
633 if(t <= 0 || t >= 16)
634 error("Reading score of invalid team!");
636 sk = teamscorekeepers[t - 1];
639 s = sk.teamscores_primary;
640 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
643 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
649 #define SCORESWIDTH 58
650 // TODO put this somewhere in common?
651 string Score_NicePrint_ItemColor(float vflags)
653 if(vflags & SFL_SORT_PRIO_PRIMARY)
655 else if(vflags & SFL_SORT_PRIO_SECONDARY)
661 void Score_NicePrint_Team(entity to, float t, float w)
669 sk = teamscorekeepers[t - 1];
672 s = strcat(s, ColoredTeamName(t));
673 for(i = 0; i < MAX_TEAMSCORE; ++i)
674 if(teamscores_label[i] != "")
676 fl = teamscores_flags[i];
677 sc = sk.(teamscores[i]);
678 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
684 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
686 for(i = 0; i < MAX_SCORE; ++i)
687 if(scores_label[i] != "")
689 fl = scores_flags[i];
690 s2 = scores_label[i];
691 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
697 void Score_NicePrint_Player(entity to, entity p, float w)
707 s = strcat(s, p.netname);
710 i = strlennocol(s) - NAMEWIDTH;
712 s = substring(s, 0, strlen(s) - i);
715 s = strcat(s, strpad(i, ""));
720 for(i = 0; i < MAX_SCORE; ++i)
721 if(scores_label[i] != "")
723 fl = scores_flags[i];
725 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
731 void Score_NicePrint_Spectators(entity to)
733 print_to(to, "Spectators:");
736 void Score_NicePrint_Spectator(entity to, entity p)
738 print_to(to, strcat(" ", p.netname));
741 .float score_dummyfield;
742 void Score_NicePrint(entity to)
749 for(i = 0; i < MAX_SCORE; ++i)
750 if(scores_label[i] != "")
752 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
754 p = PlayerScore_Sort(score_dummyfield);
757 if(!teamscores_entities_count)
758 Score_NicePrint_Team(to, t, w);
761 if(teamscores_entities_count)
763 Score_NicePrint_Team(to, p.team, w);
764 Score_NicePrint_Player(to, p, w);
771 if(p.classname != "player")
774 Score_NicePrint_Spectators(to);
775 Score_NicePrint_Spectator(to, p);