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;
14 void Net_LinkEntity(entity e)
16 e.model = "net_entity";
18 e.effects = EF_NODEPTHTEST | EF_LOWPRECISION;
21 vector ScoreField_Compare(entity t1, entity t2, .float field, float fieldflags, vector previous) // returns: cmp value, best prio
23 if(!(fieldflags & SFL_SORT_PRIO_MASK)) // column does not sort
25 if(fieldflags & SFL_SORT_PRIO_MASK < previous_y)
27 if(t1.field == t2.field)
30 previous_y = fieldflags & SFL_SORT_PRIO_MASK;
32 if(fieldflags & SFL_ZERO_IS_WORST)
39 else if(t2.field == 0)
46 if(fieldflags & SFL_LOWER_IS_BETTER)
47 previous_x = (t2.field - t1.field);
49 previous_x = (t1.field - t2.field);
58 float TeamScore_SendEntity(entity to)
62 WriteByte(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
63 WriteByte(MSG_ENTITY, self.team - 1);
64 for(i = 0; i < MAX_TEAMSCORE; ++i)
65 WriteShort(MSG_ENTITY, self.teamscores[i]);
70 void TeamScore_Spawn(float t, string name)
74 ts.classname = "csqc_score_team";
75 ts.SendEntity = TeamScore_SendEntity;
76 ts.netname = name; // not used yet, FIXME
77 ts.Version = 1; // immediately send, so csqc knows about the team
80 teamscorekeepers[t - 1] = ts;
81 ++teamscores_entities_count;
84 float TeamScore_AddToTeam(float t, float scorefield, float score)
87 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
89 error("Adding score to invalid team!");
90 s = teamscorekeepers[t - 1];
92 error("Adding score to unknown team!");
95 return (s.(teamscores[scorefield]) += score);
98 float TeamScore_Add(entity player, float scorefield, float score)
100 return TeamScore_AddToTeam(player.team, scorefield, score);
103 float TeamScore_Compare(entity t1, entity t2)
105 if(!t1 || !t2) return (!t2) - !t1;
109 for(i = 0; i < MAX_TEAMSCORE; ++i)
113 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
119 * the scoreinfo entity
122 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
124 scores_label[i] = label;
125 scores_flags[i] = scoreflags;
126 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
127 scores_primary = scores[i];
130 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
132 teamscores_label[i] = label;
133 teamscores_flags[i] = scoreflags;
134 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
135 teamscores_primary = teamscores[i];
138 void ScoreInfo_Write(float targ)
141 if not(scores_initialized)
143 WriteByte(targ, SVC_TEMPENTITY);
144 WriteByte(targ, TE_CSQC_SCORESINFO);
145 WriteByte(targ, game);
146 for(i = 0; i < MAX_SCORE; ++i)
148 WriteString(targ, scores_label[i]);
149 WriteByte(targ, scores_flags[i]);
151 for(i = 0; i < MAX_TEAMSCORE; ++i)
153 WriteString(targ, teamscores_label[i]);
154 WriteByte(targ, teamscores_flags[i]);
158 void ScoreInfo_Init(float teams)
161 scores_initialized = 1;
163 TeamScore_Spawn(COLOR_TEAM1, "Red");
165 TeamScore_Spawn(COLOR_TEAM2, "Blue");
167 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
169 TeamScore_Spawn(COLOR_TEAM4, "Pink");
170 FOR_EACH_REALCLIENT(msg_entity) // cannot use MSG_ALL here, as that may come too early on level changes (that SUCKS)
171 ScoreInfo_Write(MSG_ONE);
175 * per-player score entities
178 float PlayerScore_SendEntity()
182 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
183 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
184 for(i = 0; i < MAX_SCORE; ++i)
185 WriteShort(MSG_ENTITY, self.scores[i]);
190 void PlayerScore_Clear(entity player)
195 if(teamscores_entities_count)
201 //print("clear clear clear... HAHA\n");
203 sk = player.scorekeeper;
204 for(i = 0; i < MAX_SCORE; ++i)
209 void Score_ClearAll()
213 FOR_EACH_CLIENTSLOT(p)
218 for(i = 0; i < MAX_SCORE; ++i)
222 for(i = 0; i < 16; ++i)
224 sk = teamscorekeepers[i];
227 for(i = 0; i < MAX_SCORE; ++i)
228 sk.(teamscores[i]) = 0;
233 void PlayerScore_Attach(entity player)
236 if(player.scorekeeper)
237 error("player already has a scorekeeper");
241 sk.SendEntity = PlayerScore_SendEntity;
243 player.scorekeeper = sk;
246 void PlayerScore_Detach(entity player)
248 if(!player.scorekeeper)
249 error("player has no scorekeeper");
250 remove(player.scorekeeper);
251 player.scorekeeper = world;
254 float PlayerScore_Add(entity player, float scorefield, float score)
257 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
258 s = player.scorekeeper;
260 error("Adding score to unknown player!");
263 return (s.(scores[scorefield]) += score);
266 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
268 PlayerScore_Add(player, pscorefield, score);
269 if(teamscores_entities_count) // only for teamplay
270 TeamScore_Add(player, tscorefield, score);
273 float PlayerScore_Compare(entity t1, entity t2)
275 if(!t1 || !t2) return (!t2) - !t1;
279 for(i = 0; i < MAX_SCORE; ++i)
283 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
288 void WinningConditionHelper()
294 s = strcat(s, ":", GetPlayerScoreString(world, 2)); // make this 1 once we can
296 if(teamscores_entities_count)
300 s = strcat(s, ":", GetTeamScoreString(0, 1));
301 for(t = 0; t < 16; ++t)
302 if(teamscorekeepers[t])
303 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
305 WinningConditionHelper_equality = 1;
306 WinningConditionHelper_winnerteam = 0;
307 for(t = 1; t < 16; ++t)
310 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
311 sk2 = teamscorekeepers[t];
312 c = TeamScore_Compare(sk1, sk2);
314 WinningConditionHelper_equality = 1;
317 WinningConditionHelper_equality = 0;
318 WinningConditionHelper_winnerteam = t;
322 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
324 WinningConditionHelper_winner = world;
325 if(WinningConditionHelper_equality)
326 WinningConditionHelper_winnerteam = -1;
328 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
332 WinningConditionHelper_equality = 1;
333 WinningConditionHelper_winner = world;
336 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
338 WinningConditionHelper_equality = 1;
341 WinningConditionHelper_equality = 0;
342 WinningConditionHelper_winner = p;
346 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
348 if(WinningConditionHelper_equality)
349 WinningConditionHelper_winner = world;
350 WinningConditionHelper_winnerteam = -1;
354 strunzone(worldstatus);
355 worldstatus = strzone(s);
359 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
360 s = GetPlayerScoreString(p, 1);
361 if(clienttype(p) == CLIENTTYPE_REAL)
362 s = strcat(s, ":human");
364 s = strcat(s, ":bot");
365 if(p.classname == "player" || g_arena || g_lms)
366 s = strcat(s, ":", ftos(p.team));
368 s = strcat(s, ":spectator");
370 if(p.classname == "player" || g_arena || g_lms)
373 s = GetPlayerScoreString(p, 2);
376 strunzone(p.clientstatus);
377 p.clientstatus = strzone(s);
381 void Score_DebugPrint()
387 for(i = 0; i < MAX_SCORE; ++i)
388 print(":", scores_label[i]);
394 for(i = 0; i < MAX_SCORE; ++i)
395 print(":", ftos(sk.(scores[i])));
400 for(i = 0; i < MAX_TEAMSCORE; ++i)
401 print(":", teamscores_label[i]);
403 for(t = 0; t < 16; ++t)
405 sk = teamscorekeepers[t];
409 for(i = 0; i < MAX_TEAMSCORE; ++i)
410 print(":", ftos(sk.(teamscores[i])));
416 string GetScoreLogLabel(string label, float fl)
418 if(fl & SFL_LOWER_IS_BETTER)
419 label = strcat(label, "<");
420 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
421 label = strcat(label, "!!");
422 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
423 label = strcat(label, "!");
427 string GetPlayerScoreString(entity pl, float shortString)
438 for(i = 0; i < MAX_SCORE; ++i)
439 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
443 out = strcat(out, GetScoreLogLabel(l, f), ",");
446 for(i = 0; i < MAX_SCORE; ++i)
447 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
451 out = strcat(out, GetScoreLogLabel(l, f), ",");
454 for(i = 0; i < MAX_SCORE; ++i)
455 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
456 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
460 out = strcat(out, GetScoreLogLabel(l, f), ",");
462 out = substring(out, 0, strlen(out) - 1);
464 else if((sk = pl.scorekeeper))
466 for(i = 0; i < MAX_SCORE; ++i)
467 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
468 out = strcat(out, ftos(sk.(scores[i])), ",");
470 for(i = 0; i < MAX_SCORE; ++i)
471 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
472 out = strcat(out, ftos(sk.(scores[i])), ",");
474 for(i = 0; i < MAX_SCORE; ++i)
475 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
476 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
477 out = strcat(out, ftos(sk.(scores[i])), ",");
478 out = substring(out, 0, strlen(out) - 1);
483 string GetTeamScoreString(float tm, float shortString)
494 for(i = 0; i < MAX_SCORE; ++i)
495 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
497 f = teamscores_flags[i];
498 l = teamscores_label[i];
499 out = strcat(out, GetScoreLogLabel(l, f), ",");
502 for(i = 0; i < MAX_SCORE; ++i)
503 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
505 f = teamscores_flags[i];
506 l = teamscores_label[i];
507 out = strcat(out, GetScoreLogLabel(l, f), ",");
510 for(i = 0; i < MAX_SCORE; ++i)
511 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
512 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
514 f = teamscores_flags[i];
515 l = teamscores_label[i];
516 out = strcat(out, GetScoreLogLabel(l, f), ",");
518 out = substring(out, 0, strlen(out) - 1);
520 else if((sk = teamscorekeepers[tm - 1]))
522 for(i = 0; i < MAX_TEAMSCORE; ++i)
523 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
524 out = strcat(out, ftos(sk.(teamscores[i])), ",");
526 for(i = 0; i < MAX_TEAMSCORE; ++i)
527 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
528 out = strcat(out, ftos(sk.(teamscores[i])), ",");
530 for(i = 0; i < MAX_TEAMSCORE; ++i)
531 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
532 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
533 out = strcat(out, ftos(sk.(teamscores[i])), ",");
534 out = substring(out, 0, strlen(out) - 1);