TrinityCore
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 "ChatTextBuilder.h"
24#include "GridNotifiers.h"
25#include "Group.h"
26#include "World.h"
27#include "WorldSession.h"
28
29template<class Builder>
31{
32public:
33 CreatureTextLocalizer(Builder const& builder, ChatMsg msgType) : _cache(), _builder(builder), _msgType(msgType)
34 {
35 }
36
37 void operator()(Player const* player) const
38 {
41
42 // create if not cached yet
43 if (!_cache[loc_idx])
44 {
45 sender = _builder(loc_idx);
46 _cache[loc_idx].reset(sender);
47 }
48 else
49 sender = _cache[loc_idx].get();
50
51 switch (_msgType)
52 {
55 {
57 message.SetReceiver(player, loc_idx);
58 player->SendDirectMessage(message.Write());
59 return;
60 }
61 default:
62 break;
63 }
64
65 (*sender)(player);
66 }
67
68private:
69 mutable std::array<std::unique_ptr<Trinity::ChatPacketSender>, TOTAL_LOCALES> _cache;
70 Builder const& _builder;
72};
73
74template<class Builder>
75void 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*/)
76{
77 if (!source)
78 return;
79
80 CreatureTextLocalizer<Builder> localizer(builder, msgType);
81
82 switch (msgType)
83 {
85 {
86 if (!whisperTarget)
87 return;
88
89 if (Player const* whisperPlayer = whisperTarget->ToPlayer())
90 if (Group const* group = whisperPlayer->GetGroup())
91 group->BroadcastWorker(localizer);
92 return;
93 }
96 {
97 if (range == TEXT_RANGE_NORMAL) // ignores team and gmOnly
98 {
99 if (!whisperTarget || whisperTarget->GetTypeId() != TYPEID_PLAYER)
100 return;
101
102 localizer(const_cast<Player*>(whisperTarget->ToPlayer()));
103 return;
104 }
105 break;
106 }
107 default:
108 break;
109 }
110
111 switch (range)
112 {
113 case TEXT_RANGE_AREA:
114 {
115 uint32 areaId = source->GetAreaId();
116 Map::PlayerList const& players = source->GetMap()->GetPlayers();
117 for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
118 if (itr->GetSource()->GetAreaId() == areaId && (!team || Team(itr->GetSource()->GetEffectiveTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
119 localizer(itr->GetSource());
120 return;
121 }
122 case TEXT_RANGE_ZONE:
123 {
124 uint32 zoneId = source->GetZoneId();
125 Map::PlayerList const& players = source->GetMap()->GetPlayers();
126 for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
127 if (itr->GetSource()->GetZoneId() == zoneId && (!team || Team(itr->GetSource()->GetEffectiveTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
128 localizer(itr->GetSource());
129 return;
130 }
131 case TEXT_RANGE_MAP:
132 {
133 Map::PlayerList const& players = source->GetMap()->GetPlayers();
134 for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
135 if ((!team || Team(itr->GetSource()->GetEffectiveTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
136 localizer(itr->GetSource());
137 return;
138 }
139 case TEXT_RANGE_WORLD:
140 {
141 SessionMap const& smap = sWorld->GetAllSessions();
142 for (SessionMap::const_iterator iter = smap.begin(); iter != smap.end(); ++iter)
143 if (Player* player = iter->second->GetPlayer())
144 if ((!team || Team(player->GetTeam()) == team) && (!gmOnly || player->IsGameMaster()))
145 localizer(player);
146 return;
147 }
149 if (!whisperTarget || !whisperTarget->IsPlayer())
150 return;
151
152 localizer(whisperTarget->ToPlayer());
153 return;
155 default:
156 break;
157 }
158
159 float dist = GetRangeForChatType(msgType);
160 Trinity::PlayerDistWorker<CreatureTextLocalizer<Builder>> worker(source, dist, localizer);
161 Cell::VisitWorldObjects(source, worker, dist);
162}
163
164#endif // CreatureTextMgrImpl_h__
LocaleConstant
Definition: Common.h:48
@ TOTAL_LOCALES
Definition: Common.h:62
CreatureTextRange
@ TEXT_RANGE_ZONE
@ TEXT_RANGE_AREA
@ TEXT_RANGE_WORLD
@ TEXT_RANGE_PERSONAL
@ TEXT_RANGE_NORMAL
@ TEXT_RANGE_MAP
uint32_t uint32
Definition: Define.h:142
@ TYPEID_PLAYER
Definition: ObjectGuid.h:41
Team
ChatMsg
@ CHAT_MSG_MONSTER_WHISPER
@ CHAT_MSG_RAID_BOSS_WHISPER
@ CHAT_MSG_MONSTER_PARTY
std::array< std::unique_ptr< Trinity::ChatPacketSender >, TOTAL_LOCALES > _cache
CreatureTextLocalizer(Builder const &builder, ChatMsg msgType)
void operator()(Player const *player) const
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)
static float GetRangeForChatType(ChatMsg msgType)
Definition: Group.h:197
iterator end()
Definition: MapRefManager.h:35
iterator begin()
Definition: MapRefManager.h:34
PlayerList const & GetPlayers() const
Definition: Map.h:367
bool IsPlayer() const
Definition: Object.h:212
TypeID GetTypeId() const
Definition: Object.h:173
static Player * ToPlayer(Object *o)
Definition: Object.h:213
void SendDirectMessage(WorldPacket const *data) const
Definition: Player.cpp:6324
WorldSession * GetSession() const
Definition: Player.h:2101
WorldPackets::Chat::Chat UntranslatedPacket
Map * GetMap() const
Definition: Object.h:624
uint32 GetAreaId() const
Definition: Object.h:546
uint32 GetZoneId() const
Definition: Object.h:545
WorldPacket const * Write() override
void SetReceiver(WorldObject const *receiver, LocaleConstant locale)
LocaleConstant GetSessionDbLocaleIndex() const
#define sWorld
Definition: World.h:931
std::unordered_map< uint32, WorldSession * > SessionMap
Definition: World.h:559
static void VisitWorldObjects(WorldObject const *obj, T &visitor, float radius, bool dont_load=true)
Definition: CellImpl.h:191