TrinityCore
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Modules Pages
CreatureTextMgr.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008-2016 TrinityCore <http://www.trinitycore.org/>
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 #ifndef TRINITY_CREATURE_TEXT_MGR_H
19 #define TRINITY_CREATURE_TEXT_MGR_H
20 
21 #include "Creature.h"
22 #include "GridNotifiers.h"
23 #include "ObjectAccessor.h"
24 #include "SharedDefines.h"
25 #include "Opcodes.h"
26 #include "Group.h"
27 #include "Packets/ChatPackets.h"
28 
30 {
36 };
37 
39 {
43  std::string text;
46  float probability;
52 };
53 
55 {
57 };
58 
60 {
62 
63  bool operator<(CreatureTextId const& right) const
64  {
65  return memcmp(this, &right, sizeof(CreatureTextId)) < 0;
66  }
67 
71 };
72 
73 typedef std::vector<CreatureTextEntry> CreatureTextGroup; // texts in a group
74 typedef std::unordered_map<uint8, CreatureTextGroup> CreatureTextHolder; // groups for a creature by groupid
75 typedef std::unordered_map<uint32, CreatureTextHolder> CreatureTextMap; // all creatures by entry
76 
77 typedef std::map<CreatureTextId, CreatureTextLocale> LocaleCreatureTextMap;
78 
80 {
81  private:
84 
85  public:
86  static CreatureTextMgr* instance();
87 
88  void LoadCreatureTexts();
89  void LoadCreatureTextLocales();
90  CreatureTextMap const& GetTextMap() const { return mTextMap; }
91 
92  static void SendSound(Creature* source, uint32 sound, ChatMsg msgType, WorldObject const* whisperTarget = nullptr, CreatureTextRange range = TEXT_RANGE_NORMAL, Team team = TEAM_OTHER, bool gmOnly = false);
93  static void SendEmote(Unit* source, uint32 emote);
94 
95  //if sent, returns the 'duration' of the text else 0 if error
96  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, Team team = TEAM_OTHER, bool gmOnly = false, Player* srcPlr = nullptr);
97  bool TextExist(uint32 sourceEntry, uint8 textGroup) const;
98  std::string GetLocalizedChatString(uint32 entry, uint8 gender, uint8 textGroup, uint32 id, LocaleConstant locale) const;
99 
100  template<class Builder> 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);
101 
102  private:
103  CreatureTextRepeatIds GetRepeatGroup(Creature* source, uint8 textGroup) const;
104  void SetRepeatId(Creature* source, uint8 textGroup, uint8 id);
105 
106  static void SendNonChatPacket(WorldObject* source, WorldPacket const* data, ChatMsg msgType, WorldObject const* whisperTarget, CreatureTextRange range, Team team, bool gmOnly);
107  static float GetRangeForChatType(ChatMsg msgType);
108 
111 };
112 
113 #define sCreatureTextMgr CreatureTextMgr::instance()
114 
115 template<class Builder>
117 {
118  public:
119  CreatureTextLocalizer(Builder const& builder, ChatMsg msgType) : _builder(builder), _msgType(msgType)
120  {
122  }
123 
125  {
126  for (size_t i = 0; i < _packetCache.size(); ++i)
127  delete _packetCache[i];
128  }
129 
130  void operator()(Player* player)
131  {
132  LocaleConstant loc_idx = player->GetSession()->GetSessionDbLocaleIndex();
133  WorldPackets::Chat::Chat* messageTemplate;
134 
135  // create if not cached yet
136  if (!_packetCache[loc_idx])
137  {
138  messageTemplate = _builder(loc_idx);
139  _packetCache[loc_idx] = messageTemplate;
140  }
141  else
142  messageTemplate = _packetCache[loc_idx];
143 
144  WorldPackets::Chat::Chat message(*messageTemplate);
145 
146  switch (_msgType)
147  {
150  message.SetReceiver(player, loc_idx);
151  break;
152  default:
153  break;
154  }
155 
156  player->SendDirectMessage(message.Write());
157  }
158 
159  private:
160  std::vector<WorldPackets::Chat::Chat*> _packetCache;
161  Builder const& _builder;
163 };
164 
165 template<class Builder>
166 void CreatureTextMgr::SendChatPacket(WorldObject* source, Builder const& builder, ChatMsg msgType, WorldObject const* whisperTarget /*= nullptr*/, CreatureTextRange range /*= TEXT_RANGE_NORMAL*/, Team team /*= TEAM_OTHER*/, bool gmOnly /*= false*/)
167 {
168  if (!source)
169  return;
170 
171  CreatureTextLocalizer<Builder> localizer(builder, msgType);
172 
173  switch (msgType)
174  {
176  {
177  if (!whisperTarget)
178  return;
179 
180  if (Player* whisperPlayer = const_cast<Player*>(whisperTarget->ToPlayer()))
181  {
182  if (Group* group = whisperPlayer->GetGroup())
183  group->BroadcastWorker(localizer);
184  }
185  return;
186  }
189  {
190  if (range == TEXT_RANGE_NORMAL) // ignores team and gmOnly
191  {
192  if (!whisperTarget || whisperTarget->GetTypeId() != TYPEID_PLAYER)
193  return;
194 
195  localizer(const_cast<Player*>(whisperTarget->ToPlayer()));
196  return;
197  }
198  break;
199  }
200  default:
201  break;
202  }
203 
204  switch (range)
205  {
206  case TEXT_RANGE_AREA:
207  {
208  uint32 areaId = source->GetAreaId();
209  Map::PlayerList const& players = source->GetMap()->GetPlayers();
210  for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
211  if (itr->GetSource()->GetAreaId() == areaId && (!team || Team(itr->GetSource()->GetTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
212  localizer(itr->GetSource());
213  return;
214  }
215  case TEXT_RANGE_ZONE:
216  {
217  uint32 zoneId = source->GetZoneId();
218  Map::PlayerList const& players = source->GetMap()->GetPlayers();
219  for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
220  if (itr->GetSource()->GetZoneId() == zoneId && (!team || Team(itr->GetSource()->GetTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
221  localizer(itr->GetSource());
222  return;
223  }
224  case TEXT_RANGE_MAP:
225  {
226  Map::PlayerList const& players = source->GetMap()->GetPlayers();
227  for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
228  if ((!team || Team(itr->GetSource()->GetTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
229  localizer(itr->GetSource());
230  return;
231  }
232  case TEXT_RANGE_WORLD:
233  {
234  SessionMap const& smap = sWorld->GetAllSessions();
235  for (SessionMap::const_iterator iter = smap.begin(); iter != smap.end(); ++iter)
236  if (Player* player = iter->second->GetPlayer())
237  if ((!team || Team(player->GetTeam()) == team) && (!gmOnly || player->IsGameMaster()))
238  localizer(player);
239  return;
240  }
241  case TEXT_RANGE_NORMAL:
242  default:
243  break;
244  }
245 
246  float dist = GetRangeForChatType(msgType);
247  Trinity::PlayerDistWorker<CreatureTextLocalizer<Builder> > worker(source, dist, localizer);
248  source->VisitNearbyWorldObject(dist, worker);
249 }
250 
251 #endif
static float GetRangeForChatType(ChatMsg msgType)
Definition: CreatureTextMgr.cpp:277
Map * GetMap() const
Definition: Object.h:543
uint32 textGroup
Definition: CreatureTextMgr.h:69
Emote emote
Definition: CreatureTextMgr.h:47
Definition: CreatureTextMgr.h:31
uint8 id
Definition: CreatureTextMgr.h:42
Emote
Definition: SharedDefines.h:2410
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)
Definition: CreatureTextMgr.h:166
std::vector< CreatureTextEntry > CreatureTextGroup
Definition: CreatureTextMgr.h:73
uint8 group
Definition: CreatureTextMgr.h:41
void SetReceiver(WorldObject const *receiver, LocaleConstant locale)
Definition: ChatPackets.cpp:155
Definition: GridNotifiers.h:560
Definition: CreatureTextMgr.h:35
Definition: SharedDefines.h:4246
std::map< CreatureTextId, CreatureTextLocale > LocaleCreatureTextMap
Definition: CreatureTextMgr.h:77
CreatureTextRange TextRange
Definition: CreatureTextMgr.h:51
uint32 entry
Definition: CreatureTextMgr.h:68
void VisitNearbyWorldObject(float const &radius, NOTIFIER &notifier) const
Definition: Object.h:589
uint32 GetZoneId() const
Definition: Object.cpp:1525
std::vector< uint8 > CreatureTextRepeatIds
Definition: Creature.h:464
Builder const & _builder
Definition: CreatureTextMgr.h:161
Definition: ChatPackets.h:150
ChatMsg
Definition: SharedDefines.h:4228
arena_t NULL
Definition: jemalloc_internal.h:624
uint32 entry
Definition: CreatureTextMgr.h:40
Definition: Object.h:423
Definition: Creature.h:467
Definition: CreatureTextMgr.h:54
#define sWorld
Definition: World.h:887
iterator begin()
Definition: MapRefManager.h:37
uint32 duration
Definition: CreatureTextMgr.h:48
Definition: CreatureTextMgr.h:34
uint32 textId
Definition: CreatureTextMgr.h:70
Player * ToPlayer()
Definition: Object.h:191
WorldPacket const * Write() override
Definition: ChatPackets.cpp:162
ChatMsg type
Definition: CreatureTextMgr.h:44
uint32 BroadcastTextId
Definition: CreatureTextMgr.h:50
TypeID GetTypeId() const
Definition: Object.h:113
Definition: CreatureTextMgr.h:38
Team
Definition: SharedDefines.h:997
std::unordered_map< uint32, CreatureTextHolder > CreatureTextMap
Definition: CreatureTextMgr.h:75
Language
Definition: SharedDefines.h:959
CreatureTextMgr()
Definition: CreatureTextMgr.h:82
Definition: SharedDefines.h:4244
Definition: CreatureTextMgr.h:32
LocaleConstant
Definition: Common.h:115
Definition: CreatureTextMgr.h:33
Definition: SharedDefines.h:4273
uint32_t uint32
Definition: Define.h:150
bool operator<(CreatureTextId const &right) const
Definition: CreatureTextMgr.h:63
Definition: LinkedList.h:141
CreatureTextMap const & GetTextMap() const
Definition: CreatureTextMgr.h:90
CreatureTextLocalizer(Builder const &builder, ChatMsg msgType)
Definition: CreatureTextMgr.h:119
CreatureTextId(uint32 e, uint32 g, uint32 i)
Definition: CreatureTextMgr.h:61
uint32 GetAreaId() const
Definition: Object.cpp:1530
CreatureTextRange
Definition: CreatureTextMgr.h:29
std::string text
Definition: CreatureTextMgr.h:43
Definition: SharedDefines.h:1006
uint32 sound
Definition: CreatureTextMgr.h:49
PlayerList const & GetPlayers() const
Definition: Map.h:433
Definition: CreatureTextMgr.h:116
Language lang
Definition: CreatureTextMgr.h:45
std::vector< std::string > StringVector
Definition: Common.h:142
Definition: MapRefManager.h:26
Definition: ObjectGuid.h:33
Definition: SharedDefines.h:4230
float probability
Definition: CreatureTextMgr.h:46
#define TC_GAME_API
Definition: Define.h:134
LocaleCreatureTextMap mLocaleTextMap
Definition: CreatureTextMgr.h:110
CreatureTextMap mTextMap
Definition: CreatureTextMgr.h:109
ChatMsg _msgType
Definition: CreatureTextMgr.h:162
uint8_t uint8
Definition: Define.h:152
std::unordered_map< uint8, CreatureTextGroup > CreatureTextHolder
Definition: CreatureTextMgr.h:74
std::unordered_map< uint32, WorldSession * > SessionMap
Definition: World.h:530
Definition: CreatureTextMgr.h:59
std::vector< WorldPackets::Chat::Chat * > _packetCache
Definition: CreatureTextMgr.h:160
Definition: Unit.h:1305
StringVector Text
Definition: CreatureTextMgr.h:56
Definition: WorldPacket.h:26
~CreatureTextMgr()
Definition: CreatureTextMgr.h:83
Definition: CreatureTextMgr.h:79
void operator()(Player *player)
Definition: CreatureTextMgr.h:130
iterator end()
Definition: MapRefManager.h:38
Definition: Group.h:191
Definition: Common.h:130
Definition: SharedDefines.h:985
~CreatureTextLocalizer()
Definition: CreatureTextMgr.h:124