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()
327 s = strcat(s, ":", cvar_string("g_nexuizversion"));
328 s = strcat(s, "::", GetPlayerScoreString(world, 2)); // make this 1 once we can
330 if(teamscores_entities_count)
334 s = strcat(s, ":", GetTeamScoreString(0, 1));
335 for(t = 0; t < 16; ++t)
336 if(teamscorekeepers[t])
337 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
339 WinningConditionHelper_equality = 0;
340 WinningConditionHelper_winnerteam = 0;
341 for(t = 1; t < 16; ++t)
344 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
345 sk2 = teamscorekeepers[t];
346 c = TeamScore_Compare(sk1, sk2);
348 WinningConditionHelper_equality = 1;
351 WinningConditionHelper_equality = 0;
352 WinningConditionHelper_winnerteam = t;
356 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
357 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
358 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
360 if(teamscores_flags_primary & SFL_TIME)
361 WinningConditionHelper_topscore /= 10;
363 if(WinningConditionHelper_equality)
364 WinningConditionHelper_winnerteam = -1;
366 WinningConditionHelper_winner = world;
367 if(WinningConditionHelper_equality)
368 WinningConditionHelper_winnerteam = -1;
370 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
372 if(WinningConditionHelper_topscore == 0)
374 if(scores_flags_primary & SFL_ZERO_IS_WORST)
376 if(WinningConditionHelper_lowerisbetter)
377 WinningConditionHelper_topscore = 999999999;
379 WinningConditionHelper_topscore = -999999999;
381 WinningConditionHelper_equality = 0;
386 WinningConditionHelper_equality = 0;
387 WinningConditionHelper_winner = world;
390 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
392 WinningConditionHelper_equality = 1;
395 WinningConditionHelper_equality = 0;
396 WinningConditionHelper_winner = p;
400 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
401 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
402 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
404 if(teamscores_flags_primary & SFL_TIME)
405 WinningConditionHelper_topscore /= 10;
407 WinningConditionHelper_winnerteam = -1;
408 if(WinningConditionHelper_equality)
409 WinningConditionHelper_winner = world;
411 if(WinningConditionHelper_topscore == 0)
413 if(scores_flags_primary & SFL_ZERO_IS_WORST)
415 if(WinningConditionHelper_lowerisbetter)
416 WinningConditionHelper_topscore = 999999999;
418 WinningConditionHelper_topscore = -999999999;
420 WinningConditionHelper_equality = 0;
425 strunzone(worldstatus);
426 worldstatus = strzone(s);
430 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
431 s = GetPlayerScoreString(p, 1);
432 if(clienttype(p) == CLIENTTYPE_REAL)
433 s = strcat(s, ":human");
435 s = strcat(s, ":bot");
436 if(p.classname == "player" || g_arena || g_lms)
437 s = strcat(s, ":", ftos(p.team));
439 s = strcat(s, ":spectator");
441 if(p.classname == "player" || g_arena || g_lms)
442 s = GetPlayerScoreString(p, 2);
447 strunzone(p.clientstatus);
448 p.clientstatus = strzone(s);
452 string GetScoreLogLabel(string label, float fl)
454 if(fl & SFL_LOWER_IS_BETTER)
455 label = strcat(label, "<");
456 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
457 label = strcat(label, "!!");
458 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
459 label = strcat(label, "!");
463 string GetPlayerScoreString(entity pl, float shortString)
474 for(i = 0; i < MAX_SCORE; ++i)
475 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
479 out = strcat(out, GetScoreLogLabel(l, f), ",");
482 for(i = 0; i < MAX_SCORE; ++i)
483 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
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_PRIMARY)
492 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
496 out = strcat(out, GetScoreLogLabel(l, f), ",");
498 out = substring(out, 0, strlen(out) - 1);
500 else if((sk = pl.scorekeeper))
502 for(i = 0; i < MAX_SCORE; ++i)
503 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
504 out = strcat(out, ftos(sk.(scores[i])), ",");
506 for(i = 0; i < MAX_SCORE; ++i)
507 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
508 out = strcat(out, ftos(sk.(scores[i])), ",");
510 for(i = 0; i < MAX_SCORE; ++i)
511 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
512 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
513 out = strcat(out, ftos(sk.(scores[i])), ",");
514 out = substring(out, 0, strlen(out) - 1);
519 string GetTeamScoreString(float tm, float shortString)
530 for(i = 0; i < MAX_SCORE; ++i)
531 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
533 f = teamscores_flags[i];
534 l = teamscores_label[i];
535 out = strcat(out, GetScoreLogLabel(l, f), ",");
538 for(i = 0; i < MAX_SCORE; ++i)
539 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
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_PRIMARY)
548 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
550 f = teamscores_flags[i];
551 l = teamscores_label[i];
552 out = strcat(out, GetScoreLogLabel(l, f), ",");
554 out = substring(out, 0, strlen(out) - 1);
556 else if((sk = teamscorekeepers[tm - 1]))
558 for(i = 0; i < MAX_TEAMSCORE; ++i)
559 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
560 out = strcat(out, ftos(sk.(teamscores[i])), ",");
562 for(i = 0; i < MAX_TEAMSCORE; ++i)
563 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
564 out = strcat(out, ftos(sk.(teamscores[i])), ",");
566 for(i = 0; i < MAX_TEAMSCORE; ++i)
567 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
568 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
569 out = strcat(out, ftos(sk.(teamscores[i])), ",");
570 out = substring(out, 0, strlen(out) - 1);
575 float PlayerTeamScore_Compare(entity p1, entity p2)
577 if(teamscores_entities_count)
578 if(p1.team != p2.team)
582 t1 = teamscorekeepers[p1.team - 1];
583 t2 = teamscorekeepers[p2.team - 1];
584 r = TeamScore_Compare(t1, t2);
585 if(r == 0) // ensure a deterministic order
586 r = p1.team - p2.team;
590 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
593 entity PlayerScore_Sort(.float field)
595 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
603 FOR_EACH_PLAYER(p) if(p.scorekeeper)
608 // Now plist points to the whole list.
610 pfirst = plast = world;
615 pprev = pbestprev = world;
617 for(p = plist; (pprev = p), (p = p.chain); )
619 if(PlayerTeamScore_Compare(p, pbest) > 0)
626 // remove pbest out of the chain
627 if(pbestprev == world)
630 pbestprev.chain = pbest.chain;
645 float TeamScore_GetCompareValue(float t)
650 if(t <= 0 || t >= 16)
651 error("Reading score of invalid team!");
653 sk = teamscorekeepers[t - 1];
656 s = sk.teamscores_primary;
657 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
660 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
666 #define SCORESWIDTH 58
667 // TODO put this somewhere in common?
668 string Score_NicePrint_ItemColor(float vflags)
670 if(vflags & SFL_SORT_PRIO_PRIMARY)
672 else if(vflags & SFL_SORT_PRIO_SECONDARY)
678 void Score_NicePrint_Team(entity to, float t, float w)
686 sk = teamscorekeepers[t - 1];
689 s = strcat(s, ColoredTeamName(t));
690 for(i = 0; i < MAX_TEAMSCORE; ++i)
691 if(teamscores_label[i] != "")
693 fl = teamscores_flags[i];
694 sc = sk.(teamscores[i]);
695 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
701 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
703 for(i = 0; i < MAX_SCORE; ++i)
704 if(scores_label[i] != "")
706 fl = scores_flags[i];
707 s2 = scores_label[i];
708 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
714 void Score_NicePrint_Player(entity to, entity p, float w)
724 s = strcat(s, p.netname);
727 i = strlennocol(s) - NAMEWIDTH;
729 s = substring(s, 0, strlen(s) - i);
732 s = strcat(s, strpad(i, ""));
737 for(i = 0; i < MAX_SCORE; ++i)
738 if(scores_label[i] != "")
740 fl = scores_flags[i];
742 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
748 void Score_NicePrint_Spectators(entity to)
750 print_to(to, "Spectators:");
753 void Score_NicePrint_Spectator(entity to, entity p)
755 print_to(to, strcat(" ", p.netname));
758 .float score_dummyfield;
759 void Score_NicePrint(entity to)
766 for(i = 0; i < MAX_SCORE; ++i)
767 if(scores_label[i] != "")
769 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
771 p = PlayerScore_Sort(score_dummyfield);
774 if(!teamscores_entities_count)
775 Score_NicePrint_Team(to, t, w);
778 if(teamscores_entities_count)
780 Score_NicePrint_Team(to, p.team, w);
781 Score_NicePrint_Player(to, p, w);
788 if(p.classname != "player")
791 Score_NicePrint_Spectators(to);
792 Score_NicePrint_Spectator(to, p);