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