]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/scores.qc
race: "g_race_qualifying 2" makes qualifying-then-race mode (using new special entiti...
[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) return;
206         if(g_arena) return;
207         if(g_race) return;
208         //print("clear clear clear... HAHA\n");
209
210         sk = player.scorekeeper;
211         for(i = 0; i < MAX_SCORE; ++i)
212                 sk.(scores[i]) = 0;
213         sk.Version += 1;
214 }
215
216 void Score_ClearAll()
217 {
218         entity p, sk;
219         float i;
220         FOR_EACH_CLIENTSLOT(p)
221         {
222                 sk = p.scorekeeper;
223                 if(!sk)
224                         continue;
225                 for(i = 0; i < MAX_SCORE; ++i)
226                         sk.(scores[i]) = 0;
227                 sk.Version += 1;
228         }
229         for(i = 0; i < 16; ++i)
230         {
231                 sk = teamscorekeepers[i];
232                 if(!sk)
233                         continue;
234                 for(i = 0; i < MAX_SCORE; ++i)
235                         sk.(teamscores[i]) = 0;
236                 sk.Version += 1;
237         }
238 }
239
240 void PlayerScore_Attach(entity player)
241 {
242         entity sk;
243         if(player.scorekeeper)
244                 error("player already has a scorekeeper");
245         sk = spawn();
246         sk.owner = player;
247         sk.Version = 1;
248         sk.SendEntity = PlayerScore_SendEntity;
249         Net_LinkEntity(sk);
250         player.scorekeeper = sk;
251 }
252
253 void PlayerScore_Detach(entity player)
254 {
255         if(!player.scorekeeper)
256                 error("player has no scorekeeper");
257         remove(player.scorekeeper);
258         player.scorekeeper = world;
259 }
260
261 float PlayerScore_Add(entity player, float scorefield, float score)
262 {
263         entity s;
264         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
265         s = player.scorekeeper;
266         if(!s)
267                 error("Adding score to unknown player!");
268         if(score)
269                 s.Version += 1;
270         return (s.(scores[scorefield]) += score);
271 }
272
273 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
274 {
275         PlayerScore_Add(player, pscorefield, score);
276         if(teamscores_entities_count) // only for teamplay
277                 TeamScore_Add(player, tscorefield, score);
278 }
279
280 float PlayerScore_Compare(entity t1, entity t2)
281 {
282         if(!t1 || !t2) return (!t2) - !t1;
283
284         vector result;
285         float i;
286         for(i = 0; i < MAX_SCORE; ++i)
287         {
288                 var .float f;
289                 f = scores[i];
290                 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
291         }
292         return result_x;
293 }
294
295 void WinningConditionHelper()
296 {
297         float c;
298         string s;
299         entity p;
300         s = GetGametype();
301         s = strcat(s, ":", GetPlayerScoreString(world, 2)); // make this 1 once we can
302
303         if(teamscores_entities_count)
304         {
305                 float t;
306
307                 s = strcat(s, ":", GetTeamScoreString(0, 1));
308                 for(t = 0; t < 16; ++t)
309                         if(teamscorekeepers[t])
310                                 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
311
312                 WinningConditionHelper_equality = 1;
313                 WinningConditionHelper_winnerteam = 0;
314                 for(t = 1; t < 16; ++t)
315                 {
316                         entity sk1, sk2;
317                         sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
318                         sk2 = teamscorekeepers[t];
319                         c = TeamScore_Compare(sk1, sk2);
320                         if(c == 0)
321                                 WinningConditionHelper_equality = 1;
322                         else if(c < 0)
323                         {
324                                 WinningConditionHelper_equality = 0;
325                                 WinningConditionHelper_winnerteam = t;
326                         }
327                 }
328
329                 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
330                 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
331                 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
332                         if(WinningConditionHelper_topscore == 0)
333                         {
334                                 if(WinningConditionHelper_lowerisbetter)
335                                         WinningConditionHelper_topscore = 999999999;
336                                 else
337                                         WinningConditionHelper_topscore = -999999999;
338                         }
339                 if(teamscores_flags_primary & SFL_TIME)
340                         WinningConditionHelper_topscore /= 10;
341
342                 WinningConditionHelper_winner = world;
343                 if(WinningConditionHelper_equality)
344                         WinningConditionHelper_winnerteam = -1;
345                 else
346                         ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
347         }
348         else
349         {
350                 WinningConditionHelper_equality = 1;
351                 WinningConditionHelper_winner = world;
352                 FOR_EACH_PLAYER(p)
353                 {
354                         c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
355                         if(c == 0)
356                                 WinningConditionHelper_equality = 1;
357                         else if(c < 0)
358                         {
359                                 WinningConditionHelper_equality = 0;
360                                 WinningConditionHelper_winner = p;
361                         }
362                 }
363
364                 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
365                 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
366                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
367                         if(WinningConditionHelper_topscore == 0)
368                         {
369                                 if(WinningConditionHelper_lowerisbetter)
370                                         WinningConditionHelper_topscore = 999999999;
371                                 else
372                                         WinningConditionHelper_topscore = -999999999;
373                         }
374                 if(teamscores_flags_primary & SFL_TIME)
375                         WinningConditionHelper_topscore /= 10;
376
377                 if(WinningConditionHelper_equality)
378                         WinningConditionHelper_winner = world;
379                 WinningConditionHelper_winnerteam = -1;
380         }
381
382         if(worldstatus)
383                 strunzone(worldstatus);
384         worldstatus = strzone(s);
385
386         FOR_EACH_CLIENT(p)
387         {
388                 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
389                 s = GetPlayerScoreString(p, 1);
390                 if(clienttype(p) == CLIENTTYPE_REAL)
391                         s = strcat(s, ":human");
392                 else
393                         s = strcat(s, ":bot");
394                 if(p.classname == "player" || g_arena || g_lms)
395                         s = strcat(s, ":", ftos(p.team));
396                 else
397                         s = strcat(s, ":spectator");
398                 */
399                 if(p.classname == "player" || g_arena || g_lms)
400                         s = "-666";
401                 else
402                         s = GetPlayerScoreString(p, 2);
403
404                 if(p.clientstatus)
405                         strunzone(p.clientstatus);
406                 p.clientstatus = strzone(s);
407         }
408 }
409
410 void Score_DebugPrint()
411 {
412         entity p, sk;
413         float i, t;
414
415         print("netname");
416         for(i = 0; i < MAX_SCORE; ++i)
417                 print(":", scores_label[i]);
418         print("\n");
419         FOR_EACH_PLAYER(p)
420         {
421                 sk = p.scorekeeper;
422                 print(p.netname);
423                 for(i = 0; i < MAX_SCORE; ++i)
424                         print(":", ftos(sk.(scores[i])));
425                 print("\n");
426         }
427
428         print("teamname");
429         for(i = 0; i < MAX_TEAMSCORE; ++i)
430                 print(":", teamscores_label[i]);
431         print("\n");
432         for(t = 0; t < 16; ++t)
433         {
434                 sk = teamscorekeepers[t];
435                 if(sk)
436                 {
437                         print(ftos(t));
438                         for(i = 0; i < MAX_TEAMSCORE; ++i)
439                                 print(":", ftos(sk.(teamscores[i])));
440                         print("\n");
441                 }
442         }
443 }
444
445 string GetScoreLogLabel(string label, float fl)
446 {
447         if(fl & SFL_LOWER_IS_BETTER)
448                 label = strcat(label, "<");
449         if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
450                 label = strcat(label, "!!");
451         else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
452                 label = strcat(label, "!");
453         return label;
454 }
455
456 string GetPlayerScoreString(entity pl, float shortString)
457 {
458         string out;
459         entity sk;
460         float i, f;
461         string l;
462
463         out = "";
464         if(!pl)
465         {
466                 // label
467                 for(i = 0; i < MAX_SCORE; ++i)
468                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
469                         {
470                                 f = scores_flags[i];
471                                 l = scores_label[i];
472                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
473                         }
474                 if(shortString < 2)
475                 for(i = 0; i < MAX_SCORE; ++i)
476                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
477                         {
478                                 f = scores_flags[i];
479                                 l = scores_label[i];
480                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
481                         }
482                 if(shortString < 1)
483                 for(i = 0; i < MAX_SCORE; ++i)
484                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
485                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
486                         {
487                                 f = scores_flags[i];
488                                 l = scores_label[i];
489                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
490                         }
491                 out = substring(out, 0, strlen(out) - 1);
492         }
493         else if((sk = pl.scorekeeper))
494         {
495                 for(i = 0; i < MAX_SCORE; ++i)
496                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
497                                 out = strcat(out, ftos(sk.(scores[i])), ",");
498                 if(shortString < 2)
499                 for(i = 0; i < MAX_SCORE; ++i)
500                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
501                                 out = strcat(out, ftos(sk.(scores[i])), ",");
502                 if(shortString < 1)
503                 for(i = 0; i < MAX_SCORE; ++i)
504                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
505                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
506                                 out = strcat(out, ftos(sk.(scores[i])), ",");
507                 out = substring(out, 0, strlen(out) - 1);
508         }
509         return out;
510 }
511
512 string GetTeamScoreString(float tm, float shortString)
513 {
514         string out;
515         entity sk;
516         float i, f;
517         string l;
518
519         out = "";
520         if(tm == 0)
521         {
522                 // label
523                 for(i = 0; i < MAX_SCORE; ++i)
524                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
525                         {
526                                 f = teamscores_flags[i];
527                                 l = teamscores_label[i];
528                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
529                         }
530                 if(shortString < 2)
531                 for(i = 0; i < MAX_SCORE; ++i)
532                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
533                         {
534                                 f = teamscores_flags[i];
535                                 l = teamscores_label[i];
536                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
537                         }
538                 if(shortString < 1)
539                 for(i = 0; i < MAX_SCORE; ++i)
540                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
541                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
542                         {
543                                 f = teamscores_flags[i];
544                                 l = teamscores_label[i];
545                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
546                         }
547                 out = substring(out, 0, strlen(out) - 1);
548         }
549         else if((sk = teamscorekeepers[tm - 1]))
550         {
551                 for(i = 0; i < MAX_TEAMSCORE; ++i)
552                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
553                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
554                 if(shortString < 2)
555                 for(i = 0; i < MAX_TEAMSCORE; ++i)
556                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
557                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
558                 if(shortString < 1)
559                 for(i = 0; i < MAX_TEAMSCORE; ++i)
560                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
561                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
562                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
563                 out = substring(out, 0, strlen(out) - 1);
564         }
565         return out;
566 }
567
568 float PlayerTeamScore_Compare(entity p1, entity p2)
569 {
570         if(teamscores_entities_count)
571                 if(p1.team != p2.team)
572                 {
573                         entity t1, t2;
574                         t1 = teamscorekeepers[p1.team];
575                         t2 = teamscorekeepers[p2.team];
576                         return TeamScore_Compare(t1, t2);
577                 }
578         
579         return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
580 }
581
582 float PlayerScore_Sort(.float field)
583 {
584         entity p, plist, pprev, pbest, pbestprev;
585         float i;
586         plist = world;
587
588         FOR_EACH_CLIENT(p)
589                 p.field = 0;
590
591         FOR_EACH_PLAYER(p) if(p.scorekeeper)
592         {
593                 p.chain = plist;
594                 plist = p;
595         }
596         // Now plist points to the whole list.
597
598         i = 0;
599         while(plist)
600         {
601                 pprev = pbestprev = world;
602                 pbest = plist;
603                 for(p = plist; (p = p.chain); )
604                 {
605                         if(PlayerTeamScore_Compare(p, pbest) > 0)
606                         {
607                                 pbest = p;
608                                 pbestprev = pprev;
609                         }
610                         pprev = p;
611                 }
612
613                 // remove pbest out of the chain
614                 if(pbestprev == world)
615                         plist = pbest.chain;
616                 else
617                         pbestprev.chain = pbest.chain;
618                 pbest.chain = world;
619
620                 pbest.field = ++i;
621         }
622
623         return i;
624 }