]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/scores.qc
fix endless loop in scores clearing
[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, float sendflags)
61 {
62         float i;
63
64         WriteByte(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
65         WriteByte(MSG_ENTITY, self.team - 1);
66 #if MAX_TEAMSCORE <= 3
67         for(i = 0; i < MAX_TEAMSCORE; ++i)
68                 WriteShort(MSG_ENTITY, self.teamscores[i]);
69 #else
70 #if MAX_TEAMSCORE <= 8
71         WriteByte(MSG_ENTITY, sendflags);
72 #else
73         WriteShort(MSG_ENTITY, sendflags);
74 #endif
75         float p;
76         for(i = 0, p = 1; i < MAX_TEAMSCORE; ++i, p *= 2)
77                 if(sendflags & p)
78                         WriteShort(MSG_ENTITY, self.teamscores[i]);
79 #endif
80
81         return TRUE;
82 }
83
84 void TeamScore_Spawn(float t, string name)
85 {
86         entity ts;
87         ts = spawn();
88         ts.classname = "csqc_score_team";
89         ts.SendEntity = TeamScore_SendEntity;
90         ts.netname = name; // not used yet, FIXME
91         //ts.SendFlags = SENDFLAGS_CREATE; // immediately send, so csqc knows about the team
92         ts.team = t;
93         Net_LinkEntity(ts);
94         teamscorekeepers[t - 1] = ts;
95         ++teamscores_entities_count;
96 }
97
98 float TeamScore_AddToTeam(float t, float scorefield, float score)
99 {
100         entity s;
101         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
102         if(t <= 0 || t >= 16)
103                 error("Adding score to invalid team!");
104         s = teamscorekeepers[t - 1];
105         if(!s)
106                 error("Adding score to unknown team!");
107         if(score)
108                 if(teamscores_label[scorefield] != "")
109                         s.SendFlags |= pow(2, scorefield);
110         return (s.(teamscores[scorefield]) += score);
111 }
112
113 float TeamScore_Add(entity player, float scorefield, float score)
114 {
115         return TeamScore_AddToTeam(player.team, scorefield, score);
116 }
117
118 float TeamScore_Compare(entity t1, entity t2)
119 {
120         if(!t1 || !t2) return (!t2) - !t1;
121
122         vector result;
123         float i;
124         for(i = 0; i < MAX_TEAMSCORE; ++i)
125         {
126                 var .float f;
127                 f = teamscores[i];
128                 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
129         }
130         return result_x;
131 }
132
133 /*
134  * the scoreinfo entity
135  */
136
137 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
138 {
139         scores_label[i] = label;
140         scores_flags[i] = scoreflags;
141         if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
142         {
143                 scores_primary = scores[i];
144                 scores_flags_primary = scoreflags;
145         }
146 }
147
148 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
149 {
150         teamscores_label[i] = label;
151         teamscores_flags[i] = scoreflags;
152         if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
153         {
154                 teamscores_primary = teamscores[i];
155                 teamscores_flags_primary = scoreflags;
156         }
157 }
158
159 void ScoreInfo_Write(float targ)
160 {
161         float i;
162         if not(scores_initialized)
163                 return;
164         WriteByte(targ, SVC_TEMPENTITY);
165         WriteByte(targ, TE_CSQC_SCORESINFO);
166         WriteByte(targ, game);
167         for(i = 0; i < MAX_SCORE; ++i)
168         {
169                 WriteString(targ, scores_label[i]);
170                 WriteByte(targ, scores_flags[i]);
171         }
172         for(i = 0; i < MAX_TEAMSCORE; ++i)
173         {
174                 WriteString(targ, teamscores_label[i]);
175                 WriteByte(targ, teamscores_flags[i]);
176         }
177 }
178
179 void ScoreInfo_Init(float teams)
180 {
181         scores_initialized = 1;
182         if(teams >= 1)
183                 TeamScore_Spawn(COLOR_TEAM1, "Red");
184         if(teams >= 2)
185                 TeamScore_Spawn(COLOR_TEAM2, "Blue");
186         if(teams >= 3)
187                 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
188         if(teams >= 4)
189                 TeamScore_Spawn(COLOR_TEAM4, "Pink");
190         ScoreInfo_Write(MSG_ALL);
191 }
192
193 /*
194  * per-player score entities
195  */
196
197 float PlayerScore_SendEntity(entity to, float sendflags)
198 {
199         float i;
200
201         WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
202         WriteByte(MSG_ENTITY, num_for_edict(self.owner));
203 #if MAX_SCORE <= 3
204         for(i = 0; i < MAX_SCORE; ++i)
205                 WriteShort(MSG_ENTITY, self.scores[i]);
206 #else
207 #if MAX_SCORE <= 8
208         WriteByte(MSG_ENTITY, sendflags);
209 #else
210         WriteShort(MSG_ENTITY, sendflags);
211 #endif
212         float p;
213         for(i = 0, p = 1; i < MAX_SCORE; ++i, p *= 2)
214                 if(sendflags & p)
215                         WriteShort(MSG_ENTITY, self.scores[i]);
216 #endif
217
218         return TRUE;
219 }
220
221 void PlayerScore_Clear(entity player)
222 {
223         entity sk;
224         float i;
225
226         if(teamscores_entities_count)
227                 return;
228         if(g_lms) return;
229         if(g_arena) return;
230         if(g_race && !g_race_qualifying) return;
231
232         sk = player.scorekeeper;
233         for(i = 0; i < MAX_SCORE; ++i)
234         {
235                 if(sk.(scores[i]) != 0)
236                         if(scores_label[i] != "")
237                                 sk.SendFlags |= pow(2, i);
238                 sk.(scores[i]) = 0;
239         }
240 }
241
242 void Score_ClearAll()
243 {
244         entity p, sk;
245         float i, t;
246         FOR_EACH_CLIENTSLOT(p)
247         {
248                 sk = p.scorekeeper;
249                 if(!sk)
250                         continue;
251                 for(i = 0; i < MAX_SCORE; ++i)
252                 {
253                         if(sk.(scores[i]) != 0)
254                                 if(scores_label[i] != "")
255                                         sk.SendFlags |= pow(2, i);
256                         sk.(scores[i]) = 0;
257                 }
258         }
259         for(t = 0; t < 16; ++t)
260         {
261                 sk = teamscorekeepers[t];
262                 if(!sk)
263                         continue;
264                 for(i = 0; i < MAX_SCORE; ++i)
265                 {
266                         if(sk.(teamscores[i]) != 0)
267                                 if(teamscores_label[i] != "")
268                                         sk.SendFlags |= pow(2, i);
269                         sk.(teamscores[i]) = 0;
270                 }
271         }
272 }
273
274 void PlayerScore_Attach(entity player)
275 {
276         entity sk;
277         if(player.scorekeeper)
278                 error("player already has a scorekeeper");
279         sk = spawn();
280         sk.owner = player;
281         sk.SendEntity = PlayerScore_SendEntity;
282         Net_LinkEntity(sk);
283         player.scorekeeper = sk;
284 }
285
286 void PlayerScore_Detach(entity player)
287 {
288         if(!player.scorekeeper)
289                 error("player has no scorekeeper");
290         remove(player.scorekeeper);
291         player.scorekeeper = world;
292 }
293
294 float PlayerScore_Add(entity player, float scorefield, float score)
295 {
296         entity s;
297         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
298         s = player.scorekeeper;
299         if(!s)
300                 error("Adding score to unknown player!");
301         if(score)
302                 if(scores_label[scorefield] != "")
303                         s.SendFlags |= pow(2, scorefield);
304         return (s.(scores[scorefield]) += score);
305 }
306
307 float PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
308 {
309         float r;
310         r = PlayerScore_Add(player, pscorefield, score);
311         if(teamscores_entities_count) // only for teamplay
312                 r = TeamScore_Add(player, tscorefield, score);
313         return r;
314 }
315
316 float PlayerScore_Compare(entity t1, entity t2)
317 {
318         if(!t1 || !t2) return (!t2) - !t1;
319
320         vector result;
321         float i;
322         for(i = 0; i < MAX_SCORE; ++i)
323         {
324                 var .float f;
325                 f = scores[i];
326                 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
327         }
328         return result_x;
329 }
330
331 void WinningConditionHelper()
332 {
333         float c;
334         string s;
335         entity p;
336         s = GetGametype();
337         s = strcat(s, ":", GetPlayerScoreString(world, 2)); // make this 1 once we can
338
339         if(teamscores_entities_count)
340         {
341                 float t;
342
343                 s = strcat(s, ":", GetTeamScoreString(0, 1));
344                 for(t = 0; t < 16; ++t)
345                         if(teamscorekeepers[t])
346                                 s = strcat(s, ":", ftos(t+1), ":", GetTeamScoreString(t+1, 1));
347
348                 WinningConditionHelper_equality = 0;
349                 WinningConditionHelper_winnerteam = 0;
350                 for(t = 1; t < 16; ++t)
351                 {
352                         entity sk1, sk2;
353                         sk1 = teamscorekeepers[WinningConditionHelper_winnerteam];
354                         sk2 = teamscorekeepers[t];
355                         c = TeamScore_Compare(sk1, sk2);
356                         if(c == 0)
357                                 WinningConditionHelper_equality = 1;
358                         else if(c < 0)
359                         {
360                                 WinningConditionHelper_equality = 0;
361                                 WinningConditionHelper_winnerteam = t;
362                         }
363                 }
364
365                 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
366                 WinningConditionHelper_lowerisbetter = (teamscores_flags_primary & SFL_LOWER_IS_BETTER);
367                 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
368                 if(teamscores_flags_primary & SFL_TIME)
369                         WinningConditionHelper_topscore /= 10;
370
371                 WinningConditionHelper_winner = world;
372                 if(WinningConditionHelper_equality)
373                         WinningConditionHelper_winnerteam = -1;
374                 else
375                         ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
376         }
377         else
378         {
379                 WinningConditionHelper_equality = 0;
380                 WinningConditionHelper_winner = world;
381                 FOR_EACH_PLAYER(p)
382                 {
383                         c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
384                         if(c == 0)
385                                 WinningConditionHelper_equality = 1;
386                         else if(c < 0)
387                         {
388                                 WinningConditionHelper_equality = 0;
389                                 WinningConditionHelper_winner = p;
390                         }
391                 }
392
393                 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
394                 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
395                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
396                         if(WinningConditionHelper_topscore == 0)
397                         {
398                                 if(WinningConditionHelper_lowerisbetter)
399                                         WinningConditionHelper_topscore = 999999999;
400                                 else
401                                         WinningConditionHelper_topscore = -999999999;
402                         }
403                 if(teamscores_flags_primary & SFL_TIME)
404                         WinningConditionHelper_topscore /= 10;
405
406                 if(WinningConditionHelper_equality)
407                         WinningConditionHelper_winner = world;
408                 WinningConditionHelper_winnerteam = -1;
409         }
410
411         if(worldstatus)
412                 strunzone(worldstatus);
413         worldstatus = strzone(s);
414
415         FOR_EACH_CLIENT(p)
416         {
417                 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
418                 s = GetPlayerScoreString(p, 1);
419                 if(clienttype(p) == CLIENTTYPE_REAL)
420                         s = strcat(s, ":human");
421                 else
422                         s = strcat(s, ":bot");
423                 if(p.classname == "player" || g_arena || g_lms)
424                         s = strcat(s, ":", ftos(p.team));
425                 else
426                         s = strcat(s, ":spectator");
427                 */
428                 if(p.classname == "player" || g_arena || g_lms)
429                         s = "-666";
430                 else
431                         s = GetPlayerScoreString(p, 2);
432
433                 if(p.clientstatus)
434                         strunzone(p.clientstatus);
435                 p.clientstatus = strzone(s);
436         }
437 }
438
439 void Score_DebugPrint()
440 {
441         entity p, sk;
442         float i, t;
443
444         print("netname");
445         for(i = 0; i < MAX_SCORE; ++i)
446                 print(":", scores_label[i]);
447         print("\n");
448         FOR_EACH_PLAYER(p)
449         {
450                 sk = p.scorekeeper;
451                 print(p.netname);
452                 for(i = 0; i < MAX_SCORE; ++i)
453                         print(":", ftos(sk.(scores[i])));
454                 print("\n");
455         }
456
457         print("teamname");
458         for(i = 0; i < MAX_TEAMSCORE; ++i)
459                 print(":", teamscores_label[i]);
460         print("\n");
461         for(t = 0; t < 16; ++t)
462         {
463                 sk = teamscorekeepers[t];
464                 if(sk)
465                 {
466                         print(ftos(t));
467                         for(i = 0; i < MAX_TEAMSCORE; ++i)
468                                 print(":", ftos(sk.(teamscores[i])));
469                         print("\n");
470                 }
471         }
472 }
473
474 string GetScoreLogLabel(string label, float fl)
475 {
476         if(fl & SFL_LOWER_IS_BETTER)
477                 label = strcat(label, "<");
478         if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
479                 label = strcat(label, "!!");
480         else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
481                 label = strcat(label, "!");
482         return label;
483 }
484
485 string GetPlayerScoreString(entity pl, float shortString)
486 {
487         string out;
488         entity sk;
489         float i, f;
490         string l;
491
492         out = "";
493         if(!pl)
494         {
495                 // label
496                 for(i = 0; i < MAX_SCORE; ++i)
497                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
498                         {
499                                 f = scores_flags[i];
500                                 l = scores_label[i];
501                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
502                         }
503                 if(shortString < 2)
504                 for(i = 0; i < MAX_SCORE; ++i)
505                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
506                         {
507                                 f = scores_flags[i];
508                                 l = scores_label[i];
509                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
510                         }
511                 if(shortString < 1)
512                 for(i = 0; i < MAX_SCORE; ++i)
513                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
514                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
515                         {
516                                 f = scores_flags[i];
517                                 l = scores_label[i];
518                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
519                         }
520                 out = substring(out, 0, strlen(out) - 1);
521         }
522         else if((sk = pl.scorekeeper))
523         {
524                 for(i = 0; i < MAX_SCORE; ++i)
525                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
526                                 out = strcat(out, ftos(sk.(scores[i])), ",");
527                 if(shortString < 2)
528                 for(i = 0; i < MAX_SCORE; ++i)
529                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
530                                 out = strcat(out, ftos(sk.(scores[i])), ",");
531                 if(shortString < 1)
532                 for(i = 0; i < MAX_SCORE; ++i)
533                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
534                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
535                                 out = strcat(out, ftos(sk.(scores[i])), ",");
536                 out = substring(out, 0, strlen(out) - 1);
537         }
538         return out;
539 }
540
541 string GetTeamScoreString(float tm, float shortString)
542 {
543         string out;
544         entity sk;
545         float i, f;
546         string l;
547
548         out = "";
549         if(tm == 0)
550         {
551                 // label
552                 for(i = 0; i < MAX_SCORE; ++i)
553                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
554                         {
555                                 f = teamscores_flags[i];
556                                 l = teamscores_label[i];
557                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
558                         }
559                 if(shortString < 2)
560                 for(i = 0; i < MAX_SCORE; ++i)
561                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
562                         {
563                                 f = teamscores_flags[i];
564                                 l = teamscores_label[i];
565                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
566                         }
567                 if(shortString < 1)
568                 for(i = 0; i < MAX_SCORE; ++i)
569                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
570                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
571                         {
572                                 f = teamscores_flags[i];
573                                 l = teamscores_label[i];
574                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
575                         }
576                 out = substring(out, 0, strlen(out) - 1);
577         }
578         else if((sk = teamscorekeepers[tm - 1]))
579         {
580                 for(i = 0; i < MAX_TEAMSCORE; ++i)
581                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
582                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
583                 if(shortString < 2)
584                 for(i = 0; i < MAX_TEAMSCORE; ++i)
585                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
586                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
587                 if(shortString < 1)
588                 for(i = 0; i < MAX_TEAMSCORE; ++i)
589                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
590                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
591                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
592                 out = substring(out, 0, strlen(out) - 1);
593         }
594         return out;
595 }
596
597 float PlayerTeamScore_Compare(entity p1, entity p2)
598 {
599         if(teamscores_entities_count)
600                 if(p1.team != p2.team)
601                 {
602                         entity t1, t2;
603                         t1 = teamscorekeepers[p1.team];
604                         t2 = teamscorekeepers[p2.team];
605                         return TeamScore_Compare(t1, t2);
606                 }
607         
608         return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
609 }
610
611 float PlayerScore_Sort(.float field)
612 {
613         entity p, plist, pprev, pbest, pbestprev;
614         float i;
615         plist = world;
616
617         FOR_EACH_CLIENT(p)
618                 p.field = 0;
619
620         FOR_EACH_PLAYER(p) if(p.scorekeeper)
621         {
622                 p.chain = plist;
623                 plist = p;
624         }
625         // Now plist points to the whole list.
626
627         i = 0;
628         while(plist)
629         {
630                 pprev = pbestprev = world;
631                 pbest = plist;
632                 for(p = plist; (pprev = p), (p = p.chain); )
633                 {
634                         if(PlayerTeamScore_Compare(p, pbest) > 0)
635                         {
636                                 pbest = p;
637                                 pbestprev = pprev;
638                         }
639                 }
640
641                 // remove pbest out of the chain
642                 if(pbestprev == world)
643                         plist = pbest.chain;
644                 else
645                         pbestprev.chain = pbest.chain;
646                 pbest.chain = world;
647
648                 pbest.field = ++i;
649         }
650
651         return i;
652 }
653
654 float TeamScore_GetCompareValue(float t)
655 {
656         float s;
657         entity sk;
658
659         if(t <= 0 || t >= 16)
660                 error("Reading score of invalid team!");
661
662         sk = teamscorekeepers[t - 1];
663         if not(sk)
664                 return -999999999;
665         s = sk.teamscores_primary;
666         if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
667                 if(!s)
668                         return -999999999;
669         if(teamscores_flags_primary & SFL_LOWER_IS_BETTER)
670                 s = -s;
671         return s;
672 }