1 .float scores[MAX_SCORE];
2 .float teamscores[MAX_TEAMSCORE];
5 entity teamscorekeepers[16];
6 string scores_label[MAX_SCORE];
7 float scores_flags[MAX_SCORE];
8 string teamscores_label[MAX_TEAMSCORE];
9 float teamscores_flags[MAX_TEAMSCORE];
10 float teamscores_entities_count;
11 var .float scores_primary;
12 var .float teamscores_primary;
13 float scores_flags_primary;
14 float teamscores_flags_primary;
16 void Net_LinkEntity(entity e)
18 e.model = "net_entity";
20 e.effects = EF_NODEPTHTEST | EF_LOWPRECISION;
23 vector ScoreField_Compare(entity t1, entity t2, .float field, float fieldflags, vector previous) // returns: cmp value, best prio
25 if(!(fieldflags & SFL_SORT_PRIO_MASK)) // column does not sort
27 if(fieldflags & SFL_SORT_PRIO_MASK < previous_y)
29 if(t1.field == t2.field)
32 previous_y = fieldflags & SFL_SORT_PRIO_MASK;
34 if(fieldflags & SFL_ZERO_IS_WORST)
41 else if(t2.field == 0)
48 if(fieldflags & SFL_LOWER_IS_BETTER)
49 previous_x = (t2.field - t1.field);
51 previous_x = (t1.field - t2.field);
60 float TeamScore_SendEntity(entity to, float sendflags)
64 WriteByte(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
65 WriteByte(MSG_ENTITY, self.team - 1);
66 #if MAX_TEAMSCORE <= 3
67 for(i = 0; i < MAX_TEAMSCORE; ++i)
68 WriteShort(MSG_ENTITY, self.teamscores[i]);
70 #if MAX_TEAMSCORE <= 8
71 WriteByte(MSG_ENTITY, sendflags);
73 WriteShort(MSG_ENTITY, sendflags);
76 for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
78 WriteShort(MSG_ENTITY, self.teamscores[i]);
84 void TeamScore_Spawn(float t, string name)
88 ts.classname = "csqc_score_team";
89 ts.SendEntity = TeamScore_SendEntity;
90 ts.netname = name; // not used yet, FIXME
91 //ts.SendFlags = SENDFLAGS_CREATE; // immediately send, so csqc knows about the team
94 teamscorekeepers[t - 1] = ts;
95 ++teamscores_entities_count;
98 float TeamScore_AddToTeam(float t, float scorefield, float score)
101 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
102 if(t <= 0 || t >= 16)
103 error("Adding score to invalid team!");
104 s = teamscorekeepers[t - 1];
106 error("Adding score to unknown team!");
108 if(teamscores_label[scorefield] != "")
109 s.SendFlags |= pow(2, scorefield);
110 return (s.(teamscores[scorefield]) += score);
113 float TeamScore_Add(entity player, float scorefield, float score)
115 return TeamScore_AddToTeam(player.team, scorefield, score);
118 float TeamScore_Compare(entity t1, entity t2)
120 if(!t1 || !t2) return (!t2) - !t1;
124 for(i = 0; i < MAX_TEAMSCORE; ++i)
128 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
134 * the scoreinfo entity
137 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
139 scores_label[i] = label;
140 scores_flags[i] = scoreflags;
141 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
143 scores_primary = scores[i];
144 scores_flags_primary = scoreflags;
148 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
150 teamscores_label[i] = label;
151 teamscores_flags[i] = scoreflags;
152 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
154 teamscores_primary = teamscores[i];
155 teamscores_flags_primary = scoreflags;
159 void ScoreInfo_Write(float targ)
162 if not(scores_initialized)
164 WriteByte(targ, SVC_TEMPENTITY);
165 WriteByte(targ, TE_CSQC_SCORESINFO);
166 WriteByte(targ, game);
167 for(i = 0; i < MAX_SCORE; ++i)
169 WriteString(targ, scores_label[i]);
170 WriteByte(targ, scores_flags[i]);
172 for(i = 0; i < MAX_TEAMSCORE; ++i)
174 WriteString(targ, teamscores_label[i]);
175 WriteByte(targ, teamscores_flags[i]);
179 void ScoreInfo_Init(float teams)
181 scores_initialized = 1;
183 TeamScore_Spawn(COLOR_TEAM1, "Red");
185 TeamScore_Spawn(COLOR_TEAM2, "Blue");
187 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
189 TeamScore_Spawn(COLOR_TEAM4, "Pink");
190 ScoreInfo_Write(MSG_ALL);
194 * per-player score entities
197 float PlayerScore_SendEntity(entity to, float sendflags)
201 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
202 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
204 for(i = 0; i < MAX_SCORE; ++i)
205 WriteShort(MSG_ENTITY, self.scores[i]);
208 WriteByte(MSG_ENTITY, sendflags);
210 WriteShort(MSG_ENTITY, sendflags);
213 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
215 WriteShort(MSG_ENTITY, self.scores[i]);
221 void PlayerScore_Clear(entity player)
226 if(teamscores_entities_count)
230 if(g_race && !g_race_qualifying) return;
232 sk = player.scorekeeper;
233 for(i = 0; i < MAX_SCORE; ++i)
235 if(sk.(scores[i]) != 0)
236 if(scores_label[i] != "")
237 sk.SendFlags |= pow(2, i);
242 void Score_ClearAll()
246 FOR_EACH_CLIENTSLOT(p)
251 for(i = 0; i < MAX_SCORE; ++i)
253 if(sk.(scores[i]) != 0)
254 if(scores_label[i] != "")
255 sk.SendFlags |= pow(2, i);
259 for(t = 0; t < 16; ++t)
261 sk = teamscorekeepers[t];
264 for(i = 0; i < MAX_SCORE; ++i)
266 if(sk.(teamscores[i]) != 0)
267 if(teamscores_label[i] != "")
268 sk.SendFlags |= pow(2, i);
269 sk.(teamscores[i]) = 0;
274 void PlayerScore_Attach(entity player)
277 if(player.scorekeeper)
278 error("player already has a scorekeeper");
281 sk.SendEntity = PlayerScore_SendEntity;
283 player.scorekeeper = sk;
286 void PlayerScore_Detach(entity player)
288 if(!player.scorekeeper)
289 error("player has no scorekeeper");
290 remove(player.scorekeeper);
291 player.scorekeeper = world;
294 float PlayerScore_Add(entity player, float scorefield, float score)
297 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
298 s = player.scorekeeper;
300 error("Adding score to unknown player!");
302 if(scores_label[scorefield] != "")
303 s.SendFlags |= pow(2, scorefield);
304 return (s.(scores[scorefield]) += score);
307 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
310 r = PlayerScore_Add(player, pscorefield, score);
311 if(teamscores_entities_count) // only for teamplay
312 r = TeamScore_Add(player, tscorefield, score);
316 float PlayerScore_Compare(entity t1, entity t2)
318 if(!t1 || !t2) return (!t2) - !t1;
322 for(i = 0; i < MAX_SCORE; ++i)
326 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
331 void WinningConditionHelper()
337 s = strcat(s, ":", cvar_string("g_nexuizversion"));
338 s = strcat(s, "::", GetPlayerScoreString(world, 2)); // make this 1 once we can
340 if(teamscores_entities_count)
344 s = strcat(s, ":", GetTeamScoreString(0, 1));
345 for(t = 0; t < 16; ++t)
346 if(teamscorekeepers[t])
347 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
349 WinningConditionHelper_equality = 0;
350 WinningConditionHelper_winnerteam = 0;
351 for(t = 1; t < 16; ++t)
354 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
355 sk2 = teamscorekeepers[t];
356 c = TeamScore_Compare(sk1, sk2);
358 WinningConditionHelper_equality = 1;
361 WinningConditionHelper_equality = 0;
362 WinningConditionHelper_winnerteam = t;
366 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
367 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
368 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
369 if(teamscores_flags_primary & SFL_TIME)
370 WinningConditionHelper_topscore /= 10;
372 WinningConditionHelper_winner = world;
373 if(WinningConditionHelper_equality)
374 WinningConditionHelper_winnerteam = -1;
376 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
380 WinningConditionHelper_equality = 0;
381 WinningConditionHelper_winner = world;
384 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
386 WinningConditionHelper_equality = 1;
389 WinningConditionHelper_equality = 0;
390 WinningConditionHelper_winner = p;
394 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
395 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
396 if(scores_flags_primary & SFL_ZERO_IS_WORST)
397 if(WinningConditionHelper_topscore == 0)
399 if(WinningConditionHelper_lowerisbetter)
400 WinningConditionHelper_topscore = 999999999;
402 WinningConditionHelper_topscore = -999999999;
404 if(teamscores_flags_primary & SFL_TIME)
405 WinningConditionHelper_topscore /= 10;
407 if(WinningConditionHelper_equality)
408 WinningConditionHelper_winner = world;
409 WinningConditionHelper_winnerteam = -1;
413 strunzone(worldstatus);
414 worldstatus = strzone(s);
418 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
419 s = GetPlayerScoreString(p, 1);
420 if(clienttype(p) == CLIENTTYPE_REAL)
421 s = strcat(s, ":human");
423 s = strcat(s, ":bot");
424 if(p.classname == "player" || g_arena || g_lms)
425 s = strcat(s, ":", ftos(p.team));
427 s = strcat(s, ":spectator");
429 if(p.classname == "player" || g_arena || g_lms)
432 s = GetPlayerScoreString(p, 2);
435 strunzone(p.clientstatus);
436 p.clientstatus = strzone(s);
440 string GetScoreLogLabel(string label, float fl)
442 if(fl & SFL_LOWER_IS_BETTER)
443 label = strcat(label, "<");
444 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
445 label = strcat(label, "!!");
446 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
447 label = strcat(label, "!");
451 string GetPlayerScoreString(entity pl, float shortString)
462 for(i = 0; i < MAX_SCORE; ++i)
463 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
467 out = strcat(out, GetScoreLogLabel(l, f), ",");
470 for(i = 0; i < MAX_SCORE; ++i)
471 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
475 out = strcat(out, GetScoreLogLabel(l, f), ",");
478 for(i = 0; i < MAX_SCORE; ++i)
479 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
480 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
484 out = strcat(out, GetScoreLogLabel(l, f), ",");
486 out = substring(out, 0, strlen(out) - 1);
488 else if((sk = pl.scorekeeper))
490 for(i = 0; i < MAX_SCORE; ++i)
491 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
492 out = strcat(out, ftos(sk.(scores[i])), ",");
494 for(i = 0; i < MAX_SCORE; ++i)
495 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
496 out = strcat(out, ftos(sk.(scores[i])), ",");
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)
501 out = strcat(out, ftos(sk.(scores[i])), ",");
502 out = substring(out, 0, strlen(out) - 1);
507 string GetTeamScoreString(float tm, float shortString)
518 for(i = 0; i < MAX_SCORE; ++i)
519 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
521 f = teamscores_flags[i];
522 l = teamscores_label[i];
523 out = strcat(out, GetScoreLogLabel(l, f), ",");
526 for(i = 0; i < MAX_SCORE; ++i)
527 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
529 f = teamscores_flags[i];
530 l = teamscores_label[i];
531 out = strcat(out, GetScoreLogLabel(l, f), ",");
534 for(i = 0; i < MAX_SCORE; ++i)
535 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
536 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
538 f = teamscores_flags[i];
539 l = teamscores_label[i];
540 out = strcat(out, GetScoreLogLabel(l, f), ",");
542 out = substring(out, 0, strlen(out) - 1);
544 else if((sk = teamscorekeepers[tm - 1]))
546 for(i = 0; i < MAX_TEAMSCORE; ++i)
547 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
548 out = strcat(out, ftos(sk.(teamscores[i])), ",");
550 for(i = 0; i < MAX_TEAMSCORE; ++i)
551 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
552 out = strcat(out, ftos(sk.(teamscores[i])), ",");
554 for(i = 0; i < MAX_TEAMSCORE; ++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)
557 out = strcat(out, ftos(sk.(teamscores[i])), ",");
558 out = substring(out, 0, strlen(out) - 1);
563 float PlayerTeamScore_Compare(entity p1, entity p2)
565 if(teamscores_entities_count)
566 if(p1.team != p2.team)
570 t1 = teamscorekeepers[p1.team - 1];
571 t2 = teamscorekeepers[p2.team - 1];
572 r = TeamScore_Compare(t1, t2);
573 if(r == 0) // ensure a deterministic order
574 r = p1.team - p2.team;
578 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
581 entity PlayerScore_Sort(.float field)
583 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
590 FOR_EACH_PLAYER(p) if(p.scorekeeper)
595 // Now plist points to the whole list.
597 pfirst = plast = world;
602 pprev = pbestprev = world;
604 for(p = plist; (pprev = p), (p = p.chain); )
606 if(PlayerTeamScore_Compare(p, pbest) > 0)
613 // remove pbest out of the chain
614 if(pbestprev == world)
617 pbestprev.chain = pbest.chain;
631 float TeamScore_GetCompareValue(float t)
636 if(t <= 0 || t >= 16)
637 error("Reading score of invalid team!");
639 sk = teamscorekeepers[t - 1];
642 s = sk.teamscores_primary;
643 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
646 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
652 #define SCORESWIDTH 58
653 // TODO put this somewhere in common?
654 string Score_NicePrint_ItemColor(float vflags)
656 if(vflags & SFL_SORT_PRIO_PRIMARY)
658 else if(vflags & SFL_SORT_PRIO_SECONDARY)
664 void Score_NicePrint_Team(entity to, float t, float w)
672 sk = teamscorekeepers[t - 1];
675 s = strcat(s, ColoredTeamName(t));
676 for(i = 0; i < MAX_TEAMSCORE; ++i)
677 if(teamscores_label[i] != "")
679 fl = teamscores_flags[i];
680 sc = sk.(teamscores[i]);
681 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
687 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
689 for(i = 0; i < MAX_SCORE; ++i)
690 if(scores_label[i] != "")
692 fl = scores_flags[i];
693 s2 = scores_label[i];
694 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
700 void Score_NicePrint_Player(entity to, entity p, float w)
710 s = strcat(s, p.netname);
713 i = strlennocol(s) - NAMEWIDTH;
715 s = substring(s, 0, strlen(s) - i);
718 s = strcat(s, strpad(i, ""));
723 for(i = 0; i < MAX_SCORE; ++i)
724 if(scores_label[i] != "")
726 fl = scores_flags[i];
728 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
734 void Score_NicePrint_Spectators(entity to)
736 print_to(to, "Spectators:");
739 void Score_NicePrint_Spectator(entity to, entity p)
741 print_to(to, strcat(" ", p.netname));
744 .float score_dummyfield;
745 void Score_NicePrint(entity to)
752 for(i = 0; i < MAX_SCORE; ++i)
753 if(scores_label[i] != "")
755 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
757 p = PlayerScore_Sort(score_dummyfield);
760 if(!teamscores_entities_count)
761 Score_NicePrint_Team(to, t, w);
764 if(teamscores_entities_count)
766 Score_NicePrint_Team(to, p.team, w);
767 Score_NicePrint_Player(to, p, w);
774 if(p.classname != "player")
777 Score_NicePrint_Spectators(to);
778 Score_NicePrint_Spectator(to, p);