Character re-customization fix
[getmangos.git] / src / game / QuestHandler.cpp
blobbbb3fa7649006ab7c784830427f15db73a4f1259
1 /*
2 * Copyright (C) 2005-2008 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 "Log.h"
21 #include "WorldPacket.h"
22 #include "WorldSession.h"
23 #include "Opcodes.h"
24 #include "World.h"
25 #include "ObjectMgr.h"
26 #include "Player.h"
27 #include "GossipDef.h"
28 #include "QuestDef.h"
29 #include "ObjectAccessor.h"
30 #include "ScriptCalls.h"
31 #include "Group.h"
33 void WorldSession::HandleQuestgiverStatusQueryOpcode( WorldPacket & recv_data )
35 CHECK_PACKET_SIZE(recv_data,8);
37 uint64 guid;
38 recv_data >> guid;
39 uint8 questStatus = DIALOG_STATUS_NONE;
40 uint8 defstatus = DIALOG_STATUS_NONE;
42 Object* questgiver = ObjectAccessor::GetObjectByTypeMask(*_player, guid,TYPEMASK_UNIT|TYPEMASK_GAMEOBJECT);
43 if(!questgiver)
45 sLog.outDetail("Error in CMSG_QUESTGIVER_STATUS_QUERY, called for not found questgiver (Typeid: %u GUID: %u)",GuidHigh2TypeId(GUID_HIPART(guid)),GUID_LOPART(guid));
46 return;
49 switch(questgiver->GetTypeId())
51 case TYPEID_UNIT:
53 sLog.outDebug( "WORLD: Received CMSG_QUESTGIVER_STATUS_QUERY for npc, guid = %u",uint32(GUID_LOPART(guid)) );
54 Creature* cr_questgiver=(Creature*)questgiver;
55 if( !cr_questgiver->IsHostileTo(_player)) // not show quest status to enemies
57 questStatus = Script->NPCDialogStatus(_player, cr_questgiver);
58 if( questStatus > 6 )
59 questStatus = getDialogStatus(_player, cr_questgiver, defstatus);
61 break;
63 case TYPEID_GAMEOBJECT:
65 sLog.outDebug( "WORLD: Received CMSG_QUESTGIVER_STATUS_QUERY for GameObject guid = %u",uint32(GUID_LOPART(guid)) );
66 GameObject* go_questgiver=(GameObject*)questgiver;
67 questStatus = Script->GODialogStatus(_player, go_questgiver);
68 if( questStatus > 6 )
69 questStatus = getDialogStatus(_player, go_questgiver, defstatus);
70 break;
72 default:
73 sLog.outError("QuestGiver called for unexpected type %u", questgiver->GetTypeId());
74 break;
77 //inform client about status of quest
78 _player->PlayerTalkClass->SendQuestGiverStatus(questStatus, guid);
81 void WorldSession::HandleQuestgiverHelloOpcode( WorldPacket & recv_data )
83 CHECK_PACKET_SIZE(recv_data,8);
85 uint64 guid;
86 recv_data >> guid;
88 sLog.outDebug ("WORLD: Received CMSG_QUESTGIVER_HELLO npc = %u", GUID_LOPART(guid));
90 Creature *pCreature = ObjectAccessor::GetNPCIfCanInteractWith(*_player, guid,UNIT_NPC_FLAG_NONE);
91 if (!pCreature)
93 sLog.outDebug ("WORLD: HandleQuestgiverHelloOpcode - Unit (GUID: %u) not found or you can't interact with him.",
94 GUID_LOPART(guid));
95 return;
98 // remove fake death
99 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
100 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
101 // Stop the npc if moving
102 pCreature->StopMoving();
104 if(Script->GossipHello( _player, pCreature ) )
105 return;
107 pCreature->prepareGossipMenu(_player);
108 pCreature->sendPreparedGossip(_player);
111 void WorldSession::HandleQuestgiverAcceptQuestOpcode( WorldPacket & recv_data )
113 CHECK_PACKET_SIZE(recv_data,8+4);
115 uint64 guid;
116 uint32 quest;
117 recv_data >> guid >> quest;
119 if(!GetPlayer()->isAlive())
120 return;
122 sLog.outDebug( "WORLD: Received CMSG_QUESTGIVER_ACCEPT_QUEST npc = %u, quest = %u",uint32(GUID_LOPART(guid)),quest );
124 Object* pObject = ObjectAccessor::GetObjectByTypeMask(*_player, guid,TYPEMASK_UNIT|TYPEMASK_GAMEOBJECT|TYPEMASK_ITEM|TYPEMASK_PLAYER);
126 // no or incorrect quest giver
127 if(!pObject
128 || (pObject->GetTypeId()!=TYPEID_PLAYER && !pObject->hasQuest(quest))
129 || (pObject->GetTypeId()==TYPEID_PLAYER && !((Player*)pObject)->CanShareQuest(quest))
132 _player->PlayerTalkClass->CloseGossip();
133 _player->SetDivider( 0 );
134 return;
137 Quest const* qInfo = objmgr.GetQuestTemplate(quest);
138 if ( qInfo )
140 // prevent cheating
141 if(!GetPlayer()->CanTakeQuest(qInfo,true) )
143 _player->PlayerTalkClass->CloseGossip();
144 _player->SetDivider( 0 );
145 return;
148 if( _player->GetDivider() != 0 )
150 Player *pPlayer = ObjectAccessor::FindPlayer( _player->GetDivider() );
151 if( pPlayer )
153 pPlayer->SendPushToPartyResponse( _player, QUEST_PARTY_MSG_ACCEPT_QUEST );
154 _player->SetDivider( 0 );
158 if( _player->CanAddQuest( qInfo, true ) )
160 _player->AddQuest( qInfo, pObject );
162 if ( _player->CanCompleteQuest( quest ) )
163 _player->CompleteQuest( quest );
165 switch(pObject->GetTypeId())
167 case TYPEID_UNIT:
168 Script->QuestAccept(_player, ((Creature*)pObject), qInfo );
169 break;
170 case TYPEID_ITEM:
171 case TYPEID_CONTAINER:
173 Script->ItemQuestAccept(_player, ((Item*)pObject), qInfo );
175 // destroy not required for quest finish quest starting item
176 bool destroyItem = true;
177 for(int i = 0; i < QUEST_OBJECTIVES_COUNT; i++)
179 if ((qInfo->ReqItemId[i] == ((Item*)pObject)->GetEntry()) && (((Item*)pObject)->GetProto()->MaxCount != 0))
181 destroyItem = false;
182 break;
186 if(destroyItem)
187 _player->DestroyItem(((Item*)pObject)->GetBagSlot(),((Item*)pObject)->GetSlot(),true);
189 break;
191 case TYPEID_GAMEOBJECT:
192 Script->GOQuestAccept(_player, ((GameObject*)pObject), qInfo );
193 break;
195 _player->PlayerTalkClass->CloseGossip();
197 if( qInfo->GetSrcSpell() > 0 )
198 _player->CastSpell( _player, qInfo->GetSrcSpell(), true);
200 return;
204 _player->PlayerTalkClass->CloseGossip();
207 void WorldSession::HandleQuestgiverQuestQueryOpcode( WorldPacket & recv_data )
209 CHECK_PACKET_SIZE(recv_data,8+4);
211 uint64 guid;
212 uint32 quest;
213 recv_data >> guid >> quest;
214 sLog.outDebug( "WORLD: Received CMSG_QUESTGIVER_QUERY_QUEST npc = %u, quest = %u",uint32(GUID_LOPART(guid)),quest );
216 // Verify that the guid is valid and is a questgiver or involved in the requested quest
217 Object* pObject = ObjectAccessor::GetObjectByTypeMask(*_player, guid,TYPEMASK_UNIT|TYPEMASK_GAMEOBJECT|TYPEMASK_ITEM);
218 if(!pObject||!pObject->hasQuest(quest) && !pObject->hasInvolvedQuest(quest))
220 _player->PlayerTalkClass->CloseGossip();
221 return;
224 Quest const* pQuest = objmgr.GetQuestTemplate(quest);
225 if ( pQuest )
227 _player->PlayerTalkClass->SendQuestGiverQuestDetails(pQuest, pObject->GetGUID(), true);
231 void WorldSession::HandleQuestQueryOpcode( WorldPacket & recv_data )
233 CHECK_PACKET_SIZE(recv_data,4);
235 uint32 quest;
236 recv_data >> quest;
237 sLog.outDebug( "WORLD: Received CMSG_QUEST_QUERY quest = %u",quest );
239 Quest const *pQuest = objmgr.GetQuestTemplate(quest);
240 if ( pQuest )
242 _player->PlayerTalkClass->SendQuestQueryResponse( pQuest );
246 void WorldSession::HandleQuestgiverChooseRewardOpcode( WorldPacket & recv_data )
248 CHECK_PACKET_SIZE(recv_data,8+4+4);
250 uint32 quest, reward;
251 uint64 guid;
252 recv_data >> guid >> quest >> reward;
254 if(reward >= QUEST_REWARD_CHOICES_COUNT)
256 sLog.outError("Error in CMSG_QUESTGIVER_CHOOSE_REWARD: player %s (guid %d) tried to get invalid reward (%u) (probably packet hacking)", _player->GetName(), _player->GetGUIDLow(), reward);
257 return;
260 if(!GetPlayer()->isAlive())
261 return;
263 sLog.outDebug( "WORLD: Received CMSG_QUESTGIVER_CHOOSE_REWARD npc = %u, quest = %u, reward = %u",uint32(GUID_LOPART(guid)),quest,reward );
265 Object* pObject = ObjectAccessor::GetObjectByTypeMask(*_player, guid,TYPEMASK_UNIT|TYPEMASK_GAMEOBJECT);
266 if(!pObject)
267 return;
269 if(!pObject->hasInvolvedQuest(quest))
270 return;
272 Quest const *pQuest = objmgr.GetQuestTemplate(quest);
273 if( pQuest )
275 if( _player->CanRewardQuest( pQuest, reward, true ) )
277 _player->RewardQuest( pQuest, reward, pObject );
279 switch(pObject->GetTypeId())
281 case TYPEID_UNIT:
282 if( !(Script->ChooseReward( _player, ((Creature*)pObject), pQuest, reward )) )
284 // Send next quest
285 if(Quest const* nextquest = _player->GetNextQuest( guid ,pQuest ) )
286 _player->PlayerTalkClass->SendQuestGiverQuestDetails(nextquest,guid,true);
288 break;
289 case TYPEID_GAMEOBJECT:
290 if( !Script->GOChooseReward( _player, ((GameObject*)pObject), pQuest, reward ) )
292 // Send next quest
293 if(Quest const* nextquest = _player->GetNextQuest( guid ,pQuest ) )
294 _player->PlayerTalkClass->SendQuestGiverQuestDetails(nextquest,guid,true);
296 break;
299 else
300 _player->PlayerTalkClass->SendQuestGiverOfferReward( pQuest, guid, true );
304 void WorldSession::HandleQuestgiverRequestRewardOpcode( WorldPacket & recv_data )
306 CHECK_PACKET_SIZE(recv_data,8+4);
308 uint32 quest;
309 uint64 guid;
310 recv_data >> guid >> quest;
312 if(!GetPlayer()->isAlive())
313 return;
315 sLog.outDebug( "WORLD: Received CMSG_QUESTGIVER_REQUEST_REWARD npc = %u, quest = %u",uint32(GUID_LOPART(guid)),quest );
317 Object* pObject = ObjectAccessor::GetObjectByTypeMask(*_player, guid,TYPEMASK_UNIT|TYPEMASK_GAMEOBJECT);
318 if(!pObject||!pObject->hasInvolvedQuest(quest))
319 return;
321 if ( _player->CanCompleteQuest( quest ) )
322 _player->CompleteQuest( quest );
324 if( _player->GetQuestStatus( quest ) != QUEST_STATUS_COMPLETE )
325 return;
327 if(Quest const *pQuest = objmgr.GetQuestTemplate(quest))
328 _player->PlayerTalkClass->SendQuestGiverOfferReward( pQuest, guid, true );
331 void WorldSession::HandleQuestgiverCancel(WorldPacket& /*recv_data*/ )
333 sLog.outDebug( "WORLD: Received CMSG_QUESTGIVER_CANCEL" );
335 _player->PlayerTalkClass->CloseGossip();
338 void WorldSession::HandleQuestLogSwapQuest(WorldPacket& recv_data )
340 CHECK_PACKET_SIZE(recv_data,1+1);
342 uint8 slot1, slot2;
343 recv_data >> slot1 >> slot2;
345 if(slot1 == slot2 || slot1 >= MAX_QUEST_LOG_SIZE || slot2 >= MAX_QUEST_LOG_SIZE)
346 return;
348 sLog.outDebug( "WORLD: Received CMSG_QUESTLOG_SWAP_QUEST slot 1 = %u, slot 2 = %u", slot1, slot2 );
350 GetPlayer()->SwapQuestSlot(slot1,slot2);
353 void WorldSession::HandleQuestLogRemoveQuest(WorldPacket& recv_data)
355 CHECK_PACKET_SIZE(recv_data,1);
357 uint8 slot;
358 recv_data >> slot;
360 sLog.outDebug( "WORLD: Received CMSG_QUESTLOG_REMOVE_QUEST slot = %u",slot );
362 if( slot < MAX_QUEST_LOG_SIZE )
364 if(uint32 quest = _player->GetQuestSlotQuestId(slot))
366 if(!_player->TakeQuestSourceItem( quest, true ))
367 return; // can't un-equip some items, reject quest cancel
369 _player->SetQuestStatus( quest, QUEST_STATUS_NONE);
372 _player->SetQuestSlot(slot, 0);
376 void WorldSession::HandleQuestConfirmAccept(WorldPacket& recv_data)
378 CHECK_PACKET_SIZE(recv_data,4);
380 uint32 quest;
381 recv_data >> quest;
383 sLog.outDebug( "WORLD: Received CMSG_QUEST_CONFIRM_ACCEPT quest = %u",quest );
386 void WorldSession::HandleQuestComplete(WorldPacket& recv_data)
388 CHECK_PACKET_SIZE(recv_data,8+4);
390 uint32 quest;
391 uint64 guid;
392 recv_data >> guid >> quest;
394 if(!GetPlayer()->isAlive())
395 return;
397 sLog.outDebug( "WORLD: Received CMSG_QUESTGIVER_COMPLETE_QUEST npc = %u, quest = %u",uint32(GUID_LOPART(guid)),quest );
399 Quest const *pQuest = objmgr.GetQuestTemplate(quest);
400 if( pQuest )
402 if( _player->GetQuestStatus( quest ) != QUEST_STATUS_COMPLETE )
404 if( pQuest->IsRepeatable() )
405 _player->PlayerTalkClass->SendQuestGiverRequestItems(pQuest, guid, _player->CanCompleteRepeatableQuest(pQuest), false);
406 else
407 _player->PlayerTalkClass->SendQuestGiverRequestItems(pQuest, guid, _player->CanRewardQuest(pQuest,false), false);
409 else
410 _player->PlayerTalkClass->SendQuestGiverRequestItems(pQuest, guid, _player->CanRewardQuest(pQuest,false), false);
414 void WorldSession::HandleQuestAutoLaunch(WorldPacket& /*recvPacket*/)
416 sLog.outDebug( "WORLD: Received CMSG_QUESTGIVER_QUEST_AUTOLAUNCH (Send your log to anakin if you see this message)" );
419 void WorldSession::HandleQuestPushToParty(WorldPacket& recvPacket)
421 CHECK_PACKET_SIZE(recvPacket,4);
423 uint32 quest;
424 recvPacket >> quest;
426 sLog.outDebug( "WORLD: Received CMSG_PUSHQUESTTOPARTY quest = %u", quest );
428 Quest const *pQuest = objmgr.GetQuestTemplate(quest);
429 if( pQuest )
431 if( _player->GetGroup() )
433 Group *pGroup = _player->GetGroup();
434 if( pGroup )
436 for(GroupReference *itr = pGroup->GetFirstMember(); itr != NULL; itr = itr->next())
438 Player *pPlayer = itr->getSource();
439 if (!pPlayer || pPlayer == _player) // skip self
440 continue;
442 _player->SendPushToPartyResponse(pPlayer, QUEST_PARTY_MSG_SHARING_QUEST);
444 if( !pPlayer->SatisfyQuestStatus( pQuest, false ) )
446 _player->SendPushToPartyResponse( pPlayer, QUEST_PARTY_MSG_HAVE_QUEST );
447 continue;
450 if( pPlayer->GetQuestStatus( quest ) == QUEST_STATUS_COMPLETE )
452 _player->SendPushToPartyResponse( pPlayer, QUEST_PARTY_MSG_FINISH_QUEST );
453 continue;
456 if( !pPlayer->CanTakeQuest( pQuest, false ) )
458 _player->SendPushToPartyResponse( pPlayer, QUEST_PARTY_MSG_CANT_TAKE_QUEST );
459 continue;
462 if( !pPlayer->SatisfyQuestLog( false ) )
464 _player->SendPushToPartyResponse( pPlayer, QUEST_PARTY_MSG_LOG_FULL );
465 continue;
468 if( pPlayer->GetDivider() != 0 )
470 _player->SendPushToPartyResponse( pPlayer, QUEST_PARTY_MSG_BUSY );
471 continue;
474 pPlayer->PlayerTalkClass->SendQuestGiverQuestDetails( pQuest, _player->GetGUID(), true );
475 pPlayer->SetDivider( _player->GetGUID() );
482 void WorldSession::HandleQuestPushResult(WorldPacket& recvPacket)
484 CHECK_PACKET_SIZE(recvPacket,8+1);
486 uint64 guid;
487 uint8 msg;
488 recvPacket >> guid >> msg;
490 sLog.outDebug( "WORLD: Received MSG_QUEST_PUSH_RESULT" );
492 if( _player->GetDivider() != 0 )
494 Player *pPlayer = ObjectAccessor::FindPlayer( _player->GetDivider() );
495 if( pPlayer )
497 WorldPacket data( MSG_QUEST_PUSH_RESULT, (8+1) );
498 data << uint64(guid);
499 data << uint8(msg); // valid values: 0-8
500 pPlayer->GetSession()->SendPacket(&data);
501 _player->SetDivider( 0 );
506 uint32 WorldSession::getDialogStatus(Player *pPlayer, Object* questgiver, uint32 defstatus)
508 uint32 result = defstatus;
510 QuestRelations const* qir;
511 QuestRelations const* qr;
513 switch(questgiver->GetTypeId())
515 case TYPEID_GAMEOBJECT:
517 qir = &objmgr.mGOQuestInvolvedRelations;
518 qr = &objmgr.mGOQuestRelations;
519 break;
521 case TYPEID_UNIT:
523 qir = &objmgr.mCreatureQuestInvolvedRelations;
524 qr = &objmgr.mCreatureQuestRelations;
525 break;
527 default:
528 //its imposible, but check ^)
529 sLog.outError("Warning: GetDialogStatus called for unexpected type %u", questgiver->GetTypeId());
530 return DIALOG_STATUS_NONE;
533 for(QuestRelations::const_iterator i = qir->lower_bound(questgiver->GetEntry()); i != qir->upper_bound(questgiver->GetEntry()); ++i )
535 uint32 result2 = 0;
536 uint32 quest_id = i->second;
537 Quest const *pQuest = objmgr.GetQuestTemplate(quest_id);
538 if ( !pQuest ) continue;
540 QuestStatus status = pPlayer->GetQuestStatus( quest_id );
541 if( (status == QUEST_STATUS_COMPLETE && !pPlayer->GetQuestRewardStatus(quest_id)) ||
542 (pQuest->IsAutoComplete() && pPlayer->CanTakeQuest(pQuest, false)) )
544 if ( pQuest->IsAutoComplete() && pQuest->IsRepeatable() )
545 result2 = DIALOG_STATUS_REWARD_REP;
546 else
547 result2 = DIALOG_STATUS_REWARD;
549 else if ( status == QUEST_STATUS_INCOMPLETE )
550 result2 = DIALOG_STATUS_INCOMPLETE;
552 if (result2 > result)
553 result = result2;
556 for(QuestRelations::const_iterator i = qr->lower_bound(questgiver->GetEntry()); i != qr->upper_bound(questgiver->GetEntry()); ++i )
558 uint32 result2 = 0;
559 uint32 quest_id = i->second;
560 Quest const *pQuest = objmgr.GetQuestTemplate(quest_id);
561 if ( !pQuest )
562 continue;
564 QuestStatus status = pPlayer->GetQuestStatus( quest_id );
565 if ( status == QUEST_STATUS_NONE )
567 if ( pPlayer->CanSeeStartQuest( pQuest ) )
569 if ( pPlayer->SatisfyQuestLevel(pQuest, false) )
571 if ( pQuest->IsAutoComplete() || (pQuest->IsRepeatable() && pPlayer->getQuestStatusMap()[quest_id].m_rewarded))
572 result2 = DIALOG_STATUS_REWARD_REP;
573 else if (pPlayer->getLevel() <= pQuest->GetQuestLevel() + sWorld.getConfig(CONFIG_QUEST_LOW_LEVEL_HIDE_DIFF) )
575 if (pQuest->HasFlag(QUEST_FLAGS_DAILY))
576 result2 = DIALOG_STATUS_AVAILABLE_REP;
577 else
578 result2 = DIALOG_STATUS_AVAILABLE;
580 else
581 result2 = DIALOG_STATUS_CHAT;
583 else
584 result2 = DIALOG_STATUS_UNAVAILABLE;
588 if (result2 > result)
589 result = result2;
592 if(questgiver->GetTypeId()==TYPEID_UNIT && ((Creature*)questgiver)->isCanTrainingAndResetTalentsOf(pPlayer) && result < DIALOG_STATUS_CHAT)
593 result = DIALOG_STATUS_CHAT;
595 return result;
598 void WorldSession::HandleQuestgiverStatusQueryMultipleOpcode(WorldPacket& /*recvPacket*/)
600 sLog.outDebug("WORLD: Received CMSG_QUESTGIVER_STATUS_MULTIPLE_QUERY");
602 uint32 count = 0;
604 WorldPacket data(SMSG_QUESTGIVER_STATUS_MULTIPLE, 4);
605 data << uint32(count); // placeholder
607 for(Player::ClientGUIDs::iterator itr = _player->m_clientGUIDs.begin(); itr != _player->m_clientGUIDs.end(); ++itr)
609 uint8 questStatus = DIALOG_STATUS_NONE;
610 uint8 defstatus = DIALOG_STATUS_NONE;
612 if(IS_CREATURE_GUID(*itr))
614 Creature *questgiver = ObjectAccessor::GetCreature(*_player, *itr);
615 if(!questgiver || questgiver->IsHostileTo(_player))
616 continue;
617 if(!questgiver->HasFlag(UNIT_NPC_FLAGS, UNIT_NPC_FLAG_QUESTGIVER))
618 continue;
619 questStatus = Script->NPCDialogStatus(_player, questgiver);
620 if( questStatus > 6 )
621 questStatus = getDialogStatus(_player, questgiver, defstatus);
623 data << uint64(questgiver->GetGUID());
624 data << uint8(questStatus);
625 ++count;
627 else if(IS_GAMEOBJECT_GUID(*itr))
629 GameObject *questgiver = ObjectAccessor::GetGameObject(*_player, *itr);
630 if(!questgiver)
631 continue;
632 if(questgiver->GetGoType() != GAMEOBJECT_TYPE_QUESTGIVER)
633 continue;
634 questStatus = Script->GODialogStatus(_player, questgiver);
635 if( questStatus > 6 )
636 questStatus = getDialogStatus(_player, questgiver, defstatus);
638 data << uint64(questgiver->GetGUID());
639 data << uint8(questStatus);
640 ++count;
644 data.put<uint32>(0, count); // write real count
645 SendPacket(&data);