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 "MapManager.h"
24 #include "ObjectAccessor.h"
25 #include "Database/DatabaseEnv.h"
27 #include "WorldSession.h"
28 #include "WorldPacket.h"
29 #include "GossipDef.h"
32 Corpse::Corpse(CorpseType type
) : WorldObject()
34 m_objectType
|= TYPEMASK_CORPSE
;
35 m_objectTypeId
= TYPEID_CORPSE
;
37 m_updateFlag
= (UPDATEFLAG_LOWGUID
| UPDATEFLAG_HIGHGUID
| UPDATEFLAG_HAS_POSITION
);
39 m_valuesCount
= CORPSE_END
;
52 void Corpse::AddToWorld()
54 ///- Register the corpse for guid lookup
55 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
59 void Corpse::RemoveFromWorld()
61 ///- Remove the corpse from the accessor
62 if(IsInWorld()) ObjectAccessor::Instance().RemoveObject(this);
63 Object::RemoveFromWorld();
66 bool Corpse::Create( uint32 guidlow
)
68 Object::_Create(guidlow
, 0, HIGHGUID_CORPSE
);
72 bool Corpse::Create( uint32 guidlow
, Player
*owner
, uint32 mapid
, float x
, float y
, float z
, float ang
)
74 SetInstanceId(owner
->GetInstanceId());
76 WorldObject::_Create(guidlow
, HIGHGUID_CORPSE
, mapid
);
80 if(!IsPositionValid())
82 sLog
.outError("ERROR: Corpse (guidlow %d, owner %s) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
83 guidlow
,owner
->GetName(),x
,y
);
87 SetFloatValue( OBJECT_FIELD_SCALE_X
, 1 );
88 SetFloatValue( CORPSE_FIELD_POS_X
, x
);
89 SetFloatValue( CORPSE_FIELD_POS_Y
, y
);
90 SetFloatValue( CORPSE_FIELD_POS_Z
, z
);
91 SetFloatValue( CORPSE_FIELD_FACING
, ang
);
92 SetUInt64Value( CORPSE_FIELD_OWNER
, owner
->GetGUID() );
94 m_grid
= MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
99 void Corpse::SaveToDB()
101 // prevent DB data inconsistance 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) VALUES ("
107 << GetGUIDLow() << ", " << GUID_LOPART(GetOwnerGUID()) << ", " << GetPositionX() << ", " << GetPositionY() << ", " << GetPositionZ() << ", "
108 << GetOrientation() << ", " << GetZoneId() << ", " << GetMapId() << ", '";
109 for(uint16 i
= 0; i
< m_valuesCount
; i
++ )
110 ss
<< GetUInt32Value(i
) << " ";
111 ss
<< "'," << uint64(m_time
) <<", " << uint32(GetType()) << ", " << int(GetInstanceId()) << ")";
112 CharacterDatabase
.Execute( ss
.str().c_str() );
113 CharacterDatabase
.CommitTransaction();
116 void Corpse::DeleteBonesFromWorld()
118 assert(GetType()==CORPSE_BONES
);
119 Corpse
* corpse
= ObjectAccessor::GetCorpse(*this, GetGUID());
123 sLog
.outError("Bones %u not found in world.", GetGUIDLow());
127 AddObjectToRemoveList();
130 void Corpse::DeleteFromDB()
132 if(GetType() == CORPSE_BONES
)
133 // only specific bones
134 CharacterDatabase
.PExecute("DELETE FROM corpse WHERE guid = '%d'", GetGUIDLow());
136 // all corpses (not bones)
137 CharacterDatabase
.PExecute("DELETE FROM corpse WHERE player = '%d' AND corpse_type <> '0'", GUID_LOPART(GetOwnerGUID()));
140 bool Corpse::LoadFromDB(uint32 guid
, QueryResult
*result
)
142 bool external
= (result
!= NULL
);
145 result
= CharacterDatabase
.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance FROM corpse WHERE guid = '%u'",guid
);
149 sLog
.outError("ERROR: Corpse (GUID: %u) not found in table `corpse`, can't load. ",guid
);
153 Field
*fields
= result
->Fetch();
155 if(!LoadFromDB(guid
,fields
))
157 if (!external
) delete result
;
161 if (!external
) delete result
;
165 bool Corpse::LoadFromDB(uint32 guid
, Field
*fields
)
168 //result = CharacterDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance FROM corpse WHERE guid = '%u'",guid);
169 float positionX
= fields
[0].GetFloat();
170 float positionY
= fields
[1].GetFloat();
171 float positionZ
= fields
[2].GetFloat();
172 float ort
= fields
[3].GetFloat();
173 uint32 mapid
= fields
[4].GetUInt32();
175 if(!LoadValues( fields
[5].GetString() ))
177 sLog
.outError("ERROR: Corpse #%d have broken data in `data` field. Can't be loaded.",guid
);
181 m_time
= time_t(fields
[6].GetUInt64());
182 m_type
= CorpseType(fields
[7].GetUInt32());
183 if(m_type
>= MAX_CORPSE_TYPE
)
185 sLog
.outError("ERROR: Corpse (guidlow %d, owner %d) have wrong corpse type, not load.",GetGUIDLow(),GUID_LOPART(GetOwnerGUID()));
188 uint32 instanceid
= fields
[8].GetUInt32();
190 // overwrite possible wrong/corrupted guid
191 SetUInt64Value(OBJECT_FIELD_GUID
, MAKE_NEW_GUID(guid
, 0, HIGHGUID_CORPSE
));
194 SetInstanceId(instanceid
);
196 Relocate(positionX
,positionY
,positionZ
,ort
);
198 if(!IsPositionValid())
200 sLog
.outError("ERROR: Corpse (guidlow %d, owner %d) not created. Suggested coordinates isn't valid (X: %f Y: %f)",
201 GetGUIDLow(),GUID_LOPART(GetOwnerGUID()),GetPositionX(),GetPositionY());
205 m_grid
= MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
210 bool Corpse::isVisibleForInState(Player
const* u
, bool inVisibleList
) const
212 return IsInWorld() && u
->IsInWorld() && IsWithinDistInMap(u
,World::GetMaxVisibleDistanceForObject()+(inVisibleList
? World::GetVisibleObjectGreyDistance() : 0.0f
), false);