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.netname = name; // not used yet, FIXME
81 Net_LinkEntity(ts, FALSE, 0, TeamScore_SendEntity);
82 teamscorekeepers[t - 1] = ts;
83 ++teamscores_entities_count;
86 float TeamScore_AddToTeam(float t, float scorefield, float score)
93 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
95 error("Adding score to invalid team!");
96 s = teamscorekeepers[t - 1];
98 error("Adding score to unknown team!");
100 if(teamscores_label[scorefield] != "")
101 s.SendFlags |= pow(2, scorefield);
102 return (s.(teamscores[scorefield]) += score);
105 float TeamScore_Add(entity player, float scorefield, float score)
107 return TeamScore_AddToTeam(player.team, scorefield, score);
110 float TeamScore_Compare(entity t1, entity t2)
112 if(!t1 || !t2) return (!t2) - !t1;
116 for(i = 0; i < MAX_TEAMSCORE; ++i)
120 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
126 * the scoreinfo entity
129 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
131 scores_label[i] = label;
132 scores_flags[i] = scoreflags;
133 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
135 scores_primary = scores[i];
136 scores_flags_primary = scoreflags;
140 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
142 teamscores_label[i] = label;
143 teamscores_flags[i] = scoreflags;
144 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
146 teamscores_primary = teamscores[i];
147 teamscores_flags_primary = scoreflags;
151 float ScoreInfo_SendEntity(entity to, float sf)
154 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
155 WriteByte(MSG_ENTITY, game);
156 for(i = 0; i < MAX_SCORE; ++i)
158 WriteString(MSG_ENTITY, scores_label[i]);
159 WriteByte(MSG_ENTITY, scores_flags[i]);
161 for(i = 0; i < MAX_TEAMSCORE; ++i)
163 WriteString(MSG_ENTITY, teamscores_label[i]);
164 WriteByte(MSG_ENTITY, teamscores_flags[i]);
169 void ScoreInfo_Init(float teams)
171 if(scores_initialized)
173 scores_initialized.SendFlags |= 1; // force a resend
177 scores_initialized = spawn();
178 scores_initialized.classname = "ent_client_scoreinfo";
179 Net_LinkEntity(scores_initialized, FALSE, 0, ScoreInfo_SendEntity);
182 TeamScore_Spawn(COLOR_TEAM1, "Red");
184 TeamScore_Spawn(COLOR_TEAM2, "Blue");
186 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
188 TeamScore_Spawn(COLOR_TEAM4, "Pink");
192 * per-player score entities
195 float PlayerScore_SendEntity(entity to, float sendflags)
199 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
200 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
202 for(i = 0; i < MAX_SCORE; ++i)
203 WriteShort(MSG_ENTITY, self.scores[i]);
206 WriteByte(MSG_ENTITY, sendflags);
208 WriteShort(MSG_ENTITY, sendflags);
211 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
213 WriteShort(MSG_ENTITY, self.scores[i]);
219 void PlayerScore_Clear(entity player)
224 if(teamscores_entities_count)
228 if(g_race && !g_race_qualifying) return;
230 sk = player.scorekeeper;
231 for(i = 0; i < MAX_SCORE; ++i)
233 if(sk.(scores[i]) != 0)
234 if(scores_label[i] != "")
235 sk.SendFlags |= pow(2, i);
240 void Score_ClearAll()
244 FOR_EACH_CLIENTSLOT(p)
249 for(i = 0; i < MAX_SCORE; ++i)
251 if(sk.(scores[i]) != 0)
252 if(scores_label[i] != "")
253 sk.SendFlags |= pow(2, i);
257 for(t = 0; t < 16; ++t)
259 sk = teamscorekeepers[t];
262 for(i = 0; i < MAX_TEAMSCORE; ++i)
264 if(sk.(teamscores[i]) != 0)
265 if(teamscores_label[i] != "")
266 sk.SendFlags |= pow(2, i);
267 sk.(teamscores[i]) = 0;
272 void PlayerScore_Attach(entity player)
275 if(player.scorekeeper)
276 error("player already has a scorekeeper");
279 Net_LinkEntity(sk, FALSE, 0, 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)
296 if not(g_lms && scorefield == SP_LMS_RANK) // allow writing to this field in intermission as it is needed for newly joining players
299 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
300 s = player.scorekeeper;
302 error("Adding score to unknown player!");
304 if(scores_label[scorefield] != "")
305 s.SendFlags |= pow(2, scorefield);
306 return (s.(scores[scorefield]) += score);
309 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
312 r = PlayerScore_Add(player, pscorefield, score);
313 if(teamscores_entities_count) // only for teamplay
314 r = TeamScore_Add(player, tscorefield, score);
318 float PlayerScore_Compare(entity t1, entity t2)
320 if(!t1 || !t2) return (!t2) - !t1;
324 for(i = 0; i < MAX_SCORE; ++i)
328 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
333 void WinningConditionHelper()
339 entity winnerscorekeeper;
340 entity secondscorekeeper;
344 s = strcat(s, ":", cvar_string("g_nexuizversion"));
345 s = strcat(s, "::", GetPlayerScoreString(world, 2)); // make this 1 once we can
347 fullstatus = cvar("g_full_getstatus_responses");
349 if(teamscores_entities_count)
353 s = strcat(s, ":", GetTeamScoreString(0, 1));
354 for(t = 0; t < 16; ++t)
355 if(teamscorekeepers[t])
356 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
358 WinningConditionHelper_winnerteam = -1;
359 WinningConditionHelper_secondteam = -1;
360 winnerscorekeeper = world;
361 secondscorekeeper = world;
362 for(t = 0; t < 16; ++t)
364 sk = teamscorekeepers[t];
365 c = TeamScore_Compare(winnerscorekeeper, sk);
368 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
369 WinningConditionHelper_winnerteam = t + 1;
370 secondscorekeeper = winnerscorekeeper;
371 winnerscorekeeper = sk;
375 c = TeamScore_Compare(secondscorekeeper, sk);
378 WinningConditionHelper_secondteam = t + 1;
379 secondscorekeeper = sk;
384 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
385 if(WinningConditionHelper_equality)
386 WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
388 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
389 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
390 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
391 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
393 WinningConditionHelper_winner = world; // not supported in teamplay
394 WinningConditionHelper_second = world; // not supported in teamplay
398 WinningConditionHelper_winner = world;
399 WinningConditionHelper_second = world;
400 winnerscorekeeper = world;
401 secondscorekeeper = world;
405 c = PlayerScore_Compare(winnerscorekeeper, sk);
408 WinningConditionHelper_second = WinningConditionHelper_winner;
409 WinningConditionHelper_winner = p;
410 secondscorekeeper = winnerscorekeeper;
411 winnerscorekeeper = sk;
415 c = PlayerScore_Compare(secondscorekeeper, sk);
418 WinningConditionHelper_second = p;
419 secondscorekeeper = sk;
424 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
425 if(WinningConditionHelper_equality)
426 WinningConditionHelper_winner = WinningConditionHelper_second = world;
428 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
429 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
430 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
431 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
433 WinningConditionHelper_winnerteam = -1; // no teamplay
434 WinningConditionHelper_secondteam = -1; // no teamplay
437 if(teamscores_flags_primary & SFL_TIME)
438 WinningConditionHelper_topscore /= 10;
440 if(WinningConditionHelper_topscore == 0)
442 if(scores_flags_primary & SFL_ZERO_IS_WORST)
444 if(WinningConditionHelper_lowerisbetter)
445 WinningConditionHelper_topscore = 999999999;
447 WinningConditionHelper_topscore = -999999999;
451 if(WinningConditionHelper_secondscore == 0)
453 if(scores_flags_primary & SFL_ZERO_IS_WORST)
455 if(WinningConditionHelper_lowerisbetter)
456 WinningConditionHelper_secondscore = 999999999;
458 WinningConditionHelper_secondscore = -999999999;
463 strunzone(worldstatus);
464 worldstatus = strzone(s);
470 s = GetPlayerScoreString(p, 1);
471 if(clienttype(p) == CLIENTTYPE_REAL)
472 s = strcat(s, ":human");
474 s = strcat(s, ":bot");
475 if(p.classname != "player" && !g_arena && !g_lms)
476 s = strcat(s, ":spectator");
480 if(p.classname == "player" || g_arena || g_lms)
481 s = GetPlayerScoreString(p, 2);
487 strunzone(p.clientstatus);
488 p.clientstatus = strzone(s);
492 string GetScoreLogLabel(string label, float fl)
494 if(fl & SFL_LOWER_IS_BETTER)
495 label = strcat(label, "<");
496 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
497 label = strcat(label, "!!");
498 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
499 label = strcat(label, "!");
503 string GetPlayerScoreString(entity pl, float shortString)
514 for(i = 0; i < MAX_SCORE; ++i)
515 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
519 out = strcat(out, GetScoreLogLabel(l, f), ",");
522 for(i = 0; i < MAX_SCORE; ++i)
523 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
527 out = strcat(out, GetScoreLogLabel(l, f), ",");
530 for(i = 0; i < MAX_SCORE; ++i)
531 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
532 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
536 out = strcat(out, GetScoreLogLabel(l, f), ",");
538 out = substring(out, 0, strlen(out) - 1);
540 else if((sk = pl.scorekeeper))
542 for(i = 0; i < MAX_SCORE; ++i)
543 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
544 out = strcat(out, ftos(sk.(scores[i])), ",");
546 for(i = 0; i < MAX_SCORE; ++i)
547 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
548 out = strcat(out, ftos(sk.(scores[i])), ",");
550 for(i = 0; i < MAX_SCORE; ++i)
551 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
552 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
553 out = strcat(out, ftos(sk.(scores[i])), ",");
554 out = substring(out, 0, strlen(out) - 1);
559 string GetTeamScoreString(float tm, float shortString)
570 for(i = 0; i < MAX_SCORE; ++i)
571 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
573 f = teamscores_flags[i];
574 l = teamscores_label[i];
575 out = strcat(out, GetScoreLogLabel(l, f), ",");
578 for(i = 0; i < MAX_SCORE; ++i)
579 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
581 f = teamscores_flags[i];
582 l = teamscores_label[i];
583 out = strcat(out, GetScoreLogLabel(l, f), ",");
586 for(i = 0; i < MAX_SCORE; ++i)
587 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
588 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
590 f = teamscores_flags[i];
591 l = teamscores_label[i];
592 out = strcat(out, GetScoreLogLabel(l, f), ",");
594 out = substring(out, 0, strlen(out) - 1);
596 else if((sk = teamscorekeepers[tm - 1]))
598 for(i = 0; i < MAX_TEAMSCORE; ++i)
599 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
600 out = strcat(out, ftos(sk.(teamscores[i])), ",");
602 for(i = 0; i < MAX_TEAMSCORE; ++i)
603 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
604 out = strcat(out, ftos(sk.(teamscores[i])), ",");
606 for(i = 0; i < MAX_TEAMSCORE; ++i)
607 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
608 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
609 out = strcat(out, ftos(sk.(teamscores[i])), ",");
610 out = substring(out, 0, strlen(out) - 1);
615 float PlayerTeamScore_Compare(entity p1, entity p2)
617 if(teamscores_entities_count)
618 if(p1.team != p2.team)
622 t1 = teamscorekeepers[p1.team - 1];
623 t2 = teamscorekeepers[p2.team - 1];
624 r = TeamScore_Compare(t1, t2);
625 if(r == 0) // ensure a deterministic order
626 r = p1.team - p2.team;
630 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
633 entity PlayerScore_Sort(.float field)
635 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
643 FOR_EACH_PLAYER(p) if(p.scorekeeper)
648 // Now plist points to the whole list.
650 pfirst = plast = world;
655 pprev = pbestprev = world;
657 for(p = plist; (pprev = p), (p = p.chain); )
659 if(PlayerTeamScore_Compare(p, pbest) > 0)
666 // remove pbest out of the chain
667 if(pbestprev == world)
670 pbestprev.chain = pbest.chain;
685 float TeamScore_GetCompareValue(float t)
690 if(t <= 0 || t >= 16)
691 error("Reading score of invalid team!");
693 sk = teamscorekeepers[t - 1];
696 s = sk.teamscores_primary;
697 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
700 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
706 #define SCORESWIDTH 58
707 // TODO put this somewhere in common?
708 string Score_NicePrint_ItemColor(float vflags)
710 if(vflags & SFL_SORT_PRIO_PRIMARY)
712 else if(vflags & SFL_SORT_PRIO_SECONDARY)
718 void Score_NicePrint_Team(entity to, float t, float w)
726 sk = teamscorekeepers[t - 1];
729 s = strcat(s, ColoredTeamName(t));
730 for(i = 0; i < MAX_TEAMSCORE; ++i)
731 if(teamscores_label[i] != "")
733 fl = teamscores_flags[i];
734 sc = sk.(teamscores[i]);
735 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
741 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
743 for(i = 0; i < MAX_SCORE; ++i)
744 if(scores_label[i] != "")
746 fl = scores_flags[i];
747 s2 = scores_label[i];
748 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
754 void Score_NicePrint_Player(entity to, entity p, float w)
764 s = strcat(s, p.netname);
767 i = strlennocol(s) - NAMEWIDTH;
769 s = substring(s, 0, strlen(s) - i);
772 s = strcat(s, strpad(i, ""));
777 for(i = 0; i < MAX_SCORE; ++i)
778 if(scores_label[i] != "")
780 fl = scores_flags[i];
782 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
788 void Score_NicePrint_Spectators(entity to)
790 print_to(to, "Spectators:");
793 void Score_NicePrint_Spectator(entity to, entity p)
795 print_to(to, strcat(" ", p.netname));
798 .float score_dummyfield;
799 void Score_NicePrint(entity to)
806 for(i = 0; i < MAX_SCORE; ++i)
807 if(scores_label[i] != "")
809 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
811 p = PlayerScore_Sort(score_dummyfield);
814 if(!teamscores_entities_count)
815 Score_NicePrint_Team(to, t, w);
818 if(teamscores_entities_count)
820 Score_NicePrint_Team(to, p.team, w);
821 Score_NicePrint_Player(to, p, w);
828 if(p.classname != "player")
831 Score_NicePrint_Spectators(to);
832 Score_NicePrint_Spectator(to, p);