1 .float scores[MAX_SCORE];
2 .float teamscores[MAX_TEAMSCORE];
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;
15 void Net_LinkEntity(entity e)
17 e.model = "net_entity";
19 e.effects = EF_NODEPTHTEST | EF_LOWPRECISION;
22 vector ScoreField_Compare(entity t1, entity t2, .float field, float fieldflags, vector previous) // returns: cmp value, best prio
24 if(!(fieldflags & SFL_SORT_PRIO_MASK)) // column does not sort
26 if(fieldflags & SFL_SORT_PRIO_MASK < previous_y)
28 if(t1.field == t2.field)
31 previous_y = fieldflags & SFL_SORT_PRIO_MASK;
33 if(fieldflags & SFL_DECREASING)
34 previous_x = (t1.field - t2.field);
36 previous_x = (t2.field - t1.field);
45 float TeamScore_SendEntity(entity to)
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]);
57 void TeamScore_Spawn(float t, string name)
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
67 teamscorekeepers[t] = ts;
68 ++teamscores_entities_count;
71 void TeamScore_AddToTeam(float t, float scorefield, float score)
74 if(!scores_initialized) return; // FIXME remove this when everything uses this system
75 s = teamscorekeepers[t];
77 error("Adding score to unknown team!");
78 s.(teamscores[scorefield]) += score;
82 void TeamScore_Add(entity player, float scorefield, float score)
84 TeamScore_AddToTeam(player.team, scorefield, score);
87 float TeamScore_Compare(entity t1, entity t2)
89 if(!t1 || !t2) return (!t2) - !t1;
93 for(i = 0; i < MAX_TEAMSCORE; ++i)
94 result = ScoreField_Compare(t1, t2, teamscores[i], teamscores_flags[i], result);
99 * the scoreinfo entity
102 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
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];
110 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
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];
118 float ScoreInfo_SendEntity(entity to)
120 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
122 WriteByte(MSG_ENTITY, game);
123 for(i = 0; i < MAX_SCORE; ++i)
125 WriteString(MSG_ENTITY, scores_label[i]);
126 WriteByte(MSG_ENTITY, scores_flags[i]);
128 for(i = 0; i < MAX_TEAMSCORE; ++i)
130 WriteString(MSG_ENTITY, teamscores_label[i]);
131 WriteByte(MSG_ENTITY, teamscores_flags[i]);
136 void ScoreInfo_Init(float teams)
138 scores_initialized = 1;
140 TeamScore_Spawn(COLOR_TEAM1, "Red");
142 TeamScore_Spawn(COLOR_TEAM2, "Blue");
144 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
146 TeamScore_Spawn(COLOR_TEAM4, "Pink");
150 si.classname = "csqc_score_info";
151 si.SendEntity = ScoreInfo_SendEntity;
156 * per-player score entities
159 float PlayerScore_SendEntity()
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]);
171 void PlayerScore_Clear(entity player)
176 if(!teamscores_entities_count)
178 sk = player.scorekeeper;
179 for(i = 0; i < MAX_SCORE; ++i)
185 void Score_ClearAll()
189 FOR_EACH_CLIENTSLOT(p)
194 for(i = 0; i < MAX_SCORE; ++i)
198 for(i = 0; i < 16; ++i)
200 sk = teamscorekeepers[i];
203 for(i = 0; i < MAX_SCORE; ++i)
204 sk.(teamscores[i]) = 0;
209 void PlayerScore_Attach(entity player)
212 if(player.scorekeeper)
213 error("player already has a scorekeeper");
217 sk.SendEntity = PlayerScore_SendEntity;
219 player.scorekeeper = sk;
222 void PlayerScore_Detach(entity player)
224 if(!player.scorekeeper)
225 error("player has no scorekeeper");
226 remove(player.scorekeeper);
227 player.scorekeeper = world;
230 void PlayerScore_Add(entity player, float scorefield, float score)
233 if(!scores_initialized) return; // FIXME remove this when everything uses this system
234 s = player.scorekeeper;
236 error("Adding score to unknown player!");
237 s.(scores[scorefield]) += score;
241 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
243 PlayerScore_Add(player, pscorefield, score);
244 if(teamscores_entities_count) // only for teamplay
245 TeamScore_Add(player, tscorefield, score);
248 float PlayerScore_Compare(entity t1, entity t2)
250 if(!t1 || !t2) return (!t2) - !t1;
254 for(i = 0; i < MAX_TEAMSCORE; ++i)
255 result = ScoreField_Compare(t1, t2, scores[i], scores_flags[i], result);
259 void WinningConditionHelper()
262 if(teamscores_entities_count)
265 WinningConditionHelper_equality = 1;
266 WinningConditionHelper_winnerteam = 0;
267 for(t = 1; t < 16; ++t)
269 c = TeamScore_Compare(teamscorekeepers[WinningConditionHelper_winnerteam], teamscorekeepers[t]);
271 WinningConditionHelper_equality = 1;
274 WinningConditionHelper_equality = 0;
275 WinningConditionHelper_winnerteam = t;
279 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
281 WinningConditionHelper_winner = world;
282 if(WinningConditionHelper_equality)
283 WinningConditionHelper_winnerteam = -1;
288 WinningConditionHelper_equality = 1;
289 WinningConditionHelper_winner = world;
292 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
294 WinningConditionHelper_equality = 1;
297 WinningConditionHelper_equality = 0;
298 WinningConditionHelper_winner = p;
302 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
304 if(WinningConditionHelper_equality)
305 WinningConditionHelper_winner = world;
306 WinningConditionHelper_winnerteam = -1;
310 void Score_DebugPrint()
316 for(i = 0; i < MAX_SCORE; ++i)
317 print(":", scores_label[i]);
323 for(i = 0; i < MAX_SCORE; ++i)
324 print(":", ftos(sk.(scores[i])));
329 for(i = 0; i < MAX_TEAMSCORE; ++i)
330 print(":", teamscores_label[i]);
332 for(t = 0; t < 16; ++t)
334 sk = teamscorekeepers[t];
338 for(i = 0; i < MAX_TEAMSCORE; ++i)
339 print(":", ftos(sk.(teamscores[i])));