OregonCore  revision 3611e8a-git
Your Favourite TBC server
CreatureTextMgr.h
Go to the documentation of this file.
1 /*
2 * This file is part of the OregonCore 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 #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 "WorldPacket.h"
28 
30 {
36 };
37 
39 {
43  std::string text;
46  float probability;
51 };
52 
54 {
56 };
57 
59 {
60  CreatureTextId(uint32 e, uint32 g, uint32 i) : entry(e), textGroup(g), textId(i) { }
61 
62  bool operator<(CreatureTextId const& right) const
63  {
64  return memcmp(this, &right, sizeof(CreatureTextId)) < 0;
65  }
66 
70 };
71 
72 typedef std::vector<CreatureTextEntry> CreatureTextGroup; // texts in a group
73 typedef UNORDERED_MAP<uint8, CreatureTextGroup> CreatureTextHolder; // groups for a creature by groupid
74 typedef UNORDERED_MAP<uint32, CreatureTextHolder> CreatureTextMap; // all creatures by entry
75 
76 typedef std::map<CreatureTextId, CreatureTextLocale> LocaleCreatureTextMap;
77 
79 {
80  private:
83 
84  public:
86  {
87  static CreatureTextMgr instance;
88  return &instance;
89  }
90 
91  void LoadCreatureTexts();
92  void LoadCreatureTextLocales();
93  CreatureTextMap const& GetTextMap() const { return mTextMap; }
94 
95  void SendSound(Creature* source, uint32 sound, ChatMsg msgType, WorldObject* whisperTarget, CreatureTextRange range, Team team, bool gmOnly);
96  void SendEmote(Unit* source, uint32 emote);
97 
98  static size_t BuildMonsterChat(WorldPacket* data, WorldObject* source, ChatMsg msgtype, std::string text, Language language, WorldObject* whisperTarget, int loc_idx);
99 
100  //if sent, returns the 'duration' of the text else 0 if error
101  uint32 SendChat(Creature* source, uint8 textGroup, WorldObject* whisperTarget = nullptr, ChatMsg msgType = CHAT_MSG_ADDON, Language language = LANG_ADDON, CreatureTextRange range = TEXT_RANGE_NORMAL, uint32 sound = 0, Team team = TEAM_NONE, bool gmOnly = false, Player* srcPlr = nullptr);
102  std::string GetLocalizedChatString(uint32 entry, uint8 gender, uint8 textGroup, uint32 id, int loc_idx) const;
103  bool TextExist(uint32 sourceEntry, uint8 textGroup);
104 
105  template<class Builder> void SendChatPacket(WorldObject* source, Builder const& builder, ChatMsg msgType, WorldObject* whisperTarget = nullptr, CreatureTextRange range = TEXT_RANGE_NORMAL, Team team = TEAM_NONE, bool gmOnly = false) const;
106 
107  private:
108  CreatureTextRepeatIds GetRepeatGroup(Creature* source, uint8 textGroup);
109  void SetRepeatId(Creature* source, uint8 textGroup, uint8 id);
110 
111  float GetRangeForChatType(ChatMsg msgType) const;
112  void SendNonChatPacket(WorldPacket* data, WorldObject* source, ChatMsg msgtype, WorldObject* whisperTarget, CreatureTextRange range, Team team, bool gmOnly) const;
113 
116 };
117 
118 #define sCreatureTextMgr CreatureTextMgr::instance()
119 
120 template<class Builder>
122 {
123 public:
124  CreatureTextLocalizer(Builder const& builder, ChatMsg msgType) : _builder(builder), _msgType(msgType)
125  {
126  }
127 
129  {
130  }
131 
132  void operator()(Player* player)
133  {
134  int loc_idx = player->GetSession()->GetSessionDbLocaleIndex();
135  WorldPacket* messageTemplate;
136  size_t whisperGUIDpos;
137 
138  messageTemplate = new WorldPacket();
139  whisperGUIDpos = _builder(messageTemplate, loc_idx);
140  ASSERT(messageTemplate->GetOpcode() != MSG_NULL_ACTION);
141 
142  WorldPacket data(*messageTemplate);
143  switch (_msgType)
144  {
147  data.put<uint64>(whisperGUIDpos, player->GetGUID());
148  break;
149  default:
150  break;
151  }
152 
153  player->SendDirectMessage(&data);
154  }
155 
156 private:
157  Builder const& _builder;
159 };
160 
161 template<class Builder>
162 void CreatureTextMgr::SendChatPacket(WorldObject* source, Builder const& builder, ChatMsg msgType, WorldObject* whisperTarget /*= nullptr*/, CreatureTextRange range /*= TEXT_RANGE_NORMAL*/, Team team /*= TEAM_OTHER*/, bool gmOnly /*= false*/) const
163 {
164  if (!source)
165  return;
166 
167  CreatureTextLocalizer<Builder> localizer(builder, msgType);
168 
169  switch (msgType)
170  {
172  {
173  if (!whisperTarget)
174  return;
175 
176  if (Player* whisperPlayer = whisperTarget->ToPlayer())
177  {
178  if (Group* group = whisperPlayer->GetGroup())
179  group->BroadcastWorker(localizer);
180  }
181  return;
182  }
185  {
186  if (range == TEXT_RANGE_NORMAL) // ignores team and gmOnly
187  {
188  if (!whisperTarget || whisperTarget->GetTypeId() != TYPEID_PLAYER)
189  return;
190 
191  localizer(const_cast<Player*>(whisperTarget->ToPlayer()));
192  return;
193  }
194  break;
195  }
196  default:
197  break;
198  }
199 
200  switch (range)
201  {
202  case TEXT_RANGE_AREA:
203  {
204  uint32 areaId = source->GetAreaId();
205  Map::PlayerList const& players = source->GetMap()->GetPlayers();
206  for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
207  if (itr->GetSource()->GetAreaId() == areaId && (!team || Team(itr->GetSource()->GetTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
208  localizer(itr->GetSource());
209  return;
210  }
211  case TEXT_RANGE_ZONE:
212  {
213  uint32 zoneId = source->GetZoneId();
214  Map::PlayerList const& players = source->GetMap()->GetPlayers();
215  for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
216  if (itr->GetSource()->GetZoneId() == zoneId && (!team || Team(itr->GetSource()->GetTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
217  localizer(itr->GetSource());
218  return;
219  }
220  case TEXT_RANGE_MAP:
221  {
222  Map::PlayerList const& players = source->GetMap()->GetPlayers();
223  for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
224  if ((!team || Team(itr->GetSource()->GetTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
225  localizer(itr->GetSource());
226  return;
227  }
228  case TEXT_RANGE_WORLD:
229  {
230  SessionMap const& smap = sWorld.GetAllSessions();
231  for (SessionMap::const_iterator iter = smap.begin(); iter != smap.end(); ++iter)
232  if (Player* player = iter->second->GetPlayer())
233  if ((!team || Team(player->GetTeam()) == team) && (!gmOnly || player->IsGameMaster()))
234  localizer(player);
235  return;
236  }
237  case TEXT_RANGE_NORMAL:
238  default:
239  break;
240  }
241 
242  float dist = GetRangeForChatType(msgType);
244  source->VisitNearbyWorldObject(dist, worker);
245 }
246 
247 #endif
uint16 GetOpcode() const
Definition: WorldPacket.h:44
UNORDERED_MAP< uint32, CreatureTextHolder > CreatureTextMap
UNORDERED_MAP< uint32, WorldSession * > SessionMap
Definition: World.h:454
Map * GetMap() const
Definition: Object.h:829
Emote
std::vector< CreatureTextEntry > CreatureTextGroup
std::map< CreatureTextId, CreatureTextLocale > LocaleCreatureTextMap
CreatureTextRange TextRange
uint32 GetZoneId() const
Definition: Object.cpp:1176
std::vector< uint8 > CreatureTextRepeatIds
Definition: Creature.h:456
Builder const & _builder
ChatMsg
iterator begin()
Definition: MapRefManager.h:48
UNORDERED_MAP< uint8, CreatureTextGroup > CreatureTextHolder
Player * ToPlayer()
Definition: Object.h:386
uint8 GetTypeId() const
Definition: Object.h:204
Team
ACE_UINT8 uint8
Definition: Define.h:73
Language
std::vector< std::string > StringVector
Definition: Common.h:232
void SendDirectMessage(WorldPacket *data)
Definition: Player.cpp:5782
bool operator<(CreatureTextId const &right) const
CreatureTextMap const & GetTextMap() const
CreatureTextLocalizer(Builder const &builder, ChatMsg msgType)
CreatureTextId(uint32 e, uint32 g, uint32 i)
uint32 GetAreaId() const
Definition: Object.cpp:1181
CreatureTextRange
void SendChatPacket(WorldObject *source, Builder const &builder, ChatMsg msgType, WorldObject *whisperTarget=nullptr, CreatureTextRange range=TEXT_RANGE_NORMAL, Team team=TEAM_NONE, bool gmOnly=false) const
ACE_UINT64 uint64
Definition: Define.h:70
PlayerList const & GetPlayers() const
Definition: Map.h:491
int GetSessionDbLocaleIndex() const
Definition: WorldSession.h:237
LocaleCreatureTextMap mLocaleTextMap
CreatureTextMap mTextMap
#define ASSERT
Definition: Errors.h:33
WorldSession * GetSession() const
Definition: Player.h:1959
#define sWorld
Definition: World.h:860
static CreatureTextMgr * instance()
ACE_UINT32 uint32
Definition: Define.h:71
Definition: Unit.h:884
static Player * GetPlayer(WorldObject &object, uint64 guid)
Definition: Unit.cpp:10597
Definition: Player.h:922
void operator()(Player *player)
void VisitNearbyWorldObject(const float &radius, NOTIFIER &notifier) const
Definition: Object.h:908
iterator end()
Definition: MapRefManager.h:52
Definition: Group.h:154
const uint64 & GetGUID() const
Definition: Object.h:156