From 78ab90c1a685a5d567b497b651f6ada3915afe90 Mon Sep 17 00:00:00 2001 From: Alug Date: Mon, 30 Jun 2025 08:25:34 +0200 Subject: [PATCH 1/2] "restrict" macro for c++ since it does not have it, use __restrict instead --- src/doomdef.h | 8 ++++++++ src/m_emotes.cpp | 1 - src/r_main.h | 2 +- 3 files changed, 9 insertions(+), 2 deletions(-) diff --git a/src/doomdef.h b/src/doomdef.h index f9467199b..15993b4bb 100644 --- a/src/doomdef.h +++ b/src/doomdef.h @@ -560,6 +560,14 @@ UINT32 quickncasehash (const char *p, size_t n) #define UNLIKELY(x) (x) #endif +#ifdef __cplusplus +#if defined(__GNUC__) || defined(__clang__) || defined(_MSC_VER) + #define restrict __restrict +#else + #define restrict +#endif +#endif + // An assert-type mechanism. #ifdef PARANOIA #define I_Assert(e) ((e) ? (void)0 : I_Error("assert failed: %s, file %s, line %d", #e, __FILE__, __LINE__)) diff --git a/src/m_emotes.cpp b/src/m_emotes.cpp index 9cfb1b953..90c7e22f3 100644 --- a/src/m_emotes.cpp +++ b/src/m_emotes.cpp @@ -11,7 +11,6 @@ extern "C" { #include "hu_stuff.h" // Goddammit -#define restrict #include "v_video.h" } diff --git a/src/r_main.h b/src/r_main.h index d3284764d..e1ca0c5fc 100644 --- a/src/r_main.h +++ b/src/r_main.h @@ -84,7 +84,7 @@ extern lighttable_t *zlight[LIGHTLEVELS][MAXLIGHTZ]; // // killough 5/2/98: reformatted // -FUNCINLINE static ATTRINLINE PUREFUNC INT32 R_PointOnSide(fixed_t x, fixed_t y, const node_t *restrict node) +FUNCINLINE static ATTRINLINE PUREFUNC INT32 R_PointOnSide(fixed_t x, fixed_t y, const node_t * restrict node) { if (!node->dx) return x <= node->x ? node->dy > 0 : node->dy < 0; From d2bab80b29a252904e5de7d98255988292a26a73 Mon Sep 17 00:00:00 2001 From: Indev Date: Mon, 30 Jun 2025 16:43:14 +0300 Subject: [PATCH 2/2] Add emote suggestions and autocompletion --- src/hu_stuff.c | 34 ++++++++++++++++++++- src/m_emotes.cpp | 17 +++++++---- src/m_emotes.h | 3 +- src/m_textinput.c | 76 +++++++++++++++++++++++++++++++++++++++++++++-- src/m_textinput.h | 4 ++- 5 files changed, 124 insertions(+), 10 deletions(-) diff --git a/src/hu_stuff.c b/src/hu_stuff.c index d2b15e9e4..0a4727b1e 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -95,6 +95,8 @@ static boolean headsupactive = false; boolean hu_showscores; // draw rankings static char hu_tick; static tic_t hu_emoteanim = 0; +#define MAXEMOTESUGGESTIONS 8 +static emote_t *emote_suggestions[MAXEMOTESUGGESTIONS] = {0}; static huddrawlist_h luahuddrawlist_scores; @@ -1369,7 +1371,7 @@ boolean HU_Responder(event_t *ev) && ev->data1 != gamecontrol[0][gc_talkkey][1])) return false; - M_TextInputHandleEmotes(&w_chat, c); + M_TextInputHandleEmotes(&w_chat, c, emote_suggestions, MAXEMOTESUGGESTIONS); if (c == KEY_ENTER) { @@ -1907,6 +1909,36 @@ static void HU_DrawChat(void) } } + if (emote_suggestions[0]) + { + // A bit of copy-paste from /pm code :p + INT32 suggesty = chaty - charheight - 1; + size_t longest_suggestion_length = 0; + + for (i = 0; i < MAXEMOTESUGGESTIONS && emote_suggestions[i]; ++i) + { + longest_suggestion_length = max(longest_suggestion_length, strlen(emote_suggestions[i]->name)); + } + +#ifdef NETSPLITSCREEN + if (splitscreen) + { + suggesty -= BASEVIDHEIGHT/2; + if (splitscreen > 1) + suggesty += 16; + } + else +#endif + suggesty -= (cv_kartspeedometer.value ? 16 : 0); + + for (i = 0; i < MAXEMOTESUGGESTIONS && emote_suggestions[i]; ++i) + { + V_DrawFillConsoleMap(chatx + boxw + 2, suggesty - (7*i), (longest_suggestion_length+2)*4 + EMOTEWIDTH, 6, 239|V_SNAPTOBOTTOM|V_SNAPTOLEFT); + V_DrawSmallString(chatx + boxw + 4 + EMOTEWIDTH, suggesty - (7*i), V_SNAPTOBOTTOM|V_SNAPTOLEFT|V_ALLOWLOWERCASE, va(":%s:", emote_suggestions[i]->name)); + HU_DrawEmote(chatx + boxw + 2, suggesty - i*7, emote_suggestions[i], V_SNAPTOBOTTOM|V_SNAPTOLEFT); + } + } + // handle /pm list. It's messy, horrible and I don't care. if (strnicmp(w_chat_buf, "/pm", 3) == 0 && vid.width >= 400 && !teamtalk) // 320x200 unsupported kthxbai { diff --git a/src/m_emotes.cpp b/src/m_emotes.cpp index 90c7e22f3..27c66be14 100644 --- a/src/m_emotes.cpp +++ b/src/m_emotes.cpp @@ -74,13 +74,20 @@ void M_LoadEmotes(UINT16 wadnum) emote_name = line.substr(split+1); - if (emote_name.find_first_of(" \t\n\r\f\v") != emote_name.npos) + if (emote_name.find_first_of(" \t\n\r\f\v:") != emote_name.npos) { - CONS_Alert(CONS_WARNING, "EMOTES: Emote name cannot contain spaces. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); + CONS_Alert(CONS_WARNING, "EMOTES: Emote name cannot contain spaces or ':' symbols. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); continue; } + if (emote_name.size() > MAXEMOTENAME) + { + CONS_Alert(CONS_WARNING, "EMOTES: Emote name is too long, truncating. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); + emote_name = emote_name.substr(0, MAXEMOTENAME); + } + emote = &emotes[emote_name]; + strlcpy(emote->name, emote_name.c_str(), MAXEMOTENAME); emote->timeperframe = 1; emote->numframes = 0; @@ -173,17 +180,17 @@ void M_InitEmotes(void) M_LoadEmotes(i); } -emote_t *M_FindEmote(const char *name, int skip) +emote_t *M_FindEmote(const char *name, int len, int skip) { if (!cv_emotes.value) return nullptr; char query[MAXEMOTENAME+1] = {0}; - std::strncpy(query, name, MAXEMOTENAME); + std::strncpy(query, name, min(len, MAXEMOTENAME)); for (auto &pair: emotes) { - if (pair.first.rfind(query, 0, MAXEMOTENAME) != 0) + if (pair.first.rfind(query, 0, min(len, MAXEMOTENAME)) != 0) continue; if (skip > 0) diff --git a/src/m_emotes.h b/src/m_emotes.h index bfce68142..783c6ec17 100644 --- a/src/m_emotes.h +++ b/src/m_emotes.h @@ -15,6 +15,7 @@ extern consvar_t cv_emotes; #define EMOTEWIDTH 6 typedef struct emote_s { + char name[MAXEMOTENAME+1]; char frames[MAXEMOTEFRAMES][9]; UINT8 numframes; tic_t timeperframe; @@ -27,7 +28,7 @@ void M_LoadEmotes(UINT16 wadnum); void M_InitEmotes(void); // Finds first matching emote, ignoring first few matches -emote_t *M_FindEmote(const char *name, int skips); +emote_t *M_FindEmote(const char *name, int len, int skips); // If first character is not ':', instantly returns null // Starts from ':' character, goes until it finds ':' or end of string diff --git a/src/m_textinput.c b/src/m_textinput.c index 18801eec2..353c8122b 100644 --- a/src/m_textinput.c +++ b/src/m_textinput.c @@ -1,6 +1,5 @@ #include "m_textinput.h" #include "m_menu.h" // MAXSTRINGLENGTH -#include "m_emotes.h" // M_EmoteV_SubStringWidth #include "v_video.h" #include "r_main.h" // renderisnewtic #include "i_system.h" @@ -401,7 +400,7 @@ boolean M_TextInputHandle(textinput_t *input, INT32 key) return M_TextInputHandleBase(input, key, false); } -boolean M_TextInputHandleEmotes(textinput_t *input, INT32 key) +boolean M_TextInputHandleEmotes(textinput_t *input, INT32 key, emote_t *suggestions[], int maxsuggestions) { boolean ret = M_TextInputHandleBase(input, key, true); @@ -409,6 +408,79 @@ boolean M_TextInputHandleEmotes(textinput_t *input, INT32 key) if (M_TextInputCheckEmote(input)) M_TextInputToWordEnd(input, !shiftdown); + // Always clear the first entry + suggestions[0] = NULL; + + // For autocomplete + int emotestart = 0; + + // Try find suggestions for emote names + for (int i = input->cursor-1; i >= 0 && input->cursor-i <= MAXEMOTENAME; --i) + { + // Space can't be part of emote name + if (isspace(input->buffer[i])) + break; + + // Found a :, try suggest emotes + if (input->buffer[i] == ':') + { + emotestart = i+1; + // ...But only if we typed at least something + if ((int)input->cursor-(i-1) < 3) + break; + + for (int skip = 0; skip < maxsuggestions; ++skip) + { + suggestions[skip] = M_FindEmote(input->buffer+i+1, input->cursor-i-1, skip); + + // No more suggestions + if (!suggestions[skip]) + break; + } + + // In any case, we found what we wanted, can exit the loop now + break; + } + } + + // If we suggest emotes, try autocomplete + if (key == '\t' && suggestions[0]) + { + int pos = (input->cursor-emotestart); + boolean autocomplete = true; + + while (autocomplete) + { + // Check if current character matches for all suggestions + char c = suggestions[0]->name[pos]; + + // End of string reached + if (!c) + break; + + for (int i = 1; i < maxsuggestions && suggestions[i]; ++i) + { + if (suggestions[i]->name[pos] != c) + { + autocomplete = false; + break; + } + } + + ++pos; + + if (autocomplete) + M_TextInputAddChar(input, c); + } + + // This was the only suggestion, finish autocomplete with a ':' and clear suggestions + if (maxsuggestions == 1 || !suggestions[1]) + { + M_TextInputAddChar(input, ':'); + suggestions[0] = NULL; + } + } + return ret; } diff --git a/src/m_textinput.h b/src/m_textinput.h index 20321cc3b..1cc1c5005 100644 --- a/src/m_textinput.h +++ b/src/m_textinput.h @@ -2,6 +2,7 @@ #define __M_TEXTINPUT__ #include "doomtype.h" +#include "m_emotes.h" // M_VerifyEmote, M_FindEmote typedef struct textinput_s { size_t cursor; @@ -23,7 +24,8 @@ void M_TextInputSetString(textinput_t *input, const char *c); boolean M_TextInputHandle(textinput_t *input, INT32 key); // Does everything same as M_TextInputHandle, but also handles emotes in input string -boolean M_TextInputHandleEmotes(textinput_t *input, INT32 key); +// If currently typing something that looks like an existing emote, return suggestions for it +boolean M_TextInputHandleEmotes(textinput_t *input, INT32 key, emote_t *suggestions[], int maxsuggestions); #define M_DrawTextInput(x, y, input, flags) M_DrawTextInputScroll((x), (y), (input), (flags), ((input)->buffer_size-1)*8) void M_DrawTextInputScroll(INT32 x, INT32 y, textinput_t *input, INT32 flags, INT32 MAXINPUTWIDTH);