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