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