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;
15 .float(entity to) SendEntity;
17 vector ScoreField_Compare(entity t1, entity t2, .float field, float fieldflags, vector previous) // returns: cmp value, best prio
19 if(fieldflags & SFL_SORT_PRIO_MASK < previous_y)
21 if(t1.field == t2.field)
24 previous_y = fieldflags & SFL_SORT_PRIO_MASK;
26 if(fieldflags & SFL_DECREASING)
27 previous_x = (t1.field - t2.field);
29 previous_x = (t2.field - t1.field);
38 void TeamScore_SendEntity(entity to)
42 WriteByte(MSG_ENTITY, ENT_CLIENT_TEAMSCORES);
43 WriteByte(MSG_ENTITY, self.team);
44 for(i = 0; i < MAX_TEAMSCORE; ++i)
45 WriteShort(MSG_ENTITY, self.teamscores[i]);
48 void TeamScore_Spawn(float t, string name)
52 ts.classname = "csqc_score_team";
53 ts.SendEntity = TeamScore_SendEntity;
54 ts.netname = name; // not used yet, FIXME
55 ts.Version = 1; // immediately send, so csqc knows about the team
56 teamscorekeepers[t] = ts;
57 ++teamscores_entities_count;
60 void TeamScore_Add(entity player, float scorefield, float score)
63 s = teamscorekeepers[player.team];
65 error("Adding score to unknown team!");
66 s.(teamscores[scorefield]) += score;
70 float TeamScore_Compare(entity t1, entity t2)
72 if(!t1 || !t2) return (!t2) - !t1;
76 for(i = 0; i < MAX_TEAMSCORE; ++i)
77 result = ScoreField_Compare(t1, t2, teamscores[i], teamscores_flags[i], result);
82 * the scoreinfo entity
85 void ScoreInfo_SetLabel_PlayerScore(float i, string label, float scoreflags)
87 scores_label[i] = label;
88 scores_flags[i] = scoreflags;
89 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
90 scores_primary = scores[i];
93 void ScoreInfo_SetLabel_TeamScore(float i, string label, float scoreflags)
95 teamscores_label[i] = label;
96 teamscores_flags[i] = scoreflags;
97 if(scoreflags & SFL_SORT_PRIO_MASK == SFL_SORT_PRIO_PRIMARY)
98 teamscores_primary = teamscores[i];
101 void ScoreInfo_SendEntity(entity to)
103 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES_INFO);
105 for(i = 0; i < MAX_SCORE; ++i)
107 WriteString(MSG_ENTITY, scores_label[i]);
108 WriteByte(MSG_ENTITY, scores_flags[i]);
110 for(i = 0; i < MAX_TEAMSCORE; ++i)
112 WriteString(MSG_ENTITY, teamscores_label[i]);
113 WriteByte(MSG_ENTITY, teamscores_flags[i]);
117 void ScoreInfo_Init(float teams)
120 TeamScore_Spawn(COLOR_TEAM1, "Red");
122 TeamScore_Spawn(COLOR_TEAM2, "Blue");
124 TeamScore_Spawn(COLOR_TEAM3, "Yellow");
126 TeamScore_Spawn(COLOR_TEAM4, "Pink");
129 si.classname = "csqc_score_info";
130 si.SendEntity = ScoreInfo_SendEntity;
135 * per-player score entities
138 void PlayerScore_SendEntity()
142 WriteByte(MSG_ENTITY, ENT_CLIENT_SCORES);
143 WriteByte(MSG_ENTITY, num_for_edict(self.owner));
144 for(i = 0; i < MAX_SCORE; ++i)
145 WriteShort(MSG_ENTITY, self.scores[i]);
148 void PlayerScore_Clear(entity player)
153 if(!teamscores_entities_count)
155 sk = player.scorekeeper;
156 for(i = 0; i < MAX_SCORE; ++i)
162 void Score_ClearAll()
166 FOR_EACH_CLIENTSLOT(p)
171 for(i = 0; i < MAX_SCORE; ++i)
175 for(i = 0; i < 16; ++i)
177 sk = teamscorekeepers[i];
180 for(i = 0; i < MAX_SCORE; ++i)
181 sk.(teamscores[i]) = 0;
186 void PlayerScore_Attach(entity player)
189 if(player.scorekeeper)
190 error("player already has a scorekeeper");
193 sk.SendEntity = PlayerScore_SendEntity;
194 player.scorekeeper = sk;
197 void PlayerScore_Detach(entity player)
199 if(!player.scorekeeper)
200 error("player has no scorekeeper");
201 remove(player.scorekeeper);
202 player.scorekeeper = world;
205 void PlayerScore_Add(entity player, float scorefield, float score)
208 s = player.scorekeeper;
210 error("Adding score to unknown player!");
211 s.(scores[scorefield]) += score;
215 float PlayerScore_Compare(entity t1, entity t2)
217 if(!t1 || !t2) return (!t2) - !t1;
221 for(i = 0; i < MAX_TEAMSCORE; ++i)
222 result = ScoreField_Compare(t1, t2, scores[i], scores_flags[i], result);
226 void WinningConditionHelper()
229 if(teamscores_entities_count)
232 WinningConditionHelper_equality = 1;
233 WinningConditionHelper_winnerteam = 0;
234 for(t = 1; t < 16; ++t)
236 c = TeamScore_Compare(teamscorekeepers[WinningConditionHelper_winnerteam], teamscorekeepers[t]);
238 WinningConditionHelper_equality = 1;
241 WinningConditionHelper_equality = 0;
242 WinningConditionHelper_winnerteam = t;
246 WinningConditionHelper_topscore = teamscorekeepers[WinningConditionHelper_winnerteam].teamscores_primary;
248 WinningConditionHelper_winner = world;
249 if(WinningConditionHelper_equality)
250 WinningConditionHelper_winnerteam = -1;
255 WinningConditionHelper_equality = 1;
256 WinningConditionHelper_winner = world;
259 c = PlayerScore_Compare(WinningConditionHelper_winner.scorekeeper, p.scorekeeper);
261 WinningConditionHelper_equality = 1;
264 WinningConditionHelper_equality = 0;
265 WinningConditionHelper_winner = p;
269 WinningConditionHelper_topscore = WinningConditionHelper_winner.scorekeeper.scores_primary;
271 if(WinningConditionHelper_equality)
272 WinningConditionHelper_winner = world;
273 WinningConditionHelper_winnerteam = -1;