]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/scores.qc
make CSQC scores safer against packet loss/rearrangements
[divverent/nexuiz.git] / data / qcsrc / server / scores.qc
1 .float scores[MAX_SCORE];
2 .float teamscores[MAX_TEAMSCORE];
3
4 .entity scorekeeper;
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
14 void Net_LinkEntity(entity e)
15 {
16         e.model = "net_entity";
17         e.modelindex = 1;
18         e.effects = EF_NODEPTHTEST | EF_LOWPRECISION;
19 }
20
21 vector ScoreField_Compare(entity t1, entity t2, .float field, float fieldflags, vector previous) // returns: cmp value, best prio
22 {
23         if(!(fieldflags & SFL_SORT_PRIO_MASK)) // column does not sort
24                 return previous;
25         if(fieldflags & SFL_SORT_PRIO_MASK < previous_y)
26                 return previous;
27         if(t1.field == t2.field)
28                 return previous;
29
30         previous_y = fieldflags & SFL_SORT_PRIO_MASK;
31
32         if(fieldflags & SFL_LOWER_IS_BETTER)
33                 previous_x = (t2.field - t1.field);
34         else
35                 previous_x = (t1.field - t2.field);
36
37         return previous;
38 }
39
40 /*
41  * teamscore entities
42  */
43
44 float TeamScore_SendEntity(entity to)
45 {
46         float i;
47
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]);
52
53         return TRUE;
54 }
55
56 void TeamScore_Spawn(float t, string name)
57 {
58         entity ts;
59         ts = spawn();
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
64         ts.team = t;
65         Net_LinkEntity(ts);
66         teamscorekeepers[t - 1] = ts;
67         ++teamscores_entities_count;
68 }
69
70 float TeamScore_AddToTeam(float t, float scorefield, float score)
71 {
72         entity s;
73         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
74         if(t <= 0 || t >= 16)
75                 error("Adding score to invalid team!");
76         s = teamscorekeepers[t - 1];
77         if(!s)
78                 error("Adding score to unknown team!");
79         if(score)
80                 s.Version += 1;
81         return (s.(teamscores[scorefield]) += score);
82 }
83
84 float TeamScore_Add(entity player, float scorefield, float score)
85 {
86         return TeamScore_AddToTeam(player.team, scorefield, score);
87 }
88
89 float TeamScore_Compare(entity t1, entity t2)
90 {
91         if(!t1 || !t2) return (!t2) - !t1;
92
93         vector result;
94         float i;
95         for(i = 0; i < MAX_TEAMSCORE; ++i)
96         {
97                 var .float f;
98                 f = teamscores[i];
99                 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
100         }
101         return result_x;
102 }
103
104 /*
105  * the scoreinfo entity
106  */
107
108 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
109 {
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];
114 }
115
116 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
117 {
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];
122 }
123
124 void ScoreInfo_Write(float targ)
125 {
126         float i;
127         WriteByte(targ, SVC_TEMPENTITY);
128         WriteByte(targ, TE_CSQC_SCORESINFO);
129         WriteByte(targ, game);
130         for(i = 0; i < MAX_SCORE; ++i)
131         {
132                 WriteString(targ, scores_label[i]);
133                 WriteByte(targ, scores_flags[i]);
134         }
135         for(i = 0; i < MAX_TEAMSCORE; ++i)
136         {
137                 WriteString(targ, teamscores_label[i]);
138                 WriteByte(targ, teamscores_flags[i]);
139         }
140 }
141
142 void ScoreInfo_Init(float teams)
143 {
144         scores_initialized = 1;
145         if(teams >= 1)
146                 TeamScore_Spawn(COLOR_TEAM1, "Red");
147         if(teams >= 2)
148                 TeamScore_Spawn(COLOR_TEAM2, "Blue");
149         if(teams >= 3)
150                 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
151         if(teams >= 4)
152                 TeamScore_Spawn(COLOR_TEAM4, "Pink");
153         ScoreInfo_Write(MSG_ALL);
154 }
155
156 /*
157  * per-player score entities
158  */
159
160 float PlayerScore_SendEntity()
161 {
162         float i;
163
164         WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
165         WriteByte(MSG_ENTITY, num_for_edict(self.owner));
166         for(i = 0; i < MAX_SCORE; ++i)
167                 WriteShort(MSG_ENTITY, self.scores[i]);
168
169         return TRUE;
170 }
171
172 void PlayerScore_Clear(entity player)
173 {
174         entity sk;
175         float i;
176
177         if(teamscores_entities_count)
178                 return;
179         if(g_lms)
180                 return;
181         if(g_arena)
182                 return;
183         //print("clear clear clear... HAHA\n");
184
185         sk = player.scorekeeper;
186         for(i = 0; i < MAX_SCORE; ++i)
187                 sk.(scores[i]) = 0;
188         sk.Version += 1;
189 }
190
191 void Score_ClearAll()
192 {
193         entity p, sk;
194         float i;
195         FOR_EACH_CLIENTSLOT(p)
196         {
197                 sk = p.scorekeeper;
198                 if(!sk)
199                         continue;
200                 for(i = 0; i < MAX_SCORE; ++i)
201                         sk.(scores[i]) = 0;
202                 sk.Version += 1;
203         }
204         for(i = 0; i < 16; ++i)
205         {
206                 sk = teamscorekeepers[i];
207                 if(!sk)
208                         continue;
209                 for(i = 0; i < MAX_SCORE; ++i)
210                         sk.(teamscores[i]) = 0;
211                 sk.Version += 1;
212         }
213 }
214
215 void PlayerScore_Attach(entity player)
216 {
217         entity sk;
218         if(player.scorekeeper)
219                 error("player already has a scorekeeper");
220         sk = spawn();
221         sk.owner = player;
222         sk.Version = 1;
223         sk.SendEntity = PlayerScore_SendEntity;
224         Net_LinkEntity(sk);
225         player.scorekeeper = sk;
226 }
227
228 void PlayerScore_Detach(entity player)
229 {
230         if(!player.scorekeeper)
231                 error("player has no scorekeeper");
232         remove(player.scorekeeper);
233         player.scorekeeper = world;
234 }
235
236 float PlayerScore_Add(entity player, float scorefield, float score)
237 {
238         entity s;
239         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
240         s = player.scorekeeper;
241         if(!s)
242                 error("Adding score to unknown player!");
243         if(score)
244                 s.Version += 1;
245         return (s.(scores[scorefield]) += score);
246 }
247
248 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
249 {
250         PlayerScore_Add(player, pscorefield, score);
251         if(teamscores_entities_count) // only for teamplay
252                 TeamScore_Add(player, tscorefield, score);
253 }
254
255 float PlayerScore_Compare(entity t1, entity t2)
256 {
257         if(!t1 || !t2) return (!t2) - !t1;
258
259         vector result;
260         float i;
261         for(i = 0; i < MAX_SCORE; ++i)
262         {
263                 var .float f;
264                 f = scores[i];
265                 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
266         }
267         return result_x;
268 }
269
270 void WinningConditionHelper()
271 {
272         float c;
273         if(teamscores_entities_count)
274         {
275                 float t;
276                 WinningConditionHelper_equality = 1;
277                 WinningConditionHelper_winnerteam = 0;
278                 for(t = 1; t < 16; ++t)
279                 {
280                         entity sk1, sk2;
281                         sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
282                         sk2 = teamscorekeepers[t];
283                         c = TeamScore_Compare(sk1, sk2);
284                         if(c == 0)
285                                 WinningConditionHelper_equality = 1;
286                         else if(c < 0)
287                         {
288                                 WinningConditionHelper_equality = 0;
289                                 WinningConditionHelper_winnerteam = t;
290                         }
291                 }
292
293                 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
294
295                 WinningConditionHelper_winner = world;
296                 if(WinningConditionHelper_equality)
297                         WinningConditionHelper_winnerteam = -1;
298                 else
299                         ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
300         }
301         else
302         {
303                 entity p;
304                 WinningConditionHelper_equality = 1;
305                 WinningConditionHelper_winner = world;
306                 FOR_EACH_PLAYER(p)
307                 {
308                         c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
309                         if(c == 0)
310                                 WinningConditionHelper_equality = 1;
311                         else if(c < 0)
312                         {
313                                 WinningConditionHelper_equality = 0;
314                                 WinningConditionHelper_winner = p;
315                         }
316                 }
317
318                 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
319
320                 if(WinningConditionHelper_equality)
321                         WinningConditionHelper_winner = world;
322                 WinningConditionHelper_winnerteam = -1;
323         }
324 }
325
326 void Score_DebugPrint()
327 {
328         entity p, sk;
329         float i, t;
330
331         print("netname");
332         for(i = 0; i < MAX_SCORE; ++i)
333                 print(":", scores_label[i]);
334         print("\n");
335         FOR_EACH_PLAYER(p)
336         {
337                 sk = p.scorekeeper;
338                 print(p.netname);
339                 for(i = 0; i < MAX_SCORE; ++i)
340                         print(":", ftos(sk.(scores[i])));
341                 print("\n");
342         }
343
344         print("teamname");
345         for(i = 0; i < MAX_TEAMSCORE; ++i)
346                 print(":", teamscores_label[i]);
347         print("\n");
348         for(t = 0; t < 16; ++t)
349         {
350                 sk = teamscorekeepers[t];
351                 if(sk)
352                 {
353                         print(ftos(t));
354                         for(i = 0; i < MAX_TEAMSCORE; ++i)
355                                 print(":", ftos(sk.(teamscores[i])));
356                         print("\n");
357                 }
358         }
359 }
360
361 string GetScoreLogLabel(string label, float fl)
362 {
363         if(fl & SFL_LOWER_IS_BETTER)
364                 label = strcat(label, "<");
365         if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
366                 label = strcat(label, "!!");
367         else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
368                 label = strcat(label, "!");
369         return label;
370 }
371
372 string GetPlayerScoreString(entity pl)
373 {
374         string out;
375         entity sk;
376         float i, f;
377         string l;
378
379         out = "";
380         if(!pl)
381         {
382                 // label
383                 for(i = 0; i < MAX_SCORE; ++i)
384                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
385                         {
386                                 f = scores_flags[i];
387                                 l = scores_label[i];
388                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
389                         }
390                 for(i = 0; i < MAX_SCORE; ++i)
391                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
392                         {
393                                 f = scores_flags[i];
394                                 l = scores_label[i];
395                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
396                         }
397                 for(i = 0; i < MAX_SCORE; ++i)
398                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
399                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
400                         {
401                                 f = scores_flags[i];
402                                 l = scores_label[i];
403                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
404                         }
405                 out = substring(out, 0, strlen(out) - 1);
406         }
407         else if((sk = pl.scorekeeper))
408         {
409                 for(i = 0; i < MAX_SCORE; ++i)
410                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
411                                 out = strcat(out, ftos(sk.(scores[i])), ",");
412                 for(i = 0; i < MAX_SCORE; ++i)
413                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
414                                 out = strcat(out, ftos(sk.(scores[i])), ",");
415                 for(i = 0; i < MAX_SCORE; ++i)
416                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
417                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
418                                 out = strcat(out, ftos(sk.(scores[i])), ",");
419                 out = substring(out, 0, strlen(out) - 1);
420         }
421         return out;
422 }
423
424 string GetTeamScoreString(float tm)
425 {
426         string out;
427         entity sk;
428         float i, f;
429         string l;
430
431         out = "";
432         if(tm == 0)
433         {
434                 // label
435                 for(i = 0; i < MAX_SCORE; ++i)
436                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
437                         {
438                                 f = teamscores_flags[i];
439                                 l = teamscores_label[i];
440                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
441                         }
442                 for(i = 0; i < MAX_SCORE; ++i)
443                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
444                         {
445                                 f = teamscores_flags[i];
446                                 l = teamscores_label[i];
447                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
448                         }
449                 for(i = 0; i < MAX_SCORE; ++i)
450                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
451                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
452                         {
453                                 f = teamscores_flags[i];
454                                 l = teamscores_label[i];
455                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
456                         }
457                 out = substring(out, 0, strlen(out) - 1);
458         }
459         else if((sk = teamscorekeepers[tm - 1]))
460         {
461                 for(i = 0; i < MAX_TEAMSCORE; ++i)
462                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
463                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
464                 for(i = 0; i < MAX_TEAMSCORE; ++i)
465                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
466                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
467                 for(i = 0; i < MAX_TEAMSCORE; ++i)
468                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
469                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
470                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
471                 out = substring(out, 0, strlen(out) - 1);
472         }
473         return out;
474 }