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)
64 WriteByte(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
65 WriteByte(MSG_ENTITY, self.team - 1);
66 for(i = 0; i < MAX_TEAMSCORE; ++i)
67 WriteShort(MSG_ENTITY, self.teamscores[i]);
72 void TeamScore_Spawn(float t, string name)
76 ts.classname = "csqc_score_team";
77 ts.SendEntity = TeamScore_SendEntity;
78 ts.netname = name; // not used yet, FIXME
79 ts.Version = 1; // immediately send, so csqc knows about the team
82 teamscorekeepers[t - 1] = ts;
83 ++teamscores_entities_count;
86 float TeamScore_AddToTeam(float t, float scorefield, float score)
89 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
91 error("Adding score to invalid team!");
92 s = teamscorekeepers[t - 1];
94 error("Adding score to unknown team!");
97 return (s.(teamscores[scorefield]) += score);
100 float TeamScore_Add(entity player, float scorefield, float score)
102 return TeamScore_AddToTeam(player.team, scorefield, score);
105 float TeamScore_Compare(entity t1, entity t2)
107 if(!t1 || !t2) return (!t2) - !t1;
111 for(i = 0; i < MAX_TEAMSCORE; ++i)
115 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
121 * the scoreinfo entity
124 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
126 scores_label[i] = label;
127 scores_flags[i] = scoreflags;
128 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
130 scores_primary = scores[i];
131 scores_flags_primary = scoreflags;
135 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
137 teamscores_label[i] = label;
138 teamscores_flags[i] = scoreflags;
139 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
141 teamscores_primary = teamscores[i];
142 teamscores_flags_primary = scoreflags;
146 void ScoreInfo_Write(float targ)
149 if not(scores_initialized)
151 WriteByte(targ, SVC_TEMPENTITY);
152 WriteByte(targ, TE_CSQC_SCORESINFO);
153 WriteByte(targ, game);
154 for(i = 0; i < MAX_SCORE; ++i)
156 WriteString(targ, scores_label[i]);
157 WriteByte(targ, scores_flags[i]);
159 for(i = 0; i < MAX_TEAMSCORE; ++i)
161 WriteString(targ, teamscores_label[i]);
162 WriteByte(targ, teamscores_flags[i]);
166 void ScoreInfo_Init(float teams)
168 scores_initialized = 1;
170 TeamScore_Spawn(COLOR_TEAM1, "Red");
172 TeamScore_Spawn(COLOR_TEAM2, "Blue");
174 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
176 TeamScore_Spawn(COLOR_TEAM4, "Pink");
177 FOR_EACH_REALCLIENT(msg_entity) // cannot use MSG_ALL here, as that may come too early on level changes (that SUCKS)
178 ScoreInfo_Write(MSG_ONE);
182 * per-player score entities
185 float PlayerScore_SendEntity()
189 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
190 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
191 for(i = 0; i < MAX_SCORE; ++i)
192 WriteShort(MSG_ENTITY, self.scores[i]);
197 void PlayerScore_Clear(entity player)
202 if(teamscores_entities_count)
207 //print("clear clear clear... HAHA\n");
209 sk = player.scorekeeper;
210 for(i = 0; i < MAX_SCORE; ++i)
215 void Score_ClearAll()
219 FOR_EACH_CLIENTSLOT(p)
224 for(i = 0; i < MAX_SCORE; ++i)
228 for(i = 0; i < 16; ++i)
230 sk = teamscorekeepers[i];
233 for(i = 0; i < MAX_SCORE; ++i)
234 sk.(teamscores[i]) = 0;
239 void PlayerScore_Attach(entity player)
242 if(player.scorekeeper)
243 error("player already has a scorekeeper");
247 sk.SendEntity = PlayerScore_SendEntity;
249 player.scorekeeper = sk;
252 void PlayerScore_Detach(entity player)
254 if(!player.scorekeeper)
255 error("player has no scorekeeper");
256 remove(player.scorekeeper);
257 player.scorekeeper = world;
260 float PlayerScore_Add(entity player, float scorefield, float score)
263 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
264 s = player.scorekeeper;
266 error("Adding score to unknown player!");
269 return (s.(scores[scorefield]) += score);
272 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
275 r = PlayerScore_Add(player, pscorefield, score);
276 if(teamscores_entities_count) // only for teamplay
277 r = TeamScore_Add(player, tscorefield, score);
281 float PlayerScore_Compare(entity t1, entity t2)
283 if(!t1 || !t2) return (!t2) - !t1;
287 for(i = 0; i < MAX_SCORE; ++i)
291 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
296 void WinningConditionHelper()
302 s = strcat(s, ":", GetPlayerScoreString(world, 2)); // make this 1 once we can
304 if(teamscores_entities_count)
308 s = strcat(s, ":", GetTeamScoreString(0, 1));
309 for(t = 0; t < 16; ++t)
310 if(teamscorekeepers[t])
311 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
313 WinningConditionHelper_equality = 1;
314 WinningConditionHelper_winnerteam = 0;
315 for(t = 1; t < 16; ++t)
318 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
319 sk2 = teamscorekeepers[t];
320 c = TeamScore_Compare(sk1, sk2);
322 WinningConditionHelper_equality = 1;
325 WinningConditionHelper_equality = 0;
326 WinningConditionHelper_winnerteam = t;
330 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
331 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
332 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
333 if(teamscores_flags_primary & SFL_TIME)
334 WinningConditionHelper_topscore /= 10;
336 WinningConditionHelper_winner = world;
337 if(WinningConditionHelper_equality)
338 WinningConditionHelper_winnerteam = -1;
340 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
344 WinningConditionHelper_equality = 1;
345 WinningConditionHelper_winner = world;
348 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
350 WinningConditionHelper_equality = 1;
353 WinningConditionHelper_equality = 0;
354 WinningConditionHelper_winner = p;
358 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
359 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
360 if(scores_flags_primary & SFL_ZERO_IS_WORST)
361 if(WinningConditionHelper_topscore == 0)
363 if(WinningConditionHelper_lowerisbetter)
364 WinningConditionHelper_topscore = 999999999;
366 WinningConditionHelper_topscore = -999999999;
368 if(teamscores_flags_primary & SFL_TIME)
369 WinningConditionHelper_topscore /= 10;
371 if(WinningConditionHelper_equality)
372 WinningConditionHelper_winner = world;
373 WinningConditionHelper_winnerteam = -1;
377 strunzone(worldstatus);
378 worldstatus = strzone(s);
382 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
383 s = GetPlayerScoreString(p, 1);
384 if(clienttype(p) == CLIENTTYPE_REAL)
385 s = strcat(s, ":human");
387 s = strcat(s, ":bot");
388 if(p.classname == "player" || g_arena || g_lms)
389 s = strcat(s, ":", ftos(p.team));
391 s = strcat(s, ":spectator");
393 if(p.classname == "player" || g_arena || g_lms)
396 s = GetPlayerScoreString(p, 2);
399 strunzone(p.clientstatus);
400 p.clientstatus = strzone(s);
404 void Score_DebugPrint()
410 for(i = 0; i < MAX_SCORE; ++i)
411 print(":", scores_label[i]);
417 for(i = 0; i < MAX_SCORE; ++i)
418 print(":", ftos(sk.(scores[i])));
423 for(i = 0; i < MAX_TEAMSCORE; ++i)
424 print(":", teamscores_label[i]);
426 for(t = 0; t < 16; ++t)
428 sk = teamscorekeepers[t];
432 for(i = 0; i < MAX_TEAMSCORE; ++i)
433 print(":", ftos(sk.(teamscores[i])));
439 string GetScoreLogLabel(string label, float fl)
441 if(fl & SFL_LOWER_IS_BETTER)
442 label = strcat(label, "<");
443 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
444 label = strcat(label, "!!");
445 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
446 label = strcat(label, "!");
450 string GetPlayerScoreString(entity pl, float shortString)
461 for(i = 0; i < MAX_SCORE; ++i)
462 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
466 out = strcat(out, GetScoreLogLabel(l, f), ",");
469 for(i = 0; i < MAX_SCORE; ++i)
470 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
474 out = strcat(out, GetScoreLogLabel(l, f), ",");
477 for(i = 0; i < MAX_SCORE; ++i)
478 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
479 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
483 out = strcat(out, GetScoreLogLabel(l, f), ",");
485 out = substring(out, 0, strlen(out) - 1);
487 else if((sk = pl.scorekeeper))
489 for(i = 0; i < MAX_SCORE; ++i)
490 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
491 out = strcat(out, ftos(sk.(scores[i])), ",");
493 for(i = 0; i < MAX_SCORE; ++i)
494 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
495 out = strcat(out, ftos(sk.(scores[i])), ",");
497 for(i = 0; i < MAX_SCORE; ++i)
498 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
499 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
500 out = strcat(out, ftos(sk.(scores[i])), ",");
501 out = substring(out, 0, strlen(out) - 1);
506 string GetTeamScoreString(float tm, float shortString)
517 for(i = 0; i < MAX_SCORE; ++i)
518 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
520 f = teamscores_flags[i];
521 l = teamscores_label[i];
522 out = strcat(out, GetScoreLogLabel(l, f), ",");
525 for(i = 0; i < MAX_SCORE; ++i)
526 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
528 f = teamscores_flags[i];
529 l = teamscores_label[i];
530 out = strcat(out, GetScoreLogLabel(l, f), ",");
533 for(i = 0; i < MAX_SCORE; ++i)
534 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
535 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
537 f = teamscores_flags[i];
538 l = teamscores_label[i];
539 out = strcat(out, GetScoreLogLabel(l, f), ",");
541 out = substring(out, 0, strlen(out) - 1);
543 else if((sk = teamscorekeepers[tm - 1]))
545 for(i = 0; i < MAX_TEAMSCORE; ++i)
546 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
547 out = strcat(out, ftos(sk.(teamscores[i])), ",");
549 for(i = 0; i < MAX_TEAMSCORE; ++i)
550 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
551 out = strcat(out, ftos(sk.(teamscores[i])), ",");
553 for(i = 0; i < MAX_TEAMSCORE; ++i)
554 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
555 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
556 out = strcat(out, ftos(sk.(teamscores[i])), ",");
557 out = substring(out, 0, strlen(out) - 1);
562 float PlayerTeamScore_Compare(entity p1, entity p2)
564 if(teamscores_entities_count)
565 if(p1.team != p2.team)
568 t1 = teamscorekeepers[p1.team];
569 t2 = teamscorekeepers[p2.team];
570 return TeamScore_Compare(t1, t2);
573 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
576 float PlayerScore_Sort(.float field)
578 entity p, plist, pprev, pbest, pbestprev;
585 FOR_EACH_PLAYER(p) if(p.scorekeeper)
590 // Now plist points to the whole list.
595 pprev = pbestprev = world;
597 for(p = plist; (p = p.chain); )
599 if(PlayerTeamScore_Compare(p, pbest) > 0)
607 // remove pbest out of the chain
608 if(pbestprev == world)
611 pbestprev.chain = pbest.chain;