diff --git a/src/Sourcefile b/src/Sourcefile index c0829bb24..463ebb198 100644 --- a/src/Sourcefile +++ b/src/Sourcefile @@ -28,6 +28,7 @@ m_cond.c m_fixed.c m_menu.c m_textinput.c +m_emotes.cpp m_misc.c m_perfstats.c m_queue.c diff --git a/src/d_main.c b/src/d_main.c index bd7019825..2a0965024 100644 --- a/src/d_main.c +++ b/src/d_main.c @@ -40,6 +40,7 @@ #include "g_game.h" #include "g_input.h" #include "hu_stuff.h" +#include "m_emotes.h" #include "i_sound.h" #include "i_system.h" #include "i_time.h" @@ -2076,6 +2077,8 @@ void D_SRB2Main(void) S_InitMusicDefs(); + M_InitEmotes(); + CONS_Printf("ST_Init(): Init status bar.\n"); ST_Init(); diff --git a/src/d_netcmd.c b/src/d_netcmd.c index 2bd7636f9..84ace9545 100644 --- a/src/d_netcmd.c +++ b/src/d_netcmd.c @@ -21,6 +21,7 @@ #include "i_system.h" #include "g_game.h" #include "hu_stuff.h" +#include "m_emotes.h" #include "g_input.h" #include "m_menu.h" #include "r_local.h" @@ -916,6 +917,8 @@ void D_RegisterClientCommands(void) CV_RegisterVar(&cv_keyboardlayout); + CV_RegisterVar(&cv_emotes); + // time attack ghost options are also saved to config CV_RegisterVar(&cv_ghost_besttime); CV_RegisterVar(&cv_ghost_bestlap); diff --git a/src/hu_stuff.c b/src/hu_stuff.c index f8f0524b2..e125a7b7e 100644 --- a/src/hu_stuff.c +++ b/src/hu_stuff.c @@ -17,6 +17,7 @@ #include "m_menu.h" // gametype_cons_t #include "m_cond.h" // emblems +#include "m_emotes.h" #include "d_clisrv.h" @@ -93,6 +94,7 @@ static textinput_t w_chat; static boolean headsupactive = false; boolean hu_showscores; // draw rankings static char hu_tick; +static tic_t hu_emoteanim = 0; static huddrawlist_h luahuddrawlist_scores; @@ -1102,6 +1104,7 @@ void HU_Ticker(void) return; hu_tick++; + hu_emoteanim++; hu_tick &= 7; // currently only to blink chat input cursor if (PLAYER1INPUTDOWN(gc_scores)) @@ -1406,6 +1409,40 @@ boolean HU_Responder(event_t *ev) #ifndef NONET +static INT32 HU_DrawEmote(INT32 x, INT32 y, emote_t *emote, INT32 flags) +{ + if (emote->numframes == 0) + return 4; // charwidth + + const char *lumpname = emote->frames[(hu_emoteanim/emote->timeperframe) % emote->numframes]; + patch_t *emotepatch = W_CachePatchName(lumpname, PU_CACHE); + + int emotewidth = min(6, emotepatch->width); + int charheight = 6; + + fixed_t scale = (FRACUNIT/emotepatch->width)*emotewidth; + + x *= FRACUNIT; + y *= FRACUNIT; + + y -= (scale*emotepatch->height-charheight*FRACUNIT)/2; + + V_DrawFixedPatch(x, y, scale, flags, emotepatch, NULL); + + return emotewidth; +} + +static INT32 HU_EmoteWidth(emote_t *emote) +{ + if (emote->numframes == 0) + return 4; // charwidth + + const char *lumpname = emote->frames[(hu_emoteanim/emote->timeperframe) % emote->numframes]; + patch_t *emotepatch = W_CachePatchName(lumpname, PU_CACHE); + + return min(8, emotepatch->width); +} + // Precompile a wordwrapped string to any given width. // This is a muuuch better method than V_WORDWRAP. // again stolen and modified a bit from video.c, don't mind me, will need to rearrange this one day. @@ -1417,6 +1454,8 @@ static char *CHAT_WordWrap(INT32 x, INT32 w, INT32 option, const char *string) size_t slen; char *newstring = Z_StrDup(string); INT32 spacewidth = (vid.width < 640) ? 8 : 4, charwidth = (vid.width < 640) ? 8 : 4; + emote_t *emote = NULL; + int emotelen = 0; slen = strlen(string); x = 0; @@ -1438,7 +1477,12 @@ static char *CHAT_WordWrap(INT32 x, INT32 w, INT32 option, const char *string) c = toupper(c); c -= HU_FONTSTART; - if (c < 0 || c >= HU_FONTSIZE || !hu_font[c]) + if (string[i] == ':' && (emote = M_VerifyEmote(string+i, &emotelen))) + { + chw = HU_EmoteWidth(emote); + i += emotelen-1; + } + else if (c < 0 || c >= HU_FONTSIZE || !hu_font[c]) { chw = spacewidth; lastusablespace = i; @@ -1460,7 +1504,6 @@ static char *CHAT_WordWrap(INT32 x, INT32 w, INT32 option, const char *string) return newstring; } - // 30/7/18: chaty is now the distance at which the lowest point of the chat will be drawn if that makes any sense. INT16 chatx = 13, chaty = 169; // let's use this as our coordinates, shh @@ -1493,6 +1536,8 @@ static void HU_drawMiniChat(void) char *msg = CHAT_WordWrap(x+2, boxw-(charwidth*2), V_SNAPTOBOTTOM|V_SNAPTOLEFT|V_ALLOWLOWERCASE, chat_mini[i-1]); size_t j = 0; INT32 linescount = 0; + emote_t *emote = NULL; + int emotelen = 0; while(msg[j]) // iterate through msg { @@ -1517,6 +1562,11 @@ static void HU_drawMiniChat(void) ++j; } + else if (msg[j] == ':' && (emote = M_VerifyEmote(msg+j, &emotelen))) + { + dx += HU_EmoteWidth(emote) - charwidth; + j += emotelen; + } else { j++; @@ -1562,6 +1612,8 @@ static void HU_drawMiniChat(void) size_t j = 0; char *msg = CHAT_WordWrap(x+2, boxw-(charwidth*2), V_SNAPTOBOTTOM|V_SNAPTOLEFT|V_ALLOWLOWERCASE, chat_mini[i]); // get the current message, and word wrap it. UINT8 *colormap = NULL; + emote_t *emote = NULL; + int emotelen = 0; while(msg[j]) // iterate through msg { @@ -1588,6 +1640,15 @@ static void HU_drawMiniChat(void) ++j; } + else if (msg[j] == ':' && (emote = M_VerifyEmote(msg+j, &emotelen))) + { + if (cv_chatbacktint.value) // on request of wolfy + V_DrawFillConsoleMap(x + dx + 2, y+dy, HU_EmoteWidth(emote)-charwidth/2, charheight, 239|V_SNAPTOBOTTOM|V_SNAPTOLEFT); + + dx += HU_DrawEmote(x+dx+2, y+dy, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT|transflag) - charwidth; + + j += emotelen; + } else { if (cv_chatbacktint.value) // on request of wolfy @@ -1665,6 +1726,9 @@ static void HU_drawChatLog(INT32 offset) INT32 j = 0; char *msg = CHAT_WordWrap(x+2, boxw-(charwidth*2), V_SNAPTOBOTTOM|V_SNAPTOLEFT|V_ALLOWLOWERCASE, chat_log[i]); // get the current message, and word wrap it. UINT8 *colormap = NULL; + emote_t *emote = NULL; + int emotelen = 0; + while(msg[j]) // iterate through msg { if (msg[j] < HU_FONTSTART) // don't draw @@ -1686,6 +1750,11 @@ static void HU_drawChatLog(INT32 offset) ++j; } + else if (msg[j] == ':' && (emote = M_VerifyEmote(msg+j, &emotelen))) + { + dx += HU_DrawEmote(x+dx+2, y+dy, emote, V_SNAPTOBOTTOM|V_SNAPTOLEFT) - charwidth; + j += emotelen; + } else { if ((y+dy+2 >= chat_topy) && (y+dy < (chat_bottomy))) diff --git a/src/m_emotes.cpp b/src/m_emotes.cpp new file mode 100644 index 000000000..4c28f72b4 --- /dev/null +++ b/src/m_emotes.cpp @@ -0,0 +1,226 @@ +#include +#include +#include +#include + +#include "m_emotes.h" + +extern "C" { +#include "w_wad.h" +} + +#include "z_zone.h" + +consvar_t cv_emotes = {"emotes", "On", CV_SAVE, CV_OnOff, NULL, 0, NULL, NULL, 0, 0, NULL};; + +static std::map emotes; + +// TODO - Would be really nice to generalize soc-like file parsing and not reimplement it 100 times :))))) +void M_LoadEmotes(UINT16 wadnum) +{ + UINT16 lumpnum = W_CheckNumForNamePwad("EMOTES", wadnum, 0); + + if (lumpnum == INT16_MAX) + return; + + char *lump = static_cast(W_CacheLumpNumPwad(wadnum, lumpnum, PU_CACHE)); + size_t size = W_LumpLengthPwad(wadnum, lumpnum); + + std::istringstream file(std::string(lump, size)); + std::string line; + + emote_t *emote = nullptr; + std::string emote_name; + + // Just smol lambda to remove all the trailing chars we might not want + auto trim = [](std::string &str) { + str.erase(0, str.find_first_not_of(" \t\n\r\f\v")); + str.erase(str.find_last_not_of(" \t\n\r\f\v")+1); + }; + + int numemotes = 0; + int linenum = 0; + + while (std::getline(file, line)) + { + ++linenum; + + trim(line); + + if (line.empty()) + continue; + + size_t split = line.find('='); + + // Didn't find the =, means we're about to parse `Emote ` + if (split == line.npos) + { + split = line.find(' '); + + if (split == line.npos) + { + CONS_Alert(CONS_WARNING, "EMOTES: Unrecognized line. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); + continue; + } + + if (strnicmp(line.c_str(), "emote", 5)) + { + CONS_Alert(CONS_WARNING, "EMOTES: 'Emote' expected. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); + continue; + } + + emote_name = line.substr(split+1); + + 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); + continue; + } + + emote = &emotes[emote_name]; + + emote->timeperframe = 1; + emote->numframes = 0; + std::memset(emote->frames, 0, sizeof(emote->frames)); + + ++numemotes; + } + else if (emote) + { + std::string field = line.substr(0, split); + std::string value = line.substr(split+1); + + trim(field); + trim(value); + + if (stricmp(field.c_str(), "frames") == 0) + { + UINT8 numframes = 0; + + auto copy_frame = [&] { + if (numframes == MAXEMOTEFRAMES) + { + CONS_Alert(CONS_WARNING, "EMOTES: Too many frames. (file %s, line %d)", wadfiles[wadnum]->filename, linenum); + return false; + } + + size_t list_split = value.find(','); + std::string framelumpname = value.substr(0, list_split); + trim(framelumpname); + + if (list_split == value.npos) + value = ""; + else + value = value.substr(list_split+1, value.npos); + + // End of list of frames + if (framelumpname.size() == 0) + return false; + + if (framelumpname.size() > 8) + { + CONS_Alert(CONS_WARNING, "EMOTES: Frame %d name is too long. (file %s, line %d)", numframes, wadfiles[wadnum]->filename, linenum); + framelumpname = framelumpname.substr(0, 8); + } + + std::strncpy(emote->frames[numframes], framelumpname.c_str(), framelumpname.size()+1); + + ++numframes; + + return true; + }; + + while (copy_frame()) {} + + if (numframes == 0) + { + CONS_Alert(CONS_WARNING, "EMOTES: Expected list of frames. (file %s, line %d)", wadfiles[wadnum]->filename, linenum); + } + + emote->numframes = numframes; + } + else if (stricmp(field.c_str(), "timeperframe") == 0) + { + emote->timeperframe = std::stoi(value); + + if (emote->timeperframe <= 0) + { + emote->timeperframe = 1; + CONS_Alert(CONS_WARNING, "EMOTES: Bad value for 'timeperframe'. (file %s, line %d)", wadfiles[wadnum]->filename, linenum); + } + } + else + { + CONS_Alert(CONS_WARNING, "EMOTES: Unrecognized field '%s'. (file %s, line %d)", field.c_str(), wadfiles[wadnum]->filename, linenum); + } + } + else + { + CONS_Alert(CONS_WARNING, "EMOTES: Unrecognized line. (file %s, line %d)\n", wadfiles[wadnum]->filename, linenum); + } + } + + CONS_Printf("Added %d emotes\n", numemotes); +} + +void M_InitEmotes(void) +{ + UINT16 i; + for (i = 0; i < numwadfiles; i++) + M_LoadEmotes(i); +} + +emote_t *M_FindEmote(const char *name, int skip) +{ + if (!cv_emotes.value) + return nullptr; + + char query[MAXEMOTENAME+1] = {0}; + std::strncpy(query, name, MAXEMOTENAME); + + for (auto &pair: emotes) + { + if (pair.first.rfind(query, 0, MAXEMOTENAME) != 0) + continue; + + if (skip > 0) + { + --skip; + continue; + } + + return &pair.second; + } + + return nullptr; +} + +emote_t *M_VerifyEmote(const char *name, int *emotelen) +{ + if (!cv_emotes.value) + return nullptr; + + const char *p = name+1; // skip ':' + emote_t *match = nullptr; + + while (*p && (p - name) < MAXEMOTENAME) + { + if (*p == ':') + { + std::string checkname = std::string(name+1, (p-name)-1); + auto it = emotes.find(checkname); + + if (it != emotes.end()) + match = &it->second; + + break; + } + + ++p; + } + + if (match && emotelen) + *emotelen = p-name+1; + + return match; +} diff --git a/src/m_emotes.h b/src/m_emotes.h new file mode 100644 index 000000000..0290e3909 --- /dev/null +++ b/src/m_emotes.h @@ -0,0 +1,41 @@ +#ifndef __M_EMOTES__ +#define __M_EMOTES__ + +#ifdef __cplusplus +extern "C" { +#endif + +#include "doomdef.h" +#include "command.h" + +extern consvar_t cv_emotes; + +#define MAXEMOTENAME 32 +#define MAXEMOTEFRAMES 32 + +typedef struct emote_s { + char frames[MAXEMOTEFRAMES][9]; + UINT8 numframes; + tic_t timeperframe; +} emote_t; + +// Reads and adds emotes from EMOTES lump, with syntax similar to soc +void M_LoadEmotes(UINT16 wadnum); + +// Load emotes from all base wads +void M_InitEmotes(void); + +// Finds first matching emote, ignoring first few matches +emote_t *M_FindEmote(const char *name, int skips); + +// Starts from ':' character, goes until it finds ':' or end of string +// If end of string is found, or closing ':' is found but resulting emote name doesn't exist, +// it returns null, otherwise it returns emote and stores into *emotelen how much characters +// needs to be skipped to get past emote name +emote_t *M_VerifyEmote(const char *name, int *emotelen); + +#ifdef __cplusplus +} // extern "C" +#endif + +#endif // __M_EMOTES__ diff --git a/src/p_setup.c b/src/p_setup.c index 8fcdc1181..5747a5c8c 100644 --- a/src/p_setup.c +++ b/src/p_setup.c @@ -34,6 +34,7 @@ #include "r_fps.h" // R_ResetViewInterpolation in level load #include "s_sound.h" +#include "m_emotes.h" #include "st_stuff.h" #include "w_wad.h" #include "z_zone.h" @@ -3344,6 +3345,11 @@ UINT16 P_PartialAddWadFile(const char *wadfilename, boolean local) // S_LoadMusicDefs(wadnum); + // + // look for emotes + // + M_LoadEmotes(wadnum); + // TODO: Experimental SPRTINFO support, test first R_LoadSpriteInfoLumps(wadnum, wadfiles[wadnum]->numlumps);