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, ":", GetPlayerScoreString(world, 2)); // make this 1 once we can
339 if(teamscores_entities_count)
343 s = strcat(s, ":", GetTeamScoreString(0, 1));
344 for(t = 0; t < 16; ++t)
345 if(teamscorekeepers[t])
346 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
348 WinningConditionHelper_equality = 0;
349 WinningConditionHelper_winnerteam = 0;
350 for(t = 1; t < 16; ++t)
353 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
354 sk2 = teamscorekeepers[t];
355 c = TeamScore_Compare(sk1, sk2);
357 WinningConditionHelper_equality = 1;
360 WinningConditionHelper_equality = 0;
361 WinningConditionHelper_winnerteam = t;
365 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
366 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
367 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
368 if(teamscores_flags_primary & SFL_TIME)
369 WinningConditionHelper_topscore /= 10;
371 WinningConditionHelper_winner = world;
372 if(WinningConditionHelper_equality)
373 WinningConditionHelper_winnerteam = -1;
375 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
379 WinningConditionHelper_equality = 0;
380 WinningConditionHelper_winner = world;
383 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
385 WinningConditionHelper_equality = 1;
388 WinningConditionHelper_equality = 0;
389 WinningConditionHelper_winner = p;
393 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
394 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
395 if(scores_flags_primary & SFL_ZERO_IS_WORST)
396 if(WinningConditionHelper_topscore == 0)
398 if(WinningConditionHelper_lowerisbetter)
399 WinningConditionHelper_topscore = 999999999;
401 WinningConditionHelper_topscore = -999999999;
403 if(teamscores_flags_primary & SFL_TIME)
404 WinningConditionHelper_topscore /= 10;
406 if(WinningConditionHelper_equality)
407 WinningConditionHelper_winner = world;
408 WinningConditionHelper_winnerteam = -1;
412 strunzone(worldstatus);
413 worldstatus = strzone(s);
417 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
418 s = GetPlayerScoreString(p, 1);
419 if(clienttype(p) == CLIENTTYPE_REAL)
420 s = strcat(s, ":human");
422 s = strcat(s, ":bot");
423 if(p.classname == "player" || g_arena || g_lms)
424 s = strcat(s, ":", ftos(p.team));
426 s = strcat(s, ":spectator");
428 if(p.classname == "player" || g_arena || g_lms)
431 s = GetPlayerScoreString(p, 2);
434 strunzone(p.clientstatus);
435 p.clientstatus = strzone(s);
439 void Score_DebugPrint()
445 for(i = 0; i < MAX_SCORE; ++i)
446 print(":", scores_label[i]);
452 for(i = 0; i < MAX_SCORE; ++i)
453 print(":", ftos(sk.(scores[i])));
458 for(i = 0; i < MAX_TEAMSCORE; ++i)
459 print(":", teamscores_label[i]);
461 for(t = 0; t < 16; ++t)
463 sk = teamscorekeepers[t];
467 for(i = 0; i < MAX_TEAMSCORE; ++i)
468 print(":", ftos(sk.(teamscores[i])));
474 string GetScoreLogLabel(string label, float fl)
476 if(fl & SFL_LOWER_IS_BETTER)
477 label = strcat(label, "<");
478 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
479 label = strcat(label, "!!");
480 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
481 label = strcat(label, "!");
485 string GetPlayerScoreString(entity pl, float shortString)
496 for(i = 0; i < MAX_SCORE; ++i)
497 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
501 out = strcat(out, GetScoreLogLabel(l, f), ",");
504 for(i = 0; i < MAX_SCORE; ++i)
505 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
509 out = strcat(out, GetScoreLogLabel(l, f), ",");
512 for(i = 0; i < MAX_SCORE; ++i)
513 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
514 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
518 out = strcat(out, GetScoreLogLabel(l, f), ",");
520 out = substring(out, 0, strlen(out) - 1);
522 else if((sk = pl.scorekeeper))
524 for(i = 0; i < MAX_SCORE; ++i)
525 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
526 out = strcat(out, ftos(sk.(scores[i])), ",");
528 for(i = 0; i < MAX_SCORE; ++i)
529 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
530 out = strcat(out, ftos(sk.(scores[i])), ",");
532 for(i = 0; i < MAX_SCORE; ++i)
533 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
534 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
535 out = strcat(out, ftos(sk.(scores[i])), ",");
536 out = substring(out, 0, strlen(out) - 1);
541 string GetTeamScoreString(float tm, float shortString)
552 for(i = 0; i < MAX_SCORE; ++i)
553 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
555 f = teamscores_flags[i];
556 l = teamscores_label[i];
557 out = strcat(out, GetScoreLogLabel(l, f), ",");
560 for(i = 0; i < MAX_SCORE; ++i)
561 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
563 f = teamscores_flags[i];
564 l = teamscores_label[i];
565 out = strcat(out, GetScoreLogLabel(l, f), ",");
568 for(i = 0; i < MAX_SCORE; ++i)
569 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
570 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
572 f = teamscores_flags[i];
573 l = teamscores_label[i];
574 out = strcat(out, GetScoreLogLabel(l, f), ",");
576 out = substring(out, 0, strlen(out) - 1);
578 else if((sk = teamscorekeepers[tm - 1]))
580 for(i = 0; i < MAX_TEAMSCORE; ++i)
581 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
582 out = strcat(out, ftos(sk.(teamscores[i])), ",");
584 for(i = 0; i < MAX_TEAMSCORE; ++i)
585 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
586 out = strcat(out, ftos(sk.(teamscores[i])), ",");
588 for(i = 0; i < MAX_TEAMSCORE; ++i)
589 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
590 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
591 out = strcat(out, ftos(sk.(teamscores[i])), ",");
592 out = substring(out, 0, strlen(out) - 1);
597 float PlayerTeamScore_Compare(entity p1, entity p2)
599 if(teamscores_entities_count)
600 if(p1.team != p2.team)
603 t1 = teamscorekeepers[p1.team];
604 t2 = teamscorekeepers[p2.team];
605 return TeamScore_Compare(t1, t2);
608 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
611 float PlayerScore_Sort(.float field)
613 entity p, plist, pprev, pbest, pbestprev;
620 FOR_EACH_PLAYER(p) if(p.scorekeeper)
625 // Now plist points to the whole list.
630 pprev = pbestprev = world;
632 for(p = plist; (pprev = p), (p = p.chain); )
634 if(PlayerTeamScore_Compare(p, pbest) > 0)
641 // remove pbest out of the chain
642 if(pbestprev == world)
645 pbestprev.chain = pbest.chain;
654 float TeamScore_GetCompareValue(float t)
659 if(t <= 0 || t >= 16)
660 error("Reading score of invalid team!");
662 sk = teamscorekeepers[t - 1];
665 s = sk.teamscores_primary;
666 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
669 if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)