1/* (c) Magnus Auvinen. See licence.txt in the root of the distribution for more information. */
2/* If you are missing that file, acquire a complete release at teeworlds.com. */
3#ifndef GAME_CLIENT_COMPONENTS_CHAT_H
4#define GAME_CLIENT_COMPONENTS_CHAT_H
5
6#include <base/str.h>
7
8#include <engine/console.h>
9#include <engine/shared/config.h>
10#include <engine/shared/protocol.h>
11#include <engine/shared/ringbuffer.h>
12
13#include <generated/protocol7.h>
14
15#include <game/client/component.h>
16#include <game/client/lineinput.h>
17#include <game/client/render.h>
18
19#include <vector>
20
21constexpr auto SAVES_FILE = "ddnet-saves.txt";
22
23class CChat : public CComponent
24{
25 static constexpr float CHAT_HEIGHT_FULL = 200.0f;
26 static constexpr float CHAT_HEIGHT_MIN = 50.0f;
27 static constexpr float CHAT_FONTSIZE_WIDTH_RATIO = 2.5f;
28
29 enum
30 {
31 MAX_LINES = 64,
32 };
33
34 CLineInputBuffered<MAX_CHAT_LENGTH> m_Input;
35 class CLine
36 {
37 public:
38 CLine();
39 void Reset(CChat &This);
40
41 bool m_Initialized;
42 int64_t m_Time;
43 float m_aYOffset[2];
44 int m_ClientId;
45 int m_TeamNumber;
46 bool m_Team;
47 bool m_Whisper;
48 int m_NameColor;
49 char m_aName[64];
50 char m_aText[MAX_CHAT_LENGTH];
51 bool m_Friend;
52 bool m_Highlighted;
53 std::optional<ColorRGBA> m_CustomColor;
54
55 STextContainerIndex m_TextContainerIndex;
56 int m_QuadContainerIndex;
57
58 std::shared_ptr<CManagedTeeRenderInfo> m_pManagedTeeRenderInfo;
59
60 float m_TextYOffset;
61
62 int m_TimesRepeated;
63 };
64
65 bool m_PrevScoreBoardShowed;
66 bool m_PrevShowChat;
67
68 CLine m_aLines[MAX_LINES];
69 int m_CurrentLine;
70
71 enum
72 {
73 // client IDs for special messages
74 CLIENT_MSG = -2,
75 SERVER_MSG = -1,
76 };
77
78 enum
79 {
80 MODE_NONE = 0,
81 MODE_ALL,
82 MODE_TEAM,
83 };
84
85 enum
86 {
87 CHAT_SERVER = 0,
88 CHAT_HIGHLIGHT,
89 CHAT_CLIENT,
90 CHAT_NUM,
91 };
92
93 int m_Mode;
94 bool m_Show;
95 bool m_CompletionUsed;
96 int m_CompletionChosen;
97 char m_aCompletionBuffer[MAX_CHAT_LENGTH];
98 int m_PlaceholderOffset;
99 int m_PlaceholderLength;
100 static char ms_aDisplayText[MAX_CHAT_LENGTH];
101 class CRateablePlayer
102 {
103 public:
104 int m_ClientId;
105 int m_Score;
106 };
107 CRateablePlayer m_aPlayerCompletionList[MAX_CLIENTS];
108 int m_PlayerCompletionListLength;
109
110 struct CCommand
111 {
112 char m_aName[IConsole::TEMPCMD_NAME_LENGTH];
113 char m_aParams[IConsole::TEMPCMD_PARAMS_LENGTH];
114 char m_aHelpText[IConsole::TEMPCMD_HELP_LENGTH];
115
116 CCommand() = default;
117 CCommand(const char *pName, const char *pParams, const char *pHelpText)
118 {
119 str_copy(dst&: m_aName, src: pName);
120 str_copy(dst&: m_aParams, src: pParams);
121 str_copy(dst&: m_aHelpText, src: pHelpText);
122 }
123
124 bool operator<(const CCommand &Other) const { return str_comp(a: m_aName, b: Other.m_aName) < 0; }
125 bool operator<=(const CCommand &Other) const { return str_comp(a: m_aName, b: Other.m_aName) <= 0; }
126 bool operator==(const CCommand &Other) const { return str_comp(a: m_aName, b: Other.m_aName) == 0; }
127 };
128
129 std::vector<CCommand> m_vServerCommands;
130 bool m_ServerCommandsNeedSorting;
131
132 struct CHistoryEntry
133 {
134 int m_Team;
135 char m_aText[1];
136 };
137 CHistoryEntry *m_pHistoryEntry;
138 CStaticRingBuffer<CHistoryEntry, 64 * 1024, CRingBufferBase::FLAG_RECYCLE> m_History;
139 int m_PendingChatCounter;
140 int64_t m_LastChatSend;
141 int64_t m_aLastSoundPlayed[CHAT_NUM];
142 bool m_IsInputCensored;
143 char m_aCurrentInputText[MAX_CHAT_LENGTH];
144 bool m_EditingNewLine;
145
146 bool m_ServerSupportsCommandInfo;
147
148 static void ConSay(IConsole::IResult *pResult, void *pUserData);
149 static void ConSayTeam(IConsole::IResult *pResult, void *pUserData);
150 static void ConChat(IConsole::IResult *pResult, void *pUserData);
151 static void ConShowChat(IConsole::IResult *pResult, void *pUserData);
152 static void ConEcho(IConsole::IResult *pResult, void *pUserData);
153 static void ConEchoConfig(IConsole::IResult *pResult, void *pUserData);
154 static void ConClearChat(IConsole::IResult *pResult, void *pUserData);
155
156 static void ConchainChatOld(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
157 static void ConchainChatFontSize(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
158 static void ConchainChatWidth(IConsole::IResult *pResult, void *pUserData, IConsole::FCommandCallback pfnCallback, void *pCallbackUserData);
159
160 bool LineShouldHighlight(const char *pLine, const char *pName);
161 void StoreSave(const char *pText);
162
163public:
164 CChat();
165 int Sizeof() const override { return sizeof(*this); }
166
167 static constexpr float MESSAGE_TEE_PADDING_RIGHT = 0.5f;
168
169 bool IsActive() const { return m_Mode != MODE_NONE; }
170 void AddLine(int ClientId, int Team, const char *pLine);
171 void EnableMode(int Team);
172 void DisableMode();
173 void RegisterCommand(const char *pName, const char *pParams, const char *pHelpText);
174 void UnregisterCommand(const char *pName);
175 void Echo(const char *pString);
176 void EchoConfig(const char *pName);
177
178 void OnWindowResize() override;
179 void OnConsoleInit() override;
180 void OnStateChange(int NewState, int OldState) override;
181 void OnRender() override;
182 void OnPrepareLines(float y);
183 void Reset();
184 void OnRelease() override;
185 void OnMessage(int MsgType, void *pRawMsg) override;
186 bool OnInput(const IInput::CEvent &Event) override;
187 void OnInit() override;
188
189 void RebuildChat();
190 void ClearLines();
191
192 void EnsureCoherentFontSize() const;
193 void EnsureCoherentWidth() const;
194
195 float FontSize() const { return g_Config.m_ClChatFontSize / 10.0f; }
196 float MessagePaddingX() const { return FontSize() * (5 / 6.f); }
197 float MessagePaddingY() const { return FontSize() * (1 / 6.f); }
198 float MessageTeeSize() const { return FontSize() * (7 / 6.f); }
199 float MessageRounding() const { return FontSize() * (1 / 2.f); }
200
201 // ----- send functions -----
202
203 // Sends a chat message to the server.
204 //
205 // @param Team MODE_ALL=0 MODE_TEAM=1
206 // @param pLine the chat message
207 void SendChat(int Team, const char *pLine);
208
209 // Sends a chat message to the server.
210 //
211 // It uses a queue with a maximum of 3 entries
212 // that ensures there is a minimum delay of one second
213 // between sent messages.
214 //
215 // It uses team or public chat depending on m_Mode.
216 void SendChatQueued(const char *pLine);
217};
218#endif
219