TrinityCore
Loading...
Searching...
No Matches
CreatureTextMgrImpl.h
Go to the documentation of this file.
1/*
2 * This file is part of the TrinityCore 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 CreatureTextMgrImpl_h__
19#define CreatureTextMgrImpl_h__
20
21#include "CreatureTextMgr.h"
22#include "CellImpl.h"
23#include "Group.h"
24#include "GridNotifiers.h"
25#include "World.h"
26#include "WorldSession.h"
27
28template<class Builder>
30{
31public:
32 CreatureTextLocalizer(Builder const& builder, ChatMsg msgType) : _builder(builder), _msgType(msgType)
33 {
34 _packetCache.resize(TOTAL_LOCALES, nullptr);
35 }
36
38 {
39 for (size_t i = 0; i < _packetCache.size(); ++i)
40 {
41 if (_packetCache[i])
42 delete _packetCache[i]->first;
43 delete _packetCache[i];
44 }
45 }
46
47 void operator()(Player const* player) const
48 {
50 WorldPacket* messageTemplate;
51 size_t whisperGUIDpos;
52
53 // create if not cached yet
54 if (!_packetCache[loc_idx])
55 {
56 messageTemplate = new WorldPacket();
57 whisperGUIDpos = _builder(messageTemplate, loc_idx);
58 _packetCache[loc_idx] = new std::pair<WorldPacket*, size_t>(messageTemplate, whisperGUIDpos);
59 }
60 else
61 {
62 messageTemplate = _packetCache[loc_idx]->first;
63 whisperGUIDpos = _packetCache[loc_idx]->second;
64 }
65
66 WorldPacket data(*messageTemplate);
67 switch (_msgType)
68 {
71 data.put<uint64>(whisperGUIDpos, player->GetGUID().GetRawValue());
72 break;
73 default:
74 break;
75 }
76
77 player->SendDirectMessage(&data);
78 }
79
80private:
81 mutable std::vector<std::pair<WorldPacket*, size_t>*> _packetCache;
82 Builder const& _builder;
84};
85
86template<class Builder>
87void 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*/) const
88{
89 if (!source)
90 return;
91
92 CreatureTextLocalizer<Builder> localizer(builder, msgType);
93
94 switch (msgType)
95 {
97 {
98 if (!whisperTarget)
99 return;
100
101 if (Player const* whisperPlayer = whisperTarget->ToPlayer())
102 if (Group const* group = whisperPlayer->GetGroup())
103 group->BroadcastWorker(localizer);
104 return;
105 }
108 {
109 if (range == TEXT_RANGE_NORMAL) // ignores team and gmOnly
110 {
111 if (!whisperTarget || whisperTarget->GetTypeId() != TYPEID_PLAYER)
112 return;
113
114 localizer(const_cast<Player*>(whisperTarget->ToPlayer()));
115 return;
116 }
117 break;
118 }
119 default:
120 break;
121 }
122
123 switch (range)
124 {
125 case TEXT_RANGE_AREA:
126 {
127 uint32 areaId = source->GetAreaId();
128 Map::PlayerList const& players = source->GetMap()->GetPlayers();
129 for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
130 if (itr->GetSource()->GetAreaId() == areaId && (!team || Team(itr->GetSource()->GetTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
131 localizer(itr->GetSource());
132 return;
133 }
134 case TEXT_RANGE_ZONE:
135 {
136 uint32 zoneId = source->GetZoneId();
137 Map::PlayerList const& players = source->GetMap()->GetPlayers();
138 for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
139 if (itr->GetSource()->GetZoneId() == zoneId && (!team || Team(itr->GetSource()->GetTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
140 localizer(itr->GetSource());
141 return;
142 }
143 case TEXT_RANGE_MAP:
144 {
145 Map::PlayerList const& players = source->GetMap()->GetPlayers();
146 for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
147 if ((!team || Team(itr->GetSource()->GetTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
148 localizer(itr->GetSource());
149 return;
150 }
151 case TEXT_RANGE_WORLD:
152 {
153 SessionMap const& smap = sWorld->GetAllSessions();
154 for (SessionMap::const_iterator iter = smap.begin(); iter != smap.end(); ++iter)
155 if (Player* player = iter->second->GetPlayer())
156 if ((!team || Team(player->GetTeam()) == team) && (!gmOnly || player->IsGameMaster()))
157 localizer(player);
158 return;
159 }
161 default:
162 break;
163 }
164
165 float dist = GetRangeForChatType(msgType);
166 Trinity::PlayerDistWorker<CreatureTextLocalizer<Builder>> worker(source, dist, localizer);
167 Cell::VisitWorldObjects(source, worker, dist);
168}
169
170#endif // CreatureTextMgrImpl_h__
LocaleConstant
Definition Common.h:48
@ TOTAL_LOCALES
Definition Common.h:59
CreatureTextRange
@ TEXT_RANGE_ZONE
@ TEXT_RANGE_AREA
@ TEXT_RANGE_WORLD
@ TEXT_RANGE_NORMAL
@ TEXT_RANGE_MAP
uint64_t uint64
Definition Define.h:132
uint32_t uint32
Definition Define.h:133
@ TYPEID_PLAYER
Definition ObjectGuid.h:39
ChatMsg
@ CHAT_MSG_MONSTER_WHISPER
@ CHAT_MSG_RAID_BOSS_WHISPER
@ CHAT_MSG_MONSTER_PARTY
void put(std::size_t pos, T value)
Definition ByteBuffer.h:137
std::vector< std::pair< WorldPacket *, size_t > * > _packetCache
CreatureTextLocalizer(Builder const &builder, ChatMsg msgType)
void operator()(Player const *player) const
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) const
float GetRangeForChatType(ChatMsg msgType) const
Definition Group.h:165
iterator end()
iterator begin()
PlayerList const & GetPlayers() const
Definition Map.h:448
uint64 GetRawValue() const
Definition ObjectGuid.h:148
TypeID GetTypeId() const
Definition Object.h:93
static ObjectGuid GetGUID(Object const *o)
Definition Object.h:78
static Player * ToPlayer(Object *o)
Definition Object.h:180
void SendDirectMessage(WorldPacket const *data) const
Definition Player.cpp:6161
WorldSession * GetSession() const
Definition Player.h:1719
Map * GetMap() const
Definition Object.h:449
uint32 GetAreaId() const
Definition Object.h:374
uint32 GetZoneId() const
Definition Object.h:373
LocaleConstant GetSessionDbLocaleIndex() const
#define sWorld
Definition World.h:900
std::unordered_map< uint32, WorldSession * > SessionMap
Definition World.h:553
static void VisitWorldObjects(WorldObject const *obj, T &visitor, float radius, bool dont_load=true)
Definition CellImpl.h:180