[7297] Fixed profession spells sorting in trainer spell list at client.
[getmangos.git] / src / game / NPCHandler.cpp
blobd762b9a775f1feaf3c135bad1a88fffc83851097
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 "WorldPacket.h"
23 #include "WorldSession.h"
24 #include "Opcodes.h"
25 #include "Log.h"
26 #include "World.h"
27 #include "ObjectMgr.h"
28 #include "SpellMgr.h"
29 #include "Player.h"
30 #include "GossipDef.h"
31 #include "SpellAuras.h"
32 #include "UpdateMask.h"
33 #include "ScriptCalls.h"
34 #include "ObjectAccessor.h"
35 #include "Creature.h"
36 #include "MapManager.h"
37 #include "Pet.h"
38 #include "BattleGroundMgr.h"
39 #include "BattleGround.h"
40 #include "Guild.h"
42 void WorldSession::HandleTabardVendorActivateOpcode( WorldPacket & recv_data )
44 CHECK_PACKET_SIZE(recv_data,8);
46 uint64 guid;
47 recv_data >> guid;
49 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, guid,UNIT_NPC_FLAG_TABARDDESIGNER);
50 if (!unit)
52 sLog.outDebug( "WORLD: HandleTabardVendorActivateOpcode - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(guid)) );
53 return;
56 // remove fake death
57 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
58 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
60 SendTabardVendorActivate(guid);
63 void WorldSession::SendTabardVendorActivate( uint64 guid )
65 WorldPacket data( MSG_TABARDVENDOR_ACTIVATE, 8 );
66 data << guid;
67 SendPacket( &data );
70 void WorldSession::HandleBankerActivateOpcode( WorldPacket & recv_data )
72 CHECK_PACKET_SIZE(recv_data,8);
74 uint64 guid;
76 sLog.outDebug( "WORLD: Received CMSG_BANKER_ACTIVATE" );
78 recv_data >> guid;
80 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, guid,UNIT_NPC_FLAG_BANKER);
81 if (!unit)
83 sLog.outDebug( "WORLD: HandleBankerActivateOpcode - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(guid)) );
84 return;
87 // remove fake death
88 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
89 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
91 SendShowBank(guid);
94 void WorldSession::SendShowBank( uint64 guid )
96 WorldPacket data( SMSG_SHOW_BANK, 8 );
97 data << guid;
98 SendPacket( &data );
101 void WorldSession::HandleTrainerListOpcode( WorldPacket & recv_data )
103 CHECK_PACKET_SIZE(recv_data,8);
105 uint64 guid;
107 recv_data >> guid;
108 SendTrainerList( guid );
111 void WorldSession::SendTrainerList( uint64 guid )
113 std::string str = GetMangosString(LANG_NPC_TAINER_HELLO);
114 SendTrainerList( guid, str );
117 void WorldSession::SendTrainerList( uint64 guid, const std::string& strTitle )
119 sLog.outDebug( "WORLD: SendTrainerList" );
121 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, guid,UNIT_NPC_FLAG_TRAINER);
122 if (!unit)
124 sLog.outDebug( "WORLD: SendTrainerList - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(guid)) );
125 return;
128 // remove fake death
129 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
130 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
132 // trainer list loaded at check;
133 if(!unit->isCanTrainingOf(_player,true))
134 return;
136 CreatureInfo const *ci = unit->GetCreatureInfo();
138 if (!ci)
140 sLog.outDebug( "WORLD: SendTrainerList - (GUID: %u) NO CREATUREINFO!",GUID_LOPART(guid) );
141 return;
144 TrainerSpellData const* trainer_spells = unit->GetTrainerSpells();
145 if(!trainer_spells)
147 sLog.outDebug( "WORLD: SendTrainerList - Training spells not found for creature (GUID: %u Entry: %u)",
148 GUID_LOPART(guid), unit->GetEntry());
149 return;
152 WorldPacket data( SMSG_TRAINER_LIST, 8+4+4+trainer_spells->spellList.size()*38 + strTitle.size()+1);
153 data << guid;
154 data << uint32(trainer_spells->trainerType);
156 size_t count_pos = data.wpos();
157 data << uint32(trainer_spells->spellList.size());
159 // reputation discount
160 float fDiscountMod = _player->GetReputationPriceDiscount(unit);
162 uint32 count = 0;
163 for(TrainerSpellMap::const_iterator itr = trainer_spells->spellList.begin(); itr != trainer_spells->spellList.end(); ++itr)
165 TrainerSpell const* tSpell = &itr->second;
167 if(!_player->IsSpellFitByClassAndRace(tSpell->learnedSpell))
168 continue;
170 ++count;
172 bool primary_prof_first_rank = spellmgr.IsPrimaryProfessionFirstRankSpell(tSpell->learnedSpell);
174 SpellChainNode const* chain_node = spellmgr.GetSpellChainNode(tSpell->learnedSpell);
176 data << uint32(tSpell->spell); // learned spell (or cast-spell in profession case)
177 data << uint8(_player->GetTrainerSpellState(tSpell));
178 data << uint32(floor(tSpell->spellCost * fDiscountMod));
180 data << uint32(primary_prof_first_rank ? 1 : 0); // primary prof. learn confirmation dialog
181 data << uint32(primary_prof_first_rank ? 1 : 0); // must be equal prev. field to have learn button in enabled state
182 data << uint8(tSpell->reqLevel);
183 data << uint32(tSpell->reqSkill);
184 data << uint32(tSpell->reqSkillValue);
185 data << uint32(chain_node ? (chain_node->prev ? chain_node->prev : chain_node->req) : 0);
186 data << uint32(chain_node && chain_node->prev ? chain_node->req : 0);
187 data << uint32(0);
190 data << strTitle;
192 data.put<uint32>(count_pos,count);
193 SendPacket( &data );
196 void WorldSession::HandleTrainerBuySpellOpcode( WorldPacket & recv_data )
198 CHECK_PACKET_SIZE(recv_data,8+4);
200 uint64 guid;
201 uint32 spellId = 0;
203 recv_data >> guid >> spellId;
204 sLog.outDebug( "WORLD: Received CMSG_TRAINER_BUY_SPELL NpcGUID=%u, learn spell id is: %u",uint32(GUID_LOPART(guid)), spellId );
206 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, guid, UNIT_NPC_FLAG_TRAINER);
207 if (!unit)
209 sLog.outDebug( "WORLD: HandleTrainerBuySpellOpcode - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(guid)) );
210 return;
213 // remove fake death
214 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
215 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
217 if(!unit->isCanTrainingOf(_player,true))
218 return;
220 // check present spell in trainer spell list
221 TrainerSpellData const* trainer_spells = unit->GetTrainerSpells();
222 if(!trainer_spells)
223 return;
225 // not found, cheat?
226 TrainerSpell const* trainer_spell = trainer_spells->Find(spellId);
227 if(!trainer_spell)
228 return;
230 // can't be learn, cheat? Or double learn with lags...
231 if(_player->GetTrainerSpellState(trainer_spell) != TRAINER_SPELL_GREEN)
232 return;
234 // apply reputation discount
235 uint32 nSpellCost = uint32(floor(trainer_spell->spellCost * _player->GetReputationPriceDiscount(unit)));
237 // check money requirement
238 if(_player->GetMoney() < nSpellCost )
239 return;
241 _player->ModifyMoney( -int32(nSpellCost) );
243 WorldPacket data(SMSG_PLAY_SPELL_VISUAL, 12); // visual effect on trainer
244 data << uint64(guid) << uint32(0xB3);
245 SendPacket(&data);
247 data.Initialize(SMSG_PLAY_SPELL_IMPACT, 12); // visual effect on player
248 data << uint64(_player->GetGUID()) << uint32(0x016A);
249 SendPacket(&data);
251 // learn explicitly or cast explicitly
252 if(trainer_spell->IsCastable ())
253 //FIXME: prof. spell entry in trainer list not marked gray until list re-open.
254 _player->CastSpell(_player,trainer_spell->spell,true);
255 else
256 _player->learnSpell(spellId,false);
258 data.Initialize(SMSG_TRAINER_BUY_SUCCEEDED, 12);
259 data << uint64(guid) << uint32(trainer_spell->spell);
260 SendPacket(&data);
263 void WorldSession::HandleGossipHelloOpcode( WorldPacket & recv_data )
265 CHECK_PACKET_SIZE(recv_data,8);
267 sLog.outDebug( "WORLD: Received CMSG_GOSSIP_HELLO" );
269 uint64 guid;
270 recv_data >> guid;
272 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, guid, UNIT_NPC_FLAG_NONE);
273 if (!unit)
275 sLog.outDebug( "WORLD: HandleGossipHelloOpcode - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(guid)) );
276 return;
279 // remove fake death
280 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
281 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
283 if( unit->isArmorer() || unit->isCivilian() || unit->isQuestGiver() || unit->isServiceProvider())
285 unit->StopMoving();
288 // If spiritguide, no need for gossip menu, just put player into resurrect queue
289 if (unit->isSpiritGuide())
291 BattleGround *bg = _player->GetBattleGround();
292 if(bg)
294 bg->AddPlayerToResurrectQueue(unit->GetGUID(), _player->GetGUID());
295 sBattleGroundMgr.SendAreaSpiritHealerQueryOpcode(_player, bg, unit->GetGUID());
296 return;
300 if(!Script->GossipHello( _player, unit ))
302 _player->TalkedToCreature(unit->GetEntry(),unit->GetGUID());
303 unit->prepareGossipMenu(_player);
304 unit->sendPreparedGossip(_player);
308 void WorldSession::HandleGossipSelectOptionOpcode( WorldPacket & recv_data )
310 CHECK_PACKET_SIZE(recv_data,8+4+4);
312 sLog.outDebug("WORLD: CMSG_GOSSIP_SELECT_OPTION");
314 uint32 option;
315 uint32 unk;
316 uint64 guid;
317 std::string code = "";
319 recv_data >> guid >> unk >> option;
321 if(_player->PlayerTalkClass->GossipOptionCoded( option ))
323 // recheck
324 CHECK_PACKET_SIZE(recv_data,8+4+1);
325 sLog.outBasic("reading string");
326 recv_data >> code;
327 sLog.outBasic("string read: %s", code.c_str());
330 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, guid, UNIT_NPC_FLAG_NONE);
331 if (!unit)
333 sLog.outDebug( "WORLD: HandleGossipSelectOptionOpcode - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(guid)) );
334 return;
337 // remove fake death
338 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
339 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
341 if(!code.empty())
343 if (!Script->GossipSelectWithCode(_player, unit, _player->PlayerTalkClass->GossipOptionSender (option), _player->PlayerTalkClass->GossipOptionAction( option ), code.c_str()))
344 unit->OnGossipSelect (_player, option);
346 else
348 if (!Script->GossipSelect (_player, unit, _player->PlayerTalkClass->GossipOptionSender (option), _player->PlayerTalkClass->GossipOptionAction (option)))
349 unit->OnGossipSelect (_player, option);
353 void WorldSession::HandleSpiritHealerActivateOpcode( WorldPacket & recv_data )
355 CHECK_PACKET_SIZE(recv_data,8);
357 sLog.outDebug("WORLD: CMSG_SPIRIT_HEALER_ACTIVATE");
359 uint64 guid;
361 recv_data >> guid;
363 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, guid, UNIT_NPC_FLAG_SPIRITHEALER);
364 if (!unit)
366 sLog.outDebug( "WORLD: HandleSpiritHealerActivateOpcode - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(guid)) );
367 return;
370 // remove fake death
371 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
372 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
374 SendSpiritResurrect();
377 void WorldSession::SendSpiritResurrect()
379 _player->ResurrectPlayer(0.5f, true);
381 _player->DurabilityLossAll(0.25f,true);
383 // get corpse nearest graveyard
384 WorldSafeLocsEntry const *corpseGrave = NULL;
385 Corpse *corpse = _player->GetCorpse();
386 if(corpse)
387 corpseGrave = objmgr.GetClosestGraveYard(
388 corpse->GetPositionX(), corpse->GetPositionY(), corpse->GetPositionZ(), corpse->GetMapId(), _player->GetTeam() );
390 // now can spawn bones
391 _player->SpawnCorpseBones();
393 // teleport to nearest from corpse graveyard, if different from nearest to player ghost
394 if(corpseGrave)
396 WorldSafeLocsEntry const *ghostGrave = objmgr.GetClosestGraveYard(
397 _player->GetPositionX(), _player->GetPositionY(), _player->GetPositionZ(), _player->GetMapId(), _player->GetTeam() );
399 if(corpseGrave != ghostGrave)
400 _player->TeleportTo(corpseGrave->map_id, corpseGrave->x, corpseGrave->y, corpseGrave->z, _player->GetOrientation());
401 // or update at original position
402 else
403 ObjectAccessor::UpdateVisibilityForPlayer(_player);
405 // or update at original position
406 else
407 ObjectAccessor::UpdateVisibilityForPlayer(_player);
409 _player->SaveToDB();
412 void WorldSession::HandleBinderActivateOpcode( WorldPacket & recv_data )
414 CHECK_PACKET_SIZE(recv_data,8);
416 uint64 npcGUID;
417 recv_data >> npcGUID;
419 if(!GetPlayer()->isAlive())
420 return;
422 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, npcGUID,UNIT_NPC_FLAG_INNKEEPER);
423 if (!unit)
425 sLog.outDebug( "WORLD: HandleBinderActivateOpcode - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(npcGUID)) );
426 return;
429 // remove fake death
430 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
431 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
433 SendBindPoint(unit);
436 void WorldSession::SendBindPoint(Creature *npc)
438 uint32 bindspell = 3286;
440 // update sql homebind
441 CharacterDatabase.PExecute("UPDATE character_homebind SET map = '%u', zone = '%u', position_x = '%f', position_y = '%f', position_z = '%f' WHERE guid = '%u'", _player->GetMapId(), _player->GetZoneId(), _player->GetPositionX(), _player->GetPositionY(), _player->GetPositionZ(), _player->GetGUIDLow());
442 _player->m_homebindMapId = _player->GetMapId();
443 _player->m_homebindZoneId = _player->GetZoneId();
444 _player->m_homebindX = _player->GetPositionX();
445 _player->m_homebindY = _player->GetPositionY();
446 _player->m_homebindZ = _player->GetPositionZ();
448 // send spell for bind 3286 bind magic
449 npc->CastSpell(_player, bindspell, true);
451 WorldPacket data( SMSG_TRAINER_BUY_SUCCEEDED, (8+4));
452 data << npc->GetGUID();
453 data << bindspell;
454 SendPacket( &data );
456 // binding
457 data.Initialize( SMSG_BINDPOINTUPDATE, (4+4+4+4+4) );
458 data << float(_player->GetPositionX());
459 data << float(_player->GetPositionY());
460 data << float(_player->GetPositionZ());
461 data << uint32(_player->GetMapId());
462 data << uint32(_player->GetZoneId());
463 SendPacket( &data );
465 DEBUG_LOG("New Home Position X is %f",_player->GetPositionX());
466 DEBUG_LOG("New Home Position Y is %f",_player->GetPositionY());
467 DEBUG_LOG("New Home Position Z is %f",_player->GetPositionZ());
468 DEBUG_LOG("New Home MapId is %u",_player->GetMapId());
469 DEBUG_LOG("New Home ZoneId is %u",_player->GetZoneId());
471 // zone update
472 data.Initialize( SMSG_PLAYERBOUND, 8+4 );
473 data << uint64(_player->GetGUID());
474 data << uint32(_player->GetZoneId());
475 SendPacket( &data );
477 _player->PlayerTalkClass->CloseGossip();
480 //Need fix
481 void WorldSession::HandleListStabledPetsOpcode( WorldPacket & recv_data )
483 CHECK_PACKET_SIZE(recv_data,8);
485 sLog.outDebug("WORLD: Recv MSG_LIST_STABLED_PETS");
486 uint64 npcGUID;
488 recv_data >> npcGUID;
490 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, npcGUID, UNIT_NPC_FLAG_STABLEMASTER);
491 if (!unit)
493 sLog.outDebug( "WORLD: HandleListStabledPetsOpcode - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(npcGUID)) );
494 return;
497 // remove fake death
498 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
499 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
501 SendStablePet(npcGUID);
504 void WorldSession::SendStablePet(uint64 guid )
506 sLog.outDebug("WORLD: Recv MSG_LIST_STABLED_PETS Send.");
508 WorldPacket data(MSG_LIST_STABLED_PETS, 200); // guess size
509 data << uint64 ( guid );
511 Pet *pet = _player->GetPet();
513 data << uint8(0); // place holder for slot show number
514 data << uint8(GetPlayer()->m_stableSlots);
516 uint8 num = 0; // counter for place holder
518 // not let move dead pet in slot
519 if(pet && pet->isAlive() && pet->getPetType()==HUNTER_PET)
521 data << uint32(pet->GetCharmInfo()->GetPetNumber());
522 data << uint32(pet->GetEntry());
523 data << uint32(pet->getLevel());
524 data << pet->GetName(); // petname
525 data << uint8(0x01); // flags?, client slot 1 == current pet (0)
526 ++num;
529 // 0 1 2 3 4 5
530 QueryResult* result = CharacterDatabase.PQuery("SELECT owner, slot, id, entry, level, name FROM character_pet WHERE owner = '%u' AND slot > 0 AND slot < 5",_player->GetGUIDLow());
532 if(result)
536 Field *fields = result->Fetch();
538 data << uint32(fields[2].GetUInt32()); // petnumber
539 data << uint32(fields[3].GetUInt32()); // creature entry
540 data << uint32(fields[4].GetUInt32()); // level
541 data << fields[5].GetString(); // name
542 data << uint8(fields[1].GetUInt32()+1); // slot
544 ++num;
545 }while( result->NextRow() );
547 delete result;
550 data.put<uint8>(8, num); // set real data to placeholder
551 SendPacket(&data);
554 void WorldSession::HandleStablePet( WorldPacket & recv_data )
556 CHECK_PACKET_SIZE(recv_data, 8);
558 sLog.outDebug("WORLD: Recv CMSG_STABLE_PET");
559 uint64 npcGUID;
561 recv_data >> npcGUID;
563 if(!GetPlayer()->isAlive())
564 return;
566 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, npcGUID, UNIT_NPC_FLAG_STABLEMASTER);
567 if (!unit)
569 sLog.outDebug( "WORLD: HandleStablePet - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(npcGUID)) );
570 return;
573 // remove fake death
574 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
575 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
577 Pet *pet = _player->GetPet();
579 WorldPacket data(SMSG_STABLE_RESULT, 200); // guess size
581 // can't place in stable dead pet
582 if(!pet||!pet->isAlive()||pet->getPetType()!=HUNTER_PET)
584 data << uint8(0x06);
585 SendPacket(&data);
586 return;
589 uint32 free_slot = 1;
591 QueryResult *result = CharacterDatabase.PQuery("SELECT owner,slot,id FROM character_pet WHERE owner = '%u' AND slot > 0 AND slot < 5 ORDER BY slot ",_player->GetGUIDLow());
592 if(result)
596 Field *fields = result->Fetch();
598 uint32 slot = fields[1].GetUInt32();
600 if(slot==free_slot) // this slot not free
601 ++free_slot;
602 }while( result->NextRow() );
604 delete result;
606 if( free_slot > 0 && free_slot <= GetPlayer()->m_stableSlots)
608 _player->RemovePet(pet,PetSaveMode(free_slot));
609 data << uint8(0x08);
611 else
612 data << uint8(0x06);
614 SendPacket(&data);
617 void WorldSession::HandleUnstablePet( WorldPacket & recv_data )
619 CHECK_PACKET_SIZE(recv_data, 8+4);
621 sLog.outDebug("WORLD: Recv CMSG_UNSTABLE_PET.");
622 uint64 npcGUID;
623 uint32 petnumber;
625 recv_data >> npcGUID >> petnumber;
627 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, npcGUID, UNIT_NPC_FLAG_STABLEMASTER);
628 if (!unit)
630 sLog.outDebug( "WORLD: HandleUnstablePet - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(npcGUID)) );
631 return;
634 // remove fake death
635 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
636 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
638 WorldPacket data(SMSG_STABLE_RESULT, 200); // guess size
640 Pet* pet = _player->GetPet();
641 if(pet && pet->isAlive())
643 uint8 i = 0x06;
644 data << uint8(i);
645 SendPacket(&data);
646 return;
649 // delete dead pet
650 if(pet)
651 _player->RemovePet(pet,PET_SAVE_AS_DELETED);
653 Pet *newpet = NULL;
655 QueryResult *result = CharacterDatabase.PQuery("SELECT entry FROM character_pet WHERE owner = '%u' AND id = '%u' AND slot > 0 AND slot < 5",_player->GetGUIDLow(),petnumber);
656 if(result)
658 Field *fields = result->Fetch();
659 uint32 petentry = fields[0].GetUInt32();
661 newpet = new Pet(HUNTER_PET);
662 if(!newpet->LoadPetFromDB(_player,petentry,petnumber))
664 delete newpet;
665 newpet = NULL;
667 delete result;
670 if(newpet)
671 data << uint8(0x09);
672 else
673 data << uint8(0x06);
674 SendPacket(&data);
677 void WorldSession::HandleBuyStableSlot( WorldPacket & recv_data )
679 CHECK_PACKET_SIZE(recv_data, 8);
681 sLog.outDebug("WORLD: Recv CMSG_BUY_STABLE_SLOT.");
682 uint64 npcGUID;
684 recv_data >> npcGUID;
686 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, npcGUID, UNIT_NPC_FLAG_STABLEMASTER);
687 if (!unit)
689 sLog.outDebug( "WORLD: HandleBuyStableSlot - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(npcGUID)) );
690 return;
693 // remove fake death
694 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
695 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
697 WorldPacket data(SMSG_STABLE_RESULT, 200);
699 if(GetPlayer()->m_stableSlots < 4) // max slots amount = 4
701 StableSlotPricesEntry const *SlotPrice = sStableSlotPricesStore.LookupEntry(GetPlayer()->m_stableSlots+1);
702 if(_player->GetMoney() >= SlotPrice->Price)
704 ++GetPlayer()->m_stableSlots;
705 _player->ModifyMoney(-int32(SlotPrice->Price));
706 data << uint8(0x0A); // success buy
708 else
709 data << uint8(0x06);
711 else
712 data << uint8(0x06);
714 SendPacket(&data);
717 void WorldSession::HandleStableRevivePet( WorldPacket &/* recv_data */)
719 sLog.outDebug("HandleStableRevivePet: Not implemented");
722 void WorldSession::HandleStableSwapPet( WorldPacket & recv_data )
724 CHECK_PACKET_SIZE(recv_data, 8+4);
726 sLog.outDebug("WORLD: Recv CMSG_STABLE_SWAP_PET.");
727 uint64 npcGUID;
728 uint32 pet_number;
730 recv_data >> npcGUID >> pet_number;
732 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, npcGUID, UNIT_NPC_FLAG_STABLEMASTER);
733 if (!unit)
735 sLog.outDebug( "WORLD: HandleStableSwapPet - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(npcGUID)) );
736 return;
739 // remove fake death
740 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
741 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
743 WorldPacket data(SMSG_STABLE_RESULT, 200); // guess size
745 Pet* pet = _player->GetPet();
747 if(!pet || pet->getPetType()!=HUNTER_PET)
748 return;
750 // find swapped pet slot in stable
751 QueryResult *result = CharacterDatabase.PQuery("SELECT slot,entry FROM character_pet WHERE owner = '%u' AND id = '%u'",_player->GetGUIDLow(),pet_number);
752 if(!result)
753 return;
755 Field *fields = result->Fetch();
757 uint32 slot = fields[0].GetUInt32();
758 uint32 petentry = fields[1].GetUInt32();
759 delete result;
761 // move alive pet to slot or delele dead pet
762 _player->RemovePet(pet,pet->isAlive() ? PetSaveMode(slot) : PET_SAVE_AS_DELETED);
764 // summon unstabled pet
765 Pet *newpet = new Pet;
766 if(!newpet->LoadPetFromDB(_player,petentry,pet_number))
768 delete newpet;
769 data << uint8(0x06);
771 else
772 data << uint8(0x09);
774 SendPacket(&data);
777 void WorldSession::HandleRepairItemOpcode( WorldPacket & recv_data )
779 CHECK_PACKET_SIZE(recv_data, 8+8+1);
781 sLog.outDebug("WORLD: CMSG_REPAIR_ITEM");
783 uint64 npcGUID, itemGUID;
784 uint8 guildBank; // new in 2.3.2, bool that means from guild bank money
786 recv_data >> npcGUID >> itemGUID >> guildBank;
788 Creature *unit = ObjectAccessor::GetNPCIfCanInteractWith(*_player, npcGUID, UNIT_NPC_FLAG_REPAIR);
789 if (!unit)
791 sLog.outDebug( "WORLD: HandleRepairItemOpcode - Unit (GUID: %u) not found or you can't interact with him.", uint32(GUID_LOPART(npcGUID)) );
792 return;
795 // remove fake death
796 if(GetPlayer()->hasUnitState(UNIT_STAT_DIED))
797 GetPlayer()->RemoveSpellsCausingAura(SPELL_AURA_FEIGN_DEATH);
799 // reputation discount
800 float discountMod = _player->GetReputationPriceDiscount(unit);
802 uint32 TotalCost = 0;
803 if (itemGUID)
805 sLog.outDebug("ITEM: Repair item, itemGUID = %u, npcGUID = %u", GUID_LOPART(itemGUID), GUID_LOPART(npcGUID));
807 Item* item = _player->GetItemByGuid(itemGUID);
809 if(item)
810 TotalCost= _player->DurabilityRepair(item->GetPos(),true,discountMod,guildBank>0?true:false);
812 else
814 sLog.outDebug("ITEM: Repair all items, npcGUID = %u", GUID_LOPART(npcGUID));
816 TotalCost = _player->DurabilityRepairAll(true,discountMod,guildBank>0?true:false);
818 if (guildBank)
820 uint32 GuildId = _player->GetGuildId();
821 if (!GuildId)
822 return;
823 Guild *pGuild = objmgr.GetGuildById(GuildId);
824 if (!pGuild)
825 return;
826 pGuild->LogBankEvent(GUILD_BANK_LOG_REPAIR_MONEY, 0, _player->GetGUIDLow(), TotalCost);
827 pGuild->SendMoneyInfo(this, _player->GetGUIDLow());