Browse Source

Add Initial Chat Functionality

Moves three chat log items to devilution namespace and adds commands for reading from and writing to chat.
pull/7983/head
NiteKat 10 months ago
parent
commit
62c1a077fc
  1. 5
      Source/dapi/Backend/Messages/command.proto
  2. 1
      Source/dapi/Backend/Messages/game.proto
  3. 1
      Source/dapi/GameData.h
  4. 34
      Source/dapi/Server.cpp
  5. 2
      Source/dapi/Server.h
  6. 14
      Source/qol/chatlog.cpp
  7. 11
      Source/qol/chatlog.h

5
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;
}
}

1
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;
}

1
Source/dapi/GameData.h

@ -31,6 +31,7 @@ struct GameData {
bool invflag;
bool qtextflag;
int currlevel;
size_t lastLogSize;
std::map<int, PlayerData> playerList;
std::vector<ItemData> itemList;

34
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<char>(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

2
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;

14
Source/qol/chatlog.cpp

@ -27,24 +27,14 @@
namespace devilution {
namespace {
struct ColoredText {
std::string text;
UiFlags color;
};
std::vector<MultiColoredText> ChatLogLines;
struct MultiColoredText {
std::string text;
std::vector<ColoredText> colors;
};
namespace {
bool UnreadFlag = false;
size_t SkipLines;
unsigned int MessageCounter = 0;
std::vector<MultiColoredText> ChatLogLines;
constexpr int PaddingTop = 32;
constexpr int PaddingLeft = 32;

11
Source/qol/chatlog.h

@ -11,7 +11,18 @@
namespace devilution {
struct ColoredText {
std::string text;
UiFlags color;
};
struct MultiColoredText {
std::string text;
std::vector<ColoredText> colors;
};
extern bool ChatLogFlag;
extern std::vector<MultiColoredText> ChatLogLines;
void ToggleChatLog();
void AddMessageToChatLog(std::string_view message, Player *player = nullptr, UiFlags flags = UiFlags::ColorWhite);

Loading…
Cancel
Save