[9581] Fixed apply damage reduction to melee/ranged damage.
[getmangos.git] / src / game / QueryHandler.cpp
blob5e3f8c85085ae233ecd54d38b96c03325729682c
1 /*
2 * Copyright (C) 2005-2010 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 "ObjectGuid.h"
30 #include "Player.h"
31 #include "UpdateMask.h"
32 #include "NPCHandler.h"
33 #include "Pet.h"
34 #include "MapManager.h"
36 void WorldSession::SendNameQueryOpcode(Player *p)
38 if(!p)
39 return;
40 // guess size
41 WorldPacket data( SMSG_NAME_QUERY_RESPONSE, (8+1+1+1+1+1+10) );
42 data << p->GetPackGUID(); // player guid
43 data << uint8(0); // added in 3.1; if > 1, then end of packet
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_BOOL_DECLINED_NAMES_USED) ?
65 // ------- Query Without Declined Names --------
66 // 0 1 2 3 4
67 "SELECT guid, name, race, gender, class "
68 "FROM characters WHERE guid = '%u'"
70 // --------- Query With Declined Names ---------
71 // 0 1 2 3 4
72 "SELECT characters.guid, name, race, gender, class, "
73 // 5 6 7 8 9
74 "genitive, dative, accusative, instrumental, prepositional "
75 "FROM characters LEFT JOIN character_declinedname ON characters.guid = character_declinedname.guid WHERE characters.guid = '%u'",
76 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 uint8 pRace = 0, pGender = 0, pClass = 0;
95 if(name == "")
96 name = session->GetMangosString(LANG_NON_EXIST_CHARACTER);
97 else
99 pRace = fields[2].GetUInt8();
100 pGender = fields[3].GetUInt8();
101 pClass = fields[4].GetUInt8();
103 // guess size
104 WorldPacket data( SMSG_NAME_QUERY_RESPONSE, (8+1+1+1+1+1+1+10) );
105 data.appendPackGUID(MAKE_NEW_GUID(guid, 0, HIGHGUID_PLAYER));
106 data << uint8(0); // added in 3.1; if > 1, then end of packet
107 data << name;
108 data << uint8(0); // realm name for cross realm BG usage
109 data << uint8(pRace); // race
110 data << uint8(pGender); // gender
111 data << uint8(pClass); // class
113 // if the first declined name field (5) is empty, the rest must be too
114 if(sWorld.getConfig(CONFIG_BOOL_DECLINED_NAMES_USED) && fields[5].GetCppString() != "")
116 data << uint8(1); // is declined
117 for(int i = 5; i < MAX_DECLINED_NAME_CASES+5; ++i)
118 data << fields[i].GetCppString();
120 else
121 data << uint8(0); // is not declined
123 session->SendPacket( &data );
124 delete result;
127 void WorldSession::HandleNameQueryOpcode( WorldPacket & recv_data )
129 uint64 guid;
131 recv_data >> guid;
133 Player *pChar = sObjectMgr.GetPlayer(guid);
135 if (pChar)
136 SendNameQueryOpcode(pChar);
137 else
138 SendNameQueryOpcodeFromDB(guid);
141 void WorldSession::HandleQueryTimeOpcode( WorldPacket & /*recv_data*/ )
143 SendQueryTimeResponse();
146 /// Only _static_ data send in this packet !!!
147 void WorldSession::HandleCreatureQueryOpcode( WorldPacket & recv_data )
149 uint32 entry;
150 recv_data >> entry;
151 uint64 guid;
152 recv_data >> guid;
154 CreatureInfo const *ci = ObjectMgr::GetCreatureTemplate(entry);
155 if (ci)
157 std::string Name, SubName;
158 Name = ci->Name;
159 SubName = ci->SubName;
161 int loc_idx = GetSessionDbLocaleIndex();
162 if (loc_idx >= 0)
164 CreatureLocale const *cl = sObjectMgr.GetCreatureLocale(entry);
165 if (cl)
167 if (cl->Name.size() > size_t(loc_idx) && !cl->Name[loc_idx].empty())
168 Name = cl->Name[loc_idx];
169 if (cl->SubName.size() > size_t(loc_idx) && !cl->SubName[loc_idx].empty())
170 SubName = cl->SubName[loc_idx];
173 sLog.outDetail("WORLD: CMSG_CREATURE_QUERY '%s' - Entry: %u.", ci->Name, entry);
174 // guess size
175 WorldPacket data( SMSG_CREATURE_QUERY_RESPONSE, 100 );
176 data << uint32(entry); // creature entry
177 data << Name;
178 data << uint8(0) << uint8(0) << uint8(0); // name2, name3, name4, always empty
179 data << SubName;
180 data << ci->IconName; // "Directions" for guard, string for Icons 2.3.0
181 data << uint32(ci->type_flags); // flags
182 data << uint32(ci->type); // CreatureType.dbc
183 data << uint32(ci->family); // CreatureFamily.dbc
184 data << uint32(ci->rank); // Creature Rank (elite, boss, etc)
185 data << uint32(ci->KillCredit[0]); // new in 3.1, kill credit
186 data << uint32(ci->KillCredit[1]); // new in 3.1, kill credit
187 data << uint32(ci->DisplayID_A[0]); // modelid_male1
188 data << uint32(ci->DisplayID_H[0]); // modelid_female1 ?
189 data << uint32(ci->DisplayID_A[1]); // modelid_male2 ?
190 data << uint32(ci->DisplayID_H[1]); // modelid_femmale2 ?
191 data << float(ci->unk16); // unk
192 data << float(ci->unk17); // unk
193 data << uint8(ci->RacialLeader);
194 for(uint32 i = 0; i < 6; ++i)
195 data << uint32(ci->questItems[i]); // itemId[6], quest drop
196 data << uint32(ci->movementId); // CreatureMovementInfo.dbc
197 SendPacket( &data );
198 sLog.outDebug( "WORLD: Sent SMSG_CREATURE_QUERY_RESPONSE" );
200 else
202 sLog.outDebug("WORLD: CMSG_CREATURE_QUERY - NO CREATURE INFO! (GUID: %u, ENTRY: %u)",
203 GUID_LOPART(guid), entry);
204 WorldPacket data( SMSG_CREATURE_QUERY_RESPONSE, 4 );
205 data << uint32(entry | 0x80000000);
206 SendPacket( &data );
207 sLog.outDebug( "WORLD: Sent SMSG_CREATURE_QUERY_RESPONSE" );
211 /// Only _static_ data send in this packet !!!
212 void WorldSession::HandleGameObjectQueryOpcode( WorldPacket & recv_data )
214 uint32 entryID;
215 recv_data >> entryID;
216 uint64 guid;
217 recv_data >> guid;
219 const GameObjectInfo *info = ObjectMgr::GetGameObjectInfo(entryID);
220 if(info)
222 std::string Name;
223 std::string IconName;
224 std::string CastBarCaption;
226 Name = info->name;
227 IconName = info->IconName;
228 CastBarCaption = info->castBarCaption;
230 int loc_idx = GetSessionDbLocaleIndex();
231 if (loc_idx >= 0)
233 GameObjectLocale const *gl = sObjectMgr.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 << uint32(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 << IconName; // 2.0.3, string. Icon name to use instead of default icon for go's (ex: "Attack" makes sword)
250 data << CastBarCaption; // 2.0.3, string. Text will appear in Cast Bar when using GO (ex: "Collecting")
251 data << info->unk1; // 2.0.3, string
252 data.append(info->raw.data, 24);
253 data << float(info->size); // go size
254 for(uint32 i = 0; i < 6; ++i)
255 data << uint32(info->questItems[i]); // itemId[6], quest drop
256 SendPacket( &data );
257 sLog.outDebug( "WORLD: Sent SMSG_GAMEOBJECT_QUERY_RESPONSE" );
259 else
261 sLog.outDebug( "WORLD: CMSG_GAMEOBJECT_QUERY - Missing gameobject info for (GUID: %u, ENTRY: %u)",
262 GUID_LOPART(guid), entryID );
263 WorldPacket data ( SMSG_GAMEOBJECT_QUERY_RESPONSE, 4 );
264 data << uint32(entryID | 0x80000000);
265 SendPacket( &data );
266 sLog.outDebug( "WORLD: Sent SMSG_GAMEOBJECT_QUERY_RESPONSE" );
270 void WorldSession::HandleCorpseQueryOpcode(WorldPacket & /*recv_data*/)
272 sLog.outDetail("WORLD: Received MSG_CORPSE_QUERY");
274 Corpse *corpse = GetPlayer()->GetCorpse();
276 if(!corpse)
278 WorldPacket data(MSG_CORPSE_QUERY, 1);
279 data << uint8(0); // corpse not found
280 SendPacket(&data);
281 return;
284 int32 mapid = corpse->GetMapId();
285 float x = corpse->GetPositionX();
286 float y = corpse->GetPositionY();
287 float z = corpse->GetPositionZ();
288 int32 corpsemapid = mapid;
290 // if corpse at different map
291 if(mapid != _player->GetMapId())
293 // search entrance map for proper show entrance
294 if(MapEntry const* corpseMapEntry = sMapStore.LookupEntry(mapid))
296 if(corpseMapEntry->IsDungeon() && corpseMapEntry->entrance_map >= 0)
298 // if corpse map have entrance
299 if(Map const* entranceMap = sMapMgr.CreateBaseMap(corpseMapEntry->entrance_map))
301 mapid = corpseMapEntry->entrance_map;
302 x = corpseMapEntry->entrance_x;
303 y = corpseMapEntry->entrance_y;
304 z = entranceMap->GetHeight(x, y, MAX_HEIGHT);
310 WorldPacket data(MSG_CORPSE_QUERY, 1+(6*4));
311 data << uint8(1); // corpse found
312 data << int32(mapid);
313 data << float(x);
314 data << float(y);
315 data << float(z);
316 data << int32(corpsemapid);
317 data << uint32(0); // unknown
318 SendPacket(&data);
321 void WorldSession::HandleNpcTextQueryOpcode( WorldPacket & recv_data )
323 uint32 textID;
324 uint64 guid;
326 recv_data >> textID;
327 sLog.outDetail("WORLD: CMSG_NPC_TEXT_QUERY ID '%u'", textID);
329 recv_data >> guid;
330 _player->SetTargetGUID(guid);
332 GossipText const* pGossip = sObjectMgr.GetGossipText(textID);
334 WorldPacket data( SMSG_NPC_TEXT_UPDATE, 100 ); // guess size
335 data << textID;
337 if (!pGossip)
339 for(uint32 i = 0; i < 8; ++i)
341 data << float(0);
342 data << "Greetings $N";
343 data << "Greetings $N";
344 data << uint32(0);
345 data << uint32(0);
346 data << uint32(0);
347 data << uint32(0);
348 data << uint32(0);
349 data << uint32(0);
350 data << uint32(0);
353 else
355 std::string Text_0[8], Text_1[8];
356 for (int i = 0; i < 8; ++i)
358 Text_0[i]=pGossip->Options[i].Text_0;
359 Text_1[i]=pGossip->Options[i].Text_1;
362 int loc_idx = GetSessionDbLocaleIndex();
363 if (loc_idx >= 0)
365 NpcTextLocale const *nl = sObjectMgr.GetNpcTextLocale(textID);
366 if (nl)
368 for (int i = 0; i < 8; ++i)
370 if (nl->Text_0[i].size() > size_t(loc_idx) && !nl->Text_0[i][loc_idx].empty())
371 Text_0[i]=nl->Text_0[i][loc_idx];
372 if (nl->Text_1[i].size() > size_t(loc_idx) && !nl->Text_1[i][loc_idx].empty())
373 Text_1[i]=nl->Text_1[i][loc_idx];
378 for (int i = 0; i < 8; ++i)
380 data << pGossip->Options[i].Probability;
382 if ( Text_0[i].empty() )
383 data << Text_1[i];
384 else
385 data << Text_0[i];
387 if ( Text_1[i].empty() )
388 data << Text_0[i];
389 else
390 data << Text_1[i];
392 data << pGossip->Options[i].Language;
394 for(int j = 0; j < 3; ++j)
396 data << pGossip->Options[i].Emotes[j]._Delay;
397 data << pGossip->Options[i].Emotes[j]._Emote;
402 SendPacket( &data );
404 sLog.outDebug( "WORLD: Sent SMSG_NPC_TEXT_UPDATE" );
407 void WorldSession::HandlePageTextQueryOpcode( WorldPacket & recv_data )
409 sLog.outDetail("WORLD: Received CMSG_PAGE_TEXT_QUERY");
410 recv_data.hexlike();
412 uint32 pageID;
413 recv_data >> pageID;
414 recv_data.read_skip<uint64>(); // guid
416 while (pageID)
418 PageText const *pPage = sPageTextStore.LookupEntry<PageText>( pageID );
419 // guess size
420 WorldPacket data( SMSG_PAGE_TEXT_QUERY_RESPONSE, 50 );
421 data << pageID;
423 if (!pPage)
425 data << "Item page missing.";
426 data << uint32(0);
427 pageID = 0;
429 else
431 std::string Text = pPage->Text;
433 int loc_idx = GetSessionDbLocaleIndex();
434 if (loc_idx >= 0)
436 PageTextLocale const *pl = sObjectMgr.GetPageTextLocale(pageID);
437 if (pl)
439 if (pl->Text.size() > size_t(loc_idx) && !pl->Text[loc_idx].empty())
440 Text = pl->Text[loc_idx];
444 data << Text;
445 data << uint32(pPage->Next_Page);
446 pageID = pPage->Next_Page;
448 SendPacket( &data );
450 sLog.outDebug( "WORLD: Sent SMSG_PAGE_TEXT_QUERY_RESPONSE" );
454 void WorldSession::HandleCorpseMapPositionQuery( WorldPacket & recv_data )
456 sLog.outDebug( "WORLD: Recv CMSG_CORPSE_MAP_POSITION_QUERY" );
458 uint32 unk;
459 recv_data >> unk;
461 WorldPacket data(SMSG_CORPSE_MAP_POSITION_QUERY_RESPONSE, 4+4+4+4);
462 data << float(0);
463 data << float(0);
464 data << float(0);
465 data << float(0);
466 SendPacket(&data);
469 void WorldSession::HandleQueryQuestsCompleted( WorldPacket & /*recv_data */)
471 uint32 count = 0;
473 WorldPacket data(SMSG_QUERY_QUESTS_COMPLETED_RESPONSE, 4+4*count);
474 data << uint32(count);
476 for(QuestStatusMap::const_iterator itr = _player->getQuestStatusMap().begin(); itr != _player->getQuestStatusMap().end(); ++itr)
478 if(itr->second.m_rewarded)
480 data << uint32(itr->first);
481 count++;
484 data.put<uint32>(0, count);
485 SendPacket(&data);
488 void WorldSession::HandleQuestPOIQuery(WorldPacket& recv_data)
490 uint32 count;
491 recv_data >> count; // quest count, max=25
493 if(count > MAX_QUEST_LOG_SIZE)
495 recv_data.rpos(recv_data.wpos()); // set to end to avoid warnings spam
496 return;
499 WorldPacket data(SMSG_QUEST_POI_QUERY_RESPONSE, 4+(4+4)*count);
500 data << uint32(count); // count
502 for(uint32 i = 0; i < count; ++i)
504 uint32 questId;
505 recv_data >> questId; // quest id
507 bool questOk = false;
509 uint16 questSlot = _player->FindQuestSlot(questId);
511 if(questSlot != MAX_QUEST_LOG_SIZE)
512 questOk =_player->GetQuestSlotQuestId(questSlot) == questId;
514 if(questOk)
516 QuestPOIVector const *POI = sObjectMgr.GetQuestPOIVector(questId);
518 if(POI)
520 data << uint32(questId); // quest ID
521 data << uint32(POI->size()); // POI count
523 int index = 0;
524 for(QuestPOIVector::const_iterator itr = POI->begin(); itr != POI->end(); ++itr)
526 data << uint32(index); // POI index
527 data << int32(itr->ObjectiveIndex); // objective index
528 data << uint32(itr->MapId); // mapid
529 data << uint32(itr->Unk1); // unknown
530 data << uint32(itr->Unk2); // unknown
531 data << uint32(itr->Unk3); // unknown
532 data << uint32(itr->Unk4); // unknown
533 data << uint32(itr->points.size()); // POI points count
535 for(std::vector<QuestPOIPoint>::const_iterator itr2 = itr->points.begin(); itr2 != itr->points.end(); ++itr2)
537 data << int32(itr2->x); // POI point x
538 data << int32(itr2->y); // POI point y
540 ++index;
543 else
545 data << uint32(questId); // quest ID
546 data << uint32(0); // POI count
549 else
551 data << uint32(questId); // quest ID
552 data << uint32(0); // POI count
556 data.hexlike();
557 SendPacket(&data);
560 void WorldSession::SendQueryTimeResponse()
562 WorldPacket data(SMSG_QUERY_TIME_RESPONSE, 4+4);
563 data << uint32(time(NULL));
564 data << uint32(sWorld.GetNextDailyQuestsResetTime() - time(NULL));
565 SendPacket(&data);