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;
461 WinningConditionHelper_equality = 0;
464 if(WinningConditionHelper_secondscore == 0)
466 if(scores_flags_primary & SFL_ZERO_IS_WORST)
468 if(WinningConditionHelper_lowerisbetter)
469 WinningConditionHelper_secondscore = 999999999;
471 WinningConditionHelper_secondscore = -999999999;
476 strunzone(worldstatus);
477 worldstatus = strzone(s);
483 s = GetPlayerScoreString(p, 1);
484 if(clienttype(p) == CLIENTTYPE_REAL)
485 s = strcat(s, ":human");
487 s = strcat(s, ":bot");
488 if(p.classname != "player" && !g_arena && !g_lms)
489 s = strcat(s, ":spectator");
493 if(p.classname == "player" || g_arena || g_lms)
494 s = GetPlayerScoreString(p, 2);
500 strunzone(p.clientstatus);
501 p.clientstatus = strzone(s);
505 string GetScoreLogLabel(string label, float fl)
507 if(fl & SFL_LOWER_IS_BETTER)
508 label = strcat(label, "<");
509 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
510 label = strcat(label, "!!");
511 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
512 label = strcat(label, "!");
516 string GetPlayerScoreString(entity pl, float shortString)
527 for(i = 0; i < MAX_SCORE; ++i)
528 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
532 out = strcat(out, GetScoreLogLabel(l, f), ",");
535 for(i = 0; i < MAX_SCORE; ++i)
536 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
540 out = strcat(out, GetScoreLogLabel(l, f), ",");
543 for(i = 0; i < MAX_SCORE; ++i)
544 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
545 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
549 out = strcat(out, GetScoreLogLabel(l, f), ",");
551 out = substring(out, 0, strlen(out) - 1);
553 else if((sk = pl.scorekeeper))
555 for(i = 0; i < MAX_SCORE; ++i)
556 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
557 out = strcat(out, ftos(sk.(scores[i])), ",");
559 for(i = 0; i < MAX_SCORE; ++i)
560 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
561 out = strcat(out, ftos(sk.(scores[i])), ",");
563 for(i = 0; i < MAX_SCORE; ++i)
564 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
565 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
566 out = strcat(out, ftos(sk.(scores[i])), ",");
567 out = substring(out, 0, strlen(out) - 1);
572 string GetTeamScoreString(float tm, float shortString)
583 for(i = 0; i < MAX_SCORE; ++i)
584 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
586 f = teamscores_flags[i];
587 l = teamscores_label[i];
588 out = strcat(out, GetScoreLogLabel(l, f), ",");
591 for(i = 0; i < MAX_SCORE; ++i)
592 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
594 f = teamscores_flags[i];
595 l = teamscores_label[i];
596 out = strcat(out, GetScoreLogLabel(l, f), ",");
599 for(i = 0; i < MAX_SCORE; ++i)
600 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
601 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
603 f = teamscores_flags[i];
604 l = teamscores_label[i];
605 out = strcat(out, GetScoreLogLabel(l, f), ",");
607 out = substring(out, 0, strlen(out) - 1);
609 else if((sk = teamscorekeepers[tm - 1]))
611 for(i = 0; i < MAX_TEAMSCORE; ++i)
612 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
613 out = strcat(out, ftos(sk.(teamscores[i])), ",");
615 for(i = 0; i < MAX_TEAMSCORE; ++i)
616 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
617 out = strcat(out, ftos(sk.(teamscores[i])), ",");
619 for(i = 0; i < MAX_TEAMSCORE; ++i)
620 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
621 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
622 out = strcat(out, ftos(sk.(teamscores[i])), ",");
623 out = substring(out, 0, strlen(out) - 1);
628 float PlayerTeamScore_Compare(entity p1, entity p2)
630 if(teamscores_entities_count)
631 if(p1.team != p2.team)
635 t1 = teamscorekeepers[p1.team - 1];
636 t2 = teamscorekeepers[p2.team - 1];
637 r = TeamScore_Compare(t1, t2);
638 if(r == 0) // ensure a deterministic order
639 r = p1.team - p2.team;
643 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
646 entity PlayerScore_Sort(.float field)
648 entity p, plist, pprev, pbest, pbestprev, pfirst, plast;
656 FOR_EACH_PLAYER(p) if(p.scorekeeper)
661 // Now plist points to the whole list.
663 pfirst = plast = world;
668 pprev = pbestprev = world;
670 for(p = plist; (pprev = p), (p = p.chain); )
672 if(PlayerTeamScore_Compare(p, pbest) > 0)
679 // remove pbest out of the chain
680 if(pbestprev == world)
683 pbestprev.chain = pbest.chain;
698 float TeamScore_GetCompareValue(float t)
703 if(t <= 0 || t >= 16)
707 error("Reading score of invalid team!");
710 sk = teamscorekeepers[t - 1];
713 s = sk.teamscores_primary;
714 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
717 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
723 #define SCORESWIDTH 58
724 // TODO put this somewhere in common?
725 string Score_NicePrint_ItemColor(float vflags)
727 if(vflags & SFL_SORT_PRIO_PRIMARY)
729 else if(vflags & SFL_SORT_PRIO_SECONDARY)
735 void Score_NicePrint_Team(entity to, float t, float w)
743 sk = teamscorekeepers[t - 1];
746 s = strcat(s, ColoredTeamName(t));
747 for(i = 0; i < MAX_TEAMSCORE; ++i)
748 if(teamscores_label[i] != "")
750 fl = teamscores_flags[i];
751 sc = sk.(teamscores[i]);
752 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), ScoreString(fl, sc));
758 s = strcat(s, strpad(max(0, NAMEWIDTH - strlennocol(s)), ""));
760 for(i = 0; i < MAX_SCORE; ++i)
761 if(scores_label[i] != "")
763 fl = scores_flags[i];
764 s2 = scores_label[i];
765 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, substring(s2, 0, w)));
771 void Score_NicePrint_Player(entity to, entity p, float w)
781 s = strcat(s, p.netname);
784 i = strlennocol(s) - NAMEWIDTH;
786 s = substring(s, 0, strlen(s) - i);
789 s = strcat(s, strpad(i, ""));
794 for(i = 0; i < MAX_SCORE; ++i)
795 if(scores_label[i] != "")
797 fl = scores_flags[i];
799 s = strcat(s, " ", Score_NicePrint_ItemColor(fl), strpad(-w, ScoreString(fl, sc)));
805 void Score_NicePrint_Spectators(entity to)
807 print_to(to, "Spectators:");
810 void Score_NicePrint_Spectator(entity to, entity p)
812 print_to(to, strcat(" ", p.netname));
815 .float score_dummyfield;
816 void Score_NicePrint(entity to)
823 for(i = 0; i < MAX_SCORE; ++i)
824 if(scores_label[i] != "")
826 w = bound(6, floor(SCORESWIDTH / t - 1), 9);
828 p = PlayerScore_Sort(score_dummyfield);
831 if(!teamscores_entities_count)
832 Score_NicePrint_Team(to, t, w);
835 if(teamscores_entities_count)
837 Score_NicePrint_Team(to, p.team, w);
838 Score_NicePrint_Player(to, p, w);
845 if(p.classname != "player")
848 Score_NicePrint_Spectators(to);
849 Score_NicePrint_Spectator(to, p);