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()
277 if(teamscores_entities_count)
280 WinningConditionHelper_equality = 1;
281 WinningConditionHelper_winnerteam = 0;
282 for(t = 1; t < 16; ++t)
285 sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
286 sk2 = teamscorekeepers[t];
287 c = TeamScore_Compare(sk1, sk2);
289 WinningConditionHelper_equality = 1;
292 WinningConditionHelper_equality = 0;
293 WinningConditionHelper_winnerteam = t;
297 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
299 WinningConditionHelper_winner = world;
300 if(WinningConditionHelper_equality)
301 WinningConditionHelper_winnerteam = -1;
303 ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
308 WinningConditionHelper_equality = 1;
309 WinningConditionHelper_winner = world;
312 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
314 WinningConditionHelper_equality = 1;
317 WinningConditionHelper_equality = 0;
318 WinningConditionHelper_winner = p;
322 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
324 if(WinningConditionHelper_equality)
325 WinningConditionHelper_winner = world;
326 WinningConditionHelper_winnerteam = -1;
330 void Score_DebugPrint()
336 for(i = 0; i < MAX_SCORE; ++i)
337 print(":", scores_label[i]);
343 for(i = 0; i < MAX_SCORE; ++i)
344 print(":", ftos(sk.(scores[i])));
349 for(i = 0; i < MAX_TEAMSCORE; ++i)
350 print(":", teamscores_label[i]);
352 for(t = 0; t < 16; ++t)
354 sk = teamscorekeepers[t];
358 for(i = 0; i < MAX_TEAMSCORE; ++i)
359 print(":", ftos(sk.(teamscores[i])));
365 string GetScoreLogLabel(string label, float fl)
367 if(fl & SFL_LOWER_IS_BETTER)
368 label = strcat(label, "<");
369 if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
370 label = strcat(label, "!!");
371 else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
372 label = strcat(label, "!");
376 string GetPlayerScoreString(entity pl)
387 for(i = 0; i < MAX_SCORE; ++i)
388 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
392 out = strcat(out, GetScoreLogLabel(l, f), ",");
394 for(i = 0; i < MAX_SCORE; ++i)
395 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
399 out = strcat(out, GetScoreLogLabel(l, f), ",");
401 for(i = 0; i < MAX_SCORE; ++i)
402 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
403 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
407 out = strcat(out, GetScoreLogLabel(l, f), ",");
409 out = substring(out, 0, strlen(out) - 1);
411 else if((sk = pl.scorekeeper))
413 for(i = 0; i < MAX_SCORE; ++i)
414 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
415 out = strcat(out, ftos(sk.(scores[i])), ",");
416 for(i = 0; i < MAX_SCORE; ++i)
417 if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
418 out = strcat(out, ftos(sk.(scores[i])), ",");
419 for(i = 0; i < MAX_SCORE; ++i)
420 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
421 if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
422 out = strcat(out, ftos(sk.(scores[i])), ",");
423 out = substring(out, 0, strlen(out) - 1);
428 string GetTeamScoreString(float tm)
439 for(i = 0; i < MAX_SCORE; ++i)
440 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
442 f = teamscores_flags[i];
443 l = teamscores_label[i];
444 out = strcat(out, GetScoreLogLabel(l, f), ",");
446 for(i = 0; i < MAX_SCORE; ++i)
447 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
449 f = teamscores_flags[i];
450 l = teamscores_label[i];
451 out = strcat(out, GetScoreLogLabel(l, f), ",");
453 for(i = 0; i < MAX_SCORE; ++i)
454 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
455 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
457 f = teamscores_flags[i];
458 l = teamscores_label[i];
459 out = strcat(out, GetScoreLogLabel(l, f), ",");
461 out = substring(out, 0, strlen(out) - 1);
463 else if((sk = teamscorekeepers[tm - 1]))
465 for(i = 0; i < MAX_TEAMSCORE; ++i)
466 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
467 out = strcat(out, ftos(sk.(teamscores[i])), ",");
468 for(i = 0; i < MAX_TEAMSCORE; ++i)
469 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
470 out = strcat(out, ftos(sk.(teamscores[i])), ",");
471 for(i = 0; i < MAX_TEAMSCORE; ++i)
472 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
473 if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
474 out = strcat(out, ftos(sk.(teamscores[i])), ",");
475 out = substring(out, 0, strlen(out) - 1);