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)
169 scores_initialized = 1;
171 TeamScore_Spawn(COLOR_TEAM1, "Red");
173 TeamScore_Spawn(COLOR_TEAM2, "Blue");
175 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
177 TeamScore_Spawn(COLOR_TEAM4, "Pink");
178 FOR_EACH_REALCLIENT(msg_entity) // cannot use MSG_ALL here, as that may come too early on level changes (that SUCKS)
179 ScoreInfo_Write(MSG_ONE);
183 * per-player score entities
186 float PlayerScore_SendEntity()
190 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
191 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
192 for(i = 0; i < MAX_SCORE; ++i)
193 WriteShort(MSG_ENTITY, self.scores[i]);
198 void PlayerScore_Clear(entity player)
203 if(teamscores_entities_count)
208 //print("clear clear clear... HAHA\n");
210 sk = player.scorekeeper;
211 for(i = 0; i < MAX_SCORE; ++i)
216 void Score_ClearAll()
220 FOR_EACH_CLIENTSLOT(p)
225 for(i = 0; i < MAX_SCORE; ++i)
229 for(i = 0; i < 16; ++i)
231 sk = teamscorekeepers[i];
234 for(i = 0; i < MAX_SCORE; ++i)
235 sk.(teamscores[i]) = 0;
240 void PlayerScore_Attach(entity player)
243 if(player.scorekeeper)
244 error("player already has a scorekeeper");
248 sk.SendEntity = PlayerScore_SendEntity;
250 player.scorekeeper = sk;
253 void PlayerScore_Detach(entity player)
255 if(!player.scorekeeper)
256 error("player has no scorekeeper");
257 remove(player.scorekeeper);
258 player.scorekeeper = world;
261 float PlayerScore_Add(entity player, float scorefield, float score)
264 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
265 s = player.scorekeeper;
267 error("Adding score to unknown player!");
270 return (s.(scores[scorefield]) += score);
273 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
276 r = PlayerScore_Add(player, pscorefield, score);
277 if(teamscores_entities_count) // only for teamplay
278 r = TeamScore_Add(player, tscorefield, score);
282 float PlayerScore_Compare(entity t1, entity t2)
284 if(!t1 || !t2) return (!t2) - !t1;
288 for(i = 0; i < MAX_SCORE; ++i)
292 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
297 void WinningConditionHelper()
303 s = strcat(s, ":", GetPlayerScoreString(world, 2)); // make this 1 once we can
305 if(teamscores_entities_count)
309 s = strcat(s, ":", GetTeamScoreString(0, 1));
310 for(t = 0; t < 16; ++t)
311 if(teamscorekeepers[t])
312 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
314 WinningConditionHelper_equality = 1;
315 WinningConditionHelper_winnerteam = 0;
316 for(t = 1; t < 16; ++t)
319 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
320 sk2 = teamscorekeepers[t];
321 c = TeamScore_Compare(sk1, sk2);
323 WinningConditionHelper_equality = 1;
326 WinningConditionHelper_equality = 0;
327 WinningConditionHelper_winnerteam = t;
331 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
332 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
333 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
334 if(teamscores_flags_primary & SFL_TIME)
335 WinningConditionHelper_topscore /= 10;
337 WinningConditionHelper_winner = world;
338 if(WinningConditionHelper_equality)
339 WinningConditionHelper_winnerteam = -1;
341 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
345 WinningConditionHelper_equality = 1;
346 WinningConditionHelper_winner = world;
349 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
351 WinningConditionHelper_equality = 1;
354 WinningConditionHelper_equality = 0;
355 WinningConditionHelper_winner = p;
359 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
360 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
361 if(scores_flags_primary & SFL_ZERO_IS_WORST)
362 if(WinningConditionHelper_topscore == 0)
364 if(WinningConditionHelper_lowerisbetter)
365 WinningConditionHelper_topscore = 999999999;
367 WinningConditionHelper_topscore = -999999999;
369 if(teamscores_flags_primary & SFL_TIME)
370 WinningConditionHelper_topscore /= 10;
372 if(WinningConditionHelper_equality)
373 WinningConditionHelper_winner = world;
374 WinningConditionHelper_winnerteam = -1;
378 strunzone(worldstatus);
379 worldstatus = strzone(s);
383 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
384 s = GetPlayerScoreString(p, 1);
385 if(clienttype(p) == CLIENTTYPE_REAL)
386 s = strcat(s, ":human");
388 s = strcat(s, ":bot");
389 if(p.classname == "player" || g_arena || g_lms)
390 s = strcat(s, ":", ftos(p.team));
392 s = strcat(s, ":spectator");
394 if(p.classname == "player" || g_arena || g_lms)
397 s = GetPlayerScoreString(p, 2);
400 strunzone(p.clientstatus);
401 p.clientstatus = strzone(s);
405 void Score_DebugPrint()
411 for(i = 0; i < MAX_SCORE; ++i)
412 print(":", scores_label[i]);
418 for(i = 0; i < MAX_SCORE; ++i)
419 print(":", ftos(sk.(scores[i])));
424 for(i = 0; i < MAX_TEAMSCORE; ++i)
425 print(":", teamscores_label[i]);
427 for(t = 0; t < 16; ++t)
429 sk = teamscorekeepers[t];
433 for(i = 0; i < MAX_TEAMSCORE; ++i)
434 print(":", ftos(sk.(teamscores[i])));
440 string GetScoreLogLabel(string label, float fl)
442 if(fl & SFL_LOWER_IS_BETTER)
443 label = strcat(label, "<");
444 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
445 label = strcat(label, "!!");
446 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
447 label = strcat(label, "!");
451 string GetPlayerScoreString(entity pl, float shortString)
462 for(i = 0; i < MAX_SCORE; ++i)
463 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
467 out = strcat(out, GetScoreLogLabel(l, f), ",");
470 for(i = 0; i < MAX_SCORE; ++i)
471 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
475 out = strcat(out, GetScoreLogLabel(l, f), ",");
478 for(i = 0; i < MAX_SCORE; ++i)
479 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
480 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
484 out = strcat(out, GetScoreLogLabel(l, f), ",");
486 out = substring(out, 0, strlen(out) - 1);
488 else if((sk = pl.scorekeeper))
490 for(i = 0; i < MAX_SCORE; ++i)
491 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
492 out = strcat(out, ftos(sk.(scores[i])), ",");
494 for(i = 0; i < MAX_SCORE; ++i)
495 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
496 out = strcat(out, ftos(sk.(scores[i])), ",");
498 for(i = 0; i < MAX_SCORE; ++i)
499 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
500 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
501 out = strcat(out, ftos(sk.(scores[i])), ",");
502 out = substring(out, 0, strlen(out) - 1);
507 string GetTeamScoreString(float tm, float shortString)
518 for(i = 0; i < MAX_SCORE; ++i)
519 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
521 f = teamscores_flags[i];
522 l = teamscores_label[i];
523 out = strcat(out, GetScoreLogLabel(l, f), ",");
526 for(i = 0; i < MAX_SCORE; ++i)
527 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
529 f = teamscores_flags[i];
530 l = teamscores_label[i];
531 out = strcat(out, GetScoreLogLabel(l, f), ",");
534 for(i = 0; i < MAX_SCORE; ++i)
535 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
536 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
538 f = teamscores_flags[i];
539 l = teamscores_label[i];
540 out = strcat(out, GetScoreLogLabel(l, f), ",");
542 out = substring(out, 0, strlen(out) - 1);
544 else if((sk = teamscorekeepers[tm - 1]))
546 for(i = 0; i < MAX_TEAMSCORE; ++i)
547 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
548 out = strcat(out, ftos(sk.(teamscores[i])), ",");
550 for(i = 0; i < MAX_TEAMSCORE; ++i)
551 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
552 out = strcat(out, ftos(sk.(teamscores[i])), ",");
554 for(i = 0; i < MAX_TEAMSCORE; ++i)
555 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
556 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
557 out = strcat(out, ftos(sk.(teamscores[i])), ",");
558 out = substring(out, 0, strlen(out) - 1);
563 float PlayerTeamScore_Compare(entity p1, entity p2)
565 if(teamscores_entities_count)
566 if(p1.team != p2.team)
569 t1 = teamscorekeepers[p1.team];
570 t2 = teamscorekeepers[p2.team];
571 return TeamScore_Compare(t1, t2);
574 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
577 float PlayerScore_Sort(.float field)
579 entity p, plist, pprev, pbest, pbestprev;
586 FOR_EACH_PLAYER(p) if(p.scorekeeper)
591 // Now plist points to the whole list.
596 pprev = pbestprev = world;
598 for(p = plist; (p = p.chain); )
600 if(PlayerTeamScore_Compare(p, pbest) > 0)
608 // remove pbest out of the chain
609 if(pbestprev == world)
612 pbestprev.chain = pbest.chain;