TrinityCore
Loading...
Searching...
No Matches
CreatureTextMgr.cpp
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore Project. See AUTHORS file for Copyright information
3 *
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by the
6 * Free Software Foundation; either version 2 of the License, or (at your
7 * option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
12 * more details.
13 *
14 * You should have received a copy of the GNU General Public License along
15 * with this program. If not, see <http://www.gnu.org/licenses/>.
16 */
17
18#include "CreatureTextMgr.h"
19#include "CreatureTextMgrImpl.h"
20#include "CellImpl.h"
21#include "Chat.h"
22#include "ChatTextBuilder.h"
23#include "Common.h"
24#include "Containers.h"
25#include "DatabaseEnv.h"
26#include "DB2Stores.h"
27#include "GridNotifiersImpl.h"
28#include "LanguageMgr.h"
29#include "Log.h"
30#include "MiscPackets.h"
31#include "ObjectMgr.h"
32#include "World.h"
33
36
42
44{
45 uint32 oldMSTime = getMSTime();
46
47 mTextMap.clear(); // for reload case
48 //all currently used temp texts are NOT reset
49
51 PreparedQueryResult result = WorldDatabase.Query(stmt);
52
53 if (!result)
54 {
55 TC_LOG_INFO("server.loading", ">> Loaded 0 ceature texts. DB table `creature_text` is empty.");
56
57 return;
58 }
59
60 uint32 textCount = 0;
61
62 do
63 {
64 Field* fields = result->Fetch();
66
67 temp.creatureId = fields[0].GetUInt32();
68 temp.groupId = fields[1].GetUInt8();
69 temp.id = fields[2].GetUInt8();
70 temp.text = fields[3].GetString();
71 temp.type = ChatMsg(fields[4].GetUInt8());
72 temp.lang = Language(fields[5].GetUInt8());
73 temp.probability = fields[6].GetFloat();
74 temp.emote = Emote(fields[7].GetUInt32());
75 temp.duration = fields[8].GetUInt32();
76 temp.sound = fields[9].GetUInt32();
77 temp.SoundPlayType = SoundKitPlayType(fields[10].GetUInt8());
78 temp.BroadcastTextId = fields[11].GetUInt32();
79 temp.TextRange = CreatureTextRange(fields[12].GetUInt8());
80
81 if (temp.sound)
82 {
83 if (!sSoundKitStore.LookupEntry(temp.sound))
84 {
85 TC_LOG_ERROR("sql.sql", "CreatureTextMgr: Entry {}, Group {} in table `creature_text` has Sound {} but sound does not exist.", temp.creatureId, temp.groupId, temp.sound);
86 temp.sound = 0;
87 }
88 }
89
91 {
92 TC_LOG_ERROR("sql.sql", "CreatureTextMgr: Entry {}, Group {} in table `creature_text` has PlayType {} but does not exist.", temp.creatureId, temp.groupId, uint32(temp.SoundPlayType));
94 }
95
96 if (temp.lang != LANG_UNIVERSAL && !sLanguageMgr->IsLanguageExist(temp.lang))
97 {
98 TC_LOG_ERROR("sql.sql", "CreatureTextMgr: Entry {}, Group {} in table `creature_text` using Language {} but Language does not exist.", temp.creatureId, temp.groupId, uint32(temp.lang));
99 temp.lang = LANG_UNIVERSAL;
100 }
101
102 if (temp.type >= MAX_CHAT_MSG_TYPE)
103 {
104 TC_LOG_ERROR("sql.sql", "CreatureTextMgr: Entry {}, Group {} in table `creature_text` has Type {} but this Chat Type does not exist.", temp.creatureId, temp.groupId, uint32(temp.type));
105 temp.type = CHAT_MSG_SAY;
106 }
107
108 if (temp.emote)
109 {
110 if (!sEmotesStore.LookupEntry(temp.emote))
111 {
112 TC_LOG_ERROR("sql.sql", "CreatureTextMgr: Entry {}, Group {} in table `creature_text` has Emote {} but emote does not exist.", temp.creatureId, temp.groupId, uint32(temp.emote));
114 }
115 }
116
117 if (temp.BroadcastTextId)
118 {
119 if (!sBroadcastTextStore.LookupEntry(temp.BroadcastTextId))
120 {
121 TC_LOG_ERROR("sql.sql", "CreatureTextMgr: Entry {}, Group {}, Id {} in table `creature_text` has non-existing or incompatible BroadcastTextId {}.", temp.creatureId, temp.groupId, temp.id, temp.BroadcastTextId);
122 temp.BroadcastTextId = 0;
123 }
124 }
125
127 {
128 TC_LOG_ERROR("sql.sql", "CreatureTextMgr: Entry {}, Group {}, Id {} in table `creature_text` has incorrect TextRange {}.", temp.creatureId, temp.groupId, temp.id, temp.TextRange);
130 }
131
132 // add the text into our entry's group
133 mTextMap[temp.creatureId][temp.groupId].push_back(temp);
134
135 ++textCount;
136 }
137 while (result->NextRow());
138
139 TC_LOG_INFO("server.loading", ">> Loaded {} creature texts for {} creatures in {} ms", textCount, mTextMap.size(), GetMSTimeDiffToNow(oldMSTime));
140}
141
143{
144 uint32 oldMSTime = getMSTime();
145
146 mLocaleTextMap.clear(); // for reload case
147
148 QueryResult result = WorldDatabase.Query("SELECT CreatureId, GroupId, ID, Locale, Text FROM creature_text_locale");
149
150 if (!result)
151 return;
152
153 do
154 {
155 Field* fields = result->Fetch();
156
157 uint32 creatureId = fields[0].GetUInt32();
158 uint32 groupId = fields[1].GetUInt8();
159 uint32 id = fields[2].GetUInt8();
160 std::string_view localeName = fields[3].GetStringView();
161
162 LocaleConstant locale = GetLocaleByName(localeName);
163 if (!IsValidLocale(locale) || locale == LOCALE_enUS)
164 continue;
165
166 CreatureTextLocale& data = mLocaleTextMap[CreatureTextId(creatureId, groupId, id)];
167 ObjectMgr::AddLocaleString(fields[4].GetStringView(), locale, data.Text);
168 } while (result->NextRow());
169
170 TC_LOG_INFO("server.loading", ">> Loaded {} creature localized texts in {} ms", uint32(mLocaleTextMap.size()), GetMSTimeDiffToNow(oldMSTime));
171}
172
173uint32 CreatureTextMgr::SendChat(Creature* source, uint8 textGroup, WorldObject const* whisperTarget /*= nullptr*/, ChatMsg msgType /*= CHAT_MSG_ADDON*/, Language language /*= LANG_ADDON*/, CreatureTextRange range /*= TEXT_RANGE_NORMAL*/, uint32 sound /*= 0*/, SoundKitPlayType playType /*= SoundKitPlayType::Normal*/, Team team /*= TEAM_OTHER*/, bool gmOnly /*= false*/, Player* srcPlr /*= nullptr*/)
174{
175 if (!source)
176 return 0;
177
178 CreatureTextMap::const_iterator sList = mTextMap.find(source->GetEntry());
179 if (sList == mTextMap.end())
180 {
181 TC_LOG_ERROR("sql.sql.creaturetextmgr", "CreatureTextMgr: Could not find Text for Creature {} ({}) in 'creature_text' table. Ignoring.", source->GetName(), source->GetGUID().ToString());
182 return 0;
183 }
184
185 CreatureTextHolder const& textHolder = sList->second;
186 CreatureTextHolder::const_iterator itr = textHolder.find(textGroup);
187 if (itr == textHolder.end())
188 {
189 TC_LOG_ERROR("sql.sql.creaturetextmgr", "CreatureTextMgr: Could not find TextGroup {} for Creature {} ({}) in 'creature_text' table. Ignoring.", uint32(textGroup), source->GetName(), source->GetGUID().ToString());
190 return 0;
191 }
192
193 CreatureTextGroup const& textGroupContainer = itr->second; //has all texts in the group
194 CreatureTextRepeatIds repeatGroup = source->GetTextRepeatGroup(textGroup);//has all textIDs from the group that were already said
195 CreatureTextGroup tempGroup;//will use this to talk after sorting repeatGroup
196
197 for (CreatureTextGroup::const_iterator giter = textGroupContainer.begin(); giter != textGroupContainer.end(); ++giter)
198 if (std::find(repeatGroup.begin(), repeatGroup.end(), giter->id) == repeatGroup.end())
199 tempGroup.push_back(*giter);
200
201 if (tempGroup.empty())
202 {
203 source->ClearTextRepeatGroup(textGroup);
204 tempGroup = textGroupContainer;
205 }
206
207 auto iter = Trinity::Containers::SelectRandomWeightedContainerElement(tempGroup, [](CreatureTextEntry const& t) -> double
208 {
209 return t.probability;
210 });
211
212 ChatMsg finalType = (msgType == CHAT_MSG_ADDON) ? iter->type : msgType;
213 Language finalLang = (language == LANG_ADDON) ? iter->lang : language;
214 uint32 finalSound = iter->sound;
215 SoundKitPlayType finalPlayType = iter->SoundPlayType;
216 BroadcastTextEntry const* bct = sBroadcastTextStore.LookupEntry(iter->BroadcastTextId);
217 if (sound)
218 {
219 finalSound = sound;
220 finalPlayType = playType;
221 }
222 else if (bct)
223 if (uint32 broadcastTextSoundId = bct->SoundKitID[source->GetGender() == GENDER_FEMALE ? 1 : 0])
224 finalSound = broadcastTextSoundId;
225
226 if (range == TEXT_RANGE_NORMAL)
227 range = iter->TextRange;
228
229 Unit* finalSource = source;
230 if (srcPlr)
231 finalSource = srcPlr;
232
233 if (srcPlr)
234 {
235 Trinity::CreatureTextTextBuilder builder(source, finalSource, finalSource->GetGender(), finalType, iter->groupId, iter->id, finalLang, whisperTarget, iter->BroadcastTextId, iter->emote, finalSound, finalPlayType, bct ? bct->ConditionID : 0);
236 SendChatPacket(finalSource, builder, finalType, whisperTarget, range, team, gmOnly);
237 }
238 else
239 {
240 Trinity::CreatureTextTextBuilder builder(finalSource, finalSource, finalSource->GetGender(), finalType, iter->groupId, iter->id, finalLang, whisperTarget, iter->BroadcastTextId, iter->emote, finalSound, finalPlayType, bct ? bct->ConditionID : 0);
241 SendChatPacket(finalSource, builder, finalType, whisperTarget, range, team, gmOnly);
242 }
243
244 source->SetTextRepeatId(textGroup, iter->id);
245 return iter->duration;
246}
247
249{
250 float dist = sWorld->getFloatConfig(CONFIG_LISTEN_RANGE_SAY);
251 switch (msgType)
252 {
254 dist = sWorld->getFloatConfig(CONFIG_LISTEN_RANGE_YELL);
255 break;
258 dist = sWorld->getFloatConfig(CONFIG_LISTEN_RANGE_TEXTEMOTE);
259 break;
260 default:
261 break;
262 }
263
264 return dist;
265}
266
267bool CreatureTextMgr::TextExist(uint32 sourceEntry, uint8 textGroup) const
268{
269 if (!sourceEntry)
270 return false;
271
272 CreatureTextMap::const_iterator sList = mTextMap.find(sourceEntry);
273 if (sList == mTextMap.end())
274 {
275 TC_LOG_DEBUG("entities.unit", "CreatureTextMgr::TextExist: Could not find Text for Creature (entry {}) in 'creature_text' table.", sourceEntry);
276 return false;
277 }
278
279 CreatureTextHolder const& textHolder = sList->second;
280 CreatureTextHolder::const_iterator itr = textHolder.find(textGroup);
281 if (itr == textHolder.end())
282 {
283 TC_LOG_DEBUG("entities.unit", "CreatureTextMgr::TextExist: Could not find TextGroup {} for Creature (entry {}).", uint32(textGroup), sourceEntry);
284 return false;
285 }
286
287 return true;
288}
289
290std::string CreatureTextMgr::GetLocalizedChatString(uint32 entry, uint8 gender, uint8 textGroup, uint32 id, LocaleConstant locale) const
291{
292 CreatureTextMap::const_iterator mapitr = mTextMap.find(entry);
293 if (mapitr == mTextMap.end())
294 return "";
295
296 CreatureTextHolder::const_iterator holderItr = mapitr->second.find(textGroup);
297 if (holderItr == mapitr->second.end())
298 return "";
299
300 CreatureTextGroup::const_iterator groupItr = holderItr->second.begin();
301 for (; groupItr != holderItr->second.end(); ++groupItr)
302 if (groupItr->id == id)
303 break;
304
305 if (groupItr == holderItr->second.end())
306 return "";
307
308 if (locale >= TOTAL_LOCALES)
309 locale = DEFAULT_LOCALE;
310
311 std::string baseText = "";
312 BroadcastTextEntry const* bct = sBroadcastTextStore.LookupEntry(groupItr->BroadcastTextId);
313
314 if (bct)
315 baseText = DB2Manager::GetBroadcastTextValue(bct, locale, gender);
316 else
317 baseText = groupItr->text;
318
319 if (locale != DEFAULT_LOCALE && !bct)
320 {
321 LocaleCreatureTextMap::const_iterator locItr = mLocaleTextMap.find(CreatureTextId(entry, uint32(textGroup), id));
322 if (locItr != mLocaleTextMap.end())
323 ObjectMgr::GetLocaleString(locItr->second.Text, locale, baseText);
324 }
325
326 return baseText;
327}
LocaleConstant GetLocaleByName(std::string_view name)
Definition Common.cpp:36
LocaleConstant
Definition Common.h:51
@ TOTAL_LOCALES
Definition Common.h:65
@ LOCALE_enUS
Definition Common.h:52
constexpr bool IsValidLocale(LocaleConstant locale)
Definition Common.h:98
#define DEFAULT_LOCALE
Definition Common.h:69
std::vector< CreatureTextEntry > CreatureTextGroup
std::unordered_map< uint8, CreatureTextGroup > CreatureTextHolder
CreatureTextRange
@ TEXT_RANGE_PERSONAL
@ TEXT_RANGE_NORMAL
std::vector< uint8 > CreatureTextRepeatIds
Definition Creature.h:78
DB2Storage< EmotesEntry > sEmotesStore("Emotes.db2", &EmotesLoadInfo::Instance)
DB2Storage< BroadcastTextEntry > sBroadcastTextStore("BroadcastText.db2", &BroadcastTextLoadInfo::Instance)
DB2Storage< SoundKitEntry > sSoundKitStore("SoundKit.db2", &SoundKitLoadInfo::Instance)
std::shared_ptr< ResultSet > QueryResult
std::shared_ptr< PreparedResultSet > PreparedQueryResult
DatabaseWorkerPool< WorldDatabaseConnection > WorldDatabase
Accessor to the world database.
uint8_t uint8
Definition Define.h:156
uint32_t uint32
Definition Define.h:154
#define sLanguageMgr
Definition LanguageMgr.h:97
#define TC_LOG_DEBUG(filterType__, message__,...)
Definition Log.h:181
#define TC_LOG_ERROR(filterType__, message__,...)
Definition Log.h:190
#define TC_LOG_INFO(filterType__, message__,...)
Definition Log.h:184
Language
@ LANG_UNIVERSAL
@ LANG_ADDON
@ GENDER_FEMALE
@ EMOTE_ONESHOT_NONE
SoundKitPlayType
ChatMsg
@ CHAT_MSG_SAY
@ CHAT_MSG_RAID_BOSS_EMOTE
@ CHAT_MSG_MONSTER_EMOTE
@ MAX_CHAT_MSG_TYPE
@ CHAT_MSG_ADDON
@ CHAT_MSG_MONSTER_YELL
uint32 GetMSTimeDiffToNow(uint32 oldMSTime)
Definition Timer.h:57
uint32 getMSTime()
Definition Timer.h:33
@ WORLD_SEL_CREATURE_TEXT
ObjectGuid const & GetGUID() const
Definition BaseEntity.h:163
LocaleCreatureTextMap mLocaleTextMap
std::string GetLocalizedChatString(uint32 entry, uint8 gender, uint8 textGroup, uint32 id, LocaleConstant locale) const
static void SendChatPacket(WorldObject *source, Builder const &builder, ChatMsg msgType, WorldObject const *whisperTarget=nullptr, CreatureTextRange range=TEXT_RANGE_NORMAL, Team team=TEAM_OTHER, bool gmOnly=false)
bool TextExist(uint32 sourceEntry, uint8 textGroup) const
static float GetRangeForChatType(ChatMsg msgType)
static CreatureTextMgr * instance()
uint32 SendChat(Creature *source, uint8 textGroup, WorldObject const *whisperTarget=nullptr, ChatMsg msgType=CHAT_MSG_ADDON, Language language=LANG_ADDON, CreatureTextRange range=TEXT_RANGE_NORMAL, uint32 sound=0, SoundKitPlayType playType=SoundKitPlayType::Normal, Team team=TEAM_OTHER, bool gmOnly=false, Player *srcPlr=nullptr)
CreatureTextMap mTextMap
CreatureTextRepeatIds GetTextRepeatGroup(uint8 textGroup)
void SetTextRepeatId(uint8 textGroup, uint8 id)
void ClearTextRepeatGroup(uint8 textGroup)
static char const * GetBroadcastTextValue(BroadcastTextEntry const *broadcastText, LocaleConstant locale=DEFAULT_LOCALE, uint8 gender=GENDER_MALE, bool forceGender=false)
Class used to access individual fields of database query result.
Definition Field.h:94
float GetFloat() const noexcept
Definition Field.cpp:85
uint32 GetUInt32() const noexcept
Definition Field.cpp:57
std::string_view GetStringView() const noexcept
Definition Field.cpp:118
uint8 GetUInt8() const noexcept
Definition Field.cpp:29
std::string GetString() const noexcept
Definition Field.cpp:113
std::string ToString() const
static void AddLocaleString(std::string_view value, LocaleConstant localeConstant, std::vector< std::string > &data)
static std::string_view GetLocaleString(std::vector< std::string > const &data, LocaleConstant locale)
Definition ObjectMgr.h:1611
uint32 GetEntry() const
Definition Object.h:89
Definition Unit.h:635
Gender GetGender() const
Definition Unit.h:767
std::string const & GetName() const
Definition Object.h:342
#define sWorld
Definition World.h:916
@ CONFIG_LISTEN_RANGE_YELL
Definition World.h:211
@ CONFIG_LISTEN_RANGE_SAY
Definition World.h:209
@ CONFIG_LISTEN_RANGE_TEXTEMOTE
Definition World.h:210
auto SelectRandomWeightedContainerElement(C const &container, std::span< double > const &weights) -> decltype(std::ranges::begin(container))
Definition Containers.h:127
std::array< uint32, 2 > SoundKitID
SoundKitPlayType SoundPlayType
CreatureTextRange TextRange
std::vector< std::string > Text