[2771] Applied MaNGOS coding style (see trunk/bcpp.cfg).
[mangos-git.git] / src / game / Pet.cpp
blobffece6033acc5e0817590a79cf40b99eafe2aa27
1 /*
2 * Copyright (C) 2005,2006 MaNGOS <http://www.mangosproject.org/>
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 "Database/DatabaseEnv.h"
21 #include "Log.h"
22 #include "WorldSession.h"
23 #include "WorldPacket.h"
24 #include "ObjectMgr.h"
25 #include "Pet.h"
26 #include "MapManager.h"
28 Pet::Pet()
30 m_isPet = true;
31 m_name = "Pet";
32 m_actState = STATE_RA_FOLLOW;
33 m_fealty = 0;
34 for(uint32 i=0; i < CREATURE_MAX_SPELLS; i++)
35 m_spells[i]=0;
38 bool Pet::LoadPetFromDB( Unit* owner, uint32 petentry )
40 WorldPacket data;
41 uint32 ownerid = owner->GetGUIDLow();
43 QueryResult *result;
45 if(petentry)
46 // known entry
47 result = sDatabase.PQuery("SELECT `id`,`entry`,`owner`,`level`,`exp`,`nextlvlexp`,`spell1`,`spell2`,`spell3`,`spell4`,`action`,`fealty`,`loyalty`,`trainpoint`,`current` FROM `character_pet` WHERE `owner` = '%u' AND `entry` = '%u'",ownerid, petentry );
48 else
49 // current pet
50 result = sDatabase.PQuery("SELECT `id`,`entry`,`owner`,`level`,`exp`,`nextlvlexp`,`spell1`,`spell2`,`spell3`,`spell4`,`action`,`fealty`,`loyalty`,`trainpoint`,`current` FROM `character_pet` WHERE `owner` = '%u' AND `current` = '1'",ownerid );
52 if(!result)
53 return false;
55 Field *fields = result->Fetch();
57 // update for case "current = 1"
58 petentry = fields[1].GetUInt32();
59 if(!petentry)
60 return false;
62 float px, py, pz;
63 owner->GetClosePoint(NULL, px, py, pz);
64 uint32 guid=objmgr.GenerateLowGuid(HIGHGUID_UNIT);
65 if(!Create(guid, owner->GetMapId(), px, py, pz, owner->GetOrientation(), petentry))
67 delete result;
68 return false;
71 SetUInt32Value(UNIT_FIELD_FACTIONTEMPLATE,owner->getFaction());
73 CreatureInfo const *cinfo = GetCreatureInfo();
74 if(cinfo->type == CREATURE_TYPE_CRITTER)
76 AIM_Initialize();
77 MapManager::Instance().GetMap(owner->GetMapId())->Add((Creature*)this);
78 owner->SetPet(this);
79 if(owner->GetTypeId() == TYPEID_PLAYER)
81 ((Player*)owner)->PetSpellInitialize();
83 return true;
85 SetUInt64Value(UNIT_FIELD_SUMMONEDBY, owner->GetGUID());
86 uint32 petlevel=fields[3].GetUInt32();
87 SetUInt32Value(UNIT_NPC_FLAGS , 0);
88 if(owner->getClass() == CLASS_WARLOCK)
90 petlevel=owner->getLevel();
92 SetUInt32Value(UNIT_FIELD_BYTES_0,2048);
93 SetStat(STAT_STRENGTH,22);
94 SetStat(STAT_AGILITY,22);
95 SetStat(STAT_STAMINA,25);
96 SetStat(STAT_INTELLECT,28);
97 SetStat(STAT_SPIRIT,27);
98 SetArmor(petlevel*50);
100 else if(owner->getClass() == CLASS_HUNTER && cinfo->type == CREATURE_TYPE_BEAST)
102 SetUInt32Value(UNIT_FIELD_BYTES_1,(fields[12].GetUInt32()<<8));
103 SetFloatValue(UNIT_FIELD_MINDAMAGE, cinfo->mindmg + float(petlevel-cinfo->minlevel)*1.5f);
104 SetFloatValue(UNIT_FIELD_MAXDAMAGE, cinfo->maxdmg + float(petlevel-cinfo->minlevel)*1.5f);
105 SetUInt32Value(UNIT_MOD_CAST_SPEED, fields[13].GetUInt32() );
106 SetUInt32Value(UNIT_TRAINING_POINTS, (getLevel()<<16) + getUsedTrainPoint() );
107 SetUInt32Value(UNIT_FIELD_PETNUMBER, fields[0].GetUInt32() );
108 SetMaxPower(POWER_HAPPINESS,1000000);
109 SetPower( POWER_HAPPINESS,fields[11].GetUInt32());
110 setPowerType(POWER_FOCUS);
111 SetStat(STAT_STRENGTH,uint32(20+petlevel*1.55));
112 SetStat(STAT_AGILITY,uint32(20+petlevel*0.64));
113 SetStat(STAT_STAMINA,uint32(20+petlevel*1.27));
114 SetStat(STAT_INTELLECT,uint32(20+petlevel*0.18));
115 SetStat(STAT_SPIRIT,uint32(20+petlevel*0.36));
116 SetArmor(petlevel*50);
118 SetLevel( petlevel);
119 SetHealth( 28 + 10 * petlevel);
120 SetMaxHealth( 28 + 10 * petlevel);
121 SetMaxPower(POWER_MANA, 28 + 10 * petlevel);
122 SetPower( POWER_MANA, 28 + 10 * petlevel);
123 SetUInt32Value(UNIT_FIELD_PET_NAME_TIMESTAMP,0);
124 SetUInt32Value(UNIT_FIELD_PETEXPERIENCE, fields[4].GetUInt32());
125 SetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP, fields[5].GetUInt32());
126 SetUInt64Value(UNIT_FIELD_CREATEDBY, owner->GetGUID());
128 m_fealty = fields[11].GetUInt32();
130 m_spells[0] = fields[6].GetUInt32();
131 m_spells[1] = fields[7].GetUInt32();
132 m_spells[2] = fields[8].GetUInt32();
133 m_spells[3] = fields[9].GetUInt32();
134 m_actState = fields[10].GetUInt32();
136 // set current pet as current
137 if(fields[14].GetUInt32() != 1)
139 sDatabase.PExecute("UPDATE `character_pet` SET `current` = '0' WHERE `owner` = '%u' AND `current` <> '0' AND `entry` <> '%u'",ownerid, petentry);
140 sDatabase.PExecute("UPDATE `character_pet` SET `current` = '1' WHERE `owner` = '%u' AND `entry` = '%u'",ownerid, petentry);
143 delete result;
145 AIM_Initialize();
146 MapManager::Instance().GetMap(owner->GetMapId())->Add((Creature*)this);
147 owner->SetPet(this);
148 sLog.outDebug("New Pet has guid %u", GetGUIDLow());
150 //summon imp Template ID is 416
151 if(owner->GetTypeId() == TYPEID_PLAYER)
153 std::string name;
154 name = ((Player*)owner)->GetName();
155 name.append("'s Pet");
156 SetName( name );
157 ((Player*)owner)->PetSpellInitialize();
159 return true;
162 void Pet::SaveToDB()
164 if(!isPet())
165 return;
166 if(GetEntry())
167 return;
169 uint32 owner = GUID_LOPART(GetOwnerGUID());
170 sDatabase.PExecute("DELETE FROM `character_pet` WHERE `owner` = '%u' AND `entry` = '%u'", owner,GetEntry() );
171 sDatabase.PExecute("UPDATE `character_pet` SET `current` = 0 WHERE `owner` = '%u' AND `current` = 1", owner );
172 sDatabase.PExecute("INSERT INTO `character_pet` (`entry`,`owner`,`level`,`exp`,`nextlvlexp`,`spell1`,`spell2`,`spell3`,`spell4`,`action`,`fealty`,`loyalty`,`trainpoint`,`current`) VALUES (%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,%u,1)",
173 GetEntry(), owner, getLevel(), GetUInt32Value(UNIT_FIELD_PETEXPERIENCE), GetUInt32Value(UNIT_FIELD_PETNEXTLEVELEXP),
174 m_spells[0], m_spells[1], m_spells[2], m_spells[3], m_actState, GetPower(POWER_HAPPINESS),getloyalty(),getUsedTrainPoint());
177 void Pet::DeleteFromDB()
179 uint32 owner = GUID_LOPART(GetOwnerGUID());
180 sDatabase.PExecute("DELETE FROM `character_pet` WHERE `owner` = '%u' AND `current` = 1", owner );
183 /*void Pet::SendPetQuery()
185 Unit *player = GetOwner();
186 if(player->GetTypeId() != TYPEID_PLAYER)
187 return;
188 char *subname = "Pet";
189 CreatureInfo *ci = objmgr.GetCreatureTemplate(GetEntry());
191 WorldPacket data;
192 data.Initialize( SMSG_CREATURE_QUERY_RESPONSE );
193 data << (uint32)GetEntry();
194 data << m_name.c_str();
195 data << uint8(0) << uint8(0) << uint8(0);
196 data << subname;
198 uint32 wdbFeild11=0,wdbFeild12=0;
200 data << ci->flag1; //flags wdbFeild7=wad flags1
201 data << uint32(ci->type); //creatureType wdbFeild8
202 data << (uint32)ci->family; //family wdbFeild9
203 data << (uint32)ci->rank; //rank wdbFeild10
204 data << (uint32)wdbFeild11; //unknow wdbFeild11
205 data << (uint32)wdbFeild12; //unknow wdbFeild12
206 data << ci->DisplayID; //DisplayID wdbFeild13
208 data << (uint16)ci->civilian; //wdbFeild14
210 player->GetSession()->SendPacket( &data );
214 void Pet::setDeathState(DeathState s) // overwrite virtual Creature::setDeathState and Unit::setDeathState
216 Creature::setDeathState(s);
217 if(s == JUST_DIED)
219 Unit* owner = GetOwner();
220 if(owner && owner->GetTypeId() == TYPEID_PLAYER)
221 ((Player*)owner)->UnsummonPet();
222 else
224 if(owner)
225 owner->SetPet(0);
227 ObjectAccessor::Instance().AddObjectToRemoveList(this);
232 void Pet::Unsummon()
234 Unit* owner = GetOwner();
236 if(owner)
238 if(owner->GetTypeId()==TYPEID_PLAYER)
239 ((Player*)owner)->UnsummonPet(this);
240 else
242 owner->SetPet(0);
243 ObjectAccessor::Instance().AddObjectToRemoveList(this);
246 else
247 ObjectAccessor::Instance().AddObjectToRemoveList(this);