Just a few renames.
[getmangos.git] / src / game / QueryHandler.cpp
blob86b165236afe779392986bcbb6cbc9cbe393b133
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 "Pet.h"
33 #include "MapManager.h"
35 void WorldSession::SendNameQueryOpcode(Player *p)
37 if(!p)
38 return;
40 // guess size
41 WorldPacket data( SMSG_NAME_QUERY_RESPONSE, (8+1+1+1+1+1+10) );
42 data.append(p->GetPackGUID()); // player guid
43 data << uint8(0); // added in 3.1
44 data << p->GetName(); // played name
45 data << uint8(0); // realm name for cross realm BG usage
46 data << uint8(p->getRace());
47 data << uint8(p->getGender());
48 data << uint8(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+1+1+1+1+1+10) );
102 data.appendPackGUID(MAKE_NEW_GUID(guid, 0, HIGHGUID_PLAYER));
103 data << uint8(0); // added in 3.1
104 data << name;
105 data << uint8(0);
106 data << uint8(field & 0xFF);
107 data << uint8((field >> 16) & 0xFF);
108 data << uint8((field >> 8) & 0xFF);
110 // if the first declined name field (3) is empty, the rest must be too
111 if(sWorld.getConfig(CONFIG_DECLINED_NAMES_USED) && fields[3].GetCppString() != "")
113 data << uint8(1); // is declined
114 for(int i = 3; i < MAX_DECLINED_NAME_CASES+3; ++i)
115 data << fields[i].GetCppString();
117 else
118 data << uint8(0); // is declined
120 session->SendPacket( &data );
121 delete result;
124 void WorldSession::HandleNameQueryOpcode( WorldPacket & recv_data )
126 CHECK_PACKET_SIZE(recv_data, 8);
128 uint64 guid;
130 recv_data >> guid;
132 Player *pChar = objmgr.GetPlayer(guid);
134 if (pChar)
135 SendNameQueryOpcode(pChar);
136 else
137 SendNameQueryOpcodeFromDB(guid);
140 void WorldSession::HandleQueryTimeOpcode( WorldPacket & /*recv_data*/ )
142 WorldPacket data( SMSG_QUERY_TIME_RESPONSE, 4+4 );
143 data << (uint32)time(NULL);
144 data << (uint32)0;
145 SendPacket( &data );
148 /// Only _static_ data send in this packet !!!
149 void WorldSession::HandleCreatureQueryOpcode( WorldPacket & recv_data )
151 CHECK_PACKET_SIZE(recv_data,4+8);
153 uint32 entry;
154 recv_data >> entry;
156 CreatureInfo const *ci = objmgr.GetCreatureTemplate(entry);
157 if (ci)
160 std::string Name, SubName;
161 Name = ci->Name;
162 SubName = ci->SubName;
164 int loc_idx = GetSessionDbLocaleIndex();
165 if (loc_idx >= 0)
167 CreatureLocale const *cl = objmgr.GetCreatureLocale(entry);
168 if (cl)
170 if (cl->Name.size() > size_t(loc_idx) && !cl->Name[loc_idx].empty())
171 Name = cl->Name[loc_idx];
172 if (cl->SubName.size() > size_t(loc_idx) && !cl->SubName[loc_idx].empty())
173 SubName = cl->SubName[loc_idx];
176 sLog.outDetail("WORLD: CMSG_CREATURE_QUERY '%s' - Entry: %u.", ci->Name, entry);
177 // guess size
178 WorldPacket data( SMSG_CREATURE_QUERY_RESPONSE, 100 );
179 data << uint32(entry); // creature entry
180 data << Name;
181 data << uint8(0) << uint8(0) << uint8(0); // name2, name3, name4, always empty
182 data << SubName;
183 data << ci->IconName; // "Directions" for guard, string for Icons 2.3.0
184 data << uint32(ci->type_flags); // flags wdbFeild7=wad flags1
185 data << uint32(ci->type);
186 data << uint32(ci->family); // family wdbFeild9
187 data << uint32(ci->rank); // rank wdbFeild10
188 data << uint32(ci->PetSpellDataId); // Id from CreatureSpellData.dbc wdbField12
189 data << uint32(ci->DisplayID_A); // modelid_male1
190 data << uint32(ci->DisplayID_H); // modelid_female1 ?
191 data << uint32(ci->DisplayID_A2); // modelid_male2 ?
192 data << uint32(ci->DisplayID_H2); // modelid_femmale2 ?
193 data << uint32(0); // new in 3.1
194 data << float(ci->unk16); // unk
195 data << float(ci->unk17); // unk
196 data << uint8(ci->RacialLeader);
197 for(uint32 i = 0; i < 4; ++i)
198 data << uint32(0); // added in 3.1
199 data << uint32(0); // added in 3.1
200 SendPacket( &data );
201 sLog.outDebug( "WORLD: Sent SMSG_CREATURE_QUERY_RESPONSE " );
203 else
205 uint64 guid;
206 recv_data >> guid;
208 sLog.outDebug("WORLD: CMSG_CREATURE_QUERY - NO CREATURE INFO! (GUID: %u, ENTRY: %u)",
209 GUID_LOPART(guid), entry);
210 WorldPacket data( SMSG_CREATURE_QUERY_RESPONSE, 4 );
211 data << uint32(entry | 0x80000000);
212 SendPacket( &data );
213 sLog.outDebug( "WORLD: Sent SMSG_CREATURE_QUERY_RESPONSE " );
217 /// Only _static_ data send in this packet !!!
218 void WorldSession::HandleGameObjectQueryOpcode( WorldPacket & recv_data )
220 CHECK_PACKET_SIZE(recv_data,4+8);
222 uint32 entryID;
223 recv_data >> entryID;
225 const GameObjectInfo *info = objmgr.GetGameObjectInfo(entryID);
226 if(info)
229 std::string Name;
230 std::string IconName;
231 std::string CastBarCaption;
233 Name = info->name;
234 IconName = info->IconName;
235 CastBarCaption = info->castBarCaption;
237 int loc_idx = GetSessionDbLocaleIndex();
238 if (loc_idx >= 0)
240 GameObjectLocale const *gl = objmgr.GetGameObjectLocale(entryID);
241 if (gl)
243 if (gl->Name.size() > size_t(loc_idx) && !gl->Name[loc_idx].empty())
244 Name = gl->Name[loc_idx];
245 if (gl->CastBarCaption.size() > size_t(loc_idx) && !gl->CastBarCaption[loc_idx].empty())
246 CastBarCaption = gl->CastBarCaption[loc_idx];
249 sLog.outDetail("WORLD: CMSG_GAMEOBJECT_QUERY '%s' - Entry: %u. ", info->name, entryID);
250 WorldPacket data ( SMSG_GAMEOBJECT_QUERY_RESPONSE, 150 );
251 data << uint32(entryID);
252 data << uint32(info->type);
253 data << uint32(info->displayId);
254 data << Name;
255 data << uint8(0) << uint8(0) << uint8(0); // name2, name3, name4
256 data << IconName; // 2.0.3, string. Icon name to use instead of default icon for go's (ex: "Attack" makes sword)
257 data << CastBarCaption; // 2.0.3, string. Text will appear in Cast Bar when using GO (ex: "Collecting")
258 data << uint8(0); // 2.0.3, string
259 data.append(info->raw.data, 24);
260 data << float(info->size); // go size
261 for(uint32 i = 0; i < 4; ++i)
262 data << uint32(0); // added in 3.1
263 SendPacket( &data );
264 sLog.outDebug( "WORLD: Sent CMSG_GAMEOBJECT_QUERY " );
266 else
269 uint64 guid;
270 recv_data >> guid;
272 sLog.outDebug( "WORLD: CMSG_GAMEOBJECT_QUERY - Missing gameobject info for (GUID: %u, ENTRY: %u)",
273 GUID_LOPART(guid), entryID );
274 WorldPacket data ( SMSG_GAMEOBJECT_QUERY_RESPONSE, 4 );
275 data << uint32(entryID | 0x80000000);
276 SendPacket( &data );
277 sLog.outDebug( "WORLD: Sent CMSG_GAMEOBJECT_QUERY " );
281 void WorldSession::HandleCorpseQueryOpcode(WorldPacket & /*recv_data*/)
283 sLog.outDetail("WORLD: Received MSG_CORPSE_QUERY");
285 Corpse *corpse = GetPlayer()->GetCorpse();
287 if(!corpse)
289 WorldPacket data(MSG_CORPSE_QUERY, 1);
290 data << uint8(0); // corpse not found
291 SendPacket(&data);
292 return;
295 int32 mapid = corpse->GetMapId();
296 float x = corpse->GetPositionX();
297 float y = corpse->GetPositionY();
298 float z = corpse->GetPositionZ();
299 int32 corpsemapid = _player->GetMapId();
301 if(Map *map = MapManager::Instance().FindMap(corpse->GetMapId(), corpse->GetInstanceId()))
303 if(map->IsDungeon())
305 if(!map->GetEntrancePos(mapid, x, y))
306 return;
308 Map *entrance_map = MapManager::Instance().GetMap(mapid, _player);
309 if(!entrance_map)
310 return;
312 z = entrance_map->GetHeight(x, y, MAX_HEIGHT);
313 corpsemapid = corpse->GetMapId();
317 WorldPacket data(MSG_CORPSE_QUERY, 1+(5*4));
318 data << uint8(1); // corpse found
319 data << int32(mapid);
320 data << float(x);
321 data << float(y);
322 data << float(z);
323 data << int32(corpsemapid);
324 SendPacket(&data);
327 void WorldSession::HandleNpcTextQueryOpcode( WorldPacket & recv_data )
329 CHECK_PACKET_SIZE(recv_data,4+8);
331 uint32 textID;
332 uint64 guid;
334 recv_data >> textID;
335 sLog.outDetail("WORLD: CMSG_NPC_TEXT_QUERY ID '%u'", textID);
337 recv_data >> guid;
338 GetPlayer()->SetUInt64Value(UNIT_FIELD_TARGET, guid);
340 GossipText const* pGossip = objmgr.GetGossipText(textID);
342 WorldPacket data( SMSG_NPC_TEXT_UPDATE, 100 ); // guess size
343 data << textID;
345 if (!pGossip)
347 for(uint32 i = 0; i < 8; ++i)
349 data << float(0);
350 data << "Greetings $N";
351 data << "Greetings $N";
352 data << uint32(0);
353 data << uint32(0);
354 data << uint32(0);
355 data << uint32(0);
356 data << uint32(0);
357 data << uint32(0);
358 data << uint32(0);
361 else
363 std::string Text_0[8], Text_1[8];
364 for (int i=0;i<8;++i)
366 Text_0[i]=pGossip->Options[i].Text_0;
367 Text_1[i]=pGossip->Options[i].Text_1;
370 int loc_idx = GetSessionDbLocaleIndex();
371 if (loc_idx >= 0)
373 NpcTextLocale const *nl = objmgr.GetNpcTextLocale(textID);
374 if (nl)
376 for (int i=0;i<8;++i)
378 if (nl->Text_0[i].size() > size_t(loc_idx) && !nl->Text_0[i][loc_idx].empty())
379 Text_0[i]=nl->Text_0[i][loc_idx];
380 if (nl->Text_1[i].size() > size_t(loc_idx) && !nl->Text_1[i][loc_idx].empty())
381 Text_1[i]=nl->Text_1[i][loc_idx];
386 for (int i=0; i<8; ++i)
388 data << pGossip->Options[i].Probability;
390 if ( Text_0[i].empty() )
391 data << Text_1[i];
392 else
393 data << Text_0[i];
395 if ( Text_1[i].empty() )
396 data << Text_0[i];
397 else
398 data << Text_1[i];
400 data << pGossip->Options[i].Language;
402 for(int j = 0; j < 3; ++j)
404 data << pGossip->Options[i].Emotes[j]._Delay;
405 data << pGossip->Options[i].Emotes[j]._Emote;
410 SendPacket( &data );
412 sLog.outDebug( "WORLD: Sent SMSG_NPC_TEXT_UPDATE " );
415 void WorldSession::HandlePageTextQueryOpcode( WorldPacket & recv_data )
417 CHECK_PACKET_SIZE(recv_data,4);
419 uint32 pageID;
421 recv_data >> pageID;
422 sLog.outDetail("WORLD: Received CMSG_PAGE_TEXT_QUERY for pageID '%u'", pageID);
424 while (pageID)
426 PageText const *pPage = sPageTextStore.LookupEntry<PageText>( pageID );
427 // guess size
428 WorldPacket data( SMSG_PAGE_TEXT_QUERY_RESPONSE, 50 );
429 data << pageID;
431 if (!pPage)
433 data << "Item page missing.";
434 data << uint32(0);
435 pageID = 0;
437 else
439 std::string Text = pPage->Text;
441 int loc_idx = GetSessionDbLocaleIndex();
442 if (loc_idx >= 0)
444 PageTextLocale const *pl = objmgr.GetPageTextLocale(pageID);
445 if (pl)
447 if (pl->Text.size() > size_t(loc_idx) && !pl->Text[loc_idx].empty())
448 Text = pl->Text[loc_idx];
452 data << Text;
453 data << uint32(pPage->Next_Page);
454 pageID = pPage->Next_Page;
456 SendPacket( &data );
458 sLog.outDebug( "WORLD: Sent SMSG_PAGE_TEXT_QUERY_RESPONSE " );