[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / QueryHandler.cpp
blob45195ec565a711c5d634bb6cb3b6e2bb14c35d39
1 /*
2 * Copyright (C) 2005-2009 MaNGOS <http://getmangos.com/>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 #include "Common.h"
20 #include "Language.h"
21 #include "Database/DatabaseEnv.h"
22 #include "Database/DatabaseImpl.h"
23 #include "WorldPacket.h"
24 #include "WorldSession.h"
25 #include "Opcodes.h"
26 #include "Log.h"
27 #include "World.h"
28 #include "ObjectMgr.h"
29 #include "Player.h"
30 #include "UpdateMask.h"
31 #include "NPCHandler.h"
32 #include "ObjectAccessor.h"
33 #include "Pet.h"
34 #include "MapManager.h"
36 void WorldSession::SendNameQueryOpcode(Player *p)
38 if(!p)
39 return;
41 // guess size
42 WorldPacket data( SMSG_NAME_QUERY_RESPONSE, (8+1+4+4+4+10) );
43 data << p->GetGUID();
44 data << p->GetName();
45 data << uint8(0); // realm name for cross realm BG usage
46 data << uint32(p->getRace());
47 data << uint32(p->getGender());
48 data << uint32(p->getClass());
49 if(DeclinedName const* names = p->GetDeclinedNames())
51 data << uint8(1); // is declined
52 for(int i = 0; i < MAX_DECLINED_NAME_CASES; ++i)
53 data << names->name[i];
55 else
56 data << uint8(0); // is not declined
58 SendPacket(&data);
61 void WorldSession::SendNameQueryOpcodeFromDB(uint64 guid)
63 CharacterDatabase.AsyncPQuery(&WorldSession::SendNameQueryOpcodeFromDBCallBack, GetAccountId(),
64 !sWorld.getConfig(CONFIG_DECLINED_NAMES_USED) ?
65 // ------- Query Without Declined Names --------
66 // 0 1 2
67 "SELECT guid, name, SUBSTRING(data, LENGTH(SUBSTRING_INDEX(data, ' ', '%u'))+2, LENGTH(SUBSTRING_INDEX(data, ' ', '%u')) - LENGTH(SUBSTRING_INDEX(data, ' ', '%u'))-1) "
68 "FROM characters WHERE guid = '%u'"
70 // --------- Query With Declined Names ---------
71 // 0 1 2
72 "SELECT characters.guid, name, SUBSTRING(data, LENGTH(SUBSTRING_INDEX(data, ' ', '%u'))+2, LENGTH(SUBSTRING_INDEX(data, ' ', '%u')) - LENGTH(SUBSTRING_INDEX(data, ' ', '%u'))-1), "
73 // 3 4 5 6 7
74 "genitive, dative, accusative, instrumental, prepositional "
75 "FROM characters LEFT JOIN character_declinedname ON characters.guid = character_declinedname.guid WHERE characters.guid = '%u'",
76 UNIT_FIELD_BYTES_0, UNIT_FIELD_BYTES_0+1, UNIT_FIELD_BYTES_0, GUID_LOPART(guid));
79 void WorldSession::SendNameQueryOpcodeFromDBCallBack(QueryResult *result, uint32 accountId)
81 if(!result)
82 return;
84 WorldSession * session = sWorld.FindSession(accountId);
85 if(!session)
87 delete result;
88 return;
91 Field *fields = result->Fetch();
92 uint32 guid = fields[0].GetUInt32();
93 std::string name = fields[1].GetCppString();
94 uint32 field = 0;
95 if(name == "")
96 name = session->GetMangosString(LANG_NON_EXIST_CHARACTER);
97 else
98 field = fields[2].GetUInt32();
100 // guess size
101 WorldPacket data( SMSG_NAME_QUERY_RESPONSE, (8+1+4+4+4+10) );
102 data << MAKE_NEW_GUID(guid, 0, HIGHGUID_PLAYER);
103 data << name;
104 data << (uint8)0;
105 data << (uint32)(field & 0xFF);
106 data << (uint32)((field >> 16) & 0xFF);
107 data << (uint32)((field >> 8) & 0xFF);
109 // if the first declined name field (3) is empty, the rest must be too
110 if(sWorld.getConfig(CONFIG_DECLINED_NAMES_USED) && fields[3].GetCppString() != "")
112 data << (uint8)1; // is declined
113 for(int i = 3; i < MAX_DECLINED_NAME_CASES+3; ++i)
114 data << fields[i].GetCppString();
116 else
117 data << (uint8)0; // is declined
119 session->SendPacket( &data );
120 delete result;
123 void WorldSession::HandleNameQueryOpcode( WorldPacket & recv_data )
125 CHECK_PACKET_SIZE(recv_data,8);
127 uint64 guid;
129 recv_data >> guid;
131 Player *pChar = objmgr.GetPlayer(guid);
133 if (pChar)
134 SendNameQueryOpcode(pChar);
135 else
136 SendNameQueryOpcodeFromDB(guid);
139 void WorldSession::HandleQueryTimeOpcode( WorldPacket & /*recv_data*/ )
141 WorldPacket data( SMSG_QUERY_TIME_RESPONSE, 4+4 );
142 data << (uint32)time(NULL);
143 data << (uint32)0;
144 SendPacket( &data );
147 /// Only _static_ data send in this packet !!!
148 void WorldSession::HandleCreatureQueryOpcode( WorldPacket & recv_data )
150 CHECK_PACKET_SIZE(recv_data,4+8);
152 uint32 entry;
153 recv_data >> entry;
155 CreatureInfo const *ci = objmgr.GetCreatureTemplate(entry);
156 if (ci)
159 std::string Name, SubName;
160 Name = ci->Name;
161 SubName = ci->SubName;
163 int loc_idx = GetSessionDbLocaleIndex();
164 if (loc_idx >= 0)
166 CreatureLocale const *cl = objmgr.GetCreatureLocale(entry);
167 if (cl)
169 if (cl->Name.size() > size_t(loc_idx) && !cl->Name[loc_idx].empty())
170 Name = cl->Name[loc_idx];
171 if (cl->SubName.size() > size_t(loc_idx) && !cl->SubName[loc_idx].empty())
172 SubName = cl->SubName[loc_idx];
175 sLog.outDetail("WORLD: CMSG_CREATURE_QUERY '%s' - Entry: %u.", ci->Name, entry);
176 // guess size
177 WorldPacket data( SMSG_CREATURE_QUERY_RESPONSE, 100 );
178 data << (uint32)entry; // creature entry
179 data << Name;
180 data << uint8(0) << uint8(0) << uint8(0); // name2, name3, name4, always empty
181 data << SubName;
182 data << ci->IconName; // "Directions" for guard, string for Icons 2.3.0
183 data << (uint32)ci->type_flags; // flags wdbFeild7=wad flags1
184 data << (uint32)ci->type;
185 data << (uint32)ci->family; // family wdbFeild9
186 data << (uint32)ci->rank; // rank wdbFeild10
187 data << (uint32)ci->PetSpellDataId; // Id from CreatureSpellData.dbc wdbField12
188 data << (uint32)ci->DisplayID_A; // modelid_male1
189 data << (uint32)ci->DisplayID_H; // modelid_female1 ?
190 data << (uint32)ci->DisplayID_A2; // modelid_male2 ?
191 data << (uint32)ci->DisplayID_H2; // modelid_femmale2 ?
192 data << (float)1.0f; // unk
193 data << (float)1.0f; // unk
194 data << (uint8)ci->RacialLeader;
195 SendPacket( &data );
196 sLog.outDebug( "WORLD: Sent SMSG_CREATURE_QUERY_RESPONSE " );
198 else
200 uint64 guid;
201 recv_data >> guid;
203 sLog.outDebug("WORLD: CMSG_CREATURE_QUERY - NO CREATURE INFO! (GUID: %u, ENTRY: %u)",
204 GUID_LOPART(guid), entry);
205 WorldPacket data( SMSG_CREATURE_QUERY_RESPONSE, 4 );
206 data << uint32(entry | 0x80000000);
207 SendPacket( &data );
208 sLog.outDebug( "WORLD: Sent SMSG_CREATURE_QUERY_RESPONSE " );
212 /// Only _static_ data send in this packet !!!
213 void WorldSession::HandleGameObjectQueryOpcode( WorldPacket & recv_data )
215 CHECK_PACKET_SIZE(recv_data,4+8);
217 uint32 entryID;
218 recv_data >> entryID;
220 const GameObjectInfo *info = objmgr.GetGameObjectInfo(entryID);
221 if(info)
224 std::string Name;
225 std::string CastBarCaption;
227 Name = info->name;
228 CastBarCaption = info->castBarCaption;
230 int loc_idx = GetSessionDbLocaleIndex();
231 if (loc_idx >= 0)
233 GameObjectLocale const *gl = objmgr.GetGameObjectLocale(entryID);
234 if (gl)
236 if (gl->Name.size() > size_t(loc_idx) && !gl->Name[loc_idx].empty())
237 Name = gl->Name[loc_idx];
238 if (gl->CastBarCaption.size() > size_t(loc_idx) && !gl->CastBarCaption[loc_idx].empty())
239 CastBarCaption = gl->CastBarCaption[loc_idx];
242 sLog.outDetail("WORLD: CMSG_GAMEOBJECT_QUERY '%s' - Entry: %u. ", info->name, entryID);
243 WorldPacket data ( SMSG_GAMEOBJECT_QUERY_RESPONSE, 150 );
244 data << entryID;
245 data << (uint32)info->type;
246 data << (uint32)info->displayId;
247 data << Name;
248 data << uint8(0) << uint8(0) << uint8(0); // name2, name3, name4
249 data << uint8(0); // 2.0.3, string
250 data << CastBarCaption; // 2.0.3, string. Text will appear in Cast Bar when using GO (ex: "Collecting")
251 data << uint8(0); // 2.0.3, probably string
252 data.append(info->raw.data,24);
253 data << float(info->size); // go size
254 SendPacket( &data );
255 sLog.outDebug( "WORLD: Sent CMSG_GAMEOBJECT_QUERY " );
257 else
260 uint64 guid;
261 recv_data >> guid;
263 sLog.outDebug( "WORLD: CMSG_GAMEOBJECT_QUERY - Missing gameobject info for (GUID: %u, ENTRY: %u)",
264 GUID_LOPART(guid), entryID );
265 WorldPacket data ( SMSG_GAMEOBJECT_QUERY_RESPONSE, 4 );
266 data << uint32(entryID | 0x80000000);
267 SendPacket( &data );
268 sLog.outDebug( "WORLD: Sent CMSG_GAMEOBJECT_QUERY " );
272 void WorldSession::HandleCorpseQueryOpcode(WorldPacket & /*recv_data*/)
274 sLog.outDetail("WORLD: Received MSG_CORPSE_QUERY");
276 Corpse *corpse = GetPlayer()->GetCorpse();
278 if(!corpse)
280 WorldPacket data(MSG_CORPSE_QUERY, 1);
281 data << uint8(0); // corpse not found
282 SendPacket(&data);
283 return;
286 int32 mapid = corpse->GetMapId();
287 float x = corpse->GetPositionX();
288 float y = corpse->GetPositionY();
289 float z = corpse->GetPositionZ();
290 int32 corpsemapid = _player->GetMapId();
292 if(Map *map = MapManager::Instance().FindMap(corpse->GetMapId(), corpse->GetInstanceId()))
294 if(map->IsDungeon())
296 if(!map->GetEntrancePos(mapid, x, y))
297 return;
299 Map *entrance_map = MapManager::Instance().GetMap(mapid, _player);
300 if(!entrance_map)
301 return;
303 z = entrance_map->GetHeight(x, y, MAX_HEIGHT);
304 corpsemapid = corpse->GetMapId();
308 WorldPacket data(MSG_CORPSE_QUERY, 1+(5*4));
309 data << uint8(1); // corpse found
310 data << int32(mapid);
311 data << float(x);
312 data << float(y);
313 data << float(z);
314 data << int32(corpsemapid);
315 SendPacket(&data);
318 void WorldSession::HandleNpcTextQueryOpcode( WorldPacket & recv_data )
320 CHECK_PACKET_SIZE(recv_data,4+8);
322 uint32 textID;
323 uint64 guid;
325 recv_data >> textID;
326 sLog.outDetail("WORLD: CMSG_NPC_TEXT_QUERY ID '%u'", textID);
328 recv_data >> guid;
329 GetPlayer()->SetUInt64Value(UNIT_FIELD_TARGET, guid);
331 GossipText const* pGossip = objmgr.GetGossipText(textID);
333 WorldPacket data( SMSG_NPC_TEXT_UPDATE, 100 ); // guess size
334 data << textID;
336 if (!pGossip)
338 for(uint32 i = 0; i < 8; ++i)
340 data << float(0);
341 data << "Greetings $N";
342 data << "Greetings $N";
343 data << uint32(0);
344 data << uint32(0);
345 data << uint32(0);
346 data << uint32(0);
347 data << uint32(0);
348 data << uint32(0);
349 data << uint32(0);
352 else
354 std::string Text_0[8], Text_1[8];
355 for (int i=0;i<8;i++)
357 Text_0[i]=pGossip->Options[i].Text_0;
358 Text_1[i]=pGossip->Options[i].Text_1;
361 int loc_idx = GetSessionDbLocaleIndex();
362 if (loc_idx >= 0)
364 NpcTextLocale const *nl = objmgr.GetNpcTextLocale(textID);
365 if (nl)
367 for (int i=0;i<8;i++)
369 if (nl->Text_0[i].size() > size_t(loc_idx) && !nl->Text_0[i][loc_idx].empty())
370 Text_0[i]=nl->Text_0[i][loc_idx];
371 if (nl->Text_1[i].size() > size_t(loc_idx) && !nl->Text_1[i][loc_idx].empty())
372 Text_1[i]=nl->Text_1[i][loc_idx];
377 for (int i=0; i<8; i++)
379 data << pGossip->Options[i].Probability;
381 if ( Text_0[i].empty() )
382 data << Text_1[i];
383 else
384 data << Text_0[i];
386 if ( Text_1[i].empty() )
387 data << Text_0[i];
388 else
389 data << Text_1[i];
391 data << pGossip->Options[i].Language;
393 for(int j = 0; j < 3; ++j)
395 data << pGossip->Options[i].Emotes[j]._Delay;
396 data << pGossip->Options[i].Emotes[j]._Emote;
401 SendPacket( &data );
403 sLog.outDebug( "WORLD: Sent SMSG_NPC_TEXT_UPDATE " );
406 void WorldSession::HandlePageQueryOpcode( WorldPacket & recv_data )
408 CHECK_PACKET_SIZE(recv_data,4);
410 uint32 pageID;
412 recv_data >> pageID;
413 sLog.outDetail("WORLD: Received CMSG_PAGE_TEXT_QUERY for pageID '%u'", pageID);
415 while (pageID)
417 PageText const *pPage = sPageTextStore.LookupEntry<PageText>( pageID );
418 // guess size
419 WorldPacket data( SMSG_PAGE_TEXT_QUERY_RESPONSE, 50 );
420 data << pageID;
422 if (!pPage)
424 data << "Item page missing.";
425 data << uint32(0);
426 pageID = 0;
428 else
430 std::string Text = pPage->Text;
432 int loc_idx = GetSessionDbLocaleIndex();
433 if (loc_idx >= 0)
435 PageTextLocale const *pl = objmgr.GetPageTextLocale(pageID);
436 if (pl)
438 if (pl->Text.size() > size_t(loc_idx) && !pl->Text[loc_idx].empty())
439 Text = pl->Text[loc_idx];
443 data << Text;
444 data << uint32(pPage->Next_Page);
445 pageID = pPage->Next_Page;
447 SendPacket( &data );
449 sLog.outDebug( "WORLD: Sent SMSG_PAGE_TEXT_QUERY_RESPONSE " );