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 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
275 PlayerScore_Add(player, pscorefield, score);
276 if(teamscores_entities_count) // only for teamplay
277 TeamScore_Add(player, tscorefield, score);
280 float PlayerScore_Compare(entity t1, entity t2)
282 if(!t1 || !t2) return (!t2) - !t1;
286 for(i = 0; i < MAX_SCORE; ++i)
290 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
295 void WinningConditionHelper()
301 s = strcat(s, ":", GetPlayerScoreString(world, 2)); // make this 1 once we can
303 if(teamscores_entities_count)
307 s = strcat(s, ":", GetTeamScoreString(0, 1));
308 for(t = 0; t < 16; ++t)
309 if(teamscorekeepers[t])
310 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
312 WinningConditionHelper_equality = 1;
313 WinningConditionHelper_winnerteam = 0;
314 for(t = 1; t < 16; ++t)
317 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
318 sk2 = teamscorekeepers[t];
319 c = TeamScore_Compare(sk1, sk2);
321 WinningConditionHelper_equality = 1;
324 WinningConditionHelper_equality = 0;
325 WinningConditionHelper_winnerteam = t;
329 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
330 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
331 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
332 if(teamscores_flags_primary & SFL_TIME)
333 WinningConditionHelper_topscore /= 10;
335 WinningConditionHelper_winner = world;
336 if(WinningConditionHelper_equality)
337 WinningConditionHelper_winnerteam = -1;
339 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
343 WinningConditionHelper_equality = 1;
344 WinningConditionHelper_winner = world;
347 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
349 WinningConditionHelper_equality = 1;
352 WinningConditionHelper_equality = 0;
353 WinningConditionHelper_winner = p;
357 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
358 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
359 if(scores_flags_primary & SFL_ZERO_IS_WORST)
360 if(WinningConditionHelper_topscore == 0)
362 if(WinningConditionHelper_lowerisbetter)
363 WinningConditionHelper_topscore = 999999999;
365 WinningConditionHelper_topscore = -999999999;
367 if(teamscores_flags_primary & SFL_TIME)
368 WinningConditionHelper_topscore /= 10;
370 if(WinningConditionHelper_equality)
371 WinningConditionHelper_winner = world;
372 WinningConditionHelper_winnerteam = -1;
376 strunzone(worldstatus);
377 worldstatus = strzone(s);
381 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
382 s = GetPlayerScoreString(p, 1);
383 if(clienttype(p) == CLIENTTYPE_REAL)
384 s = strcat(s, ":human");
386 s = strcat(s, ":bot");
387 if(p.classname == "player" || g_arena || g_lms)
388 s = strcat(s, ":", ftos(p.team));
390 s = strcat(s, ":spectator");
392 if(p.classname == "player" || g_arena || g_lms)
395 s = GetPlayerScoreString(p, 2);
398 strunzone(p.clientstatus);
399 p.clientstatus = strzone(s);
403 void Score_DebugPrint()
409 for(i = 0; i < MAX_SCORE; ++i)
410 print(":", scores_label[i]);
416 for(i = 0; i < MAX_SCORE; ++i)
417 print(":", ftos(sk.(scores[i])));
422 for(i = 0; i < MAX_TEAMSCORE; ++i)
423 print(":", teamscores_label[i]);
425 for(t = 0; t < 16; ++t)
427 sk = teamscorekeepers[t];
431 for(i = 0; i < MAX_TEAMSCORE; ++i)
432 print(":", ftos(sk.(teamscores[i])));
438 string GetScoreLogLabel(string label, float fl)
440 if(fl & SFL_LOWER_IS_BETTER)
441 label = strcat(label, "<");
442 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
443 label = strcat(label, "!!");
444 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
445 label = strcat(label, "!");
449 string GetPlayerScoreString(entity pl, float shortString)
460 for(i = 0; i < MAX_SCORE; ++i)
461 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
465 out = strcat(out, GetScoreLogLabel(l, f), ",");
468 for(i = 0; i < MAX_SCORE; ++i)
469 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
473 out = strcat(out, GetScoreLogLabel(l, f), ",");
476 for(i = 0; i < MAX_SCORE; ++i)
477 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
478 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
482 out = strcat(out, GetScoreLogLabel(l, f), ",");
484 out = substring(out, 0, strlen(out) - 1);
486 else if((sk = pl.scorekeeper))
488 for(i = 0; i < MAX_SCORE; ++i)
489 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
490 out = strcat(out, ftos(sk.(scores[i])), ",");
492 for(i = 0; i < MAX_SCORE; ++i)
493 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
494 out = strcat(out, ftos(sk.(scores[i])), ",");
496 for(i = 0; i < MAX_SCORE; ++i)
497 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
498 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
499 out = strcat(out, ftos(sk.(scores[i])), ",");
500 out = substring(out, 0, strlen(out) - 1);
505 string GetTeamScoreString(float tm, float shortString)
516 for(i = 0; i < MAX_SCORE; ++i)
517 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
519 f = teamscores_flags[i];
520 l = teamscores_label[i];
521 out = strcat(out, GetScoreLogLabel(l, f), ",");
524 for(i = 0; i < MAX_SCORE; ++i)
525 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
527 f = teamscores_flags[i];
528 l = teamscores_label[i];
529 out = strcat(out, GetScoreLogLabel(l, f), ",");
532 for(i = 0; i < MAX_SCORE; ++i)
533 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
534 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
536 f = teamscores_flags[i];
537 l = teamscores_label[i];
538 out = strcat(out, GetScoreLogLabel(l, f), ",");
540 out = substring(out, 0, strlen(out) - 1);
542 else if((sk = teamscorekeepers[tm - 1]))
544 for(i = 0; i < MAX_TEAMSCORE; ++i)
545 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
546 out = strcat(out, ftos(sk.(teamscores[i])), ",");
548 for(i = 0; i < MAX_TEAMSCORE; ++i)
549 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
550 out = strcat(out, ftos(sk.(teamscores[i])), ",");
552 for(i = 0; i < MAX_TEAMSCORE; ++i)
553 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
554 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
555 out = strcat(out, ftos(sk.(teamscores[i])), ",");
556 out = substring(out, 0, strlen(out) - 1);
561 float PlayerTeamScore_Compare(entity p1, entity p2)
563 if(teamscores_entities_count)
564 if(p1.team != p2.team)
567 t1 = teamscorekeepers[p1.team];
568 t2 = teamscorekeepers[p2.team];
569 return TeamScore_Compare(t1, t2);
572 return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
575 float PlayerScore_Sort(.float field)
577 entity p, plist, pprev, pbest, pbestprev;
584 FOR_EACH_PLAYER(p) if(p.scorekeeper)
589 // Now plist points to the whole list.
594 pprev = pbestprev = world;
596 for(p = plist; (p = p.chain); )
598 if(PlayerTeamScore_Compare(p, pbest) > 0)
606 // remove pbest out of the chain
607 if(pbestprev == world)
610 pbestprev.chain = pbest.chain;