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 ScoreInfo_Write(MSG_ALL);
181 * per-player score entities
184 float PlayerScore_SendEntity()
188 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
189 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
190 for(i = 0; i < MAX_SCORE; ++i)
191 WriteShort(MSG_ENTITY, self.scores[i]);
196 void PlayerScore_Clear(entity player)
201 if(teamscores_entities_count)
205 if(g_race && !g_race_qualifying) return;
207 sk = player.scorekeeper;
208 for(i = 0; i < MAX_SCORE; ++i)
213 void Score_ClearAll()
217 FOR_EACH_CLIENTSLOT(p)
222 for(i = 0; i < MAX_SCORE; ++i)
226 for(i = 0; i < 16; ++i)
228 sk = teamscorekeepers[i];
231 for(i = 0; i < MAX_SCORE; ++i)
232 sk.(teamscores[i]) = 0;
237 void PlayerScore_Attach(entity player)
240 if(player.scorekeeper)
241 error("player already has a scorekeeper");
245 sk.SendEntity = PlayerScore_SendEntity;
247 player.scorekeeper = sk;
250 void PlayerScore_Detach(entity player)
252 if(!player.scorekeeper)
253 error("player has no scorekeeper");
254 remove(player.scorekeeper);
255 player.scorekeeper = world;
258 float PlayerScore_Add(entity player, float scorefield, float score)
261 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
262 s = player.scorekeeper;
264 error("Adding score to unknown player!");
267 return (s.(scores[scorefield]) += score);
270 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
273 r = PlayerScore_Add(player, pscorefield, score);
274 if(teamscores_entities_count) // only for teamplay
275 r = TeamScore_Add(player, tscorefield, score);
279 float PlayerScore_Compare(entity t1, entity t2)
281 if(!t1 || !t2) return (!t2) - !t1;
285 for(i = 0; i < MAX_SCORE; ++i)
289 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
294 void WinningConditionHelper()
300 s = strcat(s, ":", GetPlayerScoreString(world, 2)); // make this 1 once we can
302 if(teamscores_entities_count)
306 s = strcat(s, ":", GetTeamScoreString(0, 1));
307 for(t = 0; t < 16; ++t)
308 if(teamscorekeepers[t])
309 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
311 WinningConditionHelper_equality = 0;
312 WinningConditionHelper_winnerteam = 0;
313 for(t = 1; t < 16; ++t)
316 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
317 sk2 = teamscorekeepers[t];
318 c = TeamScore_Compare(sk1, sk2);
320 WinningConditionHelper_equality = 1;
323 WinningConditionHelper_equality = 0;
324 WinningConditionHelper_winnerteam = t;
328 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
329 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
330 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
331 if(teamscores_flags_primary & SFL_TIME)
332 WinningConditionHelper_topscore /= 10;
334 WinningConditionHelper_winner = world;
335 if(WinningConditionHelper_equality)
336 WinningConditionHelper_winnerteam = -1;
338 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
342 WinningConditionHelper_equality = 0;
343 WinningConditionHelper_winner = world;
346 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
348 WinningConditionHelper_equality = 1;
351 WinningConditionHelper_equality = 0;
352 WinningConditionHelper_winner = p;
356 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
357 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
358 if(scores_flags_primary & SFL_ZERO_IS_WORST)
359 if(WinningConditionHelper_topscore == 0)
361 if(WinningConditionHelper_lowerisbetter)
362 WinningConditionHelper_topscore = 999999999;
364 WinningConditionHelper_topscore = -999999999;
366 if(teamscores_flags_primary & SFL_TIME)
367 WinningConditionHelper_topscore /= 10;
369 if(WinningConditionHelper_equality)
370 WinningConditionHelper_winner = world;
371 WinningConditionHelper_winnerteam = -1;
375 strunzone(worldstatus);
376 worldstatus = strzone(s);
380 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
381 s = GetPlayerScoreString(p, 1);
382 if(clienttype(p) == CLIENTTYPE_REAL)
383 s = strcat(s, ":human");
385 s = strcat(s, ":bot");
386 if(p.classname == "player" || g_arena || g_lms)
387 s = strcat(s, ":", ftos(p.team));
389 s = strcat(s, ":spectator");
391 if(p.classname == "player" || g_arena || g_lms)
394 s = GetPlayerScoreString(p, 2);
397 strunzone(p.clientstatus);
398 p.clientstatus = strzone(s);
402 void Score_DebugPrint()
408 for(i = 0; i < MAX_SCORE; ++i)
409 print(":", scores_label[i]);
415 for(i = 0; i < MAX_SCORE; ++i)
416 print(":", ftos(sk.(scores[i])));
421 for(i = 0; i < MAX_TEAMSCORE; ++i)
422 print(":", teamscores_label[i]);
424 for(t = 0; t < 16; ++t)
426 sk = teamscorekeepers[t];
430 for(i = 0; i < MAX_TEAMSCORE; ++i)
431 print(":", ftos(sk.(teamscores[i])));
437 string GetScoreLogLabel(string label, float fl)
439 if(fl & SFL_LOWER_IS_BETTER)
440 label = strcat(label, "<");
441 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
442 label = strcat(label, "!!");
443 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
444 label = strcat(label, "!");
448 string GetPlayerScoreString(entity pl, float shortString)
459 for(i = 0; i < MAX_SCORE; ++i)
460 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
464 out = strcat(out, GetScoreLogLabel(l, f), ",");
467 for(i = 0; i < MAX_SCORE; ++i)
468 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
472 out = strcat(out, GetScoreLogLabel(l, f), ",");
475 for(i = 0; i < MAX_SCORE; ++i)
476 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
477 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
481 out = strcat(out, GetScoreLogLabel(l, f), ",");
483 out = substring(out, 0, strlen(out) - 1);
485 else if((sk = pl.scorekeeper))
487 for(i = 0; i < MAX_SCORE; ++i)
488 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
489 out = strcat(out, ftos(sk.(scores[i])), ",");
491 for(i = 0; i < MAX_SCORE; ++i)
492 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
493 out = strcat(out, ftos(sk.(scores[i])), ",");
495 for(i = 0; i < MAX_SCORE; ++i)
496 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
497 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
498 out = strcat(out, ftos(sk.(scores[i])), ",");
499 out = substring(out, 0, strlen(out) - 1);
504 string GetTeamScoreString(float tm, float shortString)
515 for(i = 0; i < MAX_SCORE; ++i)
516 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
518 f = teamscores_flags[i];
519 l = teamscores_label[i];
520 out = strcat(out, GetScoreLogLabel(l, f), ",");
523 for(i = 0; i < MAX_SCORE; ++i)
524 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
526 f = teamscores_flags[i];
527 l = teamscores_label[i];
528 out = strcat(out, GetScoreLogLabel(l, f), ",");
531 for(i = 0; i < MAX_SCORE; ++i)
532 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
533 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
535 f = teamscores_flags[i];
536 l = teamscores_label[i];
537 out = strcat(out, GetScoreLogLabel(l, f), ",");
539 out = substring(out, 0, strlen(out) - 1);
541 else if((sk = teamscorekeepers[tm - 1]))
543 for(i = 0; i < MAX_TEAMSCORE; ++i)
544 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
545 out = strcat(out, ftos(sk.(teamscores[i])), ",");
547 for(i = 0; i < MAX_TEAMSCORE; ++i)
548 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
549 out = strcat(out, ftos(sk.(teamscores[i])), ",");
551 for(i = 0; i < MAX_TEAMSCORE; ++i)
552 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
553 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
554 out = strcat(out, ftos(sk.(teamscores[i])), ",");
555 out = substring(out, 0, strlen(out) - 1);
560 float PlayerTeamScore_Compare(entity p1, entity p2)
562 if(teamscores_entities_count)
563 if(p1.team != p2.team)
566 t1 = teamscorekeepers[p1.team];
567 t2 = teamscorekeepers[p2.team];
568 return TeamScore_Compare(t1, t2);
571 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
574 float PlayerScore_Sort(.float field)
576 entity p, plist, pprev, pbest, pbestprev;
583 FOR_EACH_PLAYER(p) if(p.scorekeeper)
588 // Now plist points to the whole list.
593 pprev = pbestprev = world;
595 for(p = plist; (pprev = p), (p = p.chain); )
597 if(PlayerTeamScore_Compare(p, pbest) > 0)
604 // remove pbest out of the chain
605 if(pbestprev == world)
608 pbestprev.chain = pbest.chain;