+// CenterPrint (#16)
+//
+#define CENTERPRINT_MAX_MSGS 10
+#define CENTERPRINT_MAX_ENTRIES 50
+#define CENTERPRINT_SPACING 0.7
+float cpm_index;
+string centerprint_messages[CENTERPRINT_MAX_MSGS];
+float centerprint_msgID[CENTERPRINT_MAX_MSGS];
+float centerprint_time[CENTERPRINT_MAX_MSGS];
+float centerprint_expire_time[CENTERPRINT_MAX_MSGS];
+float centerprint_countdown_num[CENTERPRINT_MAX_MSGS];
+float centerprint_fadetime;
+float centerprint_showing;
+
+void centerprint_generic(float new_id, string strMessage, float duration, float countdown_num)
+{
+ float i, j;
+
+ if(strMessage == "" && new_id == 0)
+ return;
+
+ // strip trailing newlines
+ j = strlen(strMessage) - 1;
+ while(substring(strMessage, j, 1) == "\n" && j >= 0)
+ --j;
+ if (j < strlen(strMessage) - 1)
+ strMessage = substring(strMessage, 0, j + 1);
+
+ if(strMessage == "" && new_id == 0)
+ return;
+
+ // strip leading newlines
+ j = 0;
+ while(substring(strMessage, j, 1) == "\n" && j < strlen(strMessage))
+ ++j;
+ if (j > 0)
+ strMessage = substring(strMessage, j, strlen(strMessage) - j);
+
+ if(strMessage == "" && new_id == 0)
+ return;
+
+ if (!centerprint_showing)
+ centerprint_showing = TRUE;
+
+ centerprint_fadetime = bound(0, autocvar_hud_panel_centerprint_fadetime, 1);
+
+ for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
+ {
+ if (j == CENTERPRINT_MAX_MSGS)
+ j = 0;
+ if (new_id && new_id == centerprint_msgID[j])
+ {
+ if (strMessage == "" && centerprint_messages[j] != "" && centerprint_countdown_num[j] == 0)
+ {
+ // fade out the current msg (duration and countdown_num are ignored)
+ centerprint_time[j] = centerprint_fadetime;
+ if (centerprint_expire_time[j] > time + centerprint_fadetime || centerprint_expire_time[j] < time)
+ centerprint_expire_time[j] = time + centerprint_fadetime;
+ return;
+ }
+ break; // found a msg with the same id, at position j
+ }
+ }
+
+ if (i == CENTERPRINT_MAX_MSGS)
+ {
+ // a msg with the same id was not found, add the msg at the next position
+ --cpm_index;
+ if (cpm_index == -1)
+ cpm_index = CENTERPRINT_MAX_MSGS - 1;
+ j = cpm_index;
+ }
+ if(centerprint_messages[j])
+ strunzone(centerprint_messages[j]);
+ centerprint_messages[j] = strzone(strMessage);
+ centerprint_msgID[j] = new_id;
+ if (duration < 0)
+ centerprint_time[j] = -1;
+ else
+ {
+ if(duration == 0)
+ duration = max(1, autocvar_hud_panel_centerprint_time);
+ centerprint_time[j] = duration;
+ centerprint_expire_time[j] = time + duration;
+ }
+ centerprint_countdown_num[j] = countdown_num;
+}
+
+void centerprint(string strMessage)
+{
+ centerprint_generic(0, strMessage, autocvar_hud_panel_centerprint_time, 0);
+}
+
+void reset_centerprint_messages(void)
+{
+ float i;
+ for (i=0; i<CENTERPRINT_MAX_MSGS; ++i)
+ {
+ centerprint_expire_time[i] = 0;
+ centerprint_time[i] = 1;
+ centerprint_msgID[i] = 0;
+ if(centerprint_messages[i])
+ strunzone(centerprint_messages[i]);
+ centerprint_messages[i] = string_null;
+ }
+}
+float hud_configure_cp_generation_time;
+void HUD_CenterPrint (void)
+{
+ if(!autocvar__hud_configure)
+ {
+ if(!autocvar_hud_panel_centerprint) return;
+
+ if (hud_configure_prev && hud_configure_prev != -1)
+ reset_centerprint_messages();
+ }
+ else
+ {
+ hud_configure_active_panel = HUD_PANEL_CENTERPRINT;
+
+ if (!hud_configure_prev)
+ reset_centerprint_messages();
+ if (time > hud_configure_cp_generation_time)
+ {
+ float r;
+ r = random();
+ if (r > 0.9)
+ centerprint_generic(floor(r*1000), strcat(sprintf("^3Countdown message at time %s", seconds_tostring(time)), ", seconds left: %d"), 1, 10);
+ else if (r > 0.8)
+ centerprint_generic(0, sprintf("^1Multiline message at time %s that\n^1lasts longer than normal", seconds_tostring(time)), 20, 0);
+ else
+ centerprint(sprintf("Message at time %s", seconds_tostring(time)));
+ hud_configure_cp_generation_time = time + 1 + random()*4;
+ }
+ }
+
+ HUD_Panel_UpdateCvars(centerprint);
+
+ // this panel doesn't fade when showing the scoreboard
+ if(autocvar__menu_alpha)
+ HUD_Panel_ApplyFadeAlpha();
+
+ if(scoreboard_fade_alpha)
+ {
+ // move the panel below the scoreboard
+ if (scoreboard_bottom >= 0.96 * vid_conheight)
+ return;
+ vector target_pos;
+ target_pos = eY * scoreboard_bottom + eX * 0.5 * (vid_conwidth - panel_size_x);
+ panel_pos = panel_pos + (target_pos - panel_pos) * sqrt(scoreboard_fade_alpha);
+ panel_size_y = min(panel_size_y, vid_conheight - scoreboard_bottom);
+ }
+
+ HUD_Panel_DrawBg(1);
+
+ if (!centerprint_showing)
+ return;
+
+ if(panel_bg_padding)
+ {
+ panel_pos += '1 1 0' * panel_bg_padding;
+ panel_size -= '2 2 0' * panel_bg_padding;
+ }
+
+ float entries, height;
+ vector fontsize;
+ // entries = bound(1, floor(CENTERPRINT_MAX_ENTRIES * 4 * panel_size_y/panel_size_x), CENTERPRINT_MAX_ENTRIES);
+ // height = panel_size_y/entries;
+ // fontsize = '1 1 0' * height;
+ height = vid_conheight/40 * autocvar_hud_panel_centerprint_fontscale;
+ fontsize = '1 1 0' * height;
+ entries = bound(1, floor(panel_size_y/height), CENTERPRINT_MAX_ENTRIES);
+
+ float i, j, k, n;
+ float a, sz, align, current_msg_pos_y, msg_size;
+ vector pos;
+ string ts;
+
+ n = -1; // if no msg will be displayed, n stays -1
+
+ pos = panel_pos;
+ if (autocvar_hud_panel_centerprint_flip)
+ pos_y += panel_size_y;
+ align = bound(0, autocvar_hud_panel_centerprint_align, 1);
+ for (i=0, j=cpm_index; i<CENTERPRINT_MAX_MSGS; ++i, ++j)
+ {
+ if (j == CENTERPRINT_MAX_MSGS)
+ j = 0;
+ if (centerprint_expire_time[j] < time)
+ {
+ if (centerprint_countdown_num[j] && centerprint_time[j] > 0)
+ {
+ centerprint_countdown_num[j] = centerprint_countdown_num[j] - 1;
+ if (centerprint_countdown_num[j] == 0)
+ continue;
+ centerprint_expire_time[j] = centerprint_expire_time[j] + centerprint_time[j];
+ }
+ else
+ continue;
+ }
+ if (centerprint_time[j] < 0 || centerprint_expire_time[j] - centerprint_fadetime > time)
+ {
+ a = 1;
+ sz = 1;
+ }
+ else if (centerprint_expire_time[j] > time)
+ {
+ a = (centerprint_expire_time[j] - time) / centerprint_fadetime;
+ sz = 0.8 + a * (1 - 0.8);
+ }
+
+ drawfontscale = sz * '1 1 0';
+ if (centerprint_countdown_num[j])
+ n = tokenizebyseparator(sprintf(centerprint_messages[j], centerprint_countdown_num[j]), "\n");
+ else
+ n = tokenizebyseparator(centerprint_messages[j], "\n");
+ if (autocvar_hud_panel_centerprint_flip)
+ {
+ // check if the message can be entirely shown
+ for(k = 0; k < n; ++k)
+ {
+ getWrappedLine_remaining = argv(k);
+ while(getWrappedLine_remaining)
+ {
+ ts = getWrappedLine(panel_size_x * sz, fontsize, stringwidth_colors);
+ if (ts != "")
+ pos_y -= fontsize_y;
+ else
+ pos_y -= fontsize_y * CENTERPRINT_SPACING/2;
+ if (pos_y < panel_pos_y) // check if the next line can be shown
+ {
+ drawfontscale = '1 1 0';
+ return;
+ }
+ }
+ }
+ current_msg_pos_y = pos_y; // save starting pos (first line) of the current message
+ }
+
+ msg_size = pos_y;
+ for(k = 0; k < n; ++k)
+ {
+ getWrappedLine_remaining = argv(k);
+ while(getWrappedLine_remaining)
+ {
+ ts = getWrappedLine(panel_size_x * sz, fontsize, stringwidth_colors);
+ if (ts != "")
+ {
+ if (align)
+ pos_x = panel_pos_x + (panel_size_x - stringwidth(ts, TRUE, fontsize)) * align;
+ drawcolorcodedstring(pos + eY * 0.5 * (1 - sz) * fontsize_y, ts, fontsize, a * panel_fg_alpha, DRAWFLAG_NORMAL);
+ pos_y += fontsize_y;
+ }
+ else
+ pos_y += fontsize_y * CENTERPRINT_SPACING/2;
+ if (!autocvar_hud_panel_centerprint_flip && pos_y > panel_pos_y + panel_size_y - fontsize_y) // check if the next line can be shown
+ {
+ drawfontscale = '1 1 0';
+ return;
+ }
+ }
+ }
+ msg_size = pos_y - msg_size;
+ if (autocvar_hud_panel_centerprint_flip)
+ {
+ pos_y = current_msg_pos_y - CENTERPRINT_SPACING * fontsize_y;
+ 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
+ pos_y += (msg_size + CENTERPRINT_SPACING * fontsize_y) * (1 - sqrt(a));
+ }
+ else
+ {
+ pos_y += CENTERPRINT_SPACING * fontsize_y;
+ 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
+ pos_y -= (msg_size + CENTERPRINT_SPACING * fontsize_y) * (1 - sqrt(a));
+ }
+ }
+ drawfontscale = '1 1 0';
+ if (n == -1)
+ {
+ centerprint_showing = FALSE;
+ reset_centerprint_messages();
+ }
+}
+