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)
91 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
93 error("Adding score to invalid team!");
94 s = teamscorekeepers[t - 1];
96 error("Adding score to unknown team!");
98 if(teamscores_label[scorefield] != "")
99 s.SendFlags |= pow(2, scorefield);
100 return (s.(teamscores[scorefield]) += score);
103 float TeamScore_Add(entity player, float scorefield, float score)
105 return TeamScore_AddToTeam(player.team, scorefield, score);
108 float TeamScore_Compare(entity t1, entity t2)
110 if(!t1 || !t2) return (!t2) - !t1;
114 for(i = 0; i < MAX_TEAMSCORE; ++i)
118 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
124 * the scoreinfo entity
127 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
129 scores_label[i] = label;
130 scores_flags[i] = scoreflags;
131 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
133 scores_primary = scores[i];
134 scores_flags_primary = scoreflags;
138 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
140 teamscores_label[i] = label;
141 teamscores_flags[i] = scoreflags;
142 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
144 teamscores_primary = teamscores[i];
145 teamscores_flags_primary = scoreflags;
149 void ScoreInfo_Write(float targ)
152 if not(scores_initialized)
154 WriteByte(targ, SVC_TEMPENTITY);
155 WriteByte(targ, TE_CSQC_SCORESINFO);
156 WriteByte(targ, game);
157 for(i = 0; i < MAX_SCORE; ++i)
159 WriteString(targ, scores_label[i]);
160 WriteByte(targ, scores_flags[i]);
162 for(i = 0; i < MAX_TEAMSCORE; ++i)
164 WriteString(targ, teamscores_label[i]);
165 WriteByte(targ, teamscores_flags[i]);
169 void ScoreInfo_Init(float teams)
171 scores_initialized = 1;
173 TeamScore_Spawn(COLOR_TEAM1, "Red");
175 TeamScore_Spawn(COLOR_TEAM2, "Blue");
177 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
179 TeamScore_Spawn(COLOR_TEAM4, "Pink");
180 ScoreInfo_Write(MSG_ALL);
184 * per-player score entities
187 float PlayerScore_SendEntity(entity to, float sendflags)
191 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
192 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
194 for(i = 0; i < MAX_SCORE; ++i)
195 WriteShort(MSG_ENTITY, self.scores[i]);
198 WriteByte(MSG_ENTITY, sendflags);
200 WriteShort(MSG_ENTITY, sendflags);
203 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
205 WriteShort(MSG_ENTITY, self.scores[i]);
211 void PlayerScore_Clear(entity player)
216 if(teamscores_entities_count)
220 if(g_race && !g_race_qualifying) return;
222 sk = player.scorekeeper;
223 for(i = 0; i < MAX_SCORE; ++i)
225 if(sk.(scores[i]) != 0)
226 if(scores_label[i] != "")
227 sk.SendFlags |= pow(2, i);
232 void Score_ClearAll()
236 FOR_EACH_CLIENTSLOT(p)
241 for(i = 0; i < MAX_SCORE; ++i)
243 if(sk.(scores[i]) != 0)
244 if(scores_label[i] != "")
245 sk.SendFlags |= pow(2, i);
249 for(t = 0; t < 16; ++t)
251 sk = teamscorekeepers[t];
254 for(i = 0; i < MAX_TEAMSCORE; ++i)
256 if(sk.(teamscores[i]) != 0)
257 if(teamscores_label[i] != "")
258 sk.SendFlags |= pow(2, i);
259 sk.(teamscores[i]) = 0;
264 void PlayerScore_Attach(entity player)
267 if(player.scorekeeper)
268 error("player already has a scorekeeper");
271 sk.SendEntity = PlayerScore_SendEntity;
273 player.scorekeeper = sk;
276 void PlayerScore_Detach(entity player)
278 if(!player.scorekeeper)
279 error("player has no scorekeeper");
280 remove(player.scorekeeper);
281 player.scorekeeper = world;
284 float PlayerScore_Add(entity player, float scorefield, float score)
287 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
288 s = player.scorekeeper;
290 error("Adding score to unknown player!");
292 if(scores_label[scorefield] != "")
293 s.SendFlags |= pow(2, scorefield);
294 return (s.(scores[scorefield]) += score);
297 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
300 r = PlayerScore_Add(player, pscorefield, score);
301 if(teamscores_entities_count) // only for teamplay
302 r = TeamScore_Add(player, tscorefield, score);
306 float PlayerScore_Compare(entity t1, entity t2)
308 if(!t1 || !t2) return (!t2) - !t1;
312 for(i = 0; i < MAX_SCORE; ++i)
316 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
321 void WinningConditionHelper()
329 s = strcat(s, ":", cvar_string("g_nexuizversion"));
330 s = strcat(s, "::", GetPlayerScoreString(world, 2)); // make this 1 once we can
332 fullstatus = cvar("g_full_getstatus_responses");
334 if(teamscores_entities_count)
338 s = strcat(s, ":", GetTeamScoreString(0, 1));
339 for(t = 0; t < 16; ++t)
340 if(teamscorekeepers[t])
341 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
343 WinningConditionHelper_equality = 0;
344 WinningConditionHelper_winnerteam = 0;
345 for(t = 1; t < 16; ++t)
348 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
349 sk2 = teamscorekeepers[t];
350 c = TeamScore_Compare(sk1, sk2);
352 WinningConditionHelper_equality = 1;
355 WinningConditionHelper_equality = 0;
356 WinningConditionHelper_winnerteam = t;
360 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
361 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
362 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
364 if(teamscores_flags_primary & SFL_TIME)
365 WinningConditionHelper_topscore /= 10;
367 if(WinningConditionHelper_equality)
368 WinningConditionHelper_winnerteam = -1;
370 WinningConditionHelper_winner = world;
371 if(WinningConditionHelper_equality)
372 WinningConditionHelper_winnerteam = -1;
374 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
376 if(WinningConditionHelper_topscore == 0)
378 if(scores_flags_primary & SFL_ZERO_IS_WORST)
380 if(WinningConditionHelper_lowerisbetter)
381 WinningConditionHelper_topscore = 999999999;
383 WinningConditionHelper_topscore = -999999999;
385 WinningConditionHelper_equality = 0;
390 WinningConditionHelper_equality = 0;
391 WinningConditionHelper_winner = world;
394 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
396 WinningConditionHelper_equality = 1;
399 WinningConditionHelper_equality = 0;
400 WinningConditionHelper_winner = p;
404 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
405 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
406 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
408 if(teamscores_flags_primary & SFL_TIME)
409 WinningConditionHelper_topscore /= 10;
411 WinningConditionHelper_winnerteam = -1;
412 if(WinningConditionHelper_equality)
413 WinningConditionHelper_winner = world;
415 if(WinningConditionHelper_topscore == 0)
417 if(scores_flags_primary & SFL_ZERO_IS_WORST)
419 if(WinningConditionHelper_lowerisbetter)
420 WinningConditionHelper_topscore = 999999999;
422 WinningConditionHelper_topscore = -999999999;
424 WinningConditionHelper_equality = 0;
429 strunzone(worldstatus);
430 worldstatus = strzone(s);
436 s = GetPlayerScoreString(p, 1);
437 if(clienttype(p) == CLIENTTYPE_REAL)
438 s = strcat(s, ":human");
440 s = strcat(s, ":bot");
441 if(p.classname == "player" || g_arena || g_lms)
442 s = strcat(s, ":", ftos(p.team));
444 s = strcat(s, ":spectator");
448 if(p.classname == "player" || g_arena || g_lms)
449 s = GetPlayerScoreString(p, 2);
455 strunzone(p.clientstatus);
456 p.clientstatus = strzone(s);
460 string GetScoreLogLabel(string label, float fl)
462 if(fl & SFL_LOWER_IS_BETTER)
463 label = strcat(label, "<");
464 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
465 label = strcat(label, "!!");
466 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
467 label = strcat(label, "!");
471 string GetPlayerScoreString(entity pl, float shortString)
482 for(i = 0; i < MAX_SCORE; ++i)
483 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
487 out = strcat(out, GetScoreLogLabel(l, f), ",");
490 for(i = 0; i < MAX_SCORE; ++i)
491 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
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_PRIMARY)
500 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
504 out = strcat(out, GetScoreLogLabel(l, f), ",");
506 out = substring(out, 0, strlen(out) - 1);
508 else if((sk = pl.scorekeeper))
510 for(i = 0; i < MAX_SCORE; ++i)
511 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
512 out = strcat(out, ftos(sk.(scores[i])), ",");
514 for(i = 0; i < MAX_SCORE; ++i)
515 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
516 out = strcat(out, ftos(sk.(scores[i])), ",");
518 for(i = 0; i < MAX_SCORE; ++i)
519 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
520 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
521 out = strcat(out, ftos(sk.(scores[i])), ",");
522 out = substring(out, 0, strlen(out) - 1);
527 string GetTeamScoreString(float tm, float shortString)
538 for(i = 0; i < MAX_SCORE; ++i)
539 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
541 f = teamscores_flags[i];
542 l = teamscores_label[i];
543 out = strcat(out, GetScoreLogLabel(l, f), ",");
546 for(i = 0; i < MAX_SCORE; ++i)
547 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
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_PRIMARY)
556 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
558 f = teamscores_flags[i];
559 l = teamscores_label[i];
560 out = strcat(out, GetScoreLogLabel(l, f), ",");
562 out = substring(out, 0, strlen(out) - 1);
564 else if((sk = teamscorekeepers[tm - 1]))
566 for(i = 0; i < MAX_TEAMSCORE; ++i)
567 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
568 out = strcat(out, ftos(sk.(teamscores[i])), ",");
570 for(i = 0; i < MAX_TEAMSCORE; ++i)
571 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
572 out = strcat(out, ftos(sk.(teamscores[i])), ",");
574 for(i = 0; i < MAX_TEAMSCORE; ++i)
575 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
576 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
577 out = strcat(out, ftos(sk.(teamscores[i])), ",");
578 out = substring(out, 0, strlen(out) - 1);
583 float PlayerTeamScore_Compare(entity p1, entity p2)
585 if(teamscores_entities_count)
586 if(p1.team != p2.team)
590 t1 = teamscorekeepers[p1.team - 1];
591 t2 = teamscorekeepers[p2.team - 1];
592 r = TeamScore_Compare(t1, t2);
593 if(r == 0) // ensure a deterministic order
594 r = p1.team - p2.team;
598 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
601 entity PlayerScore_Sort(.float field)
603 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
611 FOR_EACH_PLAYER(p) if(p.scorekeeper)
616 // Now plist points to the whole list.
618 pfirst = plast = world;
623 pprev = pbestprev = world;
625 for(p = plist; (pprev = p), (p = p.chain); )
627 if(PlayerTeamScore_Compare(p, pbest) > 0)
634 // remove pbest out of the chain
635 if(pbestprev == world)
638 pbestprev.chain = pbest.chain;
653 float TeamScore_GetCompareValue(float t)
658 if(t <= 0 || t >= 16)
659 error("Reading score of invalid team!");
661 sk = teamscorekeepers[t - 1];
664 s = sk.teamscores_primary;
665 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
668 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
674 #define SCORESWIDTH 58
675 // TODO put this somewhere in common?
676 string Score_NicePrint_ItemColor(float vflags)
678 if(vflags & SFL_SORT_PRIO_PRIMARY)
680 else if(vflags & SFL_SORT_PRIO_SECONDARY)
686 void Score_NicePrint_Team(entity to, float t, float w)
694 sk = teamscorekeepers[t - 1];
697 s = strcat(s, ColoredTeamName(t));
698 for(i = 0; i < MAX_TEAMSCORE; ++i)
699 if(teamscores_label[i] != "")
701 fl = teamscores_flags[i];
702 sc = sk.(teamscores[i]);
703 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
709 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
711 for(i = 0; i < MAX_SCORE; ++i)
712 if(scores_label[i] != "")
714 fl = scores_flags[i];
715 s2 = scores_label[i];
716 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
722 void Score_NicePrint_Player(entity to, entity p, float w)
732 s = strcat(s, p.netname);
735 i = strlennocol(s) - NAMEWIDTH;
737 s = substring(s, 0, strlen(s) - i);
740 s = strcat(s, strpad(i, ""));
745 for(i = 0; i < MAX_SCORE; ++i)
746 if(scores_label[i] != "")
748 fl = scores_flags[i];
750 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
756 void Score_NicePrint_Spectators(entity to)
758 print_to(to, "Spectators:");
761 void Score_NicePrint_Spectator(entity to, entity p)
763 print_to(to, strcat(" ", p.netname));
766 .float score_dummyfield;
767 void Score_NicePrint(entity to)
774 for(i = 0; i < MAX_SCORE; ++i)
775 if(scores_label[i] != "")
777 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
779 p = PlayerScore_Sort(score_dummyfield);
782 if(!teamscores_entities_count)
783 Score_NicePrint_Team(to, t, w);
786 if(teamscores_entities_count)
788 Score_NicePrint_Team(to, p.team, w);
789 Score_NicePrint_Player(to, p, w);
796 if(p.classname != "player")
799 Score_NicePrint_Spectators(to);
800 Score_NicePrint_Spectator(to, p);