]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/scores.qc
can't use MSG_ALL here, as that may come too early on level changes (that SUCKS)
[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         if not(scores_initialized)
128                 return;
129         WriteByte(targ, SVC_TEMPENTITY);
130         WriteByte(targ, TE_CSQC_SCORESINFO);
131         WriteByte(targ, game);
132         for(i = 0; i < MAX_SCORE; ++i)
133         {
134                 WriteString(targ, scores_label[i]);
135                 WriteByte(targ, scores_flags[i]);
136         }
137         for(i = 0; i < MAX_TEAMSCORE; ++i)
138         {
139                 WriteString(targ, teamscores_label[i]);
140                 WriteByte(targ, teamscores_flags[i]);
141         }
142 }
143
144 void ScoreInfo_Init(float teams)
145 {
146         entity pl;
147         scores_initialized = 1;
148         if(teams >= 1)
149                 TeamScore_Spawn(COLOR_TEAM1, "Red");
150         if(teams >= 2)
151                 TeamScore_Spawn(COLOR_TEAM2, "Blue");
152         if(teams >= 3)
153                 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
154         if(teams >= 4)
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);
158 }
159
160 /*
161  * per-player score entities
162  */
163
164 float PlayerScore_SendEntity()
165 {
166         float i;
167
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]);
172
173         return TRUE;
174 }
175
176 void PlayerScore_Clear(entity player)
177 {
178         entity sk;
179         float i;
180
181         if(teamscores_entities_count)
182                 return;
183         if(g_lms)
184                 return;
185         if(g_arena)
186                 return;
187         //print("clear clear clear... HAHA\n");
188
189         sk = player.scorekeeper;
190         for(i = 0; i < MAX_SCORE; ++i)
191                 sk.(scores[i]) = 0;
192         sk.Version += 1;
193 }
194
195 void Score_ClearAll()
196 {
197         entity p, sk;
198         float i;
199         FOR_EACH_CLIENTSLOT(p)
200         {
201                 sk = p.scorekeeper;
202                 if(!sk)
203                         continue;
204                 for(i = 0; i < MAX_SCORE; ++i)
205                         sk.(scores[i]) = 0;
206                 sk.Version += 1;
207         }
208         for(i = 0; i < 16; ++i)
209         {
210                 sk = teamscorekeepers[i];
211                 if(!sk)
212                         continue;
213                 for(i = 0; i < MAX_SCORE; ++i)
214                         sk.(teamscores[i]) = 0;
215                 sk.Version += 1;
216         }
217 }
218
219 void PlayerScore_Attach(entity player)
220 {
221         entity sk;
222         if(player.scorekeeper)
223                 error("player already has a scorekeeper");
224         sk = spawn();
225         sk.owner = player;
226         sk.Version = 1;
227         sk.SendEntity = PlayerScore_SendEntity;
228         Net_LinkEntity(sk);
229         player.scorekeeper = sk;
230 }
231
232 void PlayerScore_Detach(entity player)
233 {
234         if(!player.scorekeeper)
235                 error("player has no scorekeeper");
236         remove(player.scorekeeper);
237         player.scorekeeper = world;
238 }
239
240 float PlayerScore_Add(entity player, float scorefield, float score)
241 {
242         entity s;
243         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
244         s = player.scorekeeper;
245         if(!s)
246                 error("Adding score to unknown player!");
247         if(score)
248                 s.Version += 1;
249         return (s.(scores[scorefield]) += score);
250 }
251
252 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
253 {
254         PlayerScore_Add(player, pscorefield, score);
255         if(teamscores_entities_count) // only for teamplay
256                 TeamScore_Add(player, tscorefield, score);
257 }
258
259 float PlayerScore_Compare(entity t1, entity t2)
260 {
261         if(!t1 || !t2) return (!t2) - !t1;
262
263         vector result;
264         float i;
265         for(i = 0; i < MAX_SCORE; ++i)
266         {
267                 var .float f;
268                 f = scores[i];
269                 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
270         }
271         return result_x;
272 }
273
274 void WinningConditionHelper()
275 {
276         float c;
277         if(teamscores_entities_count)
278         {
279                 float t;
280                 WinningConditionHelper_equality = 1;
281                 WinningConditionHelper_winnerteam = 0;
282                 for(t = 1; t < 16; ++t)
283                 {
284                         entity sk1, sk2;
285                         sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
286                         sk2 = teamscorekeepers[t];
287                         c = TeamScore_Compare(sk1, sk2);
288                         if(c == 0)
289                                 WinningConditionHelper_equality = 1;
290                         else if(c < 0)
291                         {
292                                 WinningConditionHelper_equality = 0;
293                                 WinningConditionHelper_winnerteam = t;
294                         }
295                 }
296
297                 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
298
299                 WinningConditionHelper_winner = world;
300                 if(WinningConditionHelper_equality)
301                         WinningConditionHelper_winnerteam = -1;
302                 else
303                         ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
304         }
305         else
306         {
307                 entity p;
308                 WinningConditionHelper_equality = 1;
309                 WinningConditionHelper_winner = world;
310                 FOR_EACH_PLAYER(p)
311                 {
312                         c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
313                         if(c == 0)
314                                 WinningConditionHelper_equality = 1;
315                         else if(c < 0)
316                         {
317                                 WinningConditionHelper_equality = 0;
318                                 WinningConditionHelper_winner = p;
319                         }
320                 }
321
322                 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
323
324                 if(WinningConditionHelper_equality)
325                         WinningConditionHelper_winner = world;
326                 WinningConditionHelper_winnerteam = -1;
327         }
328 }
329
330 void Score_DebugPrint()
331 {
332         entity p, sk;
333         float i, t;
334
335         print("netname");
336         for(i = 0; i < MAX_SCORE; ++i)
337                 print(":", scores_label[i]);
338         print("\n");
339         FOR_EACH_PLAYER(p)
340         {
341                 sk = p.scorekeeper;
342                 print(p.netname);
343                 for(i = 0; i < MAX_SCORE; ++i)
344                         print(":", ftos(sk.(scores[i])));
345                 print("\n");
346         }
347
348         print("teamname");
349         for(i = 0; i < MAX_TEAMSCORE; ++i)
350                 print(":", teamscores_label[i]);
351         print("\n");
352         for(t = 0; t < 16; ++t)
353         {
354                 sk = teamscorekeepers[t];
355                 if(sk)
356                 {
357                         print(ftos(t));
358                         for(i = 0; i < MAX_TEAMSCORE; ++i)
359                                 print(":", ftos(sk.(teamscores[i])));
360                         print("\n");
361                 }
362         }
363 }
364
365 string GetScoreLogLabel(string label, float fl)
366 {
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, "!");
373         return label;
374 }
375
376 string GetPlayerScoreString(entity pl)
377 {
378         string out;
379         entity sk;
380         float i, f;
381         string l;
382
383         out = "";
384         if(!pl)
385         {
386                 // label
387                 for(i = 0; i < MAX_SCORE; ++i)
388                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
389                         {
390                                 f = scores_flags[i];
391                                 l = scores_label[i];
392                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
393                         }
394                 for(i = 0; i < MAX_SCORE; ++i)
395                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
396                         {
397                                 f = scores_flags[i];
398                                 l = scores_label[i];
399                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
400                         }
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)
404                         {
405                                 f = scores_flags[i];
406                                 l = scores_label[i];
407                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
408                         }
409                 out = substring(out, 0, strlen(out) - 1);
410         }
411         else if((sk = pl.scorekeeper))
412         {
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);
424         }
425         return out;
426 }
427
428 string GetTeamScoreString(float tm)
429 {
430         string out;
431         entity sk;
432         float i, f;
433         string l;
434
435         out = "";
436         if(tm == 0)
437         {
438                 // label
439                 for(i = 0; i < MAX_SCORE; ++i)
440                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
441                         {
442                                 f = teamscores_flags[i];
443                                 l = teamscores_label[i];
444                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
445                         }
446                 for(i = 0; i < MAX_SCORE; ++i)
447                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
448                         {
449                                 f = teamscores_flags[i];
450                                 l = teamscores_label[i];
451                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
452                         }
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)
456                         {
457                                 f = teamscores_flags[i];
458                                 l = teamscores_label[i];
459                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
460                         }
461                 out = substring(out, 0, strlen(out) - 1);
462         }
463         else if((sk = teamscorekeepers[tm - 1]))
464         {
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);
476         }
477         return out;
478 }