]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/scores.qc
make the score adding functions return the new score (may help LMS implementation)
[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_initialized;
14
15 void Net_LinkEntity(entity e)
16 {
17         e.model = "net_entity";
18         e.modelindex = 1;
19         e.effects = EF_NODEPTHTEST | EF_LOWPRECISION;
20 }
21
22 vector ScoreField_Compare(entity t1, entity t2, .float field, float fieldflags, vector previous) // returns: cmp value, best prio
23 {
24         if(!(fieldflags & SFL_SORT_PRIO_MASK)) // column does not sort
25                 return previous;
26         if(fieldflags & SFL_SORT_PRIO_MASK < previous_y)
27                 return previous;
28         if(t1.field == t2.field)
29                 return previous;
30
31         previous_y = fieldflags & SFL_SORT_PRIO_MASK;
32
33         if(fieldflags & SFL_DECREASING)
34                 previous_x = (t1.field - t2.field);
35         else
36                 previous_x = (t2.field - t1.field);
37
38         return previous;
39 }
40
41 /*
42  * teamscore entities
43  */
44
45 float TeamScore_SendEntity(entity to)
46 {
47         float i;
48
49         WriteByte(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
50         WriteByte(MSG_ENTITY, self.team);
51         for(i = 0; i < MAX_TEAMSCORE; ++i)
52                 WriteShort(MSG_ENTITY, self.teamscores[i]);
53
54         return TRUE;
55 }
56
57 void TeamScore_Spawn(float t, string name)
58 {
59         entity ts;
60         ts = spawn();
61         ts.classname = "csqc_score_team";
62         ts.SendEntity = TeamScore_SendEntity;
63         ts.netname = name; // not used yet, FIXME
64         ts.Version = 1; // immediately send, so csqc knows about the team
65         ts.team = t;
66         Net_LinkEntity(ts);
67         teamscorekeepers[t] = ts;
68         ++teamscores_entities_count;
69 }
70
71 float TeamScore_AddToTeam(float t, float scorefield, float score)
72 {
73         entity s;
74         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
75         s = teamscorekeepers[t];
76         if(!s)
77                 error("Adding score to unknown team!");
78         if(score)
79                 s.Version += 1;
80         return (s.(teamscores[scorefield]) += score);
81 }
82
83 float TeamScore_Add(entity player, float scorefield, float score)
84 {
85         return TeamScore_AddToTeam(player.team, scorefield, score);
86 }
87
88 float TeamScore_Compare(entity t1, entity t2)
89 {
90         if(!t1 || !t2) return (!t2) - !t1;
91
92         vector result;
93         float i;
94         for(i = 0; i < MAX_TEAMSCORE; ++i)
95                 result = ScoreField_Compare(t1, t2, teamscores[i], teamscores_flags[i], result);
96         return result_x;
97 }
98
99 /*
100  * the scoreinfo entity
101  */
102
103 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
104 {
105         scores_label[i] = label;
106         scores_flags[i] = scoreflags;
107         if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
108                 scores_primary = scores[i];
109 }
110
111 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
112 {
113         teamscores_label[i] = label;
114         teamscores_flags[i] = scoreflags;
115         if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
116                 teamscores_primary = teamscores[i];
117 }
118
119 float ScoreInfo_SendEntity(entity to)
120 {
121         WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
122         float i;
123         WriteByte(MSG_ENTITY, game);
124         for(i = 0; i < MAX_SCORE; ++i)
125         {
126                 WriteString(MSG_ENTITY, scores_label[i]);
127                 WriteByte(MSG_ENTITY, scores_flags[i]);
128         }
129         for(i = 0; i < MAX_TEAMSCORE; ++i)
130         {
131                 WriteString(MSG_ENTITY, teamscores_label[i]);
132                 WriteByte(MSG_ENTITY, teamscores_flags[i]);
133         }
134         return TRUE;
135 }
136
137 void ScoreInfo_Init(float teams)
138 {
139         scores_initialized = 1;
140         if(teams >= 1)
141                 TeamScore_Spawn(COLOR_TEAM1, "Red");
142         if(teams >= 2)
143                 TeamScore_Spawn(COLOR_TEAM2, "Blue");
144         if(teams >= 3)
145                 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
146         if(teams >= 4)
147                 TeamScore_Spawn(COLOR_TEAM4, "Pink");
148         entity si;
149         si = spawn();
150         Net_LinkEntity(si);
151         si.classname = "csqc_score_info";
152         si.SendEntity = ScoreInfo_SendEntity;
153         si.Version = 1;
154 }
155
156 /*
157  * per-player score entities
158  */
159
160 float PlayerScore_SendEntity()
161 {
162         float i;
163
164         WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
165         WriteByte(MSG_ENTITY, num_for_edict(self.owner));
166         for(i = 0; i < MAX_SCORE; ++i)
167                 WriteShort(MSG_ENTITY, self.scores[i]);
168
169         return TRUE;
170 }
171
172 void PlayerScore_Clear(entity player)
173 {
174         entity sk;
175         float i;
176
177         if(!teamscores_entities_count)
178         {
179                 sk = player.scorekeeper;
180                 for(i = 0; i < MAX_SCORE; ++i)
181                         sk.(scores[i]) = 0;
182                 sk.Version += 1;
183         }
184 }
185
186 void Score_ClearAll()
187 {
188         entity p, sk;
189         float i;
190         FOR_EACH_CLIENTSLOT(p)
191         {
192                 sk = p.scorekeeper;
193                 if(!sk)
194                         continue;
195                 for(i = 0; i < MAX_SCORE; ++i)
196                         sk.(scores[i]) = 0;
197                 sk.Version += 1;
198         }
199         for(i = 0; i < 16; ++i)
200         {
201                 sk = teamscorekeepers[i];
202                 if(!sk)
203                         continue;
204                 for(i = 0; i < MAX_SCORE; ++i)
205                         sk.(teamscores[i]) = 0;
206                 sk.Version += 1;
207         }
208 }
209
210 void PlayerScore_Attach(entity player)
211 {
212         entity sk;
213         if(player.scorekeeper)
214                 error("player already has a scorekeeper");
215         sk = spawn();
216         sk.owner = player;
217         sk.Version = 1;
218         sk.SendEntity = PlayerScore_SendEntity;
219         Net_LinkEntity(sk);
220         player.scorekeeper = sk;
221 }
222
223 void PlayerScore_Detach(entity player)
224 {
225         if(!player.scorekeeper)
226                 error("player has no scorekeeper");
227         remove(player.scorekeeper);
228         player.scorekeeper = world;
229 }
230
231 float PlayerScore_Add(entity player, float scorefield, float score)
232 {
233         entity s;
234         if(!scores_initialized) return 0; // FIXME remove this when everything uses this system
235         s = player.scorekeeper;
236         if(!s)
237                 error("Adding score to unknown player!");
238         if(score)
239                 s.Version += 1;
240         return (s.(scores[scorefield]) += score);
241 }
242
243 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
244 {
245         PlayerScore_Add(player, pscorefield, score);
246         if(teamscores_entities_count) // only for teamplay
247                 TeamScore_Add(player, tscorefield, score);
248 }
249
250 float PlayerScore_Compare(entity t1, entity t2)
251 {
252         if(!t1 || !t2) return (!t2) - !t1;
253
254         vector result;
255         float i;
256         for(i = 0; i < MAX_TEAMSCORE; ++i)
257                 result = ScoreField_Compare(t1, t2, scores[i], scores_flags[i], result);
258         return result_x;
259 }
260
261 void WinningConditionHelper()
262 {
263         float c;
264         if(teamscores_entities_count)
265         {
266                 float t;
267                 WinningConditionHelper_equality = 1;
268                 WinningConditionHelper_winnerteam = 0;
269                 for(t = 1; t < 16; ++t)
270                 {
271                         c = TeamScore_Compare(teamscorekeepers[WinningConditionHelper_winnerteam], teamscorekeepers[t]);
272                         if(c == 0)
273                                 WinningConditionHelper_equality = 1;
274                         else if(c < 0)
275                         {
276                                 WinningConditionHelper_equality = 0;
277                                 WinningConditionHelper_winnerteam = t;
278                         }
279                 }
280
281                 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
282
283                 WinningConditionHelper_winner = world;
284                 if(WinningConditionHelper_equality)
285                         WinningConditionHelper_winnerteam = -1;
286         }
287         else
288         {
289                 entity p;
290                 WinningConditionHelper_equality = 1;
291                 WinningConditionHelper_winner = world;
292                 FOR_EACH_PLAYER(p)
293                 {
294                         c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
295                         if(c == 0)
296                                 WinningConditionHelper_equality = 1;
297                         else if(c < 0)
298                         {
299                                 WinningConditionHelper_equality = 0;
300                                 WinningConditionHelper_winner = p;
301                         }
302                 }
303
304                 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
305
306                 if(WinningConditionHelper_equality)
307                         WinningConditionHelper_winner = world;
308                 WinningConditionHelper_winnerteam = -1;
309         }
310 }
311
312 void Score_DebugPrint()
313 {
314         entity p, sk;
315         float i, t;
316
317         print("netname");
318         for(i = 0; i < MAX_SCORE; ++i)
319                 print(":", scores_label[i]);
320         print("\n");
321         FOR_EACH_PLAYER(p)
322         {
323                 sk = p.scorekeeper;
324                 print(p.netname);
325                 for(i = 0; i < MAX_SCORE; ++i)
326                         print(":", ftos(sk.(scores[i])));
327                 print("\n");
328         }
329
330         print("teamname");
331         for(i = 0; i < MAX_TEAMSCORE; ++i)
332                 print(":", teamscores_label[i]);
333         print("\n");
334         for(t = 0; t < 16; ++t)
335         {
336                 sk = teamscorekeepers[t];
337                 if(sk)
338                 {
339                         print(ftos(t));
340                         for(i = 0; i < MAX_TEAMSCORE; ++i)
341                                 print(":", ftos(sk.(teamscores[i])));
342                         print("\n");
343                 }
344         }
345 }