200{
201 if (!source)
202 return 0;
203
206 {
207 LOG_ERROR(
"sql.sql",
"CreatureTextMgr: Could not find Text for Creature({}) Entry {} in 'creature_text' table. Ignoring.", source->
GetName(), source->
GetEntry());
208 return 0;
209 }
210
212 CreatureTextHolder::const_iterator itr = textHolder.find(textGroup);
213 if (itr == textHolder.end())
214 {
215 LOG_ERROR(
"sql.sql",
"CreatureTextMgr: Could not find TextGroup {} for Creature {} ({}). Ignoring.",
217 return 0;
218 }
219
223
224 for (CreatureTextGroup::const_iterator giter = textGroupContainer.begin(); giter != textGroupContainer.end(); ++giter)
225 if (std::find(repeatGroup.begin(), repeatGroup.end(), giter->id) == repeatGroup.end())
226 tempGroup.push_back(*giter);
227
228 if (tempGroup.empty())
229 {
232 {
233 CreatureTextRepeatGroup::iterator groupItr = mapItr->second.find(textGroup);
234 groupItr->second.clear();
235 }
236
237 tempGroup = textGroupContainer;
238 }
239
241 float lastChance = -1;
242 bool isEqualChanced = true;
243
244 float totalChance = 0;
245
246 for (CreatureTextGroup::const_iterator iter = tempGroup.begin(); iter != tempGroup.end(); ++iter)
247 {
248 if (lastChance >= 0 && lastChance != iter->probability)
249 isEqualChanced = false;
250
251 lastChance = iter->probability;
252 totalChance += iter->probability;
253 ++count;
254 }
255
257 if (!isEqualChanced)
258 {
259 for (CreatureTextGroup::const_iterator iter = tempGroup.begin(); iter != tempGroup.end(); ++iter)
260 {
263 ++offset;
264 if (r <= chance)
265 break;
266 }
267 }
268
270 if (isEqualChanced || offset < 0)
271 pos =
urand(0, count - 1);
272 else if (offset >= 0)
273 pos = offset;
274
275 CreatureTextGroup::const_iterator iter = tempGroup.begin() + pos;
276
279 uint32 finalSound = sound ? sound : iter->sound;
280
282 range = iter->TextRange;
283
284 if (finalSound)
285 SendSound(source, finalSound, finalType, target, range, teamId, gmOnly);
286
287 Unit* finalSource = source;
288 if (srcPlr)
289 finalSource = srcPlr;
290
291 if (iter->emote)
293
294 if (srcPlr)
295 {
296 PlayerTextBuilder builder(source, finalSource, finalSource->
getGender(), finalType, iter->group, iter->id, finalLang, target);
297 SendChatPacket(finalSource, builder, finalType, target, range, teamId, gmOnly);
298 }
299 else
300 {
302 SendChatPacket(finalSource, builder, finalType, target, range, teamId, gmOnly);
303 }
304 if (isEqualChanced || (!isEqualChanced && totalChance == 100.0f))
306
307 return iter->duration;
308}
uint32 urand(uint32 min, uint32 max)
Definition: Random.cpp:44
std::int32_t int32
Definition: Define.h:103
@ CHAT_MSG_ADDON
Definition: SharedDefines.h:3151
@ LANG_ADDON
Definition: SharedDefines.h:753
std::vector< CreatureTextEntry > CreatureTextGroup
Definition: CreatureTextMgr.h:73
std::unordered_map< uint8, CreatureTextGroup > CreatureTextHolder
Definition: CreatureTextMgr.h:74
uint32 GetEntry() const
Definition: Object.h:115
std::string const & GetName() const
Definition: Object.h:463
std::string ToString() const
Definition: ObjectGuid.cpp:47
uint8 getGender() const
Definition: Unit.h:805
Definition: CreatureTextMgr.cpp:29
Definition: CreatureTextMgr.cpp:52
void SendChatPacket(WorldObject *source, Builder const &builder, ChatMsg msgType, WorldObject const *target=nullptr, CreatureTextRange range=TEXT_RANGE_NORMAL, TeamId teamId=TEAM_NEUTRAL, bool gmOnly=false) const
Definition: CreatureTextMgr.h:179
void SetRepeatId(Creature *source, uint8 textGroup, uint8 id)
Definition: CreatureTextMgr.cpp:413
void SendEmote(Unit *source, uint32 emote)
Definition: CreatureTextMgr.cpp:405
void SendSound(Creature *source, uint32 sound, ChatMsg msgType, WorldObject const *target, CreatureTextRange range, TeamId teamId, bool gmOnly)
Definition: CreatureTextMgr.cpp:329
CreatureTextRepeatIds GetRepeatGroup(Creature *source, uint8 textGroup)
Definition: CreatureTextMgr.cpp:426