]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/scores.qc
use DP_SV_QCSTATUS to show frags to qstat again; make menu aware of player lists...
[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         string s;
278         entity p;
279         s = GetGametype();
280         s = strcat(s, ":", GetPlayerScoreString(world, 2)); // make this 1 once we can
281
282         if(teamscores_entities_count)
283         {
284                 float t;
285
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));
290
291                 WinningConditionHelper_equality = 1;
292                 WinningConditionHelper_winnerteam = 0;
293                 for(t = 1; t < 16; ++t)
294                 {
295                         entity sk1, sk2;
296                         sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
297                         sk2 = teamscorekeepers[t];
298                         c = TeamScore_Compare(sk1, sk2);
299                         if(c == 0)
300                                 WinningConditionHelper_equality = 1;
301                         else if(c < 0)
302                         {
303                                 WinningConditionHelper_equality = 0;
304                                 WinningConditionHelper_winnerteam = t;
305                         }
306                 }
307
308                 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
309
310                 WinningConditionHelper_winner = world;
311                 if(WinningConditionHelper_equality)
312                         WinningConditionHelper_winnerteam = -1;
313                 else
314                         ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
315         }
316         else
317         {
318                 WinningConditionHelper_equality = 1;
319                 WinningConditionHelper_winner = world;
320                 FOR_EACH_PLAYER(p)
321                 {
322                         c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
323                         if(c == 0)
324                                 WinningConditionHelper_equality = 1;
325                         else if(c < 0)
326                         {
327                                 WinningConditionHelper_equality = 0;
328                                 WinningConditionHelper_winner = p;
329                         }
330                 }
331
332                 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
333
334                 if(WinningConditionHelper_equality)
335                         WinningConditionHelper_winner = world;
336                 WinningConditionHelper_winnerteam = -1;
337         }
338
339         if(worldstatus)
340                 strunzone(worldstatus);
341         worldstatus = strzone(s);
342
343         FOR_EACH_CLIENT(p)
344         {
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");
349                 else
350                         s = strcat(s, ":bot");
351                 if(p.classname == "player" || g_arena || g_lms)
352                         s = strcat(s, ":", ftos(p.team));
353                 else
354                         s = strcat(s, ":spectator");
355                 */
356                 if(p.classname == "player" || g_arena || g_lms)
357                         s = "-666";
358                 else
359                         s = GetPlayerScoreString(p, 2);
360
361                 if(p.clientstatus)
362                         strunzone(p.clientstatus);
363                 p.clientstatus = strzone(s);
364         }
365 }
366
367 void Score_DebugPrint()
368 {
369         entity p, sk;
370         float i, t;
371
372         print("netname");
373         for(i = 0; i < MAX_SCORE; ++i)
374                 print(":", scores_label[i]);
375         print("\n");
376         FOR_EACH_PLAYER(p)
377         {
378                 sk = p.scorekeeper;
379                 print(p.netname);
380                 for(i = 0; i < MAX_SCORE; ++i)
381                         print(":", ftos(sk.(scores[i])));
382                 print("\n");
383         }
384
385         print("teamname");
386         for(i = 0; i < MAX_TEAMSCORE; ++i)
387                 print(":", teamscores_label[i]);
388         print("\n");
389         for(t = 0; t < 16; ++t)
390         {
391                 sk = teamscorekeepers[t];
392                 if(sk)
393                 {
394                         print(ftos(t));
395                         for(i = 0; i < MAX_TEAMSCORE; ++i)
396                                 print(":", ftos(sk.(teamscores[i])));
397                         print("\n");
398                 }
399         }
400 }
401
402 string GetScoreLogLabel(string label, float fl)
403 {
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, "!");
410         return label;
411 }
412
413 string GetPlayerScoreString(entity pl, float shortString)
414 {
415         string out;
416         entity sk;
417         float i, f;
418         string l;
419
420         out = "";
421         if(!pl)
422         {
423                 // label
424                 for(i = 0; i < MAX_SCORE; ++i)
425                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
426                         {
427                                 f = scores_flags[i];
428                                 l = scores_label[i];
429                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
430                         }
431                 if(shortString < 2)
432                 for(i = 0; i < MAX_SCORE; ++i)
433                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
434                         {
435                                 f = scores_flags[i];
436                                 l = scores_label[i];
437                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
438                         }
439                 if(shortString < 1)
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)
443                         {
444                                 f = scores_flags[i];
445                                 l = scores_label[i];
446                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
447                         }
448                 out = substring(out, 0, strlen(out) - 1);
449         }
450         else if((sk = pl.scorekeeper))
451         {
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])), ",");
455                 if(shortString < 2)
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])), ",");
459                 if(shortString < 1)
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);
465         }
466         return out;
467 }
468
469 string GetTeamScoreString(float tm, float shortString)
470 {
471         string out;
472         entity sk;
473         float i, f;
474         string l;
475
476         out = "";
477         if(tm == 0)
478         {
479                 // label
480                 for(i = 0; i < MAX_SCORE; ++i)
481                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
482                         {
483                                 f = teamscores_flags[i];
484                                 l = teamscores_label[i];
485                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
486                         }
487                 if(shortString < 2)
488                 for(i = 0; i < MAX_SCORE; ++i)
489                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
490                         {
491                                 f = teamscores_flags[i];
492                                 l = teamscores_label[i];
493                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
494                         }
495                 if(shortString < 1)
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)
499                         {
500                                 f = teamscores_flags[i];
501                                 l = teamscores_label[i];
502                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
503                         }
504                 out = substring(out, 0, strlen(out) - 1);
505         }
506         else if((sk = teamscorekeepers[tm - 1]))
507         {
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])), ",");
511                 if(shortString < 2)
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])), ",");
515                 if(shortString < 1)
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);
521         }
522         return out;
523 }