Project SkyFire Core
SkyFire 5.4.8 server core API documentation
Loading...
Searching...
No Matches
CreatureTextMgr.h
Go to the documentation of this file.
1/*
2* This file is part of Project SkyFire https://www.projectskyfire.org.
3* See LICENSE.md file for Copyright information
4*/
5
6#ifndef SKYFIRE_CREATURE_TEXT_MGR_H
7#define SKYFIRE_CREATURE_TEXT_MGR_H
8
9#include "Creature.h"
10#include "GridNotifiers.h"
11#include "ObjectAccessor.h"
12#include "Opcodes.h"
13#include "Platform/Singleton.h"
14#include "SharedDefines.h"
15
29
38
43
45{
47 {
48 }
49
50 bool operator<(CreatureTextId const& right) const
51 {
52 return memcmp(this, &right, sizeof(CreatureTextId)) < 0;
53 }
54
58};
59
60typedef std::vector<CreatureTextEntry> CreatureTextGroup; //texts in a group
61typedef UNORDERED_MAP<uint8, CreatureTextGroup> CreatureTextHolder; //groups for a creature by groupid
63
64typedef std::map<CreatureTextId, CreatureTextLocale> LocaleCreatureTextMap;
65
66//used for handling non-repeatable random texts
67typedef std::vector<uint8> CreatureTextRepeatIds;
70
72{
73 friend class Skyfire::Singleton<CreatureTextMgr, Skyfire::NullMutex>;
75
76public:
78 void LoadCreatureTexts();
80 CreatureTextMap const& GetTextMap() const { return mTextMap; }
81
82 void SendSound(Creature* source, uint32 sound, ChatMsg msgType, WorldObject const* whisperTarget, CreatureTextRange range, Team team, bool gmOnly);
83 void SendEmote(Unit* source, uint32 emote);
84
85 //if sent, returns the 'duration' of the text else 0 if error
86 uint32 SendChat(Creature* source, uint8 textGroup, WorldObject const* whisperTarget = NULL, ChatMsg msgType = ChatMsg::CHAT_MSG_ADDON, Language language = Language::LANG_ADDON, CreatureTextRange range = TEXT_RANGE_NORMAL, uint32 sound = 0, Team team = TEAM_OTHER, bool gmOnly = false, Player* srcPlr = NULL);
87 bool TextExist(uint32 sourceEntry, uint8 textGroup);
88 std::string GetLocalizedChatString(uint32 entry, uint8 textGroup, uint32 id, LocaleConstant locale) const;
89
90 template<class Builder>
91 void SendChatPacket(WorldObject* source, Builder const& builder, ChatMsg msgType, WorldObject const* whisperTarget = NULL, CreatureTextRange range = TEXT_RANGE_NORMAL, Team team = TEAM_OTHER, bool gmOnly = false) const;
92private:
94 void SetRepeatId(Creature* source, uint8 textGroup, uint8 id);
95
96 void SendNonChatPacket(WorldObject* source, WorldPacket* data, ChatMsg msgType, WorldObject const* whisperTarget, CreatureTextRange range, Team team, bool gmOnly) const;
97 float GetRangeForChatType(ChatMsg msgType) const;
98
102};
103
104#define sCreatureTextMgr Skyfire::Singleton<CreatureTextMgr, Skyfire::NullMutex>::instance()
105
106template<class Builder>
108{
109public:
110 CreatureTextLocalizer(Builder const& builder, ChatMsg msgType) : _builder(builder), _msgType(msgType)
111 {
112 _packetCache.resize(TOTAL_LOCALES, NULL);
113 }
114
116 {
117 for (size_t i = 0; i < _packetCache.size(); ++i)
118 {
119 if (_packetCache[i])
120 delete _packetCache[i]->first;
121 delete _packetCache[i];
122 }
123 }
124
125 void operator()(Player* player)
126 {
128 WorldPacket* messageTemplate;
129 size_t whisperGUIDpos;
130
131 // create if not cached yet
132 if (!_packetCache[loc_idx])
133 {
134 messageTemplate = new WorldPacket();
135 whisperGUIDpos = _builder(messageTemplate, loc_idx);
136 //ASSERT(messageTemplate->GetOpcode() != MSG_NULL_ACTION); // ???
137 _packetCache[loc_idx] = new std::pair<WorldPacket*, size_t>(messageTemplate, whisperGUIDpos);
138 }
139 else
140 {
141 messageTemplate = _packetCache[loc_idx]->first;
142 whisperGUIDpos = _packetCache[loc_idx]->second;
143 }
144
145 WorldPacket data(*messageTemplate);
146 switch (_msgType)
147 {
150 data.put<uint64>(whisperGUIDpos, player->GetGUID());
151 break;
152 default:
153 break;
154 }
155
156 player->SendDirectMessage(&data);
157 }
158
159private:
160 std::vector<std::pair<WorldPacket*, size_t>* > _packetCache;
161 Builder const& _builder;
163};
164
165template<class Builder>
166void CreatureTextMgr::SendChatPacket(WorldObject* source, Builder const& builder, ChatMsg msgType, WorldObject const* whisperTarget /*= NULL*/, CreatureTextRange range, Team team, bool gmOnly) const
167{
168 if (!source)
169 return;
170
171 CreatureTextLocalizer<Builder> localizer(builder, msgType);
172
173 switch (msgType)
174 {
177 {
178 if (range == TEXT_RANGE_NORMAL) //ignores team and gmOnly
179 {
180 if (!whisperTarget || whisperTarget->GetTypeId() != TypeID::TYPEID_PLAYER)
181 return;
182
183 localizer(const_cast<Player*>(whisperTarget->ToPlayer()));
184 return;
185 }
186 break;
187 }
188 default:
189 break;
190 }
191
192 switch (range)
193 {
194 case TEXT_RANGE_AREA:
195 {
196 uint32 areaId = source->GetAreaId();
197 Map::PlayerList const& players = source->GetMap()->GetPlayers();
198 for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
199 if (itr->GetSource()->GetAreaId() == areaId && (!team || Team(itr->GetSource()->GetTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
200 localizer(itr->GetSource());
201 return;
202 }
203 case TEXT_RANGE_ZONE:
204 {
205 uint32 zoneId = source->GetZoneId();
206 Map::PlayerList const& players = source->GetMap()->GetPlayers();
207 for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
208 if (itr->GetSource()->GetZoneId() == zoneId && (!team || Team(itr->GetSource()->GetTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
209 localizer(itr->GetSource());
210 return;
211 }
212 case TEXT_RANGE_MAP:
213 {
214 Map::PlayerList const& players = source->GetMap()->GetPlayers();
215 for (Map::PlayerList::const_iterator itr = players.begin(); itr != players.end(); ++itr)
216 if ((!team || Team(itr->GetSource()->GetTeam()) == team) && (!gmOnly || itr->GetSource()->IsGameMaster()))
217 localizer(itr->GetSource());
218 return;
219 }
220 case TEXT_RANGE_WORLD:
221 {
222 SessionMap const& smap = sWorld->GetAllSessions();
223 for (SessionMap::const_iterator iter = smap.begin(); iter != smap.end(); ++iter)
224 if (Player* player = iter->second->GetPlayer())
225 if (player->GetSession() && (!team || Team(player->GetTeam()) == team) && (!gmOnly || player->IsGameMaster()))
226 localizer(player);
227 return;
228 }
230 default:
231 break;
232 }
233
234 float dist = GetRangeForChatType(msgType);
235 Skyfire::PlayerDistWorker<CreatureTextLocalizer<Builder> > worker(source, dist, localizer);
236 source->VisitNearbyWorldObject(dist, worker);
237}
238
239#endif
const uint8 TOTAL_LOCALES
Definition Common.h:153
LocaleConstant
Definition Common.h:138
std::vector< std::string > StringVector
Definition Common.h:163
UNORDERED_MAP< uint32, CreatureTextHolder > CreatureTextMap
UNORDERED_MAP< uint8, CreatureTextRepeatIds > CreatureTextRepeatGroup
std::vector< uint8 > CreatureTextRepeatIds
UNORDERED_MAP< uint64, CreatureTextRepeatGroup > CreatureTextRepeatMap
UNORDERED_MAP< uint8, CreatureTextGroup > CreatureTextHolder
std::vector< CreatureTextEntry > CreatureTextGroup
std::map< CreatureTextId, CreatureTextLocale > LocaleCreatureTextMap
CreatureTextRange
@ TEXT_RANGE_ZONE
@ TEXT_RANGE_AREA
@ TEXT_RANGE_WORLD
@ TEXT_RANGE_NORMAL
@ TEXT_RANGE_MAP
std::uint8_t uint8
Definition Define.h:79
std::uint32_t uint32
Definition Define.h:77
std::uint64_t uint64
Definition Define.h:76
@ TYPEID_PLAYER
Definition Object.h:55
ChatMsg
@ CHAT_MSG_RAID_BOSS_WHISPER
@ CHAT_MSG_MONSTER_WHISPER
Language
@ TEAM_OTHER
#define UNORDERED_MAP
void put(size_t pos, T value)
Definition ByteBuffer.h:233
Builder const & _builder
CreatureTextLocalizer(Builder const &builder, ChatMsg msgType)
void operator()(Player *player)
std::vector< std::pair< WorldPacket *, size_t > * > _packetCache
LocaleCreatureTextMap mLocaleTextMap
std::string GetLocalizedChatString(uint32 entry, uint8 textGroup, uint32 id, LocaleConstant locale) const
uint32 SendChat(Creature *source, uint8 textGroup, WorldObject const *whisperTarget=NULL, ChatMsg msgType=ChatMsg::CHAT_MSG_ADDON, Language language=Language::LANG_ADDON, CreatureTextRange range=TEXT_RANGE_NORMAL, uint32 sound=0, Team team=TEAM_OTHER, bool gmOnly=false, Player *srcPlr=NULL)
void SetRepeatId(Creature *source, uint8 textGroup, uint8 id)
CreatureTextMap const & GetTextMap() const
CreatureTextRepeatMap mTextRepeatMap
void SendEmote(Unit *source, uint32 emote)
bool TextExist(uint32 sourceEntry, uint8 textGroup)
CreatureTextRepeatIds GetRepeatGroup(Creature *source, uint8 textGroup)
void SendNonChatPacket(WorldObject *source, WorldPacket *data, ChatMsg msgType, WorldObject const *whisperTarget, CreatureTextRange range, Team team, bool gmOnly) const
void SendSound(Creature *source, uint32 sound, ChatMsg msgType, WorldObject const *whisperTarget, CreatureTextRange range, Team team, bool gmOnly)
CreatureTextMap mTextMap
float GetRangeForChatType(ChatMsg msgType) const
void SendChatPacket(WorldObject *source, Builder const &builder, ChatMsg msgType, WorldObject const *whisperTarget=NULL, CreatureTextRange range=TEXT_RANGE_NORMAL, Team team=TEAM_OTHER, bool gmOnly=false) const
MapRefManager PlayerList
Definition Map.h:406
PlayerList const & GetPlayers() const
Definition Map.h:407
iterator end()
iterator begin()
LinkedListHead::Iterator< MapReference const > const_iterator
uint64 GetGUID() const
Definition Object.h:119
Player * ToPlayer()
Definition Object.h:204
TypeID GetTypeId() const
Definition Object.h:131
void SendDirectMessage(WorldPacket *data)
Definition Player.cpp:6904
WorldSession * GetSession() const
Definition Player.h:2417
Definition Unit.h:1367
static Player * GetPlayer(WorldObject &object, uint64 guid)
Definition Unit.cpp:4900
Map * GetMap() const
Definition Object.h:740
void VisitNearbyWorldObject(float const &radius, NOTIFIER &notifier) const
Definition Object.h:784
uint32 GetAreaId() const
Definition Object.cpp:1602
uint32 GetZoneId() const
Definition Object.cpp:1597
LocaleConstant GetSessionDbLocaleIndex() const
#define sWorld
Definition World.h:910
UNORDERED_MAP< uint32, WorldSession * > SessionMap
Definition World.h:543
uint32 duration
uint32 entry
ChatMsg type
std::string text
Language lang
float probability
Emote emote
uint8 id
uint8 group
uint32 sound
bool operator<(CreatureTextId const &right) const
CreatureTextId(uint32 e, uint32 g, uint32 i)