]> icculus.org git repositories - divverent/nexuiz.git/blob - data/qcsrc/server/scores.qc
CSQC part for the new score system (not really finished yet, needs cleaning)
[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_Add(entity player, float scorefield, float score)
72 {
73         entity s;
74         if(!scores_initialized) return; // FIXME remove this when everything uses this system
75         s = teamscorekeepers[player.team];
76         if(!s)
77                 error("Adding score to unknown team!");
78         s.(teamscores[scorefield]) += score;
79         s.Version += 1;
80 }
81
82 float TeamScore_Compare(entity t1, entity t2)
83 {
84         if(!t1 || !t2) return (!t2) - !t1;
85
86         vector result;
87         float i;
88         for(i = 0; i < MAX_TEAMSCORE; ++i)
89                 result = ScoreField_Compare(t1, t2, teamscores[i], teamscores_flags[i], result);
90         return result_x;
91 }
92
93 /*
94  * the scoreinfo entity
95  */
96
97 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
98 {
99         scores_label[i] = label;
100         scores_flags[i] = scoreflags;
101         if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
102                 scores_primary = scores[i];
103 }
104
105 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
106 {
107         teamscores_label[i] = label;
108         teamscores_flags[i] = scoreflags;
109         if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
110                 teamscores_primary = teamscores[i];
111 }
112
113 float ScoreInfo_SendEntity(entity to)
114 {
115         WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
116         float i;
117         for(i = 0; i < MAX_SCORE; ++i)
118         {
119                 WriteString(MSG_ENTITY, scores_label[i]);
120                 WriteByte(MSG_ENTITY, scores_flags[i]);
121         }
122         for(i = 0; i < MAX_TEAMSCORE; ++i)
123         {
124                 WriteString(MSG_ENTITY, teamscores_label[i]);
125                 WriteByte(MSG_ENTITY, teamscores_flags[i]);
126         }
127         return TRUE;
128 }
129
130 void ScoreInfo_Init(float teams)
131 {
132         scores_initialized = 1;
133         if(teams >= 1)
134                 TeamScore_Spawn(COLOR_TEAM1, "Red");
135         if(teams >= 2)
136                 TeamScore_Spawn(COLOR_TEAM2, "Blue");
137         if(teams >= 3)
138                 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
139         if(teams >= 4)
140                 TeamScore_Spawn(COLOR_TEAM4, "Pink");
141         entity si;
142         si = spawn();
143         Net_LinkEntity(si);
144         si.classname = "csqc_score_info";
145         si.SendEntity = ScoreInfo_SendEntity;
146         si.Version = 1;
147 }
148
149 /*
150  * per-player score entities
151  */
152
153 float PlayerScore_SendEntity()
154 {
155         float i;
156
157         WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
158         WriteByte(MSG_ENTITY, num_for_edict(self.owner));
159         for(i = 0; i < MAX_SCORE; ++i)
160                 WriteShort(MSG_ENTITY, self.scores[i]);
161
162         return TRUE;
163 }
164
165 void PlayerScore_Clear(entity player)
166 {
167         entity sk;
168         float i;
169
170         if(!teamscores_entities_count)
171         {
172                 sk = player.scorekeeper;
173                 for(i = 0; i < MAX_SCORE; ++i)
174                         sk.(scores[i]) = 0;
175                 sk.Version += 1;
176         }
177 }
178
179 void Score_ClearAll()
180 {
181         entity p, sk;
182         float i;
183         FOR_EACH_CLIENTSLOT(p)
184         {
185                 sk = p.scorekeeper;
186                 if(!sk)
187                         continue;
188                 for(i = 0; i < MAX_SCORE; ++i)
189                         sk.(scores[i]) = 0;
190                 sk.Version += 1;
191         }
192         for(i = 0; i < 16; ++i)
193         {
194                 sk = teamscorekeepers[i];
195                 if(!sk)
196                         continue;
197                 for(i = 0; i < MAX_SCORE; ++i)
198                         sk.(teamscores[i]) = 0;
199                 sk.Version += 1;
200         }
201 }
202
203 float PlayerScore_IsValid(entity player)
204 {
205         if(!player.scorekeeper)
206                 return FALSE;
207
208         return TRUE;
209 }
210
211 void PlayerScore_Attach(entity player)
212 {
213         entity sk;
214         if(player.scorekeeper)
215                 error("player already has a scorekeeper");
216         sk = spawn();
217         sk.owner = player;
218         sk.Version = 1;
219         sk.SendEntity = PlayerScore_SendEntity;
220         Net_LinkEntity(sk);
221         player.scorekeeper = sk;
222 }
223
224 void PlayerScore_Detach(entity player)
225 {
226         if(!player.scorekeeper)
227                 error("player has no scorekeeper");
228         remove(player.scorekeeper);
229         player.scorekeeper = world;
230 }
231
232 void PlayerScore_Add(entity player, float scorefield, float score)
233 {
234         entity s;
235         if(!scores_initialized) return; // FIXME remove this when everything uses this system
236         s = player.scorekeeper;
237         if(!s)
238                 error("Adding score to unknown player!");
239         s.(scores[scorefield]) += score;
240         s.Version += 1;
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 }