[8483] Implement glyph 43361.
[getmangos.git] / src / game / Corpse.cpp
blob17f76c74de944249bb32559149153f61eb8d37d7
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 "Corpse.h"
21 #include "Player.h"
22 #include "UpdateMask.h"
23 #include "ObjectAccessor.h"
24 #include "Database/DatabaseEnv.h"
25 #include "Opcodes.h"
26 #include "GossipDef.h"
27 #include "World.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;
38 m_type = type;
40 m_time = time(NULL);
42 lootForBody = false;
45 Corpse::~Corpse()
49 void Corpse::AddToWorld()
51 ///- Register the corpse for guid lookup
52 if(!IsInWorld())
53 ObjectAccessor::Instance().AddObject(this);
55 Object::AddToWorld();
58 void Corpse::RemoveFromWorld()
60 ///- Remove the corpse from the accessor
61 if(IsInWorld())
62 ObjectAccessor::Instance().RemoveObject(this);
64 Object::RemoveFromWorld();
67 bool Corpse::Create( uint32 guidlow )
69 Object::_Create(guidlow, 0, HIGHGUID_CORPSE);
70 return true;
73 bool Corpse::Create( uint32 guidlow, Player *owner)
75 ASSERT(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());
88 return false;
91 SetFloatValue( OBJECT_FIELD_SCALE_X, 1 );
92 SetUInt64Value( CORPSE_FIELD_OWNER, owner->GetGUID() );
94 m_grid = MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
96 return true;
99 void Corpse::SaveToDB()
101 // prevent DB data inconsistence problems and duplicates
102 CharacterDatabase.BeginTransaction();
103 DeleteFromDB();
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) << " ";
117 ss << "',"
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());
131 if (!corpse)
133 sLog.outError("Bones %u not found in world.", GetGUIDLow());
134 return;
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());
145 else
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);
153 if (!external)
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);
157 if( !result )
159 sLog.outError("Corpse (GUID: %u) not found in table `corpse`, can't load. ",guid);
160 return false;
163 Field *fields = result->Fetch();
165 if(!LoadFromDB(guid, fields))
167 if (!external)
168 delete result;
170 return false;
173 if (!external)
174 delete result;
176 return true;
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);
194 return false;
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()));
203 return false;
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));
212 // place
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());
222 return false;
225 m_grid = MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
227 return true;
230 bool Corpse::isVisibleForInState(Player const* u, WorldObject const* viewPoint, bool inVisibleList) const
232 return IsInWorld() && u->IsInWorld() && IsWithinDistInMap(viewPoint, World::GetMaxVisibleDistanceForObject() + (inVisibleList ? World::GetVisibleObjectGreyDistance() : 0.0f), false);