diff --git a/Source/dapi/Backend/Messages/command.proto b/Source/dapi/Backend/Messages/command.proto index ced3e1f05..c9c365624 100644 --- a/Source/dapi/Backend/Messages/command.proto +++ b/Source/dapi/Backend/Messages/command.proto @@ -141,6 +141,10 @@ message IdentifyItem { uint32 ID = 1; } +message SendChat { + string message = 1; +} + message Command { oneof command { Move move = 1; @@ -176,5 +180,6 @@ message Command { Quit quit = 31; ClearCursor clearCursor = 32; IdentifyItem identifyItem = 33; + SendChat sendChat = 34; } } diff --git a/Source/dapi/Backend/Messages/game.proto b/Source/dapi/Backend/Messages/game.proto index df7e58a26..a5d28451e 100644 --- a/Source/dapi/Backend/Messages/game.proto +++ b/Source/dapi/Backend/Messages/game.proto @@ -34,4 +34,5 @@ message FrameUpdate { repeated dapi.data.MissileData missileData = 26; repeated dapi.data.PortalData portalData = 27; repeated dapi.data.QuestData questData = 28; + repeated string chatMessages = 29; } diff --git a/Source/dapi/GameData.h b/Source/dapi/GameData.h index 0edd16505..0a245483e 100644 --- a/Source/dapi/GameData.h +++ b/Source/dapi/GameData.h @@ -31,6 +31,7 @@ struct GameData { bool invflag; bool qtextflag; int currlevel; + size_t lastLogSize; std::map playerList; std::vector itemList; diff --git a/Source/dapi/Server.cpp b/Source/dapi/Server.cpp index eba31d230..bb90a8f9f 100644 --- a/Source/dapi/Server.cpp +++ b/Source/dapi/Server.cpp @@ -201,6 +201,9 @@ void Server::processMessages() } else if (command.has_identifyitem()) { auto identifyItem = command.identifyitem(); this->identifyItem(identifyItem.id()); + } else if (command.has_sendchat()) { + auto sendChat = command.sendchat(); + this->sendChat(sendChat.message()); } issuedCommand = true; if (command.has_setfps()) { @@ -414,6 +417,24 @@ void Server::updateGameData() update->set_connectedto(1); + for (auto chatLogLine = data->lastLogSize; chatLogLine < devilution::ChatLogLines.size(); chatLogLine++) + { + std::stringstream message; + for (auto &textLine : devilution::ChatLogLines[chatLogLine].colors) + { + if (devilution::HasAnyOf(textLine.color, devilution::UiFlags::ColorWhitegold & devilution::UiFlags::ColorBlue)) + message << textLine.text << ": "; + if (devilution::HasAnyOf(textLine.color, devilution::UiFlags::ColorWhite)) + message << textLine.text; + } + if (message.str().size()) + { + auto chatMessage = update->add_chatmessages(); + *chatMessage = message.str(); + } + } + data->lastLogSize = devilution::ChatLogLines.size(); + update->set_player(devilution::MyPlayerId); update->set_stextflag(static_cast(devilution::ActiveStore)); @@ -2448,4 +2469,17 @@ void Server::identifyItem(int itemID) } } } + +void Server::sendChat(std::string message) +{ + if (!devilution::gbIsMultiplayer) + return; + + if (79 < message.length()) + message = message.substr(0, 79); + + char charMsg[MAX_SEND_STR_LEN]; + devilution::CopyUtf8(charMsg, message, sizeof(charMsg)); + devilution::NetSendCmdString(0xFFFFFF, charMsg); +} } // namespace DAPI diff --git a/Source/dapi/Server.h b/Source/dapi/Server.h index 69f69052f..af1247b8d 100644 --- a/Source/dapi/Server.h +++ b/Source/dapi/Server.h @@ -25,6 +25,7 @@ #include "msg.h" #include "engine/random.hpp" #include "gamemenu.h" +#include "qol/chatlog.h" namespace DAPI { enum struct CommandType { @@ -247,6 +248,7 @@ private: void quit(); void clearCursor(); void identifyItem(int itemID); + void sendChat(std::string message); bool listening = false; diff --git a/Source/qol/chatlog.cpp b/Source/qol/chatlog.cpp index 5704cdd5d..fc1065623 100644 --- a/Source/qol/chatlog.cpp +++ b/Source/qol/chatlog.cpp @@ -27,24 +27,14 @@ namespace devilution { -namespace { - -struct ColoredText { - std::string text; - UiFlags color; -}; +std::vector ChatLogLines; -struct MultiColoredText { - std::string text; - std::vector colors; -}; +namespace { bool UnreadFlag = false; size_t SkipLines; unsigned int MessageCounter = 0; -std::vector ChatLogLines; - constexpr int PaddingTop = 32; constexpr int PaddingLeft = 32; diff --git a/Source/qol/chatlog.h b/Source/qol/chatlog.h index 5291684f5..c13b1cde2 100644 --- a/Source/qol/chatlog.h +++ b/Source/qol/chatlog.h @@ -11,7 +11,18 @@ namespace devilution { +struct ColoredText { + std::string text; + UiFlags color; +}; + +struct MultiColoredText { + std::string text; + std::vector colors; +}; + extern bool ChatLogFlag; +extern std::vector ChatLogLines; void ToggleChatLog(); void AddMessageToChatLog(std::string_view message, Player *player = nullptr, UiFlags flags = UiFlags::ColorWhite);