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