aboutsummaryrefslogtreecommitdiff
path: root/src/server/game/Texts/CreatureTextMgr.cpp
diff options
context:
space:
mode:
authorRat <none@none>2010-09-26 20:48:25 +0200
committerRat <none@none>2010-09-26 20:48:25 +0200
commitbbf86641a1c35fcc66de30d7a79175c31effde88 (patch)
tree165aa4c561800bf54d7037eb5f1fd7cf5b0f8801 /src/server/game/Texts/CreatureTextMgr.cpp
parent133ab88f8c43edf6e987649c86ad4f7b684c2886 (diff)
Core/Scripts/SmartAI: removed test code
Core/CreatureTextMgr: added reload command handle text from DB handle grouped texts handle repeatable/single texts based on probability todo: sound/emote, code cleanup --HG-- branch : trunk
Diffstat (limited to 'src/server/game/Texts/CreatureTextMgr.cpp')
-rw-r--r--src/server/game/Texts/CreatureTextMgr.cpp132
1 files changed, 131 insertions, 1 deletions
diff --git a/src/server/game/Texts/CreatureTextMgr.cpp b/src/server/game/Texts/CreatureTextMgr.cpp
index 9557362ea36..9e165adc8fb 100644
--- a/src/server/game/Texts/CreatureTextMgr.cpp
+++ b/src/server/game/Texts/CreatureTextMgr.cpp
@@ -26,6 +26,7 @@
void CreatureTextMgr::LoadCreatureTexts()
{
mTextMap.clear(); // for reload case
+ mTextRepeatMap.clear(); //reset all currently used temp texts
PreparedStatement* stmt = WorldDatabase.GetPreparedStatement(WORLD_LOAD_CRETEXT);
PreparedQueryResult result = WorldDatabase.Query(stmt);
@@ -108,8 +109,131 @@ void CreatureTextMgr::LoadCreatureTexts()
sLog.outString(">> Loaded %u Creature Texts for %u Creatures.", TextCount, CreatureCount);
}
-void CreatureTextMgr::SendChat(WorldObject* source, char const* text, ChatType msgtype, Language language, uint64 whisperGuid, TextRange range) const
+void CreatureTextMgr::SendChat(Creature* source, uint8 textGroup, uint64 whisperGuid, ChatType msgtype, Language language, TextRange range)
{
+ if (!source)
+ return;
+ CreatureTextMap::const_iterator sList = mTextMap.find(source->GetEntry());
+ if (sList == mTextMap.end())
+ {
+ sLog.outErrorDb("CreatureTextMgr: Could not find Text for Creature(%s) Entry %u in 'creature_text' table. Ignoring.",source->GetName(), source->GetEntry());
+ return;
+ }
+ CreatureTextHolder TextHolder = (*sList).second;
+ CreatureTextHolder::const_iterator itr = TextHolder.find(textGroup);
+ if (itr == TextHolder.end())
+ {
+ sLog.outErrorDb("CreatureTextMgr: Could not find TextGroup %u for Creature(%s) GuidLow %u Entry %u. Ignoring.",uint32(textGroup), source->GetName(), source->GetGUIDLow(), source->GetEntry());
+ return;
+ }
+ CreatureTextGroup TextGroup = (*itr).second;//has all texts in the group
+ CreatureTextRepeatIds repeatGroup = GetRepeatGroup(source,textGroup);//has all textIDs from the group that were already said
+ CreatureTextGroup tempGroup;//will use this to talk after sorting repeatGroup
+
+ for (CreatureTextGroup::const_iterator giter = TextGroup.begin(); giter != TextGroup.end(); ++giter)
+ {
+ if (std::find(repeatGroup.begin(), repeatGroup.end(), (*giter).id) == repeatGroup.end())
+ tempGroup.push_back((*giter));
+ }
+ if (tempGroup.empty())
+ {
+ //mTextRepeatMap[source->GetGUID()][textGroup].clear();
+ CreatureTextRepeatMap::iterator mapItr = mTextRepeatMap.find(source->GetGUID());
+ if (mapItr != mTextRepeatMap.end())
+ {
+ CreatureTextRepeatGroup::iterator groupItr = (*mapItr).second.find(textGroup);
+ (*groupItr).second.clear();
+ }
+ tempGroup = TextGroup;
+ }
+ uint8 count = 0;
+ float lastChance = -1;
+ bool isEqualChanced = true;
+
+ float totalChance = 0;
+
+ for (CreatureTextGroup::const_iterator iter = tempGroup.begin(); iter != tempGroup.end(); ++iter)
+ {
+ if (lastChance >= 0 && lastChance != (*iter).probability)
+ isEqualChanced = false;
+
+ lastChance = (*iter).probability;
+ totalChance += (*iter).probability;
+ count++;
+ }
+ if (isEqualChanced)
+ {
+ uint32 pos = urand(0,count - 1);
+ CreatureTextGroup::const_iterator iter = tempGroup.begin() + pos;
+ SendChatString(source, (*iter).text.c_str(), (*iter).type, (*iter).lang, whisperGuid);
+ SetRepeatId(source, textGroup, (*iter).id);
+ }else
+ {
+ for (CreatureTextGroup::const_iterator iter = tempGroup.begin(); iter != tempGroup.end(); ++iter)
+ {
+ uint32 chance = uint32((*iter).probability);
+ uint32 r = urand(0, 100);
+ if (r <= chance)
+ {
+ SendChatString(source, (*iter).text.c_str(), (*iter).type, (*iter).lang, whisperGuid);
+ if (totalChance == 100.0f)// only if total chances are 100 do not repeat
+ SetRepeatId(source, textGroup, (*iter).id);
+ return;
+ }
+ }
+ uint32 pos = urand(0,count - 1);
+ CreatureTextGroup::const_iterator iter = tempGroup.begin() + pos;
+ SendChatString(source, (*iter).text.c_str(), (*iter).type, (*iter).lang, whisperGuid);
+ SetRepeatId(source, textGroup, (*iter).id);
+ }
+}
+
+void CreatureTextMgr::SetRepeatId(Creature* source, uint8 textGroup, uint8 id)
+{
+ if (!source)
+ return;
+
+ if (mTextRepeatMap.find(source->GetGUID()) == mTextRepeatMap.end())
+ {
+ CreatureTextRepeatGroup TextGroup;
+ mTextRepeatMap[source->GetGUID()] = TextGroup;
+ }
+ if (mTextRepeatMap[source->GetGUID()].find(textGroup) == mTextRepeatMap[source->GetGUID()].end())
+ {
+ CreatureTextRepeatIds ids;
+ mTextRepeatMap[source->GetGUID()][textGroup] = ids;
+ }
+
+ if (std::find(mTextRepeatMap[source->GetGUID()][textGroup].begin(), mTextRepeatMap[source->GetGUID()][textGroup].end(), id) == mTextRepeatMap[source->GetGUID()][textGroup].end())
+ {
+ mTextRepeatMap[source->GetGUID()][textGroup].push_back(id);
+ }
+ else
+ sLog.outErrorDb("CreatureTextMgr: TextGroup %u for Creature(%s) GuidLow %u Entry %u, id %u already added",uint32(textGroup), source->GetName(), source->GetGUIDLow(), source->GetEntry(), uint32(id));
+}
+
+CreatureTextRepeatIds CreatureTextMgr::GetRepeatGroup(Creature* source, uint8 textGroup)
+{
+ ASSERT(source);//should never happen
+ CreatureTextRepeatIds ids;
+
+ CreatureTextRepeatMap::const_iterator mapItr = mTextRepeatMap.find(source->GetGUID());
+ if (mapItr != mTextRepeatMap.end())
+ {
+ CreatureTextRepeatGroup::const_iterator groupItr = (*mapItr).second.find(textGroup);
+ if (groupItr != (*mapItr).second.end())
+ {
+ ids = (*groupItr).second;
+ }
+ }
+ return ids;
+}
+
+void CreatureTextMgr::SendChatString(WorldObject* source, char const* text, ChatType msgtype, Language language, uint64 whisperGuid, TextRange range) const
+{
+ if (!source)
+ return;
+
WorldPacket data(SMSG_MESSAGECHAT, 200);
BuildMonsterChat(&data, source, msgtype, text, language, whisperGuid);//build our packet
SendChatPacket(&data, source, msgtype, whisperGuid, range);//send our packet
@@ -117,6 +241,9 @@ void CreatureTextMgr::SendChat(WorldObject* source, char const* text, ChatType m
void CreatureTextMgr::BuildMonsterChat(WorldPacket *data, WorldObject* source, ChatType msgtype, char const* text, Language language, uint64 whisperGuid) const
{
+ if (!source)
+ return;
+
ChatMsg sendType = CHAT_MSG_MONSTER_SAY;
switch (msgtype)
{
@@ -172,6 +299,9 @@ void CreatureTextMgr::BuildMonsterChat(WorldPacket *data, WorldObject* source, C
void CreatureTextMgr::SendChatPacket(WorldPacket *data, WorldObject* source, ChatType msgtype, uint64 whisperGuid, TextRange range) const
{
+ if (!source)
+ return;
+
float dist = sWorld.getFloatConfig(CONFIG_LISTEN_RANGE_SAY);
switch (msgtype)