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