[6982] Implemented gmlevel-based command security
[getmangos.git] / src / game / Corpse.cpp
blob83358c3b30b25b9c8b60078aaf1a8328864e4dad
1 /*
2 * Copyright (C) 2005-2008 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 "MapManager.h"
24 #include "ObjectAccessor.h"
25 #include "Database/DatabaseEnv.h"
26 #include "Opcodes.h"
27 #include "WorldSession.h"
28 #include "WorldPacket.h"
29 #include "GossipDef.h"
30 #include "World.h"
32 Corpse::Corpse(CorpseType type) : WorldObject()
34 m_objectType |= TYPEMASK_CORPSE;
35 m_objectTypeId = TYPEID_CORPSE;
36 // 2.3.2 - 0x58
37 m_updateFlag = (UPDATEFLAG_LOWGUID | UPDATEFLAG_HIGHGUID | UPDATEFLAG_HAS_POSITION);
39 m_valuesCount = CORPSE_END;
41 m_type = type;
43 m_time = time(NULL);
45 lootForBody = false;
48 Corpse::~Corpse()
52 void Corpse::AddToWorld()
54 ///- Register the corpse for guid lookup
55 if(!IsInWorld()) ObjectAccessor::Instance().AddObject(this);
56 Object::AddToWorld();
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);
69 return true;
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);
78 Relocate(x,y,z,ang);
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);
84 return false;
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());
96 return true;
99 void Corpse::SaveToDB()
101 // prevent DB data inconsistance 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) 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());
121 if (!corpse)
123 sLog.outError("Bones %u not found in world.", GetGUIDLow());
124 return;
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());
135 else
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);
143 if (!external)
144 // 0 1 2 3 4 5 6 7 8
145 result = CharacterDatabase.PQuery("SELECT position_x,position_y,position_z,orientation,map,data,time,corpse_type,instance FROM corpse WHERE guid = '%u'",guid);
147 if( ! result )
149 sLog.outError("ERROR: Corpse (GUID: %u) not found in table `corpse`, can't load. ",guid);
150 return false;
153 Field *fields = result->Fetch();
155 if(!LoadFromDB(guid,fields))
157 if (!external) delete result;
158 return false;
161 if (!external) delete result;
162 return true;
165 bool Corpse::LoadFromDB(uint32 guid, Field *fields)
167 // 0 1 2 3 4 5 6 7 8
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);
178 return false;
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()));
186 return false;
188 uint32 instanceid = fields[8].GetUInt32();
190 // overwrite possible wrong/corrupted guid
191 SetUInt64Value(OBJECT_FIELD_GUID, MAKE_NEW_GUID(guid, 0, HIGHGUID_CORPSE));
193 // place
194 SetInstanceId(instanceid);
195 SetMapId(mapid);
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());
202 return false;
205 m_grid = MaNGOS::ComputeGridPair(GetPositionX(), GetPositionY());
207 return true;
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);