]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/scores.qc
add generic scores for all other game modes (hope it somewhat works)
[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_DECREASING)
33                 previous_x = (t1.field - t2.field);
34         else
35                 previous_x = (t2.field - t1.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);
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] = 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         s = teamscorekeepers[t];
75         if(!s)
76                 error("Adding score to unknown team!");
77         if(score)
78                 s.Version += 1;
79         return (s.(teamscores[scorefield]) += score);
80 }
81
82 float TeamScore_Add(entity player, float scorefield, float score)
83 {
84         return TeamScore_AddToTeam(player.team, scorefield, score);
85 }
86
87 float TeamScore_Compare(entity t1, entity t2)
88 {
89         if(!t1 || !t2) return (!t2) - !t1;
90
91         vector result;
92         float i;
93         for(i = 0; i < MAX_TEAMSCORE; ++i)
94         {
95                 var .float f;
96                 f = teamscores[i];
97                 result = ScoreField_Compare(t1, t2, f, teamscores_flags[i], result);
98         }
99         return result_x;
100 }
101
102 /*
103  * the scoreinfo entity
104  */
105
106 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
107 {
108         scores_label[i] = label;
109         scores_flags[i] = scoreflags;
110         if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
111                 scores_primary = scores[i];
112 }
113
114 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
115 {
116         teamscores_label[i] = label;
117         teamscores_flags[i] = scoreflags;
118         if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
119                 teamscores_primary = teamscores[i];
120 }
121
122 float ScoreInfo_SendEntity(entity to)
123 {
124         WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
125         float i;
126         WriteByte(MSG_ENTITY, game);
127         for(i = 0; i < MAX_SCORE; ++i)
128         {
129                 WriteString(MSG_ENTITY, scores_label[i]);
130                 WriteByte(MSG_ENTITY, scores_flags[i]);
131         }
132         for(i = 0; i < MAX_TEAMSCORE; ++i)
133         {
134                 WriteString(MSG_ENTITY, teamscores_label[i]);
135                 WriteByte(MSG_ENTITY, teamscores_flags[i]);
136         }
137         return TRUE;
138 }
139
140 void ScoreInfo_Init(float teams)
141 {
142         scores_initialized = 1;
143         if(teams >= 1)
144                 TeamScore_Spawn(COLOR_TEAM1, "Red");
145         if(teams >= 2)
146                 TeamScore_Spawn(COLOR_TEAM2, "Blue");
147         if(teams >= 3)
148                 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
149         if(teams >= 4)
150                 TeamScore_Spawn(COLOR_TEAM4, "Pink");
151         entity si;
152         si = spawn();
153         Net_LinkEntity(si);
154         si.classname = "csqc_score_info";
155         si.SendEntity = ScoreInfo_SendEntity;
156         si.Version = 1;
157 }
158
159 /*
160  * per-player score entities
161  */
162
163 float PlayerScore_SendEntity()
164 {
165         float i;
166
167         WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
168         WriteByte(MSG_ENTITY, num_for_edict(self.owner));
169         for(i = 0; i < MAX_SCORE; ++i)
170                 WriteShort(MSG_ENTITY, self.scores[i]);
171
172         return TRUE;
173 }
174
175 void PlayerScore_Clear(entity player)
176 {
177         entity sk;
178         float i;
179
180         if(!teamscores_entities_count)
181         {
182                 sk = player.scorekeeper;
183                 for(i = 0; i < MAX_SCORE; ++i)
184                         sk.(scores[i]) = 0;
185                 sk.Version += 1;
186         }
187 }
188
189 void Score_ClearAll()
190 {
191         entity p, sk;
192         float i;
193         FOR_EACH_CLIENTSLOT(p)
194         {
195                 sk = p.scorekeeper;
196                 if(!sk)
197                         continue;
198                 for(i = 0; i < MAX_SCORE; ++i)
199                         sk.(scores[i]) = 0;
200                 sk.Version += 1;
201         }
202         for(i = 0; i < 16; ++i)
203         {
204                 sk = teamscorekeepers[i];
205                 if(!sk)
206                         continue;
207                 for(i = 0; i < MAX_SCORE; ++i)
208                         sk.(teamscores[i]) = 0;
209                 sk.Version += 1;
210         }
211 }
212
213 void PlayerScore_Attach(entity player)
214 {
215         entity sk;
216         if(player.scorekeeper)
217                 error("player already has a scorekeeper");
218         sk = spawn();
219         sk.owner = player;
220         sk.Version = 1;
221         sk.SendEntity = PlayerScore_SendEntity;
222         Net_LinkEntity(sk);
223         player.scorekeeper = sk;
224 }
225
226 void PlayerScore_Detach(entity player)
227 {
228         if(!player.scorekeeper)
229                 error("player has no scorekeeper");
230         remove(player.scorekeeper);
231         player.scorekeeper = world;
232 }
233
234 float PlayerScore_Add(entity player, float scorefield, float score)
235 {
236         entity s;
237         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
238         s = player.scorekeeper;
239         if(!s)
240                 error("Adding score to unknown player!");
241         if(score)
242                 s.Version += 1;
243         return (s.(scores[scorefield]) += score);
244 }
245
246 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
247 {
248         PlayerScore_Add(player, pscorefield, score);
249         if(teamscores_entities_count) // only for teamplay
250                 TeamScore_Add(player, tscorefield, score);
251 }
252
253 float PlayerScore_Compare(entity t1, entity t2)
254 {
255         if(!t1 || !t2) return (!t2) - !t1;
256
257         vector result;
258         float i;
259         for(i = 0; i < MAX_SCORE; ++i)
260         {
261                 var .float f;
262                 f = scores[i];
263                 result = ScoreField_Compare(t1, t2, f, scores_flags[i], result);
264         }
265         return result_x;
266 }
267
268 void WinningConditionHelper()
269 {
270         float c;
271         if(teamscores_entities_count)
272         {
273                 float t;
274                 WinningConditionHelper_equality = 1;
275                 WinningConditionHelper_winnerteam = 0;
276                 for(t = 1; t < 16; ++t)
277                 {
278                         c = TeamScore_Compare(teamscorekeepers[WinningConditionHelper_winnerteam], teamscorekeepers[t]);
279                         if(c == 0)
280                                 WinningConditionHelper_equality = 1;
281                         else if(c < 0)
282                         {
283                                 WinningConditionHelper_equality = 0;
284                                 WinningConditionHelper_winnerteam = t;
285                         }
286                 }
287
288                 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
289
290                 WinningConditionHelper_winner = world;
291                 if(WinningConditionHelper_equality)
292                         WinningConditionHelper_winnerteam = -1;
293         }
294         else
295         {
296                 entity p;
297                 WinningConditionHelper_equality = 1;
298                 WinningConditionHelper_winner = world;
299                 FOR_EACH_PLAYER(p)
300                 {
301                         c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
302                         if(c == 0)
303                                 WinningConditionHelper_equality = 1;
304                         else if(c < 0)
305                         {
306                                 WinningConditionHelper_equality = 0;
307                                 WinningConditionHelper_winner = p;
308                         }
309                 }
310
311                 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
312
313                 if(WinningConditionHelper_equality)
314                         WinningConditionHelper_winner = world;
315                 WinningConditionHelper_winnerteam = -1;
316         }
317 }
318
319 void Score_DebugPrint()
320 {
321         entity p, sk;
322         float i, t;
323
324         print("netname");
325         for(i = 0; i < MAX_SCORE; ++i)
326                 print(":", scores_label[i]);
327         print("\n");
328         FOR_EACH_PLAYER(p)
329         {
330                 sk = p.scorekeeper;
331                 print(p.netname);
332                 for(i = 0; i < MAX_SCORE; ++i)
333                         print(":", ftos(sk.(scores[i])));
334                 print("\n");
335         }
336
337         print("teamname");
338         for(i = 0; i < MAX_TEAMSCORE; ++i)
339                 print(":", teamscores_label[i]);
340         print("\n");
341         for(t = 0; t < 16; ++t)
342         {
343                 sk = teamscorekeepers[t];
344                 if(sk)
345                 {
346                         print(ftos(t));
347                         for(i = 0; i < MAX_TEAMSCORE; ++i)
348                                 print(":", ftos(sk.(teamscores[i])));
349                         print("\n");
350                 }
351         }
352 }