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_Add(entity player, float scorefield, float score)
74 if(!scores_initialized) return; // FIXME remove this when everything uses this system
75 s = teamscorekeepers[player.team];
77 error("Adding score to unknown team!");
78 s.(teamscores[scorefield]) += score;
82 float TeamScore_Compare(entity t1, entity t2)
84 if(!t1 || !t2) return (!t2) - !t1;
88 for(i = 0; i < MAX_TEAMSCORE; ++i)
89 result = ScoreField_Compare(t1, t2, teamscores[i], teamscores_flags[i], result);
94 * the scoreinfo entity
97 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
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];
105 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
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];
113 float ScoreInfo_SendEntity(entity to)
115 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
117 WriteByte(MSG_ENTITY, game);
118 for(i = 0; i < MAX_SCORE; ++i)
120 WriteString(MSG_ENTITY, scores_label[i]);
121 WriteByte(MSG_ENTITY, scores_flags[i]);
123 for(i = 0; i < MAX_TEAMSCORE; ++i)
125 WriteString(MSG_ENTITY, teamscores_label[i]);
126 WriteByte(MSG_ENTITY, teamscores_flags[i]);
131 void ScoreInfo_Init(float teams)
133 scores_initialized = 1;
135 TeamScore_Spawn(COLOR_TEAM1, "Red");
137 TeamScore_Spawn(COLOR_TEAM2, "Blue");
139 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
141 TeamScore_Spawn(COLOR_TEAM4, "Pink");
145 si.classname = "csqc_score_info";
146 si.SendEntity = ScoreInfo_SendEntity;
151 * per-player score entities
154 float PlayerScore_SendEntity()
158 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
159 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
160 for(i = 0; i < MAX_SCORE; ++i)
161 WriteShort(MSG_ENTITY, self.scores[i]);
166 void PlayerScore_Clear(entity player)
171 if(!teamscores_entities_count)
173 sk = player.scorekeeper;
174 for(i = 0; i < MAX_SCORE; ++i)
180 void Score_ClearAll()
184 FOR_EACH_CLIENTSLOT(p)
189 for(i = 0; i < MAX_SCORE; ++i)
193 for(i = 0; i < 16; ++i)
195 sk = teamscorekeepers[i];
198 for(i = 0; i < MAX_SCORE; ++i)
199 sk.(teamscores[i]) = 0;
204 void PlayerScore_Attach(entity player)
207 if(player.scorekeeper)
208 error("player already has a scorekeeper");
212 sk.SendEntity = PlayerScore_SendEntity;
214 player.scorekeeper = sk;
217 void PlayerScore_Detach(entity player)
219 if(!player.scorekeeper)
220 error("player has no scorekeeper");
221 remove(player.scorekeeper);
222 player.scorekeeper = world;
225 void PlayerScore_Add(entity player, float scorefield, float score)
228 if(!scores_initialized) return; // FIXME remove this when everything uses this system
229 s = player.scorekeeper;
231 error("Adding score to unknown player!");
232 s.(scores[scorefield]) += score;
236 void PlayerTeamScore_Add(entity player, float pscorefield, float tscorefield, float score)
238 PlayerScore_Add(player, pscorefield, score);
239 if(teamscores_entities_count) // only for teamplay
240 TeamScore_Add(player, tscorefield, score);
243 float PlayerScore_Compare(entity t1, entity t2)
245 if(!t1 || !t2) return (!t2) - !t1;
249 for(i = 0; i < MAX_TEAMSCORE; ++i)
250 result = ScoreField_Compare(t1, t2, scores[i], scores_flags[i], result);
254 void WinningConditionHelper()
257 if(teamscores_entities_count)
260 WinningConditionHelper_equality = 1;
261 WinningConditionHelper_winnerteam = 0;
262 for(t = 1; t < 16; ++t)
264 c = TeamScore_Compare(teamscorekeepers[WinningConditionHelper_winnerteam], teamscorekeepers[t]);
266 WinningConditionHelper_equality = 1;
269 WinningConditionHelper_equality = 0;
270 WinningConditionHelper_winnerteam = t;
274 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
276 WinningConditionHelper_winner = world;
277 if(WinningConditionHelper_equality)
278 WinningConditionHelper_winnerteam = -1;
283 WinningConditionHelper_equality = 1;
284 WinningConditionHelper_winner = world;
287 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
289 WinningConditionHelper_equality = 1;
292 WinningConditionHelper_equality = 0;
293 WinningConditionHelper_winner = p;
297 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
299 if(WinningConditionHelper_equality)
300 WinningConditionHelper_winner = world;
301 WinningConditionHelper_winnerteam = -1;
305 void Score_DebugPrint()
311 for(i = 0; i < MAX_SCORE; ++i)
312 print(":", scores_label[i]);
318 for(i = 0; i < MAX_SCORE; ++i)
319 print(":", ftos(sk.(scores[i])));
324 for(i = 0; i < MAX_TEAMSCORE; ++i)
325 print(":", teamscores_label[i]);
327 for(t = 0; t < 16; ++t)
329 sk = teamscorekeepers[t];
333 for(i = 0; i < MAX_TEAMSCORE; ++i)
334 print(":", ftos(sk.(teamscores[i])));