From 13a1861c8d55a1bc286fdce5ae931e86d3039bc5 Mon Sep 17 00:00:00 2001 From: CommonLoon102 <321850+CommonLoon102@users.noreply.github.com> Date: Thu, 19 Sep 2019 06:10:18 +0000 Subject: [PATCH] Show messages by priority (#175) # Conflicts: # source/blood/src/triggers.cpp --- source/blood/src/endgame.cpp | 5 +- source/blood/src/loadsave.cpp | 5 +- source/blood/src/menu.cpp | 2 +- source/blood/src/messages.cpp | 214 ++++++++++++++++++++++++---------- source/blood/src/messages.h | 45 ++++--- source/blood/src/osdcmd.cpp | 1 + source/blood/src/player.cpp | 3 +- source/blood/src/triggers.cpp | 3 +- source/blood/src/view.cpp | 4 +- source/blood/src/view.h | 2 +- 10 files changed, 201 insertions(+), 83 deletions(-) diff --git a/source/blood/src/endgame.cpp b/source/blood/src/endgame.cpp index a7d6c1f25..bca6db976 100644 --- a/source/blood/src/endgame.cpp +++ b/source/blood/src/endgame.cpp @@ -35,6 +35,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "player.h" #include "sound.h" #include "view.h" +#include "messages.h" CEndGameMgr::CEndGameMgr() { @@ -215,10 +216,10 @@ void CSecretMgr::Found(int nType) switch (Random(2)) { case 0: - viewSetMessage("A secret is revealed."); + viewSetMessage("A secret is revealed.", 0, MESSAGE_PRIORITY_SECRET); break; case 1: - viewSetMessage("You found a secret."); + viewSetMessage("You found a secret.", 0, MESSAGE_PRIORITY_SECRET); break; } } diff --git a/source/blood/src/loadsave.cpp b/source/blood/src/loadsave.cpp index 39546b7eb..216c42565 100644 --- a/source/blood/src/loadsave.cpp +++ b/source/blood/src/loadsave.cpp @@ -148,7 +148,10 @@ void LoadSave::LoadGame(char *pzFile) bOutOfSync = 0; for (int i = 0; i < gNetPlayers; i++) playerSetRace(&gPlayer[i], gPlayer[i].at5f); - viewSetMessage(""); + if (VanillaMode()) + viewSetMessage(""); + else + gGameMessageMgr.Clear(); viewSetErrorMessage(""); if (!gGameStarted) { diff --git a/source/blood/src/menu.cpp b/source/blood/src/menu.cpp index 403ff7f71..71176c316 100644 --- a/source/blood/src/menu.cpp +++ b/source/blood/src/menu.cpp @@ -742,7 +742,7 @@ void SetupOptionsOldMenu(void) boolSlopeTilting.at20 = gSlopeTilting; boolViewBobbing.at20 = gViewVBobbing; boolViewSwaying.at20 = gViewHBobbing; - boolMessages.at20 = gGameMessageMgr.at0; + boolMessages.at20 = gGameMessageMgr.state; menuOptionsOld.Add(&itemOptionsTitle, false); menuOptionsOld.Add(&itemOption1, true); menuOptionsOld.Add(&sliderDetail, false); diff --git a/source/blood/src/messages.cpp b/source/blood/src/messages.cpp index 7e727e615..8350c7086 100644 --- a/source/blood/src/messages.cpp +++ b/source/blood/src/messages.cpp @@ -306,118 +306,214 @@ void LevelWarpAndRecord(int nEpisode, int nLevel) CGameMessageMgr::CGameMessageMgr() { - at1 = 1; - at5 = 0; + if (!VanillaMode()) + Clear(); + x = 1; + y = 0; at9 = 0; atd = 0; - at11 = 0; - at15 = 8; - at19 = 4; - at1d = 5; - at21 = 15; - at22 = 0; - at2a = at26 = 0; + nFont = 0; + fontHeight = 8; + maxNumberOfMessagesToDisplay = 4; + visibilityDurationInSecs = 5; + messageFlags = 15; + numberOfDisplayedMessages = 0; + nextMessagesIndex = messagesIndex = 0; } void CGameMessageMgr::SetState(char state) { - if (at0 && !state) + if (this->state && !state) { - at0 = 0; + this->state = 0; Clear(); } - else if (!at0 && state) - at0 = 1; + else if (!this->state && state) + this->state = 1; } -void CGameMessageMgr::Add(const char *pText, char a2, const int pal) +void CGameMessageMgr::Add(const char *pText, char a2, const int pal, const MESSAGE_PRIORITY priority) { - if (a2 && at21) + if (a2 && messageFlags) { - messageStruct *pMessage = &at2e[at2a]; - strncpy(pMessage->at4, pText, 80); - pMessage->at4[80] = 0; - pMessage->at0 = gFrameClock + at1d*120; + messageStruct *pMessage = &messages[nextMessagesIndex]; + strncpy(pMessage->text, pText, kMaxMessageTextLength-1); + pMessage->text[kMaxMessageTextLength-1] = 0; + pMessage->lastTickWhenVisible = gFrameClock + visibilityDurationInSecs*kTicRate; pMessage->pal = pal; - at2a = (at2a+1)%16; - at22++; - if (at22 > at19) + pMessage->priority = priority; + pMessage->deleted = false; + nextMessagesIndex = (nextMessagesIndex+1)%kMessageLogSize; + if (VanillaMode()) { - at26 = (at26+1)%16; - atd = 0; - at22 = at19; - at9 = at15; + numberOfDisplayedMessages++; + if (numberOfDisplayedMessages > maxNumberOfMessagesToDisplay) + { + messagesIndex = (messagesIndex+1)%kMessageLogSize; + atd = 0; + numberOfDisplayedMessages = maxNumberOfMessagesToDisplay; + at9 = fontHeight; + } } } } void CGameMessageMgr::Display(void) { - if (at22 && at0 && gInputMode != INPUT_MODE_2) + if (VanillaMode()) { - int v10 = at22; - int v18 = at26; - int vc = ClipHigh(v10*8, 48); - int v14 = gViewMode == 3 ? gViewX0S : 0; - int v8 = (gViewMode == 3 ? at5 : 0) + (int)at9; - for (int i = 0; i < v10; i++) + if (numberOfDisplayedMessages && this->state && gInputMode != INPUT_MODE_2) { - messageStruct *pMessage = &at2e[(v18+i)%16]; - if (pMessage->at0 < gFrameClock) + int initialNrOfDisplayedMsgs = numberOfDisplayedMessages; + int initialMessagesIndex = messagesIndex; + int shade = ClipHigh(initialNrOfDisplayedMsgs*8, 48); + int x = gViewMode == 3 ? gViewX0S : 0; + int y = (gViewMode == 3 ? this->y : 0) + (int)at9; + for (int i = 0; i < initialNrOfDisplayedMsgs; i++) { - at26 = (at26+1)%16; - at22--; - continue; + messageStruct* pMessage = &messages[(initialMessagesIndex+i)%kMessageLogSize]; + if (pMessage->lastTickWhenVisible < gFrameClock) + { + messagesIndex = (messagesIndex+1)%kMessageLogSize; + numberOfDisplayedMessages--; + continue; + } + viewDrawText(nFont, pMessage->text, x, y, shade, pMessage->pal, 0, false, 256); + if (gViewMode == 3) + { + int height; + gMenuTextMgr.GetFontInfo(nFont, pMessage->text, &height, NULL); + if (x+height > gViewX1S) + viewUpdatePages(); + } + y += fontHeight; + shade = ClipLow(shade-64/initialNrOfDisplayedMsgs, -128); } - viewDrawText(at11, pMessage->at4, v14, v8, vc, pMessage->pal, 0, false, 256); - if (gViewMode == 3) - { - int height; - gMenuTextMgr.GetFontInfo(at11, pMessage->at4, &height, NULL); - if (v14+height > gViewX1S) - viewUpdatePages(); - } - v8 += at15; - vc = ClipLow(vc-64/v10, -128); } - if (at9 != 0) + } + else + { + if (this->state && gInputMode != INPUT_MODE_2) { - at9 = at15*at9/120; - atd += gFrameTicks; + messageStruct* currentMessages[kMessageLogSize]; + int currentMessagesCount = 0; + for (int i = 0; i < kMessageLogSize; i++) + { + messageStruct* pMessage = &messages[i]; + if (gFrameClock < pMessage->lastTickWhenVisible && !pMessage->deleted) + { + currentMessages[currentMessagesCount++] = pMessage; + } + } + + SortMessagesByPriority(currentMessages, currentMessagesCount); + + messageStruct* messagesToDisplay[kMessageLogSize]; + int messagesToDisplayCount = 0; + for (int i = 0; i < currentMessagesCount && messagesToDisplayCount < maxNumberOfMessagesToDisplay; i++) + { + messagesToDisplay[messagesToDisplayCount++] = currentMessages[i]; + } + + SortMessagesByTime(messagesToDisplay, messagesToDisplayCount); + + int shade = ClipHigh(messagesToDisplayCount*8, 48); + int x = gViewMode == 3 ? gViewX0S : 0; + int y = (gViewMode == 3 ? this->y : 0) + (int)at9; + for (int i = 0; i < messagesToDisplayCount; i++) + { + messageStruct* pMessage = messagesToDisplay[i]; + viewDrawText(nFont, pMessage->text, x, y, shade, pMessage->pal, 0, false, 256); + if (gViewMode == 3) + { + int height; + gMenuTextMgr.GetFontInfo(nFont, pMessage->text, &height, NULL); + if (x+height > gViewX1S) + viewUpdatePages(); + } + y += fontHeight; + shade = ClipLow(shade-64/messagesToDisplayCount, -128); + } } } + if (at9 != 0) + { + at9 = fontHeight*at9/kTicRate; + atd += gFrameTicks; + } } void CGameMessageMgr::Clear(void) { - at26 = at2a = at22 = 0; + if (VanillaMode()) + { + messagesIndex = nextMessagesIndex = numberOfDisplayedMessages = 0; + } + else + { + for (int i = 0; i < kMessageLogSize; i++) + { + messageStruct* pMessage = &messages[i]; + pMessage->deleted = true; + } + } } void CGameMessageMgr::SetMaxMessages(int nMessages) { - at19 = ClipRange(nMessages, 1, 16); + maxNumberOfMessagesToDisplay = ClipRange(nMessages, 1, 16); } void CGameMessageMgr::SetFont(int nFont) { - at11 = nFont; - at15 = gFont[nFont].ySize; + this->nFont = nFont; + fontHeight = gFont[nFont].ySize; } void CGameMessageMgr::SetCoordinates(int x, int y) { - at1 = ClipRange(x, 0, gViewX1S); - at5 = ClipRange(y, 0, gViewY1S); + this->x = ClipRange(x, 0, gViewX1S); + this->y = ClipRange(y, 0, gViewY1S); } void CGameMessageMgr::SetMessageTime(int nTime) { - at1d = ClipRange(nTime, 1, 8); + visibilityDurationInSecs = ClipRange(nTime, 1, 8); } void CGameMessageMgr::SetMessageFlags(unsigned int nFlags) { - at21 = nFlags&0xf; + messageFlags = nFlags&0xf; +} + +void CGameMessageMgr::SortMessagesByPriority(messageStruct** messages, int count) { + for (int i = 1; i < count; i++) + { + for (int j = 0; j < count - i; j++) + { + if (messages[j]->priority != messages[j + 1]->priority ? messages[j]->priority < messages[j + 1]->priority : messages[j]->lastTickWhenVisible < messages[j + 1]->lastTickWhenVisible) + { + messageStruct* temp = messages[j]; + messages[j] = messages[j + 1]; + messages[j + 1] = temp; + } + } + } +} + +void CGameMessageMgr::SortMessagesByTime(messageStruct** messages, int count) { + for (int i = 1; i < count; i++) + { + for (int j = 0; j < count - i; j++) + { + if (messages[j]->lastTickWhenVisible > messages[j + 1]->lastTickWhenVisible) + { + messageStruct* temp = messages[j]; + messages[j] = messages[j + 1]; + messages[j + 1] = temp; + } + } + } } void CPlayerMsg::Clear(void) diff --git a/source/blood/src/messages.h b/source/blood/src/messages.h index 53ff33914..c184bb811 100644 --- a/source/blood/src/messages.h +++ b/source/blood/src/messages.h @@ -26,32 +26,44 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "build.h" #include "player.h" +#define kMessageLogSize 32 +#define kMaxMessageTextLength 81 + +enum MESSAGE_PRIORITY { + MESSAGE_PRIORITY_PICKUP = -10, + MESSAGE_PRIORITY_NORMAL = 0, + MESSAGE_PRIORITY_SECRET = 10, + MESSAGE_PRIORITY_INI = 20 +}; + class CGameMessageMgr { public: struct messageStruct { - ClockTicks at0; - char at4[81]; + ClockTicks lastTickWhenVisible; + char text[kMaxMessageTextLength]; int pal; + MESSAGE_PRIORITY priority; + bool deleted = false; }; - char at0; - int at1; - int at5; + char state; + int x; + int y; ClockTicks at9; ClockTicks atd; - int at11; - int at15; - int at19; - int at1d; - char at21; - int at22; - int at26; - int at2a; - messageStruct at2e[16]; + int nFont; + int fontHeight; + int maxNumberOfMessagesToDisplay; + int visibilityDurationInSecs; + char messageFlags; + int numberOfDisplayedMessages; + int messagesIndex; + int nextMessagesIndex; + messageStruct messages[kMessageLogSize]; CGameMessageMgr(); void SetState(char state); - void Add(const char *pText, char a2, const int pal = 0); + void Add(const char *pText, char a2, const int pal = 0, const MESSAGE_PRIORITY priority = MESSAGE_PRIORITY_NORMAL); void Display(void); void Clear(); void SetMaxMessages(int nMessages); @@ -59,6 +71,9 @@ public: void SetCoordinates(int x, int y); void SetMessageTime(int nTime); void SetMessageFlags(unsigned int nFlags); +private: + void SortMessagesByPriority(messageStruct** messages, int count); + void SortMessagesByTime(messageStruct** messages, int count); }; diff --git a/source/blood/src/osdcmd.cpp b/source/blood/src/osdcmd.cpp index 25c1d142c..09ed3fd3e 100644 --- a/source/blood/src/osdcmd.cpp +++ b/source/blood/src/osdcmd.cpp @@ -936,6 +936,7 @@ int32_t registerosdcommands(void) // { "hud_custom", "change the custom hud", (void *)&ud.statusbarcustom, CVAR_INT, 0, ud.statusbarrange }, // { "hud_position", "aligns the status bar to the bottom/top", (void *)&ud.hudontop, CVAR_BOOL, 0, 1 }, // { "hud_bgstretch", "enable/disable background image stretching in wide resolutions", (void *)&ud.bgstretch, CVAR_BOOL, 0, 1 }, + { "hud_messages", "enable/disable showing messages", (void *)&gMessageState, CVAR_BOOL, 0, 1 }, // { "hud_messagetime", "length of time to display multiplayer chat messages", (void *)&ud.msgdisptime, CVAR_INT, 0, 3600 }, // { "hud_numbertile", "first tile in alt hud number set", (void *)&althud_numbertile, CVAR_INT, 0, MAXUSERTILES-10 }, // { "hud_numberpal", "pal for alt hud numbers", (void *)&althud_numberpal, CVAR_INT, 0, MAXPALOOKUPS-1 }, diff --git a/source/blood/src/player.cpp b/source/blood/src/player.cpp index 268a9561b..36cd8d363 100644 --- a/source/blood/src/player.cpp +++ b/source/blood/src/player.cpp @@ -50,6 +50,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "warp.h" #include "weapon.h" #include "common_game.h" +#include "messages.h" PROFILE gProfile[kMaxPlayers]; @@ -1375,7 +1376,7 @@ void PickUp(PLAYER *pPlayer, spritetype *pSprite) pPlayer->at377 = 30; if (pPlayer == gMe) if (customMsg > 0) trTextOver(customMsg - 1); - else viewSetMessage(buffer); + else viewSetMessage(buffer, 0, MESSAGE_PRIORITY_PICKUP); } } diff --git a/source/blood/src/triggers.cpp b/source/blood/src/triggers.cpp index 6bd90764c..daba08f18 100644 --- a/source/blood/src/triggers.cpp +++ b/source/blood/src/triggers.cpp @@ -49,6 +49,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #include "triggers.h" #include "trig.h" #include "view.h" +#include "messages.h" int basePath[kMaxSectors]; @@ -3835,7 +3836,7 @@ void trTextOver(int nId) { char *pzMessage = levelGetMessage(nId); if (pzMessage) - viewSetMessage(pzMessage, VanillaMode() ? 0 : 8); // 8: gold + viewSetMessage(pzMessage, VanillaMode() ? 0 : 8, MESSAGE_PRIORITY_INI); // 8: gold } void InitGenerator(int nSprite) diff --git a/source/blood/src/view.cpp b/source/blood/src/view.cpp index b5c40bfb1..f35085d15 100644 --- a/source/blood/src/view.cpp +++ b/source/blood/src/view.cpp @@ -2783,10 +2783,10 @@ void viewBurnTime(int gScale) } } -void viewSetMessage(const char *pMessage, const int pal) +void viewSetMessage(const char *pMessage, const int pal, const MESSAGE_PRIORITY priority) { OSD_Printf("%s\n", pMessage); - gGameMessageMgr.Add(pMessage, 15, pal); + gGameMessageMgr.Add(pMessage, 15, pal, priority); } void viewDisplayMessage(void) diff --git a/source/blood/src/view.h b/source/blood/src/view.h index 6163535f5..b7bf7d36d 100644 --- a/source/blood/src/view.h +++ b/source/blood/src/view.h @@ -143,7 +143,7 @@ uspritetype *viewAddEffect(int nTSprite, VIEW_EFFECT nViewEffect); void viewProcessSprites(int32_t cX, int32_t cY, int32_t cZ, int32_t cA, int32_t smooth); void CalcOtherPosition(spritetype *pSprite, int *pX, int *pY, int *pZ, int *vsectnum, int nAng, int zm); void CalcPosition(spritetype *pSprite, int *pX, int *pY, int *pZ, int *vsectnum, int nAng, int zm); -void viewSetMessage(const char *pMessage, const int pal = 0); +void viewSetMessage(const char *pMessage, const int pal = 0, const MESSAGE_PRIORITY priority = MESSAGE_PRIORITY_NORMAL); void viewDisplayMessage(void); void viewSetErrorMessage(const char *pMessage); void DoLensEffect(void);