]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/scores.qc
fix Qualifying mode for race; make teleporters able to trigger a timing event
[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                 if(teamscores_flags_primary & SFL_ZERO_IS_WORST)
333                         if(WinningConditionHelper_topscore == 0)
334                         {
335                                 if(WinningConditionHelper_lowerisbetter)
336                                         WinningConditionHelper_topscore = 999999999;
337                                 else
338                                         WinningConditionHelper_topscore = -999999999;
339                         }
340                 if(teamscores_flags_primary & SFL_TIME)
341                         WinningConditionHelper_topscore /= 10;
342
343                 WinningConditionHelper_winner = world;
344                 if(WinningConditionHelper_equality)
345                         WinningConditionHelper_winnerteam = -1;
346                 else
347                         ++WinningConditionHelper_winnerteam; // map to Nexuiz team numbers (as opposed to colors)
348         }
349         else
350         {
351                 WinningConditionHelper_equality = 1;
352                 WinningConditionHelper_winner = world;
353                 FOR_EACH_PLAYER(p)
354                 {
355                         c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
356                         if(c == 0)
357                                 WinningConditionHelper_equality = 1;
358                         else if(c < 0)
359                         {
360                                 WinningConditionHelper_equality = 0;
361                                 WinningConditionHelper_winner = p;
362                         }
363                 }
364
365                 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
366                 WinningConditionHelper_lowerisbetter = (scores_flags_primary & SFL_LOWER_IS_BETTER);
367                 if(scores_flags_primary & SFL_ZERO_IS_WORST)
368                         if(WinningConditionHelper_topscore == 0)
369                         {
370                                 if(WinningConditionHelper_lowerisbetter)
371                                         WinningConditionHelper_topscore = 999999999;
372                                 else
373                                         WinningConditionHelper_topscore = -999999999;
374                         }
375                 if(teamscores_flags_primary & SFL_TIME)
376                         WinningConditionHelper_topscore /= 10;
377
378                 if(WinningConditionHelper_equality)
379                         WinningConditionHelper_winner = world;
380                 WinningConditionHelper_winnerteam = -1;
381         }
382
383         if(worldstatus)
384                 strunzone(worldstatus);
385         worldstatus = strzone(s);
386
387         FOR_EACH_CLIENT(p)
388         {
389                 /* this breaks qstat :( find a way to make qstat parse this at least as an int first
390                 s = GetPlayerScoreString(p, 1);
391                 if(clienttype(p) == CLIENTTYPE_REAL)
392                         s = strcat(s, ":human");
393                 else
394                         s = strcat(s, ":bot");
395                 if(p.classname == "player" || g_arena || g_lms)
396                         s = strcat(s, ":", ftos(p.team));
397                 else
398                         s = strcat(s, ":spectator");
399                 */
400                 if(p.classname == "player" || g_arena || g_lms)
401                         s = "-666";
402                 else
403                         s = GetPlayerScoreString(p, 2);
404
405                 if(p.clientstatus)
406                         strunzone(p.clientstatus);
407                 p.clientstatus = strzone(s);
408         }
409 }
410
411 void Score_DebugPrint()
412 {
413         entity p, sk;
414         float i, t;
415
416         print("netname");
417         for(i = 0; i < MAX_SCORE; ++i)
418                 print(":", scores_label[i]);
419         print("\n");
420         FOR_EACH_PLAYER(p)
421         {
422                 sk = p.scorekeeper;
423                 print(p.netname);
424                 for(i = 0; i < MAX_SCORE; ++i)
425                         print(":", ftos(sk.(scores[i])));
426                 print("\n");
427         }
428
429         print("teamname");
430         for(i = 0; i < MAX_TEAMSCORE; ++i)
431                 print(":", teamscores_label[i]);
432         print("\n");
433         for(t = 0; t < 16; ++t)
434         {
435                 sk = teamscorekeepers[t];
436                 if(sk)
437                 {
438                         print(ftos(t));
439                         for(i = 0; i < MAX_TEAMSCORE; ++i)
440                                 print(":", ftos(sk.(teamscores[i])));
441                         print("\n");
442                 }
443         }
444 }
445
446 string GetScoreLogLabel(string label, float fl)
447 {
448         if(fl & SFL_LOWER_IS_BETTER)
449                 label = strcat(label, "<");
450         if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
451                 label = strcat(label, "!!");
452         else if(fl & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
453                 label = strcat(label, "!");
454         return label;
455 }
456
457 string GetPlayerScoreString(entity pl, float shortString)
458 {
459         string out;
460         entity sk;
461         float i, f;
462         string l;
463
464         out = "";
465         if(!pl)
466         {
467                 // label
468                 for(i = 0; i < MAX_SCORE; ++i)
469                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
470                         {
471                                 f = scores_flags[i];
472                                 l = scores_label[i];
473                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
474                         }
475                 if(shortString < 2)
476                 for(i = 0; i < MAX_SCORE; ++i)
477                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
478                         {
479                                 f = scores_flags[i];
480                                 l = scores_label[i];
481                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
482                         }
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                         {
488                                 f = scores_flags[i];
489                                 l = scores_label[i];
490                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
491                         }
492                 out = substring(out, 0, strlen(out) - 1);
493         }
494         else if((sk = pl.scorekeeper))
495         {
496                 for(i = 0; i < MAX_SCORE; ++i)
497                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
498                                 out = strcat(out, ftos(sk.(scores[i])), ",");
499                 if(shortString < 2)
500                 for(i = 0; i < MAX_SCORE; ++i)
501                         if(scores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
502                                 out = strcat(out, ftos(sk.(scores[i])), ",");
503                 if(shortString < 1)
504                 for(i = 0; i < MAX_SCORE; ++i)
505                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
506                         if(scores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
507                                 out = strcat(out, ftos(sk.(scores[i])), ",");
508                 out = substring(out, 0, strlen(out) - 1);
509         }
510         return out;
511 }
512
513 string GetTeamScoreString(float tm, float shortString)
514 {
515         string out;
516         entity sk;
517         float i, f;
518         string l;
519
520         out = "";
521         if(tm == 0)
522         {
523                 // label
524                 for(i = 0; i < MAX_SCORE; ++i)
525                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
526                         {
527                                 f = teamscores_flags[i];
528                                 l = teamscores_label[i];
529                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
530                         }
531                 if(shortString < 2)
532                 for(i = 0; i < MAX_SCORE; ++i)
533                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
534                         {
535                                 f = teamscores_flags[i];
536                                 l = teamscores_label[i];
537                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
538                         }
539                 if(shortString < 1)
540                 for(i = 0; i < MAX_SCORE; ++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                         {
544                                 f = teamscores_flags[i];
545                                 l = teamscores_label[i];
546                                 out = strcat(out, GetScoreLogLabel(l, f), ",");
547                         }
548                 out = substring(out, 0, strlen(out) - 1);
549         }
550         else if((sk = teamscorekeepers[tm - 1]))
551         {
552                 for(i = 0; i < MAX_TEAMSCORE; ++i)
553                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
554                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
555                 if(shortString < 2)
556                 for(i = 0; i < MAX_TEAMSCORE; ++i)
557                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_SECONDARY)
558                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
559                 if(shortString < 1)
560                 for(i = 0; i < MAX_TEAMSCORE; ++i)
561                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_PRIMARY)
562                         if(teamscores_flags[i] & SFL_SORT_PRIO_MASK != SFL_SORT_PRIO_SECONDARY)
563                                 out = strcat(out, ftos(sk.(teamscores[i])), ",");
564                 out = substring(out, 0, strlen(out) - 1);
565         }
566         return out;
567 }