]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/score.qc
Merge branch 'master' into terencehill/dynamic_hud
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / score.qc
1 #include "score.qh"
2
3 #include <client/scoreboard.qh>
4 #include <common/ent_cs.qh>
5 #include <common/mapinfo.qh>
6
7 // Score (#7)
8
9 void HUD_UpdatePlayerTeams();
10 void HUD_Score_Rankings(vector pos, vector mySize, entity me)
11 {
12         float score;
13         entity tm = world, pl;
14         int SCOREPANEL_MAX_ENTRIES = 6;
15         float SCOREPANEL_ASPECTRATIO = 2;
16         int entries = bound(1, floor(SCOREPANEL_MAX_ENTRIES * mySize.y/mySize.x * SCOREPANEL_ASPECTRATIO), SCOREPANEL_MAX_ENTRIES);
17         vector fontsize = '1 1 0' * (mySize.y/entries);
18
19         vector rgb, score_color;
20         rgb = '1 1 1';
21         score_color = '1 1 1';
22
23         float name_size = mySize.x*0.75;
24         float spacing_size = mySize.x*0.04;
25         const float highlight_alpha = 0.2;
26         int i = 0, first_pl = 0;
27         bool me_printed = false;
28         string s;
29         if (autocvar__hud_configure)
30         {
31                 float players_per_team = 0;
32                 if (team_count)
33                 {
34                         // show team scores in the first line
35                         float score_size = mySize.x / team_count;
36                         players_per_team = max(2, ceil((entries - 1) / team_count));
37                         for(i=0; i<team_count; ++i) {
38                                 if (i == floor((entries - 2) / players_per_team) || (entries == 1 && i == 0))
39                                         HUD_Panel_DrawHighlight(pos + eX * score_size * i, eX * score_size + eY * fontsize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
40                                 drawstring_aspect(pos + eX * score_size * i, ftos(175 - 23*i), eX * score_size + eY * fontsize.y, Team_ColorRGB(ColorByTeam(i)) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
41                         }
42                         first_pl = 1;
43                         pos.y += fontsize.y;
44                 }
45                 score = 10 + SCOREPANEL_MAX_ENTRIES * 3;
46                 for (i=first_pl; i<entries; ++i)
47                 {
48                         //simulate my score is lower than all displayed players,
49                         //so that I don't appear at all showing pure rankings.
50                         //This is to better show the difference between the 2 ranking views
51                         if (i == entries-1 && autocvar_hud_panel_score_rankings == 1)
52                         {
53                                 rgb = '1 1 0';
54                                 drawfill(pos, eX * mySize.x + eY * fontsize.y, rgb, highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
55                                 s = entcs_GetName(player_localnum);
56                                 score = 7;
57                         }
58                         else
59                         {
60                                 s = sprintf(_("Player %d"), i + 1 - first_pl);
61                                 score -= 3;
62                         }
63
64                         if (team_count)
65                                 score_color = Team_ColorRGB(ColorByTeam(floor((i - first_pl) / players_per_team))) * 0.8;
66                         s = textShortenToWidth(s, name_size, fontsize, stringwidth_colors);
67                         drawcolorcodedstring(pos + eX * (name_size - stringwidth(s, true, fontsize)), s, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
68                         drawstring(pos + eX * (name_size + spacing_size), ftos(score), fontsize, score_color, panel_fg_alpha, DRAWFLAG_NORMAL);
69                         pos.y += fontsize.y;
70                 }
71                 return;
72         }
73
74         if (!scoreboard_fade_alpha) // the scoreboard too calls HUD_UpdatePlayerTeams
75                 HUD_UpdatePlayerTeams();
76         if (team_count)
77         {
78                 // show team scores in the first line
79                 float score_size = mySize.x / team_count;
80                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
81                         if(tm.team == NUM_SPECTATOR)
82                                 continue;
83                         if(!tm.team && teamplay)
84                                 continue;
85                         if (tm.team == myteam)
86                                 drawfill(pos + eX * score_size * i, eX * score_size + eY * fontsize.y, '1 1 1', highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
87                         drawstring_aspect(pos + eX * score_size * i, ftos(tm.(teamscores[ts_primary])), eX * score_size + eY * fontsize.y, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
88                         ++i;
89                 }
90                 first_pl = 1;
91                 pos.y += fontsize.y;
92                 tm = teams.sort_next;
93         }
94         i = first_pl;
95
96         do
97         for (pl = players.sort_next; pl && i<entries; pl = pl.sort_next)
98         {
99                 if ((team_count && pl.team != tm.team) || pl.team == NUM_SPECTATOR)
100                         continue;
101
102                 if (i == entries-1 && !me_printed && pl != me)
103                 if (autocvar_hud_panel_score_rankings == 1 && spectatee_status != -1)
104                 {
105                         for (pl = me.sort_next; pl; pl = pl.sort_next)
106                                 if (pl.team != NUM_SPECTATOR)
107                                         break;
108
109                         if (pl)
110                                 rgb = '1 1 0'; //not last but not among the leading players: yellow
111                         else
112                                 rgb = '1 0 0'; //last: red
113                         pl = me;
114                 }
115
116                 if (pl == me)
117                 {
118                         if (i == first_pl)
119                                 rgb = '0 1 0'; //first: green
120                         me_printed = true;
121                         drawfill(pos, eX * mySize.x + eY * fontsize.y, rgb, highlight_alpha * panel_fg_alpha, DRAWFLAG_NORMAL);
122                 }
123                 if (team_count)
124                         score_color = Team_ColorRGB(pl.team) * 0.8;
125                 s = textShortenToWidth(entcs_GetName(pl.sv_entnum), name_size, fontsize, stringwidth_colors);
126                 drawcolorcodedstring(pos + eX * (name_size - stringwidth(s, true, fontsize)), s, fontsize, panel_fg_alpha, DRAWFLAG_NORMAL);
127                 drawstring(pos + eX * (name_size + spacing_size), ftos(pl.(scores[ps_primary])), fontsize, score_color, panel_fg_alpha, DRAWFLAG_NORMAL);
128                 pos.y += fontsize.y;
129                 ++i;
130         }
131         while (i<entries && team_count && (tm = tm.sort_next) && (tm.team != NUM_SPECTATOR || (tm = tm.sort_next)));
132 }
133
134 void HUD_Score()
135 {
136         if(!autocvar__hud_configure)
137         {
138                 if(!autocvar_hud_panel_score) return;
139                 if(spectatee_status == -1 && (gametype == MAPINFO_TYPE_RACE || gametype == MAPINFO_TYPE_CTS)) return;
140         }
141
142         HUD_Panel_UpdateCvars();
143         vector pos, mySize;
144         pos = panel_pos;
145         mySize = panel_size;
146
147         HUD_Scale_Enable();
148         HUD_Panel_DrawBg(1);
149         if(panel_bg_padding)
150         {
151                 pos += '1 1 0' * panel_bg_padding;
152                 mySize -= '2 2 0' * panel_bg_padding;
153         }
154
155         float score, distribution = 0;
156         string sign;
157         vector distribution_color;
158         entity tm, pl, me;
159
160         me = playerslots[current_player];
161
162         if((scores_flags[ps_primary] & SFL_TIME) && !teamplay) { // race/cts record display on HUD
163                 string timer, distrtimer;
164
165                 pl = players.sort_next;
166                 if(pl == me)
167                         pl = pl.sort_next;
168                 if(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)
169                         if(pl.scores[ps_primary] == 0)
170                                 pl = world;
171
172                 score = me.(scores[ps_primary]);
173                 timer = TIME_ENCODED_TOSTRING(score);
174
175                 draw_beginBoldFont();
176                 if (pl && ((!(scores_flags[ps_primary] & SFL_ZERO_IS_WORST)) || score)) {
177                         // distribution display
178                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
179
180                         distrtimer = ftos_decimals(fabs(distribution/pow(10, TIME_DECIMALS)), TIME_DECIMALS);
181
182                         if (distribution <= 0) {
183                                 distribution_color = '0 1 0';
184                                 sign = "-";
185                         }
186                         else {
187                                 distribution_color = '1 0 0';
188                                 sign = "+";
189                         }
190                         drawstring_aspect(pos + eX * 0.75 * mySize.x, strcat(sign, distrtimer), eX * 0.25 * mySize.x + eY * (1/3) * mySize.y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
191                 }
192                 // race record display
193                 if (distribution <= 0)
194                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
195                 drawstring_aspect(pos, timer, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
196                 draw_endBoldFont();
197         } else if (!teamplay) { // non-teamgames
198                 if ((spectatee_status == -1 && !autocvar__hud_configure) || autocvar_hud_panel_score_rankings)
199                 {
200                         HUD_Score_Rankings(pos, mySize, me);
201                         return;
202                 }
203                 // me vector := [team/connected frags id]
204                 pl = players.sort_next;
205                 if(pl == me)
206                         pl = pl.sort_next;
207
208                 if(autocvar__hud_configure)
209                         distribution = 42;
210                 else if(pl)
211                         distribution = me.(scores[ps_primary]) - pl.(scores[ps_primary]);
212                 else
213                         distribution = 0;
214
215                 score = me.(scores[ps_primary]);
216                 if(autocvar__hud_configure)
217                         score = 123;
218
219                 if(distribution >= 5)
220                         distribution_color = eY;
221                 else if(distribution >= 0)
222                         distribution_color = '1 1 1';
223                 else if(distribution >= -5)
224                         distribution_color = '1 1 0';
225                 else
226                         distribution_color = eX;
227
228                 string distribution_str;
229                 distribution_str = ftos(distribution);
230                 draw_beginBoldFont();
231                 if (distribution >= 0)
232                 {
233                         if (distribution > 0)
234                                 distribution_str = strcat("+", distribution_str);
235                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
236                 }
237                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize.x + eY * mySize.y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
238                 drawstring_aspect(pos + eX * 0.75 * mySize.x, distribution_str, eX * 0.25 * mySize.x + eY * (1/3) * mySize.y, distribution_color, panel_fg_alpha, DRAWFLAG_NORMAL);
239                 draw_endBoldFont();
240         } else { // teamgames
241                 float row, column, rows = 0, columns = 0;
242                 vector offset = '0 0 0';
243                 vector score_pos, score_size; //for scores other than myteam
244                 if(autocvar_hud_panel_score_rankings)
245                 {
246                         HUD_Score_Rankings(pos, mySize, me);
247                         return;
248                 }
249                 if(spectatee_status == -1)
250                 {
251                         rows = HUD_GetRowCount(team_count, mySize, 3);
252                         columns = ceil(team_count/rows);
253                         score_size = eX * mySize.x*(1/columns) + eY * mySize.y*(1/rows);
254
255                         float newSize;
256                         if(score_size.x/score_size.y > 3)
257                         {
258                                 newSize = 3 * score_size.y;
259                                 offset.x = score_size.x - newSize;
260                                 pos.x += offset.x/2;
261                                 score_size.x = newSize;
262                         }
263                         else
264                         {
265                                 newSize = 1/3 * score_size.x;
266                                 offset.y = score_size.y - newSize;
267                                 pos.y += offset.y/2;
268                                 score_size.y = newSize;
269                         }
270                 }
271                 else
272                         score_size = eX * mySize.x*(1/4) + eY * mySize.y*(1/3);
273
274                 float max_fragcount;
275                 max_fragcount = -99;
276                 draw_beginBoldFont();
277                 row = column = 0;
278                 for(tm = teams.sort_next; tm; tm = tm.sort_next) {
279                         if(tm.team == NUM_SPECTATOR)
280                                 continue;
281                         if(!tm.team && teamplay)
282                                 continue;
283                         score = tm.(teamscores[ts_primary]);
284                         if(autocvar__hud_configure)
285                                 score = 123;
286
287                         if (score > max_fragcount)
288                                 max_fragcount = score;
289
290                         if (spectatee_status == -1)
291                         {
292                                 score_pos = pos + eX * column * (score_size.x + offset.x) + eY * row * (score_size.y + offset.y);
293                                 if (max_fragcount == score)
294                                         HUD_Panel_DrawHighlight(score_pos, score_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
295                                 drawstring_aspect(score_pos, ftos(score), score_size, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
296                                 ++row;
297                                 if(row >= rows)
298                                 {
299                                         row = 0;
300                                         ++column;
301                                 }
302                         }
303                         else if(tm.team == myteam) {
304                                 if (max_fragcount == score)
305                                         HUD_Panel_DrawHighlight(pos, eX * 0.75 * mySize.x + eY * mySize.y, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
306                                 drawstring_aspect(pos, ftos(score), eX * 0.75 * mySize.x + eY * mySize.y, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
307                         } else {
308                                 if (max_fragcount == score)
309                                         HUD_Panel_DrawHighlight(pos + eX * 0.75 * mySize.x + eY * (1/3) * rows * mySize.y, score_size, '1 1 1', panel_fg_alpha, DRAWFLAG_NORMAL);
310                                 drawstring_aspect(pos + eX * 0.75 * mySize.x + eY * (1/3) * rows * mySize.y, ftos(score), score_size, Team_ColorRGB(tm.team) * 0.8, panel_fg_alpha, DRAWFLAG_NORMAL);
311                                 ++rows;
312                         }
313                 }
314                 draw_endBoldFont();
315         }
316 }