]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/scores.qc
new improvements (winning condition, zero is worst handling)
[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                 WinningConditionHelper_zeroisworst = (teamscores_flags_primary & SFL_ZERO_IS_WORST);
332                 if(teamscores_flags_primary & SFL_TIME)
333                         WinningConditionHelper_topscore /= 10;
334
335                 WinningConditionHelper_winner = world;
336                 if(WinningConditionHelper_equality)
337                         WinningConditionHelper_winnerteam = -1;
338                 else
339                         ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
340         }
341         else
342         {
343                 WinningConditionHelper_equality = 1;
344                 WinningConditionHelper_winner = world;
345                 FOR_EACH_PLAYER(p)
346                 {
347                         c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
348                         if(c == 0)
349                                 WinningConditionHelper_equality = 1;
350                         else if(c < 0)
351                         {
352                                 WinningConditionHelper_equality = 0;
353                                 WinningConditionHelper_winner = p;
354                         }
355                 }
356
357                 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
358                 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
359                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
360                         if(WinningConditionHelper_topscore == 0)
361                         {
362                                 if(WinningConditionHelper_lowerisbetter)
363                                         WinningConditionHelper_topscore = 999999999;
364                                 else
365                                         WinningConditionHelper_topscore = -999999999;
366                         }
367                 if(teamscores_flags_primary & SFL_TIME)
368                         WinningConditionHelper_topscore /= 10;
369
370                 if(WinningConditionHelper_equality)
371                         WinningConditionHelper_winner = world;
372                 WinningConditionHelper_winnerteam = -1;
373         }
374
375         if(worldstatus)
376                 strunzone(worldstatus);
377         worldstatus = strzone(s);
378
379         FOR_EACH_CLIENT(p)
380         {
381                 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
382                 s = GetPlayerScoreString(p, 1);
383                 if(clienttype(p) == CLIENTTYPE_REAL)
384                         s = strcat(s, ":human");
385                 else
386                         s = strcat(s, ":bot");
387                 if(p.classname == "player" || g_arena || g_lms)
388                         s = strcat(s, ":", ftos(p.team));
389                 else
390                         s = strcat(s, ":spectator");
391                 */
392                 if(p.classname == "player" || g_arena || g_lms)
393                         s = "-666";
394                 else
395                         s = GetPlayerScoreString(p, 2);
396
397                 if(p.clientstatus)
398                         strunzone(p.clientstatus);
399                 p.clientstatus = strzone(s);
400         }
401 }
402
403 void Score_DebugPrint()
404 {
405         entity p, sk;
406         float i, t;
407
408         print("netname");
409         for(i = 0; i < MAX_SCORE; ++i)
410                 print(":", scores_label[i]);
411         print("\n");
412         FOR_EACH_PLAYER(p)
413         {
414                 sk = p.scorekeeper;
415                 print(p.netname);
416                 for(i = 0; i < MAX_SCORE; ++i)
417                         print(":", ftos(sk.(scores[i])));
418                 print("\n");
419         }
420
421         print("teamname");
422         for(i = 0; i < MAX_TEAMSCORE; ++i)
423                 print(":", teamscores_label[i]);
424         print("\n");
425         for(t = 0; t < 16; ++t)
426         {
427                 sk = teamscorekeepers[t];
428                 if(sk)
429                 {
430                         print(ftos(t));
431                         for(i = 0; i < MAX_TEAMSCORE; ++i)
432                                 print(":", ftos(sk.(teamscores[i])));
433                         print("\n");
434                 }
435         }
436 }
437
438 string GetScoreLogLabel(string label, float fl)
439 {
440         if(fl & SFL_LOWER_IS_BETTER)
441                 label = strcat(label, "<");
442         if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
443                 label = strcat(label, "!!");
444         else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
445                 label = strcat(label, "!");
446         return label;
447 }
448
449 string GetPlayerScoreString(entity pl, float shortString)
450 {
451         string out;
452         entity sk;
453         float i, f;
454         string l;
455
456         out = "";
457         if(!pl)
458         {
459                 // label
460                 for(i = 0; i < MAX_SCORE; ++i)
461                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
462                         {
463                                 f = scores_flags[i];
464                                 l = scores_label[i];
465                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
466                         }
467                 if(shortString < 2)
468                 for(i = 0; i < MAX_SCORE; ++i)
469                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
470                         {
471                                 f = scores_flags[i];
472                                 l = scores_label[i];
473                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
474                         }
475                 if(shortString < 1)
476                 for(i = 0; i < MAX_SCORE; ++i)
477                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
478                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
479                         {
480                                 f = scores_flags[i];
481                                 l = scores_label[i];
482                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
483                         }
484                 out = substring(out, 0, strlen(out) - 1);
485         }
486         else if((sk = pl.scorekeeper))
487         {
488                 for(i = 0; i < MAX_SCORE; ++i)
489                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
490                                 out = strcat(out, ftos(sk.(scores[i])), ",");
491                 if(shortString < 2)
492                 for(i = 0; i < MAX_SCORE; ++i)
493                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
494                                 out = strcat(out, ftos(sk.(scores[i])), ",");
495                 if(shortString < 1)
496                 for(i = 0; i < MAX_SCORE; ++i)
497                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
498                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
499                                 out = strcat(out, ftos(sk.(scores[i])), ",");
500                 out = substring(out, 0, strlen(out) - 1);
501         }
502         return out;
503 }
504
505 string GetTeamScoreString(float tm, float shortString)
506 {
507         string out;
508         entity sk;
509         float i, f;
510         string l;
511
512         out = "";
513         if(tm == 0)
514         {
515                 // label
516                 for(i = 0; i < MAX_SCORE; ++i)
517                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
518                         {
519                                 f = teamscores_flags[i];
520                                 l = teamscores_label[i];
521                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
522                         }
523                 if(shortString < 2)
524                 for(i = 0; i < MAX_SCORE; ++i)
525                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
526                         {
527                                 f = teamscores_flags[i];
528                                 l = teamscores_label[i];
529                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
530                         }
531                 if(shortString < 1)
532                 for(i = 0; i < MAX_SCORE; ++i)
533                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
534                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
535                         {
536                                 f = teamscores_flags[i];
537                                 l = teamscores_label[i];
538                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
539                         }
540                 out = substring(out, 0, strlen(out) - 1);
541         }
542         else if((sk = teamscorekeepers[tm - 1]))
543         {
544                 for(i = 0; i < MAX_TEAMSCORE; ++i)
545                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
546                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
547                 if(shortString < 2)
548                 for(i = 0; i < MAX_TEAMSCORE; ++i)
549                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
550                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
551                 if(shortString < 1)
552                 for(i = 0; i < MAX_TEAMSCORE; ++i)
553                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
554                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
555                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
556                 out = substring(out, 0, strlen(out) - 1);
557         }
558         return out;
559 }
560
561 float PlayerTeamScore_Compare(entity p1, entity p2)
562 {
563         if(teamscores_entities_count)
564                 if(p1.team != p2.team)
565                 {
566                         entity t1, t2;
567                         t1 = teamscorekeepers[p1.team];
568                         t2 = teamscorekeepers[p2.team];
569                         return TeamScore_Compare(t1, t2);
570                 }
571         
572         return PlayerScore_Compare(p1.scorekeeper, p2.scorekeeper);
573 }
574
575 float PlayerScore_Sort(.float field)
576 {
577         entity p, plist, pprev, pbest, pbestprev;
578         float i;
579         plist = world;
580
581         FOR_EACH_CLIENT(p)
582                 p.field = 0;
583
584         FOR_EACH_PLAYER(p) if(p.scorekeeper)
585         {
586                 p.chain = plist;
587                 plist = p;
588         }
589         // Now plist points to the whole list.
590
591         i = 0;
592         while(plist)
593         {
594                 pprev = pbestprev = world;
595                 pbest = plist;
596                 for(p = plist; (p = p.chain); )
597                 {
598                         if(PlayerTeamScore_Compare(p, pbest) > 0)
599                         {
600                                 pbest = p;
601                                 pbestprev = pprev;
602                         }
603                         pprev = p;
604                 }
605
606                 // remove pbest out of the chain
607                 if(pbestprev == world)
608                         plist = pbest.chain;
609                 else
610                         pbestprev.chain = pbest.chain;
611                 pbest.chain = world;
612
613                 pbest.field = ++i;
614         }
615
616         return i;
617 }