Core/CreatureTextMgr: Added support for creature_text localization

This commit is contained in:
Shauren
2012-03-01 22:26:05 +01:00
parent 91e7dc8561
commit 09f0a2c04f
6 changed files with 455 additions and 191 deletions

View File

@@ -0,0 +1,15 @@
DROP TABLE IF EXISTS `locales_creature_text`;
CREATE TABLE `locales_creature_text` (
`entry` int(10) UNSIGNED NOT NULL,
`textGroup` tinyint(3) UNSIGNED NOT NULL,
`id` int(10) UNSIGNED NOT NULL,
`text_loc1` text,
`text_loc2` text,
`text_loc3` text,
`text_loc4` text,
`text_loc5` text,
`text_loc6` text,
`text_loc7` text,
`text_loc8` text,
PRIMARY KEY (`entry`,`textGroup`,`id`)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

View File

@@ -34,6 +34,46 @@
#include "Vehicle.h"
#include "ScriptedGossip.h"
class TrinityStringTextBuilder
{
public:
TrinityStringTextBuilder(WorldObject* obj, ChatMsg msgtype, int32 id, uint32 language, uint64 targetGUID)
: _source(obj), _msgType(msgtype), _textId(id), _language(language), _targetGUID(targetGUID)
{
}
size_t operator()(WorldPacket* data, LocaleConstant locale) const
{
std::string text = sObjectMgr->GetTrinityString(_textId, locale);
char const* localizedName = _source->GetNameForLocaleIdx(locale);
*data << uint8(_msgType);
*data << uint32(_language);
*data << uint64(_source->GetGUID());
*data << uint32(1); // 2.1.0
*data << uint32(strlen(localizedName)+1);
*data << localizedName;
size_t whisperGUIDpos = data->wpos();
*data << uint64(_targetGUID); // Unit Target
if (_targetGUID && !IS_PLAYER_GUID(_targetGUID))
{
*data << uint32(1); // target name length
*data << uint8(0); // target name
}
*data << uint32(text.length() + 1);
*data << text;
*data << uint8(0); // ChatTag
return whisperGUIDpos;
}
WorldObject* _source;
ChatMsg _msgType;
int32 _textId;
uint32 _language;
uint64 _targetGUID;
};
SmartScript::SmartScript()
{
go = NULL;
@@ -695,7 +735,10 @@ void SmartScript::ProcessAction(SmartScriptHolder& e, Unit* unit, uint32 var0, u
me->DoFleeToGetAssistance();
if (e.action.flee.withEmote)
sCreatureTextMgr->SendChatString(me, sObjectMgr->GetTrinityStringForDBCLocale(LANG_FLEE), CHAT_MSG_MONSTER_EMOTE);
{
TrinityStringTextBuilder builder(me, CHAT_MSG_MONSTER_EMOTE, LANG_FLEE, LANG_UNIVERSAL, 0);
sCreatureTextMgr->SendChatPacket(me, builder, CHAT_MSG_MONSTER_EMOTE);
}
sLog->outDebug(LOG_FILTER_DATABASE_AI, "SmartScript::ProcessAction:: SMART_ACTION_FLEE_FOR_ASSIST: Creature %u DoFleeToGetAssistance", me->GetGUIDLow());
break;
}

View File

@@ -17,8 +17,53 @@
#include "Common.h"
#include "DatabaseEnv.h"
#include "CreatureTextMgr.h"
#include "ObjectMgr.h"
#include "Cell.h"
#include "CellImpl.h"
#include "GridNotifiers.h"
#include "GridNotifiersImpl.h"
#include "CreatureTextMgr.h"
class CreatureTextBuilder
{
public:
CreatureTextBuilder(WorldObject* obj, ChatMsg msgtype, uint8 textGroup, uint32 id, uint32 language, uint64 targetGUID)
: _source(obj), _msgType(msgtype), _textGroup(textGroup), _textId(id), _language(language), _targetGUID(targetGUID)
{
}
size_t operator()(WorldPacket* data, LocaleConstant locale) const
{
std::string text = sCreatureTextMgr->GetLocalizedChatString(_source->GetEntry(), _textGroup, _textId, locale);
char const* localizedName = _source->GetNameForLocaleIdx(locale);
*data << uint8(_msgType);
*data << uint32(_language);
*data << uint64(_source->GetGUID());
*data << uint32(1); // 2.1.0
*data << uint32(strlen(localizedName)+1);
*data << localizedName;
size_t whisperGUIDpos = data->wpos();
*data << uint64(_targetGUID); // Unit Target
if (_targetGUID && !IS_PLAYER_GUID(_targetGUID))
{
*data << uint32(1); // target name length
*data << uint8(0); // target name
}
*data << uint32(text.length() + 1);
*data << text;
*data << uint8(0); // ChatTag
return whisperGUIDpos;
}
WorldObject* _source;
ChatMsg _msgType;
uint8 _textGroup;
uint32 _textId;
uint32 _language;
uint64 _targetGUID;
};
void CreatureTextMgr::LoadCreatureTexts()
{
@@ -83,17 +128,8 @@ void CreatureTextMgr::LoadCreatureTexts()
}
//entry not yet added, add empty TextHolder (list of groups)
if (mTextMap.find(temp.entry) == mTextMap.end())
{
++creatureCount;
CreatureTextHolder TextHolder;
mTextMap[temp.entry] = TextHolder;
}
//group not yet added, add empty TextGroup (list of texts)
if (mTextMap[temp.entry].find(temp.group) == mTextMap[temp.entry].end())
{
CreatureTextGroup TextGroup;
mTextMap[temp.entry][temp.group] = TextGroup;
}
//add the text into our entry's group
mTextMap[temp.entry][temp.group].push_back(temp);
@@ -104,42 +140,76 @@ void CreatureTextMgr::LoadCreatureTexts()
sLog->outString();
}
void CreatureTextMgr::LoadCreatureTextLocales()
{
uint32 oldMSTime = getMSTime();
mLocaleTextMap.clear(); // for reload case
QueryResult result = WorldDatabase.Query("SELECT entry, textGroup, id, text_loc1, text_loc2, text_loc3, text_loc4, text_loc5, text_loc6, text_loc7, text_loc8 FROM locales_creature_text");
if (!result)
return;
uint32 textCount = 0;
do
{
Field* fields = result->Fetch();
CreatureTextLocale& loc = mLocaleTextMap[CreatureTextId(fields[0].GetUInt32(), uint32(fields[1].GetUInt8()), fields[2].GetUInt32())];
for (uint8 i = 1; i < TOTAL_LOCALES; ++i)
{
LocaleConstant locale = LocaleConstant(i);
ObjectMgr::AddLocaleString(fields[2 + i - 1].GetString(), locale, loc.Text);
}
++textCount;
} while (result->NextRow());
sLog->outString(">> Loaded %u creature localized texts in %u ms", textCount, GetMSTimeDiffToNow(oldMSTime));
sLog->outString();
}
uint32 CreatureTextMgr::SendChat(Creature* source, uint8 textGroup, uint64 whisperGuid /*= 0*/, ChatMsg msgType /*= CHAT_MSG_ADDON*/, Language language /*= LANG_ADDON*/, TextRange range /*= TEXT_RANGE_NORMAL*/, uint32 sound /*= 0*/, Team team /*= TEAM_OTHER*/, bool gmOnly /*= false*/, Player* srcPlr /*= NULL*/)
{
if (!source)
return 0;
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 0;
}
CreatureTextHolder TextHolder = (*sList).second;
CreatureTextHolder::const_iterator itr = TextHolder.find(textGroup);
if (itr == TextHolder.end())
CreatureTextHolder const& 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 0;
}
CreatureTextGroup TextGroup = (*itr).second;//has all texts in the group
CreatureTextGroup const& textGroupContainer = 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));
}
for (CreatureTextGroup::const_iterator giter = textGroupContainer.begin(); giter != textGroupContainer.end(); ++giter)
if (std::find(repeatGroup.begin(), repeatGroup.end(), giter->id) == repeatGroup.end())
tempGroup.push_back(*giter);
if (tempGroup.empty())
{
CreatureTextRepeatMap::iterator mapItr = mTextRepeatMap.find(source->GetGUID());
if (mapItr != mTextRepeatMap.end())
{
CreatureTextRepeatGroup::iterator groupItr = (*mapItr).second.find(textGroup);
(*groupItr).second.clear();
CreatureTextRepeatGroup::iterator groupItr = mapItr->second.find(textGroup);
groupItr->second.clear();
}
tempGroup = TextGroup;
tempGroup = textGroupContainer;
}
uint8 count = 0;
float lastChance = -1;
bool isEqualChanced = true;
@@ -148,164 +218,60 @@ uint32 CreatureTextMgr::SendChat(Creature* source, uint8 textGroup, uint64 whisp
for (CreatureTextGroup::const_iterator iter = tempGroup.begin(); iter != tempGroup.end(); ++iter)
{
if (lastChance >= 0 && lastChance != (*iter).probability)
if (lastChance >= 0 && lastChance != iter->probability)
isEqualChanced = false;
lastChance = (*iter).probability;
totalChance += (*iter).probability;
count++;
lastChance = iter->probability;
totalChance += iter->probability;
++count;
}
int32 offset = -1;
if (!isEqualChanced)
{
for (CreatureTextGroup::const_iterator iter = tempGroup.begin(); iter != tempGroup.end(); ++iter)
{
uint32 chance = uint32((*iter).probability);
uint32 chance = uint32(iter->probability);
uint32 r = urand(0, 100);
offset++;
++offset;
if (r <= chance)
break;
}
}
uint32 pos = 0;
if (isEqualChanced || offset < 0)
pos = urand(0, count - 1);
else if (offset >= 0)
pos = offset;
CreatureTextGroup::const_iterator iter = tempGroup.begin() + pos;
ChatMsg finalType = (msgType == CHAT_MSG_ADDON) ? (*iter).type : msgType;
Language finalLang = (language == LANG_ADDON) ? (*iter).lang : language;
uint32 finalSound = sound ? sound : (*iter).sound;
ChatMsg finalType = (msgType == CHAT_MSG_ADDON) ? iter->type : msgType;
Language finalLang = (language == LANG_ADDON) ? iter->lang : language;
uint32 finalSound = sound ? sound : iter->sound;
if (finalSound)
SendSound(source, finalSound, finalType, whisperGuid, range, team, gmOnly);
if ((*iter).emote)
SendEmote(srcPlr ? srcPlr->ToUnit() : source, (*iter).emote);
Unit* finalSource = source;
if (srcPlr)
finalSource = srcPlr;
SendChatString(srcPlr ? srcPlr->ToUnit() : source, (*iter).text.c_str(), finalType, finalLang, whisperGuid, range, team, gmOnly);
if (iter->emote)
SendEmote(finalSource, iter->emote);
CreatureTextBuilder builder(finalSource, finalType, iter->group, iter->id, finalLang, whisperGuid);
SendChatPacket(finalSource, builder, finalType, whisperGuid, range, team, gmOnly);
if (isEqualChanced || (!isEqualChanced && totalChance == 100.0f))
SetRepeatId(source, textGroup, (*iter).id);
SetRepeatId(source, textGroup, iter->id);
return (*iter).duration;
return iter->duration;
}
void CreatureTextMgr::SendSound(Creature* source, uint32 sound, ChatMsg msgType, uint64 whisperGuid, TextRange range, Team team, bool gmOnly)
float CreatureTextMgr::GetRangeForChatType(ChatMsg msgType) const
{
if (!sound || !source)
return;
WorldPacket data(SMSG_PLAY_SOUND, 4);
data << uint32(sound);
SendChatPacket(&data, source, msgType, whisperGuid, range, team, gmOnly);
}
void CreatureTextMgr::SendEmote(Unit* source, uint32 emote)
{
if (!source) return;
source->HandleEmoteCommand(emote);
}
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, ChatMsg msgtype /*= CHAT_MSG_MONSTER_SAY*/, Language language /*= LANG_UNIVERSAL*/, uint64 whisperGuid /*= 0*/, TextRange range /*= TEXT_RANGE_NORMAL*/, Team team /*= TEAM_OTHER*/, bool gmOnly /*= false*/) 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, team, gmOnly);//send our packet
}
void CreatureTextMgr::BuildMonsterChat(WorldPacket* data, WorldObject* source, ChatMsg msgType, char const* text, Language language, uint64 whisperGuid) const
{
if (!source)
return;
switch (msgType)
{
case CHAT_MSG_MONSTER_WHISPER:
if (!whisperGuid)
{
sLog->outError("CreatureTextMgr: WorldObject(%s) TypeId %u GuidLow %u sent CHAT_TYPE_WHISPER with targetGuid 0. Ignoring.", source->GetName(), uint32(source->GetTypeId()), source->GetGUIDLow());
return;
}
break;
case CHAT_MSG_RAID_BOSS_WHISPER:
if (!whisperGuid)
{
sLog->outError("CreatureTextMgr: WorldObject(%s) TypeId %u GuidLow %u sent CHAT_TYPE_BOSS_WHISPER with targetGuid 0. Ignoring.", source->GetName(), uint32(source->GetTypeId()), source->GetGUIDLow());
return;
}
break;
default:
break;
}
*data << uint8(msgType);
*data << uint32(language);
*data << uint64(source->GetGUID());
*data << uint32(0); // 2.1.0
*data << uint32(strlen(source->GetName()) + 1);
*data << source->GetName();
*data << uint64(whisperGuid); // Unit Target
if (whisperGuid && !IS_PLAYER_GUID(whisperGuid)) //can only whisper players
{
sLog->outError("CreatureTextMgr: WorldObject(%s) TypeId %u GuidLow %u sent WHISPER msg to Non-Player target. Ignoring.", source->GetName(), uint32(source->GetTypeId()), source->GetGUIDLow());
return;
// *data << (uint32)1; // target name length
// *data << (uint8)0; // target name
}
*data << uint32(strlen(text) + 1);
*data << text;
*data << uint8(0); // ChatTag
}
void CreatureTextMgr::SendChatPacket(WorldPacket* data, WorldObject* source, ChatMsg msgType, uint64 whisperGuid, TextRange range, Team team, bool gmOnly) const
{
if (!source)
return;
float dist = sWorld->getFloatConfig(CONFIG_LISTEN_RANGE_SAY);
switch (msgType)
{
case CHAT_MSG_MONSTER_YELL:
@@ -315,6 +281,29 @@ void CreatureTextMgr::SendChatPacket(WorldPacket* data, WorldObject* source, Cha
case CHAT_MSG_RAID_BOSS_EMOTE:
dist = sWorld->getFloatConfig(CONFIG_LISTEN_RANGE_TEXTEMOTE);
break;
default:
break;
}
return dist;
}
void CreatureTextMgr::SendSound(Creature* source, uint32 sound, ChatMsg msgType, uint64 whisperGuid, TextRange range, Team team, bool gmOnly)
{
if (!sound || !source)
return;
WorldPacket data(SMSG_PLAY_SOUND, 4);
data << uint32(sound);
SendNonChatPacket(source, &data, msgType, whisperGuid, range, team, gmOnly);
}
void CreatureTextMgr::SendNonChatPacket(WorldObject* source, WorldPacket* data, ChatMsg msgType, uint64 whisperGuid, TextRange range, Team team, bool gmOnly) const
{
float dist = GetRangeForChatType(msgType);
switch (msgType)
{
case CHAT_MSG_MONSTER_WHISPER:
case CHAT_MSG_RAID_BOSS_WHISPER:
{
@@ -337,83 +326,135 @@ void CreatureTextMgr::SendChatPacket(WorldPacket* data, WorldObject* source, Cha
case TEXT_RANGE_AREA:
{
uint32 areaId = source->GetAreaId();
Map::PlayerList const& pList = source->GetMap()->GetPlayers();
for (Map::PlayerList::const_iterator itr = pList.begin(); itr != pList.end(); ++itr)
{
Map::PlayerList const& players = source->GetMap()->GetPlayers();
for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
if (itr->getSource()->GetAreaId() == areaId && (!team || Team(itr->getSource()->GetTeam()) == team) && (!gmOnly || itr->getSource()->isGameMaster()))
{
if (data->GetOpcode() == SMSG_MESSAGECHAT)//override whisperguid with actual player's guid
data->put<uint64>(1+4+8+4+4+(int32)(strlen(source->GetName())+1), uint64(itr->getSource()->GetGUID()));
(itr->getSource())->GetSession()->SendPacket(data);
}
}
itr->getSource()->GetSession()->SendPacket(data);
return;
}
case TEXT_RANGE_ZONE:
{
uint32 zoneId = source->GetZoneId();
Map::PlayerList const& pList = source->GetMap()->GetPlayers();
for (Map::PlayerList::const_iterator itr = pList.begin(); itr != pList.end(); ++itr)
{
Map::PlayerList const& players = source->GetMap()->GetPlayers();
for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
if (itr->getSource()->GetZoneId() == zoneId && (!team || Team(itr->getSource()->GetTeam()) == team) && (!gmOnly || itr->getSource()->isGameMaster()))
{
if (data->GetOpcode() == SMSG_MESSAGECHAT)//override whisperguid with actual player's guid
data->put<uint64>(1+4+8+4+4+(int32)(strlen(source->GetName())+1), uint64(itr->getSource()->GetGUID()));
(itr->getSource())->GetSession()->SendPacket(data);
}
}
itr->getSource()->GetSession()->SendPacket(data);
return;
}
case TEXT_RANGE_MAP:
{
Map::PlayerList const& pList = source->GetMap()->GetPlayers();
for (Map::PlayerList::const_iterator itr = pList.begin(); itr != pList.end(); ++itr)
{
if (data->GetOpcode() == SMSG_MESSAGECHAT)//override whisperguid with actual player's guid
data->put<uint64>(1+4+8+4+4+(int32)(strlen(source->GetName())+1), uint64(itr->getSource()->GetGUID()));
Map::PlayerList const& players = source->GetMap()->GetPlayers();
for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
if ((!team || Team(itr->getSource()->GetTeam()) == team) && (!gmOnly || itr->getSource()->isGameMaster()))
(itr->getSource())->GetSession()->SendPacket(data);
}
itr->getSource()->GetSession()->SendPacket(data);
return;
}
case TEXT_RANGE_WORLD:
{
const SessionMap smap = sWorld->GetAllSessions();
SessionMap const& smap = sWorld->GetAllSessions();
for (SessionMap::const_iterator iter = smap.begin(); iter != smap.end(); ++iter)
{
if (Player* player = (*iter).second->GetPlayer())
{
if (data->GetOpcode() == SMSG_MESSAGECHAT)//override whisperguid with actual player's guid
data->put<uint64>(1+4+8+4+4+(int32)(strlen(source->GetName())+1), uint64(player->GetGUID()));
if (Player* player = iter->second->GetPlayer())
if (player->GetSession() && (!team || Team(player->GetTeam()) == team) && (!gmOnly || player->isGameMaster()))
player->GetSession()->SendPacket(data);
}
}
return;
}
case TEXT_RANGE_NORMAL:
default:
break;
}
source->SendMessageToSetInRange(data, dist, true);
}
void CreatureTextMgr::SendEmote(Unit* source, uint32 emote)
{
if (!source)
return;
source->HandleEmoteCommand(emote);
}
void CreatureTextMgr::SetRepeatId(Creature* source, uint8 textGroup, uint8 id)
{
if (!source)
return;
CreatureTextRepeatIds& repeats = mTextRepeatMap[source->GetGUID()][textGroup];
if (std::find(repeats.begin(), repeats.end(), id) == repeats.end())
repeats.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;
}
bool CreatureTextMgr::TextExist(uint32 sourceEntry, uint8 textGroup)
{
if (!sourceEntry)
return false;
CreatureTextMap::const_iterator sList = mTextMap.find(sourceEntry);
if (sList == mTextMap.end())
{
sLog->outDebug(LOG_FILTER_UNITS, "CreatureTextMgr::TextExist: Could not find Text for Creature (entry %u) in 'creature_text' table.", sourceEntry);
return false;
}
CreatureTextHolder TextHolder = (*sList).second;
CreatureTextHolder::const_iterator itr = TextHolder.find(textGroup);
if (itr == TextHolder.end())
CreatureTextHolder const& textHolder = sList->second;
CreatureTextHolder::const_iterator itr = textHolder.find(textGroup);
if (itr == textHolder.end())
{
sLog->outDebug(LOG_FILTER_UNITS, "CreatureTextMgr::TextExist: Could not find TextGroup %u for Creature (entry %u).", uint32(textGroup), sourceEntry);
return false;
}
return true;
}
std::string CreatureTextMgr::GetLocalizedChatString(uint32 entry, uint8 textGroup, uint32 id, LocaleConstant locale) const
{
CreatureTextMap::const_iterator mapitr = mTextMap.find(entry);
if (mapitr == mTextMap.end())
return "";
CreatureTextHolder::const_iterator holderItr = mapitr->second.find(textGroup);
if (holderItr == mapitr->second.end())
return "";
CreatureTextGroup::const_iterator groupItr = holderItr->second.begin();
for (; groupItr != holderItr->second.end(); ++groupItr)
if (groupItr->id == id)
break;
if (groupItr == holderItr->second.end())
return "";
std::string baseText = groupItr->text;
if (locale == DEFAULT_LOCALE)
return baseText;
if (locale > MAX_LOCALES)
return baseText;
LocaleCreatureTextMap::const_iterator locItr = mLocaleTextMap.find(CreatureTextId(entry, uint32(textGroup), id));
if (locItr == mLocaleTextMap.end())
return baseText;
if (locItr->second.Text[locale].length())
return locItr->second.Text[locale];
return baseText;
}

View File

@@ -44,10 +44,33 @@ enum TextRange
TEXT_RANGE_WORLD = 4
};
struct CreatureTextLocale
{
StringVector Text;
};
struct CreatureTextId
{
CreatureTextId(uint32 e, uint32 g, uint32 i) : entry(e), textGroup(g), textId(i)
{
}
bool operator<(CreatureTextId const& right) const
{
return memcmp(this, &right, sizeof(CreatureTextId)) < 0;
}
uint32 entry;
uint32 textGroup;
uint32 textId;
};
typedef std::vector<CreatureTextEntry> CreatureTextGroup; //texts in a group
typedef UNORDERED_MAP<uint8, CreatureTextGroup> CreatureTextHolder; //groups for a creature by groupid
typedef UNORDERED_MAP<uint32, CreatureTextHolder> CreatureTextMap; //all creatures by entry
typedef std::map<CreatureTextId, CreatureTextLocale> LocaleCreatureTextMap;
//used for handling non-repeatable random texts
typedef std::vector<uint8> CreatureTextRepeatIds;
typedef UNORDERED_MAP<uint8, CreatureTextRepeatIds> CreatureTextRepeatGroup;
@@ -57,9 +80,11 @@ class CreatureTextMgr
{
friend class ACE_Singleton<CreatureTextMgr, ACE_Null_Mutex>;
CreatureTextMgr() {};
public:
~CreatureTextMgr() {};
void LoadCreatureTexts();
void LoadCreatureTextLocales();
CreatureTextMap const& GetTextMap() const { return mTextMap; }
void SendSound(Creature* source, uint32 sound, ChatMsg msgType, uint64 whisperGuid, TextRange range, Team team, bool gmOnly);
@@ -67,17 +92,154 @@ class CreatureTextMgr
//if sent, returns the 'duration' of the text else 0 if error
uint32 SendChat(Creature* source, uint8 textGroup, uint64 whisperGuid = 0, ChatMsg msgType = CHAT_MSG_ADDON, Language language = LANG_ADDON, TextRange range = TEXT_RANGE_NORMAL, uint32 sound = 0, Team team = TEAM_OTHER, bool gmOnly = false, Player* srcPlr = NULL);
void SendChatString(WorldObject* source, char const* text, ChatMsg msgtype = CHAT_MSG_MONSTER_SAY, Language language = LANG_UNIVERSAL, uint64 whisperGuid = 0, TextRange range = TEXT_RANGE_NORMAL, Team team = TEAM_OTHER, bool gmOnly = false) const;
bool TextExist(uint32 sourceEntry, uint8 textGroup);
std::string GetLocalizedChatString(uint32 entry, uint8 textGroup, uint32 id, LocaleConstant locale) const;
template<class Builder>
void SendChatPacket(WorldObject* source, Builder const& builder, ChatMsg msgType, uint64 whisperGuid = 0, TextRange range = TEXT_RANGE_NORMAL, Team team = TEAM_OTHER, bool gmOnly = false) const;
private:
CreatureTextRepeatIds GetRepeatGroup(Creature* source, uint8 textGroup);
void SetRepeatId(Creature* source, uint8 textGroup, uint8 id);
void BuildMonsterChat(WorldPacket* data, WorldObject* source, ChatMsg msgType, char const* text, Language language, uint64 whisperGuid) const;
void SendChatPacket(WorldPacket* data, WorldObject* source, ChatMsg msgType, uint64 whisperGuid, TextRange range, Team team, bool gmOnly) const;
void SendNonChatPacket(WorldObject* source, WorldPacket* data, ChatMsg msgType, uint64 whisperGuid, TextRange range, Team team, bool gmOnly) const;
float GetRangeForChatType(ChatMsg msgType) const;
CreatureTextMap mTextMap;
CreatureTextRepeatMap mTextRepeatMap;
LocaleCreatureTextMap mLocaleTextMap;
};
#define sCreatureTextMgr ACE_Singleton<CreatureTextMgr, ACE_Null_Mutex>::instance()
template<class Builder>
class CreatureTextLocalizer
{
public:
CreatureTextLocalizer(Builder const& builder, ChatMsg msgType) : _builder(builder), _msgType(msgType)
{
_packetCache.resize(TOTAL_LOCALES, NULL);
}
~CreatureTextLocalizer()
{
for (size_t i = 0; i < _packetCache.size(); ++i)
{
if (_packetCache[i])
delete _packetCache[i]->first;
delete _packetCache[i];
}
}
void operator()(Player* player)
{
LocaleConstant loc_idx = player->GetSession()->GetSessionDbLocaleIndex();
WorldPacket* messageTemplate;
size_t whisperGUIDpos;
// create if not cached yet
if (!_packetCache[loc_idx])
{
messageTemplate = new WorldPacket(SMSG_MESSAGECHAT, 200);
whisperGUIDpos = _builder(messageTemplate, loc_idx);
_packetCache[loc_idx] = new std::pair<WorldPacket*, size_t>(messageTemplate, whisperGUIDpos);
}
else
{
messageTemplate = _packetCache[loc_idx]->first;
whisperGUIDpos = _packetCache[loc_idx]->second;
}
WorldPacket data(*messageTemplate);
switch (_msgType)
{
case CHAT_MSG_MONSTER_WHISPER:
case CHAT_MSG_RAID_BOSS_WHISPER:
data.put<uint64>(whisperGUIDpos, player->GetGUID());
break;
}
player->SendDirectMessage(&data);
}
private:
std::vector<std::pair<WorldPacket*, size_t>* > _packetCache;
Builder const& _builder;
ChatMsg _msgType;
};
template<class Builder>
void CreatureTextMgr::SendChatPacket(WorldObject* source, Builder const& builder, ChatMsg msgType, uint64 whisperGuid, TextRange range, Team team, bool gmOnly) const
{
if (!source)
return;
CreatureTextLocalizer<Builder> localizer(builder, msgType);
switch (msgType)
{
case CHAT_MSG_MONSTER_WHISPER:
case CHAT_MSG_RAID_BOSS_WHISPER:
{
if (range == TEXT_RANGE_NORMAL) //ignores team and gmOnly
{
Player* player = ObjectAccessor::FindPlayer(whisperGuid);
if (!player || !player->GetSession())
return;
localizer(player);
return;
}
break;
}
default:
break;
}
switch (range)
{
case TEXT_RANGE_AREA:
{
uint32 areaId = source->GetAreaId();
Map::PlayerList const& players = source->GetMap()->GetPlayers();
for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
if (itr->getSource()->GetAreaId() == areaId && (!team || Team(itr->getSource()->GetTeam()) == team) && (!gmOnly || itr->getSource()->isGameMaster()))
localizer(itr->getSource());
return;
}
case TEXT_RANGE_ZONE:
{
uint32 zoneId = source->GetZoneId();
Map::PlayerList const& players = source->GetMap()->GetPlayers();
for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
if (itr->getSource()->GetZoneId() == zoneId && (!team || Team(itr->getSource()->GetTeam()) == team) && (!gmOnly || itr->getSource()->isGameMaster()))
localizer(itr->getSource());
return;
}
case TEXT_RANGE_MAP:
{
Map::PlayerList const& players = source->GetMap()->GetPlayers();
for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
if ((!team || Team(itr->getSource()->GetTeam()) == team) && (!gmOnly || itr->getSource()->isGameMaster()))
localizer(itr->getSource());
return;
}
case TEXT_RANGE_WORLD:
{
SessionMap const& smap = sWorld->GetAllSessions();
for (SessionMap::const_iterator iter = smap.begin(); iter != smap.end(); ++iter)
if (Player* player = iter->second->GetPlayer())
if (player->GetSession() && (!team || Team(player->GetTeam()) == team) && (!gmOnly || player->isGameMaster()))
localizer(player);
return;
}
case TEXT_RANGE_NORMAL:
default:
break;
}
float dist = GetRangeForChatType(msgType);
Trinity::PlayerDistWorker<CreatureTextLocalizer<Builder> > worker(source, dist, localizer);
source->VisitNearbyWorldObject(dist, worker);
}
#endif

View File

@@ -1651,6 +1651,9 @@ void World::SetInitialWorldSettings()
sLog->outString("Loading Creature Texts...");
sCreatureTextMgr->LoadCreatureTexts();
sLog->outString("Loading Creature Text Locales...");
sCreatureTextMgr->LoadCreatureTextLocales();
sLog->outString("Initializing Scripts...");
sScriptMgr->Initialize();

View File

@@ -194,7 +194,7 @@ public:
void KilledUnit(Unit* /*victim*/)
{
Talk(SAY_SLAY), me);
Talk(SAY_SLAY);
}
void JustDied(Unit* /*killer*/)