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
22 #include "UpdateMask.h"
23 #include "ObjectAccessor.h"
24 #include "Database/DatabaseEnv.h"
26 #include "GossipDef.h"
29 Corpse::Corpse(CorpseType type
) : WorldObject()
31 m_objectType
|= TYPEMASK_CORPSE
;
32 m_objectTypeId
= TYPEID_CORPSE
;
34 m_updateFlag
= (UPDATEFLAG_LOWGUID
| UPDATEFLAG_HIGHGUID
| UPDATEFLAG_HAS_POSITION
);
36 m_valuesCount
= CORPSE_END
;
49 void Corpse::AddToWorld()
51 ///- Register the corpse for guid lookup
52 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
56 void Corpse::RemoveFromWorld()
58 ///- Remove the corpse from the accessor
59 if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
60 Object::RemoveFromWorld();
63 bool Corpse::Create( uint32 guidlow
)
65 Object::_Create(guidlow
, 0, HIGHGUID_CORPSE
);
69 bool Corpse::Create( uint32 guidlow
, Player
*owner
)
71 SetInstanceId(owner
->GetInstanceId());
73 WorldObject::_Create(guidlow
, HIGHGUID_CORPSE
, owner
->GetMapId(), owner
->GetPhaseMask());
75 Relocate(owner
->GetPositionX(), owner
->GetPositionY(), owner
->GetPositionZ(), owner
->GetOrientation());
77 if(!IsPositionValid())
79 sLog
.outError("Corpse (guidlow %d, owner %s) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
80 guidlow
,owner
->GetName(),owner
->GetPositionX(), owner
->GetPositionY());
84 SetFloatValue( OBJECT_FIELD_SCALE_X
, 1 );
85 SetFloatValue( CORPSE_FIELD_POS_X
, GetPositionX() );
86 SetFloatValue( CORPSE_FIELD_POS_Y
, GetPositionY() );
87 SetFloatValue( CORPSE_FIELD_POS_Z
, GetPositionZ() );
88 SetFloatValue( CORPSE_FIELD_FACING
, GetOrientation() );
89 SetUInt64Value( CORPSE_FIELD_OWNER
, owner
->GetGUID() );
91 m_grid
= MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
96 void Corpse::SaveToDB()
98 // prevent DB data inconsistence problems and duplicates
99 CharacterDatabase
.BeginTransaction();
102 std::ostringstream ss
;
103 ss
<< "INSERT INTO corpse (guid,player,position_x,position_y,position_z,orientation,zone,map,data,time,corpse_type,instance,phaseMask) VALUES ("
104 << GetGUIDLow() << ", "
105 << GUID_LOPART(GetOwnerGUID()) << ", "
106 << GetPositionX() << ", "
107 << GetPositionY() << ", "
108 << GetPositionZ() << ", "
109 << GetOrientation() << ", "
110 << GetZoneId() << ", "
111 << GetMapId() << ", '";
112 for(uint16 i
= 0; i
< m_valuesCount
; i
++ )
113 ss
<< GetUInt32Value(i
) << " ";
115 << uint64(m_time
) <<", "
116 << uint32(GetType()) << ", "
117 << int(GetInstanceId()) << ", "
118 << uint16(GetPhaseMask()) << ")"; // prevent out of range error
119 CharacterDatabase
.Execute( ss
.str().c_str() );
120 CharacterDatabase
.CommitTransaction();
123 void Corpse::DeleteBonesFromWorld()
125 assert(GetType()==CORPSE_BONES
);
126 Corpse
* corpse
= ObjectAccessor::GetCorpse(*this, GetGUID());
130 sLog
.outError("Bones %u not found in world.", GetGUIDLow());
134 AddObjectToRemoveList();
137 void Corpse::DeleteFromDB()
139 if(GetType() == CORPSE_BONES
)
140 // only specific bones
141 CharacterDatabase
.PExecute("DELETE FROM corpse WHERE guid = '%d'", GetGUIDLow());
143 // all corpses (not bones)
144 CharacterDatabase
.PExecute("DELETE FROM corpse WHERE player = '%d' AND corpse_type <> '0'", GUID_LOPART(GetOwnerGUID()));
147 bool Corpse::LoadFromDB(uint32 guid
, QueryResult
*result
)
149 bool external
= (result
!= NULL
);
151 // 0 1 2 3 4 5 6 7 8 9
152 result
= CharacterDatabase
.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance,phaseMask FROM corpse WHERE guid = '%u'",guid
);
156 sLog
.outError("Corpse (GUID: %u) not found in table `corpse`, can't load. ",guid
);
160 Field
*fields
= result
->Fetch();
162 if(!LoadFromDB(guid
,fields
))
164 if (!external
) delete result
;
168 if (!external
) delete result
;
172 bool Corpse::LoadFromDB(uint32 guid
, Field
*fields
)
174 // 0 1 2 3 4 5 6 7 8 9
175 //result = CharacterDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance,phaseMask FROM corpse WHERE guid = '%u'",guid);
176 float positionX
= fields
[0].GetFloat();
177 float positionY
= fields
[1].GetFloat();
178 float positionZ
= fields
[2].GetFloat();
179 float ort
= fields
[3].GetFloat();
180 uint32 mapid
= fields
[4].GetUInt32();
182 if(!LoadValues( fields
[5].GetString() ))
184 sLog
.outError("Corpse #%d have broken data in `data` field. Can't be loaded.",guid
);
188 m_time
= time_t(fields
[6].GetUInt64());
189 m_type
= CorpseType(fields
[7].GetUInt32());
190 if(m_type
>= MAX_CORPSE_TYPE
)
192 sLog
.outError("Corpse (guidlow %d, owner %d) have wrong corpse type, not load.",GetGUIDLow(),GUID_LOPART(GetOwnerGUID()));
195 uint32 instanceid
= fields
[8].GetUInt32();
196 uint32 phaseMask
= fields
[9].GetUInt32();
198 // overwrite possible wrong/corrupted guid
199 SetUInt64Value(OBJECT_FIELD_GUID
, MAKE_NEW_GUID(guid
, 0, HIGHGUID_CORPSE
));
202 SetInstanceId(instanceid
);
204 SetPhaseMask(phaseMask
,false);
205 Relocate(positionX
,positionY
,positionZ
,ort
);
207 if(!IsPositionValid())
209 sLog
.outError("Corpse (guidlow %d, owner %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
210 GetGUIDLow(),GUID_LOPART(GetOwnerGUID()),GetPositionX(),GetPositionY());
214 m_grid
= MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
219 bool Corpse::isVisibleForInState(Player
const* u
, bool inVisibleList
) const
221 return IsInWorld() && u
->IsInWorld() && IsWithinDistInMap(u
,World::GetMaxVisibleDistanceForObject()+(inVisibleList
? World::GetVisibleObjectGreyDistance() : 0.0f
), false);