some missing files; g_race_qualifying is still broken (ignores fraglimit, no idea...
[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 float scores_flags_primary;
14 float teamscores_flags_primary;
15
16 void Net_LinkEntity(entity e)
17 {
18         e.model = "net_entity";
19         e.modelindex = 1;
20         e.effects = EF_NODEPTHTEST | EF_LOWPRECISION;
21 }
22
23 vector ScoreField_Compare(entity t1, entity t2, .float field, float fieldflags, vector previous) // returns: cmp value, best prio
24 {
25         if(!(fieldflags & SFL_SORT_PRIO_MASK)) // column does not sort
26                 return previous;
27         if(fieldflags & SFL_SORT_PRIO_MASK < previous_y)
28                 return previous;
29         if(t1.field == t2.field)
30                 return previous;
31
32         previous_y = fieldflags & SFL_SORT_PRIO_MASK;
33
34         if(fieldflags & SFL_ZERO_IS_WORST)
35         {
36                 if(t1.field == 0)
37                 {
38                         previous_x = -1;
39                         return previous;
40                 }
41                 else if(t2.field == 0)
42                 {
43                         previous_x = +1;
44                         return previous;
45                 }
46         }
47
48         if(fieldflags & SFL_LOWER_IS_BETTER)
49                 previous_x = (t2.field - t1.field);
50         else
51                 previous_x = (t1.field - t2.field);
52
53         return previous;
54 }
55
56 /*
57  * teamscore entities
58  */
59
60 float TeamScore_SendEntity(entity to)
61 {
62         float i;
63
64         WriteByte(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
65         WriteByte(MSG_ENTITY, self.team - 1);
66         for(i = 0; i < MAX_TEAMSCORE; ++i)
67                 WriteShort(MSG_ENTITY, self.teamscores[i]);
68
69         return TRUE;
70 }
71
72 void TeamScore_Spawn(float t, string name)
73 {
74         entity ts;
75         ts = spawn();
76         ts.classname = "csqc_score_team";
77         ts.SendEntity = TeamScore_SendEntity;
78         ts.netname = name; // not used yet, FIXME
79         ts.Version = 1; // immediately send, so csqc knows about the team
80         ts.team = t;
81         Net_LinkEntity(ts);
82         teamscorekeepers[t - 1] = ts;
83         ++teamscores_entities_count;
84 }
85
86 float TeamScore_AddToTeam(float t, float scorefield, float score)
87 {
88         entity s;
89         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
90         if(t <= 0 || t >= 16)
91                 error("Adding score to invalid team!");
92         s = teamscorekeepers[t - 1];
93         if(!s)
94                 error("Adding score to unknown team!");
95         if(score)
96                 s.Version += 1;
97         return (s.(teamscores[scorefield]) += score);
98 }
99
100 float TeamScore_Add(entity player, float scorefield, float score)
101 {
102         return TeamScore_AddToTeam(player.team, scorefield, score);
103 }
104
105 float TeamScore_Compare(entity t1, entity t2)
106 {
107         if(!t1 || !t2) return (!t2) - !t1;
108
109         vector result;
110         float i;
111         for(i = 0; i < MAX_TEAMSCORE; ++i)
112         {
113                 var .float f;
114                 f = teamscores[i];
115                 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
116         }
117         return result_x;
118 }
119
120 /*
121  * the scoreinfo entity
122  */
123
124 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
125 {
126         scores_label[i] = label;
127         scores_flags[i] = scoreflags;
128         if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
129         {
130                 scores_primary = scores[i];
131                 scores_flags_primary = scoreflags;
132         }
133 }
134
135 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
136 {
137         teamscores_label[i] = label;
138         teamscores_flags[i] = scoreflags;
139         if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
140         {
141                 teamscores_primary = teamscores[i];
142                 teamscores_flags_primary = scoreflags;
143         }
144 }
145
146 void ScoreInfo_Write(float targ)
147 {
148         float i;
149         if not(scores_initialized)
150                 return;
151         WriteByte(targ, SVC_TEMPENTITY);
152         WriteByte(targ, TE_CSQC_SCORESINFO);
153         WriteByte(targ, game);
154         for(i = 0; i < MAX_SCORE; ++i)
155         {
156                 WriteString(targ, scores_label[i]);
157                 WriteByte(targ, scores_flags[i]);
158         }
159         for(i = 0; i < MAX_TEAMSCORE; ++i)
160         {
161                 WriteString(targ, teamscores_label[i]);
162                 WriteByte(targ, teamscores_flags[i]);
163         }
164 }
165
166 void ScoreInfo_Init(float teams)
167 {
168         entity pl;
169         scores_initialized = 1;
170         if(teams >= 1)
171                 TeamScore_Spawn(COLOR_TEAM1, "Red");
172         if(teams >= 2)
173                 TeamScore_Spawn(COLOR_TEAM2, "Blue");
174         if(teams >= 3)
175                 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
176         if(teams >= 4)
177                 TeamScore_Spawn(COLOR_TEAM4, "Pink");
178         FOR_EACH_REALCLIENT(msg_entity) // cannot use MSG_ALL here, as that may come too early on level changes (that SUCKS)
179                 ScoreInfo_Write(MSG_ONE);
180 }
181
182 /*
183  * per-player score entities
184  */
185
186 float PlayerScore_SendEntity()
187 {
188         float i;
189
190         WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
191         WriteByte(MSG_ENTITY, num_for_edict(self.owner));
192         for(i = 0; i < MAX_SCORE; ++i)
193                 WriteShort(MSG_ENTITY, self.scores[i]);
194
195         return TRUE;
196 }
197
198 void PlayerScore_Clear(entity player)
199 {
200         entity sk;
201         float i;
202
203         if(teamscores_entities_count)
204                 return;
205         if(g_lms)
206                 return;
207         if(g_arena)
208                 return;
209         //print("clear clear clear... HAHA\n");
210
211         sk = player.scorekeeper;
212         for(i = 0; i < MAX_SCORE; ++i)
213                 sk.(scores[i]) = 0;
214         sk.Version += 1;
215 }
216
217 void Score_ClearAll()
218 {
219         entity p, sk;
220         float i;
221         FOR_EACH_CLIENTSLOT(p)
222         {
223                 sk = p.scorekeeper;
224                 if(!sk)
225                         continue;
226                 for(i = 0; i < MAX_SCORE; ++i)
227                         sk.(scores[i]) = 0;
228                 sk.Version += 1;
229         }
230         for(i = 0; i < 16; ++i)
231         {
232                 sk = teamscorekeepers[i];
233                 if(!sk)
234                         continue;
235                 for(i = 0; i < MAX_SCORE; ++i)
236                         sk.(teamscores[i]) = 0;
237                 sk.Version += 1;
238         }
239 }
240
241 void PlayerScore_Attach(entity player)
242 {
243         entity sk;
244         if(player.scorekeeper)
245                 error("player already has a scorekeeper");
246         sk = spawn();
247         sk.owner = player;
248         sk.Version = 1;
249         sk.SendEntity = PlayerScore_SendEntity;
250         Net_LinkEntity(sk);
251         player.scorekeeper = sk;
252 }
253
254 void PlayerScore_Detach(entity player)
255 {
256         if(!player.scorekeeper)
257                 error("player has no scorekeeper");
258         remove(player.scorekeeper);
259         player.scorekeeper = world;
260 }
261
262 float PlayerScore_Add(entity player, float scorefield, float score)
263 {
264         entity s;
265         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
266         s = player.scorekeeper;
267         if(!s)
268                 error("Adding score to unknown player!");
269         if(score)
270                 s.Version += 1;
271         return (s.(scores[scorefield]) += score);
272 }
273
274 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
275 {
276         PlayerScore_Add(player, pscorefield, score);
277         if(teamscores_entities_count) // only for teamplay
278                 TeamScore_Add(player, tscorefield, score);
279 }
280
281 float PlayerScore_Compare(entity t1, entity t2)
282 {
283         if(!t1 || !t2) return (!t2) - !t1;
284
285         vector result;
286         float i;
287         for(i = 0; i < MAX_SCORE; ++i)
288         {
289                 var .float f;
290                 f = scores[i];
291                 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
292         }
293         return result_x;
294 }
295
296 void WinningConditionHelper()
297 {
298         float c;
299         string s;
300         entity p;
301         s = GetGametype();
302         s = strcat(s, ":", GetPlayerScoreString(world, 2)); // make this 1 once we can
303
304         if(teamscores_entities_count)
305         {
306                 float t;
307
308                 s = strcat(s, ":", GetTeamScoreString(0, 1));
309                 for(t = 0; t < 16; ++t)
310                         if(teamscorekeepers[t])
311                                 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
312
313                 WinningConditionHelper_equality = 1;
314                 WinningConditionHelper_winnerteam = 0;
315                 for(t = 1; t < 16; ++t)
316                 {
317                         entity sk1, sk2;
318                         sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
319                         sk2 = teamscorekeepers[t];
320                         c = TeamScore_Compare(sk1, sk2);
321                         if(c == 0)
322                                 WinningConditionHelper_equality = 1;
323                         else if(c < 0)
324                         {
325                                 WinningConditionHelper_equality = 0;
326                                 WinningConditionHelper_winnerteam = t;
327                         }
328                 }
329
330                 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
331                 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
332
333                 WinningConditionHelper_winner = world;
334                 if(WinningConditionHelper_equality)
335                         WinningConditionHelper_winnerteam = -1;
336                 else
337                         ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
338         }
339         else
340         {
341                 WinningConditionHelper_equality = 1;
342                 WinningConditionHelper_winner = world;
343                 FOR_EACH_PLAYER(p)
344                 {
345                         c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
346                         if(c == 0)
347                                 WinningConditionHelper_equality = 1;
348                         else if(c < 0)
349                         {
350                                 WinningConditionHelper_equality = 0;
351                                 WinningConditionHelper_winner = p;
352                         }
353                 }
354
355                 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
356                 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
357
358                 if(WinningConditionHelper_equality)
359                         WinningConditionHelper_winner = world;
360                 WinningConditionHelper_winnerteam = -1;
361         }
362
363         if(worldstatus)
364                 strunzone(worldstatus);
365         worldstatus = strzone(s);
366
367         FOR_EACH_CLIENT(p)
368         {
369                 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
370                 s = GetPlayerScoreString(p, 1);
371                 if(clienttype(p) == CLIENTTYPE_REAL)
372                         s = strcat(s, ":human");
373                 else
374                         s = strcat(s, ":bot");
375                 if(p.classname == "player" || g_arena || g_lms)
376                         s = strcat(s, ":", ftos(p.team));
377                 else
378                         s = strcat(s, ":spectator");
379                 */
380                 if(p.classname == "player" || g_arena || g_lms)
381                         s = "-666";
382                 else
383                         s = GetPlayerScoreString(p, 2);
384
385                 if(p.clientstatus)
386                         strunzone(p.clientstatus);
387                 p.clientstatus = strzone(s);
388         }
389 }
390
391 void Score_DebugPrint()
392 {
393         entity p, sk;
394         float i, t;
395
396         print("netname");
397         for(i = 0; i < MAX_SCORE; ++i)
398                 print(":", scores_label[i]);
399         print("\n");
400         FOR_EACH_PLAYER(p)
401         {
402                 sk = p.scorekeeper;
403                 print(p.netname);
404                 for(i = 0; i < MAX_SCORE; ++i)
405                         print(":", ftos(sk.(scores[i])));
406                 print("\n");
407         }
408
409         print("teamname");
410         for(i = 0; i < MAX_TEAMSCORE; ++i)
411                 print(":", teamscores_label[i]);
412         print("\n");
413         for(t = 0; t < 16; ++t)
414         {
415                 sk = teamscorekeepers[t];
416                 if(sk)
417                 {
418                         print(ftos(t));
419                         for(i = 0; i < MAX_TEAMSCORE; ++i)
420                                 print(":", ftos(sk.(teamscores[i])));
421                         print("\n");
422                 }
423         }
424 }
425
426 string GetScoreLogLabel(string label, float fl)
427 {
428         if(fl & SFL_LOWER_IS_BETTER)
429                 label = strcat(label, "<");
430         if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
431                 label = strcat(label, "!!");
432         else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
433                 label = strcat(label, "!");
434         return label;
435 }
436
437 string GetPlayerScoreString(entity pl, float shortString)
438 {
439         string out;
440         entity sk;
441         float i, f;
442         string l;
443
444         out = "";
445         if(!pl)
446         {
447                 // label
448                 for(i = 0; i < MAX_SCORE; ++i)
449                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
450                         {
451                                 f = scores_flags[i];
452                                 l = scores_label[i];
453                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
454                         }
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                         {
459                                 f = scores_flags[i];
460                                 l = scores_label[i];
461                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
462                         }
463                 if(shortString < 1)
464                 for(i = 0; i < MAX_SCORE; ++i)
465                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
466                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
467                         {
468                                 f = scores_flags[i];
469                                 l = scores_label[i];
470                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
471                         }
472                 out = substring(out, 0, strlen(out) - 1);
473         }
474         else if((sk = pl.scorekeeper))
475         {
476                 for(i = 0; i < MAX_SCORE; ++i)
477                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
478                                 out = strcat(out, ftos(sk.(scores[i])), ",");
479                 if(shortString < 2)
480                 for(i = 0; i < MAX_SCORE; ++i)
481                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
482                                 out = strcat(out, ftos(sk.(scores[i])), ",");
483                 if(shortString < 1)
484                 for(i = 0; i < MAX_SCORE; ++i)
485                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
486                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
487                                 out = strcat(out, ftos(sk.(scores[i])), ",");
488                 out = substring(out, 0, strlen(out) - 1);
489         }
490         return out;
491 }
492
493 string GetTeamScoreString(float tm, float shortString)
494 {
495         string out;
496         entity sk;
497         float i, f;
498         string l;
499
500         out = "";
501         if(tm == 0)
502         {
503                 // label
504                 for(i = 0; i < MAX_SCORE; ++i)
505                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
506                         {
507                                 f = teamscores_flags[i];
508                                 l = teamscores_label[i];
509                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
510                         }
511                 if(shortString < 2)
512                 for(i = 0; i < MAX_SCORE; ++i)
513                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
514                         {
515                                 f = teamscores_flags[i];
516                                 l = teamscores_label[i];
517                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
518                         }
519                 if(shortString < 1)
520                 for(i = 0; i < MAX_SCORE; ++i)
521                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
522                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
523                         {
524                                 f = teamscores_flags[i];
525                                 l = teamscores_label[i];
526                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
527                         }
528                 out = substring(out, 0, strlen(out) - 1);
529         }
530         else if((sk = teamscorekeepers[tm - 1]))
531         {
532                 for(i = 0; i < MAX_TEAMSCORE; ++i)
533                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
534                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
535                 if(shortString < 2)
536                 for(i = 0; i < MAX_TEAMSCORE; ++i)
537                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
538                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
539                 if(shortString < 1)
540                 for(i = 0; i < MAX_TEAMSCORE; ++i)
541                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
542                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
543                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
544                 out = substring(out, 0, strlen(out) - 1);
545         }
546         return out;
547 }