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
98 error("Adding score to invalid team!");
100 s = teamscorekeepers[t - 1];
105 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 float ScoreInfo_SendEntity(entity to, float sf)
162 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
163 WriteByte(MSG_ENTITY, game);
164 for(i = 0; i < MAX_SCORE; ++i)
166 WriteString(MSG_ENTITY, scores_label[i]);
167 WriteByte(MSG_ENTITY, scores_flags[i]);
169 for(i = 0; i < MAX_TEAMSCORE; ++i)
171 WriteString(MSG_ENTITY, teamscores_label[i]);
172 WriteByte(MSG_ENTITY, teamscores_flags[i]);
177 void ScoreInfo_Init(float teams)
179 if(scores_initialized)
181 scores_initialized.SendFlags |= 1; // force a resend
185 scores_initialized = spawn();
186 scores_initialized.classname = "ent_client_scoreinfo";
187 Net_LinkEntity(scores_initialized, FALSE, 0, ScoreInfo_SendEntity);
190 TeamScore_Spawn(COLOR_TEAM1, "Red");
192 TeamScore_Spawn(COLOR_TEAM2, "Blue");
194 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
196 TeamScore_Spawn(COLOR_TEAM4, "Pink");
200 * per-player score entities
203 float PlayerScore_SendEntity(entity to, float sendflags)
207 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
208 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
210 for(i = 0; i < MAX_SCORE; ++i)
211 WriteShort(MSG_ENTITY, self.scores[i]);
214 WriteByte(MSG_ENTITY, sendflags);
216 WriteShort(MSG_ENTITY, sendflags);
219 for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
221 WriteShort(MSG_ENTITY, self.scores[i]);
227 void PlayerScore_Clear(entity player)
232 if(teamscores_entities_count)
236 if(g_race && !g_race_qualifying) return;
238 sk = player.scorekeeper;
239 for(i = 0; i < MAX_SCORE; ++i)
241 if(sk.(scores[i]) != 0)
242 if(scores_label[i] != "")
243 sk.SendFlags |= pow(2, i);
248 void Score_ClearAll()
252 FOR_EACH_CLIENTSLOT(p)
257 for(i = 0; i < MAX_SCORE; ++i)
259 if(sk.(scores[i]) != 0)
260 if(scores_label[i] != "")
261 sk.SendFlags |= pow(2, i);
265 for(t = 0; t < 16; ++t)
267 sk = teamscorekeepers[t];
270 for(i = 0; i < MAX_TEAMSCORE; ++i)
272 if(sk.(teamscores[i]) != 0)
273 if(teamscores_label[i] != "")
274 sk.SendFlags |= pow(2, i);
275 sk.(teamscores[i]) = 0;
280 void PlayerScore_Attach(entity player)
283 if(player.scorekeeper)
284 error("player already has a scorekeeper");
287 Net_LinkEntity(sk, FALSE, 0, PlayerScore_SendEntity);
288 player.scorekeeper = sk;
291 void PlayerScore_Detach(entity player)
293 if(!player.scorekeeper)
294 error("player has no scorekeeper");
295 remove(player.scorekeeper);
296 player.scorekeeper = world;
299 float PlayerScore_Add(entity player, float scorefield, float score)
304 if not(g_lms && scorefield == SP_LMS_RANK) // allow writing to this field in intermission as it is needed for newly joining players
307 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
308 s = player.scorekeeper;
313 error("Adding score to unknown player!");
316 if(scores_label[scorefield] != "")
317 s.SendFlags |= pow(2, scorefield);
318 return (s.(scores[scorefield]) += score);
321 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
324 r = PlayerScore_Add(player, pscorefield, score);
325 if(teamscores_entities_count) // only for teamplay
326 r = TeamScore_Add(player, tscorefield, score);
330 float PlayerScore_Compare(entity t1, entity t2)
332 if(!t1 || !t2) return (!t2) - !t1;
336 for(i = 0; i < MAX_SCORE; ++i)
340 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
345 void WinningConditionHelper()
351 entity winnerscorekeeper;
352 entity secondscorekeeper;
356 s = strcat(s, ":", cvar_string("g_nexuizversion"));
357 s = strcat(s, "::", GetPlayerScoreString(world, 2)); // make this 1 once we can
359 fullstatus = cvar("g_full_getstatus_responses");
361 if(teamscores_entities_count)
365 s = strcat(s, ":", GetTeamScoreString(0, 1));
366 for(t = 0; t < 16; ++t)
367 if(teamscorekeepers[t])
368 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
370 WinningConditionHelper_winnerteam = -1;
371 WinningConditionHelper_secondteam = -1;
372 winnerscorekeeper = world;
373 secondscorekeeper = world;
374 for(t = 0; t < 16; ++t)
376 sk = teamscorekeepers[t];
377 c = TeamScore_Compare(winnerscorekeeper, sk);
380 WinningConditionHelper_secondteam = WinningConditionHelper_winnerteam;
381 WinningConditionHelper_winnerteam = t + 1;
382 secondscorekeeper = winnerscorekeeper;
383 winnerscorekeeper = sk;
387 c = TeamScore_Compare(secondscorekeeper, sk);
390 WinningConditionHelper_secondteam = t + 1;
391 secondscorekeeper = sk;
396 WinningConditionHelper_equality = (TeamScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
397 if(WinningConditionHelper_equality)
398 WinningConditionHelper_winnerteam = WinningConditionHelper_secondteam = -1;
400 WinningConditionHelper_topscore = winnerscorekeeper.teamscores_primary;
401 WinningConditionHelper_secondscore = secondscorekeeper.teamscores_primary;
402 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
403 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
405 WinningConditionHelper_winner = world; // not supported in teamplay
406 WinningConditionHelper_second = world; // not supported in teamplay
410 WinningConditionHelper_winner = world;
411 WinningConditionHelper_second = world;
412 winnerscorekeeper = world;
413 secondscorekeeper = world;
417 c = PlayerScore_Compare(winnerscorekeeper, sk);
420 WinningConditionHelper_second = WinningConditionHelper_winner;
421 WinningConditionHelper_winner = p;
422 secondscorekeeper = winnerscorekeeper;
423 winnerscorekeeper = sk;
427 c = PlayerScore_Compare(secondscorekeeper, sk);
430 WinningConditionHelper_second = p;
431 secondscorekeeper = sk;
436 WinningConditionHelper_equality = (PlayerScore_Compare(winnerscorekeeper, secondscorekeeper) == 0);
437 if(WinningConditionHelper_equality)
438 WinningConditionHelper_winner = WinningConditionHelper_second = world;
440 WinningConditionHelper_topscore = winnerscorekeeper.scores_primary;
441 WinningConditionHelper_secondscore = secondscorekeeper.scores_primary;
442 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
443 WinningConditionHelper_zeroisworst = (scores_flags_primary & SFL_ZERO_IS_WORST);
445 WinningConditionHelper_winnerteam = -1; // no teamplay
446 WinningConditionHelper_secondteam = -1; // no teamplay
449 if(teamscores_flags_primary & SFL_TIME)
450 WinningConditionHelper_topscore /= 10;
452 if(WinningConditionHelper_topscore == 0)
454 if(scores_flags_primary & SFL_ZERO_IS_WORST)
456 if(WinningConditionHelper_lowerisbetter)
457 WinningConditionHelper_topscore = 999999999;
459 WinningConditionHelper_topscore = -999999999;
463 if(WinningConditionHelper_secondscore == 0)
465 if(scores_flags_primary & SFL_ZERO_IS_WORST)
467 if(WinningConditionHelper_lowerisbetter)
468 WinningConditionHelper_secondscore = 999999999;
470 WinningConditionHelper_secondscore = -999999999;
475 strunzone(worldstatus);
476 worldstatus = strzone(s);
482 s = GetPlayerScoreString(p, 1);
483 if(clienttype(p) == CLIENTTYPE_REAL)
484 s = strcat(s, ":human");
486 s = strcat(s, ":bot");
487 if(p.classname != "player" && !g_arena && !g_lms)
488 s = strcat(s, ":spectator");
492 if(p.classname == "player" || g_arena || g_lms)
493 s = GetPlayerScoreString(p, 2);
499 strunzone(p.clientstatus);
500 p.clientstatus = strzone(s);
504 string GetScoreLogLabel(string label, float fl)
506 if(fl & SFL_LOWER_IS_BETTER)
507 label = strcat(label, "<");
508 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
509 label = strcat(label, "!!");
510 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
511 label = strcat(label, "!");
515 string GetPlayerScoreString(entity pl, float shortString)
526 for(i = 0; i < MAX_SCORE; ++i)
527 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
531 out = strcat(out, GetScoreLogLabel(l, f), ",");
534 for(i = 0; i < MAX_SCORE; ++i)
535 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
539 out = strcat(out, GetScoreLogLabel(l, f), ",");
542 for(i = 0; i < MAX_SCORE; ++i)
543 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
544 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
548 out = strcat(out, GetScoreLogLabel(l, f), ",");
550 out = substring(out, 0, strlen(out) - 1);
552 else if((sk = pl.scorekeeper))
554 for(i = 0; i < MAX_SCORE; ++i)
555 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
556 out = strcat(out, ftos(sk.(scores[i])), ",");
558 for(i = 0; i < MAX_SCORE; ++i)
559 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
560 out = strcat(out, ftos(sk.(scores[i])), ",");
562 for(i = 0; i < MAX_SCORE; ++i)
563 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
564 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
565 out = strcat(out, ftos(sk.(scores[i])), ",");
566 out = substring(out, 0, strlen(out) - 1);
571 string GetTeamScoreString(float tm, float shortString)
582 for(i = 0; i < MAX_SCORE; ++i)
583 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
585 f = teamscores_flags[i];
586 l = teamscores_label[i];
587 out = strcat(out, GetScoreLogLabel(l, f), ",");
590 for(i = 0; i < MAX_SCORE; ++i)
591 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
593 f = teamscores_flags[i];
594 l = teamscores_label[i];
595 out = strcat(out, GetScoreLogLabel(l, f), ",");
598 for(i = 0; i < MAX_SCORE; ++i)
599 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
600 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
602 f = teamscores_flags[i];
603 l = teamscores_label[i];
604 out = strcat(out, GetScoreLogLabel(l, f), ",");
606 out = substring(out, 0, strlen(out) - 1);
608 else if((sk = teamscorekeepers[tm - 1]))
610 for(i = 0; i < MAX_TEAMSCORE; ++i)
611 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
612 out = strcat(out, ftos(sk.(teamscores[i])), ",");
614 for(i = 0; i < MAX_TEAMSCORE; ++i)
615 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
616 out = strcat(out, ftos(sk.(teamscores[i])), ",");
618 for(i = 0; i < MAX_TEAMSCORE; ++i)
619 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
620 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
621 out = strcat(out, ftos(sk.(teamscores[i])), ",");
622 out = substring(out, 0, strlen(out) - 1);
627 float PlayerTeamScore_Compare(entity p1, entity p2)
629 if(teamscores_entities_count)
630 if(p1.team != p2.team)
634 t1 = teamscorekeepers[p1.team - 1];
635 t2 = teamscorekeepers[p2.team - 1];
636 r = TeamScore_Compare(t1, t2);
637 if(r == 0) // ensure a deterministic order
638 r = p1.team - p2.team;
642 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
645 entity PlayerScore_Sort(.float field)
647 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
655 FOR_EACH_PLAYER(p) if(p.scorekeeper)
660 // Now plist points to the whole list.
662 pfirst = plast = world;
667 pprev = pbestprev = world;
669 for(p = plist; (pprev = p), (p = p.chain); )
671 if(PlayerTeamScore_Compare(p, pbest) > 0)
678 // remove pbest out of the chain
679 if(pbestprev == world)
682 pbestprev.chain = pbest.chain;
697 float TeamScore_GetCompareValue(float t)
702 if(t <= 0 || t >= 16)
706 error("Reading score of invalid team!");
709 sk = teamscorekeepers[t - 1];
712 s = sk.teamscores_primary;
713 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
716 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
722 #define SCORESWIDTH 58
723 // TODO put this somewhere in common?
724 string Score_NicePrint_ItemColor(float vflags)
726 if(vflags & SFL_SORT_PRIO_PRIMARY)
728 else if(vflags & SFL_SORT_PRIO_SECONDARY)
734 void Score_NicePrint_Team(entity to, float t, float w)
742 sk = teamscorekeepers[t - 1];
745 s = strcat(s, ColoredTeamName(t));
746 for(i = 0; i < MAX_TEAMSCORE; ++i)
747 if(teamscores_label[i] != "")
749 fl = teamscores_flags[i];
750 sc = sk.(teamscores[i]);
751 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
757 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
759 for(i = 0; i < MAX_SCORE; ++i)
760 if(scores_label[i] != "")
762 fl = scores_flags[i];
763 s2 = scores_label[i];
764 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
770 void Score_NicePrint_Player(entity to, entity p, float w)
780 s = strcat(s, p.netname);
783 i = strlennocol(s) - NAMEWIDTH;
785 s = substring(s, 0, strlen(s) - i);
788 s = strcat(s, strpad(i, ""));
793 for(i = 0; i < MAX_SCORE; ++i)
794 if(scores_label[i] != "")
796 fl = scores_flags[i];
798 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
804 void Score_NicePrint_Spectators(entity to)
806 print_to(to, "Spectators:");
809 void Score_NicePrint_Spectator(entity to, entity p)
811 print_to(to, strcat(" ", p.netname));
814 .float score_dummyfield;
815 void Score_NicePrint(entity to)
822 for(i = 0; i < MAX_SCORE; ++i)
823 if(scores_label[i] != "")
825 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
827 p = PlayerScore_Sort(score_dummyfield);
830 if(!teamscores_entities_count)
831 Score_NicePrint_Team(to, t, w);
834 if(teamscores_entities_count)
836 Score_NicePrint_Team(to, p.team, w);
837 Score_NicePrint_Player(to, p, w);
844 if(p.classname != "player")
847 Score_NicePrint_Spectators(to);
848 Score_NicePrint_Spectator(to, p);