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