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_HIGHGUID
| UPDATEFLAG_HAS_POSITION
| UPDATEFLAG_POSITION
);
36 m_valuesCount
= CORPSE_END
;
49 void Corpse::AddToWorld()
51 ///- Register the corpse for guid lookup
53 ObjectAccessor::Instance().AddObject(this);
58 void Corpse::RemoveFromWorld()
60 ///- Remove the corpse from the accessor
62 ObjectAccessor::Instance().RemoveObject(this);
64 Object::RemoveFromWorld();
67 bool Corpse::Create( uint32 guidlow
)
69 Object::_Create(guidlow
, 0, HIGHGUID_CORPSE
);
73 bool Corpse::Create( uint32 guidlow
, Player
*owner
)
77 WorldObject::_Create(guidlow
, HIGHGUID_CORPSE
, owner
->GetPhaseMask());
78 Relocate(owner
->GetPositionX(), owner
->GetPositionY(), owner
->GetPositionZ(), owner
->GetOrientation());
80 //we need to assign owner's map for corpse
81 //in other way we will get a crash in Corpse::SaveToDB()
82 SetMap(owner
->GetMap());
84 if(!IsPositionValid())
86 sLog
.outError("Corpse (guidlow %d, owner %s) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
87 guidlow
, owner
->GetName(), owner
->GetPositionX(), owner
->GetPositionY());
91 SetFloatValue( OBJECT_FIELD_SCALE_X
, 1 );
92 SetUInt64Value( CORPSE_FIELD_OWNER
, owner
->GetGUID() );
94 m_grid
= MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
99 void Corpse::SaveToDB()
101 // prevent DB data inconsistence problems and duplicates
102 CharacterDatabase
.BeginTransaction();
105 std::ostringstream ss
;
106 ss
<< "INSERT INTO corpse (guid,player,position_x,position_y,position_z,orientation,zone,map,data,time,corpse_type,instance,phaseMask) VALUES ("
107 << GetGUIDLow() << ", "
108 << GUID_LOPART(GetOwnerGUID()) << ", "
109 << GetPositionX() << ", "
110 << GetPositionY() << ", "
111 << GetPositionZ() << ", "
112 << GetOrientation() << ", "
113 << GetZoneId() << ", "
114 << GetMapId() << ", '";
115 for(uint16 i
= 0; i
< m_valuesCount
; ++i
)
116 ss
<< GetUInt32Value(i
) << " ";
118 << uint64(m_time
) <<", "
119 << uint32(GetType()) << ", "
120 << int(GetInstanceId()) << ", "
121 << uint16(GetPhaseMask()) << ")"; // prevent out of range error
122 CharacterDatabase
.Execute( ss
.str().c_str() );
123 CharacterDatabase
.CommitTransaction();
126 void Corpse::DeleteBonesFromWorld()
128 assert(GetType() == CORPSE_BONES
);
129 Corpse
* corpse
= ObjectAccessor::GetCorpse(*this, GetGUID());
133 sLog
.outError("Bones %u not found in world.", GetGUIDLow());
137 AddObjectToRemoveList();
140 void Corpse::DeleteFromDB()
142 if(GetType() == CORPSE_BONES
)
143 // only specific bones
144 CharacterDatabase
.PExecute("DELETE FROM corpse WHERE guid = '%d'", GetGUIDLow());
146 // all corpses (not bones)
147 CharacterDatabase
.PExecute("DELETE FROM corpse WHERE player = '%d' AND corpse_type <> '0'", GUID_LOPART(GetOwnerGUID()));
150 bool Corpse::LoadFromDB(uint32 guid
, QueryResult
*result
)
152 bool external
= (result
!= NULL
);
154 // 0 1 2 3 4 5 6 7 8 9
155 result
= CharacterDatabase
.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance,phaseMask FROM corpse WHERE guid = '%u'",guid
);
159 sLog
.outError("Corpse (GUID: %u) not found in table `corpse`, can't load. ",guid
);
163 Field
*fields
= result
->Fetch();
165 if(!LoadFromDB(guid
, fields
))
179 bool Corpse::LoadFromDB(uint32 guid
, Field
*fields
)
181 // 0 1 2 3 4 5 6 7 8 9
182 //result = CharacterDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance,phaseMask FROM corpse WHERE guid = '%u'",guid);
183 float positionX
= fields
[0].GetFloat();
184 float positionY
= fields
[1].GetFloat();
185 float positionZ
= fields
[2].GetFloat();
186 float ort
= fields
[3].GetFloat();
187 uint32 mapid
= fields
[4].GetUInt32();
189 Object::_Create(guid
, 0, HIGHGUID_CORPSE
);
191 if(!LoadValues( fields
[5].GetString() ))
193 sLog
.outError("Corpse #%d have broken data in `data` field. Can't be loaded.",guid
);
197 m_time
= time_t(fields
[6].GetUInt64());
198 m_type
= CorpseType(fields
[7].GetUInt32());
200 if(m_type
>= MAX_CORPSE_TYPE
)
202 sLog
.outError("Corpse (guidlow %d, owner %d) have wrong corpse type, not load.",GetGUIDLow(),GUID_LOPART(GetOwnerGUID()));
206 uint32 instanceid
= fields
[8].GetUInt32();
207 uint32 phaseMask
= fields
[9].GetUInt32();
209 // overwrite possible wrong/corrupted guid
210 SetUInt64Value(OBJECT_FIELD_GUID
, MAKE_NEW_GUID(guid
, 0, HIGHGUID_CORPSE
));
213 SetLocationInstanceId(instanceid
);
214 SetLocationMapId(mapid
);
215 SetPhaseMask(phaseMask
, false);
216 Relocate(positionX
, positionY
, positionZ
, ort
);
218 if(!IsPositionValid())
220 sLog
.outError("Corpse (guidlow %d, owner %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
221 GetGUIDLow(), GUID_LOPART(GetOwnerGUID()), GetPositionX(), GetPositionY());
225 m_grid
= MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
230 bool Corpse::isVisibleForInState(Player
const* u
, bool inVisibleList
) const
232 return IsInWorld() && u
->IsInWorld() && IsWithinDistInMap(u
, World::GetMaxVisibleDistanceForObject() + (inVisibleList
? World::GetVisibleObjectGreyDistance() : 0.0f
), false);