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)
209 //print("clear clear clear... HAHA\n");
211 sk = player.scorekeeper;
212 for(i = 0; i < MAX_SCORE; ++i)
217 void Score_ClearAll()
221 FOR_EACH_CLIENTSLOT(p)
226 for(i = 0; i < MAX_SCORE; ++i)
230 for(i = 0; i < 16; ++i)
232 sk = teamscorekeepers[i];
235 for(i = 0; i < MAX_SCORE; ++i)
236 sk.(teamscores[i]) = 0;
241 void PlayerScore_Attach(entity player)
244 if(player.scorekeeper)
245 error("player already has a scorekeeper");
249 sk.SendEntity = PlayerScore_SendEntity;
251 player.scorekeeper = sk;
254 void PlayerScore_Detach(entity player)
256 if(!player.scorekeeper)
257 error("player has no scorekeeper");
258 remove(player.scorekeeper);
259 player.scorekeeper = world;
262 float PlayerScore_Add(entity player, float scorefield, float score)
265 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
266 s = player.scorekeeper;
268 error("Adding score to unknown player!");
271 return (s.(scores[scorefield]) += score);
274 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
276 PlayerScore_Add(player, pscorefield, score);
277 if(teamscores_entities_count) // only for teamplay
278 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);
333 WinningConditionHelper_winner = world;
334 if(WinningConditionHelper_equality)
335 WinningConditionHelper_winnerteam = -1;
337 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
341 WinningConditionHelper_equality = 1;
342 WinningConditionHelper_winner = world;
345 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
347 WinningConditionHelper_equality = 1;
350 WinningConditionHelper_equality = 0;
351 WinningConditionHelper_winner = p;
355 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
356 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
358 if(WinningConditionHelper_equality)
359 WinningConditionHelper_winner = world;
360 WinningConditionHelper_winnerteam = -1;
364 strunzone(worldstatus);
365 worldstatus = strzone(s);
369 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
370 s = GetPlayerScoreString(p, 1);
371 if(clienttype(p) == CLIENTTYPE_REAL)
372 s = strcat(s, ":human");
374 s = strcat(s, ":bot");
375 if(p.classname == "player" || g_arena || g_lms)
376 s = strcat(s, ":", ftos(p.team));
378 s = strcat(s, ":spectator");
380 if(p.classname == "player" || g_arena || g_lms)
383 s = GetPlayerScoreString(p, 2);
386 strunzone(p.clientstatus);
387 p.clientstatus = strzone(s);
391 void Score_DebugPrint()
397 for(i = 0; i < MAX_SCORE; ++i)
398 print(":", scores_label[i]);
404 for(i = 0; i < MAX_SCORE; ++i)
405 print(":", ftos(sk.(scores[i])));
410 for(i = 0; i < MAX_TEAMSCORE; ++i)
411 print(":", teamscores_label[i]);
413 for(t = 0; t < 16; ++t)
415 sk = teamscorekeepers[t];
419 for(i = 0; i < MAX_TEAMSCORE; ++i)
420 print(":", ftos(sk.(teamscores[i])));
426 string GetScoreLogLabel(string label, float fl)
428 if(fl & SFL_LOWER_IS_BETTER)
429 label = strcat(label, "<");
430 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
431 label = strcat(label, "!!");
432 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
433 label = strcat(label, "!");
437 string GetPlayerScoreString(entity pl, float shortString)
448 for(i = 0; i < MAX_SCORE; ++i)
449 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
453 out = strcat(out, GetScoreLogLabel(l, f), ",");
456 for(i = 0; i < MAX_SCORE; ++i)
457 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
461 out = strcat(out, GetScoreLogLabel(l, f), ",");
464 for(i = 0; i < MAX_SCORE; ++i)
465 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
466 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
470 out = strcat(out, GetScoreLogLabel(l, f), ",");
472 out = substring(out, 0, strlen(out) - 1);
474 else if((sk = pl.scorekeeper))
476 for(i = 0; i < MAX_SCORE; ++i)
477 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
478 out = strcat(out, ftos(sk.(scores[i])), ",");
480 for(i = 0; i < MAX_SCORE; ++i)
481 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
482 out = strcat(out, ftos(sk.(scores[i])), ",");
484 for(i = 0; i < MAX_SCORE; ++i)
485 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
486 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
487 out = strcat(out, ftos(sk.(scores[i])), ",");
488 out = substring(out, 0, strlen(out) - 1);
493 string GetTeamScoreString(float tm, float shortString)
504 for(i = 0; i < MAX_SCORE; ++i)
505 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
507 f = teamscores_flags[i];
508 l = teamscores_label[i];
509 out = strcat(out, GetScoreLogLabel(l, f), ",");
512 for(i = 0; i < MAX_SCORE; ++i)
513 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
515 f = teamscores_flags[i];
516 l = teamscores_label[i];
517 out = strcat(out, GetScoreLogLabel(l, f), ",");
520 for(i = 0; i < MAX_SCORE; ++i)
521 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
522 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
524 f = teamscores_flags[i];
525 l = teamscores_label[i];
526 out = strcat(out, GetScoreLogLabel(l, f), ",");
528 out = substring(out, 0, strlen(out) - 1);
530 else if((sk = teamscorekeepers[tm - 1]))
532 for(i = 0; i < MAX_TEAMSCORE; ++i)
533 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
534 out = strcat(out, ftos(sk.(teamscores[i])), ",");
536 for(i = 0; i < MAX_TEAMSCORE; ++i)
537 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
538 out = strcat(out, ftos(sk.(teamscores[i])), ",");
540 for(i = 0; i < MAX_TEAMSCORE; ++i)
541 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
542 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
543 out = strcat(out, ftos(sk.(teamscores[i])), ",");
544 out = substring(out, 0, strlen(out) - 1);