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_LOWER_IS_BETTER)
33 previous_x = (t2.field - t1.field);
35 previous_x = (t1.field - t2.field);
44 float TeamScore_SendEntity(entity to)
48 WriteByte(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
49 WriteByte(MSG_ENTITY, self.team - 1);
50 for(i = 0; i < MAX_TEAMSCORE; ++i)
51 WriteShort(MSG_ENTITY, self.teamscores[i]);
56 void TeamScore_Spawn(float t, string name)
60 ts.classname = "csqc_score_team";
61 ts.SendEntity = TeamScore_SendEntity;
62 ts.netname = name; // not used yet, FIXME
63 ts.Version = 1; // immediately send, so csqc knows about the team
66 teamscorekeepers[t - 1] = ts;
67 ++teamscores_entities_count;
70 float TeamScore_AddToTeam(float t, float scorefield, float score)
73 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
75 error("Adding score to invalid team!");
76 s = teamscorekeepers[t - 1];
78 error("Adding score to unknown team!");
81 return (s.(teamscores[scorefield]) += score);
84 float TeamScore_Add(entity player, float scorefield, float score)
86 return TeamScore_AddToTeam(player.team, scorefield, score);
89 float TeamScore_Compare(entity t1, entity t2)
91 if(!t1 || !t2) return (!t2) - !t1;
95 for(i = 0; i < MAX_TEAMSCORE; ++i)
99 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
105 * the scoreinfo entity
108 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
110 scores_label[i] = label;
111 scores_flags[i] = scoreflags;
112 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
113 scores_primary = scores[i];
116 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
118 teamscores_label[i] = label;
119 teamscores_flags[i] = scoreflags;
120 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
121 teamscores_primary = teamscores[i];
124 void ScoreInfo_Write(float targ)
127 if not(scores_initialized)
129 WriteByte(targ, SVC_TEMPENTITY);
130 WriteByte(targ, TE_CSQC_SCORESINFO);
131 WriteByte(targ, game);
132 for(i = 0; i < MAX_SCORE; ++i)
134 WriteString(targ, scores_label[i]);
135 WriteByte(targ, scores_flags[i]);
137 for(i = 0; i < MAX_TEAMSCORE; ++i)
139 WriteString(targ, teamscores_label[i]);
140 WriteByte(targ, teamscores_flags[i]);
144 void ScoreInfo_Init(float teams)
147 scores_initialized = 1;
149 TeamScore_Spawn(COLOR_TEAM1, "Red");
151 TeamScore_Spawn(COLOR_TEAM2, "Blue");
153 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
155 TeamScore_Spawn(COLOR_TEAM4, "Pink");
156 FOR_EACH_REALCLIENT(msg_entity) // cannot use MSG_ALL here, as that may come too early on level changes (that SUCKS)
157 ScoreInfo_Write(MSG_ONE);
161 * per-player score entities
164 float PlayerScore_SendEntity()
168 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
169 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
170 for(i = 0; i < MAX_SCORE; ++i)
171 WriteShort(MSG_ENTITY, self.scores[i]);
176 void PlayerScore_Clear(entity player)
181 if(teamscores_entities_count)
187 //print("clear clear clear... HAHA\n");
189 sk = player.scorekeeper;
190 for(i = 0; i < MAX_SCORE; ++i)
195 void Score_ClearAll()
199 FOR_EACH_CLIENTSLOT(p)
204 for(i = 0; i < MAX_SCORE; ++i)
208 for(i = 0; i < 16; ++i)
210 sk = teamscorekeepers[i];
213 for(i = 0; i < MAX_SCORE; ++i)
214 sk.(teamscores[i]) = 0;
219 void PlayerScore_Attach(entity player)
222 if(player.scorekeeper)
223 error("player already has a scorekeeper");
227 sk.SendEntity = PlayerScore_SendEntity;
229 player.scorekeeper = sk;
232 void PlayerScore_Detach(entity player)
234 if(!player.scorekeeper)
235 error("player has no scorekeeper");
236 remove(player.scorekeeper);
237 player.scorekeeper = world;
240 float PlayerScore_Add(entity player, float scorefield, float score)
243 if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
244 s = player.scorekeeper;
246 error("Adding score to unknown player!");
249 return (s.(scores[scorefield]) += score);
252 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
254 PlayerScore_Add(player, pscorefield, score);
255 if(teamscores_entities_count) // only for teamplay
256 TeamScore_Add(player, tscorefield, score);
259 float PlayerScore_Compare(entity t1, entity t2)
261 if(!t1 || !t2) return (!t2) - !t1;
265 for(i = 0; i < MAX_SCORE; ++i)
269 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
274 void WinningConditionHelper()
280 s = strcat(s, ":", GetPlayerScoreString(world, 2)); // make this 1 once we can
282 if(teamscores_entities_count)
286 s = strcat(s, ":", GetTeamScoreString(0, 1));
287 for(t = 0; t < 16; ++t)
288 if(teamscorekeepers[t])
289 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
291 WinningConditionHelper_equality = 1;
292 WinningConditionHelper_winnerteam = 0;
293 for(t = 1; t < 16; ++t)
296 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
297 sk2 = teamscorekeepers[t];
298 c = TeamScore_Compare(sk1, sk2);
300 WinningConditionHelper_equality = 1;
303 WinningConditionHelper_equality = 0;
304 WinningConditionHelper_winnerteam = t;
308 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
310 WinningConditionHelper_winner = world;
311 if(WinningConditionHelper_equality)
312 WinningConditionHelper_winnerteam = -1;
314 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
318 WinningConditionHelper_equality = 1;
319 WinningConditionHelper_winner = world;
322 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
324 WinningConditionHelper_equality = 1;
327 WinningConditionHelper_equality = 0;
328 WinningConditionHelper_winner = p;
332 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
334 if(WinningConditionHelper_equality)
335 WinningConditionHelper_winner = world;
336 WinningConditionHelper_winnerteam = -1;
340 strunzone(worldstatus);
341 worldstatus = strzone(s);
345 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
346 s = GetPlayerScoreString(p, 1);
347 if(clienttype(p) == CLIENTTYPE_REAL)
348 s = strcat(s, ":human");
350 s = strcat(s, ":bot");
351 if(p.classname == "player" || g_arena || g_lms)
352 s = strcat(s, ":", ftos(p.team));
354 s = strcat(s, ":spectator");
356 if(p.classname == "player" || g_arena || g_lms)
359 s = GetPlayerScoreString(p, 2);
362 strunzone(p.clientstatus);
363 p.clientstatus = strzone(s);
367 void Score_DebugPrint()
373 for(i = 0; i < MAX_SCORE; ++i)
374 print(":", scores_label[i]);
380 for(i = 0; i < MAX_SCORE; ++i)
381 print(":", ftos(sk.(scores[i])));
386 for(i = 0; i < MAX_TEAMSCORE; ++i)
387 print(":", teamscores_label[i]);
389 for(t = 0; t < 16; ++t)
391 sk = teamscorekeepers[t];
395 for(i = 0; i < MAX_TEAMSCORE; ++i)
396 print(":", ftos(sk.(teamscores[i])));
402 string GetScoreLogLabel(string label, float fl)
404 if(fl & SFL_LOWER_IS_BETTER)
405 label = strcat(label, "<");
406 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
407 label = strcat(label, "!!");
408 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
409 label = strcat(label, "!");
413 string GetPlayerScoreString(entity pl, float shortString)
424 for(i = 0; i < MAX_SCORE; ++i)
425 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
429 out = strcat(out, GetScoreLogLabel(l, f), ",");
432 for(i = 0; i < MAX_SCORE; ++i)
433 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
437 out = strcat(out, GetScoreLogLabel(l, f), ",");
440 for(i = 0; i < MAX_SCORE; ++i)
441 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
442 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
446 out = strcat(out, GetScoreLogLabel(l, f), ",");
448 out = substring(out, 0, strlen(out) - 1);
450 else if((sk = pl.scorekeeper))
452 for(i = 0; i < MAX_SCORE; ++i)
453 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
454 out = strcat(out, ftos(sk.(scores[i])), ",");
456 for(i = 0; i < MAX_SCORE; ++i)
457 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
458 out = strcat(out, ftos(sk.(scores[i])), ",");
460 for(i = 0; i < MAX_SCORE; ++i)
461 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
462 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
463 out = strcat(out, ftos(sk.(scores[i])), ",");
464 out = substring(out, 0, strlen(out) - 1);
469 string GetTeamScoreString(float tm, float shortString)
480 for(i = 0; i < MAX_SCORE; ++i)
481 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
483 f = teamscores_flags[i];
484 l = teamscores_label[i];
485 out = strcat(out, GetScoreLogLabel(l, f), ",");
488 for(i = 0; i < MAX_SCORE; ++i)
489 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
491 f = teamscores_flags[i];
492 l = teamscores_label[i];
493 out = strcat(out, GetScoreLogLabel(l, f), ",");
496 for(i = 0; i < MAX_SCORE; ++i)
497 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
498 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
500 f = teamscores_flags[i];
501 l = teamscores_label[i];
502 out = strcat(out, GetScoreLogLabel(l, f), ",");
504 out = substring(out, 0, strlen(out) - 1);
506 else if((sk = teamscorekeepers[tm - 1]))
508 for(i = 0; i < MAX_TEAMSCORE; ++i)
509 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
510 out = strcat(out, ftos(sk.(teamscores[i])), ",");
512 for(i = 0; i < MAX_TEAMSCORE; ++i)
513 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
514 out = strcat(out, ftos(sk.(teamscores[i])), ",");
516 for(i = 0; i < MAX_TEAMSCORE; ++i)
517 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
518 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
519 out = strcat(out, ftos(sk.(teamscores[i])), ",");
520 out = substring(out, 0, strlen(out) - 1);