]> de.git.xonotic.org Git - xonotic/xonotic-data.pk3dir.git/blob - qcsrc/client/hud/panel/centerprint.qc
Merge branch 'master' into terencehill/bot_waypoints
[xonotic/xonotic-data.pk3dir.git] / qcsrc / client / hud / panel / centerprint.qc
1 #include "centerprint.qh"
2
3 #include "scoreboard.qh"
4 #include <common/notifications/all.qh>
5 #include <client/defs.qh>
6 #include <client/miscfunctions.qh>
7
8 // CenterPrint (#16)
9
10 const int CENTERPRINT_MAX_MSGS = 10;
11 const int CENTERPRINT_MAX_ENTRIES = 50;
12 const float CENTERPRINT_SPACING = 0.7;
13 int cpm_index;
14 string centerprint_messages[CENTERPRINT_MAX_MSGS];
15 int centerprint_msgID[CENTERPRINT_MAX_MSGS];
16 float centerprint_time[CENTERPRINT_MAX_MSGS];
17 float centerprint_expire_time[CENTERPRINT_MAX_MSGS];
18 int centerprint_countdown_num[CENTERPRINT_MAX_MSGS];
19 bool centerprint_showing;
20
21 void centerprint_generic(int new_id, string strMessage, float duration, int countdown_num)
22 {
23     TC(int, new_id); TC(int, countdown_num);
24         //printf("centerprint_generic(%d, '%s^7', %d, %d);\n", new_id, strMessage, duration, countdown_num);
25         int i, j;
26
27         if(strMessage == "" && new_id == 0)
28                 return;
29
30         // strip trailing newlines
31         j = strlen(strMessage) - 1;
32         while(substring(strMessage, j, 1) == "\n" && j >= 0)
33                 --j;
34         if (j < strlen(strMessage) - 1)
35                 strMessage = substring(strMessage, 0, j + 1);
36
37         if(strMessage == "" && new_id == 0)
38                 return;
39
40         // strip leading newlines
41         j = 0;
42         while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
43                 ++j;
44         if (j > 0)
45                 strMessage = substring(strMessage, j, strlen(strMessage) - j);
46
47         if(strMessage == "" && new_id == 0)
48                 return;
49
50         if (!centerprint_showing)
51                 centerprint_showing = true;
52
53         for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
54         {
55                 if (j == CENTERPRINT_MAX_MSGS)
56                         j = 0;
57                 if (new_id && new_id == centerprint_msgID[j])
58                 {
59                         if (strMessage == "" && centerprint_messages[j] != "" && centerprint_countdown_num[j] == 0)
60                         {
61                                 // fade out the current msg (duration and countdown_num are ignored)
62                                 centerprint_time[j] = min(5, autocvar_hud_panel_centerprint_fade_out);
63                                 centerprint_expire_time[j] = -1; // don't use the variable time here!
64                                 return;
65                         }
66                         break; // found a msg with the same id, at position j
67                 }
68         }
69
70         if (i == CENTERPRINT_MAX_MSGS)
71         {
72                 // a msg with the same id was not found, add the msg at the next position
73                 --cpm_index;
74                 if (cpm_index == -1)
75                         cpm_index = CENTERPRINT_MAX_MSGS - 1;
76                 j = cpm_index;
77         }
78         strcpy(centerprint_messages[j], strMessage);
79         centerprint_msgID[j] = new_id;
80         if (duration < 0)
81         {
82                 centerprint_time[j] = -1;
83                 centerprint_expire_time[j] = -1; // don't use the variable time here!
84         }
85         else
86         {
87                 if(duration == 0)
88                         duration = max(1, autocvar_hud_panel_centerprint_time);
89                 centerprint_time[j] = duration;
90                 centerprint_expire_time[j] = -1; // don't use the variable time here!
91         }
92         centerprint_countdown_num[j] = countdown_num;
93 }
94
95 void centerprint_kill(int id)
96 {
97     TC(int, id);
98         centerprint_generic(id, "", 0, 0);
99 }
100
101 void centerprint_hud(string strMessage)
102 {
103         centerprint_generic(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
104 }
105
106 void reset_centerprint_messages()
107 {
108         for (int i=0; i<CENTERPRINT_MAX_MSGS; ++i)
109         {
110                 centerprint_expire_time[i] = 0;
111                 centerprint_time[i] = 1;
112                 centerprint_msgID[i] = 0;
113                 strfree(centerprint_messages[i]);
114         }
115 }
116 float hud_configure_cp_generation_time;
117 void HUD_CenterPrint ()
118 {
119         if(!autocvar__hud_configure)
120         {
121                 if(!autocvar_hud_panel_centerprint) return;
122
123                 if(hud_configure_prev)
124                         reset_centerprint_messages();
125         }
126         else
127         {
128                 if(!hud_configure_prev)
129                 {
130                         reset_centerprint_messages();
131                         hud_configure_cp_generation_time = time; // show a message immediately
132                 }
133                 if (time > hud_configure_cp_generation_time)
134                 {
135                         if(highlightedPanel == HUD_PANEL(CENTERPRINT))
136                         {
137                                 float r;
138                                 r = random();
139                                 if (r > 0.8)
140                                         centerprint_generic(floor(r*1000), strcat(sprintf("^3Countdown message at time %s", seconds_tostring(time)), ", seconds left: ^COUNT"), 1, 10);
141                                 else if (r > 0.55)
142                                         centerprint_generic(0, sprintf("^1Multiline message at time %s that\n^1lasts longer than normal", seconds_tostring(time)), 20, 0);
143                                 else
144                                         centerprint_hud(sprintf("Message at time %s", seconds_tostring(time)));
145                                 hud_configure_cp_generation_time = time + 1 + random()*4;
146                         }
147                         else
148                         {
149                                 centerprint_generic(0, sprintf("Centerprint message", seconds_tostring(time)), 10, 0);
150                                 hud_configure_cp_generation_time = time + 10 - random()*3;
151                         }
152                 }
153         }
154
155         HUD_Panel_LoadCvars();
156
157         if ( HUD_Radar_Clickable() )
158         {
159                 if (hud_panel_radar_bottom >= 0.96 * vid_conheight)
160                         return;
161
162                 panel_pos.x = 0.5 * (vid_conwidth - panel_size.x);
163                 panel_pos.y = hud_panel_radar_bottom;
164                 panel_size.y = min(panel_size.y, vid_conheight - hud_panel_radar_bottom);
165         }
166         else if(!autocvar__hud_configure && scoreboard_fade_alpha)
167         {
168                 // move the panel below the scoreboard
169                 if (scoreboard_bottom >= 0.96 * vid_conheight)
170                         return;
171                 vector target_pos = vec2(0.5 * (vid_conwidth - panel_size.x), scoreboard_bottom);
172                 if(target_pos.y > panel_pos.y)
173                 {
174                         panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
175                         panel_size.y = min(panel_size.y, vid_conheight - scoreboard_bottom);
176                 }
177         }
178
179         if (autocvar_hud_panel_centerprint_dynamichud)
180                 HUD_Scale_Enable();
181         else
182                 HUD_Scale_Disable();
183         HUD_Panel_DrawBg();
184
185         if (!centerprint_showing)
186                 return;
187
188         if(panel_bg_padding)
189         {
190                 panel_pos += '1 1 0' * panel_bg_padding;
191                 panel_size -= '2 2 0' * panel_bg_padding;
192         }
193
194         int entries;
195         float height;
196         vector fontsize;
197         // entries = bound(1, floor(CENTERPRINT_MAX_ENTRIES * 4 * panel_size_y/panel_size_x), CENTERPRINT_MAX_ENTRIES);
198         // height = panel_size_y/entries;
199         // fontsize = '1 1 0' * height;
200         height = vid_conheight/50 * autocvar_hud_panel_centerprint_fontscale;
201         fontsize = '1 1 0' * height;
202         entries = bound(1, floor(panel_size.y/height), CENTERPRINT_MAX_ENTRIES);
203
204         int i, j, k, n, g;
205         float a, sz, align, current_msg_posY = 0, msg_size;
206         vector pos;
207         string ts;
208         bool all_messages_expired = true;
209
210         pos = panel_pos;
211         if (autocvar_hud_panel_centerprint_flip)
212                 pos.y += panel_size.y;
213         align = bound(0, autocvar_hud_panel_centerprint_align, 1);
214         for (g=0, i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
215         {
216                 if (j == CENTERPRINT_MAX_MSGS)
217                         j = 0;
218                 if (centerprint_expire_time[j] == -1)
219                 {
220                         // here we are sure the time variable is not altered by CSQC_Ent_Update
221                         centerprint_expire_time[j] = time;
222                         if (centerprint_time[j] > 0)
223                                 centerprint_expire_time[j] += centerprint_time[j];
224                 }
225                 if (centerprint_expire_time[j] <= time)
226                 {
227                         if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
228                         {
229                                 centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
230                                 if (centerprint_countdown_num[j] == 0)
231                                         continue;
232                                 centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
233                         }
234                         else if(centerprint_time[j] != -1)
235                                 continue;
236                 }
237
238                 all_messages_expired = false;
239
240                 // fade the centerprint_hud in/out
241                 if(centerprint_time[j] < 0)  // Expired but forced. Expire time is the fade-in time.
242                         a = (time - centerprint_expire_time[j]) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
243                 else if(centerprint_expire_time[j] - autocvar_hud_panel_centerprint_fade_out > time)  // Regularily printed. Not fading out yet.
244                         a = (time - (centerprint_expire_time[j] - centerprint_time[j])) / max(0.0001, autocvar_hud_panel_centerprint_fade_in);
245                 else // Expiring soon, so fade it out.
246                         a = (centerprint_expire_time[j] - time) / max(0.0001, autocvar_hud_panel_centerprint_fade_out);
247
248                 if(centerprint_msgID[j] == CPID_TIMEIN)
249                         a = 1;
250
251                 // while counting down show it anyway in order to hold the current message position
252                 if (a <= 0.5/255.0 && centerprint_countdown_num[j] == 0)  // Guaranteed invisible - don't show.
253                         continue;
254                 if (a > 1)
255                         a = 1;
256
257                 // set the size from fading in/out before subsequent fading
258                 sz = autocvar_hud_panel_centerprint_fade_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_minfontsize);
259
260                 // also fade it based on positioning
261                 if(autocvar_hud_panel_centerprint_fade_subsequent)
262                 {
263                         // pass one: all messages after the first have half alpha
264                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passone_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passone))), 1);
265                         // pass two: after that, gradually lower alpha even more for each message
266                         a = a * bound(autocvar_hud_panel_centerprint_fade_subsequent_passtwo_minalpha, (1 - (g / max(1, autocvar_hud_panel_centerprint_fade_subsequent_passtwo))), 1);
267                 }
268                 a *= panel_fg_alpha;
269
270                 // finally set the size based on the new alpha from subsequent fading
271                 sz = sz * (autocvar_hud_panel_centerprint_fade_subsequent_minfontsize + a * (1 - autocvar_hud_panel_centerprint_fade_subsequent_minfontsize));
272                 drawfontscale = hud_scale * sz;
273
274                 if (centerprint_countdown_num[j])
275                         n = tokenizebyseparator(strreplace("^COUNT", count_seconds(centerprint_countdown_num[j]), centerprint_messages[j]), "\n");
276                 else
277                         n = tokenizebyseparator(centerprint_messages[j], "\n");
278
279                 if (autocvar_hud_panel_centerprint_flip)
280                 {
281                         // check if the message can be entirely shown
282                         for(k = 0; k < n; ++k)
283                         {
284                                 getWrappedLine_remaining = argv(k);
285                                 while(getWrappedLine_remaining)
286                                 {
287                                         ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
288                                         if (ts != "")
289                                                 pos.y -= fontsize.y;
290                                         else
291                                                 pos.y -= fontsize.y * CENTERPRINT_SPACING/2;
292                                 }
293                         }
294                         current_msg_posY = pos.y; // save starting pos (first line) of the current message
295                 }
296
297                 msg_size = pos.y;
298                 for(k = 0; k < n; ++k)
299                 {
300                         getWrappedLine_remaining = argv(k);
301                         while(getWrappedLine_remaining)
302                         {
303                                 ts = getWrappedLine(panel_size.x * hud_scale.x * sz, fontsize, stringwidth_colors);
304                                 if (ts != "")
305                                 {
306                                         if (align)
307                                                 pos.x = panel_pos.x + (panel_size.x - stringwidth(ts, true, fontsize) * sz) * align;
308                                         if (a > 0.5/255.0)  // Otherwise guaranteed invisible - don't show. This is checked a second time after some multiplications with other factors were done so temporary changes of these cannot cause flicker.
309                                                 drawcolorcodedstring(pos + eY * 0.5 * (1 - sz * hud_scale.x) * fontsize.y, ts, fontsize, a, DRAWFLAG_NORMAL);
310                                         pos.y += fontsize.y;
311                                 }
312                                 else
313                                         pos.y += fontsize.y * CENTERPRINT_SPACING/2;
314                         }
315                 }
316
317                 ++g; // move next position number up
318
319                 msg_size = pos.y - msg_size;
320                 if (autocvar_hud_panel_centerprint_flip)
321                 {
322                         pos.y = current_msg_posY - CENTERPRINT_SPACING * fontsize.y;
323                         if (a < 1 && centerprint_msgID[j] == 0) // messages with id can be replaced just after they are faded out, so never move over them the next messages
324                                 pos.y += (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
325
326                         if (pos.y < panel_pos.y) // check if the next message can be shown
327                         {
328                                 drawfontscale = hud_scale;
329                                 return;
330                         }
331                 }
332                 else
333                 {
334                         pos.y += CENTERPRINT_SPACING * fontsize.y;
335                         if (a < 1 && centerprint_msgID[j] == 0) // messages with id can be replaced just after they are faded out, so never move over them the next messages
336                                 pos.y -= (msg_size + CENTERPRINT_SPACING * fontsize.y) * (1 - sqrt(sz));
337
338                         if(pos.y > panel_pos.y + panel_size.y - fontsize.y) // check if the next message can be shown
339                         {
340                                 drawfontscale = hud_scale;
341                                 return;
342                         }
343                 }
344         }
345         drawfontscale = hud_scale;
346         if (all_messages_expired)
347         {
348                 centerprint_showing = false;
349                 reset_centerprint_messages();
350         }
351 }