]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/scores.qc
add team scores display; csqc .team is NOT svqc .team, that sucks! (csqc .team is...
[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
187         sk = player.scorekeeper;
188         for(i = 0; i < MAX_SCORE; ++i)
189                 sk.(scores[i]) = 0;
190         sk.Version += 1;
191 }
192
193 void Score_ClearAll()
194 {
195         entity p, sk;
196         float i;
197         FOR_EACH_CLIENTSLOT(p)
198         {
199                 sk = p.scorekeeper;
200                 if(!sk)
201                         continue;
202                 for(i = 0; i < MAX_SCORE; ++i)
203                         sk.(scores[i]) = 0;
204                 sk.Version += 1;
205         }
206         for(i = 0; i < 16; ++i)
207         {
208                 sk = teamscorekeepers[i];
209                 if(!sk)
210                         continue;
211                 for(i = 0; i < MAX_SCORE; ++i)
212                         sk.(teamscores[i]) = 0;
213                 sk.Version += 1;
214         }
215 }
216
217 void PlayerScore_Attach(entity player)
218 {
219         entity sk;
220         if(player.scorekeeper)
221                 error("player already has a scorekeeper");
222         sk = spawn();
223         sk.owner = player;
224         sk.Version = 1;
225         sk.SendEntity = PlayerScore_SendEntity;
226         Net_LinkEntity(sk);
227         player.scorekeeper = sk;
228 }
229
230 void PlayerScore_Detach(entity player)
231 {
232         if(!player.scorekeeper)
233                 error("player has no scorekeeper");
234         remove(player.scorekeeper);
235         player.scorekeeper = world;
236 }
237
238 float PlayerScore_Add(entity player, float scorefield, float score)
239 {
240         entity s;
241         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
242         s = player.scorekeeper;
243         if(!s)
244                 error("Adding score to unknown player!");
245         if(score)
246                 s.Version += 1;
247         return (s.(scores[scorefield]) += score);
248 }
249
250 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
251 {
252         PlayerScore_Add(player, pscorefield, score);
253         if(teamscores_entities_count) // only for teamplay
254                 TeamScore_Add(player, tscorefield, score);
255 }
256
257 float PlayerScore_Compare(entity t1, entity t2)
258 {
259         if(!t1 || !t2) return (!t2) - !t1;
260
261         vector result;
262         float i;
263         for(i = 0; i < MAX_SCORE; ++i)
264         {
265                 var .float f;
266                 f = scores[i];
267                 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
268         }
269         return result_x;
270 }
271
272 void WinningConditionHelper()
273 {
274         float c;
275         if(teamscores_entities_count)
276         {
277                 float t;
278                 WinningConditionHelper_equality = 1;
279                 WinningConditionHelper_winnerteam = 0;
280                 for(t = 1; t < 16; ++t)
281                 {
282                         c = TeamScore_Compare(teamscorekeepers[WinningConditionHelper_winnerteam], teamscorekeepers[t]);
283                         if(c == 0)
284                                 WinningConditionHelper_equality = 1;
285                         else if(c < 0)
286                         {
287                                 WinningConditionHelper_equality = 0;
288                                 WinningConditionHelper_winnerteam = t;
289                         }
290                 }
291
292                 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
293
294                 WinningConditionHelper_winner = world;
295                 if(WinningConditionHelper_equality)
296                         WinningConditionHelper_winnerteam = -1;
297         }
298         else
299         {
300                 entity p;
301                 WinningConditionHelper_equality = 1;
302                 WinningConditionHelper_winner = world;
303                 FOR_EACH_PLAYER(p)
304                 {
305                         c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
306                         if(c == 0)
307                                 WinningConditionHelper_equality = 1;
308                         else if(c < 0)
309                         {
310                                 WinningConditionHelper_equality = 0;
311                                 WinningConditionHelper_winner = p;
312                         }
313                 }
314
315                 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
316
317                 if(WinningConditionHelper_equality)
318                         WinningConditionHelper_winner = world;
319                 WinningConditionHelper_winnerteam = -1;
320         }
321 }
322
323 void Score_DebugPrint()
324 {
325         entity p, sk;
326         float i, t;
327
328         print("netname");
329         for(i = 0; i < MAX_SCORE; ++i)
330                 print(":", scores_label[i]);
331         print("\n");
332         FOR_EACH_PLAYER(p)
333         {
334                 sk = p.scorekeeper;
335                 print(p.netname);
336                 for(i = 0; i < MAX_SCORE; ++i)
337                         print(":", ftos(sk.(scores[i])));
338                 print("\n");
339         }
340
341         print("teamname");
342         for(i = 0; i < MAX_TEAMSCORE; ++i)
343                 print(":", teamscores_label[i]);
344         print("\n");
345         for(t = 0; t < 16; ++t)
346         {
347                 sk = teamscorekeepers[t];
348                 if(sk)
349                 {
350                         print(ftos(t));
351                         for(i = 0; i < MAX_TEAMSCORE; ++i)
352                                 print(":", ftos(sk.(teamscores[i])));
353                         print("\n");
354                 }
355         }
356 }